PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/cogl/cogl-matrix.c

https://github.com/collects/cogl
C | 1659 lines | 1042 code | 188 blank | 429 comment | 170 complexity | d11f21eab356638b55e595e0efac5062 MD5 | raw file
  1. /*
  2. * Cogl
  3. *
  4. * An object oriented GL/GLES Abstraction/Utility Layer
  5. *
  6. * Copyright (C) 2009,2010,2011 Intel Corporation.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Authors:
  22. * Robert Bragg <robert@linux.intel.com>
  23. */
  24. /*
  25. * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
  26. *
  27. * Permission is hereby granted, free of charge, to any person obtaining a
  28. * copy of this software and associated documentation files (the "Software"),
  29. * to deal in the Software without restriction, including without limitation
  30. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  31. * and/or sell copies of the Software, and to permit persons to whom the
  32. * Software is furnished to do so, subject to the following conditions:
  33. *
  34. * The above copyright notice and this permission notice shall be included
  35. * in all copies or substantial portions of the Software.
  36. *
  37. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  38. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  39. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  40. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  41. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  42. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  43. */
  44. /*
  45. * Note: a lot of this code is based on code that was taken from Mesa.
  46. *
  47. * Changes compared to the original code from Mesa:
  48. *
  49. * - instead of allocating matrix->m and matrix->inv using malloc, our
  50. * public CoglMatrix typedef is large enough to directly contain the
  51. * matrix, its inverse, a type and a set of flags.
  52. * - instead of having a _cogl_matrix_analyse which updates the type,
  53. * flags and inverse, we have _cogl_matrix_update_inverse which
  54. * essentially does the same thing (internally making use of
  55. * _cogl_matrix_update_type_and_flags()) but with additional guards in
  56. * place to bail out when the inverse matrix is still valid.
  57. * - when initializing a matrix with the identity matrix we don't
  58. * immediately initialize the inverse matrix; rather we just set the
  59. * dirty flag for the inverse (since it's likely the user won't request
  60. * the inverse of the identity matrix)
  61. */
  62. #ifdef HAVE_CONFIG_H
  63. #include "config.h"
  64. #endif
  65. #include <cogl.h>
  66. #include <cogl-util.h>
  67. #include <cogl-debug.h>
  68. #include <cogl-quaternion.h>
  69. #include <cogl-quaternion-private.h>
  70. #include <cogl-matrix.h>
  71. #include <cogl-matrix-private.h>
  72. #include <cogl-quaternion-private.h>
  73. #include <glib.h>
  74. #include <math.h>
  75. #include <string.h>
  76. #ifdef _COGL_SUPPORTS_GTYPE_INTEGRATION
  77. #include <cogl-gtype-private.h>
  78. COGL_GTYPE_DEFINE_BOXED ("Matrix", matrix,
  79. cogl_matrix_copy,
  80. cogl_matrix_free);
  81. #endif
  82. /*
  83. * Symbolic names to some of the entries in the matrix
  84. *
  85. * These are handy for the viewport mapping, which is expressed as a matrix.
  86. */
  87. #define MAT_SX 0
  88. #define MAT_SY 5
  89. #define MAT_SZ 10
  90. #define MAT_TX 12
  91. #define MAT_TY 13
  92. #define MAT_TZ 14
  93. /*
  94. * These identify different kinds of 4x4 transformation matrices and we use
  95. * this information to find fast-paths when available.
  96. */
  97. enum CoglMatrixType {
  98. COGL_MATRIX_TYPE_GENERAL, /**< general 4x4 matrix */
  99. COGL_MATRIX_TYPE_IDENTITY, /**< identity matrix */
  100. COGL_MATRIX_TYPE_3D_NO_ROT, /**< orthogonal projection and others... */
  101. COGL_MATRIX_TYPE_PERSPECTIVE, /**< perspective projection matrix */
  102. COGL_MATRIX_TYPE_2D, /**< 2-D transformation */
  103. COGL_MATRIX_TYPE_2D_NO_ROT, /**< 2-D scale & translate only */
  104. COGL_MATRIX_TYPE_3D, /**< 3-D transformation */
  105. COGL_MATRIX_N_TYPES
  106. } ;
  107. #define DEG2RAD (G_PI/180.0)
  108. /* Dot product of two 2-element vectors */
  109. #define DOT2(A,B) ( (A)[0]*(B)[0] + (A)[1]*(B)[1] )
  110. /* Dot product of two 3-element vectors */
  111. #define DOT3(A,B) ( (A)[0]*(B)[0] + (A)[1]*(B)[1] + (A)[2]*(B)[2] )
  112. #define CROSS3(N, U, V) \
  113. do { \
  114. (N)[0] = (U)[1]*(V)[2] - (U)[2]*(V)[1]; \
  115. (N)[1] = (U)[2]*(V)[0] - (U)[0]*(V)[2]; \
  116. (N)[2] = (U)[0]*(V)[1] - (U)[1]*(V)[0]; \
  117. } while (0)
  118. #define SUB_3V(DST, SRCA, SRCB) \
  119. do { \
  120. (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
  121. (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
  122. (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
  123. } while (0)
  124. #define LEN_SQUARED_3FV( V ) ((V)[0]*(V)[0]+(V)[1]*(V)[1]+(V)[2]*(V)[2])
  125. /*
  126. * \defgroup MatFlags MAT_FLAG_XXX-flags
  127. *
  128. * Bitmasks to indicate different kinds of 4x4 matrices in CoglMatrix::flags
  129. */
  130. #define MAT_FLAG_IDENTITY 0 /*< is an identity matrix flag.
  131. * (Not actually used - the identity
  132. * matrix is identified by the absense
  133. * of all other flags.)
  134. */
  135. #define MAT_FLAG_GENERAL 0x1 /*< is a general matrix flag */
  136. #define MAT_FLAG_ROTATION 0x2 /*< is a rotation matrix flag */
  137. #define MAT_FLAG_TRANSLATION 0x4 /*< is a translation matrix flag */
  138. #define MAT_FLAG_UNIFORM_SCALE 0x8 /*< is an uniform scaling matrix flag */
  139. #define MAT_FLAG_GENERAL_SCALE 0x10 /*< is a general scaling matrix flag */
  140. #define MAT_FLAG_GENERAL_3D 0x20 /*< general 3D matrix flag */
  141. #define MAT_FLAG_PERSPECTIVE 0x40 /*< is a perspective proj matrix flag */
  142. #define MAT_FLAG_SINGULAR 0x80 /*< is a singular matrix flag */
  143. #define MAT_DIRTY_TYPE 0x100 /*< matrix type is dirty */
  144. #define MAT_DIRTY_FLAGS 0x200 /*< matrix flags are dirty */
  145. #define MAT_DIRTY_INVERSE 0x400 /*< matrix inverse is dirty */
  146. /* angle preserving matrix flags mask */
  147. #define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \
  148. MAT_FLAG_TRANSLATION | \
  149. MAT_FLAG_UNIFORM_SCALE)
  150. /* geometry related matrix flags mask */
  151. #define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \
  152. MAT_FLAG_ROTATION | \
  153. MAT_FLAG_TRANSLATION | \
  154. MAT_FLAG_UNIFORM_SCALE | \
  155. MAT_FLAG_GENERAL_SCALE | \
  156. MAT_FLAG_GENERAL_3D | \
  157. MAT_FLAG_PERSPECTIVE | \
  158. MAT_FLAG_SINGULAR)
  159. /* length preserving matrix flags mask */
  160. #define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \
  161. MAT_FLAG_TRANSLATION)
  162. /* 3D (non-perspective) matrix flags mask */
  163. #define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \
  164. MAT_FLAG_TRANSLATION | \
  165. MAT_FLAG_UNIFORM_SCALE | \
  166. MAT_FLAG_GENERAL_SCALE | \
  167. MAT_FLAG_GENERAL_3D)
  168. /* dirty matrix flags mask */
  169. #define MAT_DIRTY_ALL (MAT_DIRTY_TYPE | \
  170. MAT_DIRTY_FLAGS | \
  171. MAT_DIRTY_INVERSE)
  172. /*
  173. * Test geometry related matrix flags.
  174. *
  175. * @mat a pointer to a CoglMatrix structure.
  176. * @a flags mask.
  177. *
  178. * Returns: non-zero if all geometry related matrix flags are contained within
  179. * the mask, or zero otherwise.
  180. */
  181. #define TEST_MAT_FLAGS(mat, a) \
  182. ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)
  183. /*
  184. * Names of the corresponding CoglMatrixType values.
  185. */
  186. static const char *types[] = {
  187. "COGL_MATRIX_TYPE_GENERAL",
  188. "COGL_MATRIX_TYPE_IDENTITY",
  189. "COGL_MATRIX_TYPE_3D_NO_ROT",
  190. "COGL_MATRIX_TYPE_PERSPECTIVE",
  191. "COGL_MATRIX_TYPE_2D",
  192. "COGL_MATRIX_TYPE_2D_NO_ROT",
  193. "COGL_MATRIX_TYPE_3D"
  194. };
  195. /*
  196. * Identity matrix.
  197. */
  198. static float identity[16] = {
  199. 1.0, 0.0, 0.0, 0.0,
  200. 0.0, 1.0, 0.0, 0.0,
  201. 0.0, 0.0, 1.0, 0.0,
  202. 0.0, 0.0, 0.0, 1.0
  203. };
  204. #define A(row,col) a[(col<<2)+row]
  205. #define B(row,col) b[(col<<2)+row]
  206. #define R(row,col) result[(col<<2)+row]
  207. /*
  208. * Perform a full 4x4 matrix multiplication.
  209. *
  210. * <note>It's assumed that @result != @b. @product == @a is allowed.</note>
  211. *
  212. * <note>KW: 4*16 = 64 multiplications</note>
  213. */
  214. static void
  215. matrix_multiply4x4 (float *result, const float *a, const float *b)
  216. {
  217. int i;
  218. for (i = 0; i < 4; i++)
  219. {
  220. const float ai0 = A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
  221. R(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
  222. R(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
  223. R(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
  224. R(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
  225. }
  226. }
  227. /*
  228. * Multiply two matrices known to occupy only the top three rows, such
  229. * as typical model matrices, and orthogonal matrices.
  230. *
  231. * @a matrix.
  232. * @b matrix.
  233. * @product will receive the product of \p a and \p b.
  234. */
  235. static void
  236. matrix_multiply3x4 (float *result, const float *a, const float *b)
  237. {
  238. int i;
  239. for (i = 0; i < 3; i++)
  240. {
  241. const float ai0 = A(i,0), ai1 = A(i,1), ai2 = A(i,2), ai3 = A(i,3);
  242. R(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
  243. R(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
  244. R(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
  245. R(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
  246. }
  247. R(3,0) = 0;
  248. R(3,1) = 0;
  249. R(3,2) = 0;
  250. R(3,3) = 1;
  251. }
  252. #undef A
  253. #undef B
  254. #undef R
  255. /*
  256. * Multiply a matrix by an array of floats with known properties.
  257. *
  258. * @mat pointer to a CoglMatrix structure containing the left multiplication
  259. * matrix, and that will receive the product result.
  260. * @m right multiplication matrix array.
  261. * @flags flags of the matrix \p m.
  262. *
  263. * Joins both flags and marks the type and inverse as dirty. Calls
  264. * matrix_multiply3x4() if both matrices are 3D, or matrix_multiply4x4()
  265. * otherwise.
  266. */
  267. static void
  268. matrix_multiply_array_with_flags (CoglMatrix *result,
  269. const float *array,
  270. unsigned int flags)
  271. {
  272. result->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
  273. if (TEST_MAT_FLAGS (result, MAT_FLAGS_3D))
  274. matrix_multiply3x4 ((float *)result, (float *)result, array);
  275. else
  276. matrix_multiply4x4 ((float *)result, (float *)result, array);
  277. }
  278. /* Joins both flags and marks the type and inverse as dirty. Calls
  279. * matrix_multiply3x4() if both matrices are 3D, or matrix_multiply4x4()
  280. * otherwise.
  281. */
  282. static void
  283. _cogl_matrix_multiply (CoglMatrix *result,
  284. const CoglMatrix *a,
  285. const CoglMatrix *b)
  286. {
  287. result->flags = (a->flags |
  288. b->flags |
  289. MAT_DIRTY_TYPE |
  290. MAT_DIRTY_INVERSE);
  291. if (TEST_MAT_FLAGS(result, MAT_FLAGS_3D))
  292. matrix_multiply3x4 ((float *)result, (float *)a, (float *)b);
  293. else
  294. matrix_multiply4x4 ((float *)result, (float *)a, (float *)b);
  295. }
  296. void
  297. cogl_matrix_multiply (CoglMatrix *result,
  298. const CoglMatrix *a,
  299. const CoglMatrix *b)
  300. {
  301. _cogl_matrix_multiply (result, a, b);
  302. _COGL_MATRIX_DEBUG_PRINT (result);
  303. }
  304. #if 0
  305. /* Marks the matrix flags with general flag, and type and inverse dirty flags.
  306. * Calls matrix_multiply4x4() for the multiplication.
  307. */
  308. static void
  309. _cogl_matrix_multiply_array (CoglMatrix *result, const float *array)
  310. {
  311. result->flags |= (MAT_FLAG_GENERAL |
  312. MAT_DIRTY_TYPE |
  313. MAT_DIRTY_INVERSE |
  314. MAT_DIRTY_FLAGS);
  315. matrix_multiply4x4 ((float *)result, (float *)result, (float *)array);
  316. }
  317. #endif
  318. /*
  319. * Print a matrix array.
  320. *
  321. * Called by _cogl_matrix_print() to print a matrix or its inverse.
  322. */
  323. static void
  324. print_matrix_floats (const float m[16])
  325. {
  326. int i;
  327. for (i = 0;i < 4; i++)
  328. g_print ("\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
  329. }
  330. /*
  331. * Dumps the contents of a CoglMatrix structure.
  332. */
  333. void
  334. _cogl_matrix_print (const CoglMatrix *matrix)
  335. {
  336. if (!(matrix->flags & MAT_DIRTY_TYPE))
  337. {
  338. _COGL_RETURN_IF_FAIL (matrix->type < COGL_MATRIX_N_TYPES);
  339. g_print ("Matrix type: %s, flags: %x\n",
  340. types[matrix->type], (int)matrix->flags);
  341. }
  342. else
  343. g_print ("Matrix type: DIRTY, flags: %x\n", (int)matrix->flags);
  344. print_matrix_floats ((float *)matrix);
  345. g_print ("Inverse: \n");
  346. if (!(matrix->flags & MAT_DIRTY_INVERSE))
  347. {
  348. float prod[16];
  349. print_matrix_floats (matrix->inv);
  350. matrix_multiply4x4 (prod, (float *)matrix, matrix->inv);
  351. g_print ("Mat * Inverse:\n");
  352. print_matrix_floats (prod);
  353. }
  354. else
  355. g_print (" - not available\n");
  356. }
  357. /*
  358. * References an element of 4x4 matrix.
  359. *
  360. * @m matrix array.
  361. * @c column of the desired element.
  362. * @r row of the desired element.
  363. *
  364. * Returns: value of the desired element.
  365. *
  366. * Calculate the linear storage index of the element and references it.
  367. */
  368. #define MAT(m,r,c) (m)[(c)*4+(r)]
  369. /*
  370. * Swaps the values of two floating pointer variables.
  371. *
  372. * Used by invert_matrix_general() to swap the row pointers.
  373. */
  374. #define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }
  375. /*
  376. * Compute inverse of 4x4 transformation matrix.
  377. *
  378. * @mat pointer to a CoglMatrix structure. The matrix inverse will be
  379. * stored in the CoglMatrix::inv attribute.
  380. *
  381. * Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
  382. *
  383. * \author
  384. * Code contributed by Jacques Leroy jle@star.be
  385. *
  386. * Calculates the inverse matrix by performing the gaussian matrix reduction
  387. * with partial pivoting followed by back/substitution with the loops manually
  388. * unrolled.
  389. */
  390. static gboolean
  391. invert_matrix_general (CoglMatrix *matrix)
  392. {
  393. const float *m = (float *)matrix;
  394. float *out = matrix->inv;
  395. float wtmp[4][8];
  396. float m0, m1, m2, m3, s;
  397. float *r0, *r1, *r2, *r3;
  398. r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
  399. r0[0] = MAT (m, 0, 0), r0[1] = MAT (m, 0, 1),
  400. r0[2] = MAT (m, 0, 2), r0[3] = MAT (m, 0, 3),
  401. r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
  402. r1[0] = MAT (m, 1, 0), r1[1] = MAT (m, 1, 1),
  403. r1[2] = MAT (m, 1, 2), r1[3] = MAT (m, 1, 3),
  404. r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
  405. r2[0] = MAT (m, 2, 0), r2[1] = MAT (m, 2, 1),
  406. r2[2] = MAT (m, 2, 2), r2[3] = MAT (m, 2, 3),
  407. r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
  408. r3[0] = MAT (m, 3, 0), r3[1] = MAT (m, 3, 1),
  409. r3[2] = MAT (m, 3, 2), r3[3] = MAT (m, 3, 3),
  410. r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
  411. /* choose pivot - or die */
  412. if (fabsf (r3[0]) > fabsf (r2[0]))
  413. SWAP_ROWS (r3, r2);
  414. if (fabsf (r2[0]) > fabsf (r1[0]))
  415. SWAP_ROWS (r2, r1);
  416. if (fabsf (r1[0]) > fabsf (r0[0]))
  417. SWAP_ROWS (r1, r0);
  418. if (0.0 == r0[0])
  419. return FALSE;
  420. /* eliminate first variable */
  421. m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
  422. s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
  423. s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
  424. s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
  425. s = r0[4];
  426. if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
  427. s = r0[5];
  428. if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
  429. s = r0[6];
  430. if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
  431. s = r0[7];
  432. if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
  433. /* choose pivot - or die */
  434. if (fabsf (r3[1]) > fabsf (r2[1]))
  435. SWAP_ROWS (r3, r2);
  436. if (fabsf (r2[1]) > fabsf (r1[1]))
  437. SWAP_ROWS (r2, r1);
  438. if (0.0 == r1[1])
  439. return FALSE;
  440. /* eliminate second variable */
  441. m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
  442. r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
  443. r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
  444. s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
  445. s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
  446. s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
  447. s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
  448. /* choose pivot - or die */
  449. if (fabsf (r3[2]) > fabsf (r2[2]))
  450. SWAP_ROWS (r3, r2);
  451. if (0.0 == r2[2])
  452. return FALSE;
  453. /* eliminate third variable */
  454. m3 = r3[2] / r2[2];
  455. r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
  456. r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
  457. r3[7] -= m3 * r2[7];
  458. /* last check */
  459. if (0.0 == r3[3])
  460. return FALSE;
  461. s = 1.0f / r3[3]; /* now back substitute row 3 */
  462. r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
  463. m2 = r2[3]; /* now back substitute row 2 */
  464. s = 1.0f / r2[2];
  465. r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
  466. r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
  467. m1 = r1[3];
  468. r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
  469. r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
  470. m0 = r0[3];
  471. r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
  472. r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
  473. m1 = r1[2]; /* now back substitute row 1 */
  474. s = 1.0f / r1[1];
  475. r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
  476. r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
  477. m0 = r0[2];
  478. r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
  479. r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
  480. m0 = r0[1]; /* now back substitute row 0 */
  481. s = 1.0f / r0[0];
  482. r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
  483. r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
  484. MAT (out, 0, 0) = r0[4]; MAT (out, 0, 1) = r0[5],
  485. MAT (out, 0, 2) = r0[6]; MAT (out, 0, 3) = r0[7],
  486. MAT (out, 1, 0) = r1[4]; MAT (out, 1, 1) = r1[5],
  487. MAT (out, 1, 2) = r1[6]; MAT (out, 1, 3) = r1[7],
  488. MAT (out, 2, 0) = r2[4]; MAT (out, 2, 1) = r2[5],
  489. MAT (out, 2, 2) = r2[6]; MAT (out, 2, 3) = r2[7],
  490. MAT (out, 3, 0) = r3[4]; MAT (out, 3, 1) = r3[5],
  491. MAT (out, 3, 2) = r3[6]; MAT (out, 3, 3) = r3[7];
  492. return TRUE;
  493. }
  494. #undef SWAP_ROWS
  495. /*
  496. * Compute inverse of a general 3d transformation matrix.
  497. *
  498. * @mat pointer to a CoglMatrix structure. The matrix inverse will be
  499. * stored in the CoglMatrix::inv attribute.
  500. *
  501. * Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
  502. *
  503. * \author Adapted from graphics gems II.
  504. *
  505. * Calculates the inverse of the upper left by first calculating its
  506. * determinant and multiplying it to the symmetric adjust matrix of each
  507. * element. Finally deals with the translation part by transforming the
  508. * original translation vector using by the calculated submatrix inverse.
  509. */
  510. static gboolean
  511. invert_matrix_3d_general (CoglMatrix *matrix)
  512. {
  513. const float *in = (float *)matrix;
  514. float *out = matrix->inv;
  515. float pos, neg, t;
  516. float det;
  517. /* Calculate the determinant of upper left 3x3 submatrix and
  518. * determine if the matrix is singular.
  519. */
  520. pos = neg = 0.0;
  521. t = MAT (in,0,0) * MAT (in,1,1) * MAT (in,2,2);
  522. if (t >= 0.0) pos += t; else neg += t;
  523. t = MAT (in,1,0) * MAT (in,2,1) * MAT (in,0,2);
  524. if (t >= 0.0) pos += t; else neg += t;
  525. t = MAT (in,2,0) * MAT (in,0,1) * MAT (in,1,2);
  526. if (t >= 0.0) pos += t; else neg += t;
  527. t = -MAT (in,2,0) * MAT (in,1,1) * MAT (in,0,2);
  528. if (t >= 0.0) pos += t; else neg += t;
  529. t = -MAT (in,1,0) * MAT (in,0,1) * MAT (in,2,2);
  530. if (t >= 0.0) pos += t; else neg += t;
  531. t = -MAT (in,0,0) * MAT (in,2,1) * MAT (in,1,2);
  532. if (t >= 0.0) pos += t; else neg += t;
  533. det = pos + neg;
  534. if (det*det < 1e-25)
  535. return FALSE;
  536. det = 1.0f / det;
  537. MAT (out,0,0) =
  538. ( (MAT (in, 1, 1)*MAT (in, 2, 2) - MAT (in, 2, 1)*MAT (in, 1, 2) )*det);
  539. MAT (out,0,1) =
  540. (- (MAT (in, 0, 1)*MAT (in, 2, 2) - MAT (in, 2, 1)*MAT (in, 0, 2) )*det);
  541. MAT (out,0,2) =
  542. ( (MAT (in, 0, 1)*MAT (in, 1, 2) - MAT (in, 1, 1)*MAT (in, 0, 2) )*det);
  543. MAT (out,1,0) =
  544. (- (MAT (in,1,0)*MAT (in,2,2) - MAT (in,2,0)*MAT (in,1,2) )*det);
  545. MAT (out,1,1) =
  546. ( (MAT (in,0,0)*MAT (in,2,2) - MAT (in,2,0)*MAT (in,0,2) )*det);
  547. MAT (out,1,2) =
  548. (- (MAT (in,0,0)*MAT (in,1,2) - MAT (in,1,0)*MAT (in,0,2) )*det);
  549. MAT (out,2,0) =
  550. ( (MAT (in,1,0)*MAT (in,2,1) - MAT (in,2,0)*MAT (in,1,1) )*det);
  551. MAT (out,2,1) =
  552. (- (MAT (in,0,0)*MAT (in,2,1) - MAT (in,2,0)*MAT (in,0,1) )*det);
  553. MAT (out,2,2) =
  554. ( (MAT (in,0,0)*MAT (in,1,1) - MAT (in,1,0)*MAT (in,0,1) )*det);
  555. /* Do the translation part */
  556. MAT (out,0,3) = - (MAT (in, 0, 3) * MAT (out, 0, 0) +
  557. MAT (in, 1, 3) * MAT (out, 0, 1) +
  558. MAT (in, 2, 3) * MAT (out, 0, 2) );
  559. MAT (out,1,3) = - (MAT (in, 0, 3) * MAT (out, 1, 0) +
  560. MAT (in, 1, 3) * MAT (out, 1, 1) +
  561. MAT (in, 2, 3) * MAT (out, 1, 2) );
  562. MAT (out,2,3) = - (MAT (in, 0, 3) * MAT (out, 2 ,0) +
  563. MAT (in, 1, 3) * MAT (out, 2, 1) +
  564. MAT (in, 2, 3) * MAT (out, 2, 2) );
  565. return TRUE;
  566. }
  567. /*
  568. * Compute inverse of a 3d transformation matrix.
  569. *
  570. * @mat pointer to a CoglMatrix structure. The matrix inverse will be
  571. * stored in the CoglMatrix::inv attribute.
  572. *
  573. * Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
  574. *
  575. * If the matrix is not an angle preserving matrix then calls
  576. * invert_matrix_3d_general for the actual calculation. Otherwise calculates
  577. * the inverse matrix analyzing and inverting each of the scaling, rotation and
  578. * translation parts.
  579. */
  580. static gboolean
  581. invert_matrix_3d (CoglMatrix *matrix)
  582. {
  583. const float *in = (float *)matrix;
  584. float *out = matrix->inv;
  585. if (!TEST_MAT_FLAGS(matrix, MAT_FLAGS_ANGLE_PRESERVING))
  586. return invert_matrix_3d_general (matrix);
  587. if (matrix->flags & MAT_FLAG_UNIFORM_SCALE)
  588. {
  589. float scale = (MAT (in, 0, 0) * MAT (in, 0, 0) +
  590. MAT (in, 0, 1) * MAT (in, 0, 1) +
  591. MAT (in, 0, 2) * MAT (in, 0, 2));
  592. if (scale == 0.0)
  593. return FALSE;
  594. scale = 1.0f / scale;
  595. /* Transpose and scale the 3 by 3 upper-left submatrix. */
  596. MAT (out, 0, 0) = scale * MAT (in, 0, 0);
  597. MAT (out, 1, 0) = scale * MAT (in, 0, 1);
  598. MAT (out, 2, 0) = scale * MAT (in, 0, 2);
  599. MAT (out, 0, 1) = scale * MAT (in, 1, 0);
  600. MAT (out, 1, 1) = scale * MAT (in, 1, 1);
  601. MAT (out, 2, 1) = scale * MAT (in, 1, 2);
  602. MAT (out, 0, 2) = scale * MAT (in, 2, 0);
  603. MAT (out, 1, 2) = scale * MAT (in, 2, 1);
  604. MAT (out, 2, 2) = scale * MAT (in, 2, 2);
  605. }
  606. else if (matrix->flags & MAT_FLAG_ROTATION)
  607. {
  608. /* Transpose the 3 by 3 upper-left submatrix. */
  609. MAT (out, 0, 0) = MAT (in, 0, 0);
  610. MAT (out, 1, 0) = MAT (in, 0, 1);
  611. MAT (out, 2, 0) = MAT (in, 0, 2);
  612. MAT (out, 0, 1) = MAT (in, 1, 0);
  613. MAT (out, 1, 1) = MAT (in, 1, 1);
  614. MAT (out, 2, 1) = MAT (in, 1, 2);
  615. MAT (out, 0, 2) = MAT (in, 2, 0);
  616. MAT (out, 1, 2) = MAT (in, 2, 1);
  617. MAT (out, 2, 2) = MAT (in, 2, 2);
  618. }
  619. else
  620. {
  621. /* pure translation */
  622. memcpy (out, identity, 16 * sizeof (float));
  623. MAT (out, 0, 3) = - MAT (in, 0, 3);
  624. MAT (out, 1, 3) = - MAT (in, 1, 3);
  625. MAT (out, 2, 3) = - MAT (in, 2, 3);
  626. return TRUE;
  627. }
  628. if (matrix->flags & MAT_FLAG_TRANSLATION)
  629. {
  630. /* Do the translation part */
  631. MAT (out,0,3) = - (MAT (in, 0, 3) * MAT (out, 0, 0) +
  632. MAT (in, 1, 3) * MAT (out, 0, 1) +
  633. MAT (in, 2, 3) * MAT (out, 0, 2) );
  634. MAT (out,1,3) = - (MAT (in, 0, 3) * MAT (out, 1, 0) +
  635. MAT (in, 1, 3) * MAT (out, 1, 1) +
  636. MAT (in, 2, 3) * MAT (out, 1, 2) );
  637. MAT (out,2,3) = - (MAT (in, 0, 3) * MAT (out, 2, 0) +
  638. MAT (in, 1, 3) * MAT (out, 2, 1) +
  639. MAT (in, 2, 3) * MAT (out, 2, 2) );
  640. }
  641. else
  642. MAT (out, 0, 3) = MAT (out, 1, 3) = MAT (out, 2, 3) = 0.0;
  643. return TRUE;
  644. }
  645. /*
  646. * Compute inverse of an identity transformation matrix.
  647. *
  648. * @mat pointer to a CoglMatrix structure. The matrix inverse will be
  649. * stored in the CoglMatrix::inv attribute.
  650. *
  651. * Returns: always %TRUE.
  652. *
  653. * Simply copies identity into CoglMatrix::inv.
  654. */
  655. static gboolean
  656. invert_matrix_identity (CoglMatrix *matrix)
  657. {
  658. memcpy (matrix->inv, identity, 16 * sizeof (float));
  659. return TRUE;
  660. }
  661. /*
  662. * Compute inverse of a no-rotation 3d transformation matrix.
  663. *
  664. * @mat pointer to a CoglMatrix structure. The matrix inverse will be
  665. * stored in the CoglMatrix::inv attribute.
  666. *
  667. * Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
  668. *
  669. * Calculates the
  670. */
  671. static gboolean
  672. invert_matrix_3d_no_rotation (CoglMatrix *matrix)
  673. {
  674. const float *in = (float *)matrix;
  675. float *out = matrix->inv;
  676. if (MAT (in,0,0) == 0 || MAT (in,1,1) == 0 || MAT (in,2,2) == 0)
  677. return FALSE;
  678. memcpy (out, identity, 16 * sizeof (float));
  679. MAT (out,0,0) = 1.0f / MAT (in,0,0);
  680. MAT (out,1,1) = 1.0f / MAT (in,1,1);
  681. MAT (out,2,2) = 1.0f / MAT (in,2,2);
  682. if (matrix->flags & MAT_FLAG_TRANSLATION)
  683. {
  684. MAT (out,0,3) = - (MAT (in,0,3) * MAT (out,0,0));
  685. MAT (out,1,3) = - (MAT (in,1,3) * MAT (out,1,1));
  686. MAT (out,2,3) = - (MAT (in,2,3) * MAT (out,2,2));
  687. }
  688. return TRUE;
  689. }
  690. /*
  691. * Compute inverse of a no-rotation 2d transformation matrix.
  692. *
  693. * @mat pointer to a CoglMatrix structure. The matrix inverse will be
  694. * stored in the CoglMatrix::inv attribute.
  695. *
  696. * Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
  697. *
  698. * Calculates the inverse matrix by applying the inverse scaling and
  699. * translation to the identity matrix.
  700. */
  701. static gboolean
  702. invert_matrix_2d_no_rotation (CoglMatrix *matrix)
  703. {
  704. const float *in = (float *)matrix;
  705. float *out = matrix->inv;
  706. if (MAT (in, 0, 0) == 0 || MAT (in, 1, 1) == 0)
  707. return FALSE;
  708. memcpy (out, identity, 16 * sizeof (float));
  709. MAT (out, 0, 0) = 1.0f / MAT (in, 0, 0);
  710. MAT (out, 1, 1) = 1.0f / MAT (in, 1, 1);
  711. if (matrix->flags & MAT_FLAG_TRANSLATION)
  712. {
  713. MAT (out, 0, 3) = - (MAT (in, 0, 3) * MAT (out, 0, 0));
  714. MAT (out, 1, 3) = - (MAT (in, 1, 3) * MAT (out, 1, 1));
  715. }
  716. return TRUE;
  717. }
  718. #if 0
  719. /* broken */
  720. static gboolean
  721. invert_matrix_perspective (CoglMatrix *matrix)
  722. {
  723. const float *in = matrix;
  724. float *out = matrix->inv;
  725. if (MAT (in,2,3) == 0)
  726. return FALSE;
  727. memcpy( out, identity, 16 * sizeof(float) );
  728. MAT (out, 0, 0) = 1.0f / MAT (in, 0, 0);
  729. MAT (out, 1, 1) = 1.0f / MAT (in, 1, 1);
  730. MAT (out, 0, 3) = MAT (in, 0, 2);
  731. MAT (out, 1, 3) = MAT (in, 1, 2);
  732. MAT (out,2,2) = 0;
  733. MAT (out,2,3) = -1;
  734. MAT (out,3,2) = 1.0f / MAT (in,2,3);
  735. MAT (out,3,3) = MAT (in,2,2) * MAT (out,3,2);
  736. return TRUE;
  737. }
  738. #endif
  739. /*
  740. * Matrix inversion function pointer type.
  741. */
  742. typedef gboolean (*inv_mat_func)(CoglMatrix *matrix);
  743. /*
  744. * Table of the matrix inversion functions according to the matrix type.
  745. */
  746. static inv_mat_func inv_mat_tab[7] = {
  747. invert_matrix_general,
  748. invert_matrix_identity,
  749. invert_matrix_3d_no_rotation,
  750. #if 0
  751. /* Don't use this function for now - it fails when the projection matrix
  752. * is premultiplied by a translation (ala Chromium's tilesort SPU).
  753. */
  754. invert_matrix_perspective,
  755. #else
  756. invert_matrix_general,
  757. #endif
  758. invert_matrix_3d, /* lazy! */
  759. invert_matrix_2d_no_rotation,
  760. invert_matrix_3d
  761. };
  762. #define ZERO(x) (1<<x)
  763. #define ONE(x) (1<<(x+16))
  764. #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
  765. #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
  766. #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
  767. ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
  768. ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
  769. ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
  770. #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
  771. ZERO(1) | ZERO(9) | \
  772. ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
  773. ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
  774. #define MASK_2D ( ZERO(8) | \
  775. ZERO(9) | \
  776. ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
  777. ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
  778. #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
  779. ZERO(1) | ZERO(9) | \
  780. ZERO(2) | ZERO(6) | \
  781. ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
  782. #define MASK_3D ( \
  783. \
  784. \
  785. ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
  786. #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
  787. ZERO(1) | ZERO(13) |\
  788. ZERO(2) | ZERO(6) | \
  789. ZERO(3) | ZERO(7) | ZERO(15) )
  790. #define SQ(x) ((x)*(x))
  791. /*
  792. * Determine type and flags from scratch.
  793. *
  794. * This is expensive enough to only want to do it once.
  795. */
  796. static void
  797. analyse_from_scratch (CoglMatrix *matrix)
  798. {
  799. const float *m = (float *)matrix;
  800. unsigned int mask = 0;
  801. unsigned int i;
  802. for (i = 0 ; i < 16 ; i++)
  803. {
  804. if (m[i] == 0.0) mask |= (1<<i);
  805. }
  806. if (m[0] == 1.0f) mask |= (1<<16);
  807. if (m[5] == 1.0f) mask |= (1<<21);
  808. if (m[10] == 1.0f) mask |= (1<<26);
  809. if (m[15] == 1.0f) mask |= (1<<31);
  810. matrix->flags &= ~MAT_FLAGS_GEOMETRY;
  811. /* Check for translation - no-one really cares
  812. */
  813. if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
  814. matrix->flags |= MAT_FLAG_TRANSLATION;
  815. /* Do the real work
  816. */
  817. if (mask == (unsigned int) MASK_IDENTITY)
  818. matrix->type = COGL_MATRIX_TYPE_IDENTITY;
  819. else if ((mask & MASK_2D_NO_ROT) == (unsigned int) MASK_2D_NO_ROT)
  820. {
  821. matrix->type = COGL_MATRIX_TYPE_2D_NO_ROT;
  822. if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
  823. matrix->flags |= MAT_FLAG_GENERAL_SCALE;
  824. }
  825. else if ((mask & MASK_2D) == (unsigned int) MASK_2D)
  826. {
  827. float mm = DOT2 (m, m);
  828. float m4m4 = DOT2 (m+4,m+4);
  829. float mm4 = DOT2 (m,m+4);
  830. matrix->type = COGL_MATRIX_TYPE_2D;
  831. /* Check for scale */
  832. if (SQ (mm-1) > SQ (1e-6) ||
  833. SQ (m4m4-1) > SQ (1e-6))
  834. matrix->flags |= MAT_FLAG_GENERAL_SCALE;
  835. /* Check for rotation */
  836. if (SQ (mm4) > SQ (1e-6))
  837. matrix->flags |= MAT_FLAG_GENERAL_3D;
  838. else
  839. matrix->flags |= MAT_FLAG_ROTATION;
  840. }
  841. else if ((mask & MASK_3D_NO_ROT) == (unsigned int) MASK_3D_NO_ROT)
  842. {
  843. matrix->type = COGL_MATRIX_TYPE_3D_NO_ROT;
  844. /* Check for scale */
  845. if (SQ (m[0]-m[5]) < SQ (1e-6) &&
  846. SQ (m[0]-m[10]) < SQ (1e-6))
  847. {
  848. if (SQ (m[0]-1.0) > SQ (1e-6))
  849. matrix->flags |= MAT_FLAG_UNIFORM_SCALE;
  850. }
  851. else
  852. matrix->flags |= MAT_FLAG_GENERAL_SCALE;
  853. }
  854. else if ((mask & MASK_3D) == (unsigned int) MASK_3D)
  855. {
  856. float c1 = DOT3 (m,m);
  857. float c2 = DOT3 (m+4,m+4);
  858. float c3 = DOT3 (m+8,m+8);
  859. float d1 = DOT3 (m, m+4);
  860. float cp[3];
  861. matrix->type = COGL_MATRIX_TYPE_3D;
  862. /* Check for scale */
  863. if (SQ (c1-c2) < SQ (1e-6) && SQ (c1-c3) < SQ (1e-6))
  864. {
  865. if (SQ (c1-1.0) > SQ (1e-6))
  866. matrix->flags |= MAT_FLAG_UNIFORM_SCALE;
  867. /* else no scale at all */
  868. }
  869. else
  870. matrix->flags |= MAT_FLAG_GENERAL_SCALE;
  871. /* Check for rotation */
  872. if (SQ (d1) < SQ (1e-6))
  873. {
  874. CROSS3 ( cp, m, m+4);
  875. SUB_3V ( cp, cp, (m+8));
  876. if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
  877. matrix->flags |= MAT_FLAG_ROTATION;
  878. else
  879. matrix->flags |= MAT_FLAG_GENERAL_3D;
  880. }
  881. else
  882. matrix->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
  883. }
  884. else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0f)
  885. {
  886. matrix->type = COGL_MATRIX_TYPE_PERSPECTIVE;
  887. matrix->flags |= MAT_FLAG_GENERAL;
  888. }
  889. else
  890. {
  891. matrix->type = COGL_MATRIX_TYPE_GENERAL;
  892. matrix->flags |= MAT_FLAG_GENERAL;
  893. }
  894. }
  895. /*
  896. * Analyze a matrix given that its flags are accurate.
  897. *
  898. * This is the more common operation, hopefully.
  899. */
  900. static void
  901. analyse_from_flags (CoglMatrix *matrix)
  902. {
  903. const float *m = (float *)matrix;
  904. if (TEST_MAT_FLAGS(matrix, 0))
  905. matrix->type = COGL_MATRIX_TYPE_IDENTITY;
  906. else if (TEST_MAT_FLAGS(matrix, (MAT_FLAG_TRANSLATION |
  907. MAT_FLAG_UNIFORM_SCALE |
  908. MAT_FLAG_GENERAL_SCALE)))
  909. {
  910. if ( m[10] == 1.0f && m[14] == 0.0f )
  911. matrix->type = COGL_MATRIX_TYPE_2D_NO_ROT;
  912. else
  913. matrix->type = COGL_MATRIX_TYPE_3D_NO_ROT;
  914. }
  915. else if (TEST_MAT_FLAGS (matrix, MAT_FLAGS_3D))
  916. {
  917. if ( m[ 8]==0.0f
  918. && m[ 9]==0.0f
  919. && m[2]==0.0f && m[6]==0.0f && m[10]==1.0f && m[14]==0.0f)
  920. {
  921. matrix->type = COGL_MATRIX_TYPE_2D;
  922. }
  923. else
  924. matrix->type = COGL_MATRIX_TYPE_3D;
  925. }
  926. else if ( m[4]==0.0f && m[12]==0.0f
  927. && m[1]==0.0f && m[13]==0.0f
  928. && m[2]==0.0f && m[6]==0.0f
  929. && m[3]==0.0f && m[7]==0.0f && m[11]==-1.0f && m[15]==0.0f)
  930. {
  931. matrix->type = COGL_MATRIX_TYPE_PERSPECTIVE;
  932. }
  933. else
  934. matrix->type = COGL_MATRIX_TYPE_GENERAL;
  935. }
  936. /*
  937. * Analyze and update the type and flags of a matrix.
  938. *
  939. * If the matrix type is dirty then calls either analyse_from_scratch() or
  940. * analyse_from_flags() to determine its type, according to whether the flags
  941. * are dirty or not, respectively. If the matrix has an inverse and it's dirty
  942. * then calls matrix_invert(). Finally clears the dirty flags.
  943. */
  944. static void
  945. _cogl_matrix_update_type_and_flags (CoglMatrix *matrix)
  946. {
  947. if (matrix->flags & MAT_DIRTY_TYPE)
  948. {
  949. if (matrix->flags & MAT_DIRTY_FLAGS)
  950. analyse_from_scratch (matrix);
  951. else
  952. analyse_from_flags (matrix);
  953. }
  954. matrix->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
  955. }
  956. /*
  957. * Compute inverse of a transformation matrix.
  958. *
  959. * @mat pointer to a CoglMatrix structure. The matrix inverse will be
  960. * stored in the CoglMatrix::inv attribute.
  961. *
  962. * Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
  963. *
  964. * Calls the matrix inversion function in inv_mat_tab corresponding to the
  965. * given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag,
  966. * and copies the identity matrix into CoglMatrix::inv.
  967. */
  968. static gboolean
  969. _cogl_matrix_update_inverse (CoglMatrix *matrix)
  970. {
  971. if (matrix->flags & MAT_DIRTY_FLAGS ||
  972. matrix->flags & MAT_DIRTY_INVERSE)
  973. {
  974. _cogl_matrix_update_type_and_flags (matrix);
  975. if (inv_mat_tab[matrix->type](matrix))
  976. matrix->flags &= ~MAT_FLAG_SINGULAR;
  977. else
  978. {
  979. matrix->flags |= MAT_FLAG_SINGULAR;
  980. memcpy (matrix->inv, identity, 16 * sizeof (float));
  981. }
  982. matrix->flags &= ~MAT_DIRTY_INVERSE;
  983. }
  984. if (matrix->flags & MAT_FLAG_SINGULAR)
  985. return FALSE;
  986. else
  987. return TRUE;
  988. }
  989. gboolean
  990. cogl_matrix_get_inverse (const CoglMatrix *matrix, CoglMatrix *inverse)
  991. {
  992. if (_cogl_matrix_update_inverse ((CoglMatrix *)matrix))
  993. {
  994. cogl_matrix_init_from_array (inverse, matrix->inv);
  995. return TRUE;
  996. }
  997. else
  998. {
  999. cogl_matrix_init_identity (inverse);
  1000. return FALSE;
  1001. }
  1002. }
  1003. /*
  1004. * Generate a 4x4 transformation matrix from glRotate parameters, and
  1005. * post-multiply the input matrix by it.
  1006. *
  1007. * \author
  1008. * This function was contributed by Erich Boleyn (erich@uruk.org).
  1009. * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
  1010. */
  1011. static void
  1012. _cogl_matrix_rotate (CoglMatrix *matrix,
  1013. float angle,
  1014. float x,
  1015. float y,
  1016. float z)
  1017. {
  1018. float xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
  1019. float m[16];
  1020. gboolean optimized;
  1021. s = sinf (angle * DEG2RAD);
  1022. c = cosf (angle * DEG2RAD);
  1023. memcpy (m, identity, 16 * sizeof (float));
  1024. optimized = FALSE;
  1025. #define M(row,col) m[col*4+row]
  1026. if (x == 0.0f)
  1027. {
  1028. if (y == 0.0f)
  1029. {
  1030. if (z != 0.0f)
  1031. {
  1032. optimized = TRUE;
  1033. /* rotate only around z-axis */
  1034. M (0,0) = c;
  1035. M (1,1) = c;
  1036. if (z < 0.0f)
  1037. {
  1038. M (0,1) = s;
  1039. M (1,0) = -s;
  1040. }
  1041. else
  1042. {
  1043. M (0,1) = -s;
  1044. M (1,0) = s;
  1045. }
  1046. }
  1047. }
  1048. else if (z == 0.0f)
  1049. {
  1050. optimized = TRUE;
  1051. /* rotate only around y-axis */
  1052. M (0,0) = c;
  1053. M (2,2) = c;
  1054. if (y < 0.0f)
  1055. {
  1056. M (0,2) = -s;
  1057. M (2,0) = s;
  1058. }
  1059. else
  1060. {
  1061. M (0,2) = s;
  1062. M (2,0) = -s;
  1063. }
  1064. }
  1065. }
  1066. else if (y == 0.0f)
  1067. {
  1068. if (z == 0.0f)
  1069. {
  1070. optimized = TRUE;
  1071. /* rotate only around x-axis */
  1072. M (1,1) = c;
  1073. M (2,2) = c;
  1074. if (x < 0.0f)
  1075. {
  1076. M (1,2) = s;
  1077. M (2,1) = -s;
  1078. }
  1079. else
  1080. {
  1081. M (1,2) = -s;
  1082. M (2,1) = s;
  1083. }
  1084. }
  1085. }
  1086. if (!optimized)
  1087. {
  1088. const float mag = sqrtf (x * x + y * y + z * z);
  1089. if (mag <= 1.0e-4)
  1090. {
  1091. /* no rotation, leave mat as-is */
  1092. return;
  1093. }
  1094. x /= mag;
  1095. y /= mag;
  1096. z /= mag;
  1097. /*
  1098. * Arbitrary axis rotation matrix.
  1099. *
  1100. * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
  1101. * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
  1102. * (which is about the X-axis), and the two composite transforms
  1103. * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
  1104. * from the arbitrary axis to the X-axis then back. They are
  1105. * all elementary rotations.
  1106. *
  1107. * Rz' is a rotation about the Z-axis, to bring the axis vector
  1108. * into the x-z plane. Then Ry' is applied, rotating about the
  1109. * Y-axis to bring the axis vector parallel with the X-axis. The
  1110. * rotation about the X-axis is then performed. Ry and Rz are
  1111. * simply the respective inverse transforms to bring the arbitrary
  1112. * axis back to it's original orientation. The first transforms
  1113. * Rz' and Ry' are considered inverses, since the data from the
  1114. * arbitrary axis gives you info on how to get to it, not how
  1115. * to get away from it, and an inverse must be applied.
  1116. *
  1117. * The basic calculation used is to recognize that the arbitrary
  1118. * axis vector (x, y, z), since it is of unit length, actually
  1119. * represents the sines and cosines of the angles to rotate the
  1120. * X-axis to the same orientation, with theta being the angle about
  1121. * Z and phi the angle about Y (in the order described above)
  1122. * as follows:
  1123. *
  1124. * cos ( theta ) = x / sqrt ( 1 - z^2 )
  1125. * sin ( theta ) = y / sqrt ( 1 - z^2 )
  1126. *
  1127. * cos ( phi ) = sqrt ( 1 - z^2 )
  1128. * sin ( phi ) = z
  1129. *
  1130. * Note that cos ( phi ) can further be inserted to the above
  1131. * formulas:
  1132. *
  1133. * cos ( theta ) = x / cos ( phi )
  1134. * sin ( theta ) = y / sin ( phi )
  1135. *
  1136. * ...etc. Because of those relations and the standard trigonometric
  1137. * relations, it is pssible to reduce the transforms down to what
  1138. * is used below. It may be that any primary axis chosen will give the
  1139. * same results (modulo a sign convention) using thie method.
  1140. *
  1141. * Particularly nice is to notice that all divisions that might
  1142. * have caused trouble when parallel to certain planes or
  1143. * axis go away with care paid to reducing the expressions.
  1144. * After checking, it does perform correctly under all cases, since
  1145. * in all the cases of division where the denominator would have
  1146. * been zero, the numerator would have been zero as well, giving
  1147. * the expected result.
  1148. */
  1149. xx = x * x;
  1150. yy = y * y;
  1151. zz = z * z;
  1152. xy = x * y;
  1153. yz = y * z;
  1154. zx = z * x;
  1155. xs = x * s;
  1156. ys = y * s;
  1157. zs = z * s;
  1158. one_c = 1.0f - c;
  1159. /* We already hold the identity-matrix so we can skip some statements */
  1160. M (0,0) = (one_c * xx) + c;
  1161. M (0,1) = (one_c * xy) - zs;
  1162. M (0,2) = (one_c * zx) + ys;
  1163. /* M (0,3) = 0.0f; */
  1164. M (1,0) = (one_c * xy) + zs;
  1165. M (1,1) = (one_c * yy) + c;
  1166. M (1,2) = (one_c * yz) - xs;
  1167. /* M (1,3) = 0.0f; */
  1168. M (2,0) = (one_c * zx) - ys;
  1169. M (2,1) = (one_c * yz) + xs;
  1170. M (2,2) = (one_c * zz) + c;
  1171. /* M (2,3) = 0.0f; */
  1172. /*
  1173. M (3,0) = 0.0f;
  1174. M (3,1) = 0.0f;
  1175. M (3,2) = 0.0f;
  1176. M (3,3) = 1.0f;
  1177. */
  1178. }
  1179. #undef M
  1180. matrix_multiply_array_with_flags (matrix, m, MAT_FLAG_ROTATION);
  1181. }
  1182. void
  1183. cogl_matrix_rotate (CoglMatrix *matrix,
  1184. float angle,
  1185. float x,
  1186. float y,
  1187. float z)
  1188. {
  1189. _cogl_matrix_rotate (matrix, angle, x, y, z);
  1190. _COGL_MATRIX_DEBUG_PRINT (matrix);
  1191. }
  1192. /*
  1193. * Apply a perspective projection matrix.
  1194. *
  1195. * Creates the projection matrix and multiplies it with matrix, marking the
  1196. * MAT_FLAG_PERSPECTIVE flag.
  1197. */
  1198. static void
  1199. _cogl_matrix_frustum (CoglMatrix *matrix,
  1200. float left,
  1201. float right,
  1202. float bottom,
  1203. float top,
  1204. float nearval,
  1205. float farval)
  1206. {
  1207. float x, y, a, b, c, d;
  1208. float m[16];
  1209. x = (2.0f * nearval) / (right - left);
  1210. y = (2.0f * nearval) / (top - bottom);
  1211. a = (right + left) / (right - left);
  1212. b = (top + bottom) / (top - bottom);
  1213. c = -(farval + nearval) / ( farval - nearval);
  1214. d = -(2.0f * farval * nearval) / (farval - nearval); /* error? */
  1215. #define M(row,col) m[col*4+row]
  1216. M (0,0) = x; M (0,1) = 0.0f; M (0,2) = a; M (0,3) = 0.0f;
  1217. M (1,0) = 0.0f; M (1,1) = y; M (1,2) = b; M (1,3) = 0.0f;
  1218. M (2,0) = 0.0f; M (2,1) = 0.0f; M (2,2) = c; M (2,3) = d;
  1219. M (3,0) = 0.0f; M (3,1) = 0.0f; M (3,2) = -1.0f; M (3,3) = 0.0f;
  1220. #undef M
  1221. matrix_multiply_array_with_flags (matrix, m, MAT_FLAG_PERSPECTIVE);
  1222. }
  1223. void
  1224. cogl_matrix_frustum (CoglMatrix *matrix,
  1225. float left,
  1226. float right,
  1227. float bottom,
  1228. float top,
  1229. float z_near,
  1230. float z_far)
  1231. {
  1232. _cogl_matrix_frustum (matrix, left, right, bottom, top, z_near, z_far);
  1233. _COGL_MATRIX_DEBUG_PRINT (matrix);
  1234. }
  1235. void
  1236. cogl_matrix_perspective (CoglMatrix *matrix,
  1237. float fov_y,
  1238. float aspect,
  1239. float z_near,
  1240. float z_far)
  1241. {
  1242. float ymax = z_near * tan (fov_y * G_PI / 360.0);
  1243. cogl_matrix_frustum (matrix,
  1244. -ymax * aspect, /* left */
  1245. ymax * aspect, /* right */
  1246. -ymax, /* bottom */
  1247. ymax, /* top */
  1248. z_near,
  1249. z_far);
  1250. _COGL_MATRIX_DEBUG_PRINT (matrix);
  1251. }
  1252. /*
  1253. * Apply an orthographic projection matrix.
  1254. *
  1255. * Creates the projection matrix and multiplies it with matrix, marking the
  1256. * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
  1257. */
  1258. static void
  1259. _cogl_matrix_orthographic (CoglMatrix *matrix,
  1260. float x_1,
  1261. float y_1,
  1262. float x_2,
  1263. float y_2,
  1264. float nearval,
  1265. float farval)
  1266. {
  1267. float m[16];
  1268. #define M(row, col) m[col * 4 + row]
  1269. M (0,0) = 2.0f / (x_2 - x_1);
  1270. M (0,1) = 0.0f;
  1271. M (0,2) = 0.0f;
  1272. M (0,3) = -(x_2 + x_1) / (x_2 - x_1);
  1273. M (1,0) = 0.0f;
  1274. M (1,1) = 2.0f / (y_1 - y_2);
  1275. M (1,2) = 0.0f;
  1276. M (1,3) = -(y_1 + y_2) / (y_1 - y_2);
  1277. M (2,0) = 0.0f;
  1278. M (2,1) = 0.0f;
  1279. M (2,2) = -2.0f / (farval - nearval);
  1280. M (2,3) = -(farval + nearval) / (farval - nearval);
  1281. M (3,0) = 0.0f;
  1282. M (3,1) = 0.0f;
  1283. M (3,2) = 0.0f;
  1284. M (3,3) = 1.0f;
  1285. #undef M
  1286. matrix_multiply_array_with_flags (matrix, m,
  1287. (MAT_FLAG_GENERAL_SCALE |
  1288. MAT_FLAG_TRANSLATION));
  1289. }
  1290. void
  1291. cogl_matrix_ortho (CoglMatrix *matrix,
  1292. float left,
  1293. float right,
  1294. float bottom,
  1295. float top,
  1296. float near,
  1297. float far)
  1298. {
  1299. _cogl_matrix_orthographic (matrix, left, top, right, bottom, near, far);
  1300. _COGL_MATRIX_DEBUG_PRINT (matrix);
  1301. }
  1302. void
  1303. cogl_matrix_orthographic (CoglMatrix *matrix,
  1304. float x_1,
  1305. float y_1,
  1306. float x_2,
  1307. float y_2,
  1308. float near,
  1309. float far)
  1310. {
  1311. _cogl_matrix_orthographic (matrix, x_1, y_1, x_2, y_2, near, far);
  1312. _COGL_MATRIX_DEBUG_PRINT (matrix);
  1313. }
  1314. /*
  1315. * Multiply a matrix with a general scaling matrix.
  1316. *
  1317. * Multiplies in-place the elements of matrix by the scale factors. Checks if
  1318. * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE
  1319. * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and
  1320. * MAT_DIRTY_INVERSE dirty flags.
  1321. */
  1322. static void
  1323. _cogl_matrix_scale (CoglMatrix *matrix, float x, float y, float z)
  1324. {
  1325. float *m = (float *)matrix;
  1326. m[0] *= x; m[4] *= y; m[8] *= z;
  1327. m[1] *= x; m[5] *= y; m[9] *= z;
  1328. m[2] *= x; m[6] *= y; m[10] *= z;
  1329. m[3] *= x; m[7] *= y; m[11] *= z;
  1330. if (fabsf (x - y) < 1e-8 && fabsf (x - z) < 1e-8)
  1331. matrix->flags |= MAT_FLAG_UNIFORM_SCALE;
  1332. else
  1333. matrix->flags |= MAT_FLAG_GENERAL_SCALE;
  1334. matrix->flags |= (MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
  1335. }
  1336. void
  1337. cogl_matrix_scale (CoglMatrix *matrix,
  1338. float sx,
  1339. float sy,
  1340. float sz)
  1341. {
  1342. _cogl_matrix_scale (matrix, sx, sy, sz);
  1343. _COGL_MATRIX_DEBUG_PRINT (matrix);
  1344. }
  1345. /*
  1346. * Multiply a matrix with a translation matrix.
  1347. *
  1348. * Adds the translation coordinates to the elements of matrix in-place. Marks
  1349. * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE
  1350. * dirty flags.
  1351. */
  1352. static void
  1353. _cogl_matrix_translate (CoglMatrix *matrix, float x, float y, float z)
  1354. {
  1355. float *m = (float *)matrix;
  1356. m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
  1357. m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
  1358. m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
  1359. m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
  1360. matrix->flags |= (MAT_FLAG_TRANSLATION |
  1361. MAT_DIRTY_TYPE |
  1362. MAT_DIRTY_INVERSE);
  1363. }
  1364. void
  1365. cogl_matrix_translate (CoglMatrix *matrix,
  1366. float x,
  1367. float y,
  1368. float z)
  1369. {
  1370. _cogl_matrix_translate (matrix, x, y, z);
  1371. _COGL_MATRIX_DEBUG_PRINT (matrix);
  1372. }
  1373. #if 0
  1374. /*
  1375. * Set matrix to do viewport and depthrange mapping.
  1376. * Transforms Normalized Device Coords to window/Z values.
  1377. */
  1378. static void
  1379. _cogl_matrix_viewport (CoglMatrix *matrix,
  1380. float x, float y,
  1381. float width, float height,
  1382. float zNear, float zFar, float depthMax)
  1383. {
  1384. float *m = (float *)matrix;
  1385. m[MAT_SX] = width / 2.0f;
  1386. m[MAT_TX] = m[MAT_SX] + x;
  1387. m[MAT_SY] = height / 2.0f;
  1388. m[MAT_TY] = m[MAT_SY] + y;
  1389. m[MAT_SZ] = depthMax * ((zFar - zNear) / 2.0f);
  1390. m[MAT_TZ] = depthMax * ((zFar - zNear) / 2.0f + zNear);
  1391. matrix->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION;
  1392. matrix->type = COGL_MATRIX_TYPE_3D_NO_ROT;
  1393. }
  1394. #endif
  1395. /*
  1396. * Set a matrix to the identity matrix.
  1397. *
  1398. * @mat matrix.
  1399. *
  1400. * Copies ::identity into \p CoglMatrix::m, and into CoglMatrix::inv if
  1401. * not NULL. Sets the matrix type to identity, resets the flags. It
  1402. * doesn't initialize the inverse matrix, it just marks it dirty.
  1403. */
  1404. static void
  1405. _cogl_matrix_init_identity (CoglMatrix *matrix)
  1406. {
  1407. memcpy (matrix, identity, 16 * sizeof (float));
  1408. matrix->type = COGL_MATRIX_TYPE_IDENTITY;
  1409. matrix->flags = MAT_DIRTY_INVERSE;
  1410. }
  1411. void
  1412. cogl_matrix_init_identity (CoglMatrix *matrix)
  1413. {
  1414. _cogl_matrix_init_identity (matrix);
  1415. _COGL_MATRIX_DEBUG_PRINT (matrix);
  1416. }
  1417. #if 0
  1418. /*
  1419. * Test if the given matrix preserves vector lengths.
  1420. */
  1421. static gboolean
  1422. _cogl_matrix_is_length_preserving (const CoglMatrix *m)
  1423. {
  1424. return TEST_MAT_FLAGS (m, MAT_FLAGS_LENGTH_PRESERVING);
  1425. }
  1426. /*
  1427. * Test if the given matrix does any rotation.
  1428. * (or perhaps if the upper-left 3x3 is non-identity)
  1429. */
  1430. static gboolean
  1431. _cogl_matrix_has_rotation (const CoglMatrix *matrix)
  1432. {
  1433. if (matrix->flags & (MAT_FLAG_GENERAL |
  1434. MAT_FLAG_ROTATION |
  1435. MAT_FLAG_GENERAL_3D |
  1436. MAT_FLAG_PERSPECTIVE))
  1437. return TRUE;
  1438. else
  1439. return FALSE;
  1440. }
  1441. static gboolean
  1442. _cogl_matrix_is_general_scale (const CoglMatrix *matrix)
  1443. {
  1444. return (matrix->flags & MAT_FLAG_GENERAL_SCALE) ? TRUE : FALSE;
  1445. }
  1446. static gboolean
  1447. _cogl_matrix_is_dirty (const CoglMatrix *matrix)
  1448. {
  1449. return (matrix->flags & MAT_DIRTY_ALL) ? TRUE : FALSE;
  1450. }
  1451. #endif
  1452. /*
  1453. * Loads a matrix array into CoglMatrix.
  1454. *
  1455. * @m matrix array.
  1456. * @mat matrix.
  1457. *
  1458. * Copies \p m into CoglMatrix::m and marks the MAT_FLAG_GENERAL and
  1459. * MAT_DIRTY_ALL
  1460. * flags.
  1461. */
  1462. static void
  1463. _cogl_matrix_init_from_array (CoglMatrix *matrix, const float *array)
  1464. {
  1465. memcpy (matrix, array, 16 * sizeof (float));
  1466. matrix->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL);
  1467. }
  1468. void
  1469. cogl_matrix_init_from_array (CoglMatrix *matrix, const float *array)
  1470. {
  1471. _cogl_matrix_init_from_array (matrix