home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / glquake_src / mathlib.c < prev    next >
C/C++ Source or Header  |  2000-02-13  |  13KB  |  593 lines

  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  12.  
  13. See the GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. */
  20. // mathlib.c -- math primitives
  21.  
  22. #include <math.h>
  23. #include "quakedef.h"
  24.  
  25. void Sys_Error (char *error, ...);
  26.  
  27. vec3_t vec3_origin = {0,0,0};
  28. int nanmask = 255<<23;
  29.  
  30. /*-----------------------------------------------------------------*/
  31.  
  32. #define DEG2RAD( a ) ( a * M_PI ) / 180.0F
  33.  
  34. void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
  35. {
  36.   float d;
  37.   vec3_t n;
  38.   float inv_denom;
  39.  
  40.   inv_denom = 1.0F / DotProduct( normal, normal );
  41.  
  42.   d = DotProduct( normal, p ) * inv_denom;
  43.  
  44.   n[0] = normal[0] * inv_denom;
  45.   n[1] = normal[1] * inv_denom;
  46.   n[2] = normal[2] * inv_denom;
  47.  
  48.   dst[0] = p[0] - d * n[0];
  49.   dst[1] = p[1] - d * n[1];
  50.   dst[2] = p[2] - d * n[2];
  51. }
  52.  
  53. /*
  54. ** assumes "src" is normalized
  55. */
  56. void PerpendicularVector( vec3_t dst, const vec3_t src )
  57. {
  58.   int pos;
  59.   int i;
  60.   float minelem = 1.0F;
  61.   vec3_t tempvec;
  62.  
  63.   /*
  64.   ** find the smallest magnitude axially aligned vector
  65.   */
  66.   for ( pos = 0, i = 0; i < 3; i++ )
  67.   {
  68.     if ( fabs( src[i] ) < minelem )
  69.     {
  70.       pos = i;
  71.       minelem = fabs( src[i] );
  72.     }
  73.   }
  74.   tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
  75.   tempvec[pos] = 1.0F;
  76.  
  77.   /*
  78.   ** project the point onto the plane defined by src
  79.   */
  80.   ProjectPointOnPlane( dst, tempvec, src );
  81.  
  82.   /*
  83.   ** normalize the result
  84.   */
  85.   VectorNormalize( dst );
  86. }
  87.  
  88. #ifdef _WIN32
  89. #pragma optimize( "", off )
  90. #endif
  91.  
  92.  
  93. void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
  94. {
  95.   float m[3][3];
  96.   float im[3][3];
  97.   float zrot[3][3];
  98.   float tmpmat[3][3];
  99.   float rot[3][3];
  100.   int i;
  101.   vec3_t vr, vup, vf;
  102.  
  103.   vf[0] = dir[0];
  104.   vf[1] = dir[1];
  105.   vf[2] = dir[2];
  106.  
  107.   PerpendicularVector( vr, dir );
  108.   CrossProduct( vr, vf, vup );
  109.  
  110.   m[0][0] = vr[0];
  111.   m[1][0] = vr[1];
  112.   m[2][0] = vr[2];
  113.  
  114.   m[0][1] = vup[0];
  115.   m[1][1] = vup[1];
  116.   m[2][1] = vup[2];
  117.  
  118.   m[0][2] = vf[0];
  119.   m[1][2] = vf[1];
  120.   m[2][2] = vf[2];
  121.  
  122.   memcpy( im, m, sizeof( im ) );
  123.  
  124.   im[0][1] = m[1][0];
  125.   im[0][2] = m[2][0];
  126.   im[1][0] = m[0][1];
  127.   im[1][2] = m[2][1];
  128.   im[2][0] = m[0][2];
  129.   im[2][1] = m[1][2];
  130.  
  131.   memset( zrot, 0, sizeof( zrot ) );
  132.   zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
  133.  
  134.   zrot[0][0] = cos( DEG2RAD( degrees ) );
  135.   zrot[0][1] = sin( DEG2RAD( degrees ) );
  136.   zrot[1][0] = -sin( DEG2RAD( degrees ) );
  137.   zrot[1][1] = cos( DEG2RAD( degrees ) );
  138.  
  139.   R_ConcatRotations( m, zrot, tmpmat );
  140.   R_ConcatRotations( tmpmat, im, rot );
  141.  
  142.   for ( i = 0; i < 3; i++ )
  143.   {
  144.     dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
  145.   }
  146. }
  147.  
  148. #ifdef _WIN32
  149. #pragma optimize( "", on )
  150. #endif
  151.  
  152. /*-----------------------------------------------------------------*/
  153.  
  154.  
  155. #if !defined(PPCASM) && !defined(M68KASM) 
  156. float anglemod(float a)
  157. {
  158. #if 0
  159.   if (a >= 0)
  160.     a -= 360*(int)(a/360);
  161.   else
  162.     a += 360*( 1 + (int)(-a/360) );
  163. #endif
  164.   a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
  165.   return a;
  166. }
  167.  
  168. /*
  169. ==================
  170. BOPS_Error
  171.  
  172. Split out like this for ASM to call.
  173. ==================
  174. */
  175. void BOPS_Error (void)
  176. {
  177.   Sys_Error ("BoxOnPlaneSide:  Bad signbits");
  178. }
  179.  
  180.  
  181. #if !id386
  182.  
  183. /*
  184. ==================
  185. BoxOnPlaneSide
  186.  
  187. Returns 1, 2, or 1 + 2
  188. ==================
  189. */
  190. int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, mplane_t *p)
  191. {
  192.   float dist1, dist2;
  193.   int   sides;
  194.  
  195. #if 0 // this is done by the BOX_ON_PLANE_SIDE macro before calling this
  196.     // function
  197. // fast axial cases
  198.   if (p->type < 3)
  199.   {
  200.     if (p->dist <= emins[p->type])
  201.       return 1;
  202.     if (p->dist >= emaxs[p->type])
  203.       return 2;
  204.     return 3;
  205.   }
  206. #endif
  207.   
  208. // general case
  209.   switch (p->signbits)
  210.   {
  211.   case 0:
  212. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  213. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  214.     break;
  215.   case 1:
  216. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  217. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  218.     break;
  219.   case 2:
  220. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  221. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  222.     break;
  223.   case 3:
  224. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  225. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  226.     break;
  227.   case 4:
  228. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  229. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  230.     break;
  231.   case 5:
  232. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  233. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  234.     break;
  235.   case 6:
  236. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  237. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  238.     break;
  239.   case 7:
  240. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  241. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  242.     break;
  243.   default:
  244.     dist1 = dist2 = 0;    // shut up compiler
  245.     BOPS_Error ();
  246.     break;
  247.   }
  248.  
  249. #if 0
  250.   int   i;
  251.   vec3_t  corners[2];
  252.  
  253.   for (i=0 ; i<3 ; i++)
  254.   {
  255.     if (plane->normal[i] < 0)
  256.     {
  257.       corners[0][i] = emins[i];
  258.       corners[1][i] = emaxs[i];
  259.     }
  260.     else
  261.     {
  262.       corners[1][i] = emins[i];
  263.       corners[0][i] = emaxs[i];
  264.     }
  265.   }
  266.   dist = DotProduct (plane->normal, corners[0]) - plane->dist;
  267.   dist2 = DotProduct (plane->normal, corners[1]) - plane->dist;
  268.   sides = 0;
  269.   if (dist1 >= 0)
  270.     sides = 1;
  271.   if (dist2 < 0)
  272.     sides |= 2;
  273.  
  274. #endif
  275.  
  276.   sides = 0;
  277.   if (dist1 >= p->dist)
  278.     sides = 1;
  279.   if (dist2 < p->dist)
  280.     sides |= 2;
  281.  
  282. #ifdef PARANOID
  283. if (sides == 0)
  284.   Sys_Error ("BoxOnPlaneSide: sides==0");
  285. #endif
  286.  
  287.   return sides;
  288. }
  289.  
  290. #endif
  291.  
  292.  
  293. void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
  294. {
  295.   float   angle;
  296.   float   sr, sp, sy, cr, cp, cy;
  297.   
  298.   angle = angles[YAW] * (M_PI*2 / 360);
  299.   sy = sin(angle);
  300.   cy = cos(angle);
  301.   angle = angles[PITCH] * (M_PI*2 / 360);
  302.   sp = sin(angle);
  303.   cp = cos(angle);
  304.   angle = angles[ROLL] * (M_PI*2 / 360);
  305.   sr = sin(angle);
  306.   cr = cos(angle);
  307.  
  308.   forward[0] = cp*cy;
  309.   forward[1] = cp*sy;
  310.   forward[2] = -sp;
  311.   right[0] = (-1*sr*sp*cy+-1*cr*-sy);
  312.   right[1] = (-1*sr*sp*sy+-1*cr*cy);
  313.   right[2] = -1*sr*cp;
  314.   up[0] = (cr*sp*cy+-sr*-sy);
  315.   up[1] = (cr*sp*sy+-sr*cy);
  316.   up[2] = cr*cp;
  317. }
  318.  
  319. int VectorCompare (vec3_t v1, vec3_t v2)
  320. {
  321.   int   i;
  322.   
  323.   for (i=0 ; i<3 ; i++)
  324.     if (v1[i] != v2[i])
  325.       return 0;
  326.       
  327.   return 1;
  328. }
  329.  
  330. void VectorMA (vec3_t veca, float scale, vec3_t vecb, vec3_t vecc)
  331. {
  332.   vecc[0] = veca[0] + scale*vecb[0];
  333.   vecc[1] = veca[1] + scale*vecb[1];
  334.   vecc[2] = veca[2] + scale*vecb[2];
  335. }
  336.  
  337.  
  338. vec_t _DotProduct (vec3_t v1, vec3_t v2)
  339. {
  340.   return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  341. }
  342.  
  343. void _VectorSubtract (vec3_t veca, vec3_t vecb, vec3_t out)
  344. {
  345.   out[0] = veca[0]-vecb[0];
  346.   out[1] = veca[1]-vecb[1];
  347.   out[2] = veca[2]-vecb[2];
  348. }
  349.  
  350. void _VectorAdd (vec3_t veca, vec3_t vecb, vec3_t out)
  351. {
  352.   out[0] = veca[0]+vecb[0];
  353.   out[1] = veca[1]+vecb[1];
  354.   out[2] = veca[2]+vecb[2];
  355. }
  356.  
  357. void _VectorCopy (vec3_t in, vec3_t out)
  358. {
  359.   out[0] = in[0];
  360.   out[1] = in[1];
  361.   out[2] = in[2];
  362. }
  363.  
  364. void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
  365. {
  366.   cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  367.   cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  368.   cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  369. }
  370.  
  371. double sqrt(double x);
  372.  
  373. vec_t Length(vec3_t v)
  374. {
  375.   int   i;
  376.   float length;
  377.   
  378.   length = 0;
  379.   for (i=0 ; i< 3 ; i++)
  380.     length += v[i]*v[i];
  381.   length = sqrt (length);   // FIXME
  382.  
  383.   return length;
  384. }
  385.  
  386. float VectorNormalize (vec3_t v)
  387. {
  388.   float length, ilength;
  389.  
  390.   length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  391.   length = sqrt (length);   // FIXME
  392.  
  393.   if (length)
  394.   {
  395.     ilength = 1/length;
  396.     v[0] *= ilength;
  397.     v[1] *= ilength;
  398.     v[2] *= ilength;
  399.   }
  400.     
  401.   return length;
  402.  
  403. }
  404.  
  405. void VectorInverse (vec3_t v)
  406. {
  407.   v[0] = -v[0];
  408.   v[1] = -v[1];
  409.   v[2] = -v[2];
  410. }
  411.  
  412. void VectorScale (vec3_t in, vec_t scale, vec3_t out)
  413. {
  414.   out[0] = in[0]*scale;
  415.   out[1] = in[1]*scale;
  416.   out[2] = in[2]*scale;
  417. }
  418. #endif /* !PPCASM/!M68KASM */
  419.  
  420.  
  421. #ifndef M68KASM
  422. int Q_log2(int val)
  423. {
  424.   int answer=0;
  425.   while (val>>=1)
  426.     answer++;
  427.   return answer;
  428. }
  429.  
  430.  
  431. /*
  432. ================
  433. R_ConcatRotations
  434. ================
  435. */
  436. void R_ConcatRotations (float in1[3][3], float in2[3][3], float out[3][3])
  437. {
  438.   out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  439.         in1[0][2] * in2[2][0];
  440.   out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  441.         in1[0][2] * in2[2][1];
  442.   out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  443.         in1[0][2] * in2[2][2];
  444.   out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  445.         in1[1][2] * in2[2][0];
  446.   out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  447.         in1[1][2] * in2[2][1];
  448.   out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  449.         in1[1][2] * in2[2][2];
  450.   out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  451.         in1[2][2] * in2[2][0];
  452.   out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  453.         in1[2][2] * in2[2][1];
  454.   out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  455.         in1[2][2] * in2[2][2];
  456. }
  457.  
  458.  
  459. /*
  460. ================
  461. R_ConcatTransforms
  462. ================
  463. */
  464. void R_ConcatTransforms (float in1[3][4], float in2[3][4], float out[3][4])
  465. {
  466.   out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  467.         in1[0][2] * in2[2][0];
  468.   out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  469.         in1[0][2] * in2[2][1];
  470.   out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  471.         in1[0][2] * in2[2][2];
  472.   out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] +
  473.         in1[0][2] * in2[2][3] + in1[0][3];
  474.   out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  475.         in1[1][2] * in2[2][0];
  476.   out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  477.         in1[1][2] * in2[2][1];
  478.   out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  479.         in1[1][2] * in2[2][2];
  480.   out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] +
  481.         in1[1][2] * in2[2][3] + in1[1][3];
  482.   out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  483.         in1[2][2] * in2[2][0];
  484.   out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  485.         in1[2][2] * in2[2][1];
  486.   out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  487.         in1[2][2] * in2[2][2];
  488.   out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] +
  489.         in1[2][2] * in2[2][3] + in1[2][3];
  490. }
  491. #endif
  492.  
  493.  
  494. #if !defined(PPCASM) && !defined(M68KASM) 
  495. /*
  496. ===================
  497. FloorDivMod
  498.  
  499. Returns mathematically correct (floor-based) quotient and remainder for
  500. numer and denom, both of which should contain no fractional part. The
  501. quotient must fit in 32 bits.
  502. ====================
  503. */
  504.  
  505. void FloorDivMod (double numer, double denom, int *quotient,
  506.     int *rem)
  507. {
  508.   int   q, r;
  509.   double  x;
  510.  
  511. #ifndef PARANOID
  512.   if (denom <= 0.0)
  513.     Sys_Error ("FloorDivMod: bad denominator %d\n", denom);
  514.  
  515. //  if ((floor(numer) != numer) || (floor(denom) != denom))
  516. //    Sys_Error ("FloorDivMod: non-integer numer or denom %f %f\n",
  517. //        numer, denom);
  518. #endif
  519.  
  520.   if (numer >= 0.0)
  521.   {
  522.  
  523.     x = floor(numer / denom);
  524.     q = (int)x;
  525.     r = (int)floor(numer - (x * denom));
  526.   }
  527.   else
  528.   {
  529.   //
  530.   // perform operations with positive values, and fix mod to make floor-based
  531.   //
  532.     x = floor(-numer / denom);
  533.     q = -(int)x;
  534.     r = (int)floor(-numer - (x * denom));
  535.     if (r != 0)
  536.     {
  537.       q--;
  538.       r = (int)denom - r;
  539.     }
  540.   }
  541.  
  542.   *quotient = q;
  543.   *rem = r;
  544. }
  545. #endif
  546.  
  547.  
  548. #ifndef M68KASM
  549. /*
  550. ===================
  551. GreatestCommonDivisor
  552. ====================
  553. */
  554. int GreatestCommonDivisor (int i1, int i2)
  555. {
  556.   if (i1 > i2)
  557.   {
  558.     if (i2 == 0)
  559.       return (i1);
  560.     return GreatestCommonDivisor (i2, i1 % i2);
  561.   }
  562.   else
  563.   {
  564.     if (i1 == 0)
  565.       return (i2);
  566.     return GreatestCommonDivisor (i1, i2 % i1);
  567.   }
  568. }
  569. #endif
  570.  
  571.  
  572. #if !id386 && !defined(M68KASM)
  573.  
  574. // TODO: move to nonintel.c
  575.  
  576. /*
  577. ===================
  578. Invert24To16
  579.  
  580. Inverts an 8.24 value to a 16.16 value
  581. ====================
  582. */
  583.  
  584. fixed16_t Invert24To16(fixed16_t val)
  585. {
  586.   if (val < 256)
  587.     return (0xFFFFFFFF);
  588.  
  589.   return (fixed16_t)
  590.       (((double)0x10000 * (double)0x1000000 / (double)val) + 0.5);
  591. }
  592. #endif
  593.