Matrix.cpp 25 KB

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