OgreMatrix4.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "OgreMatrix4.h"
  25. #include "OgreVector3.h"
  26. #include "OgreMatrix3.h"
  27. namespace Ogre
  28. {
  29. const Matrix4 Matrix4::ZERO(
  30. 0, 0, 0, 0,
  31. 0, 0, 0, 0,
  32. 0, 0, 0, 0,
  33. 0, 0, 0, 0 );
  34. const Matrix4 Matrix4::IDENTITY(
  35. 1, 0, 0, 0,
  36. 0, 1, 0, 0,
  37. 0, 0, 1, 0,
  38. 0, 0, 0, 1 );
  39. const Matrix4 Matrix4::CLIPSPACE2DTOIMAGESPACE(
  40. 0.5, 0, 0, 0.5,
  41. 0, -0.5, 0, 0.5,
  42. 0, 0, 1, 0,
  43. 0, 0, 0, 1);
  44. //-----------------------------------------------------------------------
  45. inline static Real
  46. MINOR(const Matrix4& m, const size_t r0, const size_t r1, const size_t r2,
  47. const size_t c0, const size_t c1, const size_t c2)
  48. {
  49. return m[r0][c0] * (m[r1][c1] * m[r2][c2] - m[r2][c1] * m[r1][c2]) -
  50. m[r0][c1] * (m[r1][c0] * m[r2][c2] - m[r2][c0] * m[r1][c2]) +
  51. m[r0][c2] * (m[r1][c0] * m[r2][c1] - m[r2][c0] * m[r1][c1]);
  52. }
  53. //-----------------------------------------------------------------------
  54. Matrix4 Matrix4::adjoint() const
  55. {
  56. return Matrix4( MINOR(*this, 1, 2, 3, 1, 2, 3),
  57. -MINOR(*this, 0, 2, 3, 1, 2, 3),
  58. MINOR(*this, 0, 1, 3, 1, 2, 3),
  59. -MINOR(*this, 0, 1, 2, 1, 2, 3),
  60. -MINOR(*this, 1, 2, 3, 0, 2, 3),
  61. MINOR(*this, 0, 2, 3, 0, 2, 3),
  62. -MINOR(*this, 0, 1, 3, 0, 2, 3),
  63. MINOR(*this, 0, 1, 2, 0, 2, 3),
  64. MINOR(*this, 1, 2, 3, 0, 1, 3),
  65. -MINOR(*this, 0, 2, 3, 0, 1, 3),
  66. MINOR(*this, 0, 1, 3, 0, 1, 3),
  67. -MINOR(*this, 0, 1, 2, 0, 1, 3),
  68. -MINOR(*this, 1, 2, 3, 0, 1, 2),
  69. MINOR(*this, 0, 2, 3, 0, 1, 2),
  70. -MINOR(*this, 0, 1, 3, 0, 1, 2),
  71. MINOR(*this, 0, 1, 2, 0, 1, 2));
  72. }
  73. //-----------------------------------------------------------------------
  74. Real Matrix4::determinant() const
  75. {
  76. return m[0][0] * MINOR(*this, 1, 2, 3, 1, 2, 3) -
  77. m[0][1] * MINOR(*this, 1, 2, 3, 0, 2, 3) +
  78. m[0][2] * MINOR(*this, 1, 2, 3, 0, 1, 3) -
  79. m[0][3] * MINOR(*this, 1, 2, 3, 0, 1, 2);
  80. }
  81. //-----------------------------------------------------------------------
  82. Matrix4 Matrix4::inverse() const
  83. {
  84. Real m00 = m[0][0], m01 = m[0][1], m02 = m[0][2], m03 = m[0][3];
  85. Real m10 = m[1][0], m11 = m[1][1], m12 = m[1][2], m13 = m[1][3];
  86. Real m20 = m[2][0], m21 = m[2][1], m22 = m[2][2], m23 = m[2][3];
  87. Real m30 = m[3][0], m31 = m[3][1], m32 = m[3][2], m33 = m[3][3];
  88. Real v0 = m20 * m31 - m21 * m30;
  89. Real v1 = m20 * m32 - m22 * m30;
  90. Real v2 = m20 * m33 - m23 * m30;
  91. Real v3 = m21 * m32 - m22 * m31;
  92. Real v4 = m21 * m33 - m23 * m31;
  93. Real v5 = m22 * m33 - m23 * m32;
  94. Real t00 = + (v5 * m11 - v4 * m12 + v3 * m13);
  95. Real t10 = - (v5 * m10 - v2 * m12 + v1 * m13);
  96. Real t20 = + (v4 * m10 - v2 * m11 + v0 * m13);
  97. Real t30 = - (v3 * m10 - v1 * m11 + v0 * m12);
  98. Real invDet = 1 / (t00 * m00 + t10 * m01 + t20 * m02 + t30 * m03);
  99. Real d00 = t00 * invDet;
  100. Real d10 = t10 * invDet;
  101. Real d20 = t20 * invDet;
  102. Real d30 = t30 * invDet;
  103. Real d01 = - (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
  104. Real d11 = + (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
  105. Real d21 = - (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
  106. Real d31 = + (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
  107. v0 = m10 * m31 - m11 * m30;
  108. v1 = m10 * m32 - m12 * m30;
  109. v2 = m10 * m33 - m13 * m30;
  110. v3 = m11 * m32 - m12 * m31;
  111. v4 = m11 * m33 - m13 * m31;
  112. v5 = m12 * m33 - m13 * m32;
  113. Real d02 = + (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
  114. Real d12 = - (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
  115. Real d22 = + (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
  116. Real d32 = - (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
  117. v0 = m21 * m10 - m20 * m11;
  118. v1 = m22 * m10 - m20 * m12;
  119. v2 = m23 * m10 - m20 * m13;
  120. v3 = m22 * m11 - m21 * m12;
  121. v4 = m23 * m11 - m21 * m13;
  122. v5 = m23 * m12 - m22 * m13;
  123. Real d03 = - (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
  124. Real d13 = + (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
  125. Real d23 = - (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
  126. Real d33 = + (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
  127. return Matrix4(
  128. d00, d01, d02, d03,
  129. d10, d11, d12, d13,
  130. d20, d21, d22, d23,
  131. d30, d31, d32, d33);
  132. }
  133. //-----------------------------------------------------------------------
  134. Matrix4 Matrix4::inverseAffine(void) const
  135. {
  136. assert(isAffine());
  137. Real m10 = m[1][0], m11 = m[1][1], m12 = m[1][2];
  138. Real m20 = m[2][0], m21 = m[2][1], m22 = m[2][2];
  139. Real t00 = m22 * m11 - m21 * m12;
  140. Real t10 = m20 * m12 - m22 * m10;
  141. Real t20 = m21 * m10 - m20 * m11;
  142. Real m00 = m[0][0], m01 = m[0][1], m02 = m[0][2];
  143. Real invDet = 1 / (m00 * t00 + m01 * t10 + m02 * t20);
  144. t00 *= invDet; t10 *= invDet; t20 *= invDet;
  145. m00 *= invDet; m01 *= invDet; m02 *= invDet;
  146. Real r00 = t00;
  147. Real r01 = m02 * m21 - m01 * m22;
  148. Real r02 = m01 * m12 - m02 * m11;
  149. Real r10 = t10;
  150. Real r11 = m00 * m22 - m02 * m20;
  151. Real r12 = m02 * m10 - m00 * m12;
  152. Real r20 = t20;
  153. Real r21 = m01 * m20 - m00 * m21;
  154. Real r22 = m00 * m11 - m01 * m10;
  155. Real m03 = m[0][3], m13 = m[1][3], m23 = m[2][3];
  156. Real r03 = - (r00 * m03 + r01 * m13 + r02 * m23);
  157. Real r13 = - (r10 * m03 + r11 * m13 + r12 * m23);
  158. Real r23 = - (r20 * m03 + r21 * m13 + r22 * m23);
  159. return Matrix4(
  160. r00, r01, r02, r03,
  161. r10, r11, r12, r13,
  162. r20, r21, r22, r23,
  163. 0, 0, 0, 1);
  164. }
  165. //-----------------------------------------------------------------------
  166. void Matrix4::makeTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation)
  167. {
  168. // Ordering:
  169. // 1. Scale
  170. // 2. Rotate
  171. // 3. Translate
  172. Matrix3 rot3x3;
  173. orientation.ToRotationMatrix(rot3x3);
  174. // Set up final matrix with scale, rotation and translation
  175. 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] = position.x;
  176. 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] = position.y;
  177. 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] = position.z;
  178. // No projection term
  179. m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
  180. }
  181. //-----------------------------------------------------------------------
  182. void Matrix4::makeInverseTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation)
  183. {
  184. // Invert the parameters
  185. Vector3 invTranslate = -position;
  186. Vector3 invScale(1 / scale.x, 1 / scale.y, 1 / scale.z);
  187. Quaternion invRot = orientation.Inverse();
  188. // Because we're inverting, order is translation, rotation, scale
  189. // So make translation relative to scale & rotation
  190. invTranslate = invRot * invTranslate; // rotate
  191. invTranslate *= invScale; // scale
  192. // Next, make a 3x3 rotation matrix
  193. Matrix3 rot3x3;
  194. invRot.ToRotationMatrix(rot3x3);
  195. // Set up final matrix with scale, rotation and translation
  196. 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;
  197. 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;
  198. 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;
  199. // No projection term
  200. m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
  201. }
  202. //-----------------------------------------------------------------------
  203. void Matrix4::decomposition(Vector3& position, Vector3& scale, Quaternion& orientation) const
  204. {
  205. assert(isAffine());
  206. Matrix3 m3x3;
  207. extract3x3Matrix(m3x3);
  208. Matrix3 matQ;
  209. Vector3 vecU;
  210. m3x3.QDUDecomposition( matQ, scale, vecU );
  211. orientation = Quaternion( matQ );
  212. position = Vector3( m[0][3], m[1][3], m[2][3] );
  213. }
  214. }