Matrix.cpp 23 KB

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