2
0

BsMatrix4.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "BsMatrix4.h"
  2. #include "BsVector3.h"
  3. #include "BsMatrix3.h"
  4. #include "BsQuaternion.h"
  5. namespace BansheeEngine
  6. {
  7. const Matrix4 Matrix4::ZERO(
  8. 0.0f, 0.0f, 0.0f, 0.0f,
  9. 0.0f, 0.0f, 0.0f, 0.0f,
  10. 0.0f, 0.0f, 0.0f, 0.0f,
  11. 0.0f, 0.0f, 0.0f, 0.0f);
  12. const Matrix4 Matrix4::IDENTITY(
  13. 1.0f, 0.0f, 0.0f, 0.0f,
  14. 0.0f, 1.0f, 0.0f, 0.0f,
  15. 0.0f, 0.0f, 1.0f, 0.0f,
  16. 0.0f, 0.0f, 0.0f, 1.0f);
  17. static float MINOR(const Matrix4& m, const UINT32 r0, const UINT32 r1, const UINT32 r2,
  18. const UINT32 c0, const UINT32 c1, const UINT32 c2)
  19. {
  20. return m[r0][c0] * (m[r1][c1] * m[r2][c2] - m[r2][c1] * m[r1][c2]) -
  21. m[r0][c1] * (m[r1][c0] * m[r2][c2] - m[r2][c0] * m[r1][c2]) +
  22. m[r0][c2] * (m[r1][c0] * m[r2][c1] - m[r2][c0] * m[r1][c1]);
  23. }
  24. Matrix4 Matrix4::adjoint() const
  25. {
  26. return Matrix4( MINOR(*this, 1, 2, 3, 1, 2, 3),
  27. -MINOR(*this, 0, 2, 3, 1, 2, 3),
  28. MINOR(*this, 0, 1, 3, 1, 2, 3),
  29. -MINOR(*this, 0, 1, 2, 1, 2, 3),
  30. -MINOR(*this, 1, 2, 3, 0, 2, 3),
  31. MINOR(*this, 0, 2, 3, 0, 2, 3),
  32. -MINOR(*this, 0, 1, 3, 0, 2, 3),
  33. MINOR(*this, 0, 1, 2, 0, 2, 3),
  34. MINOR(*this, 1, 2, 3, 0, 1, 3),
  35. -MINOR(*this, 0, 2, 3, 0, 1, 3),
  36. MINOR(*this, 0, 1, 3, 0, 1, 3),
  37. -MINOR(*this, 0, 1, 2, 0, 1, 3),
  38. -MINOR(*this, 1, 2, 3, 0, 1, 2),
  39. MINOR(*this, 0, 2, 3, 0, 1, 2),
  40. -MINOR(*this, 0, 1, 3, 0, 1, 2),
  41. MINOR(*this, 0, 1, 2, 0, 1, 2));
  42. }
  43. float Matrix4::determinant() const
  44. {
  45. return m[0][0] * MINOR(*this, 1, 2, 3, 1, 2, 3) -
  46. m[0][1] * MINOR(*this, 1, 2, 3, 0, 2, 3) +
  47. m[0][2] * MINOR(*this, 1, 2, 3, 0, 1, 3) -
  48. m[0][3] * MINOR(*this, 1, 2, 3, 0, 1, 2);
  49. }
  50. Matrix4 Matrix4::inverse() const
  51. {
  52. float m00 = m[0][0], m01 = m[0][1], m02 = m[0][2], m03 = m[0][3];
  53. float m10 = m[1][0], m11 = m[1][1], m12 = m[1][2], m13 = m[1][3];
  54. float m20 = m[2][0], m21 = m[2][1], m22 = m[2][2], m23 = m[2][3];
  55. float m30 = m[3][0], m31 = m[3][1], m32 = m[3][2], m33 = m[3][3];
  56. float v0 = m20 * m31 - m21 * m30;
  57. float v1 = m20 * m32 - m22 * m30;
  58. float v2 = m20 * m33 - m23 * m30;
  59. float v3 = m21 * m32 - m22 * m31;
  60. float v4 = m21 * m33 - m23 * m31;
  61. float v5 = m22 * m33 - m23 * m32;
  62. float t00 = + (v5 * m11 - v4 * m12 + v3 * m13);
  63. float t10 = - (v5 * m10 - v2 * m12 + v1 * m13);
  64. float t20 = + (v4 * m10 - v2 * m11 + v0 * m13);
  65. float t30 = - (v3 * m10 - v1 * m11 + v0 * m12);
  66. float invDet = 1 / (t00 * m00 + t10 * m01 + t20 * m02 + t30 * m03);
  67. float d00 = t00 * invDet;
  68. float d10 = t10 * invDet;
  69. float d20 = t20 * invDet;
  70. float d30 = t30 * invDet;
  71. float d01 = - (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
  72. float d11 = + (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
  73. float d21 = - (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
  74. float d31 = + (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
  75. v0 = m10 * m31 - m11 * m30;
  76. v1 = m10 * m32 - m12 * m30;
  77. v2 = m10 * m33 - m13 * m30;
  78. v3 = m11 * m32 - m12 * m31;
  79. v4 = m11 * m33 - m13 * m31;
  80. v5 = m12 * m33 - m13 * m32;
  81. float d02 = + (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
  82. float d12 = - (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
  83. float d22 = + (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
  84. float d32 = - (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
  85. v0 = m21 * m10 - m20 * m11;
  86. v1 = m22 * m10 - m20 * m12;
  87. v2 = m23 * m10 - m20 * m13;
  88. v3 = m22 * m11 - m21 * m12;
  89. v4 = m23 * m11 - m21 * m13;
  90. v5 = m23 * m12 - m22 * m13;
  91. float d03 = - (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
  92. float d13 = + (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
  93. float d23 = - (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
  94. float d33 = + (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
  95. return Matrix4(
  96. d00, d01, d02, d03,
  97. d10, d11, d12, d13,
  98. d20, d21, d22, d23,
  99. d30, d31, d32, d33);
  100. }
  101. Matrix4 Matrix4::inverseAffine() const
  102. {
  103. assert(isAffine());
  104. float m10 = m[1][0], m11 = m[1][1], m12 = m[1][2];
  105. float m20 = m[2][0], m21 = m[2][1], m22 = m[2][2];
  106. float t00 = m22 * m11 - m21 * m12;
  107. float t10 = m20 * m12 - m22 * m10;
  108. float t20 = m21 * m10 - m20 * m11;
  109. float m00 = m[0][0], m01 = m[0][1], m02 = m[0][2];
  110. float invDet = 1 / (m00 * t00 + m01 * t10 + m02 * t20);
  111. t00 *= invDet; t10 *= invDet; t20 *= invDet;
  112. m00 *= invDet; m01 *= invDet; m02 *= invDet;
  113. float r00 = t00;
  114. float r01 = m02 * m21 - m01 * m22;
  115. float r02 = m01 * m12 - m02 * m11;
  116. float r10 = t10;
  117. float r11 = m00 * m22 - m02 * m20;
  118. float r12 = m02 * m10 - m00 * m12;
  119. float r20 = t20;
  120. float r21 = m01 * m20 - m00 * m21;
  121. float r22 = m00 * m11 - m01 * m10;
  122. float m03 = m[0][3], m13 = m[1][3], m23 = m[2][3];
  123. float r03 = - (r00 * m03 + r01 * m13 + r02 * m23);
  124. float r13 = - (r10 * m03 + r11 * m13 + r12 * m23);
  125. float r23 = - (r20 * m03 + r21 * m13 + r22 * m23);
  126. return Matrix4(
  127. r00, r01, r02, r03,
  128. r10, r11, r12, r13,
  129. r20, r21, r22, r23,
  130. 0, 0, 0, 1);
  131. }
  132. void Matrix4::setTRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
  133. {
  134. Matrix3 rot3x3;
  135. rotation.toRotationMatrix(rot3x3);
  136. m[0][0] = scale.x * rot3x3[0][0]; m[0][1] = scale.y * rot3x3[0][1]; m[0][2] = scale.z * rot3x3[0][2]; m[0][3] = translation.x;
  137. m[1][0] = scale.x * rot3x3[1][0]; m[1][1] = scale.y * rot3x3[1][1]; m[1][2] = scale.z * rot3x3[1][2]; m[1][3] = translation.y;
  138. m[2][0] = scale.x * rot3x3[2][0]; m[2][1] = scale.y * rot3x3[2][1]; m[2][2] = scale.z * rot3x3[2][2]; m[2][3] = translation.z;
  139. // No projection term
  140. m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
  141. }
  142. void Matrix4::setInverseTRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale)
  143. {
  144. // Invert the parameters
  145. Vector3 invTranslate = -translation;
  146. Vector3 invScale(1 / scale.x, 1 / scale.y, 1 / scale.z);
  147. Quaternion invRot = rotation.inverse();
  148. // Because we're inverting, order is translation, rotation, scale
  149. // So make translation relative to scale & rotation
  150. invTranslate = invRot.rotate(invTranslate);
  151. invTranslate *= invScale;
  152. // Next, make a 3x3 rotation matrix
  153. Matrix3 rot3x3;
  154. invRot.toRotationMatrix(rot3x3);
  155. // Set up final matrix with scale, rotation and translation
  156. m[0][0] = invScale.x * rot3x3[0][0]; m[0][1] = invScale.x * rot3x3[0][1]; m[0][2] = invScale.x * rot3x3[0][2]; m[0][3] = invTranslate.x;
  157. m[1][0] = invScale.y * rot3x3[1][0]; m[1][1] = invScale.y * rot3x3[1][1]; m[1][2] = invScale.y * rot3x3[1][2]; m[1][3] = invTranslate.y;
  158. m[2][0] = invScale.z * rot3x3[2][0]; m[2][1] = invScale.z * rot3x3[2][1]; m[2][2] = invScale.z * rot3x3[2][2]; m[2][3] = invTranslate.z;
  159. // No projection term
  160. m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
  161. }
  162. void Matrix4::decomposition(Vector3& position, Quaternion& rotation, Vector3& scale) const
  163. {
  164. Matrix3 m3x3;
  165. extract3x3Matrix(m3x3);
  166. Matrix3 matQ;
  167. Vector3 vecU;
  168. m3x3.QDUDecomposition(matQ, scale, vecU);
  169. rotation = Quaternion(matQ);
  170. position = Vector3(m[0][3], m[1][3], m[2][3]);
  171. }
  172. void Matrix4::makeView(const Vector3& position, const Quaternion& orientation, const Matrix4* reflectMatrix)
  173. {
  174. // View matrix is:
  175. //
  176. // [ Lx Uy Dz Tx ]
  177. // [ Lx Uy Dz Ty ]
  178. // [ Lx Uy Dz Tz ]
  179. // [ 0 0 0 1 ]
  180. //
  181. // Where T = -(Transposed(Rot) * Pos)
  182. // This is most efficiently done using 3x3 Matrices
  183. Matrix3 rot;
  184. orientation.toRotationMatrix(rot);
  185. // Make the translation relative to new axes
  186. Matrix3 rotT = rot.transpose();
  187. Vector3 trans = (-rotT).transform(position);
  188. // Make final matrix
  189. *this = Matrix4(rotT);
  190. m[0][3] = trans.x;
  191. m[1][3] = trans.y;
  192. m[2][3] = trans.z;
  193. // Deal with reflections
  194. if (reflectMatrix)
  195. {
  196. *this = (*this) * (*reflectMatrix);
  197. }
  198. }
  199. }