home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / mathinit.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  4KB  |  161 lines

  1. // Routines to initialize math tables for the integer math library
  2. // All code by Dave Stampe, last updated 23/12/93
  3.  
  4. /*
  5.  This code is part of the VR-386 project, created by Dave Stampe.
  6.  VR-386 is a desendent of REND386, created by Dave Stampe and
  7.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  8.  Stampre for VR-386.
  9.  
  10.  Copyright (c) 1994 by Dave Stampe:
  11.  May be freely used to write software for release into the public domain
  12.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  13.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  14.  this software or source code into their products!  Usually there is no
  15.  charge for under 50-100 items for low-cost or shareware products, and terms
  16.  are reasonable.  Any royalties are used for development, so equipment is
  17.  often acceptable payment.
  18.  
  19.  ATTRIBUTION:  If you use any part of this source code or the libraries
  20.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  21.  and any other authors in your documentation, source code, and at startup
  22.  of your program.  Let's keep the freeware ball rolling!
  23.  
  24.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  25.  REND386, improving programmer access by rewriting the code and supplying
  26.  a standard API.  If you write improvements, add new functions rather
  27.  than rewriting current functions.  This will make it possible to
  28.  include you improved code in the next API release.  YOU can help advance
  29.  VR-386.  Comments on the API are welcome.
  30.  
  31.  CONTACT: dstampe@psych.toronto.edu
  32. */
  33.  
  34. #include <stdlib.h>
  35. #include <alloc.h>
  36. #include <math.h>
  37. #include <stdio.h>
  38.  
  39. #include "intmath.h"
  40.  
  41.  
  42. /* this stuff moved out of integer core so that there are
  43.    no uncompilable TC references in integer core library */
  44.  
  45. #define XFSC 536870912   /* 2**29 for shifting float to <3.29> fixed format */
  46.  
  47. static float xfsc = XFSC;
  48.  
  49. // long sintable[258];    /* what these contain: for fast trig */
  50. // long atantable[258];
  51.  
  52.  
  53. #define SINESIZE 258
  54. #define ATANSIZE 258
  55.  
  56. long *sintable  = NULL;
  57. long *atantable = NULL;
  58.  
  59. static int fill_sine()
  60. {
  61.   int i;
  62.  
  63.   if(sintable) return 0;
  64.   sintable = malloc(SINESIZE * sizeof(long));
  65.   if(sintable==NULL) return 1;
  66.  
  67.   if(atantable) return 0;
  68.   atantable = malloc(ATANSIZE * sizeof(long));
  69.   if(atantable==NULL) return 1;
  70.  
  71.   for (i = 0; i < 256; i++)
  72.     sintable[i] = (XFSC * sin(3.14159/2/256 * i));
  73.   sintable[256] = XFSC;
  74.   sintable[257] = XFSC;
  75.  
  76.   for (i = 0; i < 256; i++)
  77.     atantable[i] = 180.0/3.14159*65536.0 * atan(i/256.0);
  78.   atantable[256] = atantable[257] = 45*65536L;
  79.  
  80.   return 0;
  81. }
  82.  
  83.  
  84. /* tables for sphere object clipping:            */
  85. /* these are needed for REND386's renderer pipeline only */
  86. /* tou can leave this out if using math elsewhere      */
  87.  
  88. /* zoom range: FOV = 2*atan(1/zoom)          */
  89. /* or about 150 to 7 degrees              */
  90.  
  91. // long sclip_C[800]; /* C = 1/sqrt(zoom^2 + 1)
  92. // long sclip_M[800]; /* M = zoom * C
  93.  
  94. #define CLIPSIZE 800
  95.  
  96. long *sclip_C = NULL;
  97. long *sclip_M = NULL;
  98.  
  99. static fill_sclip()
  100. {
  101.   int i;
  102.   float n;
  103.  
  104.   if(sclip_C) return 0;
  105.   sclip_C = malloc(CLIPSIZE * sizeof(long));
  106.   if(sclip_C==NULL) return 1;
  107.  
  108.   if(sclip_M) return 0;
  109.   sclip_M = malloc(CLIPSIZE * sizeof(long));
  110.   if(sclip_M==NULL) return 1;
  111.  
  112.   for (i = 0; i < 800; i++)
  113.     {
  114.       n = 1.0/sqrt((i/16.0)*(i/16.0) + 1);
  115.       sclip_C[i] = XFSC * n;
  116.       sclip_M[i] = XFSC * ((i/16.0) * n) ;
  117.     }
  118.   return 0;
  119. }
  120.  
  121. //  int sqrtable[1024];        // table for fast 16-bit sqrt lookup
  122.                 // expects index to be normalized
  123. #define SQRTSIZE 1024
  124.  
  125. int *sqrtable = NULL;
  126.  
  127. static fill_sqrt()
  128. {
  129.   int i;
  130.  
  131.   if(sqrtable) return 0;
  132.   sqrtable = malloc(SQRTSIZE * sizeof(int));
  133.   if(sqrtable==NULL) return 1;
  134.  
  135.   for (i = 0; i < 1024; i++)
  136.     sqrtable[i] = 1024*sqrt(i);
  137.   return 0;
  138. }
  139.  
  140.  
  141. static void free_math()
  142. {
  143.   if (sqrtable)  free(sqrtable);
  144.  
  145.   if (sclip_C)   free(sclip_C);  // these two for REND386 only
  146.   if (sclip_M)   free(sclip_M);  //
  147.  
  148.   if (sintable)  free(sintable);
  149.   if (atantable) free(atantable);
  150. }
  151.  
  152.  
  153. int init_math()        // returns 0 if OK, else out of memory
  154. {
  155.  atexit(free_math);
  156.  if(fill_sqrt()) return 1;
  157.  if(fill_sine()) return 1;
  158.  if(fill_sclip()) return 1;   // comment out if not using for REND386
  159.  return 0;
  160. }
  161.