home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 4 / CD_Magazyn_EXEC_nr_4.iso / Recent / dev / c / GSys.lha / gsys / g3d / G3DMatrix.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-01  |  2.4 KB  |  151 lines

  1.  
  2. /* Author Anders Kjeldsen (partly) */
  3.  
  4. #ifndef G3DMATRIX_CPP
  5. #define G3DMATRIX_CPP
  6.  
  7. #include "g3d/G3DMatrix.h"
  8. #include "g3d/G3DQuat.cpp"
  9.  
  10. G3DMatrix::G3DMatrix()
  11. {
  12.     SetIdentity();
  13. }
  14.  
  15. void G3DMatrix::SetIdentity()
  16. {
  17.     int i, j;
  18.     for (i = 0; i < 4; i++)
  19.     {
  20.         for (j = 0; j < 4; j++)
  21.         {
  22.             if (i==j) Matrix[i][j] = 1.0;
  23.             else Matrix[i][j] = 0.0;
  24.         }
  25.     }
  26. }
  27.  
  28. void G3DMatrix::SetIdentity(float scale)
  29. {
  30.     for (int i = 0; i < 4; i++)
  31.     {
  32.         for (int j = 0; j < 4; j++)
  33.         {
  34.             if (i==j) Matrix[i][j] = scale;
  35.             else Matrix[i][j] = 0.0;
  36.         }
  37.     }
  38. }
  39.  
  40. void G3DMatrix::SetAsX(float AngleX)
  41. {
  42.  
  43.     Matrix[1][1] = cos(AngleX);
  44.     Matrix[1][2] = sin(AngleX);
  45.     Matrix[2][1] = -(sin(AngleX));
  46.     Matrix[2][2] = cos(AngleX);
  47. }
  48.  
  49. void G3DMatrix::SetAsY(float AngleY)
  50. {
  51.     Matrix[0][0] = cos(AngleY);
  52.     Matrix[0][2] = -(sin(AngleY));
  53.     Matrix[2][0] = sin(AngleY);
  54.     Matrix[2][2] = cos(AngleY);
  55. }
  56.  
  57. void G3DMatrix::SetAsZ(float AngleZ)
  58. {
  59.     Matrix[0][0] = cos(AngleZ);
  60.     Matrix[0][1] = sin(AngleZ);
  61.     Matrix[1][0] = -(sin(AngleZ));
  62.     Matrix[1][1] = cos(AngleZ);
  63. }
  64.  
  65. void G3DMatrix::SetPosition(float vx, float vy, float vz)
  66. {
  67.     Matrix[3][0] = vx;
  68.     Matrix[3][1] = vy;
  69.     Matrix[3][2] = vz;
  70. }
  71.  
  72. void G3DMatrix::operator=(G3DMatrix &m)
  73. {
  74.     for (int i = 0; i < 4; i++)
  75.     {
  76.         for (int j = 0; j < 4; j++)
  77.         {
  78.             Matrix[i][j] = m.Matrix[i][j];
  79.         }
  80.     }
  81. }
  82.  
  83. void G3DMatrix::PrintfAll()
  84. {
  85.     for (int i = 0; i < 4; i++)
  86.     {
  87.         for (int j = 0; j < 4; j++)
  88.         {
  89.             //Matrix[i][j] = m.Matrix[i][j];
  90.             printf("%f\t", Matrix[i][j]);
  91.         }
  92.         printf("\n");
  93.     }
  94. }
  95.  
  96. //G3DMatrix G3DMatrix::operator = (GQuat &b)
  97. void G3DMatrix::operator=(GQuat &b)
  98. {
  99.     SetIdentity();
  100.  
  101.     float sqrt2 = 1.4142136f;
  102.  
  103.     float x = sqrt2*b.x;
  104.     float y = sqrt2*b.y;
  105.     float z = sqrt2*b.z;
  106.     float w = sqrt2*b.w;
  107.  
  108.     float xx = x*x;
  109.     float yy = y*y;
  110.     float zz = z*z;
  111.  
  112.     float xy = x*y;
  113.     float xz = x*z;
  114.     float yz = y*z;
  115.     float wx = w*x;
  116.     float wy = w*y;
  117.     float wz = w*z;
  118.  
  119.     Matrix[0][0] = 1-yy-zz;
  120.     Matrix[0][1] = xy-wz;
  121.     Matrix[0][2] = xz+wy;
  122.  
  123.     Matrix[1][0] = xy+wz;
  124.     Matrix[1][1] = 1-xx-zz;
  125.     Matrix[1][2] = yz-wx;
  126.  
  127.     Matrix[2][0] = xz-wy;
  128.     Matrix[2][1] = yz-wx;
  129.     Matrix[2][2] = 1-xx-yy;
  130.  
  131. }
  132.  
  133. G3DMatrix operator*(class G3DMatrix &a, class G3DMatrix &b)
  134. {
  135.     class G3DMatrix d;
  136.  
  137.     for (int i = 0; i < 4; i++)
  138.     {
  139.         for (int j = 0; j < 4; j++)
  140.         {
  141.             d.Matrix[i][j] = a.Matrix[0][j] * b.Matrix[i][0] +
  142.                      a.Matrix[1][j] * b.Matrix[i][1] +
  143.                      a.Matrix[2][j] * b.Matrix[i][2] +
  144.                      a.Matrix[3][j] * b.Matrix[i][3];
  145.         }
  146.     }
  147.  
  148.     return d;
  149. }
  150.  
  151. #endif /* GMATRIXMET */