home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Zone / VRZONE.ISO / mac / PC / REND386 / JIREND / MATHINIT.C < prev    next >
C/C++ Source or Header  |  1993-04-11  |  3KB  |  127 lines

  1. /* Routines to initialize math tables for greater speed later on.
  2.    Part of the REND386 package by Dave Stampe and Bernie Roehl.
  3.  */
  4.   
  5. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  6.    May be freely used to write software for release into the public domain;
  7.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  8.    for permission to incorporate any part of this software into their
  9.    products!
  10.  */
  11.   
  12. #include <stdlib.h>
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include "rend386.h"
  16. #include "intmath.h"
  17.   
  18. /* this stuff moved out of integer core so that there are
  19.    no uncompilable TC references in integer core library */
  20.   
  21. #define XFSC 536870912   /* 2**29 for shifting xform coeffs to long */
  22. static float xfsc = XFSC;
  23.   
  24. long sintable[258];
  25. long atantable[258];
  26.   
  27. void fill_sine(void)
  28. {
  29.    int i;
  30.   
  31.    for (i = 0; i < 256; i++)
  32.       sintable[i] = (XFSC * sin(3.14159/2/256 * i));
  33.    sintable[256] = XFSC;
  34.    sintable[257] = XFSC;
  35.   
  36.    for (i = 0; i < 256; i++)
  37.       atantable[i] = 180.0/3.14159*65536.0 * atan(i/256.0);
  38.    atantable[256] = atantable[257] = 45*65536L;
  39. }
  40.   
  41.   
  42. /* tables for sphere object clipping:   */
  43.   
  44. long sclip_C[800]; /* 1/sqrt(zoom^2 + 1) table        */
  45. long sclip_M[800]; /* zoom * C table  (table: i = 32*zoom) */
  46.   
  47. /* range: FOV = 2*atan(1/zoom)          */
  48. /* or about 150 to 7 degrees     */
  49.   
  50. void fill_sclip(void)
  51. {
  52.    int i;
  53.    float n;
  54.   
  55.    for (i = 0; i < 800; i++)
  56.    {
  57.       n = 1.0/sqrt((i/16.0)*(i/16.0) + 1);
  58.       sclip_C[i] = XFSC * n;
  59.       sclip_M[i] = XFSC * ((i/16.0) * n) ;
  60.    }
  61. }
  62.   
  63. int sqrtable[1024];
  64.   
  65. void fill_sqrt(void)
  66. {
  67.    int i;
  68.   
  69.    for (i = 0; i < 1024; i++)
  70.       sqrtable[i] = 1024*sqrt(i);
  71. }
  72.   
  73. long slow_magnitude(long a, long b) /* done the float way */
  74. {
  75.    float x = a;
  76.    float y = b;
  77.   
  78.    return sqrt(x*x + y*y);
  79. }
  80.   
  81.   
  82. /*  m[0][0] = cos(rz)*cos(ry) + sin(rz)*sin(rx)*sin(ry) */
  83. /*  m[0][1] = -sin(rz)*cos(rx);  */
  84. /*  m[0][2] = cos(rz)*sin(ry) + sin(rz)*sin(rx)*cos(ry); */
  85. /*  m[1][0] = sin(rz)*cos(ry) - cos(rz)*sin(rx)*sin(ry); */
  86. /*  m[1][1] = cos(rz)*cos(rx);   */
  87. /*  m[1][2] = -sin(rz)*sin(ry) - cos(rz)*sin(rx)*cos(ry); */
  88. /*  m[2][0] = cos(rx)*sin(ry); */
  89. /* m[2][1] = sinx;              */
  90. /*  m[2][2] = cos(rx)*cos(ry) ;  */
  91.   
  92. /* this uses float, so use only where speed is not important */
  93. #define XFLC 536870912.0
  94. #define XRLC 3754939.378
  95.   
  96.   
  97. /* makes matrix that will xform Z axis to given vector */
  98. void vector_to_matrix(MATRIX m, long x, long y, long z)
  99. {
  100.    float ya = atan2(x, z);
  101.    float xa = -asin(y/sqrt((float)x*x+(float)y*y+(float)z*z));
  102.   
  103.    std_matrix(m, xa*XRLC, ya*XRLC, 0, 0, 0, 0);
  104. }
  105.   
  106.   
  107. static void setlength(long length, long *x, long *y, long *z)
  108. {
  109.    float fx = *x;
  110.    float fy = *y;
  111.    float fz = *z;
  112.    float d = length/sqrt(fx*fx + fy*fy + fz*fz);
  113.    *x = d*fx;
  114.    *y = d*fy;
  115.    *z = d*fz;
  116. }
  117.   
  118. void fix_matrix_scale(MATRIX m) /* slow but sure: do once every 1000 matrix mults */
  119. {
  120.    setlength(XFLC, &m[0][0], &m[0][1], &m[0][2]);
  121.    setlength(XFLC, &m[1][0], &m[1][1], &m[1][2]);
  122.    setlength(XFLC, &m[2][0], &m[2][1], &m[2][2]);
  123.    setlength(XFLC, &m[0][0], &m[1][0], &m[2][0]);
  124.    setlength(XFLC, &m[0][1], &m[1][1], &m[2][1]);
  125.    setlength(XFLC, &m[0][2], &m[1][2], &m[2][2]);
  126. }
  127.