home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / openglso.zip / util.c < prev    next >
C/C++ Source or Header  |  2000-02-16  |  8KB  |  364 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. #include "gr.h"
  5. #include "util.h"
  6.  
  7. /*
  8.  * Some functions handling vectors
  9.  */
  10.  
  11. /*
  12.  * Creates a new vector and returns it
  13.  */
  14. GrVector3D mkVector3D( double x, double y, double z )
  15. {
  16.     GrVector3D result;
  17.     result.x = x;
  18.     result.y = y;
  19.     result.z = z;
  20.     return result;
  21. }
  22.  
  23. /*
  24.  * Creates a new point and returns it
  25.  */
  26. GrPoint3D mkPoint3D( double x, double y, double z )
  27. {
  28.     GrPoint3D result;
  29.     result.x = x;
  30.     result.y = y;
  31.     result.z = z;
  32.     return result;
  33. }
  34.  
  35. /*
  36.  * calculates the dot product of two vectors and returns it
  37.  */
  38. double vvDot( GrVector3D v1, GrVector3D v2 )
  39. {
  40.     return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
  41. }
  42.  
  43. /*
  44.  * Multiplies a vector by a scaler
  45.  */
  46. GrVector3D svMpy( double s, GrVector3D v )
  47. {
  48.     GrVector3D rval;
  49.     rval.x = s * v.x;
  50.     rval.y = s * v.y;
  51.     rval.z = s * v.z;
  52.     return rval;
  53. }
  54.  
  55. /*
  56.  * Adds two vectors and returns the sum vector.
  57.  */
  58. GrVector3D vvAdd( GrVector3D v1, GrVector3D v2 )
  59. {
  60.     GrVector3D result;
  61.     result.x = v1.x + v2.x;
  62.     result.y = v1.y + v2.y;
  63.     result.z = v1.z + v2.z;
  64.     return result;
  65. }
  66.  
  67. /*
  68.  * Adds a point and a vector.
  69.  */
  70. GrPoint3D pvAdd( GrPoint3D p1, GrVector3D v2 )
  71. {
  72.     GrPoint3D result;
  73.     result.x = p1.x + v2.x;
  74.     result.y = p1.y + v2.y;
  75.     result.z = p1.z + v2.z;
  76.     return result;
  77. }
  78.  
  79. /*
  80.  * subtracts point v2 from v1
  81.  */
  82. GrVector3D ppSub( GrPoint3D p1, GrPoint3D p2 )
  83. {
  84.     GrVector3D result;
  85.     result.x = p1.x - p2.x;
  86.     result.y = p1.y - p2.y;
  87.     result.z = p1.z - p2.z;
  88.     return result;
  89. }
  90.  
  91. /*
  92.  * subtracts vector v2 from v1
  93.  */
  94. GrVector3D vvSub( GrVector3D v1, GrVector3D v2 )
  95. {
  96.     GrVector3D result;
  97.     result.x = v1.x - v2.x;
  98.     result.y = v1.y - v2.y;
  99.     result.z = v1.z - v2.z;
  100.     return result;
  101. }
  102.  
  103. /*
  104.  * subtracts vector v2 from point v1
  105.  */
  106. GrPoint3D pvSub( GrPoint3D p1, GrVector3D v2 )
  107. {
  108.     GrPoint3D result;
  109.     result.x = p1.x - v2.x;
  110.     result.y = p1.y - v2.y;
  111.     result.z = p1.z - v2.z;
  112.     return result;
  113.  
  114. }
  115.  
  116. /*
  117.  * Normalizes vector v and returns the length before the
  118.  * normalization
  119.  */
  120. double vNormalize( GrVector3D *v )
  121. {
  122.     double denom;
  123.     double x = ( v->x > 0.0 ) ? v->x : - v->x;
  124.     double y = ( v->y > 0.0 ) ? v->y : - v->y;
  125.     double z = ( v->z > 0.0 ) ? v->z : - v->z;
  126.     if( x > y )
  127.         {
  128.         if( x > z )
  129.             {
  130.             if( 1.0 + x > 1.0 )
  131.                 {
  132.                 y = y / x;
  133.                 z = z / x;
  134.                 denom = 1.0 / ( x * sqrt( 1.0 + y*y + z*z ) );
  135.                 }
  136.             }
  137.         else /* z > x > y */
  138.             {
  139.             if( 1.0 + z > 1.0 )
  140.                 {
  141.                 y = y / z;
  142.                 x = x / z;
  143.                 denom = 1.0 / ( z * sqrt( 1.0 + y*y + x*x ) );
  144.                 }
  145.             }
  146.         }
  147.     else
  148.         {
  149.         if( y > z )
  150.             {
  151.             if( 1.0 + y > 1.0 )
  152.                 {
  153.                 z = z / y;
  154.                 x = x / y;
  155.                 denom = 1.0 / ( y * sqrt( 1.0 + z*z + x*x ) );
  156.                 }
  157.             }
  158.         else /* x < y < z */
  159.             {
  160.             if( 1.0 + z > 1.0 )
  161.                 {
  162.                 y = y / z;
  163.                 x = x / z;
  164.                 denom = 1.0 / ( z * sqrt( 1.0 + y*y + x*x ) );
  165.                 }
  166.             }
  167.         }
  168.     if( 1.0 + x + y + z > 1.0 )
  169.         {
  170.         *v = svMpy( denom, *v );
  171.     return 1.0/denom;
  172.         }
  173.     else
  174.     {
  175.     return 0.;
  176.     }
  177. }
  178.  
  179. /*
  180.  * Calculates the crossproduct two vectors.
  181.  */
  182. GrVector3D crossProd(GrVector3D u, GrVector3D v)
  183. {
  184.   GrVector3D ret;
  185.   ret.x = u.y * v.z - u.z * v.y;
  186.   ret.y = u.z * v.x - u.x * v.z;
  187.   ret.z = u.x * v.y - u.y * v.x;
  188.   return ret;
  189. }
  190.  
  191. /* 
  192.  * multiplies a vector to a matrix form the right 
  193.  */
  194. GrVector3D transVec(GrMatrix4x4 mat, GrVector3D v)
  195. {
  196.   GrVector3D r;
  197.   r.x = v.x * mat[0][0] + v.y * mat[0][1] + v.z * mat[0][2];
  198.   r.y = v.x * mat[1][0] + v.y * mat[1][1] + v.z * mat[1][2];
  199.   r.z = v.x * mat[2][0] + v.y * mat[2][1] + v.z * mat[2][2];
  200.   return r;
  201. }
  202.  
  203. /*
  204.  * transform a point by multiplying it to a matrix from the right
  205.  */
  206. GrPoint3D transPoint(GrMatrix4x4 mat, GrPoint3D p)
  207. {
  208.   GrPoint3D r;
  209.   r.x = p.x * mat[0][0] + p.y * mat[0][1] + p.z * mat[0][2];
  210.   r.y = p.x * mat[1][0] + p.y * mat[1][1] + p.z * mat[1][2];
  211.   r.z = p.x * mat[2][0] + p.y * mat[2][1] + p.z * mat[2][2];
  212.   r.x += mat[0][3];
  213.   r.y += mat[1][3];
  214.   r.z += mat[2][3];
  215.   return r;
  216. }
  217.  
  218. /*
  219.  * transforms a vector by multiplying it to a matrix from the left
  220.  */
  221. GrVector3D transNorm(GrVector3D n, GrMatrix4x4 mat)
  222. {
  223.   GrVector3D r;
  224.   r.x = n.x * mat[0][0] + n.y * mat[1][0] + n.z * mat[2][0];
  225.   r.y = n.x * mat[0][1] + n.y * mat[1][1] + n.z * mat[2][1];
  226.   r.z = n.x * mat[0][2] + n.y * mat[1][2] + n.z * mat[2][2];
  227.   return r;
  228. }
  229.  
  230.  
  231. /* *************************************************************************
  232.  *
  233.  * Handling matrices
  234.  *
  235.  * *************************************************************************/
  236.  
  237. /*
  238.  * initialize a matrix to the identity matrix.
  239.  */
  240. void initMatrix(GrMatrix4x4 h)
  241. {
  242.   int i,j;
  243.   
  244.   for( i= 0 ; i< 4 ; i++ )
  245.     for( j= 0 ; j< 4 ; j++ )
  246.       h[i][j]= (i==j);
  247. }
  248.  
  249. /*
  250.  * Multiplying mat1*mat2
  251.  * ret can be one of mat1 or mat2
  252.  */
  253. void mulMatrix(GrMatrix4x4 ret, GrMatrix4x4 mat1, GrMatrix4x4 mat2)
  254. {
  255.   int i,j;
  256.   GrMatrix4x4 r;
  257.  
  258.   for( i= 0 ; i< 4 ; i++ )
  259.     for( j= 0 ; j< 4 ; j++ )
  260.       r[i][j]= mat1[i][0] * mat2[0][j] +
  261.                   mat1[i][1] * mat2[1][j] +
  262.           mat1[i][2] * mat2[2][j] +
  263.           mat1[i][3] * mat2[3][j];
  264.  
  265.   for( i= 0 ; i< 4 ; i++ )
  266.     for( j= 0 ; j< 4 ; j++ )
  267.       ret[i][j] = r[i][j];
  268. }
  269.  
  270.  
  271. /* 
  272.  * TransposeMatrix
  273.  
  274.  */
  275. void TransposeMatrix(GrMatrix4x4 ret, GrMatrix4x4 inMatrix)
  276. {
  277.   int i, j;
  278.  
  279.   for (i=0; i<4; i++)
  280.     for (j=0; j<4; j++)
  281.       ret[i][j] = inMatrix[j][i];
  282.       
  283. }
  284. /* *************************************************************************
  285.  *
  286.  * Handling colours
  287.  *
  288.  * *************************************************************************/
  289.  
  290. /*
  291.  * Creates a colour with the same value in each component
  292.  */
  293. GrColour mkColour( double c)
  294. {
  295.   GrColour result;
  296.   result.r = c;
  297.   result.g = c;
  298.   result.b = c;
  299.   return result;
  300. }
  301.  
  302.  
  303. /*
  304.  * component by component multiplication of two colours
  305.  */
  306. GrColour cMult( GrColour c1, GrColour c2 )
  307. {
  308.   GrColour result;
  309.   result.r = c1.r * c2.r;
  310.   result.g = c1.g * c2.g;
  311.   result.b = c1.b * c2.b;
  312.  
  313.   return result;
  314. }
  315.  
  316. /*
  317.  * calculates c1+(c2*c3)
  318.  */
  319. GrColour cMultAdd( GrColour c1, GrColour c2, GrColour c3 )
  320. {
  321.   GrColour result;
  322.   result.r = c1.r + c2.r * c3.r;
  323.   result.g = c1.g + c2.g * c3.g;
  324.   result.b = c1.b + c2.b * c3.b;
  325.  
  326.   return result;
  327. }
  328.  
  329. /*
  330.  * printPoint3D
  331.  */
  332. void printPoint3D(GrPoint3D point)
  333. {
  334.   printf("x:%lf  y:%lf  z:%lf\n", point.x, point.y, point.z);
  335. }
  336.  
  337.  
  338. /* ------------------------------------------------------------------ *
  339.  * Print_Matrix                                                       * 
  340.  * This prcedure prints out the matrix for checking.                  *
  341.  * ------------------------------------------------------------------ */
  342. void Print_Matrix(GrMatrix4x4 A)
  343. {
  344.   int i, j;
  345.  
  346.   for (i=0; i<4; i++) {
  347.     for (j=0; j<4; j++)
  348.       printf("%.2lf      ", A[i][j]);
  349.     printf("\n");
  350.   }
  351. }
  352.  
  353. /* ------------------------------------------------------------------ *
  354.  * CopyMatrix                                                         * 
  355.  * This procedure copies the contents of a matrix to another.         *
  356.  * ------------------------------------------------------------------ */
  357. void CopyMatrix(GrMatrix4x4 From, GrMatrix4x4 To)
  358. {
  359.   int i, j;
  360.   for (i=0; i<4; i++)
  361.     for (j=0; j<4; j++)
  362.       To[i][j] = From[i][j];
  363. }
  364.