PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/MSTableView/libs/kazmath/src/mat4.c

https://bitbucket.org/safetyscissors/mstableview
C | 789 lines | 515 code | 124 blank | 150 comment | 44 complexity | 228b7ae36c4e5fcc7f8cbdf568399952 MD5 | raw file
  1. /*
  2. Copyright (c) 2008, Luke Benstead.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  15. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  18. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /**
  23. * @file mat4.c
  24. */
  25. #include <memory.h>
  26. #include <assert.h>
  27. #include <stdlib.h>
  28. #include "kazmath/utility.h"
  29. #include "kazmath/vec3.h"
  30. #include "kazmath/mat4.h"
  31. #include "kazmath/mat3.h"
  32. #include "kazmath/quaternion.h"
  33. #include "kazmath/plane.h"
  34. #include "kazmath/neon_matrix_impl.h"
  35. /**
  36. * Fills a kmMat4 structure with the values from a 16
  37. * element array of floats
  38. * @Params pOut - A pointer to the destination matrix
  39. * pMat - A 16 element array of floats
  40. * @Return Returns pOut so that the call can be nested
  41. */
  42. kmMat4* const kmMat4Fill(kmMat4* pOut, const kmScalar* pMat)
  43. {
  44. memcpy(pOut->mat, pMat, sizeof(kmScalar) * 16);
  45. return pOut;
  46. }
  47. /**
  48. * Sets pOut to an identity matrix returns pOut
  49. * @Params pOut - A pointer to the matrix to set to identity
  50. * @Return Returns pOut so that the call can be nested
  51. */
  52. kmMat4* const kmMat4Identity(kmMat4* pOut)
  53. {
  54. memset(pOut->mat, 0, sizeof(float) * 16);
  55. pOut->mat[0] = pOut->mat[5] = pOut->mat[10] = pOut->mat[15] = 1.0f;
  56. return pOut;
  57. }
  58. float get(const kmMat4 * pIn, int row, int col)
  59. {
  60. return pIn->mat[row + 4*col];
  61. }
  62. void set(kmMat4 * pIn, int row, int col, float value)
  63. {
  64. pIn->mat[row + 4*col] = value;
  65. }
  66. void swap(kmMat4 * pIn, int r1, int c1, int r2, int c2)
  67. {
  68. float tmp = get(pIn,r1,c1);
  69. set(pIn,r1,c1,get(pIn,r2,c2));
  70. set(pIn,r2,c2, tmp);
  71. }
  72. //Returns an upper and a lower triangular matrix which are L and R in the Gauss algorithm
  73. int gaussj(kmMat4 *a, kmMat4 *b)
  74. {
  75. int i, icol = 0, irow = 0, j, k, l, ll, n = 4, m = 4;
  76. float big, dum, pivinv;
  77. int indxc[n];
  78. int indxr[n];
  79. int ipiv[n];
  80. for (j = 0; j < n; j++) {
  81. ipiv[j] = 0;
  82. }
  83. for (i = 0; i < n; i++) {
  84. big = 0.0f;
  85. for (j = 0; j < n; j++) {
  86. if (ipiv[j] != 1) {
  87. for (k = 0; k < n; k++) {
  88. if (ipiv[k] == 0) {
  89. if (abs(get(a,j, k)) >= big) {
  90. big = abs(get(a,j, k));
  91. irow = j;
  92. icol = k;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. ++(ipiv[icol]);
  99. if (irow != icol) {
  100. for (l = 0; l < n; l++) {
  101. swap(a,irow, l, icol, l);
  102. }
  103. for (l = 0; l < m; l++) {
  104. swap(b,irow, l, icol, l);
  105. }
  106. }
  107. indxr[i] = irow;
  108. indxc[i] = icol;
  109. if (get(a,icol, icol) == 0.0) {
  110. return KM_FALSE;
  111. }
  112. pivinv = 1.0f / get(a,icol, icol);
  113. set(a,icol, icol, 1.0f);
  114. for (l = 0; l < n; l++) {
  115. set(a,icol, l, get(a,icol, l) * pivinv);
  116. }
  117. for (l = 0; l < m; l++) {
  118. set(b,icol, l, get(b,icol, l) * pivinv);
  119. }
  120. for (ll = 0; ll < n; ll++) {
  121. if (ll != icol) {
  122. dum = get(a,ll, icol);
  123. set(a,ll, icol, 0.0f);
  124. for (l = 0; l < n; l++) {
  125. set(a,ll, l, get(a,ll, l) - get(a,icol, l) * dum);
  126. }
  127. for (l = 0; l < m; l++) {
  128. set(b,ll, l, get(a,ll, l) - get(b,icol, l) * dum);
  129. }
  130. }
  131. }
  132. }
  133. // This is the end of the main loop over columns of the reduction. It only remains to unscram-
  134. // ble the solution in view of the column interchanges. We do this by interchanging pairs of
  135. // columns in the reverse order that the permutation was built up.
  136. for (l = n - 1; l >= 0; l--) {
  137. if (indxr[l] != indxc[l]) {
  138. for (k = 0; k < n; k++) {
  139. swap(a,k, indxr[l], k, indxc[l]);
  140. }
  141. }
  142. }
  143. return KM_TRUE;
  144. }
  145. /**
  146. * Calculates the inverse of pM and stores the result in
  147. * pOut.
  148. * @Return Returns NULL if there is no inverse, else pOut
  149. */
  150. kmMat4* const kmMat4Inverse(kmMat4* pOut, const kmMat4* pM)
  151. {
  152. kmMat4 inv;
  153. kmMat4Assign(&inv, pM);
  154. kmMat4 tmp;
  155. kmMat4Identity(&tmp);
  156. if(gaussj(&inv, &tmp) == KM_FALSE) {
  157. return NULL;
  158. }
  159. kmMat4Assign(pOut, &inv);
  160. return pOut;
  161. }
  162. /**
  163. * Returns KM_TRUE if pIn is an identity matrix
  164. * KM_FALSE otherwise
  165. */
  166. const int kmMat4IsIdentity(const kmMat4* pIn)
  167. {
  168. static const float identity [] = { 1.0f, 0.0f, 0.0f, 0.0f,
  169. 0.0f, 1.0f, 0.0f, 0.0f,
  170. 0.0f, 0.0f, 1.0f, 0.0f,
  171. 0.0f, 0.0f, 0.0f, 1.0f
  172. };
  173. return (memcmp(identity, pIn->mat, sizeof(float) * 16) == 0);
  174. }
  175. /**
  176. * Sets pOut to the transpose of pIn, returns pOut
  177. */
  178. kmMat4* const kmMat4Transpose(kmMat4* pOut, const kmMat4* pIn)
  179. {
  180. int x, z;
  181. for (z = 0; z < 4; ++z) {
  182. for (x = 0; x < 4; ++x) {
  183. pOut->mat[(z * 4) + x] = pIn->mat[(x * 4) + z];
  184. }
  185. }
  186. return pOut;
  187. }
  188. /**
  189. * Multiplies pM1 with pM2, stores the result in pOut, returns pOut
  190. */
  191. kmMat4* const kmMat4Multiply(kmMat4* pOut, const kmMat4* pM1, const kmMat4* pM2)
  192. {
  193. #if defined(__ARM_NEON__)
  194. float mat[16];
  195. // Invert column-order with row-order
  196. NEON_Matrix4Mul( &pM2->mat[0], &pM1->mat[0], &mat[0] );
  197. #else
  198. float mat[16];
  199. const float *m1 = pM1->mat, *m2 = pM2->mat;
  200. mat[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
  201. mat[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
  202. mat[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
  203. mat[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
  204. mat[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
  205. mat[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
  206. mat[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
  207. mat[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
  208. mat[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
  209. mat[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
  210. mat[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
  211. mat[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
  212. mat[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
  213. mat[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
  214. mat[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
  215. mat[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
  216. #endif
  217. memcpy(pOut->mat, mat, sizeof(float)*16);
  218. return pOut;
  219. }
  220. /**
  221. * Assigns the value of pIn to pOut
  222. */
  223. kmMat4* const kmMat4Assign(kmMat4* pOut, const kmMat4* pIn)
  224. {
  225. assert(pOut != pIn && "You have tried to self-assign!!");
  226. memcpy(pOut->mat, pIn->mat, sizeof(float)*16);
  227. return pOut;
  228. }
  229. /**
  230. * Returns KM_TRUE if the 2 matrices are equal (approximately)
  231. */
  232. const int kmMat4AreEqual(const kmMat4* pMat1, const kmMat4* pMat2)
  233. {
  234. int i = 0;
  235. assert(pMat1 != pMat2 && "You are comparing the same thing!");
  236. for (i = 0; i < 16; ++i)
  237. {
  238. if (!(pMat1->mat[i] + kmEpsilon > pMat2->mat[i] &&
  239. pMat1->mat[i] - kmEpsilon < pMat2->mat[i])) {
  240. return KM_FALSE;
  241. }
  242. }
  243. return KM_TRUE;
  244. }
  245. /**
  246. * Build a rotation matrix from an axis and an angle. Result is stored in pOut.
  247. * pOut is returned.
  248. */
  249. kmMat4* const kmMat4RotationAxisAngle(kmMat4* pOut, const kmVec3* axis, kmScalar radians)
  250. {
  251. float rcos = cosf(radians);
  252. float rsin = sinf(radians);
  253. kmVec3 normalizedAxis;
  254. kmVec3Normalize(&normalizedAxis, axis);
  255. pOut->mat[0] = rcos + normalizedAxis.x * normalizedAxis.x * (1 - rcos);
  256. pOut->mat[1] = normalizedAxis.z * rsin + normalizedAxis.y * normalizedAxis.x * (1 - rcos);
  257. pOut->mat[2] = -normalizedAxis.y * rsin + normalizedAxis.z * normalizedAxis.x * (1 - rcos);
  258. pOut->mat[3] = 0.0f;
  259. pOut->mat[4] = -normalizedAxis.z * rsin + normalizedAxis.x * normalizedAxis.y * (1 - rcos);
  260. pOut->mat[5] = rcos + normalizedAxis.y * normalizedAxis.y * (1 - rcos);
  261. pOut->mat[6] = normalizedAxis.x * rsin + normalizedAxis.z * normalizedAxis.y * (1 - rcos);
  262. pOut->mat[7] = 0.0f;
  263. pOut->mat[8] = normalizedAxis.y * rsin + normalizedAxis.x * normalizedAxis.z * (1 - rcos);
  264. pOut->mat[9] = -normalizedAxis.x * rsin + normalizedAxis.y * normalizedAxis.z * (1 - rcos);
  265. pOut->mat[10] = rcos + normalizedAxis.z * normalizedAxis.z * (1 - rcos);
  266. pOut->mat[11] = 0.0f;
  267. pOut->mat[12] = 0.0f;
  268. pOut->mat[13] = 0.0f;
  269. pOut->mat[14] = 0.0f;
  270. pOut->mat[15] = 1.0f;
  271. return pOut;
  272. }
  273. /**
  274. * Builds an X-axis rotation matrix and stores it in pOut, returns pOut
  275. */
  276. kmMat4* const kmMat4RotationX(kmMat4* pOut, const float radians)
  277. {
  278. /*
  279. | 1 0 0 0 |
  280. M = | 0 cos(A) -sin(A) 0 |
  281. | 0 sin(A) cos(A) 0 |
  282. | 0 0 0 1 |
  283. */
  284. pOut->mat[0] = 1.0f;
  285. pOut->mat[1] = 0.0f;
  286. pOut->mat[2] = 0.0f;
  287. pOut->mat[3] = 0.0f;
  288. pOut->mat[4] = 0.0f;
  289. pOut->mat[5] = cosf(radians);
  290. pOut->mat[6] = sinf(radians);
  291. pOut->mat[7] = 0.0f;
  292. pOut->mat[8] = 0.0f;
  293. pOut->mat[9] = -sinf(radians);
  294. pOut->mat[10] = cosf(radians);
  295. pOut->mat[11] = 0.0f;
  296. pOut->mat[12] = 0.0f;
  297. pOut->mat[13] = 0.0f;
  298. pOut->mat[14] = 0.0f;
  299. pOut->mat[15] = 1.0f;
  300. return pOut;
  301. }
  302. /**
  303. * Builds a rotation matrix using the rotation around the Y-axis
  304. * The result is stored in pOut, pOut is returned.
  305. */
  306. kmMat4* const kmMat4RotationY(kmMat4* pOut, const float radians)
  307. {
  308. /*
  309. | cos(A) 0 sin(A) 0 |
  310. M = | 0 1 0 0 |
  311. | -sin(A) 0 cos(A) 0 |
  312. | 0 0 0 1 |
  313. */
  314. pOut->mat[0] = cosf(radians);
  315. pOut->mat[1] = 0.0f;
  316. pOut->mat[2] = -sinf(radians);
  317. pOut->mat[3] = 0.0f;
  318. pOut->mat[4] = 0.0f;
  319. pOut->mat[5] = 1.0f;
  320. pOut->mat[6] = 0.0f;
  321. pOut->mat[7] = 0.0f;
  322. pOut->mat[8] = sinf(radians);
  323. pOut->mat[9] = 0.0f;
  324. pOut->mat[10] = cosf(radians);
  325. pOut->mat[11] = 0.0f;
  326. pOut->mat[12] = 0.0f;
  327. pOut->mat[13] = 0.0f;
  328. pOut->mat[14] = 0.0f;
  329. pOut->mat[15] = 1.0f;
  330. return pOut;
  331. }
  332. /**
  333. * Builds a rotation matrix around the Z-axis. The resulting
  334. * matrix is stored in pOut. pOut is returned.
  335. */
  336. kmMat4* const kmMat4RotationZ(kmMat4* pOut, const float radians)
  337. {
  338. /*
  339. | cos(A) -sin(A) 0 0 |
  340. M = | sin(A) cos(A) 0 0 |
  341. | 0 0 1 0 |
  342. | 0 0 0 1 |
  343. */
  344. pOut->mat[0] = cosf(radians);
  345. pOut->mat[1] = sinf(radians);
  346. pOut->mat[2] = 0.0f;
  347. pOut->mat[3] = 0.0f;
  348. pOut->mat[4] = -sinf(radians);;
  349. pOut->mat[5] = cosf(radians);
  350. pOut->mat[6] = 0.0f;
  351. pOut->mat[7] = 0.0f;
  352. pOut->mat[8] = 0.0f;
  353. pOut->mat[9] = 0.0f;
  354. pOut->mat[10] = 1.0f;
  355. pOut->mat[11] = 0.0f;
  356. pOut->mat[12] = 0.0f;
  357. pOut->mat[13] = 0.0f;
  358. pOut->mat[14] = 0.0f;
  359. pOut->mat[15] = 1.0f;
  360. return pOut;
  361. }
  362. /**
  363. * Builds a rotation matrix from pitch, yaw and roll. The resulting
  364. * matrix is stored in pOut and pOut is returned
  365. */
  366. kmMat4* const kmMat4RotationPitchYawRoll(kmMat4* pOut, const kmScalar pitch, const kmScalar yaw, const kmScalar roll)
  367. {
  368. double cr = cos(pitch);
  369. double sr = sin(pitch);
  370. double cp = cos(yaw);
  371. double sp = sin(yaw);
  372. double cy = cos(roll);
  373. double sy = sin(roll);
  374. double srsp = sr * sp;
  375. double crsp = cr * sp;
  376. pOut->mat[0] = (kmScalar) cp * cy;
  377. pOut->mat[4] = (kmScalar) cp * sy;
  378. pOut->mat[8] = (kmScalar) - sp;
  379. pOut->mat[1] = (kmScalar) srsp * cy - cr * sy;
  380. pOut->mat[5] = (kmScalar) srsp * sy + cr * cy;
  381. pOut->mat[9] = (kmScalar) sr * cp;
  382. pOut->mat[2] = (kmScalar) crsp * cy + sr * sy;
  383. pOut->mat[6] = (kmScalar) crsp * sy - sr * cy;
  384. pOut->mat[10] = (kmScalar) cr * cp;
  385. pOut->mat[3] = pOut->mat[7] = pOut->mat[11] = 0.0;
  386. pOut->mat[15] = 1.0;
  387. return pOut;
  388. }
  389. /** Converts a quaternion to a rotation matrix,
  390. * the result is stored in pOut, returns pOut
  391. */
  392. kmMat4* const kmMat4RotationQuaternion(kmMat4* pOut, const kmQuaternion* pQ)
  393. {
  394. pOut->mat[0] = 1.0f - 2.0f * (pQ->y * pQ->y + pQ->z * pQ->z );
  395. pOut->mat[1] = 2.0f * (pQ->x * pQ->y + pQ->z * pQ->w);
  396. pOut->mat[2] = 2.0f * (pQ->x * pQ->z - pQ->y * pQ->w);
  397. pOut->mat[3] = 0.0f;
  398. // Second row
  399. pOut->mat[4] = 2.0f * ( pQ->x * pQ->y - pQ->z * pQ->w );
  400. pOut->mat[5] = 1.0f - 2.0f * ( pQ->x * pQ->x + pQ->z * pQ->z );
  401. pOut->mat[6] = 2.0f * (pQ->z * pQ->y + pQ->x * pQ->w );
  402. pOut->mat[7] = 0.0f;
  403. // Third row
  404. pOut->mat[8] = 2.0f * ( pQ->x * pQ->z + pQ->y * pQ->w );
  405. pOut->mat[9] = 2.0f * ( pQ->y * pQ->z - pQ->x * pQ->w );
  406. pOut->mat[10] = 1.0f - 2.0f * ( pQ->x * pQ->x + pQ->y * pQ->y );
  407. pOut->mat[11] = 0.0f;
  408. // Fourth row
  409. pOut->mat[12] = 0;
  410. pOut->mat[13] = 0;
  411. pOut->mat[14] = 0;
  412. pOut->mat[15] = 1.0f;
  413. return pOut;
  414. }
  415. /** Builds a scaling matrix */
  416. kmMat4* const kmMat4Scaling(kmMat4* pOut, const kmScalar x, const kmScalar y,
  417. const kmScalar z)
  418. {
  419. memset(pOut->mat, 0, sizeof(float) * 16);
  420. pOut->mat[0] = x;
  421. pOut->mat[5] = y;
  422. pOut->mat[10] = z;
  423. pOut->mat[15] = 1.0f;
  424. return pOut;
  425. }
  426. /**
  427. * Builds a translation matrix. All other elements in the matrix
  428. * will be set to zero except for the diagonal which is set to 1.0
  429. */
  430. kmMat4* const kmMat4Translation(kmMat4* pOut, const kmScalar x,
  431. const kmScalar y, const kmScalar z)
  432. {
  433. //FIXME: Write a test for this
  434. memset(pOut->mat, 0, sizeof(float) * 16);
  435. pOut->mat[0] = 1.0f;
  436. pOut->mat[5] = 1.0f;
  437. pOut->mat[10] = 1.0f;
  438. pOut->mat[12] = x;
  439. pOut->mat[13] = y;
  440. pOut->mat[14] = z;
  441. pOut->mat[15] = 1.0f;
  442. return pOut;
  443. }
  444. /**
  445. * Get the up vector from a matrix. pIn is the matrix you
  446. * wish to extract the vector from. pOut is a pointer to the
  447. * kmVec3 structure that should hold the resulting vector
  448. */
  449. kmVec3* const kmMat4GetUpVec3(kmVec3* pOut, const kmMat4* pIn)
  450. {
  451. pOut->x = pIn->mat[4];
  452. pOut->y = pIn->mat[5];
  453. pOut->z = pIn->mat[6];
  454. kmVec3Normalize(pOut, pOut);
  455. return pOut;
  456. }
  457. /** Extract the right vector from a 4x4 matrix. The result is
  458. * stored in pOut. Returns pOut.
  459. */
  460. kmVec3* const kmMat4GetRightVec3(kmVec3* pOut, const kmMat4* pIn)
  461. {
  462. pOut->x = pIn->mat[0];
  463. pOut->y = pIn->mat[1];
  464. pOut->z = pIn->mat[2];
  465. kmVec3Normalize(pOut, pOut);
  466. return pOut;
  467. }
  468. /**
  469. * Extract the forward vector from a 4x4 matrix. The result is
  470. * stored in pOut. Returns pOut.
  471. */
  472. kmVec3* const kmMat4GetForwardVec3(kmVec3* pOut, const kmMat4* pIn)
  473. {
  474. pOut->x = pIn->mat[8];
  475. pOut->y = pIn->mat[9];
  476. pOut->z = pIn->mat[10];
  477. kmVec3Normalize(pOut, pOut);
  478. return pOut;
  479. }
  480. /**
  481. * Creates a perspective projection matrix in the
  482. * same way as gluPerspective
  483. */
  484. kmMat4* const kmMat4PerspectiveProjection(kmMat4* pOut, kmScalar fovY,
  485. kmScalar aspect, kmScalar zNear,
  486. kmScalar zFar)
  487. {
  488. kmScalar r = kmDegreesToRadians(fovY / 2);
  489. kmScalar deltaZ = zFar - zNear;
  490. kmScalar s = sin(r);
  491. kmScalar cotangent = 0;
  492. if (deltaZ == 0 || s == 0 || aspect == 0) {
  493. return NULL;
  494. }
  495. //cos(r) / sin(r) = cot(r)
  496. cotangent = cos(r) / s;
  497. kmMat4Identity(pOut);
  498. pOut->mat[0] = cotangent / aspect;
  499. pOut->mat[5] = cotangent;
  500. pOut->mat[10] = -(zFar + zNear) / deltaZ;
  501. pOut->mat[11] = -1;
  502. pOut->mat[14] = -2 * zNear * zFar / deltaZ;
  503. pOut->mat[15] = 0;
  504. return pOut;
  505. }
  506. /** Creates an orthographic projection matrix like glOrtho */
  507. kmMat4* const kmMat4OrthographicProjection(kmMat4* pOut, kmScalar left,
  508. kmScalar right, kmScalar bottom,
  509. kmScalar top, kmScalar nearVal,
  510. kmScalar farVal)
  511. {
  512. kmScalar tx = -((right + left) / (right - left));
  513. kmScalar ty = -((top + bottom) / (top - bottom));
  514. kmScalar tz = -((farVal + nearVal) / (farVal - nearVal));
  515. kmMat4Identity(pOut);
  516. pOut->mat[0] = 2 / (right - left);
  517. pOut->mat[5] = 2 / (top - bottom);
  518. pOut->mat[10] = -2 / (farVal - nearVal);
  519. pOut->mat[12] = tx;
  520. pOut->mat[13] = ty;
  521. pOut->mat[14] = tz;
  522. return pOut;
  523. }
  524. /**
  525. * Builds a translation matrix in the same way as gluLookAt()
  526. * the resulting matrix is stored in pOut. pOut is returned.
  527. */
  528. kmMat4* const kmMat4LookAt(kmMat4* pOut, const kmVec3* pEye,
  529. const kmVec3* pCenter, const kmVec3* pUp)
  530. {
  531. kmVec3 f, up, s, u;
  532. kmMat4 translate;
  533. kmVec3Subtract(&f, pCenter, pEye);
  534. kmVec3Normalize(&f, &f);
  535. kmVec3Assign(&up, pUp);
  536. kmVec3Normalize(&up, &up);
  537. kmVec3Cross(&s, &f, &up);
  538. kmVec3Normalize(&s, &s);
  539. kmVec3Cross(&u, &s, &f);
  540. kmVec3Normalize(&s, &s);
  541. kmMat4Identity(pOut);
  542. pOut->mat[0] = s.x;
  543. pOut->mat[4] = s.y;
  544. pOut->mat[8] = s.z;
  545. pOut->mat[1] = u.x;
  546. pOut->mat[5] = u.y;
  547. pOut->mat[9] = u.z;
  548. pOut->mat[2] = -f.x;
  549. pOut->mat[6] = -f.y;
  550. pOut->mat[10] = -f.z;
  551. kmMat4Translation(&translate, -pEye->x, -pEye->y, -pEye->z);
  552. kmMat4Multiply(pOut, pOut, &translate);
  553. return pOut;
  554. }
  555. /**
  556. * Extract a 3x3 rotation matrix from the input 4x4 transformation.
  557. * Stores the result in pOut, returns pOut
  558. */
  559. kmMat3* const kmMat4ExtractRotation(kmMat3* pOut, const kmMat4* pIn)
  560. {
  561. pOut->mat[0] = pIn->mat[0];
  562. pOut->mat[1] = pIn->mat[1];
  563. pOut->mat[2] = pIn->mat[2];
  564. pOut->mat[3] = pIn->mat[4];
  565. pOut->mat[4] = pIn->mat[5];
  566. pOut->mat[5] = pIn->mat[6];
  567. pOut->mat[6] = pIn->mat[8];
  568. pOut->mat[7] = pIn->mat[9];
  569. pOut->mat[8] = pIn->mat[10];
  570. return pOut;
  571. }
  572. /**
  573. * Take the rotation from a 4x4 transformation matrix, and return it as an axis and an angle (in radians)
  574. * returns the output axis.
  575. */
  576. kmVec3* const kmMat4RotationToAxisAngle(kmVec3* pAxis, kmScalar* radians, const kmMat4* pIn)
  577. {
  578. /*Surely not this easy?*/
  579. kmQuaternion temp;
  580. kmMat3 rotation;
  581. kmMat4ExtractRotation(&rotation, pIn);
  582. kmQuaternionRotationMatrix(&temp, &rotation);
  583. kmQuaternionToAxisAngle(&temp, pAxis, radians);
  584. return pAxis;
  585. }
  586. /** Build a 4x4 OpenGL transformation matrix using a 3x3 rotation matrix,
  587. * and a 3d vector representing a translation. Assign the result to pOut,
  588. * pOut is also returned.
  589. */
  590. kmMat4* const kmMat4RotationTranslation(kmMat4* pOut, const kmMat3* rotation, const kmVec3* translation)
  591. {
  592. pOut->mat[0] = rotation->mat[0];
  593. pOut->mat[1] = rotation->mat[1];
  594. pOut->mat[2] = rotation->mat[2];
  595. pOut->mat[3] = 0.0f;
  596. pOut->mat[4] = rotation->mat[3];
  597. pOut->mat[5] = rotation->mat[4];
  598. pOut->mat[6] = rotation->mat[5];
  599. pOut->mat[7] = 0.0f;
  600. pOut->mat[8] = rotation->mat[6];
  601. pOut->mat[9] = rotation->mat[7];
  602. pOut->mat[10] = rotation->mat[8];
  603. pOut->mat[11] = 0.0f;
  604. pOut->mat[12] = translation->x;
  605. pOut->mat[13] = translation->y;
  606. pOut->mat[14] = translation->z;
  607. pOut->mat[15] = 1.0f;
  608. return pOut;
  609. }
  610. kmPlane* const kmMat4ExtractPlane(kmPlane* pOut, const kmMat4* pIn, const kmEnum plane)
  611. {
  612. float t = 1.0f;
  613. switch(plane) {
  614. case KM_PLANE_RIGHT:
  615. pOut->a = pIn->mat[3] - pIn->mat[0];
  616. pOut->b = pIn->mat[7] - pIn->mat[4];
  617. pOut->c = pIn->mat[11] - pIn->mat[8];
  618. pOut->d = pIn->mat[15] - pIn->mat[12];
  619. break;
  620. case KM_PLANE_LEFT:
  621. pOut->a = pIn->mat[3] + pIn->mat[0];
  622. pOut->b = pIn->mat[7] + pIn->mat[4];
  623. pOut->c = pIn->mat[11] + pIn->mat[8];
  624. pOut->d = pIn->mat[15] + pIn->mat[12];
  625. break;
  626. case KM_PLANE_BOTTOM:
  627. pOut->a = pIn->mat[3] + pIn->mat[1];
  628. pOut->b = pIn->mat[7] + pIn->mat[5];
  629. pOut->c = pIn->mat[11] + pIn->mat[9];
  630. pOut->d = pIn->mat[15] + pIn->mat[13];
  631. break;
  632. case KM_PLANE_TOP:
  633. pOut->a = pIn->mat[3] - pIn->mat[1];
  634. pOut->b = pIn->mat[7] - pIn->mat[5];
  635. pOut->c = pIn->mat[11] - pIn->mat[9];
  636. pOut->d = pIn->mat[15] - pIn->mat[13];
  637. break;
  638. case KM_PLANE_FAR:
  639. pOut->a = pIn->mat[3] - pIn->mat[2];
  640. pOut->b = pIn->mat[7] - pIn->mat[6];
  641. pOut->c = pIn->mat[11] - pIn->mat[10];
  642. pOut->d = pIn->mat[15] - pIn->mat[14];
  643. break;
  644. case KM_PLANE_NEAR:
  645. pOut->a = pIn->mat[3] + pIn->mat[2];
  646. pOut->b = pIn->mat[7] + pIn->mat[6];
  647. pOut->c = pIn->mat[11] + pIn->mat[10];
  648. pOut->d = pIn->mat[15] + pIn->mat[14];
  649. break;
  650. default:
  651. assert(0 && "Invalid plane index");
  652. }
  653. t = sqrtf(pOut->a * pOut->a +
  654. pOut->b * pOut->b +
  655. pOut->c * pOut->c);
  656. pOut->a /= t;
  657. pOut->b /= t;
  658. pOut->c /= t;
  659. pOut->d /= t;
  660. return pOut;
  661. }