home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / DRAWFN3S.ZIP / GENMAT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-06  |  6.6 KB  |  170 lines

  1. /*****************************************************************************
  2. * General matrix manipulation routines.                         *
  3. *                                         *
  4. * Written by:  Gershon Elber            IBM PC Ver 1.0,    June 1988    *
  5. *****************************************************************************/
  6.  
  7. #include <math.h>
  8. #include "GenMat.h"
  9.  
  10. /*****************************************************************************
  11. * Routine to generate a 4*4 unit matrix:                                     *
  12. *****************************************************************************/
  13. void GenUnitMat(MatrixType Mat)
  14.  
  15. {
  16.     int i, j;
  17.  
  18.     for (i=0; i<4; i++) for (j=0; j<4; j++)
  19.         if (i==j) Mat[i][j] = 1.0;
  20.         else      Mat[i][j] = 0.0;
  21. }
  22.  
  23. /*****************************************************************************
  24. * Routine to generate a 4*4 matrix to translate in Tx, Ty, Tz amounts.       *
  25. *****************************************************************************/
  26. void GenMatTrans(double Tx, double Ty, double Tz, MatrixType Mat)
  27. {
  28.     GenUnitMat(Mat);                                 /* Make it unit matrix. */
  29.     Mat[3][0] = Tx;
  30.     Mat[3][1] = Ty;
  31.     Mat[3][2] = Tz;
  32. }
  33.  
  34. /*****************************************************************************
  35. * Routine to generate a 4*4 matrix to Scale x, y, z in Sx, Sy, Sz amounts.   *
  36. *****************************************************************************/
  37. void GenMatScale(double Sx, double Sy, double Sz, MatrixType Mat)
  38. {
  39.     GenUnitMat(Mat);                                 /* Make it unit matrix. */
  40.     Mat[0][0] = Sx;
  41.     Mat[1][1] = Sy;
  42.     Mat[2][2] = Sz;
  43. }
  44.  
  45. /*****************************************************************************
  46. * Routine to generate a 4*4 matrix to Rotate around X with angle of Teta.    *
  47. * Note Teta must be given in radians.                                        *
  48. *****************************************************************************/
  49. void GenMatRotX1(double Teta, MatrixType Mat)
  50. {
  51.     double CTeta, STeta;
  52.  
  53.     CTeta = cos(Teta);
  54.     STeta = sin(Teta);
  55.     GenMatRotX(CTeta, STeta, Mat);
  56. }
  57.  
  58. /*****************************************************************************
  59. * Routine to generate a 4*4 matrix to Rotate around X with angle of Teta.    *
  60. *****************************************************************************/
  61. void GenMatRotX(double CosTeta, double SinTeta, MatrixType Mat)
  62. {
  63.     GenUnitMat(Mat);                                 /* Make it unit matrix. */
  64.     Mat[1][1] = CosTeta;
  65.     Mat[1][2] = SinTeta;
  66.     Mat[2][1] = -SinTeta;
  67.     Mat[2][2] = CosTeta;
  68. }
  69.  
  70. /*****************************************************************************
  71. * Routine to generate a 4*4 matrix to Rotate around Y with angle of Teta.    *
  72. * Note Teta must be given in radians.                                        *
  73. *****************************************************************************/
  74. void GenMatRotY1(double Teta, MatrixType Mat)
  75. {
  76.     double CTeta, STeta;
  77.  
  78.     CTeta = cos(Teta);
  79.     STeta = sin(Teta);
  80.     GenMatRotY(CTeta, STeta, Mat);
  81. }
  82.  
  83. /*****************************************************************************
  84. * Routine to generate a 4*4 matrix to Rotate around Y with angle of Teta.    *
  85. *****************************************************************************/
  86. void GenMatRotY(double CosTeta, double SinTeta, MatrixType Mat)
  87. {
  88.     GenUnitMat(Mat);                                 /* Make it unit matrix. */
  89.     Mat[0][0] = CosTeta;
  90.     Mat[0][2] = -SinTeta;
  91.     Mat[2][0] = SinTeta;
  92.     Mat[2][2] = CosTeta;
  93. }
  94.  
  95. /*****************************************************************************
  96. * Routine to generate a 4*4 matrix to Rotate around Z with angle of Teta.    *
  97. * Note Teta must be given in radians.                                        *
  98. *****************************************************************************/
  99. void GenMatRotZ1(double Teta, MatrixType Mat)
  100. {
  101.     double CTeta, STeta;
  102.  
  103.     CTeta = cos(Teta);
  104.     STeta = sin(Teta);
  105.     GenMatRotZ(CTeta, STeta, Mat);
  106. }
  107.  
  108. /*****************************************************************************
  109. * Routine to generate a 4*4 matrix to Rotate around Z with angle of Teta.    *
  110. *****************************************************************************/
  111. void GenMatRotZ(double CosTeta, double SinTeta, MatrixType Mat)
  112. {
  113.     GenUnitMat(Mat);                                 /* Make it unit matrix. */
  114.     Mat[0][0] = CosTeta;
  115.     Mat[0][1] = SinTeta;
  116.      Mat[1][0] = -SinTeta;
  117.    Mat[1][1] = CosTeta;
  118. }
  119.  
  120. /*****************************************************************************
  121. * Routine to multiply 2 4by4 matrices:                                       *
  122. * Note MatRes might be one of Mat1 or Mat2 - it is only updated in the end!  *
  123. *****************************************************************************/
  124. void MultTwo4by4(MatrixType MatRes, MatrixType Mat1, MatrixType Mat2)
  125. {
  126.     int i, j, k;
  127.     MatrixType MatResTemp;
  128.  
  129.     for (i=0; i<4; i++) for (j=0; j<4; j++) {
  130.         MatResTemp[i][j] = 0;
  131.         for (k=0; k<4; k++) MatResTemp[i][j] += Mat1[i][k] * Mat2[k][j];
  132.     }
  133.     for (i=0; i<4; i++) for (j=0; j<4; j++) MatRes[i][j] = MatResTemp[i][j];
  134. }
  135.  
  136. /*****************************************************************************
  137. * Routine to multiply Vector by 4by4 matrix:                                 *
  138. * The Vector has only 3 components (X, Y, Z) and it is assumed that W = 1    *
  139. * Note VRes might be Vec as it is only updated in the end.                   *
  140. *****************************************************************************/
  141. void MultVecby4by4(double VRes[3], double Vec[3], MatrixType Mat)
  142. {
  143.     int i, j;
  144.     double CalcW = Mat[3][3];
  145.     double VTemp[3];
  146.  
  147.     for (i=0; i<3; i++) {
  148.         VTemp[i] = Mat[3][i];         /* Initiate it with the weight factor. */
  149.         for (j=0; j<3; j++) VTemp[i] += Vec[j] * Mat[j][i];
  150.     }
  151.  
  152.     for (i=0; i<3; i++) CalcW += Vec[i] * Mat[i][3];
  153.     if (CalcW == 0) CalcW = 1 / INFINITY;
  154.  
  155.     for (i=0; i<3; i++) VRes[i] = VTemp[i]/CalcW;
  156. }
  157.  
  158. /*****************************************************************************
  159. * Routine to generate an isometric view    transformation matrix.             *
  160. *****************************************************************************/
  161. void GenIsometricView(MatrixType TransMat)
  162. {
  163.     MatrixType TempMat;
  164.  
  165.     /* 90 - 35.2644 = 54.7356 */
  166.     GenMatRotX1(-54.7356*(M_PI/180), TransMat);       /* Generate default view. */
  167.     GenMatRotZ1(M_PI+M_PI/4, TempMat);       /* Trans which is isometric view. */
  168.     MultTwo4by4(TransMat, TempMat, TransMat);
  169. }
  170.