Matrix4.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  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. #pragma once
  24. #include "Quaternion.h"
  25. #include "Vector4.h"
  26. /// 4x4 matrix for arbitrary linear transforms including projection
  27. class Matrix4
  28. {
  29. public:
  30. /// Construct an undefined matrix
  31. Matrix4()
  32. {
  33. }
  34. /// Copy-construct from another matrix
  35. Matrix4(const Matrix4& matrix) :
  36. m00_(matrix.m00_),
  37. m01_(matrix.m01_),
  38. m02_(matrix.m02_),
  39. m03_(matrix.m03_),
  40. m10_(matrix.m10_),
  41. m11_(matrix.m11_),
  42. m12_(matrix.m12_),
  43. m13_(matrix.m13_),
  44. m20_(matrix.m20_),
  45. m21_(matrix.m21_),
  46. m22_(matrix.m22_),
  47. m23_(matrix.m23_),
  48. m30_(matrix.m30_),
  49. m31_(matrix.m31_),
  50. m32_(matrix.m32_),
  51. m33_(matrix.m33_)
  52. {
  53. }
  54. /// Copy-cnstruct from a 3x3 matrix and set the extra elements to identity
  55. Matrix4(const Matrix3& matrix) :
  56. m00_(matrix.m00_),
  57. m01_(matrix.m01_),
  58. m02_(matrix.m02_),
  59. m03_(0.0f),
  60. m10_(matrix.m10_),
  61. m11_(matrix.m11_),
  62. m12_(matrix.m12_),
  63. m13_(0.0f),
  64. m20_(matrix.m20_),
  65. m21_(matrix.m21_),
  66. m22_(matrix.m22_),
  67. m23_(0.0f),
  68. m30_(0.0f),
  69. m31_(0.0f),
  70. m32_(0.0f),
  71. m33_(1.0f)
  72. {
  73. }
  74. // Construct from values
  75. Matrix4(float v00, float v01, float v02, float v03,
  76. float v10, float v11, float v12, float v13,
  77. float v20, float v21, float v22, float v23,
  78. float v30, float v31, float v32, float v33) :
  79. m00_(v00),
  80. m01_(v01),
  81. m02_(v02),
  82. m03_(v03),
  83. m10_(v10),
  84. m11_(v11),
  85. m12_(v12),
  86. m13_(v13),
  87. m20_(v20),
  88. m21_(v21),
  89. m22_(v22),
  90. m23_(v23),
  91. m30_(v30),
  92. m31_(v31),
  93. m32_(v32),
  94. m33_(v33)
  95. {
  96. }
  97. /// Construct from a float array
  98. Matrix4(float* data) :
  99. m00_(data[0]),
  100. m01_(data[1]),
  101. m02_(data[2]),
  102. m03_(data[3]),
  103. m10_(data[4]),
  104. m11_(data[5]),
  105. m12_(data[6]),
  106. m13_(data[7]),
  107. m20_(data[8]),
  108. m21_(data[9]),
  109. m22_(data[10]),
  110. m23_(data[11]),
  111. m30_(data[12]),
  112. m31_(data[13]),
  113. m32_(data[14]),
  114. m33_(data[15])
  115. {
  116. }
  117. /// Assign from another matrix
  118. Matrix4& operator = (const Matrix4& rhs)
  119. {
  120. m00_ = rhs.m00_;
  121. m01_ = rhs.m01_;
  122. m02_ = rhs.m02_;
  123. m03_ = rhs.m03_;
  124. m10_ = rhs.m10_;
  125. m11_ = rhs.m11_;
  126. m12_ = rhs.m12_;
  127. m13_ = rhs.m13_;
  128. m20_ = rhs.m20_;
  129. m21_ = rhs.m21_;
  130. m22_ = rhs.m22_;
  131. m23_ = rhs.m23_;
  132. m30_ = rhs.m30_;
  133. m31_ = rhs.m31_;
  134. m32_ = rhs.m32_;
  135. m33_ = rhs.m33_;
  136. return *this;
  137. }
  138. /// Assign from a 3x3 matrix. Set the extra elements to identity
  139. Matrix4& operator = (const Matrix3& rhs)
  140. {
  141. m00_ = rhs.m00_;
  142. m01_ = rhs.m01_;
  143. m02_ = rhs.m02_;
  144. m03_ = 0.0f;
  145. m10_ = rhs.m10_;
  146. m11_ = rhs.m11_;
  147. m12_ = rhs.m12_;
  148. m13_ = 0.0f;
  149. m20_ = rhs.m20_;
  150. m21_ = rhs.m21_;
  151. m22_ = rhs.m22_;
  152. m23_ = 0.0f;
  153. m30_ = 0.0f;
  154. m31_ = 0.0f;
  155. m32_ = 0.0f;
  156. m33_ = 1.0f;
  157. return *this;
  158. }
  159. /// Multiply a Vector3 which is assumed to represent position
  160. Vector3 operator * (const Vector3& rhs) const
  161. {
  162. float invW = 1.0f / (m30_ * rhs.x_ + m31_ * rhs.y_ + m32_ * rhs.z_ + m33_);
  163. return Vector3(
  164. (m00_ * rhs.x_ + m01_ * rhs.y_ + m02_ * rhs.z_ + m03_) * invW,
  165. (m10_ * rhs.x_ + m11_ * rhs.y_ + m12_ * rhs.z_ + m13_) * invW,
  166. (m20_ * rhs.x_ + m21_ * rhs.y_ + m22_ * rhs.z_ + m23_) * invW
  167. );
  168. }
  169. /// Multiply a Vector4
  170. Vector4 operator * (const Vector4& rhs) const
  171. {
  172. return Vector4(
  173. m00_ * rhs.x_ + m01_ * rhs.y_ + m02_ * rhs.z_ + m03_ * rhs.w_,
  174. m10_ * rhs.x_ + m11_ * rhs.y_ + m12_ * rhs.z_ + m13_ * rhs.w_,
  175. m20_ * rhs.x_ + m21_ * rhs.y_ + m22_ * rhs.z_ + m23_ * rhs.w_,
  176. m30_ * rhs.x_ + m31_ * rhs.y_ + m32_ * rhs.z_ + m33_ * rhs.w_
  177. );
  178. }
  179. /// Add a matrix
  180. Matrix4 operator + (const Matrix4& rhs) const
  181. {
  182. return Matrix4(
  183. m00_ + rhs.m00_,
  184. m01_ + rhs.m01_,
  185. m02_ + rhs.m02_,
  186. m03_ + rhs.m03_,
  187. m10_ + rhs.m10_,
  188. m11_ + rhs.m11_,
  189. m12_ + rhs.m12_,
  190. m13_ + rhs.m13_,
  191. m20_ + rhs.m20_,
  192. m21_ + rhs.m21_,
  193. m22_ + rhs.m22_,
  194. m23_ + rhs.m23_,
  195. m30_ + rhs.m30_,
  196. m31_ + rhs.m31_,
  197. m32_ + rhs.m32_,
  198. m33_ + rhs.m33_
  199. );
  200. }
  201. /// Subtract a matrix
  202. Matrix4 operator - (const Matrix4& rhs) const
  203. {
  204. return Matrix4(
  205. m00_ - rhs.m00_,
  206. m01_ - rhs.m01_,
  207. m02_ - rhs.m02_,
  208. m03_ - rhs.m03_,
  209. m10_ - rhs.m10_,
  210. m11_ - rhs.m11_,
  211. m12_ - rhs.m12_,
  212. m13_ - rhs.m13_,
  213. m20_ - rhs.m20_,
  214. m21_ - rhs.m21_,
  215. m22_ - rhs.m22_,
  216. m23_ - rhs.m23_,
  217. m30_ - rhs.m30_,
  218. m31_ - rhs.m31_,
  219. m32_ - rhs.m32_,
  220. m33_ - rhs.m33_
  221. );
  222. }
  223. /// Multiply with a scalar
  224. Matrix4 operator * (float rhs) const
  225. {
  226. return Matrix4(
  227. m00_ * rhs,
  228. m01_ * rhs,
  229. m02_ * rhs,
  230. m03_ * rhs,
  231. m10_ * rhs,
  232. m11_ * rhs,
  233. m12_ * rhs,
  234. m13_ * rhs,
  235. m20_ * rhs,
  236. m21_ * rhs,
  237. m22_ * rhs,
  238. m23_ * rhs,
  239. m30_ * rhs,
  240. m31_ * rhs,
  241. m32_ * rhs,
  242. m33_ * rhs
  243. );
  244. }
  245. /// Multiply a matrix
  246. Matrix4 operator * (const Matrix4& rhs) const
  247. {
  248. return Matrix4(
  249. m00_ * rhs.m00_ + m01_ * rhs.m10_ + m02_ * rhs.m20_ + m03_ * rhs.m30_,
  250. m00_ * rhs.m01_ + m01_ * rhs.m11_ + m02_ * rhs.m21_ + m03_ * rhs.m31_,
  251. m00_ * rhs.m02_ + m01_ * rhs.m12_ + m02_ * rhs.m22_ + m03_ * rhs.m32_,
  252. m00_ * rhs.m03_ + m01_ * rhs.m13_ + m02_ * rhs.m23_ + m03_ * rhs.m33_,
  253. m10_ * rhs.m00_ + m11_ * rhs.m10_ + m12_ * rhs.m20_ + m13_ * rhs.m30_,
  254. m10_ * rhs.m01_ + m11_ * rhs.m11_ + m12_ * rhs.m21_ + m13_ * rhs.m31_,
  255. m10_ * rhs.m02_ + m11_ * rhs.m12_ + m12_ * rhs.m22_ + m13_ * rhs.m32_,
  256. m10_ * rhs.m03_ + m11_ * rhs.m13_ + m12_ * rhs.m23_ + m13_ * rhs.m33_,
  257. m20_ * rhs.m00_ + m21_ * rhs.m10_ + m22_ * rhs.m20_ + m23_ * rhs.m30_,
  258. m20_ * rhs.m01_ + m21_ * rhs.m11_ + m22_ * rhs.m21_ + m23_ * rhs.m31_,
  259. m20_ * rhs.m02_ + m21_ * rhs.m12_ + m22_ * rhs.m22_ + m23_ * rhs.m32_,
  260. m20_ * rhs.m03_ + m21_ * rhs.m13_ + m22_ * rhs.m23_ + m23_ * rhs.m33_,
  261. m30_ * rhs.m00_ + m31_ * rhs.m10_ + m32_ * rhs.m20_ + m33_ * rhs.m30_,
  262. m30_ * rhs.m01_ + m31_ * rhs.m11_ + m32_ * rhs.m21_ + m33_ * rhs.m31_,
  263. m30_ * rhs.m02_ + m31_ * rhs.m12_ + m32_ * rhs.m22_ + m33_ * rhs.m32_,
  264. m30_ * rhs.m03_ + m31_ * rhs.m13_ + m32_ * rhs.m23_ + m33_ * rhs.m33_
  265. );
  266. }
  267. /// Set translation elements
  268. void SetTranslation(const Vector3& translation)
  269. {
  270. m03_ = translation.x_;
  271. m13_ = translation.y_;
  272. m23_ = translation.z_;
  273. }
  274. /// Set rotation elements from a 3x3 matrix
  275. void SetRotation(const Matrix3& rotation)
  276. {
  277. m00_ = rotation.m00_;
  278. m01_ = rotation.m01_;
  279. m02_ = rotation.m02_;
  280. m10_ = rotation.m10_;
  281. m11_ = rotation.m11_;
  282. m12_ = rotation.m12_;
  283. m20_ = rotation.m20_;
  284. m21_ = rotation.m21_;
  285. m22_ = rotation.m22_;
  286. }
  287. // Set scaling elements
  288. void SetScale(const Vector3& scale)
  289. {
  290. m00_ = scale.x_;
  291. m11_ = scale.y_;
  292. m22_ = scale.z_;
  293. }
  294. // Set uniform scaling elements
  295. void SetScale(float scale)
  296. {
  297. m00_ = scale;
  298. m11_ = scale;
  299. m22_ = scale;
  300. }
  301. /// Return the combined rotation and scaling matrix
  302. Matrix3 ToMatrix3() const
  303. {
  304. return Matrix3(
  305. m00_,
  306. m01_,
  307. m02_,
  308. m10_,
  309. m11_,
  310. m12_,
  311. m20_,
  312. m21_,
  313. m22_
  314. );
  315. }
  316. /// Return the rotation matrix with scaling removed
  317. Matrix3 RotationMatrix() const
  318. {
  319. Vector3 invScale(
  320. 1.0f / sqrtf(m00_ * m00_ + m10_ * m10_ + m20_ * m20_),
  321. 1.0f / sqrtf(m01_ * m01_ + m11_ * m11_ + m21_ * m21_),
  322. 1.0f / sqrtf(m02_ * m02_ + m12_ * m12_ + m22_ * m22_)
  323. );
  324. return ToMatrix3().Scaled(invScale);
  325. }
  326. /// Return the translation part
  327. Vector3 Translation() const
  328. {
  329. return Vector3(
  330. m03_,
  331. m13_,
  332. m23_
  333. );
  334. }
  335. /// Return the rotation part
  336. Quaternion Rotation() const
  337. {
  338. return Quaternion(RotationMatrix());
  339. }
  340. /// Return the scaling part
  341. Vector3 Scale() const
  342. {
  343. return Vector3(
  344. sqrtf(m00_ * m00_ + m10_ * m10_ + m20_ * m20_),
  345. sqrtf(m01_ * m01_ + m11_ * m11_ + m21_ * m21_),
  346. sqrtf(m02_ * m02_ + m12_ * m12_ + m22_ * m22_)
  347. );
  348. }
  349. /// Return transpose
  350. Matrix4 Transpose() const
  351. {
  352. return Matrix4(
  353. m00_,
  354. m10_,
  355. m20_,
  356. m30_,
  357. m01_,
  358. m11_,
  359. m21_,
  360. m31_,
  361. m02_,
  362. m12_,
  363. m22_,
  364. m32_,
  365. m03_,
  366. m13_,
  367. m23_,
  368. m33_
  369. );
  370. }
  371. /// Return decomposition to translation, rotation and scale
  372. void Decompose(Vector3& translation, Quaternion& rotation, Vector3& scale) const;
  373. /// Return inverse
  374. Matrix4 Inverse() const;
  375. /// Return float data
  376. const float* GetData() const { return &m00_; }
  377. float m00_;
  378. float m01_;
  379. float m02_;
  380. float m03_;
  381. float m10_;
  382. float m11_;
  383. float m12_;
  384. float m13_;
  385. float m20_;
  386. float m21_;
  387. float m22_;
  388. float m23_;
  389. float m30_;
  390. float m31_;
  391. float m32_;
  392. float m33_;
  393. /// Zero matrix
  394. static const Matrix4 ZERO;
  395. /// Identity matrix
  396. static const Matrix4 IDENTITY;
  397. };
  398. /// Multiply a 4x4 matrix with a scalar
  399. inline Matrix4 operator * (float lhs, const Matrix4& rhs) { return rhs * lhs; }