2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 * -# 4x4 transformation matrices are stored in memory in column major order.
32 * -# Points/vertices are to be thought of as column vectors.
33 * -# Transformation of a point p by a matrix M is: p' = M * p
46 * Names of the corresponding GLmatrixtype values.
48 static const char *types
[] = {
62 static GLfloat Identity
[16] = {
71 /**********************************************************************/
72 /** \name Matrix multiplication */
75 #define A(row,col) a[(col<<2)+row]
76 #define B(row,col) b[(col<<2)+row]
77 #define P(row,col) product[(col<<2)+row]
80 * Perform a full 4x4 matrix multiplication.
84 * \param product will receive the product of \p a and \p b.
86 * \warning Is assumed that \p product != \p b. \p product == \p a is allowed.
88 * \note KW: 4*16 = 64 multiplications
90 * \author This \c matmul was contributed by Thomas Malik
92 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
95 for (i
= 0; i
< 4; i
++) {
96 const GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
97 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
98 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
99 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
100 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
105 * Multiply two matrices known to occupy only the top three rows, such
106 * as typical model matrices, and orthogonal matrices.
110 * \param product will receive the product of \p a and \p b.
112 static void matmul34( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
115 for (i
= 0; i
< 3; i
++) {
116 const GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
117 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0);
118 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1);
119 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2);
120 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
;
133 * Multiply a matrix by an array of floats with known properties.
135 * \param mat pointer to a GLmatrix structure containing the left multiplication
136 * matrix, and that will receive the product result.
137 * \param m right multiplication matrix array.
138 * \param flags flags of the matrix \p m.
140 * Joins both flags and marks the type and inverse as dirty. Calls matmul34()
141 * if both matrices are 3D, or matmul4() otherwise.
143 static void matrix_multf( GLmatrix
*mat
, const GLfloat
*m
, GLuint flags
)
145 mat
->flags
|= (flags
| MAT_DIRTY_TYPE
| MAT_DIRTY_INVERSE
);
147 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
148 matmul34( mat
->m
, mat
->m
, m
);
150 matmul4( mat
->m
, mat
->m
, m
);
154 * Matrix multiplication.
156 * \param dest destination matrix.
157 * \param a left matrix.
158 * \param b right matrix.
160 * Joins both flags and marks the type and inverse as dirty. Calls matmul34()
161 * if both matrices are 3D, or matmul4() otherwise.
164 _math_matrix_mul_matrix( GLmatrix
*dest
, const GLmatrix
*a
, const GLmatrix
*b
)
166 dest
->flags
= (a
->flags
|
171 if (TEST_MAT_FLAGS(dest
, MAT_FLAGS_3D
))
172 matmul34( dest
->m
, a
->m
, b
->m
);
174 matmul4( dest
->m
, a
->m
, b
->m
);
178 * Matrix multiplication.
180 * \param dest left and destination matrix.
181 * \param m right matrix array.
183 * Marks the matrix flags with general flag, and type and inverse dirty flags.
184 * Calls matmul4() for the multiplication.
187 _math_matrix_mul_floats( GLmatrix
*dest
, const GLfloat
*m
)
189 dest
->flags
|= (MAT_FLAG_GENERAL
|
194 matmul4( dest
->m
, dest
->m
, m
);
200 /**********************************************************************/
201 /** \name Matrix output */
205 * Print a matrix array.
207 * \param m matrix array.
209 * Called by _math_matrix_print() to print a matrix or its inverse.
211 static void print_matrix_floats( const GLfloat m
[16] )
215 _mesa_debug(NULL
,"\t%f %f %f %f\n", m
[i
], m
[4+i
], m
[8+i
], m
[12+i
] );
220 * Dumps the contents of a GLmatrix structure.
222 * \param m pointer to the GLmatrix structure.
225 _math_matrix_print( const GLmatrix
*m
)
227 _mesa_debug(NULL
, "Matrix type: %s, flags: %x\n", types
[m
->type
], m
->flags
);
228 print_matrix_floats(m
->m
);
229 _mesa_debug(NULL
, "Inverse: \n");
232 print_matrix_floats(m
->inv
);
233 matmul4(prod
, m
->m
, m
->inv
);
234 _mesa_debug(NULL
, "Mat * Inverse:\n");
235 print_matrix_floats(prod
);
238 _mesa_debug(NULL
, " - not available\n");
246 * References an element of 4x4 matrix.
248 * \param m matrix array.
249 * \param c column of the desired element.
250 * \param r row of the desired element.
252 * \return value of the desired element.
254 * Calculate the linear storage index of the element and references it.
256 #define MAT(m,r,c) (m)[(c)*4+(r)]
259 /**********************************************************************/
260 /** \name Matrix inversion */
264 * Swaps the values of two floating pointer variables.
266 * Used by invert_matrix_general() to swap the row pointers.
268 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
271 * Compute inverse of 4x4 transformation matrix.
273 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
274 * stored in the GLmatrix::inv attribute.
276 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
279 * Code contributed by Jacques Leroy jle@star.be
281 * Calculates the inverse matrix by performing the gaussian matrix reduction
282 * with partial pivoting followed by back/substitution with the loops manually
285 static GLboolean
invert_matrix_general( GLmatrix
*mat
)
287 const GLfloat
*m
= mat
->m
;
288 GLfloat
*out
= mat
->inv
;
290 GLfloat m0
, m1
, m2
, m3
, s
;
291 GLfloat
*r0
, *r1
, *r2
, *r3
;
293 r0
= wtmp
[0], r1
= wtmp
[1], r2
= wtmp
[2], r3
= wtmp
[3];
295 r0
[0] = MAT(m
,0,0), r0
[1] = MAT(m
,0,1),
296 r0
[2] = MAT(m
,0,2), r0
[3] = MAT(m
,0,3),
297 r0
[4] = 1.0, r0
[5] = r0
[6] = r0
[7] = 0.0,
299 r1
[0] = MAT(m
,1,0), r1
[1] = MAT(m
,1,1),
300 r1
[2] = MAT(m
,1,2), r1
[3] = MAT(m
,1,3),
301 r1
[5] = 1.0, r1
[4] = r1
[6] = r1
[7] = 0.0,
303 r2
[0] = MAT(m
,2,0), r2
[1] = MAT(m
,2,1),
304 r2
[2] = MAT(m
,2,2), r2
[3] = MAT(m
,2,3),
305 r2
[6] = 1.0, r2
[4] = r2
[5] = r2
[7] = 0.0,
307 r3
[0] = MAT(m
,3,0), r3
[1] = MAT(m
,3,1),
308 r3
[2] = MAT(m
,3,2), r3
[3] = MAT(m
,3,3),
309 r3
[7] = 1.0, r3
[4] = r3
[5] = r3
[6] = 0.0;
311 /* choose pivot - or die */
312 if (fabs(r3
[0])>fabs(r2
[0])) SWAP_ROWS(r3
, r2
);
313 if (fabs(r2
[0])>fabs(r1
[0])) SWAP_ROWS(r2
, r1
);
314 if (fabs(r1
[0])>fabs(r0
[0])) SWAP_ROWS(r1
, r0
);
315 if (0.0 == r0
[0]) return GL_FALSE
;
317 /* eliminate first variable */
318 m1
= r1
[0]/r0
[0]; m2
= r2
[0]/r0
[0]; m3
= r3
[0]/r0
[0];
319 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
320 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
321 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
323 if (s
!= 0.0) { r1
[4] -= m1
* s
; r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
325 if (s
!= 0.0) { r1
[5] -= m1
* s
; r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
327 if (s
!= 0.0) { r1
[6] -= m1
* s
; r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
329 if (s
!= 0.0) { r1
[7] -= m1
* s
; r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
331 /* choose pivot - or die */
332 if (fabs(r3
[1])>fabs(r2
[1])) SWAP_ROWS(r3
, r2
);
333 if (fabs(r2
[1])>fabs(r1
[1])) SWAP_ROWS(r2
, r1
);
334 if (0.0 == r1
[1]) return GL_FALSE
;
336 /* eliminate second variable */
337 m2
= r2
[1]/r1
[1]; m3
= r3
[1]/r1
[1];
338 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
339 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
340 s
= r1
[4]; if (0.0 != s
) { r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
341 s
= r1
[5]; if (0.0 != s
) { r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
342 s
= r1
[6]; if (0.0 != s
) { r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
343 s
= r1
[7]; if (0.0 != s
) { r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
345 /* choose pivot - or die */
346 if (fabs(r3
[2])>fabs(r2
[2])) SWAP_ROWS(r3
, r2
);
347 if (0.0 == r2
[2]) return GL_FALSE
;
349 /* eliminate third variable */
351 r3
[3] -= m3
* r2
[3], r3
[4] -= m3
* r2
[4],
352 r3
[5] -= m3
* r2
[5], r3
[6] -= m3
* r2
[6],
356 if (0.0 == r3
[3]) return GL_FALSE
;
358 s
= 1.0F
/r3
[3]; /* now back substitute row 3 */
359 r3
[4] *= s
; r3
[5] *= s
; r3
[6] *= s
; r3
[7] *= s
;
361 m2
= r2
[3]; /* now back substitute row 2 */
363 r2
[4] = s
* (r2
[4] - r3
[4] * m2
), r2
[5] = s
* (r2
[5] - r3
[5] * m2
),
364 r2
[6] = s
* (r2
[6] - r3
[6] * m2
), r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
366 r1
[4] -= r3
[4] * m1
, r1
[5] -= r3
[5] * m1
,
367 r1
[6] -= r3
[6] * m1
, r1
[7] -= r3
[7] * m1
;
369 r0
[4] -= r3
[4] * m0
, r0
[5] -= r3
[5] * m0
,
370 r0
[6] -= r3
[6] * m0
, r0
[7] -= r3
[7] * m0
;
372 m1
= r1
[2]; /* now back substitute row 1 */
374 r1
[4] = s
* (r1
[4] - r2
[4] * m1
), r1
[5] = s
* (r1
[5] - r2
[5] * m1
),
375 r1
[6] = s
* (r1
[6] - r2
[6] * m1
), r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
377 r0
[4] -= r2
[4] * m0
, r0
[5] -= r2
[5] * m0
,
378 r0
[6] -= r2
[6] * m0
, r0
[7] -= r2
[7] * m0
;
380 m0
= r0
[1]; /* now back substitute row 0 */
382 r0
[4] = s
* (r0
[4] - r1
[4] * m0
), r0
[5] = s
* (r0
[5] - r1
[5] * m0
),
383 r0
[6] = s
* (r0
[6] - r1
[6] * m0
), r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
385 MAT(out
,0,0) = r0
[4]; MAT(out
,0,1) = r0
[5],
386 MAT(out
,0,2) = r0
[6]; MAT(out
,0,3) = r0
[7],
387 MAT(out
,1,0) = r1
[4]; MAT(out
,1,1) = r1
[5],
388 MAT(out
,1,2) = r1
[6]; MAT(out
,1,3) = r1
[7],
389 MAT(out
,2,0) = r2
[4]; MAT(out
,2,1) = r2
[5],
390 MAT(out
,2,2) = r2
[6]; MAT(out
,2,3) = r2
[7],
391 MAT(out
,3,0) = r3
[4]; MAT(out
,3,1) = r3
[5],
392 MAT(out
,3,2) = r3
[6]; MAT(out
,3,3) = r3
[7];
399 * Compute inverse of a general 3d transformation matrix.
401 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
402 * stored in the GLmatrix::inv attribute.
404 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
406 * \author Adapted from graphics gems II.
408 * Calculates the inverse of the upper left by first calculating its
409 * determinant and multiplying it to the symmetric adjust matrix of each
410 * element. Finally deals with the translation part by transforming the
411 * original translation vector using by the calculated submatrix inverse.
413 static GLboolean
invert_matrix_3d_general( GLmatrix
*mat
)
415 const GLfloat
*in
= mat
->m
;
416 GLfloat
*out
= mat
->inv
;
420 /* Calculate the determinant of upper left 3x3 submatrix and
421 * determine if the matrix is singular.
424 t
= MAT(in
,0,0) * MAT(in
,1,1) * MAT(in
,2,2);
425 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
427 t
= MAT(in
,1,0) * MAT(in
,2,1) * MAT(in
,0,2);
428 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
430 t
= MAT(in
,2,0) * MAT(in
,0,1) * MAT(in
,1,2);
431 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
433 t
= -MAT(in
,2,0) * MAT(in
,1,1) * MAT(in
,0,2);
434 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
436 t
= -MAT(in
,1,0) * MAT(in
,0,1) * MAT(in
,2,2);
437 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
439 t
= -MAT(in
,0,0) * MAT(in
,2,1) * MAT(in
,1,2);
440 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
448 MAT(out
,0,0) = ( (MAT(in
,1,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,1,2) )*det
);
449 MAT(out
,0,1) = (- (MAT(in
,0,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,0,2) )*det
);
450 MAT(out
,0,2) = ( (MAT(in
,0,1)*MAT(in
,1,2) - MAT(in
,1,1)*MAT(in
,0,2) )*det
);
451 MAT(out
,1,0) = (- (MAT(in
,1,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,1,2) )*det
);
452 MAT(out
,1,1) = ( (MAT(in
,0,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,0,2) )*det
);
453 MAT(out
,1,2) = (- (MAT(in
,0,0)*MAT(in
,1,2) - MAT(in
,1,0)*MAT(in
,0,2) )*det
);
454 MAT(out
,2,0) = ( (MAT(in
,1,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,1,1) )*det
);
455 MAT(out
,2,1) = (- (MAT(in
,0,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,0,1) )*det
);
456 MAT(out
,2,2) = ( (MAT(in
,0,0)*MAT(in
,1,1) - MAT(in
,1,0)*MAT(in
,0,1) )*det
);
458 /* Do the translation part */
459 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
460 MAT(in
,1,3) * MAT(out
,0,1) +
461 MAT(in
,2,3) * MAT(out
,0,2) );
462 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
463 MAT(in
,1,3) * MAT(out
,1,1) +
464 MAT(in
,2,3) * MAT(out
,1,2) );
465 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
466 MAT(in
,1,3) * MAT(out
,2,1) +
467 MAT(in
,2,3) * MAT(out
,2,2) );
473 * Compute inverse of a 3d transformation matrix.
475 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
476 * stored in the GLmatrix::inv attribute.
478 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
480 * If the matrix is not an angle preserving matrix then calls
481 * invert_matrix_3d_general for the actual calculation. Otherwise calculates
482 * the inverse matrix analyzing and inverting each of the scaling, rotation and
485 static GLboolean
invert_matrix_3d( GLmatrix
*mat
)
487 const GLfloat
*in
= mat
->m
;
488 GLfloat
*out
= mat
->inv
;
490 if (!TEST_MAT_FLAGS(mat
, MAT_FLAGS_ANGLE_PRESERVING
)) {
491 return invert_matrix_3d_general( mat
);
494 if (mat
->flags
& MAT_FLAG_UNIFORM_SCALE
) {
495 GLfloat scale
= (MAT(in
,0,0) * MAT(in
,0,0) +
496 MAT(in
,0,1) * MAT(in
,0,1) +
497 MAT(in
,0,2) * MAT(in
,0,2));
502 scale
= 1.0F
/ scale
;
504 /* Transpose and scale the 3 by 3 upper-left submatrix. */
505 MAT(out
,0,0) = scale
* MAT(in
,0,0);
506 MAT(out
,1,0) = scale
* MAT(in
,0,1);
507 MAT(out
,2,0) = scale
* MAT(in
,0,2);
508 MAT(out
,0,1) = scale
* MAT(in
,1,0);
509 MAT(out
,1,1) = scale
* MAT(in
,1,1);
510 MAT(out
,2,1) = scale
* MAT(in
,1,2);
511 MAT(out
,0,2) = scale
* MAT(in
,2,0);
512 MAT(out
,1,2) = scale
* MAT(in
,2,1);
513 MAT(out
,2,2) = scale
* MAT(in
,2,2);
515 else if (mat
->flags
& MAT_FLAG_ROTATION
) {
516 /* Transpose the 3 by 3 upper-left submatrix. */
517 MAT(out
,0,0) = MAT(in
,0,0);
518 MAT(out
,1,0) = MAT(in
,0,1);
519 MAT(out
,2,0) = MAT(in
,0,2);
520 MAT(out
,0,1) = MAT(in
,1,0);
521 MAT(out
,1,1) = MAT(in
,1,1);
522 MAT(out
,2,1) = MAT(in
,1,2);
523 MAT(out
,0,2) = MAT(in
,2,0);
524 MAT(out
,1,2) = MAT(in
,2,1);
525 MAT(out
,2,2) = MAT(in
,2,2);
528 /* pure translation */
529 MEMCPY( out
, Identity
, sizeof(Identity
) );
530 MAT(out
,0,3) = - MAT(in
,0,3);
531 MAT(out
,1,3) = - MAT(in
,1,3);
532 MAT(out
,2,3) = - MAT(in
,2,3);
536 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
537 /* Do the translation part */
538 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
539 MAT(in
,1,3) * MAT(out
,0,1) +
540 MAT(in
,2,3) * MAT(out
,0,2) );
541 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
542 MAT(in
,1,3) * MAT(out
,1,1) +
543 MAT(in
,2,3) * MAT(out
,1,2) );
544 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
545 MAT(in
,1,3) * MAT(out
,2,1) +
546 MAT(in
,2,3) * MAT(out
,2,2) );
549 MAT(out
,0,3) = MAT(out
,1,3) = MAT(out
,2,3) = 0.0;
556 * Compute inverse of an identity transformation matrix.
558 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
559 * stored in the GLmatrix::inv attribute.
561 * \return always GL_TRUE.
563 * Simply copies Identity into GLmatrix::inv.
565 static GLboolean
invert_matrix_identity( GLmatrix
*mat
)
567 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
572 * Compute inverse of a no-rotation 3d transformation matrix.
574 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
575 * stored in the GLmatrix::inv attribute.
577 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
581 static GLboolean
invert_matrix_3d_no_rot( GLmatrix
*mat
)
583 const GLfloat
*in
= mat
->m
;
584 GLfloat
*out
= mat
->inv
;
586 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0 || MAT(in
,2,2) == 0 )
589 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
590 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
591 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
592 MAT(out
,2,2) = 1.0F
/ MAT(in
,2,2);
594 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
595 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
596 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
597 MAT(out
,2,3) = - (MAT(in
,2,3) * MAT(out
,2,2));
604 * Compute inverse of a no-rotation 2d transformation matrix.
606 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
607 * stored in the GLmatrix::inv attribute.
609 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
611 * Calculates the inverse matrix by applying the inverse scaling and
612 * translation to the identity matrix.
614 static GLboolean
invert_matrix_2d_no_rot( GLmatrix
*mat
)
616 const GLfloat
*in
= mat
->m
;
617 GLfloat
*out
= mat
->inv
;
619 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0)
622 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
623 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
624 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
626 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
627 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
628 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
636 static GLboolean
invert_matrix_perspective( GLmatrix
*mat
)
638 const GLfloat
*in
= mat
->m
;
639 GLfloat
*out
= mat
->inv
;
641 if (MAT(in
,2,3) == 0)
644 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
646 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
647 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
649 MAT(out
,0,3) = MAT(in
,0,2);
650 MAT(out
,1,3) = MAT(in
,1,2);
655 MAT(out
,3,2) = 1.0F
/ MAT(in
,2,3);
656 MAT(out
,3,3) = MAT(in
,2,2) * MAT(out
,3,2);
663 * Matrix inversion function pointer type.
665 typedef GLboolean (*inv_mat_func
)( GLmatrix
*mat
);
668 * Table of the matrix inversion functions according to the matrix type.
670 static inv_mat_func inv_mat_tab
[7] = {
671 invert_matrix_general
,
672 invert_matrix_identity
,
673 invert_matrix_3d_no_rot
,
675 /* Don't use this function for now - it fails when the projection matrix
676 * is premultiplied by a translation (ala Chromium's tilesort SPU).
678 invert_matrix_perspective
,
680 invert_matrix_general
,
682 invert_matrix_3d
, /* lazy! */
683 invert_matrix_2d_no_rot
,
688 * Compute inverse of a transformation matrix.
690 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
691 * stored in the GLmatrix::inv attribute.
693 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
695 * Calls the matrix inversion function in inv_mat_tab corresponding to the
696 * given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag,
697 * and copies the identity matrix into GLmatrix::inv.
699 static GLboolean
matrix_invert( GLmatrix
*mat
)
701 if (inv_mat_tab
[mat
->type
](mat
)) {
702 mat
->flags
&= ~MAT_FLAG_SINGULAR
;
705 mat
->flags
|= MAT_FLAG_SINGULAR
;
706 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
714 /**********************************************************************/
715 /** \name Matrix generation */
719 * Generate a 4x4 transformation matrix from glRotate parameters, and
720 * post-multiply the input matrix by it.
723 * This function was contributed by Erich Boleyn (erich@uruk.org).
724 * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
727 _math_matrix_rotate( GLmatrix
*mat
,
728 GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
730 GLfloat xx
, yy
, zz
, xy
, yz
, zx
, xs
, ys
, zs
, one_c
, s
, c
;
734 s
= (GLfloat
) sin( angle
* DEG2RAD
);
735 c
= (GLfloat
) cos( angle
* DEG2RAD
);
737 MEMCPY(m
, Identity
, sizeof(GLfloat
)*16);
738 optimized
= GL_FALSE
;
740 #define M(row,col) m[col*4+row]
746 /* rotate only around z-axis */
759 else if (z
== 0.0F
) {
761 /* rotate only around y-axis */
774 else if (y
== 0.0F
) {
777 /* rotate only around x-axis */
792 const GLfloat mag
= SQRTF(x
* x
+ y
* y
+ z
* z
);
795 /* no rotation, leave mat as-is */
805 * Arbitrary axis rotation matrix.
807 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
808 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
809 * (which is about the X-axis), and the two composite transforms
810 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
811 * from the arbitrary axis to the X-axis then back. They are
812 * all elementary rotations.
814 * Rz' is a rotation about the Z-axis, to bring the axis vector
815 * into the x-z plane. Then Ry' is applied, rotating about the
816 * Y-axis to bring the axis vector parallel with the X-axis. The
817 * rotation about the X-axis is then performed. Ry and Rz are
818 * simply the respective inverse transforms to bring the arbitrary
819 * axis back to it's original orientation. The first transforms
820 * Rz' and Ry' are considered inverses, since the data from the
821 * arbitrary axis gives you info on how to get to it, not how
822 * to get away from it, and an inverse must be applied.
824 * The basic calculation used is to recognize that the arbitrary
825 * axis vector (x, y, z), since it is of unit length, actually
826 * represents the sines and cosines of the angles to rotate the
827 * X-axis to the same orientation, with theta being the angle about
828 * Z and phi the angle about Y (in the order described above)
831 * cos ( theta ) = x / sqrt ( 1 - z^2 )
832 * sin ( theta ) = y / sqrt ( 1 - z^2 )
834 * cos ( phi ) = sqrt ( 1 - z^2 )
837 * Note that cos ( phi ) can further be inserted to the above
840 * cos ( theta ) = x / cos ( phi )
841 * sin ( theta ) = y / sin ( phi )
843 * ...etc. Because of those relations and the standard trigonometric
844 * relations, it is pssible to reduce the transforms down to what
845 * is used below. It may be that any primary axis chosen will give the
846 * same results (modulo a sign convention) using thie method.
848 * Particularly nice is to notice that all divisions that might
849 * have caused trouble when parallel to certain planes or
850 * axis go away with care paid to reducing the expressions.
851 * After checking, it does perform correctly under all cases, since
852 * in all the cases of division where the denominator would have
853 * been zero, the numerator would have been zero as well, giving
854 * the expected result.
868 /* We already hold the identity-matrix so we can skip some statements */
869 M(0,0) = (one_c
* xx
) + c
;
870 M(0,1) = (one_c
* xy
) - zs
;
871 M(0,2) = (one_c
* zx
) + ys
;
874 M(1,0) = (one_c
* xy
) + zs
;
875 M(1,1) = (one_c
* yy
) + c
;
876 M(1,2) = (one_c
* yz
) - xs
;
879 M(2,0) = (one_c
* zx
) - ys
;
880 M(2,1) = (one_c
* yz
) + xs
;
881 M(2,2) = (one_c
* zz
) + c
;
893 matrix_multf( mat
, m
, MAT_FLAG_ROTATION
);
897 * Apply a perspective projection matrix.
899 * \param mat matrix to apply the projection.
900 * \param left left clipping plane coordinate.
901 * \param right right clipping plane coordinate.
902 * \param bottom bottom clipping plane coordinate.
903 * \param top top clipping plane coordinate.
904 * \param nearval distance to the near clipping plane.
905 * \param farval distance to the far clipping plane.
907 * Creates the projection matrix and multiplies it with \p mat, marking the
908 * MAT_FLAG_PERSPECTIVE flag.
911 _math_matrix_frustum( GLmatrix
*mat
,
912 GLfloat left
, GLfloat right
,
913 GLfloat bottom
, GLfloat top
,
914 GLfloat nearval
, GLfloat farval
)
916 GLfloat x
, y
, a
, b
, c
, d
;
919 x
= (2.0F
*nearval
) / (right
-left
);
920 y
= (2.0F
*nearval
) / (top
-bottom
);
921 a
= (right
+left
) / (right
-left
);
922 b
= (top
+bottom
) / (top
-bottom
);
923 c
= -(farval
+nearval
) / ( farval
-nearval
);
924 d
= -(2.0F
*farval
*nearval
) / (farval
-nearval
); /* error? */
926 #define M(row,col) m[col*4+row]
927 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
928 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
929 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
930 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
933 matrix_multf( mat
, m
, MAT_FLAG_PERSPECTIVE
);
937 * Apply an orthographic projection matrix.
939 * \param mat matrix to apply the projection.
940 * \param left left clipping plane coordinate.
941 * \param right right clipping plane coordinate.
942 * \param bottom bottom clipping plane coordinate.
943 * \param top top clipping plane coordinate.
944 * \param nearval distance to the near clipping plane.
945 * \param farval distance to the far clipping plane.
947 * Creates the projection matrix and multiplies it with \p mat, marking the
948 * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
951 _math_matrix_ortho( GLmatrix
*mat
,
952 GLfloat left
, GLfloat right
,
953 GLfloat bottom
, GLfloat top
,
954 GLfloat nearval
, GLfloat farval
)
958 #define M(row,col) m[col*4+row]
959 M(0,0) = 2.0F
/ (right
-left
);
962 M(0,3) = -(right
+left
) / (right
-left
);
965 M(1,1) = 2.0F
/ (top
-bottom
);
967 M(1,3) = -(top
+bottom
) / (top
-bottom
);
971 M(2,2) = -2.0F
/ (farval
-nearval
);
972 M(2,3) = -(farval
+nearval
) / (farval
-nearval
);
980 matrix_multf( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
984 * Multiply a matrix with a general scaling matrix.
987 * \param x x axis scale factor.
988 * \param y y axis scale factor.
989 * \param z z axis scale factor.
991 * Multiplies in-place the elements of \p mat by the scale factors. Checks if
992 * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE
993 * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and
994 * MAT_DIRTY_INVERSE dirty flags.
997 _math_matrix_scale( GLmatrix
*mat
, GLfloat x
, GLfloat y
, GLfloat z
)
1000 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
1001 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
1002 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
1003 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
1005 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
1006 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1008 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1010 mat
->flags
|= (MAT_DIRTY_TYPE
|
1015 * Multiply a matrix with a translation matrix.
1017 * \param mat matrix.
1018 * \param x translation vector x coordinate.
1019 * \param y translation vector y coordinate.
1020 * \param z translation vector z coordinate.
1022 * Adds the translation coordinates to the elements of \p mat in-place. Marks
1023 * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE
1027 _math_matrix_translate( GLmatrix
*mat
, GLfloat x
, GLfloat y
, GLfloat z
)
1029 GLfloat
*m
= mat
->m
;
1030 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1031 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1032 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1033 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1035 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
1041 * Set a matrix to the identity matrix.
1043 * \param mat matrix.
1045 * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL.
1046 * Sets the matrix type to identity, and clear the dirty flags.
1049 _math_matrix_set_identity( GLmatrix
*mat
)
1051 MEMCPY( mat
->m
, Identity
, 16*sizeof(GLfloat
) );
1054 MEMCPY( mat
->inv
, Identity
, 16*sizeof(GLfloat
) );
1056 mat
->type
= MATRIX_IDENTITY
;
1057 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
1065 /**********************************************************************/
1066 /** \name Matrix analysis */
1069 #define ZERO(x) (1<<x)
1070 #define ONE(x) (1<<(x+16))
1072 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
1073 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
1075 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
1076 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
1077 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1078 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1080 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
1081 ZERO(1) | ZERO(9) | \
1082 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1083 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1085 #define MASK_2D ( ZERO(8) | \
1087 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1088 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1091 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
1092 ZERO(1) | ZERO(9) | \
1093 ZERO(2) | ZERO(6) | \
1094 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1099 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1102 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
1103 ZERO(1) | ZERO(13) |\
1104 ZERO(2) | ZERO(6) | \
1105 ZERO(3) | ZERO(7) | ZERO(15) )
1107 #define SQ(x) ((x)*(x))
1110 * Determine type and flags from scratch.
1112 * \param mat matrix.
1114 * This is expensive enough to only want to do it once.
1116 static void analyse_from_scratch( GLmatrix
*mat
)
1118 const GLfloat
*m
= mat
->m
;
1122 for (i
= 0 ; i
< 16 ; i
++) {
1123 if (m
[i
] == 0.0) mask
|= (1<<i
);
1126 if (m
[0] == 1.0F
) mask
|= (1<<16);
1127 if (m
[5] == 1.0F
) mask
|= (1<<21);
1128 if (m
[10] == 1.0F
) mask
|= (1<<26);
1129 if (m
[15] == 1.0F
) mask
|= (1<<31);
1131 mat
->flags
&= ~MAT_FLAGS_GEOMETRY
;
1133 /* Check for translation - no-one really cares
1135 if ((mask
& MASK_NO_TRX
) != MASK_NO_TRX
)
1136 mat
->flags
|= MAT_FLAG_TRANSLATION
;
1140 if (mask
== (GLuint
) MASK_IDENTITY
) {
1141 mat
->type
= MATRIX_IDENTITY
;
1143 else if ((mask
& MASK_2D_NO_ROT
) == (GLuint
) MASK_2D_NO_ROT
) {
1144 mat
->type
= MATRIX_2D_NO_ROT
;
1146 if ((mask
& MASK_NO_2D_SCALE
) != MASK_NO_2D_SCALE
)
1147 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1149 else if ((mask
& MASK_2D
) == (GLuint
) MASK_2D
) {
1150 GLfloat mm
= DOT2(m
, m
);
1151 GLfloat m4m4
= DOT2(m
+4,m
+4);
1152 GLfloat mm4
= DOT2(m
,m
+4);
1154 mat
->type
= MATRIX_2D
;
1156 /* Check for scale */
1157 if (SQ(mm
-1) > SQ(1e-6) ||
1158 SQ(m4m4
-1) > SQ(1e-6))
1159 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1161 /* Check for rotation */
1162 if (SQ(mm4
) > SQ(1e-6))
1163 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
1165 mat
->flags
|= MAT_FLAG_ROTATION
;
1168 else if ((mask
& MASK_3D_NO_ROT
) == (GLuint
) MASK_3D_NO_ROT
) {
1169 mat
->type
= MATRIX_3D_NO_ROT
;
1171 /* Check for scale */
1172 if (SQ(m
[0]-m
[5]) < SQ(1e-6) &&
1173 SQ(m
[0]-m
[10]) < SQ(1e-6)) {
1174 if (SQ(m
[0]-1.0) > SQ(1e-6)) {
1175 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1179 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1182 else if ((mask
& MASK_3D
) == (GLuint
) MASK_3D
) {
1183 GLfloat c1
= DOT3(m
,m
);
1184 GLfloat c2
= DOT3(m
+4,m
+4);
1185 GLfloat c3
= DOT3(m
+8,m
+8);
1186 GLfloat d1
= DOT3(m
, m
+4);
1189 mat
->type
= MATRIX_3D
;
1191 /* Check for scale */
1192 if (SQ(c1
-c2
) < SQ(1e-6) && SQ(c1
-c3
) < SQ(1e-6)) {
1193 if (SQ(c1
-1.0) > SQ(1e-6))
1194 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1195 /* else no scale at all */
1198 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1201 /* Check for rotation */
1202 if (SQ(d1
) < SQ(1e-6)) {
1203 CROSS3( cp
, m
, m
+4 );
1204 SUB_3V( cp
, cp
, (m
+8) );
1205 if (LEN_SQUARED_3FV(cp
) < SQ(1e-6))
1206 mat
->flags
|= MAT_FLAG_ROTATION
;
1208 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
1211 mat
->flags
|= MAT_FLAG_GENERAL_3D
; /* shear, etc */
1214 else if ((mask
& MASK_PERSPECTIVE
) == MASK_PERSPECTIVE
&& m
[11]==-1.0F
) {
1215 mat
->type
= MATRIX_PERSPECTIVE
;
1216 mat
->flags
|= MAT_FLAG_GENERAL
;
1219 mat
->type
= MATRIX_GENERAL
;
1220 mat
->flags
|= MAT_FLAG_GENERAL
;
1225 * Analyze a matrix given that its flags are accurate.
1227 * This is the more common operation, hopefully.
1229 static void analyse_from_flags( GLmatrix
*mat
)
1231 const GLfloat
*m
= mat
->m
;
1233 if (TEST_MAT_FLAGS(mat
, 0)) {
1234 mat
->type
= MATRIX_IDENTITY
;
1236 else if (TEST_MAT_FLAGS(mat
, (MAT_FLAG_TRANSLATION
|
1237 MAT_FLAG_UNIFORM_SCALE
|
1238 MAT_FLAG_GENERAL_SCALE
))) {
1239 if ( m
[10]==1.0F
&& m
[14]==0.0F
) {
1240 mat
->type
= MATRIX_2D_NO_ROT
;
1243 mat
->type
= MATRIX_3D_NO_ROT
;
1246 else if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
)) {
1249 && m
[2]==0.0F
&& m
[6]==0.0F
&& m
[10]==1.0F
&& m
[14]==0.0F
) {
1250 mat
->type
= MATRIX_2D
;
1253 mat
->type
= MATRIX_3D
;
1256 else if ( m
[4]==0.0F
&& m
[12]==0.0F
1257 && m
[1]==0.0F
&& m
[13]==0.0F
1258 && m
[2]==0.0F
&& m
[6]==0.0F
1259 && m
[3]==0.0F
&& m
[7]==0.0F
&& m
[11]==-1.0F
&& m
[15]==0.0F
) {
1260 mat
->type
= MATRIX_PERSPECTIVE
;
1263 mat
->type
= MATRIX_GENERAL
;
1268 * Analyze and update a matrix.
1270 * \param mat matrix.
1272 * If the matrix type is dirty then calls either analyse_from_scratch() or
1273 * analyse_from_flags() to determine its type, according to whether the flags
1274 * are dirty or not, respectively. If the matrix has an inverse and it's dirty
1275 * then calls matrix_invert(). Finally clears the dirty flags.
1278 _math_matrix_analyse( GLmatrix
*mat
)
1280 if (mat
->flags
& MAT_DIRTY_TYPE
) {
1281 if (mat
->flags
& MAT_DIRTY_FLAGS
)
1282 analyse_from_scratch( mat
);
1284 analyse_from_flags( mat
);
1287 if (mat
->inv
&& (mat
->flags
& MAT_DIRTY_INVERSE
)) {
1288 matrix_invert( mat
);
1291 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
1299 /**********************************************************************/
1300 /** \name Matrix setup */
1306 * \param to destination matrix.
1307 * \param from source matrix.
1309 * Copies all fields in GLmatrix, creating an inverse array if necessary.
1312 _math_matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
1314 MEMCPY( to
->m
, from
->m
, sizeof(Identity
) );
1315 to
->flags
= from
->flags
;
1316 to
->type
= from
->type
;
1319 if (from
->inv
== 0) {
1320 matrix_invert( to
);
1323 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
1329 * Loads a matrix array into GLmatrix.
1331 * \param m matrix array.
1332 * \param mat matrix.
1334 * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
1338 _math_matrix_loadf( GLmatrix
*mat
, const GLfloat
*m
)
1340 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
1341 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY
);
1345 * Matrix constructor.
1349 * Initialize the GLmatrix fields.
1352 _math_matrix_ctr( GLmatrix
*m
)
1354 m
->m
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
1356 MEMCPY( m
->m
, Identity
, sizeof(Identity
) );
1358 m
->type
= MATRIX_IDENTITY
;
1363 * Matrix destructor.
1367 * Frees the data in a GLmatrix.
1370 _math_matrix_dtr( GLmatrix
*m
)
1377 ALIGN_FREE( m
->inv
);
1383 * Allocate a matrix inverse.
1387 * Allocates the matrix inverse, GLmatrix::inv, and sets it to Identity.
1390 _math_matrix_alloc_inv( GLmatrix
*m
)
1393 m
->inv
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
1395 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
1402 /**********************************************************************/
1403 /** \name Matrix transpose */
1407 * Transpose a GLfloat matrix.
1409 * \param to destination array.
1410 * \param from source array.
1413 _math_transposef( GLfloat to
[16], const GLfloat from
[16] )
1434 * Transpose a GLdouble matrix.
1436 * \param to destination array.
1437 * \param from source array.
1440 _math_transposed( GLdouble to
[16], const GLdouble from
[16] )
1461 * Transpose a GLdouble matrix and convert to GLfloat.
1463 * \param to destination array.
1464 * \param from source array.
1467 _math_transposefd( GLfloat to
[16], const GLdouble from
[16] )
1469 to
[0] = (GLfloat
) from
[0];
1470 to
[1] = (GLfloat
) from
[4];
1471 to
[2] = (GLfloat
) from
[8];
1472 to
[3] = (GLfloat
) from
[12];
1473 to
[4] = (GLfloat
) from
[1];
1474 to
[5] = (GLfloat
) from
[5];
1475 to
[6] = (GLfloat
) from
[9];
1476 to
[7] = (GLfloat
) from
[13];
1477 to
[8] = (GLfloat
) from
[2];
1478 to
[9] = (GLfloat
) from
[6];
1479 to
[10] = (GLfloat
) from
[10];
1480 to
[11] = (GLfloat
) from
[14];
1481 to
[12] = (GLfloat
) from
[3];
1482 to
[13] = (GLfloat
) from
[7];
1483 to
[14] = (GLfloat
) from
[11];
1484 to
[15] = (GLfloat
) from
[15];