fork download
  1. class CMatrix
  2. {
  3. public:
  4. CVector vRight;
  5. CVector vFront;
  6. CVector vUp;
  7. CVector vPos;
  8.  
  9. CMatrix ( )
  10. {
  11. // Load the identity matrix
  12. vRight = CVector ( 1.0f, 0.0f, 0.0f );
  13. vFront = CVector ( 0.0f, 1.0f, 0.0f );
  14. vUp = CVector ( 0.0f, 0.0f, 1.0f );
  15. vPos = CVector ( 0.0f, 0.0f, 0.0f );
  16. }
  17.  
  18. CMatrix operator+ ( const CMatrix& other ) const
  19. {
  20. CMatrix matResult;
  21. matResult.vRight = vRight + other.vRight;
  22. matResult.vFront = vFront + other.vFront;
  23. matResult.vUp = vUp + other.vUp;
  24. matResult.vPos = vPos + other.vPos;
  25. return matResult;
  26. }
  27.  
  28. CMatrix operator- ( const CMatrix& other ) const
  29. {
  30. CMatrix matResult;
  31. matResult.vRight = vRight - other.vRight;
  32. matResult.vFront = vFront - other.vFront;
  33. matResult.vUp = vUp - other.vUp;
  34. matResult.vPos = vPos - other.vPos;
  35. return matResult;
  36. }
  37.  
  38. CMatrix operator* ( const CMatrix& other ) const
  39. {
  40. CMatrix matResult;
  41. matResult.vRight = (*this) * other.vRight;
  42. matResult.vFront = (*this) * other.vFront;
  43. matResult.vUp = (*this) * other.vUp;
  44. matResult.vPos = (*this) * other.vPos;
  45. return matResult;
  46. }
  47.  
  48. CMatrix operator/ ( CMatrix other ) const
  49. {
  50. other.Invert ();
  51. return (*this) * other;
  52. }
  53.  
  54. CVector operator* ( const CVector& vec ) const
  55. {
  56. return CVector (
  57. vRight.fX*vec.fX + vFront.fX*vec.fY + vUp.fX*vec.fZ,
  58. vRight.fY*vec.fX + vFront.fY*vec.fY + vUp.fY*vec.fZ,
  59. vRight.fZ*vec.fX + vFront.fZ*vec.fY + vUp.fZ*vec.fZ
  60. );
  61. }
  62.  
  63. void Invert ()
  64. {
  65. // Transpose the rotation matrix and negate the position
  66. CVector vOldRight = vRight, vOldFront = vFront, vOldUp = vUp;
  67. vRight = CVector ( vOldRight.fX, vOldFront.fX, vOldUp.fX );
  68. vFront = CVector ( vOldRight.fY, vOldFront.fY, vOldUp.fY );
  69. vUp = CVector ( vOldRight.fZ, vOldFront.fZ, vOldUp.fZ );
  70. vPos *= -1.0f;
  71. }
  72.  
  73. CMatrix Rotate ( const CVector * param, float theta )
  74. {
  75. // Rotate the rotation matrix
  76. double sin_t = sinf( theta ), cos_t = cosf( theta );
  77. CMatrix mRotateMult;
  78. // rotate X
  79. mRotateMult.vRight.fX = (double) cos_t + ( 1.0f - cos_t ) * param->fX * param->fX;
  80. mRotateMult.vRight.fY = (double) ( 1.0f - cos_t ) * param->fX * param->fY - sin_t * param->fZ;
  81. mRotateMult.vRight.fZ = (double) ( 1.0f - cos_t ) * param->fX * param->fZ + sin_t * param->fY;
  82. // rotate Y
  83. mRotateMult.vFront.fX = (double) ( 1.0f - cos_t ) * param->fY * param->fX + sin_t * param->fZ;
  84. mRotateMult.vFront.fY = (double) cos_t + ( 1.0f - cos_t ) * param->fY * param->fY;
  85. mRotateMult.vFront.fZ = (double) ( 1.0f - cos_t ) * param->fY * param->fZ - sin_t * param->fX;
  86. // rotate Z
  87. mRotateMult.vUp.fX = (double) ( 1.0f - cos_t ) * param->fZ * param->fX - sin_t * param->fY;
  88. mRotateMult.vUp.fY = (double) ( 1.0f - cos_t ) * param->fZ * param->fY + sin_t * param->fX;
  89. mRotateMult.vUp.fZ = (double) cos_t + ( 1.0f - cos_t ) * param->fZ * param->fZ;
  90. // multiply matrix
  91. mRotateMult = mRotateMult * (*this);
  92. // set vectors
  93. mRotateMult.vPos = vPos;
  94. // return
  95. return mRotateMult;
  96. }
  97.  
  98. void Normalize ( bool bNormalizePosition=0 )
  99. {
  100. // normalize vectors
  101. vRight.Normalize();
  102. vFront.Normalize();
  103. vUp.Normalize();
  104.  
  105. // if you want the position matric to also be normalized, defaults to no.
  106. if (bNormalizePosition)
  107. vPos.Normalize();
  108. }
  109.  
  110. };
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty