home *** CD-ROM | disk | FTP | other *** search
/ Launch & Play / spustahrej2.iso / Egoboo / code / mathstuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-03  |  10.5 KB  |  298 lines

  1. /**> HEADER FILES <**/
  2. #include "mathstuff.h"
  3.  
  4.  
  5. // FAKE D3D FUNCTIONS
  6. GLVECTOR vsub(GLVECTOR A, GLVECTOR B)
  7. {
  8.   GLVECTOR tmp;
  9.   tmp.x=A.x-B.x; tmp.y=A.y-B.y; tmp.z=A.z-B.z;
  10.   return(tmp);
  11. }
  12.  
  13. GLVECTOR Normalize(GLVECTOR vec)
  14. {
  15.   GLVECTOR tmp=vec;
  16.   float len;
  17.   len= (float)sqrt(vec.x*vec.x+vec.y*vec.y+vec.z*vec.z);
  18.   tmp.x/=len;
  19.   tmp.y/=len;
  20.   tmp.z/=len;
  21.   return(tmp);
  22. }
  23.  
  24. GLVECTOR CrossProduct(GLVECTOR A, GLVECTOR B)
  25. {
  26.   GLVECTOR tmp;
  27.   tmp.x=A.y*B.z-A.z*B.y;
  28.   tmp.y=A.z*B.x-A.x*B.z;
  29.   tmp.z=A.x*B.y-A.y*B.x;
  30.   return(tmp);
  31. }
  32.  
  33. float DotProduct(GLVECTOR A, GLVECTOR B)
  34. { return(A.x*B.x+A.y*B.y+A.z*B.z); }
  35.  
  36. //---------------------------------------------------------------------------------------------
  37. //Math Stuff-----------------------------------------------------------------------------------
  38. //---------------------------------------------------------------------------------------------
  39. //inline D3DMATRIX IdentityMatrix()
  40. GLMATRIX IdentityMatrix()
  41. {
  42.         GLMATRIX tmp;
  43.  
  44.         (tmp)_CNV(0,0)=1; (tmp)_CNV(1,0)=0; (tmp)_CNV(2,0)=0; (tmp)_CNV(3,0)=0;
  45.         (tmp)_CNV(0,1)=0; (tmp)_CNV(1,1)=1; (tmp)_CNV(2,1)=0; (tmp)_CNV(3,1)=0; 
  46.         (tmp)_CNV(0,2)=0; (tmp)_CNV(1,2)=0; (tmp)_CNV(2,2)=1; (tmp)_CNV(3,2)=0; 
  47.         (tmp)_CNV(0,3)=0; (tmp)_CNV(1,3)=0; (tmp)_CNV(2,3)=0; (tmp)_CNV(3,3)=1; 
  48.         return(tmp);
  49. }
  50.  
  51. //--------------------------------------------------------------------------------------------
  52. //inline D3DMATRIX ZeroMatrix(void)  // initializes matrix to zero
  53. GLMATRIX ZeroMatrix(void)
  54. {
  55.     GLMATRIX ret;
  56.     int i,j;
  57.  
  58.         for (i=0; i<4; i++)
  59.           for (j=0; j<4; j++)
  60.             (ret)_CNV(i,j)=0;
  61.     return ret;
  62. }
  63.  
  64. //--------------------------------------------------------------------------------------------
  65. //inline D3DMATRIX MatrixMult(const D3DMATRIX a, const D3DMATRIX b)  
  66. GLMATRIX MatrixMult(const GLMATRIX a, const GLMATRIX b)
  67. {
  68.     GLMATRIX ret = ZeroMatrix();
  69.     int i,j,k;
  70.     
  71.     for (i=0; i<4; i++)
  72.       for (j=0; j<4; j++)
  73.         for (k=0; k<4; k++)
  74.           (ret)_CNV(i,j) += (a)_CNV(k,j) * (b)_CNV(i,k);
  75.     return ret;
  76. }
  77.  
  78. //--------------------------------------------------------------------------------------------
  79. //D3DMATRIX Translate(const float dx, const float dy, const float dz)
  80. GLMATRIX Translate(const float dx, const float dy, const float dz)
  81. {
  82.     GLMATRIX ret = IdentityMatrix();
  83.     (ret)_CNV(3,0) = dx;
  84.     (ret)_CNV(3,1) = dy;
  85.     (ret)_CNV(3,2) = dz;
  86.     return ret;
  87. }
  88.  
  89. //--------------------------------------------------------------------------------------------
  90. //D3DMATRIX RotateX(const float rads)
  91. GLMATRIX RotateX(const float rads)
  92. {
  93.     float cosine = (float)cos(rads);
  94.     float sine = (float)sin(rads);
  95.     GLMATRIX ret = IdentityMatrix();
  96.     (ret)_CNV(1,1) = cosine;
  97.     (ret)_CNV(2,2) = cosine;
  98.     (ret)_CNV(1,2) = -sine;
  99.     (ret)_CNV(2,1) = sine;
  100.     return ret;
  101. }
  102.  
  103. //--------------------------------------------------------------------------------------------
  104. //D3DMATRIX RotateY(const float rads)
  105. GLMATRIX RotateY(const float rads)
  106. {
  107.     float cosine = (float)cos(rads);
  108.     float sine = (float)sin(rads);
  109.     GLMATRIX ret = IdentityMatrix();
  110.     (ret)_CNV(0,0) = cosine;            //0,0
  111.     (ret)_CNV(2,2) = cosine;            //2,2
  112.     (ret)_CNV(0,2) = sine;            //0,2
  113.     (ret)_CNV(2,0) = -sine;            //2,0
  114.     return ret;
  115. }
  116.  
  117. //--------------------------------------------------------------------------------------------
  118. //D3DMATRIX RotateZ(const float rads)
  119. GLMATRIX RotateZ(const float rads)
  120. {
  121.     float cosine = (float)cos(rads);
  122.     float sine = (float)sin(rads);
  123.     GLMATRIX ret = IdentityMatrix();
  124.     (ret)_CNV(0,0) = cosine;            //0,0
  125.     (ret)_CNV(1,1) = cosine;            //1,1
  126.     (ret)_CNV(0,1) = -sine;            //0,1
  127.     (ret)_CNV(1,0) = sine;            //1,0
  128.     return ret;
  129. }
  130.  
  131. //--------------------------------------------------------------------------------------------
  132. //D3DMATRIX ScaleXYZ(const float sizex, const float sizey, const float sizez)
  133. GLMATRIX ScaleXYZ(const float sizex, const float sizey, const float sizez)
  134. {
  135.     GLMATRIX ret = IdentityMatrix();
  136.     (ret)_CNV(0,0) = sizex;            //0,0
  137.     (ret)_CNV(1,1) = sizey;            //1,1
  138.     (ret)_CNV(2,2) = sizez;            //2,2
  139.     return ret;
  140. }
  141.  
  142. //--------------------------------------------------------------------------------------------
  143. /*D3DMATRIX ScaleXYZRotateXYZTranslate(const float sizex, const float sizey, const float sizez,
  144.    unsigned short turnz, unsigned short turnx, unsigned short turny,
  145.    float tx, float ty, float tz)*/
  146. GLMATRIX ScaleXYZRotateXYZTranslate(const float sizex, const float sizey, const float sizez, unsigned short turnz, unsigned short turnx, unsigned short turny, float tx, float ty, float tz)
  147. {
  148.     float cx = turntosin[(turnx+4096)&16383];
  149.     float sx = turntosin[turnx];
  150.     float cy = turntosin[(turny+4096)&16383];
  151.     float sy = turntosin[turny];
  152.     float cz = turntosin[(turnz+4096)&16383];
  153.     float sz = turntosin[turnz];
  154.     float sxsy = sx*sy;
  155.     float cxsy = cx*sy;
  156.     float sxcy = sx*cy;
  157.     float cxcy = cx*cy;
  158.     GLMATRIX ret;
  159.     (ret)_CNV(0,0) = sizex*(cy*cz);            //0,0
  160.     (ret)_CNV(0,1) = sizex*(sxsy*cz+cx*sz);    //0,1
  161.     (ret)_CNV(0,2) = sizex*(-cxsy*cz+sx*sz);    //0,2
  162.     (ret)_CNV(0,3) = 0;                        //0,3
  163.  
  164.     (ret)_CNV(1,0) = sizey*(-cy*sz);            //1,0
  165.     (ret)_CNV(1,1) = sizey*(-sxsy*sz+cx*cz);    //1,1
  166.     (ret)_CNV(1,2) = sizey*(cxsy*sz+sx*cz);    //1,2
  167.     (ret)_CNV(1,3) = 0;                        //1,3
  168.  
  169.     (ret)_CNV(2,0) = sizez*(sy);                //2,0
  170.     (ret)_CNV(2,1) = sizez*(-sxcy);            //2,1
  171.     (ret)_CNV(2,2) = sizez*(cxcy);            //2,2
  172.     (ret)_CNV(2,3) = 0;                        //2,3
  173.  
  174.     (ret)_CNV(3,0) = tx;                        //3,0
  175.     (ret)_CNV(3,1) = ty;                        //3,1
  176.     (ret)_CNV(3,2) = tz;                        //3,2
  177.     (ret)_CNV(3,3) = 1;                        //3,3
  178.     return ret;
  179. }
  180.  
  181. //--------------------------------------------------------------------------------------------
  182. //D3DMATRIX FourPoints(float orix, float oriy, float oriz,
  183. GLMATRIX FourPoints(float orix, float oriy, float oriz,
  184.                      float widx, float widy, float widz,
  185.                      float forx, float fory, float forz,
  186.                      float upx,  float upy,  float upz,
  187.                      float scale)
  188. {
  189.     GLMATRIX tmp;
  190.     widx-=orix;  forx-=orix;  upx-=orix;
  191. widx=-widx;  // HUK
  192.     widy-=oriy;  fory-=oriy;  upy-=oriy;
  193. widy=-widy; // HUK
  194.     widz-=oriz;  forz-=oriz;  upz-=oriz;
  195. widz=-widz; // HUK
  196.     widx=widx*scale;  forx=forx*scale;  upx=upx*scale;
  197.     widy=widy*scale;  fory=fory*scale;  upy=upy*scale;
  198.     widz=widz*scale;  forz=forz*scale;  upz=upz*scale;
  199.     (tmp)_CNV(0,0)=widx;                                    //0,0
  200.     (tmp)_CNV(0,1)=widy;                                    //0,1
  201.     (tmp)_CNV(0,2)=widz;                                    //0,2
  202.     (tmp)_CNV(0,3)=0;                                        //0,3
  203.     (tmp)_CNV(1,0)=forx;                                    //1,0
  204.     (tmp)_CNV(1,1)=fory;                                    //1,1
  205.     (tmp)_CNV(1,2)=forz;                                    //1,2
  206.     (tmp)_CNV(1,3)=0;                                        //1,3
  207.     (tmp)_CNV(2,0)=upx;                                    //2,0
  208.     (tmp)_CNV(2,1)=upy;                                    //2,1
  209.     (tmp)_CNV(2,2)=upz;                                    //2,2
  210.     (tmp)_CNV(2,3)=0;                                    //2,3
  211.     (tmp)_CNV(3,0)=orix;                                    //3,0
  212.     (tmp)_CNV(3,1)=oriy;                                    //3,1
  213.     (tmp)_CNV(3,2)=oriz;                                    //3,2
  214.     (tmp)_CNV(3,3)=1;                                    //3,3
  215.     return(tmp);
  216. }
  217.  
  218. //--------------------------------------------------------------------------------------------
  219. // MN This probably should be replaced by a call to gluLookAt, don't see why we need to make our own...
  220. //
  221. //inline D3DMATRIX ViewMatrix(const D3DVECTOR from,      // camera location
  222. GLMATRIX ViewMatrix(const GLVECTOR from,      // camera location
  223.                             const GLVECTOR at,        // camera look-at target
  224.                             const GLVECTOR world_up,  // worldÆs up, usually 0, 0, 1
  225.                             const float roll)          // clockwise roll around
  226.                                                        //    viewing direction, 
  227.                                                        //    in radians
  228. {
  229.     GLMATRIX view = IdentityMatrix();
  230.     GLVECTOR up, right, view_dir;
  231.  
  232.     view_dir = Normalize(vsub(at,from));
  233.     right = CrossProduct(world_up, view_dir);
  234.     up = CrossProduct(view_dir, right);
  235.     right = Normalize(right);
  236.     up = Normalize(up);
  237.     (view)_CNV(0,0) = right.x;                        //0,0
  238.     (view)_CNV(1,0) = right.y;                        //1,0
  239.     (view)_CNV(2,0) = right.z;                        //2,0
  240.     (view)_CNV(0,1) = up.x;                            //0,1
  241.     (view)_CNV(1,1) = up.y;                            //1,1
  242.     (view)_CNV(2,1) = up.z;                            //2,1
  243.     (view)_CNV(0,2) = view_dir.x;                        //0,2
  244.     (view)_CNV(1,2) = view_dir.y;                        //1,2
  245.     (view)_CNV(2,2) = view_dir.z;                    //2,2
  246.     (view)_CNV(3,0) = -DotProduct(right, from);        //3,0
  247.     (view)_CNV(3,1) = -DotProduct(up, from);            //3,1
  248.     (view)_CNV(3,2) = -DotProduct(view_dir, from);    //3,2
  249.  
  250.     if (roll != 0.0f)
  251.     {
  252.         // MatrixMult function shown above
  253.         view = MatrixMult(RotateZ(-roll), view); 
  254.     }
  255.  
  256.     return view;
  257. }
  258.  
  259. //--------------------------------------------------------------------------------------------
  260. // MN Again, there is a gl function for this, glFrustum or gluPerspective... does this account for viewport ratio?
  261. //
  262. //inline D3DMATRIX ProjectionMatrix(const float near_plane,     // distance to near clipping plane
  263. GLMATRIX ProjectionMatrix(const float near_plane,     // distance to near clipping plane
  264.                                   const float far_plane,      // distance to far clipping plane
  265.                                   const float fov)            // field of view angle, in radians
  266. {
  267.     float c = (float)cos(fov*0.5);
  268.     float s = (float)sin(fov*0.5);
  269.     float Q = s/(1.0f - near_plane/far_plane);
  270.     GLMATRIX ret = ZeroMatrix();
  271.     (ret)_CNV(0,0) = c;                            //0,0
  272.     (ret)_CNV(1,1) = c;                            //1,1
  273.     (ret)_CNV(2,2) = Q;                            //2,2
  274.     (ret)_CNV(3,2) = -Q*near_plane;                //3,2
  275.     (ret)_CNV(2,3) = s;                            //2,3
  276.     return ret;
  277. }
  278.  
  279.  
  280. //----------------------------------------------------
  281. // GS - Normally we souldn't this function but I found it in the rendering of the particules.
  282. //
  283. // This is just a MulVectorMatrix for now. The W division and screen size multiplication 
  284. // must be done afterward.
  285. // Isn't tested!!!!
  286. void    TransformVertices( GLMATRIX *pMatrix, GLVECTOR *pSourceV, GLVECTOR *pDestV, Uint32 pNumVertor )
  287. {
  288.     while ( pNumVertor-- )
  289.     {
  290.         pDestV->x = pSourceV->x * pMatrix->v[0] + pSourceV->y * pMatrix->v[4] + pSourceV->z * pMatrix->v[8] + pSourceV->w * pMatrix->v[12];
  291.         pDestV->y = pSourceV->x * pMatrix->v[1] + pSourceV->y * pMatrix->v[5] + pSourceV->z * pMatrix->v[9] + pSourceV->w * pMatrix->v[13];
  292.         pDestV->z = pSourceV->x * pMatrix->v[2] + pSourceV->y * pMatrix->v[6] + pSourceV->z * pMatrix->v[10] + pSourceV->w * pMatrix->v[14];
  293.         pDestV->w = pSourceV->x * pMatrix->v[3] + pSourceV->y * pMatrix->v[7] + pSourceV->z * pMatrix->v[11] + pSourceV->w * pMatrix->v[15];
  294.         pDestV++;
  295.         pSourceV++;
  296.     }
  297. }
  298.