home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / crend5 / mathinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  3.4 KB  |  137 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.      ATTRIBUTION:  If you use any part of this source code or the libraries
  12.      in your projects, you must give attribution to REND386, Dave Stampe,
  13.      and Bernie Roehl in your documentation, source code, and at startup
  14.      of your program.  Let's keep the freeware ball rolling!
  15.  */
  16.  
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include "rend386.h"
  21. #include "intmath.h"
  22.  
  23. /* this stuff moved out of integer core so that there are
  24.    no uncompilable TC references in integer core library */
  25.  
  26. #define XFSC 536870912   /* 2**29 for shifting xform coeffs to long */
  27. static float xfsc = XFSC;
  28.  
  29. long sintable[258];
  30. long atantable[258];
  31.  
  32. fill_sine()
  33. {
  34.     int i;
  35.  
  36.     for (i = 0; i < 256; i++)
  37.         sintable[i] = (XFSC * sin(3.14159/2/256 * i));
  38.     sintable[256] = XFSC;
  39.     sintable[257] = XFSC;
  40.  
  41.     for (i = 0; i < 256; i++)
  42.         atantable[i] = 180.0/3.14159*65536.0 * atan(i/256.0);
  43.     atantable[256] = atantable[257] = 45*65536L;
  44.  
  45.     return 0;
  46. }
  47.  
  48.  
  49. /* tables for sphere object clipping:   */
  50.  
  51. long sclip_C[800]; /* 1/sqrt(zoom^2 + 1) table           */
  52. long sclip_M[800]; /* zoom * C table  (table: i = 32*zoom) */
  53.  
  54. /* range: FOV = 2*atan(1/zoom)          */
  55. /* or about 150 to 7 degrees         */
  56.  
  57. fill_sclip()
  58. {
  59.     int i;
  60.     float n;
  61.  
  62.     for (i = 0; i < 800; i++)
  63.     {
  64.         n = 1.0/sqrt((i/16.0)*(i/16.0) + 1);
  65.         sclip_C[i] = XFSC * n;
  66.         sclip_M[i] = XFSC * ((i/16.0) * n) ;
  67.     }
  68.     return 0;
  69. }
  70.  
  71. int sqrtable[1024];
  72.  
  73. fill_sqrt()
  74. {
  75.     int i;
  76.  
  77.     for (i = 0; i < 1024; i++)
  78.         sqrtable[i] = 1024*sqrt(i);
  79.     return 0;
  80. }
  81.  
  82.  
  83. long slow_magnitude(long a, long b) /* done the float way */
  84. {
  85.     float x = a;
  86.     float y = b;
  87.  
  88.     return sqrt(x*x + y*y);
  89. }
  90.  
  91.  
  92. /*  m[0][0] = cos(rz)*cos(ry) + sin(rz)*sin(rx)*sin(ry) */
  93. /*  m[0][1] = -sin(rz)*cos(rx);  */
  94. /*  m[0][2] = cos(rz)*sin(ry) + sin(rz)*sin(rx)*cos(ry); */
  95. /*  m[1][0] = sin(rz)*cos(ry) - cos(rz)*sin(rx)*sin(ry); */
  96. /*  m[1][1] = cos(rz)*cos(rx);   */
  97. /*  m[1][2] = -sin(rz)*sin(ry) - cos(rz)*sin(rx)*cos(ry); */
  98. /*  m[2][0] = cos(rx)*sin(ry); */
  99. /*    m[2][1] = sinx;              */
  100. /*  m[2][2] = cos(rx)*cos(ry) ;  */
  101.  
  102. /* this uses float, so use only where speed is not important */
  103. #define XFLC 536870912.0
  104. #define XRLC 3754939.378
  105.  
  106.  
  107. /* makes matrix that will xform Z axis to given vector */
  108. void vector_to_matrix(MATRIX m, long x, long y, long z)
  109. {
  110.     float ya = atan2(x, z);
  111.     float xa = -asin(y/sqrt((float)x*x+(float)y*y+(float)z*z));
  112.  
  113.     std_matrix(m, xa*XRLC, ya*XRLC, 0, 0, 0, 0);
  114. }
  115.  
  116.  
  117. static void setlength(long length, long *x, long *y, long *z)
  118. {
  119.     float fx = *x;
  120.     float fy = *y;
  121.     float fz = *z;
  122.     float d = length/sqrt(fx*fx + fy*fy + fz*fz);
  123.     *x = d*fx;
  124.     *y = d*fy;
  125.     *z = d*fz;
  126. }
  127.  
  128. void fix_matrix_scale(MATRIX m) /* slow but sure: do once every 1000 matrix mults */
  129. {
  130.     setlength(XFLC, &m[0][0], &m[0][1], &m[0][2]);
  131.     setlength(XFLC, &m[1][0], &m[1][1], &m[1][2]);
  132.     setlength(XFLC, &m[2][0], &m[2][1], &m[2][2]);
  133.     setlength(XFLC, &m[0][0], &m[1][0], &m[2][0]);
  134.     setlength(XFLC, &m[0][1], &m[1][1], &m[2][1]);
  135.     setlength(XFLC, &m[0][2], &m[1][2], &m[2][2]);
  136. }
  137.