1 /* $Id: m_matrix.c,v 1.12 2002/06/29 19:48:17 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 * 1. 4x4 transformation matrices are stored in memory in column major order.
33 * 2. Points/vertices are to be thought of as column vectors.
34 * 3. Transformation of a point p by a matrix M is: p' = M * p
48 static const char *types
[] = {
59 static GLfloat Identity
[16] = {
70 * This matmul was contributed by Thomas Malik
72 * Perform a 4x4 matrix multiplication (product = a x b).
73 * Input: a, b - matrices to multiply
74 * Output: product - product of a and b
75 * WARNING: (product != b) assumed
76 * NOTE: (product == a) allowed
80 #define A(row,col) a[(col<<2)+row]
81 #define B(row,col) b[(col<<2)+row]
82 #define P(row,col) product[(col<<2)+row]
84 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
87 for (i
= 0; i
< 4; i
++) {
88 const GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
89 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
90 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
91 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
92 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
97 /* Multiply two matrices known to occupy only the top three rows, such
98 * as typical model matrices, and ortho matrices.
100 static void matmul34( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
103 for (i
= 0; i
< 3; i
++) {
104 const GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
105 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0);
106 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1);
107 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2);
108 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
;
123 * Multiply a matrix by an array of floats with known properties.
125 static void matrix_multf( GLmatrix
*mat
, const GLfloat
*m
, GLuint flags
)
127 mat
->flags
|= (flags
| MAT_DIRTY_TYPE
| MAT_DIRTY_INVERSE
);
129 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
130 matmul34( mat
->m
, mat
->m
, m
);
132 matmul4( mat
->m
, mat
->m
, m
);
136 static void print_matrix_floats( const GLfloat m
[16] )
140 _mesa_debug(NULL
,"\t%f %f %f %f\n", m
[i
], m
[4+i
], m
[8+i
], m
[12+i
] );
145 _math_matrix_print( const GLmatrix
*m
)
147 _mesa_debug(NULL
, "Matrix type: %s, flags: %x\n", types
[m
->type
], m
->flags
);
148 print_matrix_floats(m
->m
);
149 _mesa_debug(NULL
, "Inverse: \n");
152 print_matrix_floats(m
->inv
);
153 matmul4(prod
, m
->m
, m
->inv
);
154 _mesa_debug(NULL
, "Mat * Inverse:\n");
155 print_matrix_floats(prod
);
158 _mesa_debug(NULL
, " - not available\n");
165 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
166 #define MAT(m,r,c) (m)[(c)*4+(r)]
169 * Compute inverse of 4x4 transformation matrix.
170 * Code contributed by Jacques Leroy jle@star.be
171 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
173 static GLboolean
invert_matrix_general( GLmatrix
*mat
)
175 const GLfloat
*m
= mat
->m
;
176 GLfloat
*out
= mat
->inv
;
178 GLfloat m0
, m1
, m2
, m3
, s
;
179 GLfloat
*r0
, *r1
, *r2
, *r3
;
181 r0
= wtmp
[0], r1
= wtmp
[1], r2
= wtmp
[2], r3
= wtmp
[3];
183 r0
[0] = MAT(m
,0,0), r0
[1] = MAT(m
,0,1),
184 r0
[2] = MAT(m
,0,2), r0
[3] = MAT(m
,0,3),
185 r0
[4] = 1.0, r0
[5] = r0
[6] = r0
[7] = 0.0,
187 r1
[0] = MAT(m
,1,0), r1
[1] = MAT(m
,1,1),
188 r1
[2] = MAT(m
,1,2), r1
[3] = MAT(m
,1,3),
189 r1
[5] = 1.0, r1
[4] = r1
[6] = r1
[7] = 0.0,
191 r2
[0] = MAT(m
,2,0), r2
[1] = MAT(m
,2,1),
192 r2
[2] = MAT(m
,2,2), r2
[3] = MAT(m
,2,3),
193 r2
[6] = 1.0, r2
[4] = r2
[5] = r2
[7] = 0.0,
195 r3
[0] = MAT(m
,3,0), r3
[1] = MAT(m
,3,1),
196 r3
[2] = MAT(m
,3,2), r3
[3] = MAT(m
,3,3),
197 r3
[7] = 1.0, r3
[4] = r3
[5] = r3
[6] = 0.0;
199 /* choose pivot - or die */
200 if (fabs(r3
[0])>fabs(r2
[0])) SWAP_ROWS(r3
, r2
);
201 if (fabs(r2
[0])>fabs(r1
[0])) SWAP_ROWS(r2
, r1
);
202 if (fabs(r1
[0])>fabs(r0
[0])) SWAP_ROWS(r1
, r0
);
203 if (0.0 == r0
[0]) return GL_FALSE
;
205 /* eliminate first variable */
206 m1
= r1
[0]/r0
[0]; m2
= r2
[0]/r0
[0]; m3
= r3
[0]/r0
[0];
207 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
208 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
209 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
211 if (s
!= 0.0) { r1
[4] -= m1
* s
; r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
213 if (s
!= 0.0) { r1
[5] -= m1
* s
; r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
215 if (s
!= 0.0) { r1
[6] -= m1
* s
; r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
217 if (s
!= 0.0) { r1
[7] -= m1
* s
; r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
219 /* choose pivot - or die */
220 if (fabs(r3
[1])>fabs(r2
[1])) SWAP_ROWS(r3
, r2
);
221 if (fabs(r2
[1])>fabs(r1
[1])) SWAP_ROWS(r2
, r1
);
222 if (0.0 == r1
[1]) return GL_FALSE
;
224 /* eliminate second variable */
225 m2
= r2
[1]/r1
[1]; m3
= r3
[1]/r1
[1];
226 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
227 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
228 s
= r1
[4]; if (0.0 != s
) { r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
229 s
= r1
[5]; if (0.0 != s
) { r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
230 s
= r1
[6]; if (0.0 != s
) { r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
231 s
= r1
[7]; if (0.0 != s
) { r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
233 /* choose pivot - or die */
234 if (fabs(r3
[2])>fabs(r2
[2])) SWAP_ROWS(r3
, r2
);
235 if (0.0 == r2
[2]) return GL_FALSE
;
237 /* eliminate third variable */
239 r3
[3] -= m3
* r2
[3], r3
[4] -= m3
* r2
[4],
240 r3
[5] -= m3
* r2
[5], r3
[6] -= m3
* r2
[6],
244 if (0.0 == r3
[3]) return GL_FALSE
;
246 s
= 1.0F
/r3
[3]; /* now back substitute row 3 */
247 r3
[4] *= s
; r3
[5] *= s
; r3
[6] *= s
; r3
[7] *= s
;
249 m2
= r2
[3]; /* now back substitute row 2 */
251 r2
[4] = s
* (r2
[4] - r3
[4] * m2
), r2
[5] = s
* (r2
[5] - r3
[5] * m2
),
252 r2
[6] = s
* (r2
[6] - r3
[6] * m2
), r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
254 r1
[4] -= r3
[4] * m1
, r1
[5] -= r3
[5] * m1
,
255 r1
[6] -= r3
[6] * m1
, r1
[7] -= r3
[7] * m1
;
257 r0
[4] -= r3
[4] * m0
, r0
[5] -= r3
[5] * m0
,
258 r0
[6] -= r3
[6] * m0
, r0
[7] -= r3
[7] * m0
;
260 m1
= r1
[2]; /* now back substitute row 1 */
262 r1
[4] = s
* (r1
[4] - r2
[4] * m1
), r1
[5] = s
* (r1
[5] - r2
[5] * m1
),
263 r1
[6] = s
* (r1
[6] - r2
[6] * m1
), r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
265 r0
[4] -= r2
[4] * m0
, r0
[5] -= r2
[5] * m0
,
266 r0
[6] -= r2
[6] * m0
, r0
[7] -= r2
[7] * m0
;
268 m0
= r0
[1]; /* now back substitute row 0 */
270 r0
[4] = s
* (r0
[4] - r1
[4] * m0
), r0
[5] = s
* (r0
[5] - r1
[5] * m0
),
271 r0
[6] = s
* (r0
[6] - r1
[6] * m0
), r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
273 MAT(out
,0,0) = r0
[4]; MAT(out
,0,1) = r0
[5],
274 MAT(out
,0,2) = r0
[6]; MAT(out
,0,3) = r0
[7],
275 MAT(out
,1,0) = r1
[4]; MAT(out
,1,1) = r1
[5],
276 MAT(out
,1,2) = r1
[6]; MAT(out
,1,3) = r1
[7],
277 MAT(out
,2,0) = r2
[4]; MAT(out
,2,1) = r2
[5],
278 MAT(out
,2,2) = r2
[6]; MAT(out
,2,3) = r2
[7],
279 MAT(out
,3,0) = r3
[4]; MAT(out
,3,1) = r3
[5],
280 MAT(out
,3,2) = r3
[6]; MAT(out
,3,3) = r3
[7];
287 /* Adapted from graphics gems II.
289 static GLboolean
invert_matrix_3d_general( GLmatrix
*mat
)
291 const GLfloat
*in
= mat
->m
;
292 GLfloat
*out
= mat
->inv
;
296 /* Calculate the determinant of upper left 3x3 submatrix and
297 * determine if the matrix is singular.
300 t
= MAT(in
,0,0) * MAT(in
,1,1) * MAT(in
,2,2);
301 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
303 t
= MAT(in
,1,0) * MAT(in
,2,1) * MAT(in
,0,2);
304 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
306 t
= MAT(in
,2,0) * MAT(in
,0,1) * MAT(in
,1,2);
307 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
309 t
= -MAT(in
,2,0) * MAT(in
,1,1) * MAT(in
,0,2);
310 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
312 t
= -MAT(in
,1,0) * MAT(in
,0,1) * MAT(in
,2,2);
313 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
315 t
= -MAT(in
,0,0) * MAT(in
,2,1) * MAT(in
,1,2);
316 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
324 MAT(out
,0,0) = ( (MAT(in
,1,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,1,2) )*det
);
325 MAT(out
,0,1) = (- (MAT(in
,0,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,0,2) )*det
);
326 MAT(out
,0,2) = ( (MAT(in
,0,1)*MAT(in
,1,2) - MAT(in
,1,1)*MAT(in
,0,2) )*det
);
327 MAT(out
,1,0) = (- (MAT(in
,1,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,1,2) )*det
);
328 MAT(out
,1,1) = ( (MAT(in
,0,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,0,2) )*det
);
329 MAT(out
,1,2) = (- (MAT(in
,0,0)*MAT(in
,1,2) - MAT(in
,1,0)*MAT(in
,0,2) )*det
);
330 MAT(out
,2,0) = ( (MAT(in
,1,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,1,1) )*det
);
331 MAT(out
,2,1) = (- (MAT(in
,0,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,0,1) )*det
);
332 MAT(out
,2,2) = ( (MAT(in
,0,0)*MAT(in
,1,1) - MAT(in
,1,0)*MAT(in
,0,1) )*det
);
334 /* Do the translation part */
335 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
336 MAT(in
,1,3) * MAT(out
,0,1) +
337 MAT(in
,2,3) * MAT(out
,0,2) );
338 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
339 MAT(in
,1,3) * MAT(out
,1,1) +
340 MAT(in
,2,3) * MAT(out
,1,2) );
341 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
342 MAT(in
,1,3) * MAT(out
,2,1) +
343 MAT(in
,2,3) * MAT(out
,2,2) );
349 static GLboolean
invert_matrix_3d( GLmatrix
*mat
)
351 const GLfloat
*in
= mat
->m
;
352 GLfloat
*out
= mat
->inv
;
354 if (!TEST_MAT_FLAGS(mat
, MAT_FLAGS_ANGLE_PRESERVING
)) {
355 return invert_matrix_3d_general( mat
);
358 if (mat
->flags
& MAT_FLAG_UNIFORM_SCALE
) {
359 GLfloat scale
= (MAT(in
,0,0) * MAT(in
,0,0) +
360 MAT(in
,0,1) * MAT(in
,0,1) +
361 MAT(in
,0,2) * MAT(in
,0,2));
366 scale
= 1.0F
/ scale
;
368 /* Transpose and scale the 3 by 3 upper-left submatrix. */
369 MAT(out
,0,0) = scale
* MAT(in
,0,0);
370 MAT(out
,1,0) = scale
* MAT(in
,0,1);
371 MAT(out
,2,0) = scale
* MAT(in
,0,2);
372 MAT(out
,0,1) = scale
* MAT(in
,1,0);
373 MAT(out
,1,1) = scale
* MAT(in
,1,1);
374 MAT(out
,2,1) = scale
* MAT(in
,1,2);
375 MAT(out
,0,2) = scale
* MAT(in
,2,0);
376 MAT(out
,1,2) = scale
* MAT(in
,2,1);
377 MAT(out
,2,2) = scale
* MAT(in
,2,2);
379 else if (mat
->flags
& MAT_FLAG_ROTATION
) {
380 /* Transpose the 3 by 3 upper-left submatrix. */
381 MAT(out
,0,0) = MAT(in
,0,0);
382 MAT(out
,1,0) = MAT(in
,0,1);
383 MAT(out
,2,0) = MAT(in
,0,2);
384 MAT(out
,0,1) = MAT(in
,1,0);
385 MAT(out
,1,1) = MAT(in
,1,1);
386 MAT(out
,2,1) = MAT(in
,1,2);
387 MAT(out
,0,2) = MAT(in
,2,0);
388 MAT(out
,1,2) = MAT(in
,2,1);
389 MAT(out
,2,2) = MAT(in
,2,2);
392 /* pure translation */
393 MEMCPY( out
, Identity
, sizeof(Identity
) );
394 MAT(out
,0,3) = - MAT(in
,0,3);
395 MAT(out
,1,3) = - MAT(in
,1,3);
396 MAT(out
,2,3) = - MAT(in
,2,3);
400 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
401 /* Do the translation part */
402 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
403 MAT(in
,1,3) * MAT(out
,0,1) +
404 MAT(in
,2,3) * MAT(out
,0,2) );
405 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
406 MAT(in
,1,3) * MAT(out
,1,1) +
407 MAT(in
,2,3) * MAT(out
,1,2) );
408 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
409 MAT(in
,1,3) * MAT(out
,2,1) +
410 MAT(in
,2,3) * MAT(out
,2,2) );
413 MAT(out
,0,3) = MAT(out
,1,3) = MAT(out
,2,3) = 0.0;
421 static GLboolean
invert_matrix_identity( GLmatrix
*mat
)
423 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
428 static GLboolean
invert_matrix_3d_no_rot( GLmatrix
*mat
)
430 const GLfloat
*in
= mat
->m
;
431 GLfloat
*out
= mat
->inv
;
433 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0 || MAT(in
,2,2) == 0 )
436 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
437 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
438 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
439 MAT(out
,2,2) = 1.0F
/ MAT(in
,2,2);
441 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
442 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
443 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
444 MAT(out
,2,3) = - (MAT(in
,2,3) * MAT(out
,2,2));
451 static GLboolean
invert_matrix_2d_no_rot( GLmatrix
*mat
)
453 const GLfloat
*in
= mat
->m
;
454 GLfloat
*out
= mat
->inv
;
456 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0)
459 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
460 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
461 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
463 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
464 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
465 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
474 static GLboolean
invert_matrix_perspective( GLmatrix
*mat
)
476 const GLfloat
*in
= mat
->m
;
477 GLfloat
*out
= mat
->inv
;
479 if (MAT(in
,2,3) == 0)
482 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
484 MAT(out
,0,0) = 1.0F
/ MAT(in
,0,0);
485 MAT(out
,1,1) = 1.0F
/ MAT(in
,1,1);
487 MAT(out
,0,3) = MAT(in
,0,2);
488 MAT(out
,1,3) = MAT(in
,1,2);
493 MAT(out
,3,2) = 1.0F
/ MAT(in
,2,3);
494 MAT(out
,3,3) = MAT(in
,2,2) * MAT(out
,3,2);
501 typedef GLboolean (*inv_mat_func
)( GLmatrix
*mat
);
504 static inv_mat_func inv_mat_tab
[7] = {
505 invert_matrix_general
,
506 invert_matrix_identity
,
507 invert_matrix_3d_no_rot
,
509 /* Don't use this function for now - it fails when the projection matrix
510 * is premultiplied by a translation (ala Chromium's tilesort SPU).
512 invert_matrix_perspective
,
514 invert_matrix_general
,
516 invert_matrix_3d
, /* lazy! */
517 invert_matrix_2d_no_rot
,
522 static GLboolean
matrix_invert( GLmatrix
*mat
)
524 if (inv_mat_tab
[mat
->type
](mat
)) {
525 mat
->flags
&= ~MAT_FLAG_SINGULAR
;
528 mat
->flags
|= MAT_FLAG_SINGULAR
;
529 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
540 * Generate a 4x4 transformation matrix from glRotate parameters, and
541 * postmultiply the input matrix by it.
544 _math_matrix_rotate( GLmatrix
*mat
,
545 GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
547 /* This function contributed by Erich Boleyn (erich@uruk.org) */
549 GLfloat xx
, yy
, zz
, xy
, yz
, zx
, xs
, ys
, zs
, one_c
;
552 s
= (GLfloat
) sin( angle
* DEG2RAD
);
553 c
= (GLfloat
) cos( angle
* DEG2RAD
);
555 mag
= (GLfloat
) GL_SQRT( x
*x
+ y
*y
+ z
*z
);
558 /* generate an identity matrix and return */
559 MEMCPY(m
, Identity
, sizeof(GLfloat
)*16);
567 #define M(row,col) m[col*4+row]
570 * Arbitrary axis rotation matrix.
572 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
573 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
574 * (which is about the X-axis), and the two composite transforms
575 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
576 * from the arbitrary axis to the X-axis then back. They are
577 * all elementary rotations.
579 * Rz' is a rotation about the Z-axis, to bring the axis vector
580 * into the x-z plane. Then Ry' is applied, rotating about the
581 * Y-axis to bring the axis vector parallel with the X-axis. The
582 * rotation about the X-axis is then performed. Ry and Rz are
583 * simply the respective inverse transforms to bring the arbitrary
584 * axis back to it's original orientation. The first transforms
585 * Rz' and Ry' are considered inverses, since the data from the
586 * arbitrary axis gives you info on how to get to it, not how
587 * to get away from it, and an inverse must be applied.
589 * The basic calculation used is to recognize that the arbitrary
590 * axis vector (x, y, z), since it is of unit length, actually
591 * represents the sines and cosines of the angles to rotate the
592 * X-axis to the same orientation, with theta being the angle about
593 * Z and phi the angle about Y (in the order described above)
596 * cos ( theta ) = x / sqrt ( 1 - z^2 )
597 * sin ( theta ) = y / sqrt ( 1 - z^2 )
599 * cos ( phi ) = sqrt ( 1 - z^2 )
602 * Note that cos ( phi ) can further be inserted to the above
605 * cos ( theta ) = x / cos ( phi )
606 * sin ( theta ) = y / sin ( phi )
608 * ...etc. Because of those relations and the standard trigonometric
609 * relations, it is pssible to reduce the transforms down to what
610 * is used below. It may be that any primary axis chosen will give the
611 * same results (modulo a sign convention) using thie method.
613 * Particularly nice is to notice that all divisions that might
614 * have caused trouble when parallel to certain planes or
615 * axis go away with care paid to reducing the expressions.
616 * After checking, it does perform correctly under all cases, since
617 * in all the cases of division where the denominator would have
618 * been zero, the numerator would have been zero as well, giving
619 * the expected result.
633 M(0,0) = (one_c
* xx
) + c
;
634 M(0,1) = (one_c
* xy
) - zs
;
635 M(0,2) = (one_c
* zx
) + ys
;
638 M(1,0) = (one_c
* xy
) + zs
;
639 M(1,1) = (one_c
* yy
) + c
;
640 M(1,2) = (one_c
* yz
) - xs
;
643 M(2,0) = (one_c
* zx
) - ys
;
644 M(2,1) = (one_c
* yz
) + xs
;
645 M(2,2) = (one_c
* zz
) + c
;
655 matrix_multf( mat
, m
, MAT_FLAG_ROTATION
);
660 _math_matrix_frustum( GLmatrix
*mat
,
661 GLfloat left
, GLfloat right
,
662 GLfloat bottom
, GLfloat top
,
663 GLfloat nearval
, GLfloat farval
)
665 GLfloat x
, y
, a
, b
, c
, d
;
668 x
= (2.0F
*nearval
) / (right
-left
);
669 y
= (2.0F
*nearval
) / (top
-bottom
);
670 a
= (right
+left
) / (right
-left
);
671 b
= (top
+bottom
) / (top
-bottom
);
672 c
= -(farval
+nearval
) / ( farval
-nearval
);
673 d
= -(2.0F
*farval
*nearval
) / (farval
-nearval
); /* error? */
675 #define M(row,col) m[col*4+row]
676 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
677 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
678 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
679 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
682 matrix_multf( mat
, m
, MAT_FLAG_PERSPECTIVE
);
686 _math_matrix_ortho( GLmatrix
*mat
,
687 GLfloat left
, GLfloat right
,
688 GLfloat bottom
, GLfloat top
,
689 GLfloat nearval
, GLfloat farval
)
695 x
= 2.0F
/ (right
-left
);
696 y
= 2.0F
/ (top
-bottom
);
697 z
= -2.0F
/ (farval
-nearval
);
698 tx
= -(right
+left
) / (right
-left
);
699 ty
= -(top
+bottom
) / (top
-bottom
);
700 tz
= -(farval
+nearval
) / (farval
-nearval
);
702 #define M(row,col) m[col*4+row]
703 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = 0.0F
; M(0,3) = tx
;
704 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = 0.0F
; M(1,3) = ty
;
705 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = z
; M(2,3) = tz
;
706 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = 0.0F
; M(3,3) = 1.0F
;
709 matrix_multf( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
713 #define ZERO(x) (1<<x)
714 #define ONE(x) (1<<(x+16))
716 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
717 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
719 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
720 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
721 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
722 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
724 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
725 ZERO(1) | ZERO(9) | \
726 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
727 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
729 #define MASK_2D ( ZERO(8) | \
731 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
732 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
735 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
736 ZERO(1) | ZERO(9) | \
737 ZERO(2) | ZERO(6) | \
738 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
743 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
746 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
747 ZERO(1) | ZERO(13) |\
748 ZERO(2) | ZERO(6) | \
749 ZERO(3) | ZERO(7) | ZERO(15) )
751 #define SQ(x) ((x)*(x))
753 /* Determine type and flags from scratch. This is expensive enough to
754 * only want to do it once.
756 static void analyse_from_scratch( GLmatrix
*mat
)
758 const GLfloat
*m
= mat
->m
;
762 for (i
= 0 ; i
< 16 ; i
++) {
763 if (m
[i
] == 0.0) mask
|= (1<<i
);
766 if (m
[0] == 1.0F
) mask
|= (1<<16);
767 if (m
[5] == 1.0F
) mask
|= (1<<21);
768 if (m
[10] == 1.0F
) mask
|= (1<<26);
769 if (m
[15] == 1.0F
) mask
|= (1<<31);
771 mat
->flags
&= ~MAT_FLAGS_GEOMETRY
;
773 /* Check for translation - no-one really cares
775 if ((mask
& MASK_NO_TRX
) != MASK_NO_TRX
)
776 mat
->flags
|= MAT_FLAG_TRANSLATION
;
780 if (mask
== (GLuint
) MASK_IDENTITY
) {
781 mat
->type
= MATRIX_IDENTITY
;
783 else if ((mask
& MASK_2D_NO_ROT
) == (GLuint
) MASK_2D_NO_ROT
) {
784 mat
->type
= MATRIX_2D_NO_ROT
;
786 if ((mask
& MASK_NO_2D_SCALE
) != MASK_NO_2D_SCALE
)
787 mat
->flags
= MAT_FLAG_GENERAL_SCALE
;
789 else if ((mask
& MASK_2D
) == (GLuint
) MASK_2D
) {
790 GLfloat mm
= DOT2(m
, m
);
791 GLfloat m4m4
= DOT2(m
+4,m
+4);
792 GLfloat mm4
= DOT2(m
,m
+4);
794 mat
->type
= MATRIX_2D
;
796 /* Check for scale */
797 if (SQ(mm
-1) > SQ(1e-6) ||
798 SQ(m4m4
-1) > SQ(1e-6))
799 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
801 /* Check for rotation */
802 if (SQ(mm4
) > SQ(1e-6))
803 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
805 mat
->flags
|= MAT_FLAG_ROTATION
;
808 else if ((mask
& MASK_3D_NO_ROT
) == (GLuint
) MASK_3D_NO_ROT
) {
809 mat
->type
= MATRIX_3D_NO_ROT
;
811 /* Check for scale */
812 if (SQ(m
[0]-m
[5]) < SQ(1e-6) &&
813 SQ(m
[0]-m
[10]) < SQ(1e-6)) {
814 if (SQ(m
[0]-1.0) > SQ(1e-6)) {
815 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
819 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
822 else if ((mask
& MASK_3D
) == (GLuint
) MASK_3D
) {
823 GLfloat c1
= DOT3(m
,m
);
824 GLfloat c2
= DOT3(m
+4,m
+4);
825 GLfloat c3
= DOT3(m
+8,m
+8);
826 GLfloat d1
= DOT3(m
, m
+4);
829 mat
->type
= MATRIX_3D
;
831 /* Check for scale */
832 if (SQ(c1
-c2
) < SQ(1e-6) && SQ(c1
-c3
) < SQ(1e-6)) {
833 if (SQ(c1
-1.0) > SQ(1e-6))
834 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
835 /* else no scale at all */
838 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
841 /* Check for rotation */
842 if (SQ(d1
) < SQ(1e-6)) {
843 CROSS3( cp
, m
, m
+4 );
844 SUB_3V( cp
, cp
, (m
+8) );
845 if (LEN_SQUARED_3FV(cp
) < SQ(1e-6))
846 mat
->flags
|= MAT_FLAG_ROTATION
;
848 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
851 mat
->flags
|= MAT_FLAG_GENERAL_3D
; /* shear, etc */
854 else if ((mask
& MASK_PERSPECTIVE
) == MASK_PERSPECTIVE
&& m
[11]==-1.0F
) {
855 mat
->type
= MATRIX_PERSPECTIVE
;
856 mat
->flags
|= MAT_FLAG_GENERAL
;
859 mat
->type
= MATRIX_GENERAL
;
860 mat
->flags
|= MAT_FLAG_GENERAL
;
865 /* Analyse a matrix given that its flags are accurate - this is the
866 * more common operation, hopefully.
868 static void analyse_from_flags( GLmatrix
*mat
)
870 const GLfloat
*m
= mat
->m
;
872 if (TEST_MAT_FLAGS(mat
, 0)) {
873 mat
->type
= MATRIX_IDENTITY
;
875 else if (TEST_MAT_FLAGS(mat
, (MAT_FLAG_TRANSLATION
|
876 MAT_FLAG_UNIFORM_SCALE
|
877 MAT_FLAG_GENERAL_SCALE
))) {
878 if ( m
[10]==1.0F
&& m
[14]==0.0F
) {
879 mat
->type
= MATRIX_2D_NO_ROT
;
882 mat
->type
= MATRIX_3D_NO_ROT
;
885 else if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
)) {
888 && m
[2]==0.0F
&& m
[6]==0.0F
&& m
[10]==1.0F
&& m
[14]==0.0F
) {
889 mat
->type
= MATRIX_2D
;
892 mat
->type
= MATRIX_3D
;
895 else if ( m
[4]==0.0F
&& m
[12]==0.0F
896 && m
[1]==0.0F
&& m
[13]==0.0F
897 && m
[2]==0.0F
&& m
[6]==0.0F
898 && m
[3]==0.0F
&& m
[7]==0.0F
&& m
[11]==-1.0F
&& m
[15]==0.0F
) {
899 mat
->type
= MATRIX_PERSPECTIVE
;
902 mat
->type
= MATRIX_GENERAL
;
908 _math_matrix_analyse( GLmatrix
*mat
)
910 if (mat
->flags
& MAT_DIRTY_TYPE
) {
911 if (mat
->flags
& MAT_DIRTY_FLAGS
)
912 analyse_from_scratch( mat
);
914 analyse_from_flags( mat
);
917 if (mat
->inv
&& (mat
->flags
& MAT_DIRTY_INVERSE
)) {
918 matrix_invert( mat
);
921 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
928 _math_matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
930 MEMCPY( to
->m
, from
->m
, sizeof(Identity
) );
931 to
->flags
= from
->flags
;
932 to
->type
= from
->type
;
935 if (from
->inv
== 0) {
939 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
946 _math_matrix_scale( GLmatrix
*mat
, GLfloat x
, GLfloat y
, GLfloat z
)
949 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
950 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
951 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
952 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
954 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
955 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
957 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
959 mat
->flags
|= (MAT_DIRTY_TYPE
|
965 _math_matrix_translate( GLmatrix
*mat
, GLfloat x
, GLfloat y
, GLfloat z
)
968 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
969 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
970 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
971 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
973 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
980 _math_matrix_loadf( GLmatrix
*mat
, const GLfloat
*m
)
982 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
983 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY
);
987 _math_matrix_ctr( GLmatrix
*m
)
989 m
->m
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
991 MEMCPY( m
->m
, Identity
, sizeof(Identity
) );
993 m
->type
= MATRIX_IDENTITY
;
998 _math_matrix_dtr( GLmatrix
*m
)
1005 ALIGN_FREE( m
->inv
);
1012 _math_matrix_alloc_inv( GLmatrix
*m
)
1015 m
->inv
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
1017 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
1023 _math_matrix_mul_matrix( GLmatrix
*dest
, const GLmatrix
*a
, const GLmatrix
*b
)
1025 dest
->flags
= (a
->flags
|
1030 if (TEST_MAT_FLAGS(dest
, MAT_FLAGS_3D
))
1031 matmul34( dest
->m
, a
->m
, b
->m
);
1033 matmul4( dest
->m
, a
->m
, b
->m
);
1038 _math_matrix_mul_floats( GLmatrix
*dest
, const GLfloat
*m
)
1040 dest
->flags
|= (MAT_FLAG_GENERAL
|
1044 matmul4( dest
->m
, dest
->m
, m
);
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 _math_transposef( GLfloat to
[16], const GLfloat from
[16] )
1086 _math_transposed( GLdouble to
[16], const GLdouble from
[16] )
1107 _math_transposefd( GLfloat to
[16], const GLdouble from
[16] )
1109 to
[0] = (GLfloat
) from
[0];
1110 to
[1] = (GLfloat
) from
[4];
1111 to
[2] = (GLfloat
) from
[8];
1112 to
[3] = (GLfloat
) from
[12];
1113 to
[4] = (GLfloat
) from
[1];
1114 to
[5] = (GLfloat
) from
[5];
1115 to
[6] = (GLfloat
) from
[9];
1116 to
[7] = (GLfloat
) from
[13];
1117 to
[8] = (GLfloat
) from
[2];
1118 to
[9] = (GLfloat
) from
[6];
1119 to
[10] = (GLfloat
) from
[10];
1120 to
[11] = (GLfloat
) from
[14];
1121 to
[12] = (GLfloat
) from
[3];
1122 to
[13] = (GLfloat
) from
[7];
1123 to
[14] = (GLfloat
) from
[11];
1124 to
[15] = (GLfloat
) from
[15];