BsMatrix4.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. #include "BsMatrix3.h"
  5. #include "BsVector4.h"
  6. #include "BsPlane.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Math
  10. * @{
  11. */
  12. /** Class representing a 4x4 matrix. */
  13. class BS_UTILITY_EXPORT Matrix4
  14. {
  15. private:
  16. union
  17. {
  18. float m[4][4];
  19. float _m[16];
  20. };
  21. public:
  22. Matrix4()
  23. { }
  24. Matrix4(
  25. float m00, float m01, float m02, float m03,
  26. float m10, float m11, float m12, float m13,
  27. float m20, float m21, float m22, float m23,
  28. float m30, float m31, float m32, float m33)
  29. {
  30. m[0][0] = m00;
  31. m[0][1] = m01;
  32. m[0][2] = m02;
  33. m[0][3] = m03;
  34. m[1][0] = m10;
  35. m[1][1] = m11;
  36. m[1][2] = m12;
  37. m[1][3] = m13;
  38. m[2][0] = m20;
  39. m[2][1] = m21;
  40. m[2][2] = m22;
  41. m[2][3] = m23;
  42. m[3][0] = m30;
  43. m[3][1] = m31;
  44. m[3][2] = m32;
  45. m[3][3] = m33;
  46. }
  47. Matrix4(const Matrix4& mat)
  48. {
  49. memcpy(_m, mat._m, 16*sizeof(float));
  50. }
  51. /** Creates a 4x4 transformation matrix with a zero translation part from a rotation/scaling 3x3 matrix. */
  52. explicit Matrix4(const Matrix3& mat3)
  53. {
  54. m[0][0] = mat3.m[0][0]; m[0][1] = mat3.m[0][1]; m[0][2] = mat3.m[0][2]; m[0][3] = 0.0f;
  55. m[1][0] = mat3.m[1][0]; m[1][1] = mat3.m[1][1]; m[1][2] = mat3.m[1][2]; m[1][3] = 0.0f;
  56. m[2][0] = mat3.m[2][0]; m[2][1] = mat3.m[2][1]; m[2][2] = mat3.m[2][2]; m[2][3] = 0.0f;
  57. m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 0.0f; m[3][3] = 1.0f;
  58. }
  59. /** Swaps the contents of this matrix with another. */
  60. void swap(Matrix4& other)
  61. {
  62. std::swap(m[0][0], other.m[0][0]);
  63. std::swap(m[0][1], other.m[0][1]);
  64. std::swap(m[0][2], other.m[0][2]);
  65. std::swap(m[0][3], other.m[0][3]);
  66. std::swap(m[1][0], other.m[1][0]);
  67. std::swap(m[1][1], other.m[1][1]);
  68. std::swap(m[1][2], other.m[1][2]);
  69. std::swap(m[1][3], other.m[1][3]);
  70. std::swap(m[2][0], other.m[2][0]);
  71. std::swap(m[2][1], other.m[2][1]);
  72. std::swap(m[2][2], other.m[2][2]);
  73. std::swap(m[2][3], other.m[2][3]);
  74. std::swap(m[3][0], other.m[3][0]);
  75. std::swap(m[3][1], other.m[3][1]);
  76. std::swap(m[3][2], other.m[3][2]);
  77. std::swap(m[3][3], other.m[3][3]);
  78. }
  79. /** Returns a row of the matrix. */
  80. float* operator[] (UINT32 row)
  81. {
  82. assert(row < 4);
  83. return m[row];
  84. }
  85. const float *operator[] (UINT32 row) const
  86. {
  87. assert(row < 4);
  88. return m[row];
  89. }
  90. Matrix4 operator* (const Matrix4 &rhs) const
  91. {
  92. Matrix4 r;
  93. r.m[0][0] = m[0][0] * rhs.m[0][0] + m[0][1] * rhs.m[1][0] + m[0][2] * rhs.m[2][0] + m[0][3] * rhs.m[3][0];
  94. r.m[0][1] = m[0][0] * rhs.m[0][1] + m[0][1] * rhs.m[1][1] + m[0][2] * rhs.m[2][1] + m[0][3] * rhs.m[3][1];
  95. r.m[0][2] = m[0][0] * rhs.m[0][2] + m[0][1] * rhs.m[1][2] + m[0][2] * rhs.m[2][2] + m[0][3] * rhs.m[3][2];
  96. r.m[0][3] = m[0][0] * rhs.m[0][3] + m[0][1] * rhs.m[1][3] + m[0][2] * rhs.m[2][3] + m[0][3] * rhs.m[3][3];
  97. r.m[1][0] = m[1][0] * rhs.m[0][0] + m[1][1] * rhs.m[1][0] + m[1][2] * rhs.m[2][0] + m[1][3] * rhs.m[3][0];
  98. r.m[1][1] = m[1][0] * rhs.m[0][1] + m[1][1] * rhs.m[1][1] + m[1][2] * rhs.m[2][1] + m[1][3] * rhs.m[3][1];
  99. r.m[1][2] = m[1][0] * rhs.m[0][2] + m[1][1] * rhs.m[1][2] + m[1][2] * rhs.m[2][2] + m[1][3] * rhs.m[3][2];
  100. r.m[1][3] = m[1][0] * rhs.m[0][3] + m[1][1] * rhs.m[1][3] + m[1][2] * rhs.m[2][3] + m[1][3] * rhs.m[3][3];
  101. r.m[2][0] = m[2][0] * rhs.m[0][0] + m[2][1] * rhs.m[1][0] + m[2][2] * rhs.m[2][0] + m[2][3] * rhs.m[3][0];
  102. r.m[2][1] = m[2][0] * rhs.m[0][1] + m[2][1] * rhs.m[1][1] + m[2][2] * rhs.m[2][1] + m[2][3] * rhs.m[3][1];
  103. r.m[2][2] = m[2][0] * rhs.m[0][2] + m[2][1] * rhs.m[1][2] + m[2][2] * rhs.m[2][2] + m[2][3] * rhs.m[3][2];
  104. r.m[2][3] = m[2][0] * rhs.m[0][3] + m[2][1] * rhs.m[1][3] + m[2][2] * rhs.m[2][3] + m[2][3] * rhs.m[3][3];
  105. r.m[3][0] = m[3][0] * rhs.m[0][0] + m[3][1] * rhs.m[1][0] + m[3][2] * rhs.m[2][0] + m[3][3] * rhs.m[3][0];
  106. r.m[3][1] = m[3][0] * rhs.m[0][1] + m[3][1] * rhs.m[1][1] + m[3][2] * rhs.m[2][1] + m[3][3] * rhs.m[3][1];
  107. r.m[3][2] = m[3][0] * rhs.m[0][2] + m[3][1] * rhs.m[1][2] + m[3][2] * rhs.m[2][2] + m[3][3] * rhs.m[3][2];
  108. r.m[3][3] = m[3][0] * rhs.m[0][3] + m[3][1] * rhs.m[1][3] + m[3][2] * rhs.m[2][3] + m[3][3] * rhs.m[3][3];
  109. return r;
  110. }
  111. Matrix4 operator+ (const Matrix4 &rhs) const
  112. {
  113. Matrix4 r;
  114. r.m[0][0] = m[0][0] + rhs.m[0][0];
  115. r.m[0][1] = m[0][1] + rhs.m[0][1];
  116. r.m[0][2] = m[0][2] + rhs.m[0][2];
  117. r.m[0][3] = m[0][3] + rhs.m[0][3];
  118. r.m[1][0] = m[1][0] + rhs.m[1][0];
  119. r.m[1][1] = m[1][1] + rhs.m[1][1];
  120. r.m[1][2] = m[1][2] + rhs.m[1][2];
  121. r.m[1][3] = m[1][3] + rhs.m[1][3];
  122. r.m[2][0] = m[2][0] + rhs.m[2][0];
  123. r.m[2][1] = m[2][1] + rhs.m[2][1];
  124. r.m[2][2] = m[2][2] + rhs.m[2][2];
  125. r.m[2][3] = m[2][3] + rhs.m[2][3];
  126. r.m[3][0] = m[3][0] + rhs.m[3][0];
  127. r.m[3][1] = m[3][1] + rhs.m[3][1];
  128. r.m[3][2] = m[3][2] + rhs.m[3][2];
  129. r.m[3][3] = m[3][3] + rhs.m[3][3];
  130. return r;
  131. }
  132. Matrix4 operator- (const Matrix4 &rhs) const
  133. {
  134. Matrix4 r;
  135. r.m[0][0] = m[0][0] - rhs.m[0][0];
  136. r.m[0][1] = m[0][1] - rhs.m[0][1];
  137. r.m[0][2] = m[0][2] - rhs.m[0][2];
  138. r.m[0][3] = m[0][3] - rhs.m[0][3];
  139. r.m[1][0] = m[1][0] - rhs.m[1][0];
  140. r.m[1][1] = m[1][1] - rhs.m[1][1];
  141. r.m[1][2] = m[1][2] - rhs.m[1][2];
  142. r.m[1][3] = m[1][3] - rhs.m[1][3];
  143. r.m[2][0] = m[2][0] - rhs.m[2][0];
  144. r.m[2][1] = m[2][1] - rhs.m[2][1];
  145. r.m[2][2] = m[2][2] - rhs.m[2][2];
  146. r.m[2][3] = m[2][3] - rhs.m[2][3];
  147. r.m[3][0] = m[3][0] - rhs.m[3][0];
  148. r.m[3][1] = m[3][1] - rhs.m[3][1];
  149. r.m[3][2] = m[3][2] - rhs.m[3][2];
  150. r.m[3][3] = m[3][3] - rhs.m[3][3];
  151. return r;
  152. }
  153. inline bool operator== (const Matrix4& rhs ) const
  154. {
  155. if(m[0][0] != rhs.m[0][0] || m[0][1] != rhs.m[0][1] || m[0][2] != rhs.m[0][2] || m[0][3] != rhs.m[0][3] ||
  156. m[1][0] != rhs.m[1][0] || m[1][1] != rhs.m[1][1] || m[1][2] != rhs.m[1][2] || m[1][3] != rhs.m[1][3] ||
  157. m[2][0] != rhs.m[2][0] || m[2][1] != rhs.m[2][1] || m[2][2] != rhs.m[2][2] || m[2][3] != rhs.m[2][3] ||
  158. m[3][0] != rhs.m[3][0] || m[3][1] != rhs.m[3][1] || m[3][2] != rhs.m[3][2] || m[3][3] != rhs.m[3][3] )
  159. {
  160. return false;
  161. }
  162. return true;
  163. }
  164. inline bool operator!= (const Matrix4& rhs) const
  165. {
  166. return !operator==(rhs);
  167. }
  168. Matrix4 operator*(float rhs) const
  169. {
  170. return Matrix4(rhs*m[0][0], rhs*m[0][1], rhs*m[0][2], rhs*m[0][3],
  171. rhs*m[1][0], rhs*m[1][1], rhs*m[1][2], rhs*m[1][3],
  172. rhs*m[2][0], rhs*m[2][1], rhs*m[2][2], rhs*m[2][3],
  173. rhs*m[3][0], rhs*m[3][1], rhs*m[3][2], rhs*m[3][3]);
  174. }
  175. /** Returns a transpose of the matrix (switched columns and rows). */
  176. Matrix4 transpose() const
  177. {
  178. return Matrix4(m[0][0], m[1][0], m[2][0], m[3][0],
  179. m[0][1], m[1][1], m[2][1], m[3][1],
  180. m[0][2], m[1][2], m[2][2], m[3][2],
  181. m[0][3], m[1][3], m[2][3], m[3][3]);
  182. }
  183. /** Assigns the vector to a column of the matrix. */
  184. void setColumn(UINT32 idx, const Vector4& column)
  185. {
  186. m[0][idx] = column.x;
  187. m[1][idx] = column.y;
  188. m[2][idx] = column.z;
  189. m[3][idx] = column.w;
  190. }
  191. /** Assigns the vector to a row of the matrix. */
  192. void setRow(UINT32 idx, const Vector4& column)
  193. {
  194. m[idx][0] = column.x;
  195. m[idx][1] = column.y;
  196. m[idx][2] = column.z;
  197. m[idx][3] = column.w;
  198. }
  199. /** Extracts the rotation/scaling part of the matrix as a 3x3 matrix. */
  200. void extract3x3Matrix(Matrix3& m3x3) const
  201. {
  202. m3x3.m[0][0] = m[0][0];
  203. m3x3.m[0][1] = m[0][1];
  204. m3x3.m[0][2] = m[0][2];
  205. m3x3.m[1][0] = m[1][0];
  206. m3x3.m[1][1] = m[1][1];
  207. m3x3.m[1][2] = m[1][2];
  208. m3x3.m[2][0] = m[2][0];
  209. m3x3.m[2][1] = m[2][1];
  210. m3x3.m[2][2] = m[2][2];
  211. }
  212. /** Calculates the adjoint of the matrix. */
  213. Matrix4 adjoint() const;
  214. /** Calculates the determinant of the matrix. */
  215. float determinant() const;
  216. /** Calculates the determinant of the 3x3 sub-matrix. */
  217. float determinant3x3() const;
  218. /** Calculates the inverse of the matrix. */
  219. Matrix4 inverse() const;
  220. /**
  221. * Creates a matrix from translation, rotation and scale.
  222. *
  223. * @note The transformation are applied in scale->rotation->translation order.
  224. */
  225. void setTRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale);
  226. /**
  227. * Creates a matrix from inverse translation, rotation and scale.
  228. *
  229. * @note This is cheaper than setTRS() and then performing inverse().
  230. */
  231. void setInverseTRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale);
  232. /**
  233. * Decompose a Matrix4 to translation, rotation and scale.
  234. *
  235. * @note
  236. * Matrix must consist only of translation, rotation and uniform scale transformations,
  237. * otherwise accurate results are not guaranteed. Applying non-uniform scale guarantees
  238. * results will not be accurate.
  239. */
  240. void decomposition(Vector3& position, Quaternion& rotation, Vector3& scale) const;
  241. /** Extracts the translation (position) part of the matrix. */
  242. Vector3 getTranslation() const { return Vector3(m[0][3], m[1][3], m[2][3]); }
  243. /**
  244. * Check whether or not the matrix is affine matrix.
  245. *
  246. * @note An affine matrix is a 4x4 matrix with row 3 equal to (0, 0, 0, 1), i.e. no projective coefficients.
  247. */
  248. bool isAffine() const
  249. {
  250. return m[3][0] == 0 && m[3][1] == 0 && m[3][2] == 0 && m[3][3] == 1;
  251. }
  252. /**
  253. * Returns the inverse of the affine matrix.
  254. *
  255. * @note Matrix must be affine.
  256. */
  257. Matrix4 inverseAffine() const;
  258. /**
  259. * Concatenate two affine matrices.
  260. *
  261. * @note Both matrices must be affine.
  262. */
  263. Matrix4 concatenateAffine(const Matrix4 &other) const
  264. {
  265. assert(isAffine() && other.isAffine());
  266. return Matrix4(
  267. m[0][0] * other.m[0][0] + m[0][1] * other.m[1][0] + m[0][2] * other.m[2][0],
  268. m[0][0] * other.m[0][1] + m[0][1] * other.m[1][1] + m[0][2] * other.m[2][1],
  269. m[0][0] * other.m[0][2] + m[0][1] * other.m[1][2] + m[0][2] * other.m[2][2],
  270. m[0][0] * other.m[0][3] + m[0][1] * other.m[1][3] + m[0][2] * other.m[2][3] + m[0][3],
  271. m[1][0] * other.m[0][0] + m[1][1] * other.m[1][0] + m[1][2] * other.m[2][0],
  272. m[1][0] * other.m[0][1] + m[1][1] * other.m[1][1] + m[1][2] * other.m[2][1],
  273. m[1][0] * other.m[0][2] + m[1][1] * other.m[1][2] + m[1][2] * other.m[2][2],
  274. m[1][0] * other.m[0][3] + m[1][1] * other.m[1][3] + m[1][2] * other.m[2][3] + m[1][3],
  275. m[2][0] * other.m[0][0] + m[2][1] * other.m[1][0] + m[2][2] * other.m[2][0],
  276. m[2][0] * other.m[0][1] + m[2][1] * other.m[1][1] + m[2][2] * other.m[2][1],
  277. m[2][0] * other.m[0][2] + m[2][1] * other.m[1][2] + m[2][2] * other.m[2][2],
  278. m[2][0] * other.m[0][3] + m[2][1] * other.m[1][3] + m[2][2] * other.m[2][3] + m[2][3],
  279. 0, 0, 0, 1);
  280. }
  281. /**
  282. * Transform a plane by this matrix.
  283. *
  284. * @note Matrix must be affine.
  285. */
  286. Plane multiplyAffine(const Plane& p) const
  287. {
  288. Vector4 localNormal(p.normal.x, p.normal.y, p.normal.z, 0.0f);
  289. Vector4 localPoint = localNormal * p.d;
  290. localPoint.w = 1.0f;
  291. Matrix4 itMat = inverse().transpose();
  292. Vector4 worldNormal = itMat.multiplyAffine(localNormal);
  293. Vector4 worldPoint = multiplyAffine(localPoint);
  294. float d = worldNormal.dot(worldPoint);
  295. return Plane(worldNormal.x, worldNormal.y, worldNormal.z, d);
  296. }
  297. /**
  298. * Transform a 3D point by this matrix.
  299. *
  300. * @note Matrix must be affine, if it is not use multiply() method.
  301. */
  302. Vector3 multiplyAffine(const Vector3& v) const
  303. {
  304. return Vector3(
  305. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3],
  306. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3],
  307. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]);
  308. }
  309. /**
  310. * Transform a 4D vector by this matrix.
  311. *
  312. * @note Matrix must be affine, if it is not use multiply() method.
  313. */
  314. Vector4 multiplyAffine(const Vector4& v) const
  315. {
  316. return Vector4(
  317. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w,
  318. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w,
  319. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w,
  320. v.w);
  321. }
  322. /** Transform a 3D direction by this matrix. */
  323. Vector3 multiplyDirection(const Vector3& v) const
  324. {
  325. return Vector3(
  326. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z,
  327. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z,
  328. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z);
  329. }
  330. /**
  331. * Transform a 3D point by this matrix.
  332. *
  333. * @note
  334. * w component of the vector is assumed to be 1. After transformation all components
  335. * are projected back so that w remains 1.
  336. * @note
  337. * If your matrix doesn't contain projection components use multiplyAffine() method as it is faster.
  338. */
  339. Vector3 multiply(const Vector3& v) const
  340. {
  341. Vector3 r;
  342. float fInvW = 1.0f / (m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3]);
  343. r.x = (m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3]) * fInvW;
  344. r.y = (m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3]) * fInvW;
  345. r.z = (m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]) * fInvW;
  346. return r;
  347. }
  348. /**
  349. * Transform a 4D vector by this matrix.
  350. *
  351. * @note If your matrix doesn't contain projection components use multiplyAffine() method as it is faster.
  352. */
  353. Vector4 multiply(const Vector4& v) const
  354. {
  355. return Vector4(
  356. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w,
  357. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w,
  358. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w,
  359. m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w
  360. );
  361. }
  362. /** Creates a view matrix and applies optional reflection. */
  363. void makeView(const Vector3& position, const Quaternion& orientation, const Matrix4* reflectMatrix = nullptr);
  364. /** Creates an ortographic projection matrix. */
  365. void makeProjectionOrtho(float left, float right, float top, float bottom, float near, float far);
  366. /** Creates a 4x4 transformation matrix that performs translation. */
  367. static Matrix4 translation(const Vector3& translation);
  368. /** Creates a 4x4 transformation matrix that performs scaling. */
  369. static Matrix4 scaling(const Vector3& scale);
  370. /** Creates a 4x4 transformation matrix that performs uniform scaling. */
  371. static Matrix4 scaling(float scale);
  372. /** Creates a 4x4 transformation matrix that performs rotation. */
  373. static Matrix4 rotation(const Quaternion& rotation);
  374. /**
  375. * Creates a matrix from translation, rotation and scale.
  376. *
  377. * @note The transformation are applied in scale->rotation->translation order.
  378. */
  379. static Matrix4 TRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale);
  380. /**
  381. * Creates a matrix from inverse translation, rotation and scale.
  382. *
  383. * @note This is cheaper than setTRS() and then performing inverse().
  384. */
  385. static Matrix4 inverseTRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale);
  386. static const Matrix4 ZERO;
  387. static const Matrix4 IDENTITY;
  388. };
  389. /** @} */
  390. /** @cond SPECIALIZATIONS */
  391. BS_ALLOW_MEMCPY_SERIALIZATION(Matrix4);
  392. /** @endcond */
  393. }