Matrix.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /*
  2. * Matrix.cpp
  3. */
  4. #include "Base.h"
  5. #include "Matrix.h"
  6. #include "Quaternion.h"
  7. #define MATRIX_SIZE ( sizeof(float) * 16 )
  8. namespace gameplay
  9. {
  10. static const float MATRIX_IDENTITY[16] =
  11. {
  12. 1.0f, 0.0f, 0.0f, 0.0f,
  13. 0.0f, 1.0f, 0.0f, 0.0f,
  14. 0.0f, 0.0f, 1.0f, 0.0f,
  15. 0.0f, 0.0f, 0.0f, 1.0f
  16. };
  17. Matrix::Matrix()
  18. {
  19. *this = Matrix::identity();
  20. }
  21. Matrix::Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44)
  22. {
  23. set(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
  24. }
  25. Matrix::Matrix(const float* m)
  26. {
  27. set(m);
  28. }
  29. Matrix::Matrix(const Matrix& copy)
  30. {
  31. memcpy(m, copy.m, MATRIX_SIZE);
  32. }
  33. Matrix::~Matrix()
  34. {
  35. }
  36. const Matrix& Matrix::identity()
  37. {
  38. static Matrix* m = new Matrix( 1, 0, 0, 0,
  39. 0, 1, 0, 0,
  40. 0, 0, 1, 0,
  41. 0, 0, 0, 1 );
  42. return *m;
  43. }
  44. const Matrix& Matrix::zero()
  45. {
  46. static Matrix* m = new Matrix( 0, 0, 0, 0,
  47. 0, 0, 0, 0,
  48. 0, 0, 0, 0,
  49. 0, 0, 0, 0 );
  50. return *m;
  51. }
  52. void Matrix::createLookAt(const Vector3& eyePosition, const Vector3& targetPosition, const Vector3& up, Matrix* dst)
  53. {
  54. createLookAt(eyePosition.x, eyePosition.y, eyePosition.z, targetPosition.x, targetPosition.y, targetPosition.z, up.x, up.y, up.z, dst);
  55. }
  56. void Matrix::createLookAt(float eyePositionX, float eyePositionY, float eyePositionZ,
  57. float targetPositionX, float targetPositionY, float targetPositionZ,
  58. float upX, float upY, float upZ, Matrix* dst)
  59. {
  60. assert(dst);
  61. Vector3 eye(eyePositionX, eyePositionY, eyePositionZ);
  62. Vector3 target(targetPositionX, targetPositionY, targetPositionZ);
  63. Vector3 up(upX, upY, upZ);
  64. up.normalize();
  65. Vector3 zaxis;
  66. Vector3::subtract(eye, target, &zaxis);
  67. zaxis.normalize();
  68. Vector3 xaxis;
  69. Vector3::cross(up, zaxis, &xaxis);
  70. xaxis.normalize();
  71. Vector3 yaxis;
  72. Vector3::cross(zaxis, xaxis, &yaxis);
  73. yaxis.normalize();
  74. dst->m[0] = xaxis.x;
  75. dst->m[1] = yaxis.x;
  76. dst->m[2] = zaxis.x;
  77. dst->m[3] = 0.0f;
  78. dst->m[4] = xaxis.y;
  79. dst->m[5] = yaxis.y;
  80. dst->m[6] = zaxis.y;
  81. dst->m[7] = 0.0f;
  82. dst->m[8] = xaxis.z;
  83. dst->m[9] = yaxis.z;
  84. dst->m[10] = zaxis.z;
  85. dst->m[11] = 0.0f;
  86. dst->m[12] = -Vector3::dot(xaxis, eye);
  87. dst->m[13] = -Vector3::dot(yaxis, eye);
  88. dst->m[14] = -Vector3::dot(zaxis, eye);
  89. dst->m[15] = 1.0f;
  90. }
  91. void Matrix::createPerspective(float fieldOfView, float aspectRatio,
  92. float zNearPlane, float zFarPlane, Matrix* dst)
  93. {
  94. assert(dst);
  95. float f_n = 1.0f / (zFarPlane - zNearPlane);
  96. float factor = 1.0f / tanf(MATH_DEG_TO_RAD(fieldOfView) * 0.5f);
  97. memset(dst, 0, MATRIX_SIZE);
  98. dst->m[0] = (1.0f / aspectRatio) * factor;
  99. dst->m[5] = factor;
  100. dst->m[10] = (-(zFarPlane + zNearPlane)) * f_n;
  101. dst->m[11] = -1.0f;
  102. dst->m[14] = -2.0f * zFarPlane * zNearPlane * f_n;
  103. }
  104. void Matrix::createOrthographic(float width, float height, float zNearPlane, float zFarPlane, Matrix* dst)
  105. {
  106. float halfWidth = width / 2.0f;
  107. float halfHeight = height / 2.0f;
  108. createOrthographicOffCenter(-halfWidth, halfWidth, -halfHeight, halfHeight, zNearPlane, zFarPlane, dst);
  109. }
  110. void Matrix::createOrthographicOffCenter(float left, float right, float bottom, float top,
  111. float zNearPlane, float zFarPlane, Matrix* dst)
  112. {
  113. assert(dst);
  114. float r_l = 1.0f / (right - left);
  115. float t_b = 1.0f / (top - bottom);
  116. float f_n = 1.0f / (zFarPlane - zNearPlane);
  117. memset(dst, 0, MATRIX_SIZE);
  118. dst->m[0] = 2.0f * r_l;
  119. dst->m[5] = 2.0f * t_b;
  120. dst->m[10] = -2.0f * f_n;
  121. dst->m[12] = (-(right + left)) * r_l;
  122. dst->m[13] = (-(top + bottom)) * t_b;
  123. dst->m[14] = (-(zFarPlane + zNearPlane)) * f_n;
  124. dst->m[15] = 1.0f;
  125. }
  126. void Matrix::createScale(const Vector3& scale, Matrix* dst)
  127. {
  128. assert(dst);
  129. memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE);
  130. dst->m[0] = scale.x;
  131. dst->m[5] = scale.y;
  132. dst->m[10] = scale.z;
  133. }
  134. void Matrix::createScale(float xScale, float yScale, float zScale, Matrix* dst)
  135. {
  136. assert(dst);
  137. memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE);
  138. dst->m[0] = xScale;
  139. dst->m[5] = yScale;
  140. dst->m[10] = zScale;
  141. }
  142. void Matrix::createRotation(const Quaternion& q, Matrix* dst)
  143. {
  144. assert(dst);
  145. float x2 = q.x + q.x;
  146. float y2 = q.y + q.y;
  147. float z2 = q.z + q.z;
  148. float xx2 = q.x * x2;
  149. float yy2 = q.y * y2;
  150. float zz2 = q.z * z2;
  151. float xy2 = q.x * y2;
  152. float xz2 = q.x * z2;
  153. float yz2 = q.y * z2;
  154. float wx2 = q.w * x2;
  155. float wy2 = q.w * y2;
  156. float wz2 = q.w * z2;
  157. dst->m[0] = 1.0f - yy2 - zz2;
  158. dst->m[1] = xy2 + wz2;
  159. dst->m[2] = xz2 - wy2;
  160. dst->m[3] = 0.0f;
  161. dst->m[4] = xy2 - wz2;
  162. dst->m[5] = 1.0f - xx2 - zz2;
  163. dst->m[6] = yz2 + wx2;
  164. dst->m[7] = 0.0f;
  165. dst->m[8] = xz2 + wy2;
  166. dst->m[9] = yz2 - wx2;
  167. dst->m[10] = 1.0f - xx2 - yy2;
  168. dst->m[11] = 0.0f;
  169. dst->m[12] = 0.0f;
  170. dst->m[13] = 0.0f;
  171. dst->m[14] = 0.0f;
  172. dst->m[15] = 1.0f;
  173. }
  174. void Matrix::createRotation(const Vector3& axis, float angle, Matrix* dst)
  175. {
  176. assert(dst);
  177. float x = axis.x;
  178. float y = axis.y;
  179. float z = axis.z;
  180. // Make sure the input axis is normalized.
  181. float n = x*x + y*y + z*z;
  182. if (n != 1.0f)
  183. {
  184. // Not normalized.
  185. n = sqrtf(n);
  186. // Prevent divide too close to zero.
  187. if (n > 0.000001f)
  188. {
  189. n = 1.0f / n;
  190. x *= n;
  191. y *= n;
  192. z *= n;
  193. }
  194. }
  195. float c = cos(angle);
  196. float s = sin(angle);
  197. float t = 1.0f - c;
  198. float tx = t * x;
  199. float ty = t * y;
  200. float tz = t * z;
  201. float txy = tx * y;
  202. float txz = tx * z;
  203. float tyz = ty * z;
  204. float sx = s * x;
  205. float sy = s * y;
  206. float sz = s * z;
  207. dst->m[0] = c + tx*x;
  208. dst->m[1] = txy + sz;
  209. dst->m[2] = txz - sy;
  210. dst->m[3] = 0.0f;
  211. dst->m[4] = txy - sz;
  212. dst->m[5] = c + ty*y;
  213. dst->m[6] = tyz + sx;
  214. dst->m[7] = 0.0f;
  215. dst->m[8] = txz + sy;
  216. dst->m[9] = tyz - sx;
  217. dst->m[10] = c + tz*z;
  218. dst->m[11] = 0.0f;
  219. dst->m[12] = 0.0f;
  220. dst->m[13] = 0.0f;
  221. dst->m[14] = 0.0f;
  222. dst->m[15] = 1.0f;
  223. }
  224. void Matrix::createRotationX(float angle, Matrix* dst)
  225. {
  226. assert(dst);
  227. memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE);
  228. float c = cos(angle);
  229. float s = sin(angle);
  230. dst->m[5] = c;
  231. dst->m[6] = s;
  232. dst->m[9] = -s;
  233. dst->m[10] = c;
  234. }
  235. void Matrix::createRotationY(float angle, Matrix* dst)
  236. {
  237. assert(dst);
  238. memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE);
  239. float c = cos(angle);
  240. float s = sin(angle);
  241. dst->m[0] = c;
  242. dst->m[2] = -s;
  243. dst->m[8] = s;
  244. dst->m[10] = c;
  245. }
  246. void Matrix::createRotationZ(float angle, Matrix* dst)
  247. {
  248. assert(dst);
  249. memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE);
  250. float c = cos(angle);
  251. float s = sin(angle);
  252. dst->m[0] = c;
  253. dst->m[1] = s;
  254. dst->m[4] = -s;
  255. dst->m[5] = c;
  256. }
  257. void Matrix::createTranslation(const Vector3& translation, Matrix* dst)
  258. {
  259. assert(dst);
  260. memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE);
  261. dst->m[12] = translation.x;
  262. dst->m[13] = translation.y;
  263. dst->m[14] = translation.z;
  264. }
  265. void Matrix::createTranslation(float xTranslation, float yTranslation, float zTranslation, Matrix* dst)
  266. {
  267. assert(dst);
  268. memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE);
  269. dst->m[12] = xTranslation;
  270. dst->m[13] = yTranslation;
  271. dst->m[14] = zTranslation;
  272. }
  273. void Matrix::add(float scalar)
  274. {
  275. add(scalar, this);
  276. }
  277. void Matrix::add(float scalar, Matrix* dst)
  278. {
  279. assert(dst);
  280. dst->m[0] = m[0] + scalar;
  281. dst->m[1] = m[1] + scalar;
  282. dst->m[2] = m[2] + scalar;
  283. dst->m[3] = m[3] + scalar;
  284. dst->m[4] = m[4] + scalar;
  285. dst->m[5] = m[5] + scalar;
  286. dst->m[6] = m[6] + scalar;
  287. dst->m[7] = m[7] + scalar;
  288. dst->m[8] = m[8] + scalar;
  289. dst->m[9] = m[9] + scalar;
  290. dst->m[10] = m[10] + scalar;
  291. dst->m[11] = m[11] + scalar;
  292. dst->m[12] = m[12] + scalar;
  293. dst->m[13] = m[13] + scalar;
  294. dst->m[14] = m[14] + scalar;
  295. dst->m[15] = m[15] + scalar;
  296. }
  297. void Matrix::add(const Matrix& m)
  298. {
  299. add(*this, m, this);
  300. }
  301. void Matrix::add(const Matrix& m1, const Matrix& m2, Matrix* dst)
  302. {
  303. assert(dst);
  304. dst->m[0] = m1.m[0] + m2.m[0];
  305. dst->m[1] = m1.m[1] + m2.m[1];
  306. dst->m[2] = m1.m[2] + m2.m[2];
  307. dst->m[3] = m1.m[3] + m2.m[3];
  308. dst->m[4] = m1.m[4] + m2.m[4];
  309. dst->m[5] = m1.m[5] + m2.m[5];
  310. dst->m[6] = m1.m[6] + m2.m[6];
  311. dst->m[7] = m1.m[7] + m2.m[7];
  312. dst->m[8] = m1.m[8] + m2.m[8];
  313. dst->m[9] = m1.m[9] + m2.m[9];
  314. dst->m[10] = m1.m[10] + m2.m[10];
  315. dst->m[11] = m1.m[11] + m2.m[11];
  316. dst->m[12] = m1.m[12] + m2.m[12];
  317. dst->m[13] = m1.m[13] + m2.m[13];
  318. dst->m[14] = m1.m[14] + m2.m[14];
  319. dst->m[15] = m1.m[15] + m2.m[15];
  320. }
  321. bool Matrix::decompose(Vector3* scale, Quaternion* rotation, Vector3* translation) const
  322. {
  323. if (translation)
  324. {
  325. // Extract the translation.
  326. translation->x = m[12];
  327. translation->y = m[13];
  328. translation->z = m[14];
  329. }
  330. // Nothing left to do.
  331. if (scale == NULL && rotation == NULL)
  332. return true;
  333. // Extract the scale.
  334. // This is simply the length of each axis (row/column) in the matrix.
  335. Vector3 xaxis(m[0], m[1], m[2]);
  336. float scaleX = xaxis.length();
  337. Vector3 yaxis(m[4], m[5], m[6]);
  338. float scaleY = yaxis.length();
  339. Vector3 zaxis(m[8], m[9], m[10]);
  340. float scaleZ = zaxis.length();
  341. // Determine if we have a negative scale (true if determinant is less than zero).
  342. // In this case, we simply negate a single axis of the scale.
  343. float det = determinant();
  344. if (det < 0)
  345. scaleZ = -scaleZ;
  346. if (scale)
  347. {
  348. scale->x = scaleX;
  349. scale->y = scaleY;
  350. scale->z = scaleZ;
  351. }
  352. // Nothing left to do.
  353. if (rotation == NULL)
  354. return true;
  355. // Scale too close to zero, can't decompose rotation.
  356. if (scaleX < MATH_TOLERANCE || scaleY < MATH_TOLERANCE || fabs(scaleZ) < MATH_TOLERANCE)
  357. return false;
  358. float rn;
  359. // Factor the scale out of the matrix axes.
  360. rn = 1.0f / scaleX;
  361. xaxis.x *= rn;
  362. xaxis.y *= rn;
  363. xaxis.z *= rn;
  364. rn = 1.0f / scaleY;
  365. yaxis.x *= rn;
  366. yaxis.y *= rn;
  367. yaxis.z *= rn;
  368. rn = 1.0f / scaleZ;
  369. zaxis.x *= rn;
  370. zaxis.y *= rn;
  371. zaxis.z *= rn;
  372. // Now calculate the rotation from the resulting matrix (axes).
  373. float trace = xaxis.x + yaxis.y + zaxis.z + 1.0f;
  374. if (trace > MATH_TOLERANCE)
  375. {
  376. float s = 0.5f / sqrt(trace);
  377. rotation->w = 0.25f / s;
  378. rotation->x = ( yaxis.z - zaxis.y ) * s;
  379. rotation->y = ( zaxis.x - xaxis.z ) * s;
  380. rotation->z = ( xaxis.y - yaxis.x ) * s;
  381. }
  382. else
  383. {
  384. if (xaxis.x > yaxis.y && xaxis.x > zaxis.z)
  385. {
  386. float s = 2.0f * sqrt(1.0f + xaxis.x - yaxis.y - zaxis.z);
  387. rotation->w = (yaxis.z - zaxis.y ) / s;
  388. rotation->x = 0.25f * s;
  389. rotation->y = (yaxis.x + xaxis.y ) / s;
  390. rotation->z = (zaxis.x + xaxis.z ) / s;
  391. }
  392. else if (yaxis.y > zaxis.z)
  393. {
  394. float s = 2.0f * sqrt(1.0f + yaxis.y - xaxis.x - zaxis.z);
  395. rotation->w = (zaxis.x - xaxis.z ) / s;
  396. rotation->x = (yaxis.x + xaxis.y ) / s;
  397. rotation->y = 0.25f * s;
  398. rotation->z = (zaxis.y + yaxis.z ) / s;
  399. }
  400. else
  401. {
  402. float s = 2.0f * sqrt(1.0f + zaxis.z - xaxis.x - yaxis.y );
  403. rotation->w = (xaxis.y - yaxis.x ) / s;
  404. rotation->x = (zaxis.x + xaxis.z ) / s;
  405. rotation->y = (zaxis.y + yaxis.z ) / s;
  406. rotation->z = 0.25f * s;
  407. }
  408. }
  409. return true;
  410. }
  411. float Matrix::determinant() const
  412. {
  413. float a0 = m[0] * m[5] - m[1] * m[4];
  414. float a1 = m[0] * m[6] - m[2] * m[4];
  415. float a2 = m[0] * m[7] - m[3] * m[4];
  416. float a3 = m[1] * m[6] - m[2] * m[5];
  417. float a4 = m[1] * m[7] - m[3] * m[5];
  418. float a5 = m[2] * m[7] - m[3] * m[6];
  419. float b0 = m[8] * m[13] - m[9] * m[12];
  420. float b1 = m[8] * m[14] - m[10] * m[12];
  421. float b2 = m[8] * m[15] - m[11] * m[12];
  422. float b3 = m[9] * m[14] - m[10] * m[13];
  423. float b4 = m[9] * m[15] - m[11] * m[13];
  424. float b5 = m[10] * m[15] - m[11] * m[14];
  425. // Calculate the determinant.
  426. return (a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0);
  427. }
  428. void Matrix::getScale(Vector3* scale) const
  429. {
  430. decompose(scale, NULL, NULL);
  431. }
  432. bool Matrix::getRotation(Quaternion* rotation) const
  433. {
  434. return decompose(NULL, rotation, NULL);
  435. }
  436. void Matrix::getTranslation(Vector3* translation) const
  437. {
  438. decompose(NULL, NULL, translation);
  439. }
  440. void Matrix::getUpVector(Vector3* dst) const
  441. {
  442. assert(dst);
  443. dst->x = m[4];
  444. dst->y = m[5];
  445. dst->z = m[6];
  446. }
  447. void Matrix::getDownVector(Vector3* dst) const
  448. {
  449. assert(dst);
  450. dst->x = -m[4];
  451. dst->y = -m[5];
  452. dst->z = -m[6];
  453. }
  454. void Matrix::getLeftVector(Vector3* dst) const
  455. {
  456. assert(dst);
  457. dst->x = -m[0];
  458. dst->y = -m[1];
  459. dst->z = -m[2];
  460. }
  461. void Matrix::getRightVector(Vector3* dst) const
  462. {
  463. assert(dst);
  464. dst->x = m[0];
  465. dst->y = m[1];
  466. dst->z = m[2];
  467. }
  468. void Matrix::getForwardVector(Vector3* dst) const
  469. {
  470. assert(dst);
  471. dst->x = -m[8];
  472. dst->y = -m[9];
  473. dst->z = -m[10];
  474. }
  475. void Matrix::getBackVector(Vector3* dst) const
  476. {
  477. assert(dst);
  478. dst->x = m[8];
  479. dst->y = m[9];
  480. dst->z = m[10];
  481. }
  482. bool Matrix::invert()
  483. {
  484. return invert(this);
  485. }
  486. bool Matrix::invert(Matrix* dst) const
  487. {
  488. float a0 = m[0] * m[5] - m[1] * m[4];
  489. float a1 = m[0] * m[6] - m[2] * m[4];
  490. float a2 = m[0] * m[7] - m[3] * m[4];
  491. float a3 = m[1] * m[6] - m[2] * m[5];
  492. float a4 = m[1] * m[7] - m[3] * m[5];
  493. float a5 = m[2] * m[7] - m[3] * m[6];
  494. float b0 = m[8] * m[13] - m[9] * m[12];
  495. float b1 = m[8] * m[14] - m[10] * m[12];
  496. float b2 = m[8] * m[15] - m[11] * m[12];
  497. float b3 = m[9] * m[14] - m[10] * m[13];
  498. float b4 = m[9] * m[15] - m[11] * m[13];
  499. float b5 = m[10] * m[15] - m[11] * m[14];
  500. // Calculate the determinant.
  501. float det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
  502. // Close to zero, can't invert.
  503. if (fabs(det) <= MATH_TOLERANCE)
  504. return false;
  505. // Support the case where m == dst.
  506. Matrix inverse;
  507. inverse.m[0] = m[5] * b5 - m[6] * b4 + m[7] * b3;
  508. inverse.m[1] = -m[1] * b5 + m[2] * b4 - m[3] * b3;
  509. inverse.m[2] = m[13] * a5 - m[14] * a4 + m[15] * a3;
  510. inverse.m[3] = -m[9] * a5 + m[10] * a4 - m[11] * a3;
  511. inverse.m[4] = -m[4] * b5 + m[6] * b2 - m[7] * b1;
  512. inverse.m[5] = m[0] * b5 - m[2] * b2 + m[3] * b1;
  513. inverse.m[6] = -m[12] * a5 + m[14] * a2 - m[15] * a1;
  514. inverse.m[7] = m[8] * a5 - m[10] * a2 + m[11] * a1;
  515. inverse.m[8] = m[4] * b4 - m[5] * b2 + m[7] * b0;
  516. inverse.m[9] = -m[0] * b4 + m[1] * b2 - m[3] * b0;
  517. inverse.m[10] = m[12] * a4 - m[13] * a2 + m[15] * a0;
  518. inverse.m[11] = -m[8] * a4 + m[9] * a2 - m[11] * a0;
  519. inverse.m[12] = -m[4] * b3 + m[5] * b1 - m[6] * b0;
  520. inverse.m[13] = m[0] * b3 - m[1] * b1 + m[2] * b0;
  521. inverse.m[14] = -m[12] * a3 + m[13] * a1 - m[14] * a0;
  522. inverse.m[15] = m[8] * a3 - m[9] * a1 + m[10] * a0;
  523. memcpy(dst->m, inverse.m, MATRIX_SIZE);
  524. multiply(inverse, 1.0f / det, dst);
  525. return true;
  526. }
  527. bool Matrix::isIdentity() const
  528. {
  529. return (memcmp(m, MATRIX_IDENTITY, MATRIX_SIZE) == 0);
  530. }
  531. void Matrix::multiply(float scalar)
  532. {
  533. multiply(scalar, this);
  534. }
  535. void Matrix::multiply(float scalar, Matrix* dst) const
  536. {
  537. multiply(*this, scalar, dst);
  538. }
  539. void Matrix::multiply(const Matrix& m, float scalar, Matrix* dst)
  540. {
  541. assert(dst);
  542. dst->m[0] = m.m[0] * scalar;
  543. dst->m[1] = m.m[1] * scalar;
  544. dst->m[2] = m.m[2] * scalar;
  545. dst->m[3] = m.m[3] * scalar;
  546. dst->m[4] = m.m[4] * scalar;
  547. dst->m[5] = m.m[5] * scalar;
  548. dst->m[6] = m.m[6] * scalar;
  549. dst->m[7] = m.m[7] * scalar;
  550. dst->m[8] = m.m[8] * scalar;
  551. dst->m[9] = m.m[9] * scalar;
  552. dst->m[10] = m.m[10] * scalar;
  553. dst->m[11] = m.m[11] * scalar;
  554. dst->m[12] = m.m[12] * scalar;
  555. dst->m[13] = m.m[13] * scalar;
  556. dst->m[14] = m.m[14] * scalar;
  557. dst->m[15] = m.m[15] * scalar;
  558. }
  559. void Matrix::multiply(const Matrix& m)
  560. {
  561. multiply(*this, m, this);
  562. }
  563. void Matrix::multiply(const Matrix& m1, const Matrix& m2, Matrix* dst)
  564. {
  565. assert(dst);
  566. // Support the case where m1 or m2 is the same array as dst.
  567. float product[16];
  568. product[0] = m1.m[0] * m2.m[0] + m1.m[4] * m2.m[1] + m1.m[8] * m2.m[2] + m1.m[12] * m2.m[3];
  569. product[1] = m1.m[1] * m2.m[0] + m1.m[5] * m2.m[1] + m1.m[9] * m2.m[2] + m1.m[13] * m2.m[3];
  570. product[2] = m1.m[2] * m2.m[0] + m1.m[6] * m2.m[1] + m1.m[10] * m2.m[2] + m1.m[14] * m2.m[3];
  571. product[3] = m1.m[3] * m2.m[0] + m1.m[7] * m2.m[1] + m1.m[11] * m2.m[2] + m1.m[15] * m2.m[3];
  572. product[4] = m1.m[0] * m2.m[4] + m1.m[4] * m2.m[5] + m1.m[8] * m2.m[6] + m1.m[12] * m2.m[7];
  573. product[5] = m1.m[1] * m2.m[4] + m1.m[5] * m2.m[5] + m1.m[9] * m2.m[6] + m1.m[13] * m2.m[7];
  574. product[6] = m1.m[2] * m2.m[4] + m1.m[6] * m2.m[5] + m1.m[10] * m2.m[6] + m1.m[14] * m2.m[7];
  575. product[7] = m1.m[3] * m2.m[4] + m1.m[7] * m2.m[5] + m1.m[11] * m2.m[6] + m1.m[15] * m2.m[7];
  576. product[8] = m1.m[0] * m2.m[8] + m1.m[4] * m2.m[9] + m1.m[8] * m2.m[10] + m1.m[12] * m2.m[11];
  577. product[9] = m1.m[1] * m2.m[8] + m1.m[5] * m2.m[9] + m1.m[9] * m2.m[10] + m1.m[13] * m2.m[11];
  578. product[10] = m1.m[2] * m2.m[8] + m1.m[6] * m2.m[9] + m1.m[10] * m2.m[10] + m1.m[14] * m2.m[11];
  579. product[11] = m1.m[3] * m2.m[8] + m1.m[7] * m2.m[9] + m1.m[11] * m2.m[10] + m1.m[15] * m2.m[11];
  580. product[12] = m1.m[0] * m2.m[12] + m1.m[4] * m2.m[13] + m1.m[8] * m2.m[14] + m1.m[12] * m2.m[15];
  581. product[13] = m1.m[1] * m2.m[12] + m1.m[5] * m2.m[13] + m1.m[9] * m2.m[14] + m1.m[13] * m2.m[15];
  582. product[14] = m1.m[2] * m2.m[12] + m1.m[6] * m2.m[13] + m1.m[10] * m2.m[14] + m1.m[14] * m2.m[15];
  583. product[15] = m1.m[3] * m2.m[12] + m1.m[7] * m2.m[13] + m1.m[11] * m2.m[14] + m1.m[15] * m2.m[15];
  584. memcpy(dst->m, product, MATRIX_SIZE);
  585. }
  586. void Matrix::negate()
  587. {
  588. negate(this);
  589. }
  590. void Matrix::negate(Matrix* dst) const
  591. {
  592. dst->m[0] = -m[0];
  593. dst->m[1] = -m[1];
  594. dst->m[2] = -m[2];
  595. dst->m[3] = -m[3];
  596. dst->m[4] = -m[4];
  597. dst->m[5] = -m[5];
  598. dst->m[6] = -m[6];
  599. dst->m[7] = -m[7];
  600. dst->m[8] = -m[8];
  601. dst->m[9] = -m[9];
  602. dst->m[10] = -m[10];
  603. dst->m[11] = -m[11];
  604. dst->m[12] = -m[12];
  605. dst->m[13] = -m[13];
  606. dst->m[14] = -m[14];
  607. dst->m[15] = -m[15];
  608. }
  609. void Matrix::rotate(const Quaternion& q)
  610. {
  611. rotate(q, this);
  612. }
  613. void Matrix::rotate(const Quaternion& q, Matrix* dst) const
  614. {
  615. Matrix r;
  616. createRotation(q, &r);
  617. multiply(*this, r, dst);
  618. }
  619. void Matrix::rotate(const Vector3& axis, float angle)
  620. {
  621. rotate(axis, angle, this);
  622. }
  623. void Matrix::rotate(const Vector3& axis, float angle, Matrix* dst) const
  624. {
  625. Matrix r;
  626. createRotation(axis, angle, &r);
  627. multiply(*this, r, dst);
  628. }
  629. void Matrix::rotateX(float angle)
  630. {
  631. rotateX(angle, this);
  632. }
  633. void Matrix::rotateX(float angle, Matrix* dst) const
  634. {
  635. Matrix r;
  636. createRotationX(angle, &r);
  637. multiply(*this, r, dst);
  638. }
  639. void Matrix::rotateY(float angle)
  640. {
  641. rotateY(angle, this);
  642. }
  643. void Matrix::rotateY(float angle, Matrix* dst) const
  644. {
  645. Matrix r;
  646. createRotationY(angle, &r);
  647. multiply(*this, r, dst);
  648. }
  649. void Matrix::rotateZ(float angle)
  650. {
  651. rotateZ(angle, this);
  652. }
  653. void Matrix::rotateZ(float angle, Matrix* dst) const
  654. {
  655. assert(dst);
  656. Matrix r;
  657. createRotationZ(angle, &r);
  658. multiply(*this, r, dst);
  659. }
  660. void Matrix::scale(float value)
  661. {
  662. scale(value, this);
  663. }
  664. void Matrix::scale(float value, Matrix* dst) const
  665. {
  666. scale(value, value, value, dst);
  667. }
  668. void Matrix::scale(float xScale, float yScale, float zScale)
  669. {
  670. scale(xScale, yScale, zScale, this);
  671. }
  672. void Matrix::scale(float xScale, float yScale, float zScale, Matrix* dst) const
  673. {
  674. assert(dst);
  675. Matrix s;
  676. createScale(xScale, yScale, zScale, &s);
  677. multiply(*this, s, dst);
  678. }
  679. void Matrix::scale(const Vector3& s)
  680. {
  681. scale(s.x, s.y, s.z, this);
  682. }
  683. void Matrix::scale(const Vector3& s, Matrix* dst) const
  684. {
  685. scale(s.x, s.y, s.z, dst);
  686. }
  687. void Matrix::set(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, float m31, float m32, float m33, float m34, float m41, float m42, float m43, float m44)
  688. {
  689. m[0] = m11;
  690. m[1] = m21;
  691. m[2] = m31;
  692. m[3] = m41;
  693. m[4] = m12;
  694. m[5] = m22;
  695. m[6] = m32;
  696. m[7] = m42;
  697. m[8] = m13;
  698. m[9] = m23;
  699. m[10] = m33;
  700. m[11] = m43;
  701. m[12] = m14;
  702. m[13] = m24;
  703. m[14] = m34;
  704. m[15] = m44;
  705. }
  706. void Matrix::set(const float* m)
  707. {
  708. assert(m);
  709. memcpy(this->m, m, MATRIX_SIZE);
  710. }
  711. void Matrix::set(const Matrix& m)
  712. {
  713. memcpy(this->m, m.m, MATRIX_SIZE);
  714. }
  715. void Matrix::setIdentity()
  716. {
  717. memcpy(m, MATRIX_IDENTITY, MATRIX_SIZE);
  718. }
  719. void Matrix::setZero()
  720. {
  721. memset(m, 0, MATRIX_SIZE);
  722. }
  723. void Matrix::subtract(const Matrix& m)
  724. {
  725. subtract(*this, m, this);
  726. }
  727. void Matrix::subtract(const Matrix& m1, const Matrix& m2, Matrix* dst)
  728. {
  729. dst->m[0] = m1.m[0] - m2.m[0];
  730. dst->m[1] = m1.m[1] - m2.m[1];
  731. dst->m[2] = m1.m[2] - m2.m[2];
  732. dst->m[3] = m1.m[3] - m2.m[3];
  733. dst->m[4] = m1.m[4] - m2.m[4];
  734. dst->m[5] = m1.m[5] - m2.m[5];
  735. dst->m[6] = m1.m[6] - m2.m[6];
  736. dst->m[7] = m1.m[7] - m2.m[7];
  737. dst->m[8] = m1.m[8] - m2.m[8];
  738. dst->m[9] = m1.m[9] - m2.m[9];
  739. dst->m[10] = m1.m[10] - m2.m[10];
  740. dst->m[11] = m1.m[11] - m2.m[11];
  741. dst->m[12] = m1.m[12] - m2.m[12];
  742. dst->m[13] = m1.m[13] - m2.m[13];
  743. dst->m[14] = m1.m[14] - m2.m[14];
  744. dst->m[15] = m1.m[15] - m2.m[15];
  745. }
  746. void Matrix::transformVector(Vector3* vector) const
  747. {
  748. transformVector(vector->x, vector->y, vector->z, 0.0f, vector);
  749. }
  750. void Matrix::transformVector(const Vector3& vector, Vector3* dst) const
  751. {
  752. transformVector(vector.x, vector.y, vector.z, 0.0f, dst);
  753. }
  754. void Matrix::transformPoint(Vector3* point) const
  755. {
  756. transformVector(point->x, point->y, point->z, 1.0f, point);
  757. }
  758. void Matrix::transformPoint(const Vector3& point, Vector3* dst) const
  759. {
  760. transformVector(point.x, point.y, point.z, 1.0f, dst);
  761. }
  762. void Matrix::transformVector(float x, float y, float z, float w, Vector3* dst) const
  763. {
  764. assert(dst);
  765. dst->set(
  766. x * m[0] + y * m[4] + z * m[8] + w * m[12],
  767. x * m[1] + y * m[5] + z * m[9] + w * m[13],
  768. x * m[2] + y * m[6] + z * m[10] + w * m[14] );
  769. }
  770. void Matrix::transformVector(const Vector4& vector, Vector4* dst) const
  771. {
  772. assert(dst);
  773. dst->set(
  774. vector.x * m[0] + vector.y * m[4] + vector.z * m[8] + vector.w * m[12],
  775. vector.x * m[1] + vector.y * m[5] + vector.z * m[9] + vector.w * m[13],
  776. vector.x * m[2] + vector.y * m[6] + vector.z * m[10] + vector.w * m[14],
  777. vector.x * m[3] + vector.y * m[7] + vector.z * m[11] + vector.w * m[15] );
  778. }
  779. void Matrix::translate(float x, float y, float z)
  780. {
  781. translate(x, y, z, this);
  782. }
  783. void Matrix::translate(float x, float y, float z, Matrix* dst) const
  784. {
  785. assert(dst);
  786. Matrix t;
  787. createTranslation(x, y, z, &t);
  788. multiply(*this, t, dst);
  789. }
  790. void Matrix::translate(const Vector3& t)
  791. {
  792. translate(t.x, t.y, t.z, this);
  793. }
  794. void Matrix::translate(const Vector3& t, Matrix* dst) const
  795. {
  796. translate(t.x, t.y, t.z, dst);
  797. }
  798. void Matrix::transpose()
  799. {
  800. transpose(this);
  801. }
  802. void Matrix::transpose(Matrix* dst) const
  803. {
  804. assert(dst);
  805. float t[16] = {
  806. m[0], m[4], m[8], m[12],
  807. m[1], m[5], m[9], m[13],
  808. m[2], m[6], m[10], m[14],
  809. m[3], m[7], m[11], m[15]
  810. };
  811. memcpy(dst->m, t, MATRIX_SIZE);
  812. }
  813. }