home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CDX.ZIP / Src / C3d / Matrix.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-10  |  6.8 KB  |  228 lines

  1. //////////////////////////////////////////////////////////////////////////////////
  2. // Project Name: [ C3D Class Library - C3D.lib ]
  3. // Author:       [ Dan Farley - 97308096@brookes.ac.uk ]
  4. // Source File:  [ C3D_Matrix Implementation ]
  5. // Revision:     [ 1.6 ]
  6. //////////////////////////////////////////////////////////////////////////////////
  7. #include "C3D.h"
  8. #include <math.h>
  9.  
  10. //////////////////////////////////////////////////////////////////////////////////
  11. // Multiply
  12. //////////////////////////////////////////////////////////////////////////////////
  13. C3D_Matrix Multiply(const C3D_Matrix a, const C3D_Matrix b)
  14. {
  15.     C3D_Matrix ret;
  16.  
  17.     for(int i = 0; i < 4; i++)
  18.         for(int j = 0; j < 4; j++)
  19.             for(int k = 0; k < 4; k++)
  20.                 ret(i, j) += a(k, j) * b(i, k);
  21.  
  22.     return ret;
  23. }
  24.  
  25. //////////////////////////////////////////////////////////////////////////////////
  26. // C3D_Matrix constructor
  27. //////////////////////////////////////////////////////////////////////////////////
  28. C3D_Matrix::C3D_Matrix(void)
  29. {
  30.     _11 = _12 = _13 = _14 = 0.0f;
  31.     _21 = _22 = _23 = _24 = 0.0f;
  32.     _31 = _32 = _33 = _34 = 0.0f;
  33.     _41 = _42 = _43 = _44 = 0.0f;
  34. }
  35.  
  36. //////////////////////////////////////////////////////////////////////////////////
  37. // C3D_Matrix = operator
  38. //////////////////////////////////////////////////////////////////////////////////
  39. C3D_Matrix C3D_Matrix::operator = (const C3D_Matrix &m)
  40. {
  41.     _11 = m._11; _12 = m._12; _13 = m._13; _14 = m._14;
  42.     _21 = m._21; _22 = m._22; _23 = m._23; _24 = m._24;
  43.     _31 = m._31; _32 = m._32; _33 = m._33; _34 = m._34;
  44.     _41 = m._41; _42 = m._42; _43 = m._43; _44 = m._44;
  45.  
  46.     return *this;
  47. }
  48.  
  49. //////////////////////////////////////////////////////////////////////////////////
  50. // C3D_Matrix + operator
  51. //////////////////////////////////////////////////////////////////////////////////
  52. C3D_Matrix C3D_Matrix::operator + (const C3D_Matrix &m)
  53. {
  54.     _11 += m._11; _12 += m._12; _13 += m._13; _14 += m._14;
  55.     _21 += m._21; _22 += m._22; _23 += m._23; _24 += m._24;
  56.     _31 += m._31; _32 += m._32; _33 += m._33; _34 += m._34;
  57.     _41 += m._41; _42 += m._42; _43 += m._43; _44 += m._44;
  58.  
  59.     return *this;
  60. }
  61.  
  62. //////////////////////////////////////////////////////////////////////////////////
  63. // C3D_Matrix - operator
  64. //////////////////////////////////////////////////////////////////////////////////
  65. C3D_Matrix C3D_Matrix::operator - (const C3D_Matrix &m)
  66. {
  67.     _11 -= m._11; _12 -= m._12; _13 -= m._13; _14 -= m._14;
  68.     _21 -= m._21; _22 -= m._22; _23 -= m._23; _24 -= m._24;
  69.     _31 -= m._31; _32 -= m._32; _33 -= m._33; _34 -= m._34;
  70.     _41 -= m._41; _42 -= m._42; _43 -= m._43; _44 -= m._44;
  71.  
  72.     return *this;
  73. }
  74.  
  75. //////////////////////////////////////////////////////////////////////////////////
  76. // C3D_Matrix Zero
  77. //////////////////////////////////////////////////////////////////////////////////
  78. void C3D_Matrix::Zero(void)
  79. {
  80.     _11 = _12 = _13 = _14 = 0.0f;
  81.     _21 = _22 = _23 = _24 = 0.0f;
  82.     _31 = _32 = _33 = _34 = 0.0f;
  83.     _41 = _42 = _43 = _44 = 0.0f;
  84. }
  85.  
  86. //////////////////////////////////////////////////////////////////////////////////
  87. // C3D_Matrix Identity
  88. //////////////////////////////////////////////////////////////////////////////////
  89. void C3D_Matrix::Identity(void)
  90. {
  91.     Zero();
  92.     _11 = _22 = _33 = _44 = 1.0f;
  93. }
  94.  
  95. //////////////////////////////////////////////////////////////////////////////////
  96. // C3D_Matrix RotateX
  97. //////////////////////////////////////////////////////////////////////////////////
  98. void C3D_Matrix::RotateX(float rx)
  99. {
  100.     float cosine = cos(rx);
  101.     float sine = sin(rx);
  102.  
  103.     _22 = cosine;
  104.     _33 = cosine;
  105.     _32 = -sine;
  106.     _23 = sine;
  107. }
  108.  
  109. //////////////////////////////////////////////////////////////////////////////////
  110. // C3D_Matrix RotateY
  111. //////////////////////////////////////////////////////////////////////////////////
  112. void C3D_Matrix::RotateY(float ry)
  113. {
  114.     float cosine = cos(ry);
  115.     float sine = sin(ry);
  116.  
  117.     _11 = cosine;
  118.     _33 = cosine;
  119.     _13 = -sine;
  120.     _31 = sine;
  121. }
  122.  
  123. //////////////////////////////////////////////////////////////////////////////////
  124. // C3D_Matrix RotateZ
  125. //////////////////////////////////////////////////////////////////////////////////
  126. void C3D_Matrix::RotateZ(float rz)
  127. {
  128.     float cosine = cos(rz);
  129.     float sine = sin(rz);
  130.  
  131.     _11 = cosine;
  132.     _22 = cosine;
  133.     _21 = -sine;
  134.     _12 = sine;
  135. }
  136.  
  137. //////////////////////////////////////////////////////////////////////////////////
  138. // C3D_Matrix Translate
  139. //////////////////////////////////////////////////////////////////////////////////
  140. void C3D_Matrix::Translate(float dx, float dy, float dz)
  141. {
  142.     _41 = dx;
  143.     _42 = dy;
  144.     _43 = dz;
  145. }
  146.  
  147. //////////////////////////////////////////////////////////////////////////////////
  148. // C3D_Matrix Scale
  149. //////////////////////////////////////////////////////////////////////////////////
  150. void C3D_Matrix::Scale(float s)
  151. {
  152.     _11 = s;
  153.     _22 = s;
  154.     _33 = s;
  155. }
  156.  
  157. //////////////////////////////////////////////////////////////////////////////////
  158. // C3D_Matrix Scale
  159. //////////////////////////////////////////////////////////////////////////////////
  160. void C3D_Matrix::Scale(float sx, float sy, float sz)
  161. {
  162.     _11 = sx;
  163.     _22 = sy;
  164.     _33 = sz;
  165. }
  166.  
  167. //////////////////////////////////////////////////////////////////////////////////
  168. // C3D_Matrix Print
  169. //////////////////////////////////////////////////////////////////////////////////
  170. void C3D_Matrix::Print(void)
  171. {
  172.     printf("%f, %f, %f, %f\n", _11, _12, _13, 14);
  173.     printf("%f, %f, %f, %f\n", _21, _22, _23, 24);
  174.     printf("%f, %f, %f, %f\n", _31, _32, _33, 34);
  175.     printf("%f, %f, %f, %f\n", _41, _42, _43, 44);
  176. }
  177.  
  178. //////////////////////////////////////////////////////////////////////////////////
  179. // C3D_Matrix SetView
  180. //////////////////////////////////////////////////////////////////////////////////
  181. void C3D_Matrix::SetView(D3DVECTOR from, D3DVECTOR at, D3DVECTOR world_up, float roll)
  182. {
  183.     D3DVECTOR up, right, view;
  184.     Identity();
  185.  
  186.     view = Normalize(at - from);
  187.     right = CrossProduct(world_up, view);
  188.     up = CrossProduct(view, right);
  189.     right = Normalize(right);
  190.     up = Normalize(up);
  191.  
  192.     _11    = right.x;
  193.     _21    = right.y;
  194.     _31    = right.z;
  195.     _12    = up.x;
  196.     _22    = up.y;
  197.     _32    = up.z;
  198.     _13    = view.x;
  199.     _23    = view.y;
  200.     _33    = view.z;
  201.     _41    = -DotProduct(right, from);
  202.     _42    = -DotProduct(up, from);
  203.     _43    = -DotProduct(view, from);
  204.  
  205.     if(roll != 0.0f)
  206.     {
  207. //        C3D_Matrix mat.Rotate(-roll);
  208. //        this = Multiply(mat, this);
  209.     }
  210. }
  211.  
  212. //////////////////////////////////////////////////////////////////////////////////
  213. // C3D_Matrix SetProjection
  214. //////////////////////////////////////////////////////////////////////////////////
  215. void C3D_Matrix::SetProjection(float near_plane, float far_plane, float fov)
  216. {
  217.     float c = (float)cos(fov * 0.5);
  218.     float s = (float)sin(fov * 0.5);
  219.     float Q = s / (1.0f - near_plane / far_plane);
  220.  
  221.     Zero();
  222.     _11 = c;
  223.     _22 = c;
  224.     _33 = Q;
  225.     _43    = -Q * near_plane;
  226.     _34    = s;
  227. }
  228.