matrix4.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. ** Command & Conquer Generals(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. * Author:: Greg_h *
  27. * *
  28. * $Modtime:: 11/13/99 10:50a $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Matrix4::Multiply -- Multiply two Matrix4's together *
  35. * Matrix4::Multiply -- Multiply a Matrix3D * Matrix4 *
  36. * Matrix4::Multiply -- Multiply a Matrix4 * Matrix3D *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "matrix4.h"
  39. #include <assert.h>
  40. /***********************************************************************************************
  41. * Matrix4::Multiply -- Multiply two Matrix4's together *
  42. * *
  43. * INPUT: *
  44. * a - first operand *
  45. * b - second operand *
  46. * res - pointer to matrix to store the result in (must not point to a or b) *
  47. * *
  48. * OUTPUT: *
  49. * *
  50. * WARNINGS: *
  51. * *
  52. * HISTORY: *
  53. * 11/13/99 gth : Created. *
  54. *=============================================================================================*/
  55. void Matrix4::Multiply(const Matrix4 &a,const Matrix4 &b,Matrix4 * res)
  56. {
  57. assert(res != &a);
  58. assert(res != &b);
  59. #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]
  60. (*res)[0][0] = ROWCOL(0,0);
  61. (*res)[0][1] = ROWCOL(0,1);
  62. (*res)[0][2] = ROWCOL(0,2);
  63. (*res)[0][3] = ROWCOL(0,3);
  64. (*res)[1][0] = ROWCOL(1,0);
  65. (*res)[1][1] = ROWCOL(1,1);
  66. (*res)[1][2] = ROWCOL(1,2);
  67. (*res)[1][3] = ROWCOL(1,3);
  68. (*res)[2][0] = ROWCOL(2,0);
  69. (*res)[2][1] = ROWCOL(2,1);
  70. (*res)[2][2] = ROWCOL(2,2);
  71. (*res)[2][3] = ROWCOL(2,3);
  72. (*res)[3][0] = ROWCOL(3,0);
  73. (*res)[3][1] = ROWCOL(3,1);
  74. (*res)[3][2] = ROWCOL(3,2);
  75. (*res)[3][3] = ROWCOL(3,3);
  76. #undef ROWCOL
  77. }
  78. /***********************************************************************************************
  79. * Matrix4::Multiply -- Multiply a Matrix3D * Matrix4 *
  80. * *
  81. * INPUT: *
  82. * *
  83. * OUTPUT: *
  84. * *
  85. * WARNINGS: *
  86. * *
  87. * HISTORY: *
  88. * 11/13/99 gth : Created. *
  89. *=============================================================================================*/
  90. void Matrix4::Multiply(const Matrix3D &a,const Matrix4 &b,Matrix4 * res)
  91. {
  92. assert(res != &b);
  93. #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]
  94. (*res)[0][0] = ROWCOL(0,0);
  95. (*res)[0][1] = ROWCOL(0,1);
  96. (*res)[0][2] = ROWCOL(0,2);
  97. (*res)[0][3] = ROWCOL(0,3);
  98. (*res)[1][0] = ROWCOL(1,0);
  99. (*res)[1][1] = ROWCOL(1,1);
  100. (*res)[1][2] = ROWCOL(1,2);
  101. (*res)[1][3] = ROWCOL(1,3);
  102. (*res)[2][0] = ROWCOL(2,0);
  103. (*res)[2][1] = ROWCOL(2,1);
  104. (*res)[2][2] = ROWCOL(2,2);
  105. (*res)[2][3] = ROWCOL(2,3);
  106. (*res)[3][0] = b[3][0]; // last row of a is 0,0,0,1
  107. (*res)[3][1] = b[3][1]; // this leaves the last row of b unchanged
  108. (*res)[3][2] = b[3][2];
  109. (*res)[3][3] = b[3][3];
  110. #undef ROWCOL
  111. }
  112. /***********************************************************************************************
  113. * Matrix4::Multiply -- Multiply a Matrix4 * Matrix3D *
  114. * *
  115. * INPUT: *
  116. * *
  117. * OUTPUT: *
  118. * *
  119. * WARNINGS: *
  120. * *
  121. * HISTORY: *
  122. *=============================================================================================*/
  123. void Matrix4::Multiply(const Matrix4 & a,const Matrix3D & b,Matrix4 * res)
  124. {
  125. assert(res != &a);
  126. // ROWCOL multiplies a row of 'a' by one of the first three columns of 'b' (4th entry in b is zero)
  127. // ROWCOL4 multiplies a row of 'a' by the fourth column of 'b' (4th entry in b is one)
  128. #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
  129. #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]
  130. (*res)[0][0] = ROWCOL(0,0);
  131. (*res)[0][1] = ROWCOL(0,1);
  132. (*res)[0][2] = ROWCOL(0,2);
  133. (*res)[0][3] = ROWCOL4(0,3);
  134. (*res)[1][0] = ROWCOL(1,0);
  135. (*res)[1][1] = ROWCOL(1,1);
  136. (*res)[1][2] = ROWCOL(1,2);
  137. (*res)[1][3] = ROWCOL4(1,3);
  138. (*res)[2][0] = ROWCOL(2,0);
  139. (*res)[2][1] = ROWCOL(2,1);
  140. (*res)[2][2] = ROWCOL(2,2);
  141. (*res)[2][3] = ROWCOL4(2,3);
  142. (*res)[3][0] = ROWCOL(3,0);
  143. (*res)[3][1] = ROWCOL(3,1);
  144. (*res)[3][2] = ROWCOL(3,2);
  145. (*res)[3][3] = ROWCOL4(3,3);
  146. #undef ROWCOL
  147. #undef ROWCOL4
  148. }