Advertisement
Guest User

matrix.h

a guest
Jan 18th, 2014
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.48 KB | None | 0 0
  1. #ifndef CMatrix_h
  2. #define CMatrix_h
  3.  
  4. const float PI = 3.14159265f;
  5. const float TO_RAD = PI / 180.0f;
  6.  
  7. #include "vec3.h"
  8.  
  9. class CMatrix
  10. {
  11. public:
  12. // Data
  13.     float mf[ 16 ];
  14.  
  15. // Functions
  16.     CMatrix( const int bIdentity = true )
  17.     {
  18.      if ( bIdentity ) Identity();
  19.     }
  20.  
  21.     void Identity( )
  22.     {
  23.     mf[ 0] = 1.0f;    mf[ 1] = 0.0f;      mf[ 2] = 0.0f;    mf[ 3] = 0.0f;
  24.     mf[ 4] = 0.0f;    mf[ 5] = 1.0f;      mf[ 6] = 0.0f;    mf[ 7] = 0.0f;
  25.     mf[ 8] = 0.0f;    mf[ 9] = 0.0f;      mf[10] = 1.0f;    mf[11] = 0.0f;
  26.     mf[12] = 0.0f;    mf[13] = 0.0f;      mf[14] = 0.0f;    mf[15] = 1.0f;
  27.     }
  28.  
  29.     // Concatenate 2 matrices with the * operator
  30.     inline CMatrix operator* (const CMatrix &InM) const
  31.     {
  32.     CMatrix Result( 0 );
  33.     for (int i=0;i<16;i+=4)
  34.         {
  35.         for (int j=0;j<4;j++)
  36.             {
  37.             Result.mf[i + j] = mf[ i + 0] * InM.mf[ 0 + j] + mf[ i + 1] * InM.mf[ 4 + j]
  38.                 + mf[ i + 2] * InM.mf[ 8 + j] + mf[ i + 3] * InM.mf[ 12 + j];
  39.             }
  40.         }
  41.     return Result;
  42.     }
  43.  
  44.     // Use a matrix to transform a 3D point with the * operator
  45.     inline CVec3 operator* (const CVec3 &Point ) const
  46.     {
  47.      float x = Point.x*mf[0] + Point.y*mf[4] + Point.z*mf[8]  + mf[12];
  48.      float y = Point.x*mf[1] + Point.y*mf[5] + Point.z*mf[9]  + mf[13];
  49.      float z = Point.x*mf[2] + Point.y*mf[6] + Point.z*mf[10] + mf[14];
  50.      return CVec3( x, y, z );
  51.     }
  52.  
  53.     // Rotate the *this matrix fDegrees counter-clockwise around a single axis( either x, y, or z )
  54.     void Rotate( float fDegrees, int x, int y, int z )
  55.     {
  56.      CMatrix Temp;
  57.      if (x == 1) Temp.RotX( -fDegrees );
  58.      if (y == 1) Temp.RotY( -fDegrees );
  59.      if (z == 1) Temp.RotZ( -fDegrees );
  60.      *this = Temp * (*this);
  61.     }
  62.  
  63.     void Scale( float sx, float sy, float sz )
  64.     {
  65.      int x;
  66.      for (x = 0; x <  4; x++) mf[x]*=sx;
  67.      for (x = 4; x <  8; x++) mf[x]*=sy;
  68.      for (x = 8; x < 12; x++) mf[x]*=sz;
  69.     }
  70.  
  71.     void Translate( const CVec3 &Test )
  72.     {
  73.     for (int j=0;j<4;j++)
  74.         {
  75.         mf[12+j] += Test.x * mf[j] + Test.y * mf[4+j] + Test.z * mf[8+j];
  76.         }
  77.     }
  78.  
  79.     CVec3 GetTranslate( )
  80.     {
  81.         return CVec3( mf[12], mf[13], mf[14] );
  82.     }
  83.  
  84.     // Zero out the translation part of the matrix
  85.     CMatrix RotationOnly( )
  86.     {
  87.      CMatrix Temp = *this;
  88.      Temp.mf[12] = 0;
  89.      Temp.mf[13] = 0;
  90.      Temp.mf[14] = 0;
  91.      return Temp;
  92.     }
  93.  
  94.     // Create a rotation matrix for a counter-clockwise rotation of fDegrees around an arbitrary axis(x, y, z)
  95.     void RotateMatrix( float fDegrees, float x, float y, float z)
  96.     {
  97.     Identity();
  98.     float cosA = cosf(fDegrees*TO_RAD);
  99.     float sinA = sinf(fDegrees*TO_RAD);
  100.     float m = 1.0f - cosA;
  101.     mf[0] = cosA + x*x*m;
  102.     mf[5] = cosA + y*y*m;
  103.     mf[10]= cosA + z*z*m;
  104.  
  105.     float tmp1 = x*y*m;
  106.     float tmp2 = z*sinA;
  107.     mf[4] = tmp1 + tmp2;
  108.     mf[1] = tmp1 - tmp2;
  109.  
  110.     tmp1 = x*z*m;
  111.     tmp2 = y*sinA;
  112.     mf[8] = tmp1 - tmp2;
  113.     mf[2] = tmp1 + tmp2;
  114.  
  115.     tmp1 = y*z*m;
  116.     tmp2 = x*sinA;
  117.     mf[9] = tmp1 + tmp2;
  118.     mf[6] = tmp1 - tmp2;
  119.     }
  120.  
  121.     // Simple but not robust matrix inversion. (Doesn't work properly if there is a scaling or skewing transformation.)
  122.     inline CMatrix InvertSimple()
  123.     {
  124.     CMatrix R(0);
  125.     R.mf[0]  = mf[0];       R.mf[1]  = mf[4];       R.mf[2]  = mf[8];   R.mf[3]  = 0.0f;
  126.     R.mf[4]  = mf[1];       R.mf[5]  = mf[5];       R.mf[6]  = mf[9];   R.mf[7]  = 0.0f;
  127.     R.mf[8]  = mf[2];       R.mf[9]  = mf[6];       R.mf[10] = mf[10];  R.mf[11] = 0.0f;
  128.     R.mf[12] = -(mf[12]*mf[0]) - (mf[13]*mf[1]) - (mf[14]*mf[2]);
  129.     R.mf[13] = -(mf[12]*mf[4]) - (mf[13]*mf[5]) - (mf[14]*mf[6]);
  130.     R.mf[14] = -(mf[12]*mf[8]) - (mf[13]*mf[9]) - (mf[14]*mf[10]);
  131.     R.mf[15] = 1.0f;
  132.     return R;
  133.     }
  134.  
  135.     // Invert for only a rotation, any translation is zeroed out
  136.     CMatrix InvertRot( )
  137.     {
  138.      CMatrix R( 0 );
  139.      R.mf[0]  = mf[0];      R.mf[1]  = mf[4];       R.mf[2]  = mf[8];   R.mf[3]  = 0.0f;
  140.      R.mf[4]  = mf[1];      R.mf[5]  = mf[5];       R.mf[6]  = mf[9];   R.mf[7]  = 0.0f;
  141.      R.mf[8]  = mf[2];      R.mf[9]  = mf[6];       R.mf[10] = mf[10];  R.mf[11] = 0.0f;
  142.      R.mf[12] = 0;          R.mf[13] = 0;           R.mf[14] = 0;       R.mf[15] = 1.0f;
  143.      return R;
  144.     }
  145.  
  146.  
  147. private:
  148.     // helpers for Rotate
  149.     void RotX(float angle)
  150.         {
  151.         mf[5]  = cosf(angle*TO_RAD);
  152.         mf[6]  = sinf(angle*TO_RAD);
  153.         mf[9]  = -sinf(angle*TO_RAD);
  154.         mf[10] = cosf(angle*TO_RAD);
  155.         }
  156.     void RotY(float angle)
  157.         {
  158.         mf[0]  =  cosf(angle*TO_RAD);
  159.         mf[2]  =  -sinf(angle*TO_RAD);
  160.         mf[8]  =  sinf(angle*TO_RAD);
  161.         mf[10] =  cosf(angle*TO_RAD);
  162.         }
  163.     void RotZ(float angle)
  164.         {
  165.         mf[0] =  cosf(angle*TO_RAD);
  166.         mf[1] =  sinf(angle*TO_RAD);
  167.         mf[4] =  -sinf(angle*TO_RAD);
  168.         mf[5] =  cosf(angle*TO_RAD);
  169.         }
  170. };
  171.  
  172. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement