fix a bug in analyse_from_scratch() reported by Wes Bethel
[mesa.git] / src / mesa / math / m_matrix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.2
4 *
5 * Copyright (C) 1999-2004 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
193 matmul4( dest->m, dest->m, m );
194 }
195
196 /*@}*/
197
198
199 /**********************************************************************/
200 /** \name Matrix output */
201 /*@{*/
202
203 /**
204 * Print a matrix array.
205 *
206 * \param m matrix array.
207 *
208 * Called by _math_matrix_print() to print a matrix or its inverse.
209 */
210 static void print_matrix_floats( const GLfloat m[16] )
211 {
212 int i;
213 for (i=0;i<4;i++) {
214 _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
215 }
216 }
217
218 /**
219 * Dumps the contents of a GLmatrix structure.
220 *
221 * \param m pointer to the GLmatrix structure.
222 */
223 void
224 _math_matrix_print( const GLmatrix *m )
225 {
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");
229 if (m->inv) {
230 GLfloat prod[16];
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);
235 }
236 else {
237 _mesa_debug(NULL, " - not available\n");
238 }
239 }
240
241 /*@}*/
242
243
244 /**
245 * References an element of 4x4 matrix.
246 *
247 * \param m matrix array.
248 * \param c column of the desired element.
249 * \param r row of the desired element.
250 *
251 * \return value of the desired element.
252 *
253 * Calculate the linear storage index of the element and references it.
254 */
255 #define MAT(m,r,c) (m)[(c)*4+(r)]
256
257
258 /**********************************************************************/
259 /** \name Matrix inversion */
260 /*@{*/
261
262 /**
263 * Swaps the values of two floating pointer variables.
264 *
265 * Used by invert_matrix_general() to swap the row pointers.
266 */
267 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
268
269 /**
270 * Compute inverse of 4x4 transformation matrix.
271 *
272 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
273 * stored in the GLmatrix::inv attribute.
274 *
275 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
276 *
277 * \author
278 * Code contributed by Jacques Leroy jle@star.be
279 *
280 * Calculates the inverse matrix by performing the gaussian matrix reduction
281 * with partial pivoting followed by back/substitution with the loops manually
282 * unrolled.
283 */
284 static GLboolean invert_matrix_general( GLmatrix *mat )
285 {
286 const GLfloat *m = mat->m;
287 GLfloat *out = mat->inv;
288 GLfloat wtmp[4][8];
289 GLfloat m0, m1, m2, m3, s;
290 GLfloat *r0, *r1, *r2, *r3;
291
292 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
293
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,
297
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,
301
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,
305
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;
309
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;
315
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;
321 s = r0[4];
322 if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
323 s = r0[5];
324 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
325 s = r0[6];
326 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
327 s = r0[7];
328 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
329
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;
334
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; }
343
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;
347
348 /* eliminate third variable */
349 m3 = r3[2]/r2[2];
350 r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
351 r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
352 r3[7] -= m3 * r2[7];
353
354 /* last check */
355 if (0.0 == r3[3]) return GL_FALSE;
356
357 s = 1.0F/r3[3]; /* now back substitute row 3 */
358 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
359
360 m2 = r2[3]; /* now back substitute row 2 */
361 s = 1.0F/r2[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);
364 m1 = r1[3];
365 r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
366 r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
367 m0 = r0[3];
368 r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
369 r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
370
371 m1 = r1[2]; /* now back substitute row 1 */
372 s = 1.0F/r1[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);
375 m0 = r0[2];
376 r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
377 r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
378
379 m0 = r0[1]; /* now back substitute row 0 */
380 s = 1.0F/r0[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);
383
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];
392
393 return GL_TRUE;
394 }
395 #undef SWAP_ROWS
396
397 /**
398 * Compute inverse of a general 3d transformation matrix.
399 *
400 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
401 * stored in the GLmatrix::inv attribute.
402 *
403 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
404 *
405 * \author Adapted from graphics gems II.
406 *
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.
411 */
412 static GLboolean invert_matrix_3d_general( GLmatrix *mat )
413 {
414 const GLfloat *in = mat->m;
415 GLfloat *out = mat->inv;
416 GLfloat pos, neg, t;
417 GLfloat det;
418
419 /* Calculate the determinant of upper left 3x3 submatrix and
420 * determine if the matrix is singular.
421 */
422 pos = neg = 0.0;
423 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
424 if (t >= 0.0) pos += t; else neg += t;
425
426 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
427 if (t >= 0.0) pos += t; else neg += t;
428
429 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
430 if (t >= 0.0) pos += t; else neg += t;
431
432 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
433 if (t >= 0.0) pos += t; else neg += t;
434
435 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
436 if (t >= 0.0) pos += t; else neg += t;
437
438 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
439 if (t >= 0.0) pos += t; else neg += t;
440
441 det = pos + neg;
442
443 if (det*det < 1e-25)
444 return GL_FALSE;
445
446 det = 1.0F / det;
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);
456
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) );
467
468 return GL_TRUE;
469 }
470
471 /**
472 * Compute inverse of a 3d transformation matrix.
473 *
474 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
475 * stored in the GLmatrix::inv attribute.
476 *
477 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
478 *
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
482 * translation parts.
483 */
484 static GLboolean invert_matrix_3d( GLmatrix *mat )
485 {
486 const GLfloat *in = mat->m;
487 GLfloat *out = mat->inv;
488
489 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
490 return invert_matrix_3d_general( mat );
491 }
492
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));
497
498 if (scale == 0.0)
499 return GL_FALSE;
500
501 scale = 1.0F / scale;
502
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);
513 }
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);
525 }
526 else {
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);
532 return GL_TRUE;
533 }
534
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) );
546 }
547 else {
548 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
549 }
550
551 return GL_TRUE;
552 }
553
554 /**
555 * Compute inverse of an identity transformation matrix.
556 *
557 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
558 * stored in the GLmatrix::inv attribute.
559 *
560 * \return always GL_TRUE.
561 *
562 * Simply copies Identity into GLmatrix::inv.
563 */
564 static GLboolean invert_matrix_identity( GLmatrix *mat )
565 {
566 MEMCPY( mat->inv, Identity, sizeof(Identity) );
567 return GL_TRUE;
568 }
569
570 /**
571 * Compute inverse of a no-rotation 3d transformation matrix.
572 *
573 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
574 * stored in the GLmatrix::inv attribute.
575 *
576 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
577 *
578 * Calculates the
579 */
580 static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
581 {
582 const GLfloat *in = mat->m;
583 GLfloat *out = mat->inv;
584
585 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
586 return GL_FALSE;
587
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);
592
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));
597 }
598
599 return GL_TRUE;
600 }
601
602 /**
603 * Compute inverse of a no-rotation 2d transformation matrix.
604 *
605 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
606 * stored in the GLmatrix::inv attribute.
607 *
608 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
609 *
610 * Calculates the inverse matrix by applying the inverse scaling and
611 * translation to the identity matrix.
612 */
613 static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
614 {
615 const GLfloat *in = mat->m;
616 GLfloat *out = mat->inv;
617
618 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
619 return GL_FALSE;
620
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);
624
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));
628 }
629
630 return GL_TRUE;
631 }
632
633 #if 0
634 /* broken */
635 static GLboolean invert_matrix_perspective( GLmatrix *mat )
636 {
637 const GLfloat *in = mat->m;
638 GLfloat *out = mat->inv;
639
640 if (MAT(in,2,3) == 0)
641 return GL_FALSE;
642
643 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
644
645 MAT(out,0,0) = 1.0F / MAT(in,0,0);
646 MAT(out,1,1) = 1.0F / MAT(in,1,1);
647
648 MAT(out,0,3) = MAT(in,0,2);
649 MAT(out,1,3) = MAT(in,1,2);
650
651 MAT(out,2,2) = 0;
652 MAT(out,2,3) = -1;
653
654 MAT(out,3,2) = 1.0F / MAT(in,2,3);
655 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
656
657 return GL_TRUE;
658 }
659 #endif
660
661 /**
662 * Matrix inversion function pointer type.
663 */
664 typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
665
666 /**
667 * Table of the matrix inversion functions according to the matrix type.
668 */
669 static inv_mat_func inv_mat_tab[7] = {
670 invert_matrix_general,
671 invert_matrix_identity,
672 invert_matrix_3d_no_rot,
673 #if 0
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).
676 */
677 invert_matrix_perspective,
678 #else
679 invert_matrix_general,
680 #endif
681 invert_matrix_3d, /* lazy! */
682 invert_matrix_2d_no_rot,
683 invert_matrix_3d
684 };
685
686 /**
687 * Compute inverse of a transformation matrix.
688 *
689 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
690 * stored in the GLmatrix::inv attribute.
691 *
692 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
693 *
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.
697 */
698 static GLboolean matrix_invert( GLmatrix *mat )
699 {
700 if (inv_mat_tab[mat->type](mat)) {
701 mat->flags &= ~MAT_FLAG_SINGULAR;
702 return GL_TRUE;
703 } else {
704 mat->flags |= MAT_FLAG_SINGULAR;
705 MEMCPY( mat->inv, Identity, sizeof(Identity) );
706 return GL_FALSE;
707 }
708 }
709
710 /*@}*/
711
712
713 /**********************************************************************/
714 /** \name Matrix generation */
715 /*@{*/
716
717 /**
718 * Generate a 4x4 transformation matrix from glRotate parameters, and
719 * post-multiply the input matrix by it.
720 *
721 * \author
722 * This function was contributed by Erich Boleyn (erich@uruk.org).
723 * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
724 */
725 void
726 _math_matrix_rotate( GLmatrix *mat,
727 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
728 {
729 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
730 GLfloat m[16];
731 GLboolean optimized;
732
733 s = (GLfloat) sin( angle * DEG2RAD );
734 c = (GLfloat) cos( angle * DEG2RAD );
735
736 MEMCPY(m, Identity, sizeof(GLfloat)*16);
737 optimized = GL_FALSE;
738
739 #define M(row,col) m[col*4+row]
740
741 if (x == 0.0F) {
742 if (y == 0.0F) {
743 if (z != 0.0F) {
744 optimized = GL_TRUE;
745 /* rotate only around z-axis */
746 M(0,0) = c;
747 M(1,1) = c;
748 if (z < 0.0F) {
749 M(0,1) = s;
750 M(1,0) = -s;
751 }
752 else {
753 M(0,1) = -s;
754 M(1,0) = s;
755 }
756 }
757 }
758 else if (z == 0.0F) {
759 optimized = GL_TRUE;
760 /* rotate only around y-axis */
761 M(0,0) = c;
762 M(2,2) = c;
763 if (y < 0.0F) {
764 M(0,2) = -s;
765 M(2,0) = s;
766 }
767 else {
768 M(0,2) = s;
769 M(2,0) = -s;
770 }
771 }
772 }
773 else if (y == 0.0F) {
774 if (z == 0.0F) {
775 optimized = GL_TRUE;
776 /* rotate only around x-axis */
777 M(1,1) = c;
778 M(2,2) = c;
779 if (x < 0.0F) {
780 M(1,2) = s;
781 M(2,1) = -s;
782 }
783 else {
784 M(1,2) = -s;
785 M(2,1) = s;
786 }
787 }
788 }
789
790 if (!optimized) {
791 const GLfloat mag = SQRTF(x * x + y * y + z * z);
792
793 if (mag <= 1.0e-4) {
794 /* no rotation, leave mat as-is */
795 return;
796 }
797
798 x /= mag;
799 y /= mag;
800 z /= mag;
801
802
803 /*
804 * Arbitrary axis rotation matrix.
805 *
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.
812 *
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.
822 *
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)
828 * as follows:
829 *
830 * cos ( theta ) = x / sqrt ( 1 - z^2 )
831 * sin ( theta ) = y / sqrt ( 1 - z^2 )
832 *
833 * cos ( phi ) = sqrt ( 1 - z^2 )
834 * sin ( phi ) = z
835 *
836 * Note that cos ( phi ) can further be inserted to the above
837 * formulas:
838 *
839 * cos ( theta ) = x / cos ( phi )
840 * sin ( theta ) = y / sin ( phi )
841 *
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.
846 *
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.
854 */
855
856 xx = x * x;
857 yy = y * y;
858 zz = z * z;
859 xy = x * y;
860 yz = y * z;
861 zx = z * x;
862 xs = x * s;
863 ys = y * s;
864 zs = z * s;
865 one_c = 1.0F - c;
866
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;
871 /* M(0,3) = 0.0F; */
872
873 M(1,0) = (one_c * xy) + zs;
874 M(1,1) = (one_c * yy) + c;
875 M(1,2) = (one_c * yz) - xs;
876 /* M(1,3) = 0.0F; */
877
878 M(2,0) = (one_c * zx) - ys;
879 M(2,1) = (one_c * yz) + xs;
880 M(2,2) = (one_c * zz) + c;
881 /* M(2,3) = 0.0F; */
882
883 /*
884 M(3,0) = 0.0F;
885 M(3,1) = 0.0F;
886 M(3,2) = 0.0F;
887 M(3,3) = 1.0F;
888 */
889 }
890 #undef M
891
892 matrix_multf( mat, m, MAT_FLAG_ROTATION );
893 }
894
895 /**
896 * Apply a perspective projection matrix.
897 *
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.
905 *
906 * Creates the projection matrix and multiplies it with \p mat, marking the
907 * MAT_FLAG_PERSPECTIVE flag.
908 */
909 void
910 _math_matrix_frustum( GLmatrix *mat,
911 GLfloat left, GLfloat right,
912 GLfloat bottom, GLfloat top,
913 GLfloat nearval, GLfloat farval )
914 {
915 GLfloat x, y, a, b, c, d;
916 GLfloat m[16];
917
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? */
924
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;
930 #undef M
931
932 matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
933 }
934
935 /**
936 * Apply an orthographic projection matrix.
937 *
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.
945 *
946 * Creates the projection matrix and multiplies it with \p mat, marking the
947 * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
948 */
949 void
950 _math_matrix_ortho( GLmatrix *mat,
951 GLfloat left, GLfloat right,
952 GLfloat bottom, GLfloat top,
953 GLfloat nearval, GLfloat farval )
954 {
955 GLfloat m[16];
956
957 #define M(row,col) m[col*4+row]
958 M(0,0) = 2.0F / (right-left);
959 M(0,1) = 0.0F;
960 M(0,2) = 0.0F;
961 M(0,3) = -(right+left) / (right-left);
962
963 M(1,0) = 0.0F;
964 M(1,1) = 2.0F / (top-bottom);
965 M(1,2) = 0.0F;
966 M(1,3) = -(top+bottom) / (top-bottom);
967
968 M(2,0) = 0.0F;
969 M(2,1) = 0.0F;
970 M(2,2) = -2.0F / (farval-nearval);
971 M(2,3) = -(farval+nearval) / (farval-nearval);
972
973 M(3,0) = 0.0F;
974 M(3,1) = 0.0F;
975 M(3,2) = 0.0F;
976 M(3,3) = 1.0F;
977 #undef M
978
979 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
980 }
981
982 /**
983 * Multiply a matrix with a general scaling matrix.
984 *
985 * \param mat matrix.
986 * \param x x axis scale factor.
987 * \param y y axis scale factor.
988 * \param z z axis scale factor.
989 *
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.
994 */
995 void
996 _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
997 {
998 GLfloat *m = mat->m;
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;
1003
1004 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
1005 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1006 else
1007 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1008
1009 mat->flags |= (MAT_DIRTY_TYPE |
1010 MAT_DIRTY_INVERSE);
1011 }
1012
1013 /**
1014 * Multiply a matrix with a translation matrix.
1015 *
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.
1020 *
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
1023 * dirty flags.
1024 */
1025 void
1026 _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1027 {
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];
1033
1034 mat->flags |= (MAT_FLAG_TRANSLATION |
1035 MAT_DIRTY_TYPE |
1036 MAT_DIRTY_INVERSE);
1037 }
1038
1039 /**
1040 * Set a matrix to the identity matrix.
1041 *
1042 * \param mat matrix.
1043 *
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.
1046 */
1047 void
1048 _math_matrix_set_identity( GLmatrix *mat )
1049 {
1050 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1051
1052 if (mat->inv)
1053 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1054
1055 mat->type = MATRIX_IDENTITY;
1056 mat->flags &= ~(MAT_DIRTY_FLAGS|
1057 MAT_DIRTY_TYPE|
1058 MAT_DIRTY_INVERSE);
1059 }
1060
1061 /*@}*/
1062
1063
1064 /**********************************************************************/
1065 /** \name Matrix analysis */
1066 /*@{*/
1067
1068 #define ZERO(x) (1<<x)
1069 #define ONE(x) (1<<(x+16))
1070
1071 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
1072 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
1073
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) )
1078
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) )
1083
1084 #define MASK_2D ( ZERO(8) | \
1085 ZERO(9) | \
1086 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1087 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1088
1089
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) )
1094
1095 #define MASK_3D ( \
1096 \
1097 \
1098 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1099
1100
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) )
1105
1106 #define SQ(x) ((x)*(x))
1107
1108 /**
1109 * Determine type and flags from scratch.
1110 *
1111 * \param mat matrix.
1112 *
1113 * This is expensive enough to only want to do it once.
1114 */
1115 static void analyse_from_scratch( GLmatrix *mat )
1116 {
1117 const GLfloat *m = mat->m;
1118 GLuint mask = 0;
1119 GLuint i;
1120
1121 for (i = 0 ; i < 16 ; i++) {
1122 if (m[i] == 0.0) mask |= (1<<i);
1123 }
1124
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);
1129
1130 mat->flags &= ~MAT_FLAGS_GEOMETRY;
1131
1132 /* Check for translation - no-one really cares
1133 */
1134 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
1135 mat->flags |= MAT_FLAG_TRANSLATION;
1136
1137 /* Do the real work
1138 */
1139 if (mask == (GLuint) MASK_IDENTITY) {
1140 mat->type = MATRIX_IDENTITY;
1141 }
1142 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
1143 mat->type = MATRIX_2D_NO_ROT;
1144
1145 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
1146 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1147 }
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);
1152
1153 mat->type = MATRIX_2D;
1154
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;
1159
1160 /* Check for rotation */
1161 if (SQ(mm4) > SQ(1e-6))
1162 mat->flags |= MAT_FLAG_GENERAL_3D;
1163 else
1164 mat->flags |= MAT_FLAG_ROTATION;
1165
1166 }
1167 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
1168 mat->type = MATRIX_3D_NO_ROT;
1169
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;
1175 }
1176 }
1177 else {
1178 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1179 }
1180 }
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);
1186 GLfloat cp[3];
1187
1188 mat->type = MATRIX_3D;
1189
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 */
1195 }
1196 else {
1197 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1198 }
1199
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;
1206 else
1207 mat->flags |= MAT_FLAG_GENERAL_3D;
1208 }
1209 else {
1210 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
1211 }
1212 }
1213 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
1214 mat->type = MATRIX_PERSPECTIVE;
1215 mat->flags |= MAT_FLAG_GENERAL;
1216 }
1217 else {
1218 mat->type = MATRIX_GENERAL;
1219 mat->flags |= MAT_FLAG_GENERAL;
1220 }
1221 }
1222
1223 /**
1224 * Analyze a matrix given that its flags are accurate.
1225 *
1226 * This is the more common operation, hopefully.
1227 */
1228 static void analyse_from_flags( GLmatrix *mat )
1229 {
1230 const GLfloat *m = mat->m;
1231
1232 if (TEST_MAT_FLAGS(mat, 0)) {
1233 mat->type = MATRIX_IDENTITY;
1234 }
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;
1240 }
1241 else {
1242 mat->type = MATRIX_3D_NO_ROT;
1243 }
1244 }
1245 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
1246 if ( m[ 8]==0.0F
1247 && m[ 9]==0.0F
1248 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
1249 mat->type = MATRIX_2D;
1250 }
1251 else {
1252 mat->type = MATRIX_3D;
1253 }
1254 }
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;
1260 }
1261 else {
1262 mat->type = MATRIX_GENERAL;
1263 }
1264 }
1265
1266 /**
1267 * Analyze and update a matrix.
1268 *
1269 * \param mat matrix.
1270 *
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.
1275 */
1276 void
1277 _math_matrix_analyse( GLmatrix *mat )
1278 {
1279 if (mat->flags & MAT_DIRTY_TYPE) {
1280 if (mat->flags & MAT_DIRTY_FLAGS)
1281 analyse_from_scratch( mat );
1282 else
1283 analyse_from_flags( mat );
1284 }
1285
1286 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
1287 matrix_invert( mat );
1288 }
1289
1290 mat->flags &= ~(MAT_DIRTY_FLAGS|
1291 MAT_DIRTY_TYPE|
1292 MAT_DIRTY_INVERSE);
1293 }
1294
1295 /*@}*/
1296
1297
1298 /**********************************************************************/
1299 /** \name Matrix setup */
1300 /*@{*/
1301
1302 /**
1303 * Copy a matrix.
1304 *
1305 * \param to destination matrix.
1306 * \param from source matrix.
1307 *
1308 * Copies all fields in GLmatrix, creating an inverse array if necessary.
1309 */
1310 void
1311 _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
1312 {
1313 MEMCPY( to->m, from->m, sizeof(Identity) );
1314 to->flags = from->flags;
1315 to->type = from->type;
1316
1317 if (to->inv != 0) {
1318 if (from->inv == 0) {
1319 matrix_invert( to );
1320 }
1321 else {
1322 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
1323 }
1324 }
1325 }
1326
1327 /**
1328 * Loads a matrix array into GLmatrix.
1329 *
1330 * \param m matrix array.
1331 * \param mat matrix.
1332 *
1333 * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
1334 * flags.
1335 */
1336 void
1337 _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1338 {
1339 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1340 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1341 }
1342
1343 /**
1344 * Matrix constructor.
1345 *
1346 * \param m matrix.
1347 *
1348 * Initialize the GLmatrix fields.
1349 */
1350 void
1351 _math_matrix_ctr( GLmatrix *m )
1352 {
1353 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1354 if (m->m)
1355 MEMCPY( m->m, Identity, sizeof(Identity) );
1356 m->inv = NULL;
1357 m->type = MATRIX_IDENTITY;
1358 m->flags = 0;
1359 }
1360
1361 /**
1362 * Matrix destructor.
1363 *
1364 * \param m matrix.
1365 *
1366 * Frees the data in a GLmatrix.
1367 */
1368 void
1369 _math_matrix_dtr( GLmatrix *m )
1370 {
1371 if (m->m) {
1372 ALIGN_FREE( m->m );
1373 m->m = NULL;
1374 }
1375 if (m->inv) {
1376 ALIGN_FREE( m->inv );
1377 m->inv = NULL;
1378 }
1379 }
1380
1381 /**
1382 * Allocate a matrix inverse.
1383 *
1384 * \param m matrix.
1385 *
1386 * Allocates the matrix inverse, GLmatrix::inv, and sets it to Identity.
1387 */
1388 void
1389 _math_matrix_alloc_inv( GLmatrix *m )
1390 {
1391 if (!m->inv) {
1392 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1393 if (m->inv)
1394 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
1395 }
1396 }
1397
1398 /*@}*/
1399
1400
1401 /**********************************************************************/
1402 /** \name Matrix transpose */
1403 /*@{*/
1404
1405 /**
1406 * Transpose a GLfloat matrix.
1407 *
1408 * \param to destination array.
1409 * \param from source array.
1410 */
1411 void
1412 _math_transposef( GLfloat to[16], const GLfloat from[16] )
1413 {
1414 to[0] = from[0];
1415 to[1] = from[4];
1416 to[2] = from[8];
1417 to[3] = from[12];
1418 to[4] = from[1];
1419 to[5] = from[5];
1420 to[6] = from[9];
1421 to[7] = from[13];
1422 to[8] = from[2];
1423 to[9] = from[6];
1424 to[10] = from[10];
1425 to[11] = from[14];
1426 to[12] = from[3];
1427 to[13] = from[7];
1428 to[14] = from[11];
1429 to[15] = from[15];
1430 }
1431
1432 /**
1433 * Transpose a GLdouble matrix.
1434 *
1435 * \param to destination array.
1436 * \param from source array.
1437 */
1438 void
1439 _math_transposed( GLdouble to[16], const GLdouble from[16] )
1440 {
1441 to[0] = from[0];
1442 to[1] = from[4];
1443 to[2] = from[8];
1444 to[3] = from[12];
1445 to[4] = from[1];
1446 to[5] = from[5];
1447 to[6] = from[9];
1448 to[7] = from[13];
1449 to[8] = from[2];
1450 to[9] = from[6];
1451 to[10] = from[10];
1452 to[11] = from[14];
1453 to[12] = from[3];
1454 to[13] = from[7];
1455 to[14] = from[11];
1456 to[15] = from[15];
1457 }
1458
1459 /**
1460 * Transpose a GLdouble matrix and convert to GLfloat.
1461 *
1462 * \param to destination array.
1463 * \param from source array.
1464 */
1465 void
1466 _math_transposefd( GLfloat to[16], const GLdouble from[16] )
1467 {
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];
1484 }
1485
1486 /*@}*/
1487