1f10800e17a1884b7f4f6585444288d84d0dc500
[mesa.git] / src / mesa / math / m_matrix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /**
27 * \file m_matrix.c
28 * Matrix operations.
29 *
30 * \note
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
34 */
35
36
37 #include "glheader.h"
38 #include "imports.h"
39 #include "macros.h"
40 #include "imports.h"
41
42 #include "m_matrix.h"
43
44
45 /**
46 * Names of the corresponding GLmatrixtype values.
47 */
48 static const char *types[] = {
49 "MATRIX_GENERAL",
50 "MATRIX_IDENTITY",
51 "MATRIX_3D_NO_ROT",
52 "MATRIX_PERSPECTIVE",
53 "MATRIX_2D",
54 "MATRIX_2D_NO_ROT",
55 "MATRIX_3D"
56 };
57
58
59 /**
60 * Identity matrix.
61 */
62 static GLfloat Identity[16] = {
63 1.0, 0.0, 0.0, 0.0,
64 0.0, 1.0, 0.0, 0.0,
65 0.0, 0.0, 1.0, 0.0,
66 0.0, 0.0, 0.0, 1.0
67 };
68
69
70
71 /**********************************************************************/
72 /** \name Matrix multiplication */
73 /*@{*/
74
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]
78
79 /**
80 * Perform a full 4x4 matrix multiplication.
81 *
82 * \param a matrix.
83 * \param b matrix.
84 * \param product will receive the product of \p a and \p b.
85 *
86 * \warning Is assumed that \p product != \p b. \p product == \p a is allowed.
87 *
88 * \note KW: 4*16 = 64 multiplications
89 *
90 * \author This \c matmul was contributed by Thomas Malik
91 */
92 static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
93 {
94 GLint i;
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);
101 }
102 }
103
104 /**
105 * Multiply two matrices known to occupy only the top three rows, such
106 * as typical model matrices, and orthogonal matrices.
107 *
108 * \param a matrix.
109 * \param b matrix.
110 * \param product will receive the product of \p a and \p b.
111 */
112 static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
113 {
114 GLint i;
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;
121 }
122 P(3,0) = 0;
123 P(3,1) = 0;
124 P(3,2) = 0;
125 P(3,3) = 1;
126 }
127
128 #undef A
129 #undef B
130 #undef P
131
132 /**
133 * Multiply a matrix by an array of floats with known properties.
134 *
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.
139 *
140 * Joins both flags and marks the type and inverse as dirty. Calls matmul34()
141 * if both matrices are 3D, or matmul4() otherwise.
142 */
143 static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
144 {
145 mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
146
147 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
148 matmul34( mat->m, mat->m, m );
149 else
150 matmul4( mat->m, mat->m, m );
151 }
152
153 /**
154 * Matrix multiplication.
155 *
156 * \param dest destination matrix.
157 * \param a left matrix.
158 * \param b right matrix.
159 *
160 * Joins both flags and marks the type and inverse as dirty. Calls matmul34()
161 * if both matrices are 3D, or matmul4() otherwise.
162 */
163 void
164 _math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
165 {
166 dest->flags = (a->flags |
167 b->flags |
168 MAT_DIRTY_TYPE |
169 MAT_DIRTY_INVERSE);
170
171 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
172 matmul34( dest->m, a->m, b->m );
173 else
174 matmul4( dest->m, a->m, b->m );
175 }
176
177 /**
178 * Matrix multiplication.
179 *
180 * \param dest left and destination matrix.
181 * \param m right matrix array.
182 *
183 * Marks the matrix flags with general flag, and type and inverse dirty flags.
184 * Calls matmul4() for the multiplication.
185 */
186 void
187 _math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
188 {
189 dest->flags |= (MAT_FLAG_GENERAL |
190 MAT_DIRTY_TYPE |
191 MAT_DIRTY_INVERSE |
192 MAT_DIRTY_FLAGS);
193
194 matmul4( dest->m, dest->m, m );
195 }
196
197 /*@}*/
198
199
200 /**********************************************************************/
201 /** \name Matrix output */
202 /*@{*/
203
204 /**
205 * Print a matrix array.
206 *
207 * \param m matrix array.
208 *
209 * Called by _math_matrix_print() to print a matrix or its inverse.
210 */
211 static void print_matrix_floats( const GLfloat m[16] )
212 {
213 int i;
214 for (i=0;i<4;i++) {
215 _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
216 }
217 }
218
219 /**
220 * Dumps the contents of a GLmatrix structure.
221 *
222 * \param m pointer to the GLmatrix structure.
223 */
224 void
225 _math_matrix_print( const GLmatrix *m )
226 {
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");
230 if (m->inv) {
231 GLfloat prod[16];
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);
236 }
237 else {
238 _mesa_debug(NULL, " - not available\n");
239 }
240 }
241
242 /*@}*/
243
244
245 /**
246 * References an element of 4x4 matrix.
247 *
248 * \param m matrix array.
249 * \param c column of the desired element.
250 * \param r row of the desired element.
251 *
252 * \return value of the desired element.
253 *
254 * Calculate the linear storage index of the element and references it.
255 */
256 #define MAT(m,r,c) (m)[(c)*4+(r)]
257
258
259 /**********************************************************************/
260 /** \name Matrix inversion */
261 /*@{*/
262
263 /**
264 * Swaps the values of two floating pointer variables.
265 *
266 * Used by invert_matrix_general() to swap the row pointers.
267 */
268 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
269
270 /**
271 * Compute inverse of 4x4 transformation matrix.
272 *
273 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
274 * stored in the GLmatrix::inv attribute.
275 *
276 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
277 *
278 * \author
279 * Code contributed by Jacques Leroy jle@star.be
280 *
281 * Calculates the inverse matrix by performing the gaussian matrix reduction
282 * with partial pivoting followed by back/substitution with the loops manually
283 * unrolled.
284 */
285 static GLboolean invert_matrix_general( GLmatrix *mat )
286 {
287 const GLfloat *m = mat->m;
288 GLfloat *out = mat->inv;
289 GLfloat wtmp[4][8];
290 GLfloat m0, m1, m2, m3, s;
291 GLfloat *r0, *r1, *r2, *r3;
292
293 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
294
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,
298
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,
302
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,
306
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;
310
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;
316
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;
322 s = r0[4];
323 if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
324 s = r0[5];
325 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
326 s = r0[6];
327 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
328 s = r0[7];
329 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
330
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;
335
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; }
344
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;
348
349 /* eliminate third variable */
350 m3 = r3[2]/r2[2];
351 r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
352 r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
353 r3[7] -= m3 * r2[7];
354
355 /* last check */
356 if (0.0 == r3[3]) return GL_FALSE;
357
358 s = 1.0F/r3[3]; /* now back substitute row 3 */
359 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
360
361 m2 = r2[3]; /* now back substitute row 2 */
362 s = 1.0F/r2[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);
365 m1 = r1[3];
366 r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
367 r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
368 m0 = r0[3];
369 r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
370 r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
371
372 m1 = r1[2]; /* now back substitute row 1 */
373 s = 1.0F/r1[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);
376 m0 = r0[2];
377 r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
378 r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
379
380 m0 = r0[1]; /* now back substitute row 0 */
381 s = 1.0F/r0[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);
384
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];
393
394 return GL_TRUE;
395 }
396 #undef SWAP_ROWS
397
398 /**
399 * Compute inverse of a general 3d transformation matrix.
400 *
401 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
402 * stored in the GLmatrix::inv attribute.
403 *
404 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
405 *
406 * \author Adapted from graphics gems II.
407 *
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.
412 */
413 static GLboolean invert_matrix_3d_general( GLmatrix *mat )
414 {
415 const GLfloat *in = mat->m;
416 GLfloat *out = mat->inv;
417 GLfloat pos, neg, t;
418 GLfloat det;
419
420 /* Calculate the determinant of upper left 3x3 submatrix and
421 * determine if the matrix is singular.
422 */
423 pos = neg = 0.0;
424 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
425 if (t >= 0.0) pos += t; else neg += t;
426
427 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
428 if (t >= 0.0) pos += t; else neg += t;
429
430 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
431 if (t >= 0.0) pos += t; else neg += t;
432
433 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
434 if (t >= 0.0) pos += t; else neg += t;
435
436 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
437 if (t >= 0.0) pos += t; else neg += t;
438
439 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
440 if (t >= 0.0) pos += t; else neg += t;
441
442 det = pos + neg;
443
444 if (det*det < 1e-25)
445 return GL_FALSE;
446
447 det = 1.0F / det;
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);
457
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) );
468
469 return GL_TRUE;
470 }
471
472 /**
473 * Compute inverse of a 3d transformation matrix.
474 *
475 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
476 * stored in the GLmatrix::inv attribute.
477 *
478 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
479 *
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
483 * translation parts.
484 */
485 static GLboolean invert_matrix_3d( GLmatrix *mat )
486 {
487 const GLfloat *in = mat->m;
488 GLfloat *out = mat->inv;
489
490 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
491 return invert_matrix_3d_general( mat );
492 }
493
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));
498
499 if (scale == 0.0)
500 return GL_FALSE;
501
502 scale = 1.0F / scale;
503
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);
514 }
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);
526 }
527 else {
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);
533 return GL_TRUE;
534 }
535
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) );
547 }
548 else {
549 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
550 }
551
552 return GL_TRUE;
553 }
554
555 /**
556 * Compute inverse of an identity transformation matrix.
557 *
558 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
559 * stored in the GLmatrix::inv attribute.
560 *
561 * \return always GL_TRUE.
562 *
563 * Simply copies Identity into GLmatrix::inv.
564 */
565 static GLboolean invert_matrix_identity( GLmatrix *mat )
566 {
567 MEMCPY( mat->inv, Identity, sizeof(Identity) );
568 return GL_TRUE;
569 }
570
571 /**
572 * Compute inverse of a no-rotation 3d transformation matrix.
573 *
574 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
575 * stored in the GLmatrix::inv attribute.
576 *
577 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
578 *
579 * Calculates the
580 */
581 static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
582 {
583 const GLfloat *in = mat->m;
584 GLfloat *out = mat->inv;
585
586 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
587 return GL_FALSE;
588
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);
593
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));
598 }
599
600 return GL_TRUE;
601 }
602
603 /**
604 * Compute inverse of a no-rotation 2d transformation matrix.
605 *
606 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
607 * stored in the GLmatrix::inv attribute.
608 *
609 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
610 *
611 * Calculates the inverse matrix by applying the inverse scaling and
612 * translation to the identity matrix.
613 */
614 static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
615 {
616 const GLfloat *in = mat->m;
617 GLfloat *out = mat->inv;
618
619 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
620 return GL_FALSE;
621
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);
625
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));
629 }
630
631 return GL_TRUE;
632 }
633
634 #if 0
635 /* broken */
636 static GLboolean invert_matrix_perspective( GLmatrix *mat )
637 {
638 const GLfloat *in = mat->m;
639 GLfloat *out = mat->inv;
640
641 if (MAT(in,2,3) == 0)
642 return GL_FALSE;
643
644 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
645
646 MAT(out,0,0) = 1.0F / MAT(in,0,0);
647 MAT(out,1,1) = 1.0F / MAT(in,1,1);
648
649 MAT(out,0,3) = MAT(in,0,2);
650 MAT(out,1,3) = MAT(in,1,2);
651
652 MAT(out,2,2) = 0;
653 MAT(out,2,3) = -1;
654
655 MAT(out,3,2) = 1.0F / MAT(in,2,3);
656 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
657
658 return GL_TRUE;
659 }
660 #endif
661
662 /**
663 * Matrix inversion function pointer type.
664 */
665 typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
666
667 /**
668 * Table of the matrix inversion functions according to the matrix type.
669 */
670 static inv_mat_func inv_mat_tab[7] = {
671 invert_matrix_general,
672 invert_matrix_identity,
673 invert_matrix_3d_no_rot,
674 #if 0
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).
677 */
678 invert_matrix_perspective,
679 #else
680 invert_matrix_general,
681 #endif
682 invert_matrix_3d, /* lazy! */
683 invert_matrix_2d_no_rot,
684 invert_matrix_3d
685 };
686
687 /**
688 * Compute inverse of a transformation matrix.
689 *
690 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
691 * stored in the GLmatrix::inv attribute.
692 *
693 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
694 *
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.
698 */
699 static GLboolean matrix_invert( GLmatrix *mat )
700 {
701 if (inv_mat_tab[mat->type](mat)) {
702 mat->flags &= ~MAT_FLAG_SINGULAR;
703 return GL_TRUE;
704 } else {
705 mat->flags |= MAT_FLAG_SINGULAR;
706 MEMCPY( mat->inv, Identity, sizeof(Identity) );
707 return GL_FALSE;
708 }
709 }
710
711 /*@}*/
712
713
714 /**********************************************************************/
715 /** \name Matrix generation */
716 /*@{*/
717
718 /**
719 * Generate a 4x4 transformation matrix from glRotate parameters, and
720 * post-multiply the input matrix by it.
721 *
722 * \author
723 * This function was contributed by Erich Boleyn (erich@uruk.org).
724 * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
725 */
726 void
727 _math_matrix_rotate( GLmatrix *mat,
728 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
729 {
730 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
731 GLfloat m[16];
732 GLboolean optimized;
733
734 s = (GLfloat) sin( angle * DEG2RAD );
735 c = (GLfloat) cos( angle * DEG2RAD );
736
737 MEMCPY(m, Identity, sizeof(GLfloat)*16);
738 optimized = GL_FALSE;
739
740 #define M(row,col) m[col*4+row]
741
742 if (x == 0.0F) {
743 if (y == 0.0F) {
744 if (z != 0.0F) {
745 optimized = GL_TRUE;
746 /* rotate only around z-axis */
747 M(0,0) = c;
748 M(1,1) = c;
749 if (z < 0.0F) {
750 M(0,1) = s;
751 M(1,0) = -s;
752 }
753 else {
754 M(0,1) = -s;
755 M(1,0) = s;
756 }
757 }
758 }
759 else if (z == 0.0F) {
760 optimized = GL_TRUE;
761 /* rotate only around y-axis */
762 M(0,0) = c;
763 M(2,2) = c;
764 if (y < 0.0F) {
765 M(0,2) = -s;
766 M(2,0) = s;
767 }
768 else {
769 M(0,2) = s;
770 M(2,0) = -s;
771 }
772 }
773 }
774 else if (y == 0.0F) {
775 if (z == 0.0F) {
776 optimized = GL_TRUE;
777 /* rotate only around x-axis */
778 M(1,1) = c;
779 M(2,2) = c;
780 if (x < 0.0F) {
781 M(1,2) = s;
782 M(2,1) = -s;
783 }
784 else {
785 M(1,2) = -s;
786 M(2,1) = s;
787 }
788 }
789 }
790
791 if (!optimized) {
792 const GLfloat mag = SQRTF(x * x + y * y + z * z);
793
794 if (mag <= 1.0e-4) {
795 /* no rotation, leave mat as-is */
796 return;
797 }
798
799 x /= mag;
800 y /= mag;
801 z /= mag;
802
803
804 /*
805 * Arbitrary axis rotation matrix.
806 *
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.
813 *
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.
823 *
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)
829 * as follows:
830 *
831 * cos ( theta ) = x / sqrt ( 1 - z^2 )
832 * sin ( theta ) = y / sqrt ( 1 - z^2 )
833 *
834 * cos ( phi ) = sqrt ( 1 - z^2 )
835 * sin ( phi ) = z
836 *
837 * Note that cos ( phi ) can further be inserted to the above
838 * formulas:
839 *
840 * cos ( theta ) = x / cos ( phi )
841 * sin ( theta ) = y / sin ( phi )
842 *
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.
847 *
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.
855 */
856
857 xx = x * x;
858 yy = y * y;
859 zz = z * z;
860 xy = x * y;
861 yz = y * z;
862 zx = z * x;
863 xs = x * s;
864 ys = y * s;
865 zs = z * s;
866 one_c = 1.0F - c;
867
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;
872 /* M(0,3) = 0.0F; */
873
874 M(1,0) = (one_c * xy) + zs;
875 M(1,1) = (one_c * yy) + c;
876 M(1,2) = (one_c * yz) - xs;
877 /* M(1,3) = 0.0F; */
878
879 M(2,0) = (one_c * zx) - ys;
880 M(2,1) = (one_c * yz) + xs;
881 M(2,2) = (one_c * zz) + c;
882 /* M(2,3) = 0.0F; */
883
884 /*
885 M(3,0) = 0.0F;
886 M(3,1) = 0.0F;
887 M(3,2) = 0.0F;
888 M(3,3) = 1.0F;
889 */
890 }
891 #undef M
892
893 matrix_multf( mat, m, MAT_FLAG_ROTATION );
894 }
895
896 /**
897 * Apply a perspective projection matrix.
898 *
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.
906 *
907 * Creates the projection matrix and multiplies it with \p mat, marking the
908 * MAT_FLAG_PERSPECTIVE flag.
909 */
910 void
911 _math_matrix_frustum( GLmatrix *mat,
912 GLfloat left, GLfloat right,
913 GLfloat bottom, GLfloat top,
914 GLfloat nearval, GLfloat farval )
915 {
916 GLfloat x, y, a, b, c, d;
917 GLfloat m[16];
918
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? */
925
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;
931 #undef M
932
933 matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
934 }
935
936 /**
937 * Apply an orthographic projection matrix.
938 *
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.
946 *
947 * Creates the projection matrix and multiplies it with \p mat, marking the
948 * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
949 */
950 void
951 _math_matrix_ortho( GLmatrix *mat,
952 GLfloat left, GLfloat right,
953 GLfloat bottom, GLfloat top,
954 GLfloat nearval, GLfloat farval )
955 {
956 GLfloat m[16];
957
958 #define M(row,col) m[col*4+row]
959 M(0,0) = 2.0F / (right-left);
960 M(0,1) = 0.0F;
961 M(0,2) = 0.0F;
962 M(0,3) = -(right+left) / (right-left);
963
964 M(1,0) = 0.0F;
965 M(1,1) = 2.0F / (top-bottom);
966 M(1,2) = 0.0F;
967 M(1,3) = -(top+bottom) / (top-bottom);
968
969 M(2,0) = 0.0F;
970 M(2,1) = 0.0F;
971 M(2,2) = -2.0F / (farval-nearval);
972 M(2,3) = -(farval+nearval) / (farval-nearval);
973
974 M(3,0) = 0.0F;
975 M(3,1) = 0.0F;
976 M(3,2) = 0.0F;
977 M(3,3) = 1.0F;
978 #undef M
979
980 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
981 }
982
983 /**
984 * Multiply a matrix with a general scaling matrix.
985 *
986 * \param mat matrix.
987 * \param x x axis scale factor.
988 * \param y y axis scale factor.
989 * \param z z axis scale factor.
990 *
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.
995 */
996 void
997 _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
998 {
999 GLfloat *m = mat->m;
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;
1004
1005 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
1006 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1007 else
1008 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1009
1010 mat->flags |= (MAT_DIRTY_TYPE |
1011 MAT_DIRTY_INVERSE);
1012 }
1013
1014 /**
1015 * Multiply a matrix with a translation matrix.
1016 *
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.
1021 *
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
1024 * dirty flags.
1025 */
1026 void
1027 _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1028 {
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];
1034
1035 mat->flags |= (MAT_FLAG_TRANSLATION |
1036 MAT_DIRTY_TYPE |
1037 MAT_DIRTY_INVERSE);
1038 }
1039
1040 /**
1041 * Set a matrix to the identity matrix.
1042 *
1043 * \param mat matrix.
1044 *
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.
1047 */
1048 void
1049 _math_matrix_set_identity( GLmatrix *mat )
1050 {
1051 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1052
1053 if (mat->inv)
1054 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1055
1056 mat->type = MATRIX_IDENTITY;
1057 mat->flags &= ~(MAT_DIRTY_FLAGS|
1058 MAT_DIRTY_TYPE|
1059 MAT_DIRTY_INVERSE);
1060 }
1061
1062 /*@}*/
1063
1064
1065 /**********************************************************************/
1066 /** \name Matrix analysis */
1067 /*@{*/
1068
1069 #define ZERO(x) (1<<x)
1070 #define ONE(x) (1<<(x+16))
1071
1072 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
1073 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
1074
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) )
1079
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) )
1084
1085 #define MASK_2D ( ZERO(8) | \
1086 ZERO(9) | \
1087 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1088 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1089
1090
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) )
1095
1096 #define MASK_3D ( \
1097 \
1098 \
1099 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1100
1101
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) )
1106
1107 #define SQ(x) ((x)*(x))
1108
1109 /**
1110 * Determine type and flags from scratch.
1111 *
1112 * \param mat matrix.
1113 *
1114 * This is expensive enough to only want to do it once.
1115 */
1116 static void analyse_from_scratch( GLmatrix *mat )
1117 {
1118 const GLfloat *m = mat->m;
1119 GLuint mask = 0;
1120 GLuint i;
1121
1122 for (i = 0 ; i < 16 ; i++) {
1123 if (m[i] == 0.0) mask |= (1<<i);
1124 }
1125
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);
1130
1131 mat->flags &= ~MAT_FLAGS_GEOMETRY;
1132
1133 /* Check for translation - no-one really cares
1134 */
1135 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
1136 mat->flags |= MAT_FLAG_TRANSLATION;
1137
1138 /* Do the real work
1139 */
1140 if (mask == (GLuint) MASK_IDENTITY) {
1141 mat->type = MATRIX_IDENTITY;
1142 }
1143 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
1144 mat->type = MATRIX_2D_NO_ROT;
1145
1146 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
1147 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1148 }
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);
1153
1154 mat->type = MATRIX_2D;
1155
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;
1160
1161 /* Check for rotation */
1162 if (SQ(mm4) > SQ(1e-6))
1163 mat->flags |= MAT_FLAG_GENERAL_3D;
1164 else
1165 mat->flags |= MAT_FLAG_ROTATION;
1166
1167 }
1168 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
1169 mat->type = MATRIX_3D_NO_ROT;
1170
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;
1176 }
1177 }
1178 else {
1179 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1180 }
1181 }
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);
1187 GLfloat cp[3];
1188
1189 mat->type = MATRIX_3D;
1190
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 */
1196 }
1197 else {
1198 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1199 }
1200
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;
1207 else
1208 mat->flags |= MAT_FLAG_GENERAL_3D;
1209 }
1210 else {
1211 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
1212 }
1213 }
1214 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
1215 mat->type = MATRIX_PERSPECTIVE;
1216 mat->flags |= MAT_FLAG_GENERAL;
1217 }
1218 else {
1219 mat->type = MATRIX_GENERAL;
1220 mat->flags |= MAT_FLAG_GENERAL;
1221 }
1222 }
1223
1224 /**
1225 * Analyze a matrix given that its flags are accurate.
1226 *
1227 * This is the more common operation, hopefully.
1228 */
1229 static void analyse_from_flags( GLmatrix *mat )
1230 {
1231 const GLfloat *m = mat->m;
1232
1233 if (TEST_MAT_FLAGS(mat, 0)) {
1234 mat->type = MATRIX_IDENTITY;
1235 }
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;
1241 }
1242 else {
1243 mat->type = MATRIX_3D_NO_ROT;
1244 }
1245 }
1246 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
1247 if ( m[ 8]==0.0F
1248 && m[ 9]==0.0F
1249 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
1250 mat->type = MATRIX_2D;
1251 }
1252 else {
1253 mat->type = MATRIX_3D;
1254 }
1255 }
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;
1261 }
1262 else {
1263 mat->type = MATRIX_GENERAL;
1264 }
1265 }
1266
1267 /**
1268 * Analyze and update a matrix.
1269 *
1270 * \param mat matrix.
1271 *
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.
1276 */
1277 void
1278 _math_matrix_analyse( GLmatrix *mat )
1279 {
1280 if (mat->flags & MAT_DIRTY_TYPE) {
1281 if (mat->flags & MAT_DIRTY_FLAGS)
1282 analyse_from_scratch( mat );
1283 else
1284 analyse_from_flags( mat );
1285 }
1286
1287 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
1288 matrix_invert( mat );
1289 }
1290
1291 mat->flags &= ~(MAT_DIRTY_FLAGS|
1292 MAT_DIRTY_TYPE|
1293 MAT_DIRTY_INVERSE);
1294 }
1295
1296 /*@}*/
1297
1298
1299 /**********************************************************************/
1300 /** \name Matrix setup */
1301 /*@{*/
1302
1303 /**
1304 * Copy a matrix.
1305 *
1306 * \param to destination matrix.
1307 * \param from source matrix.
1308 *
1309 * Copies all fields in GLmatrix, creating an inverse array if necessary.
1310 */
1311 void
1312 _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
1313 {
1314 MEMCPY( to->m, from->m, sizeof(Identity) );
1315 to->flags = from->flags;
1316 to->type = from->type;
1317
1318 if (to->inv != 0) {
1319 if (from->inv == 0) {
1320 matrix_invert( to );
1321 }
1322 else {
1323 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
1324 }
1325 }
1326 }
1327
1328 /**
1329 * Loads a matrix array into GLmatrix.
1330 *
1331 * \param m matrix array.
1332 * \param mat matrix.
1333 *
1334 * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
1335 * flags.
1336 */
1337 void
1338 _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1339 {
1340 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1341 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1342 }
1343
1344 /**
1345 * Matrix constructor.
1346 *
1347 * \param m matrix.
1348 *
1349 * Initialize the GLmatrix fields.
1350 */
1351 void
1352 _math_matrix_ctr( GLmatrix *m )
1353 {
1354 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1355 if (m->m)
1356 MEMCPY( m->m, Identity, sizeof(Identity) );
1357 m->inv = NULL;
1358 m->type = MATRIX_IDENTITY;
1359 m->flags = 0;
1360 }
1361
1362 /**
1363 * Matrix destructor.
1364 *
1365 * \param m matrix.
1366 *
1367 * Frees the data in a GLmatrix.
1368 */
1369 void
1370 _math_matrix_dtr( GLmatrix *m )
1371 {
1372 if (m->m) {
1373 ALIGN_FREE( m->m );
1374 m->m = NULL;
1375 }
1376 if (m->inv) {
1377 ALIGN_FREE( m->inv );
1378 m->inv = NULL;
1379 }
1380 }
1381
1382 /**
1383 * Allocate a matrix inverse.
1384 *
1385 * \param m matrix.
1386 *
1387 * Allocates the matrix inverse, GLmatrix::inv, and sets it to Identity.
1388 */
1389 void
1390 _math_matrix_alloc_inv( GLmatrix *m )
1391 {
1392 if (!m->inv) {
1393 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1394 if (m->inv)
1395 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
1396 }
1397 }
1398
1399 /*@}*/
1400
1401
1402 /**********************************************************************/
1403 /** \name Matrix transpose */
1404 /*@{*/
1405
1406 /**
1407 * Transpose a GLfloat matrix.
1408 *
1409 * \param to destination array.
1410 * \param from source array.
1411 */
1412 void
1413 _math_transposef( GLfloat to[16], const GLfloat from[16] )
1414 {
1415 to[0] = from[0];
1416 to[1] = from[4];
1417 to[2] = from[8];
1418 to[3] = from[12];
1419 to[4] = from[1];
1420 to[5] = from[5];
1421 to[6] = from[9];
1422 to[7] = from[13];
1423 to[8] = from[2];
1424 to[9] = from[6];
1425 to[10] = from[10];
1426 to[11] = from[14];
1427 to[12] = from[3];
1428 to[13] = from[7];
1429 to[14] = from[11];
1430 to[15] = from[15];
1431 }
1432
1433 /**
1434 * Transpose a GLdouble matrix.
1435 *
1436 * \param to destination array.
1437 * \param from source array.
1438 */
1439 void
1440 _math_transposed( GLdouble to[16], const GLdouble from[16] )
1441 {
1442 to[0] = from[0];
1443 to[1] = from[4];
1444 to[2] = from[8];
1445 to[3] = from[12];
1446 to[4] = from[1];
1447 to[5] = from[5];
1448 to[6] = from[9];
1449 to[7] = from[13];
1450 to[8] = from[2];
1451 to[9] = from[6];
1452 to[10] = from[10];
1453 to[11] = from[14];
1454 to[12] = from[3];
1455 to[13] = from[7];
1456 to[14] = from[11];
1457 to[15] = from[15];
1458 }
1459
1460 /**
1461 * Transpose a GLdouble matrix and convert to GLfloat.
1462 *
1463 * \param to destination array.
1464 * \param from source array.
1465 */
1466 void
1467 _math_transposefd( GLfloat to[16], const GLdouble from[16] )
1468 {
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];
1485 }
1486
1487 /*@}*/
1488