2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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
|
193 matmul4( dest
->m
, dest
->m
, m
);
199 /**********************************************************************/
200 /** \name Matrix output */
204 * Print a matrix array.
206 * \param m matrix array.
208 * Called by _math_matrix_print() to print a matrix or its inverse.
210 static void print_matrix_floats( const GLfloat m
[16] )
214 _mesa_debug(NULL
,"\t%f %f %f %f\n", m
[i
], m
[4+i
], m
[8+i
], m
[12+i
] );
219 * Dumps the contents of a GLmatrix structure.
221 * \param m pointer to the GLmatrix structure.
224 _math_matrix_print( const GLmatrix
*m
)
226 _mesa_debug(NULL
, "Matrix type: %s, flags: %x\n", types
[m
->type
], m
->flags
);
227 print_matrix_floats(m
->m
);
228 _mesa_debug(NULL
, "Inverse: \n");
231 print_matrix_floats(m
->inv
);
232 matmul4(prod
, m
->m
, m
->inv
);
233 _mesa_debug(NULL
, "Mat * Inverse:\n");
234 print_matrix_floats(prod
);
237 _mesa_debug(NULL
, " - not available\n");
245 * References an element of 4x4 matrix.
247 * \param m matrix array.
248 * \param c column of the desired element.
249 * \param r row of the desired element.
251 * \return value of the desired element.
253 * Calculate the linear storage index of the element and references it.
255 #define MAT(m,r,c) (m)[(c)*4+(r)]
258 /**********************************************************************/
259 /** \name Matrix inversion */
263 * Swaps the values of two floating pointer variables.
265 * Used by invert_matrix_general() to swap the row pointers.
267 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
270 * Compute inverse of 4x4 transformation matrix.
272 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
273 * stored in the GLmatrix::inv attribute.
275 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
278 * Code contributed by Jacques Leroy jle@star.be
280 * Calculates the inverse matrix by performing the gaussian matrix reduction
281 * with partial pivoting followed by back/substitution with the loops manually
284 static GLboolean
invert_matrix_general( GLmatrix
*mat
)
286 const GLfloat
*m
= mat
->m
;
287 GLfloat
*out
= mat
->inv
;
289 GLfloat m0
, m1
, m2
, m3
, s
;
290 GLfloat
*r0
, *r1
, *r2
, *r3
;
292 r0
= wtmp
[0], r1
= wtmp
[1], r2
= wtmp
[2], r3
= wtmp
[3];
294 r0
[0] = MAT(m
,0,0), r0
[1] = MAT(m
,0,1),
295 r0
[2] = MAT(m
,0,2), r0
[3] = MAT(m
,0,3),
296 r0
[4] = 1.0, r0
[5] = r0
[6] = r0
[7] = 0.0,
298 r1
[0] = MAT(m
,1,0), r1
[1] = MAT(m
,1,1),
299 r1
[2] = MAT(m
,1,2), r1
[3] = MAT(m
,1,3),
300 r1
[5] = 1.0, r1
[4] = r1
[6] = r1
[7] = 0.0,
302 r2
[0] = MAT(m
,2,0), r2
[1] = MAT(m
,2,1),
303 r2
[2] = MAT(m
,2,2), r2
[3] = MAT(m
,2,3),
304 r2
[6] = 1.0, r2
[4] = r2
[5] = r2
[7] = 0.0,
306 r3
[0] = MAT(m
,3,0), r3
[1] = MAT(m
,3,1),
307 r3
[2] = MAT(m
,3,2), r3
[3] = MAT(m
,3,3),
308 r3
[7] = 1.0, r3
[4] = r3
[5] = r3
[6] = 0.0;
310 /* choose pivot - or die */
311 if (fabs(r3
[0])>fabs(r2
[0])) SWAP_ROWS(r3
, r2
);
312 if (fabs(r2
[0])>fabs(r1
[0])) SWAP_ROWS(r2
, r1
);
313 if (fabs(r1
[0])>fabs(r0
[0])) SWAP_ROWS(r1
, r0
);
314 if (0.0 == r0
[0]) return GL_FALSE
;
316 /* eliminate first variable */
317 m1
= r1
[0]/r0
[0]; m2
= r2
[0]/r0
[0]; m3
= r3
[0]/r0
[0];
318 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
319 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
320 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
322 if (s
!= 0.0) { r1
[4] -= m1
* s
; r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
324 if (s
!= 0.0) { r1
[5] -= m1
* s
; r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
326 if (s
!= 0.0) { r1
[6] -= m1
* s
; r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
328 if (s
!= 0.0) { r1
[7] -= m1
* s
; r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
330 /* choose pivot - or die */
331 if (fabs(r3
[1])>fabs(r2
[1])) SWAP_ROWS(r3
, r2
);
332 if (fabs(r2
[1])>fabs(r1
[1])) SWAP_ROWS(r2
, r1
);
333 if (0.0 == r1
[1]) return GL_FALSE
;
335 /* eliminate second variable */
336 m2
= r2
[1]/r1
[1]; m3
= r3
[1]/r1
[1];
337 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
338 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
339 s
= r1
[4]; if (0.0 != s
) { r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
340 s
= r1
[5]; if (0.0 != s
) { r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
341 s
= r1
[6]; if (0.0 != s
) { r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
342 s
= r1
[7]; if (0.0 != s
) { r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
344 /* choose pivot - or die */
345 if (fabs(r3
[2])>fabs(r2
[2])) SWAP_ROWS(r3
, r2
);
346 if (0.0 == r2
[2]) return GL_FALSE
;
348 /* eliminate third variable */
350 r3
[3] -= m3
* r2
[3], r3
[4] -= m3
* r2
[4],
351 r3
[5] -= m3
* r2
[5], r3
[6] -= m3
* r2
[6],
355 if (0.0 == r3
[3]) return GL_FALSE
;
357 s
= 1.0F
/r3
[3]; /* now back substitute row 3 */
358 r3
[4] *= s
; r3
[5] *= s
; r3
[6] *= s
; r3
[7] *= s
;
360 m2
= r2
[3]; /* now back substitute row 2 */
362 r2
[4] = s
* (r2
[4] - r3
[4] * m2
), r2
[5] = s
* (r2
[5] - r3
[5] * m2
),
363 r2
[6] = s
* (r2
[6] - r3
[6] * m2
), r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
365 r1
[4] -= r3
[4] * m1
, r1
[5] -= r3
[5] * m1
,
366 r1
[6] -= r3
[6] * m1
, r1
[7] -= r3
[7] * m1
;
368 r0
[4] -= r3
[4] * m0
, r0
[5] -= r3
[5] * m0
,
369 r0
[6] -= r3
[6] * m0
, r0
[7] -= r3
[7] * m0
;
371 m1
= r1
[2]; /* now back substitute row 1 */
373 r1
[4] = s
* (r1
[4] - r2
[4] * m1
), r1
[5] = s
* (r1
[5] - r2
[5] * m1
),
374 r1
[6] = s
* (r1
[6] - r2
[6] * m1
), r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
376 r0
[4] -= r2
[4] * m0
, r0
[5] -= r2
[5] * m0
,
377 r0
[6] -= r2
[6] * m0
, r0
[7] -= r2
[7] * m0
;
379 m0
= r0
[1]; /* now back substitute row 0 */
381 r0
[4] = s
* (r0
[4] - r1
[4] * m0
), r0
[5] = s
* (r0
[5] - r1
[5] * m0
),
382 r0
[6] = s
* (r0
[6] - r1
[6] * m0
), r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
384 MAT(out
,0,0) = r0
[4]; MAT(out
,0,1) = r0
[5],
385 MAT(out
,0,2) = r0
[6]; MAT(out
,0,3) = r0
[7],
386 MAT(out
,1,0) = r1
[4]; MAT(out
,1,1) = r1
[5],
387 MAT(out
,1,2) = r1
[6]; MAT(out
,1,3) = r1
[7],
388 MAT(out
,2,0) = r2
[4]; MAT(out
,2,1) = r2
[5],
389 MAT(out
,2,2) = r2
[6]; MAT(out
,2,3) = r2
[7],
390 MAT(out
,3,0) = r3
[4]; MAT(out
,3,1) = r3
[5],
391 MAT(out
,3,2) = r3
[6]; MAT(out
,3,3) = r3
[7];
398 * Compute inverse of a general 3d transformation matrix.
400 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
401 * stored in the GLmatrix::inv attribute.
403 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
405 * \author Adapted from graphics gems II.
407 * Calculates the inverse of the upper left by first calculating its
408 * determinant and multiplying it to the symmetric adjust matrix of each
409 * element. Finally deals with the translation part by transforming the
410 * original translation vector using by the calculated submatrix inverse.
412 static GLboolean
invert_matrix_3d_general( GLmatrix
*mat
)
414 const GLfloat
*in
= mat
->m
;
415 GLfloat
*out
= mat
->inv
;
419 /* Calculate the determinant of upper left 3x3 submatrix and
420 * determine if the matrix is singular.
423 t
= MAT(in
,0,0) * MAT(in
,1,1) * MAT(in
,2,2);
424 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
426 t
= MAT(in
,1,0) * MAT(in
,2,1) * MAT(in
,0,2);
427 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
429 t
= MAT(in
,2,0) * MAT(in
,0,1) * MAT(in
,1,2);
430 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
432 t
= -MAT(in
,2,0) * MAT(in
,1,1) * MAT(in
,0,2);
433 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
435 t
= -MAT(in
,1,0) * MAT(in
,0,1) * MAT(in
,2,2);
436 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
438 t
= -MAT(in
,0,0) * MAT(in
,2,1) * MAT(in
,1,2);
439 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
447 MAT(out
,0,0) = ( (MAT(in
,1,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,1,2) )*det
);
448 MAT(out
,0,1) = (- (MAT(in
,0,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,0,2) )*det
);
449 MAT(out
,0,2) = ( (MAT(in
,0,1)*MAT(in
,1,2) - MAT(in
,1,1)*MAT(in
,0,2) )*det
);
450 MAT(out
,1,0) = (- (MAT(in
,1,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,1,2) )*det
);
451 MAT(out
,1,1) = ( (MAT(in
,0,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,0,2) )*det
);
452 MAT(out
,1,2) = (- (MAT(in
,0,0)*MAT(in
,1,2) - MAT(in
,1,0)*MAT(in
,0,2) )*det
);
453 MAT(out
,2,0) = ( (MAT(in
,1,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,1,1) )*det
);
454 MAT(out
,2,1) = (- (MAT(in
,0,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,0,1) )*det
);
455 MAT(out
,2,2) = ( (MAT(in
,0,0)*MAT(in
,1,1) - MAT(in
,1,0)*MAT(in
,0,1) )*det
);
457 /* Do the translation part */
458 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
459 MAT(in
,1,3) * MAT(out
,0,1) +
460 MAT(in
,2,3) * MAT(out
,0,2) );
461 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
462 MAT(in
,1,3) * MAT(out
,1,1) +
463 MAT(in
,2,3) * MAT(out
,1,2) );
464 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
465 MAT(in
,1,3) * MAT(out
,2,1) +
466 MAT(in
,2,3) * MAT(out
,2,2) );
472 * Compute inverse of a 3d transformation matrix.
474 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
475 * stored in the GLmatrix::inv attribute.
477 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
479 * If the matrix is not an angle preserving matrix then calls
480 * invert_matrix_3d_general for the actual calculation. Otherwise calculates
481 * the inverse matrix analyzing and inverting each of the scaling, rotation and
484 static GLboolean
invert_matrix_3d( GLmatrix
*mat
)
486 const GLfloat
*in
= mat
->m
;
487 GLfloat
*out
= mat
->inv
;
489 if (!TEST_MAT_FLAGS(mat
, MAT_FLAGS_ANGLE_PRESERVING
)) {
490 return invert_matrix_3d_general( mat
);
493 if (mat
->flags
& MAT_FLAG_UNIFORM_SCALE
) {
494 GLfloat scale
= (MAT(in
,0,0) * MAT(in
,0,0) +
495 MAT(in
,0,1) * MAT(in
,0,1) +
496 MAT(in
,0,2) * MAT(in
,0,2));
501 scale
= 1.0F
/ scale
;
503 /* Transpose and scale the 3 by 3 upper-left submatrix. */
504 MAT(out
,0,0) = scale
* MAT(in
,0,0);
505 MAT(out
,1,0) = scale
* MAT(in
,0,1);
506 MAT(out
,2,0) = scale
* MAT(in
,0,2);
507 MAT(out
,0,1) = scale
* MAT(in
,1,0);
508 MAT(out
,1,1) = scale
* MAT(in
,1,1);
509 MAT(out
,2,1) = scale
* MAT(in
,1,2);
510 MAT(out
,0,2) = scale
* MAT(in
,2,0);
511 MAT(out
,1,2) = scale
* MAT(in
,2,1);
512 MAT(out
,2,2) = scale
* MAT(in
,2,2);
514 else if (mat
->flags
& MAT_FLAG_ROTATION
) {
515 /* Transpose the 3 by 3 upper-left submatrix. */
516 MAT(out
,0,0) = MAT(in
,0,0);
517 MAT(out
,1,0) = MAT(in
,0,1);
518 MAT(out
,2,0) = MAT(in
,0,2);
519 MAT(out
,0,1) = MAT(in
,1,0);
520 MAT(out
,1,1) = MAT(in
,1,1);
521 MAT(out
,2,1) = MAT(in
,1,2);
522 MAT(out
,0,2) = MAT(in
,2,0);
523 MAT(out
,1,2) = MAT(in
,2,1);
524 MAT(out
,2,2) = MAT(in
,2,2);
527 /* pure translation */
528 MEMCPY( out
, Identity
, sizeof(Identity
) );
529 MAT(out
,0,3) = - MAT(in
,0,3);
530 MAT(out
,1,3) = - MAT(in
,1,3);
531 MAT(out
,2,3) = - MAT(in
,2,3);
535 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
536 /* Do the translation part */
537 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
538 MAT(in
,1,3) * MAT(out
,0,1) +
539 MAT(in
,2,3) * MAT(out
,0,2) );
540 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
541 MAT(in
,1,3) * MAT(out
,1,1) +
542 MAT(in
,2,3) * MAT(out
,1,2) );
543 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
544 MAT(in
,1,3) * MAT(out
,2,1) +
545 MAT(in
,2,3) * MAT(out
,2,2) );
548 MAT(out
,0,3) = MAT(out
,1,3) = MAT(out
,2,3) = 0.0;
555 * Compute inverse of an identity transformation matrix.
557 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
558 * stored in the GLmatrix::inv attribute.
560 * \return always GL_TRUE.
562 * Simply copies Identity into GLmatrix::inv.
564 static GLboolean
invert_matrix_identity( GLmatrix
*mat
)
566 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
571 * Compute inverse of a no-rotation 3d transformation matrix.
573 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
574 * stored in the GLmatrix::inv attribute.
576 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
580 static GLboolean
invert_matrix_3d_no_rot( GLmatrix
*mat
)
582 const GLfloat
*in
= mat
->m
;
583 GLfloat
*out
= mat
->inv
;
585 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0 || MAT(in
,2,2) == 0 )
588 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
589 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
590 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
591 MAT(out
,2,2) = 1.0F
/ MAT(in
,2,2);
593 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
594 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
595 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
596 MAT(out
,2,3) = - (MAT(in
,2,3) * MAT(out
,2,2));
603 * Compute inverse of a no-rotation 2d transformation matrix.
605 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
606 * stored in the GLmatrix::inv attribute.
608 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
610 * Calculates the inverse matrix by applying the inverse scaling and
611 * translation to the identity matrix.
613 static GLboolean
invert_matrix_2d_no_rot( GLmatrix
*mat
)
615 const GLfloat
*in
= mat
->m
;
616 GLfloat
*out
= mat
->inv
;
618 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0)
621 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
622 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
623 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
625 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
626 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
627 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
635 static GLboolean
invert_matrix_perspective( GLmatrix
*mat
)
637 const GLfloat
*in
= mat
->m
;
638 GLfloat
*out
= mat
->inv
;
640 if (MAT(in
,2,3) == 0)
643 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
645 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
646 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
648 MAT(out
,0,3) = MAT(in
,0,2);
649 MAT(out
,1,3) = MAT(in
,1,2);
654 MAT(out
,3,2) = 1.0F
/ MAT(in
,2,3);
655 MAT(out
,3,3) = MAT(in
,2,2) * MAT(out
,3,2);
662 * Matrix inversion function pointer type.
664 typedef GLboolean (*inv_mat_func
)( GLmatrix
*mat
);
667 * Table of the matrix inversion functions according to the matrix type.
669 static inv_mat_func inv_mat_tab
[7] = {
670 invert_matrix_general
,
671 invert_matrix_identity
,
672 invert_matrix_3d_no_rot
,
674 /* Don't use this function for now - it fails when the projection matrix
675 * is premultiplied by a translation (ala Chromium's tilesort SPU).
677 invert_matrix_perspective
,
679 invert_matrix_general
,
681 invert_matrix_3d
, /* lazy! */
682 invert_matrix_2d_no_rot
,
687 * Compute inverse of a transformation matrix.
689 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
690 * stored in the GLmatrix::inv attribute.
692 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
694 * Calls the matrix inversion function in inv_mat_tab corresponding to the
695 * given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag,
696 * and copies the identity matrix into GLmatrix::inv.
698 static GLboolean
matrix_invert( GLmatrix
*mat
)
700 if (inv_mat_tab
[mat
->type
](mat
)) {
701 mat
->flags
&= ~MAT_FLAG_SINGULAR
;
704 mat
->flags
|= MAT_FLAG_SINGULAR
;
705 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
713 /**********************************************************************/
714 /** \name Matrix generation */
718 * Generate a 4x4 transformation matrix from glRotate parameters, and
719 * post-multiply the input matrix by it.
722 * This function was contributed by Erich Boleyn (erich@uruk.org).
723 * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
726 _math_matrix_rotate( GLmatrix
*mat
,
727 GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
729 GLfloat xx
, yy
, zz
, xy
, yz
, zx
, xs
, ys
, zs
, one_c
, s
, c
;
733 s
= (GLfloat
) sin( angle
* DEG2RAD
);
734 c
= (GLfloat
) cos( angle
* DEG2RAD
);
736 MEMCPY(m
, Identity
, sizeof(GLfloat
)*16);
737 optimized
= GL_FALSE
;
739 #define M(row,col) m[col*4+row]
745 /* rotate only around z-axis */
758 else if (z
== 0.0F
) {
760 /* rotate only around y-axis */
773 else if (y
== 0.0F
) {
776 /* rotate only around x-axis */
791 const GLfloat mag
= SQRTF(x
* x
+ y
* y
+ z
* z
);
794 /* no rotation, leave mat as-is */
804 * Arbitrary axis rotation matrix.
806 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
807 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
808 * (which is about the X-axis), and the two composite transforms
809 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
810 * from the arbitrary axis to the X-axis then back. They are
811 * all elementary rotations.
813 * Rz' is a rotation about the Z-axis, to bring the axis vector
814 * into the x-z plane. Then Ry' is applied, rotating about the
815 * Y-axis to bring the axis vector parallel with the X-axis. The
816 * rotation about the X-axis is then performed. Ry and Rz are
817 * simply the respective inverse transforms to bring the arbitrary
818 * axis back to it's original orientation. The first transforms
819 * Rz' and Ry' are considered inverses, since the data from the
820 * arbitrary axis gives you info on how to get to it, not how
821 * to get away from it, and an inverse must be applied.
823 * The basic calculation used is to recognize that the arbitrary
824 * axis vector (x, y, z), since it is of unit length, actually
825 * represents the sines and cosines of the angles to rotate the
826 * X-axis to the same orientation, with theta being the angle about
827 * Z and phi the angle about Y (in the order described above)
830 * cos ( theta ) = x / sqrt ( 1 - z^2 )
831 * sin ( theta ) = y / sqrt ( 1 - z^2 )
833 * cos ( phi ) = sqrt ( 1 - z^2 )
836 * Note that cos ( phi ) can further be inserted to the above
839 * cos ( theta ) = x / cos ( phi )
840 * sin ( theta ) = y / sin ( phi )
842 * ...etc. Because of those relations and the standard trigonometric
843 * relations, it is pssible to reduce the transforms down to what
844 * is used below. It may be that any primary axis chosen will give the
845 * same results (modulo a sign convention) using thie method.
847 * Particularly nice is to notice that all divisions that might
848 * have caused trouble when parallel to certain planes or
849 * axis go away with care paid to reducing the expressions.
850 * After checking, it does perform correctly under all cases, since
851 * in all the cases of division where the denominator would have
852 * been zero, the numerator would have been zero as well, giving
853 * the expected result.
867 /* We already hold the identity-matrix so we can skip some statements */
868 M(0,0) = (one_c
* xx
) + c
;
869 M(0,1) = (one_c
* xy
) - zs
;
870 M(0,2) = (one_c
* zx
) + ys
;
873 M(1,0) = (one_c
* xy
) + zs
;
874 M(1,1) = (one_c
* yy
) + c
;
875 M(1,2) = (one_c
* yz
) - xs
;
878 M(2,0) = (one_c
* zx
) - ys
;
879 M(2,1) = (one_c
* yz
) + xs
;
880 M(2,2) = (one_c
* zz
) + c
;
892 matrix_multf( mat
, m
, MAT_FLAG_ROTATION
);
896 * Apply a perspective projection matrix.
898 * \param mat matrix to apply the projection.
899 * \param left left clipping plane coordinate.
900 * \param right right clipping plane coordinate.
901 * \param bottom bottom clipping plane coordinate.
902 * \param top top clipping plane coordinate.
903 * \param nearval distance to the near clipping plane.
904 * \param farval distance to the far clipping plane.
906 * Creates the projection matrix and multiplies it with \p mat, marking the
907 * MAT_FLAG_PERSPECTIVE flag.
910 _math_matrix_frustum( GLmatrix
*mat
,
911 GLfloat left
, GLfloat right
,
912 GLfloat bottom
, GLfloat top
,
913 GLfloat nearval
, GLfloat farval
)
915 GLfloat x
, y
, a
, b
, c
, d
;
918 x
= (2.0F
*nearval
) / (right
-left
);
919 y
= (2.0F
*nearval
) / (top
-bottom
);
920 a
= (right
+left
) / (right
-left
);
921 b
= (top
+bottom
) / (top
-bottom
);
922 c
= -(farval
+nearval
) / ( farval
-nearval
);
923 d
= -(2.0F
*farval
*nearval
) / (farval
-nearval
); /* error? */
925 #define M(row,col) m[col*4+row]
926 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
927 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
928 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
929 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
932 matrix_multf( mat
, m
, MAT_FLAG_PERSPECTIVE
);
936 * Apply an orthographic projection matrix.
938 * \param mat matrix to apply the projection.
939 * \param left left clipping plane coordinate.
940 * \param right right clipping plane coordinate.
941 * \param bottom bottom clipping plane coordinate.
942 * \param top top clipping plane coordinate.
943 * \param nearval distance to the near clipping plane.
944 * \param farval distance to the far clipping plane.
946 * Creates the projection matrix and multiplies it with \p mat, marking the
947 * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
950 _math_matrix_ortho( GLmatrix
*mat
,
951 GLfloat left
, GLfloat right
,
952 GLfloat bottom
, GLfloat top
,
953 GLfloat nearval
, GLfloat farval
)
957 #define M(row,col) m[col*4+row]
958 M(0,0) = 2.0F
/ (right
-left
);
961 M(0,3) = -(right
+left
) / (right
-left
);
964 M(1,1) = 2.0F
/ (top
-bottom
);
966 M(1,3) = -(top
+bottom
) / (top
-bottom
);
970 M(2,2) = -2.0F
/ (farval
-nearval
);
971 M(2,3) = -(farval
+nearval
) / (farval
-nearval
);
979 matrix_multf( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
983 * Multiply a matrix with a general scaling matrix.
986 * \param x x axis scale factor.
987 * \param y y axis scale factor.
988 * \param z z axis scale factor.
990 * Multiplies in-place the elements of \p mat by the scale factors. Checks if
991 * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE
992 * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and
993 * MAT_DIRTY_INVERSE dirty flags.
996 _math_matrix_scale( GLmatrix
*mat
, GLfloat x
, GLfloat y
, GLfloat z
)
999 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
1000 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
1001 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
1002 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
1004 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
1005 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1007 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1009 mat
->flags
|= (MAT_DIRTY_TYPE
|
1014 * Multiply a matrix with a translation matrix.
1016 * \param mat matrix.
1017 * \param x translation vector x coordinate.
1018 * \param y translation vector y coordinate.
1019 * \param z translation vector z coordinate.
1021 * Adds the translation coordinates to the elements of \p mat in-place. Marks
1022 * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE
1026 _math_matrix_translate( GLmatrix
*mat
, GLfloat x
, GLfloat y
, GLfloat z
)
1028 GLfloat
*m
= mat
->m
;
1029 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1030 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1031 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1032 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1034 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
1040 * Set a matrix to the identity matrix.
1042 * \param mat matrix.
1044 * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL.
1045 * Sets the matrix type to identity, and clear the dirty flags.
1048 _math_matrix_set_identity( GLmatrix
*mat
)
1050 MEMCPY( mat
->m
, Identity
, 16*sizeof(GLfloat
) );
1053 MEMCPY( mat
->inv
, Identity
, 16*sizeof(GLfloat
) );
1055 mat
->type
= MATRIX_IDENTITY
;
1056 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
1064 /**********************************************************************/
1065 /** \name Matrix analysis */
1068 #define ZERO(x) (1<<x)
1069 #define ONE(x) (1<<(x+16))
1071 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
1072 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
1074 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
1075 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
1076 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1077 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1079 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
1080 ZERO(1) | ZERO(9) | \
1081 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1082 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1084 #define MASK_2D ( ZERO(8) | \
1086 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1087 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1090 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
1091 ZERO(1) | ZERO(9) | \
1092 ZERO(2) | ZERO(6) | \
1093 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1098 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1101 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
1102 ZERO(1) | ZERO(13) |\
1103 ZERO(2) | ZERO(6) | \
1104 ZERO(3) | ZERO(7) | ZERO(15) )
1106 #define SQ(x) ((x)*(x))
1109 * Determine type and flags from scratch.
1111 * \param mat matrix.
1113 * This is expensive enough to only want to do it once.
1115 static void analyse_from_scratch( GLmatrix
*mat
)
1117 const GLfloat
*m
= mat
->m
;
1121 for (i
= 0 ; i
< 16 ; i
++) {
1122 if (m
[i
] == 0.0) mask
|= (1<<i
);
1125 if (m
[0] == 1.0F
) mask
|= (1<<16);
1126 if (m
[5] == 1.0F
) mask
|= (1<<21);
1127 if (m
[10] == 1.0F
) mask
|= (1<<26);
1128 if (m
[15] == 1.0F
) mask
|= (1<<31);
1130 mat
->flags
&= ~MAT_FLAGS_GEOMETRY
;
1132 /* Check for translation - no-one really cares
1134 if ((mask
& MASK_NO_TRX
) != MASK_NO_TRX
)
1135 mat
->flags
|= MAT_FLAG_TRANSLATION
;
1139 if (mask
== (GLuint
) MASK_IDENTITY
) {
1140 mat
->type
= MATRIX_IDENTITY
;
1142 else if ((mask
& MASK_2D_NO_ROT
) == (GLuint
) MASK_2D_NO_ROT
) {
1143 mat
->type
= MATRIX_2D_NO_ROT
;
1145 if ((mask
& MASK_NO_2D_SCALE
) != MASK_NO_2D_SCALE
)
1146 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1148 else if ((mask
& MASK_2D
) == (GLuint
) MASK_2D
) {
1149 GLfloat mm
= DOT2(m
, m
);
1150 GLfloat m4m4
= DOT2(m
+4,m
+4);
1151 GLfloat mm4
= DOT2(m
,m
+4);
1153 mat
->type
= MATRIX_2D
;
1155 /* Check for scale */
1156 if (SQ(mm
-1) > SQ(1e-6) ||
1157 SQ(m4m4
-1) > SQ(1e-6))
1158 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1160 /* Check for rotation */
1161 if (SQ(mm4
) > SQ(1e-6))
1162 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
1164 mat
->flags
|= MAT_FLAG_ROTATION
;
1167 else if ((mask
& MASK_3D_NO_ROT
) == (GLuint
) MASK_3D_NO_ROT
) {
1168 mat
->type
= MATRIX_3D_NO_ROT
;
1170 /* Check for scale */
1171 if (SQ(m
[0]-m
[5]) < SQ(1e-6) &&
1172 SQ(m
[0]-m
[10]) < SQ(1e-6)) {
1173 if (SQ(m
[0]-1.0) > SQ(1e-6)) {
1174 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1178 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1181 else if ((mask
& MASK_3D
) == (GLuint
) MASK_3D
) {
1182 GLfloat c1
= DOT3(m
,m
);
1183 GLfloat c2
= DOT3(m
+4,m
+4);
1184 GLfloat c3
= DOT3(m
+8,m
+8);
1185 GLfloat d1
= DOT3(m
, m
+4);
1188 mat
->type
= MATRIX_3D
;
1190 /* Check for scale */
1191 if (SQ(c1
-c2
) < SQ(1e-6) && SQ(c1
-c3
) < SQ(1e-6)) {
1192 if (SQ(c1
-1.0) > SQ(1e-6))
1193 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1194 /* else no scale at all */
1197 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1200 /* Check for rotation */
1201 if (SQ(d1
) < SQ(1e-6)) {
1202 CROSS3( cp
, m
, m
+4 );
1203 SUB_3V( cp
, cp
, (m
+8) );
1204 if (LEN_SQUARED_3FV(cp
) < SQ(1e-6))
1205 mat
->flags
|= MAT_FLAG_ROTATION
;
1207 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
1210 mat
->flags
|= MAT_FLAG_GENERAL_3D
; /* shear, etc */
1213 else if ((mask
& MASK_PERSPECTIVE
) == MASK_PERSPECTIVE
&& m
[11]==-1.0F
) {
1214 mat
->type
= MATRIX_PERSPECTIVE
;
1215 mat
->flags
|= MAT_FLAG_GENERAL
;
1218 mat
->type
= MATRIX_GENERAL
;
1219 mat
->flags
|= MAT_FLAG_GENERAL
;
1224 * Analyze a matrix given that its flags are accurate.
1226 * This is the more common operation, hopefully.
1228 static void analyse_from_flags( GLmatrix
*mat
)
1230 const GLfloat
*m
= mat
->m
;
1232 if (TEST_MAT_FLAGS(mat
, 0)) {
1233 mat
->type
= MATRIX_IDENTITY
;
1235 else if (TEST_MAT_FLAGS(mat
, (MAT_FLAG_TRANSLATION
|
1236 MAT_FLAG_UNIFORM_SCALE
|
1237 MAT_FLAG_GENERAL_SCALE
))) {
1238 if ( m
[10]==1.0F
&& m
[14]==0.0F
) {
1239 mat
->type
= MATRIX_2D_NO_ROT
;
1242 mat
->type
= MATRIX_3D_NO_ROT
;
1245 else if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
)) {
1248 && m
[2]==0.0F
&& m
[6]==0.0F
&& m
[10]==1.0F
&& m
[14]==0.0F
) {
1249 mat
->type
= MATRIX_2D
;
1252 mat
->type
= MATRIX_3D
;
1255 else if ( m
[4]==0.0F
&& m
[12]==0.0F
1256 && m
[1]==0.0F
&& m
[13]==0.0F
1257 && m
[2]==0.0F
&& m
[6]==0.0F
1258 && m
[3]==0.0F
&& m
[7]==0.0F
&& m
[11]==-1.0F
&& m
[15]==0.0F
) {
1259 mat
->type
= MATRIX_PERSPECTIVE
;
1262 mat
->type
= MATRIX_GENERAL
;
1267 * Analyze and update a matrix.
1269 * \param mat matrix.
1271 * If the matrix type is dirty then calls either analyse_from_scratch() or
1272 * analyse_from_flags() to determine its type, according to whether the flags
1273 * are dirty or not, respectively. If the matrix has an inverse and it's dirty
1274 * then calls matrix_invert(). Finally clears the dirty flags.
1277 _math_matrix_analyse( GLmatrix
*mat
)
1279 if (mat
->flags
& MAT_DIRTY_TYPE
) {
1280 if (mat
->flags
& MAT_DIRTY_FLAGS
)
1281 analyse_from_scratch( mat
);
1283 analyse_from_flags( mat
);
1286 if (mat
->inv
&& (mat
->flags
& MAT_DIRTY_INVERSE
)) {
1287 matrix_invert( mat
);
1290 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
1298 /**********************************************************************/
1299 /** \name Matrix setup */
1305 * \param to destination matrix.
1306 * \param from source matrix.
1308 * Copies all fields in GLmatrix, creating an inverse array if necessary.
1311 _math_matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
1313 MEMCPY( to
->m
, from
->m
, sizeof(Identity
) );
1314 to
->flags
= from
->flags
;
1315 to
->type
= from
->type
;
1318 if (from
->inv
== 0) {
1319 matrix_invert( to
);
1322 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
1328 * Loads a matrix array into GLmatrix.
1330 * \param m matrix array.
1331 * \param mat matrix.
1333 * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
1337 _math_matrix_loadf( GLmatrix
*mat
, const GLfloat
*m
)
1339 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
1340 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY
);
1344 * Matrix constructor.
1348 * Initialize the GLmatrix fields.
1351 _math_matrix_ctr( GLmatrix
*m
)
1353 m
->m
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
1355 MEMCPY( m
->m
, Identity
, sizeof(Identity
) );
1357 m
->type
= MATRIX_IDENTITY
;
1362 * Matrix destructor.
1366 * Frees the data in a GLmatrix.
1369 _math_matrix_dtr( GLmatrix
*m
)
1376 ALIGN_FREE( m
->inv
);
1382 * Allocate a matrix inverse.
1386 * Allocates the matrix inverse, GLmatrix::inv, and sets it to Identity.
1389 _math_matrix_alloc_inv( GLmatrix
*m
)
1392 m
->inv
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
1394 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
1401 /**********************************************************************/
1402 /** \name Matrix transpose */
1406 * Transpose a GLfloat matrix.
1408 * \param to destination array.
1409 * \param from source array.
1412 _math_transposef( GLfloat to
[16], const GLfloat from
[16] )
1433 * Transpose a GLdouble matrix.
1435 * \param to destination array.
1436 * \param from source array.
1439 _math_transposed( GLdouble to
[16], const GLdouble from
[16] )
1460 * Transpose a GLdouble matrix and convert to GLfloat.
1462 * \param to destination array.
1463 * \param from source array.
1466 _math_transposefd( GLfloat to
[16], const GLdouble from
[16] )
1468 to
[0] = (GLfloat
) from
[0];
1469 to
[1] = (GLfloat
) from
[4];
1470 to
[2] = (GLfloat
) from
[8];
1471 to
[3] = (GLfloat
) from
[12];
1472 to
[4] = (GLfloat
) from
[1];
1473 to
[5] = (GLfloat
) from
[5];
1474 to
[6] = (GLfloat
) from
[9];
1475 to
[7] = (GLfloat
) from
[13];
1476 to
[8] = (GLfloat
) from
[2];
1477 to
[9] = (GLfloat
) from
[6];
1478 to
[10] = (GLfloat
) from
[10];
1479 to
[11] = (GLfloat
) from
[14];
1480 to
[12] = (GLfloat
) from
[3];
1481 to
[13] = (GLfloat
) from
[7];
1482 to
[14] = (GLfloat
) from
[11];
1483 to
[15] = (GLfloat
) from
[15];