matrix4.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WW3D PS2 *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/W3DShellExt/External/matrix4.cpp $*
  25. * *
  26. * Programmer : Kenny Mitchell *
  27. * *
  28. * Start Date : 11/16/99 *
  29. * *
  30. * Last Update : 11/16/99 *
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Based on Greg Hjelstrom 97 *
  34. * Functions: *
  35. * Matrix4::Multiply -- Multiply two Matrix4's together *
  36. * Matrix4::Multiply -- Multiply a Matrix3D * Matrix4 *
  37. * Matrix4::Multiply -- Multiply a Matrix4 * Matrix3D *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include <assert.h>
  40. #include "matrix4.h"
  41. #if 0
  42. /***********************************************************************************************
  43. * Matrix4::Multiply -- Multiply two Matrix4's together *
  44. * *
  45. * INPUT: *
  46. * a - first operand *
  47. * b - second operand *
  48. * res - pointer to matrix to store the result in (must not point to a or b) *
  49. * *
  50. * OUTPUT: *
  51. * *
  52. * WARNINGS: *
  53. * *
  54. * HISTORY: *
  55. * 11/13/99 gth : Created. *
  56. *=============================================================================================*/
  57. void Matrix4::Multiply(const Matrix4 &a,const Matrix4 &b,Matrix4 * res)
  58. {
  59. assert(res != &a);
  60. assert(res != &b);
  61. #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j] + a[i][3]*b[3][j]
  62. (*res)[0][0] = ROWCOL(0,0);
  63. (*res)[0][1] = ROWCOL(0,1);
  64. (*res)[0][2] = ROWCOL(0,2);
  65. (*res)[0][3] = ROWCOL(0,3);
  66. (*res)[1][0] = ROWCOL(1,0);
  67. (*res)[1][1] = ROWCOL(1,1);
  68. (*res)[1][2] = ROWCOL(1,2);
  69. (*res)[1][3] = ROWCOL(1,3);
  70. (*res)[2][0] = ROWCOL(2,0);
  71. (*res)[2][1] = ROWCOL(2,1);
  72. (*res)[2][2] = ROWCOL(2,2);
  73. (*res)[2][3] = ROWCOL(2,3);
  74. (*res)[3][0] = ROWCOL(3,0);
  75. (*res)[3][1] = ROWCOL(3,1);
  76. (*res)[3][2] = ROWCOL(3,2);
  77. (*res)[3][3] = ROWCOL(3,3);
  78. #undef ROWCOL
  79. }
  80. /***********************************************************************************************
  81. * Matrix4::Multiply -- Multiply a Matrix3D * Matrix4 *
  82. * *
  83. * INPUT: *
  84. * *
  85. * OUTPUT: *
  86. * *
  87. * WARNINGS: *
  88. * *
  89. * HISTORY: *
  90. * 11/13/99 gth : Created. *
  91. *=============================================================================================*/
  92. void Matrix4::Multiply(const Matrix3D &a,const Matrix4 &b,Matrix4 * res)
  93. {
  94. assert(res != &b);
  95. #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j] + a[i][3]*b[3][j]
  96. (*res)[0][0] = ROWCOL(0,0);
  97. (*res)[0][1] = ROWCOL(0,1);
  98. (*res)[0][2] = ROWCOL(0,2);
  99. (*res)[0][3] = ROWCOL(0,3);
  100. (*res)[1][0] = ROWCOL(1,0);
  101. (*res)[1][1] = ROWCOL(1,1);
  102. (*res)[1][2] = ROWCOL(1,2);
  103. (*res)[1][3] = ROWCOL(1,3);
  104. (*res)[2][0] = ROWCOL(2,0);
  105. (*res)[2][1] = ROWCOL(2,1);
  106. (*res)[2][2] = ROWCOL(2,2);
  107. (*res)[2][3] = ROWCOL(2,3);
  108. (*res)[3][0] = b[3][0]; // last row of a is 0,0,0,1
  109. (*res)[3][1] = b[3][1]; // this leaves the last row of b unchanged
  110. (*res)[3][2] = b[3][2];
  111. (*res)[3][3] = b[3][3];
  112. #undef ROWCOL
  113. }
  114. /***********************************************************************************************
  115. * Matrix4::Multiply -- Multiply a Matrix4 * Matrix3D *
  116. * *
  117. * INPUT: *
  118. * *
  119. * OUTPUT: *
  120. * *
  121. * WARNINGS: *
  122. * *
  123. * HISTORY: *
  124. *=============================================================================================*/
  125. void Matrix4::Multiply(const Matrix4 & a,const Matrix3D & b,Matrix4 * res)
  126. {
  127. assert(res != &a);
  128. // ROWCOL multiplies a row of 'a' by one of the first three columns of 'b' (4th entry in b is zero)
  129. // ROWCOL4 multiplies a row of 'a' by the fourth column of 'b' (4th entry in b is one)
  130. #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
  131. #define ROWCOL4(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j] + a[i][3]
  132. (*res)[0][0] = ROWCOL(0,0);
  133. (*res)[0][1] = ROWCOL(0,1);
  134. (*res)[0][2] = ROWCOL(0,2);
  135. (*res)[0][3] = ROWCOL4(0,3);
  136. (*res)[1][0] = ROWCOL(1,0);
  137. (*res)[1][1] = ROWCOL(1,1);
  138. (*res)[1][2] = ROWCOL(1,2);
  139. (*res)[1][3] = ROWCOL4(1,3);
  140. (*res)[2][0] = ROWCOL(2,0);
  141. (*res)[2][1] = ROWCOL(2,1);
  142. (*res)[2][2] = ROWCOL(2,2);
  143. (*res)[2][3] = ROWCOL4(2,3);
  144. (*res)[3][0] = ROWCOL(3,0);
  145. (*res)[3][1] = ROWCOL(3,1);
  146. (*res)[3][2] = ROWCOL(3,2);
  147. (*res)[3][3] = ROWCOL4(3,3);
  148. #undef ROWCOL
  149. #undef ROWCOL4
  150. }
  151. /***********************************************************************************************
  152. * Set_Perspective -- Sets perspective matrix *
  153. // Desc: Sets the passed in 4x4 matrix to a perpsective projection matrix built
  154. // from the field-of-view (fov, in y), aspect ratio, near plane (D),
  155. // and far plane (F).
  156. * *
  157. * HISTORY: *
  158. * 11/16/99 KJM: Created. *
  159. *=============================================================================================*/
  160. void Matrix4::Set_Perspective
  161. (
  162. Matrix4* m,
  163. float fFOV,
  164. float ScreenWidth,
  165. float ScreenHeight,
  166. float fNearPlane,
  167. float fFarPlane
  168. )
  169. {
  170. float w;
  171. float h;
  172. float Q,Q2;
  173. Q=DEGTORAD*0.5f;
  174. float c = cosf(fFOV*Q);
  175. float s = sinf(fFOV*Q);
  176. //printf("sincos %f %f\n",c,s);
  177. Q=0.000244140625f; // 1/4096
  178. w = (ScreenWidth*Q);
  179. h = (ScreenHeight*Q);
  180. Q = s/(1.0f - (fNearPlane/fFarPlane));
  181. Q2 = -Q*fNearPlane;
  182. m->Make_Identity();
  183. m->Row[0][0]= c*w;
  184. m->Row[1][1]= -c*h;
  185. m->Row[2][2]= Q;
  186. m->Row[2][3]= s;//1.0f;
  187. m->Row[3][2]= Q2;
  188. }
  189. void Matrix4::Set_View_Matrix
  190. (
  191. Matrix4* m,
  192. Vector3& vFrom,
  193. Vector3& vAt,
  194. Vector3& vWorldUp
  195. )
  196. {
  197. Vector3 vView;
  198. Vector3 vUp;
  199. Vector3 vRight;
  200. float inv_length;
  201. float dot_product;
  202. // Get the z basis vector, which points straight ahead. This is the
  203. // difference from the eyepoint to the lookat point.
  204. vView=vAt-vFrom;
  205. inv_length=Inv_Sqrt(vView.Length2());
  206. // Normalize the z basis vector
  207. vView*=inv_length;
  208. // Get the dot product, and calculate the projection of the z basis
  209. // vector onto the up vector. The projection is the y basis vector.
  210. dot_product=Vector3::Dot_Product(vWorldUp,vView);
  211. // vUp = vWorldUp - fDotProduct * vView
  212. vUp=vWorldUp-(dot_product*vView);
  213. inv_length=Inv_Sqrt(vUp.Length2());
  214. // Normalize the y basis vector
  215. vUp*=inv_length;
  216. // The x basis vector is found simply with the cross product of the y
  217. // and z basis vectors
  218. Vector3::Cross_Product(vUp,vView,&vRight);
  219. // Start building the matrix. The first three rows contains the basis
  220. // vectors used to rotate the view to point at the lookat point
  221. m->Row[0][0] = vRight.x; m->Row[0][1] = vUp.x; m->Row[0][2] = vView.x; m->Row[0][3] = 0.0f;
  222. m->Row[1][0] = vRight.y; m->Row[1][1] = vUp.y; m->Row[1][2] = vView.y; m->Row[1][3] = 0.0f;
  223. m->Row[2][0] = vRight.z; m->Row[2][1] = vUp.z; m->Row[2][2] = vView.z; m->Row[2][3] = 0.0f;
  224. // Do the translation values (rotations are still about the eyepoint)
  225. m->Row[3][0] = - (vFrom * vRight);
  226. m->Row[3][1] = - (vFrom * vUp);
  227. m->Row[3][2] = - (vFrom * vView);
  228. m->Row[3][3] = 1.0f;
  229. }
  230. void Matrix4::Print_Matrix() const
  231. {
  232. printf("%f %f %f %f\n",Row[0][0],Row[0][1],Row[0][2],Row[0][3]);
  233. printf("%f %f %f %f\n",Row[1][0],Row[1][1],Row[1][2],Row[1][3]);
  234. printf("%f %f %f %f\n",Row[2][0],Row[2][1],Row[2][2],Row[2][3]);
  235. printf("%f %f %f %f\n",Row[3][0],Row[3][1],Row[3][2],Row[3][3]);
  236. }
  237. #endif