BsMatrix4.h 17 KB

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