matrix4.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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 : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/matrix4.cpp $*
  25. * *
  26. * Org Author:: Greg_h *
  27. * *
  28. * Author : Kenny_m *
  29. * *
  30. * $Modtime:: 06/26/02 4:04p $*
  31. * *
  32. * $Revision:: 6 $*
  33. * *
  34. * 06/26/02 KM Matrix name change to avoid MAX conflicts *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * Matrix4x4::Multiply -- Multiply two Matrix4x4's together *
  38. * Matrix4x4::Multiply -- Multiply a Matrix3D * Matrix4x4 *
  39. * Matrix4x4::Multiply -- Multiply a Matrix4x4 * Matrix3D *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. #include "matrix4.h"
  42. #include <assert.h>
  43. /***********************************************************************************************
  44. * Matrix4x4::Multiply -- Multiply two Matrix4x4's together *
  45. * *
  46. * INPUT: *
  47. * a - first operand *
  48. * b - second operand *
  49. * res - pointer to matrix to store the result in (must not point to a or b) *
  50. * *
  51. * OUTPUT: *
  52. * *
  53. * WARNINGS: *
  54. * *
  55. * HISTORY: *
  56. * 11/13/99 gth : Created. *
  57. *=============================================================================================*/
  58. void Matrix4x4::Multiply(const Matrix4x4 &a,const Matrix4x4 &b,Matrix4x4 * res)
  59. {
  60. assert(res != &a);
  61. assert(res != &b);
  62. #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]
  63. (*res)[0][0] = ROWCOL(0,0);
  64. (*res)[0][1] = ROWCOL(0,1);
  65. (*res)[0][2] = ROWCOL(0,2);
  66. (*res)[0][3] = ROWCOL(0,3);
  67. (*res)[1][0] = ROWCOL(1,0);
  68. (*res)[1][1] = ROWCOL(1,1);
  69. (*res)[1][2] = ROWCOL(1,2);
  70. (*res)[1][3] = ROWCOL(1,3);
  71. (*res)[2][0] = ROWCOL(2,0);
  72. (*res)[2][1] = ROWCOL(2,1);
  73. (*res)[2][2] = ROWCOL(2,2);
  74. (*res)[2][3] = ROWCOL(2,3);
  75. (*res)[3][0] = ROWCOL(3,0);
  76. (*res)[3][1] = ROWCOL(3,1);
  77. (*res)[3][2] = ROWCOL(3,2);
  78. (*res)[3][3] = ROWCOL(3,3);
  79. #undef ROWCOL
  80. }
  81. /***********************************************************************************************
  82. * Matrix4x4::Multiply -- Multiply a Matrix3D * Matrix4x4 *
  83. * *
  84. * INPUT: *
  85. * *
  86. * OUTPUT: *
  87. * *
  88. * WARNINGS: *
  89. * *
  90. * HISTORY: *
  91. * 11/13/99 gth : Created. *
  92. *=============================================================================================*/
  93. void Matrix4x4::Multiply(const Matrix3D &a,const Matrix4x4 &b,Matrix4x4 * res)
  94. {
  95. assert(res != &b);
  96. #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]
  97. (*res)[0][0] = ROWCOL(0,0);
  98. (*res)[0][1] = ROWCOL(0,1);
  99. (*res)[0][2] = ROWCOL(0,2);
  100. (*res)[0][3] = ROWCOL(0,3);
  101. (*res)[1][0] = ROWCOL(1,0);
  102. (*res)[1][1] = ROWCOL(1,1);
  103. (*res)[1][2] = ROWCOL(1,2);
  104. (*res)[1][3] = ROWCOL(1,3);
  105. (*res)[2][0] = ROWCOL(2,0);
  106. (*res)[2][1] = ROWCOL(2,1);
  107. (*res)[2][2] = ROWCOL(2,2);
  108. (*res)[2][3] = ROWCOL(2,3);
  109. (*res)[3][0] = b[3][0]; // last row of a is 0,0,0,1
  110. (*res)[3][1] = b[3][1]; // this leaves the last row of b unchanged
  111. (*res)[3][2] = b[3][2];
  112. (*res)[3][3] = b[3][3];
  113. #undef ROWCOL
  114. }
  115. /***********************************************************************************************
  116. * Matrix4x4::Multiply -- Multiply a Matrix4x4 * Matrix3D *
  117. * *
  118. * INPUT: *
  119. * *
  120. * OUTPUT: *
  121. * *
  122. * WARNINGS: *
  123. * *
  124. * HISTORY: *
  125. *=============================================================================================*/
  126. void Matrix4x4::Multiply(const Matrix4x4 & a,const Matrix3D & b,Matrix4x4 * res)
  127. {
  128. assert(res != &a);
  129. // ROWCOL multiplies a row of 'a' by one of the first three columns of 'b' (4th entry in b is zero)
  130. // ROWCOL4 multiplies a row of 'a' by the fourth column of 'b' (4th entry in b is one)
  131. #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
  132. #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]
  133. (*res)[0][0] = ROWCOL(0,0);
  134. (*res)[0][1] = ROWCOL(0,1);
  135. (*res)[0][2] = ROWCOL(0,2);
  136. (*res)[0][3] = ROWCOL4(0,3);
  137. (*res)[1][0] = ROWCOL(1,0);
  138. (*res)[1][1] = ROWCOL(1,1);
  139. (*res)[1][2] = ROWCOL(1,2);
  140. (*res)[1][3] = ROWCOL4(1,3);
  141. (*res)[2][0] = ROWCOL(2,0);
  142. (*res)[2][1] = ROWCOL(2,1);
  143. (*res)[2][2] = ROWCOL(2,2);
  144. (*res)[2][3] = ROWCOL4(2,3);
  145. (*res)[3][0] = ROWCOL(3,0);
  146. (*res)[3][1] = ROWCOL(3,1);
  147. (*res)[3][2] = ROWCOL(3,2);
  148. (*res)[3][3] = ROWCOL4(3,3);
  149. #undef ROWCOL
  150. #undef ROWCOL4
  151. }
  152. int operator == (const Matrix4x4 & a, const Matrix4x4 & b)
  153. {
  154. unsigned* m1=(unsigned*)&a;
  155. unsigned* m2=(unsigned*)&b;
  156. unsigned res=0;
  157. for (int i=0;i<4;++i) {
  158. res|=*m1++^*m2++;
  159. res|=*m1++^*m2++;
  160. res|=*m1++^*m2++;
  161. res|=*m1++^*m2++;
  162. }
  163. return !res;
  164. }
  165. int operator != (const Matrix4x4 & a, const Matrix4x4 & b)
  166. {
  167. return (!(a == b));
  168. }