Matrix.cpp 23 KB

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