mesa: Replace gen_matypes with a simple header for V4F/mat layout.
[mesa.git] / src / mesa / math / m_matrix.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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 "c99_math.h"
38 #include "main/errors.h"
39 #include "main/glheader.h"
40 #include "main/imports.h"
41 #include "main/macros.h"
42 #define MATH_ASM_PTR_SIZE sizeof(void *)
43 #include "math/m_vector_asm.h"
44
45 #include "m_matrix.h"
46
47
48 /**
49 * \defgroup MatFlags MAT_FLAG_XXX-flags
50 *
51 * Bitmasks to indicate different kinds of 4x4 matrices in GLmatrix::flags
52 */
53 /*@{*/
54 #define MAT_FLAG_IDENTITY 0 /**< is an identity matrix flag.
55 * (Not actually used - the identity
56 * matrix is identified by the absence
57 * of all other flags.)
58 */
59 #define MAT_FLAG_GENERAL 0x1 /**< is a general matrix flag */
60 #define MAT_FLAG_ROTATION 0x2 /**< is a rotation matrix flag */
61 #define MAT_FLAG_TRANSLATION 0x4 /**< is a translation matrix flag */
62 #define MAT_FLAG_UNIFORM_SCALE 0x8 /**< is an uniform scaling matrix flag */
63 #define MAT_FLAG_GENERAL_SCALE 0x10 /**< is a general scaling matrix flag */
64 #define MAT_FLAG_GENERAL_3D 0x20 /**< general 3D matrix flag */
65 #define MAT_FLAG_PERSPECTIVE 0x40 /**< is a perspective proj matrix flag */
66 #define MAT_FLAG_SINGULAR 0x80 /**< is a singular matrix flag */
67 #define MAT_DIRTY_TYPE 0x100 /**< matrix type is dirty */
68 #define MAT_DIRTY_FLAGS 0x200 /**< matrix flags are dirty */
69 #define MAT_DIRTY_INVERSE 0x400 /**< matrix inverse is dirty */
70
71 /** angle preserving matrix flags mask */
72 #define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \
73 MAT_FLAG_TRANSLATION | \
74 MAT_FLAG_UNIFORM_SCALE)
75
76 /** geometry related matrix flags mask */
77 #define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \
78 MAT_FLAG_ROTATION | \
79 MAT_FLAG_TRANSLATION | \
80 MAT_FLAG_UNIFORM_SCALE | \
81 MAT_FLAG_GENERAL_SCALE | \
82 MAT_FLAG_GENERAL_3D | \
83 MAT_FLAG_PERSPECTIVE | \
84 MAT_FLAG_SINGULAR)
85
86 /** length preserving matrix flags mask */
87 #define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \
88 MAT_FLAG_TRANSLATION)
89
90
91 /** 3D (non-perspective) matrix flags mask */
92 #define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \
93 MAT_FLAG_TRANSLATION | \
94 MAT_FLAG_UNIFORM_SCALE | \
95 MAT_FLAG_GENERAL_SCALE | \
96 MAT_FLAG_GENERAL_3D)
97
98 /** dirty matrix flags mask */
99 #define MAT_DIRTY (MAT_DIRTY_TYPE | \
100 MAT_DIRTY_FLAGS | \
101 MAT_DIRTY_INVERSE)
102
103 /*@}*/
104
105
106 /**
107 * Test geometry related matrix flags.
108 *
109 * \param mat a pointer to a GLmatrix structure.
110 * \param a flags mask.
111 *
112 * \returns non-zero if all geometry related matrix flags are contained within
113 * the mask, or zero otherwise.
114 */
115 #define TEST_MAT_FLAGS(mat, a) \
116 ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)
117
118
119
120 /**
121 * Names of the corresponding GLmatrixtype values.
122 */
123 static const char *types[] = {
124 "MATRIX_GENERAL",
125 "MATRIX_IDENTITY",
126 "MATRIX_3D_NO_ROT",
127 "MATRIX_PERSPECTIVE",
128 "MATRIX_2D",
129 "MATRIX_2D_NO_ROT",
130 "MATRIX_3D"
131 };
132
133
134 /**
135 * Identity matrix.
136 */
137 static const GLfloat Identity[16] = {
138 1.0, 0.0, 0.0, 0.0,
139 0.0, 1.0, 0.0, 0.0,
140 0.0, 0.0, 1.0, 0.0,
141 0.0, 0.0, 0.0, 1.0
142 };
143
144
145
146 /**********************************************************************/
147 /** \name Matrix multiplication */
148 /*@{*/
149
150 #define A(row,col) a[(col<<2)+row]
151 #define B(row,col) b[(col<<2)+row]
152 #define P(row,col) product[(col<<2)+row]
153
154 /**
155 * Perform a full 4x4 matrix multiplication.
156 *
157 * \param a matrix.
158 * \param b matrix.
159 * \param product will receive the product of \p a and \p b.
160 *
161 * \warning Is assumed that \p product != \p b. \p product == \p a is allowed.
162 *
163 * \note KW: 4*16 = 64 multiplications
164 *
165 * \author This \c matmul was contributed by Thomas Malik
166 */
167 static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
168 {
169 GLint i;
170 for (i = 0; i < 4; i++) {
171 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
172 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
173 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
174 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
175 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
176 }
177 }
178
179 /**
180 * Multiply two matrices known to occupy only the top three rows, such
181 * as typical model matrices, and orthogonal matrices.
182 *
183 * \param a matrix.
184 * \param b matrix.
185 * \param product will receive the product of \p a and \p b.
186 */
187 static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
188 {
189 GLint i;
190 for (i = 0; i < 3; i++) {
191 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
192 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
193 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
194 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
195 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
196 }
197 P(3,0) = 0;
198 P(3,1) = 0;
199 P(3,2) = 0;
200 P(3,3) = 1;
201 }
202
203 #undef A
204 #undef B
205 #undef P
206
207 /**
208 * Multiply a matrix by an array of floats with known properties.
209 *
210 * \param mat pointer to a GLmatrix structure containing the left multiplication
211 * matrix, and that will receive the product result.
212 * \param m right multiplication matrix array.
213 * \param flags flags of the matrix \p m.
214 *
215 * Joins both flags and marks the type and inverse as dirty. Calls matmul34()
216 * if both matrices are 3D, or matmul4() otherwise.
217 */
218 static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
219 {
220 mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
221
222 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
223 matmul34( mat->m, mat->m, m );
224 else
225 matmul4( mat->m, mat->m, m );
226 }
227
228 /**
229 * Matrix multiplication.
230 *
231 * \param dest destination matrix.
232 * \param a left matrix.
233 * \param b right matrix.
234 *
235 * Joins both flags and marks the type and inverse as dirty. Calls matmul34()
236 * if both matrices are 3D, or matmul4() otherwise.
237 */
238 void
239 _math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
240 {
241 dest->flags = (a->flags |
242 b->flags |
243 MAT_DIRTY_TYPE |
244 MAT_DIRTY_INVERSE);
245
246 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
247 matmul34( dest->m, a->m, b->m );
248 else
249 matmul4( dest->m, a->m, b->m );
250 }
251
252 /**
253 * Matrix multiplication.
254 *
255 * \param dest left and destination matrix.
256 * \param m right matrix array.
257 *
258 * Marks the matrix flags with general flag, and type and inverse dirty flags.
259 * Calls matmul4() for the multiplication.
260 */
261 void
262 _math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
263 {
264 dest->flags |= (MAT_FLAG_GENERAL |
265 MAT_DIRTY_TYPE |
266 MAT_DIRTY_INVERSE |
267 MAT_DIRTY_FLAGS);
268
269 matmul4( dest->m, dest->m, m );
270 }
271
272 /*@}*/
273
274
275 /**********************************************************************/
276 /** \name Matrix output */
277 /*@{*/
278
279 /**
280 * Print a matrix array.
281 *
282 * \param m matrix array.
283 *
284 * Called by _math_matrix_print() to print a matrix or its inverse.
285 */
286 static void print_matrix_floats( const GLfloat m[16] )
287 {
288 int i;
289 for (i=0;i<4;i++) {
290 _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
291 }
292 }
293
294 /**
295 * Dumps the contents of a GLmatrix structure.
296 *
297 * \param m pointer to the GLmatrix structure.
298 */
299 void
300 _math_matrix_print( const GLmatrix *m )
301 {
302 GLfloat prod[16];
303
304 _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
305 print_matrix_floats(m->m);
306 _mesa_debug(NULL, "Inverse: \n");
307 print_matrix_floats(m->inv);
308 matmul4(prod, m->m, m->inv);
309 _mesa_debug(NULL, "Mat * Inverse:\n");
310 print_matrix_floats(prod);
311 }
312
313 /*@}*/
314
315
316 /**
317 * References an element of 4x4 matrix.
318 *
319 * \param m matrix array.
320 * \param c column of the desired element.
321 * \param r row of the desired element.
322 *
323 * \return value of the desired element.
324 *
325 * Calculate the linear storage index of the element and references it.
326 */
327 #define MAT(m,r,c) (m)[(c)*4+(r)]
328
329
330 /**********************************************************************/
331 /** \name Matrix inversion */
332 /*@{*/
333
334 /**
335 * Swaps the values of two floating point variables.
336 *
337 * Used by invert_matrix_general() to swap the row pointers.
338 */
339 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
340
341 /**
342 * Compute inverse of 4x4 transformation matrix.
343 *
344 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
345 * stored in the GLmatrix::inv attribute.
346 *
347 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
348 *
349 * \author
350 * Code contributed by Jacques Leroy jle@star.be
351 *
352 * Calculates the inverse matrix by performing the gaussian matrix reduction
353 * with partial pivoting followed by back/substitution with the loops manually
354 * unrolled.
355 */
356 static GLboolean invert_matrix_general( GLmatrix *mat )
357 {
358 const GLfloat *m = mat->m;
359 GLfloat *out = mat->inv;
360 GLfloat wtmp[4][8];
361 GLfloat m0, m1, m2, m3, s;
362 GLfloat *r0, *r1, *r2, *r3;
363
364 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
365
366 r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
367 r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
368 r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
369
370 r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
371 r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
372 r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
373
374 r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
375 r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
376 r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
377
378 r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
379 r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
380 r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
381
382 /* choose pivot - or die */
383 if (fabsf(r3[0])>fabsf(r2[0])) SWAP_ROWS(r3, r2);
384 if (fabsf(r2[0])>fabsf(r1[0])) SWAP_ROWS(r2, r1);
385 if (fabsf(r1[0])>fabsf(r0[0])) SWAP_ROWS(r1, r0);
386 if (0.0F == r0[0]) return GL_FALSE;
387
388 /* eliminate first variable */
389 m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
390 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
391 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
392 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
393 s = r0[4];
394 if (s != 0.0F) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
395 s = r0[5];
396 if (s != 0.0F) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
397 s = r0[6];
398 if (s != 0.0F) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
399 s = r0[7];
400 if (s != 0.0F) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
401
402 /* choose pivot - or die */
403 if (fabsf(r3[1])>fabsf(r2[1])) SWAP_ROWS(r3, r2);
404 if (fabsf(r2[1])>fabsf(r1[1])) SWAP_ROWS(r2, r1);
405 if (0.0F == r1[1]) return GL_FALSE;
406
407 /* eliminate second variable */
408 m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
409 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
410 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
411 s = r1[4]; if (0.0F != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
412 s = r1[5]; if (0.0F != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
413 s = r1[6]; if (0.0F != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
414 s = r1[7]; if (0.0F != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
415
416 /* choose pivot - or die */
417 if (fabsf(r3[2])>fabsf(r2[2])) SWAP_ROWS(r3, r2);
418 if (0.0F == r2[2]) return GL_FALSE;
419
420 /* eliminate third variable */
421 m3 = r3[2]/r2[2];
422 r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
423 r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
424 r3[7] -= m3 * r2[7];
425
426 /* last check */
427 if (0.0F == r3[3]) return GL_FALSE;
428
429 s = 1.0F/r3[3]; /* now back substitute row 3 */
430 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
431
432 m2 = r2[3]; /* now back substitute row 2 */
433 s = 1.0F/r2[2];
434 r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
435 r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
436 m1 = r1[3];
437 r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
438 r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
439 m0 = r0[3];
440 r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
441 r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
442
443 m1 = r1[2]; /* now back substitute row 1 */
444 s = 1.0F/r1[1];
445 r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
446 r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
447 m0 = r0[2];
448 r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
449 r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
450
451 m0 = r0[1]; /* now back substitute row 0 */
452 s = 1.0F/r0[0];
453 r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
454 r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
455
456 MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
457 MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
458 MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
459 MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
460 MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
461 MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
462 MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
463 MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
464
465 return GL_TRUE;
466 }
467 #undef SWAP_ROWS
468
469 /**
470 * Compute inverse of a general 3d transformation matrix.
471 *
472 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
473 * stored in the GLmatrix::inv attribute.
474 *
475 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
476 *
477 * \author Adapted from graphics gems II.
478 *
479 * Calculates the inverse of the upper left by first calculating its
480 * determinant and multiplying it to the symmetric adjust matrix of each
481 * element. Finally deals with the translation part by transforming the
482 * original translation vector using by the calculated submatrix inverse.
483 */
484 static GLboolean invert_matrix_3d_general( GLmatrix *mat )
485 {
486 const GLfloat *in = mat->m;
487 GLfloat *out = mat->inv;
488 GLfloat pos, neg, t;
489 GLfloat det;
490
491 /* Calculate the determinant of upper left 3x3 submatrix and
492 * determine if the matrix is singular.
493 */
494 pos = neg = 0.0;
495 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
496 if (t >= 0.0F) pos += t; else neg += t;
497
498 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
499 if (t >= 0.0F) pos += t; else neg += t;
500
501 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
502 if (t >= 0.0F) pos += t; else neg += t;
503
504 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
505 if (t >= 0.0F) pos += t; else neg += t;
506
507 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
508 if (t >= 0.0F) pos += t; else neg += t;
509
510 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
511 if (t >= 0.0F) pos += t; else neg += t;
512
513 det = pos + neg;
514
515 if (fabsf(det) < 1e-25F)
516 return GL_FALSE;
517
518 det = 1.0F / det;
519 MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
520 MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
521 MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
522 MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
523 MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
524 MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
525 MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
526 MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
527 MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
528
529 /* Do the translation part */
530 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
531 MAT(in,1,3) * MAT(out,0,1) +
532 MAT(in,2,3) * MAT(out,0,2) );
533 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
534 MAT(in,1,3) * MAT(out,1,1) +
535 MAT(in,2,3) * MAT(out,1,2) );
536 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
537 MAT(in,1,3) * MAT(out,2,1) +
538 MAT(in,2,3) * MAT(out,2,2) );
539
540 return GL_TRUE;
541 }
542
543 /**
544 * Compute inverse of a 3d transformation matrix.
545 *
546 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
547 * stored in the GLmatrix::inv attribute.
548 *
549 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
550 *
551 * If the matrix is not an angle preserving matrix then calls
552 * invert_matrix_3d_general for the actual calculation. Otherwise calculates
553 * the inverse matrix analyzing and inverting each of the scaling, rotation and
554 * translation parts.
555 */
556 static GLboolean invert_matrix_3d( GLmatrix *mat )
557 {
558 const GLfloat *in = mat->m;
559 GLfloat *out = mat->inv;
560
561 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
562 return invert_matrix_3d_general( mat );
563 }
564
565 if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
566 GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
567 MAT(in,0,1) * MAT(in,0,1) +
568 MAT(in,0,2) * MAT(in,0,2));
569
570 if (scale == 0.0F)
571 return GL_FALSE;
572
573 scale = 1.0F / scale;
574
575 /* Transpose and scale the 3 by 3 upper-left submatrix. */
576 MAT(out,0,0) = scale * MAT(in,0,0);
577 MAT(out,1,0) = scale * MAT(in,0,1);
578 MAT(out,2,0) = scale * MAT(in,0,2);
579 MAT(out,0,1) = scale * MAT(in,1,0);
580 MAT(out,1,1) = scale * MAT(in,1,1);
581 MAT(out,2,1) = scale * MAT(in,1,2);
582 MAT(out,0,2) = scale * MAT(in,2,0);
583 MAT(out,1,2) = scale * MAT(in,2,1);
584 MAT(out,2,2) = scale * MAT(in,2,2);
585 }
586 else if (mat->flags & MAT_FLAG_ROTATION) {
587 /* Transpose the 3 by 3 upper-left submatrix. */
588 MAT(out,0,0) = MAT(in,0,0);
589 MAT(out,1,0) = MAT(in,0,1);
590 MAT(out,2,0) = MAT(in,0,2);
591 MAT(out,0,1) = MAT(in,1,0);
592 MAT(out,1,1) = MAT(in,1,1);
593 MAT(out,2,1) = MAT(in,1,2);
594 MAT(out,0,2) = MAT(in,2,0);
595 MAT(out,1,2) = MAT(in,2,1);
596 MAT(out,2,2) = MAT(in,2,2);
597 }
598 else {
599 /* pure translation */
600 memcpy( out, Identity, sizeof(Identity) );
601 MAT(out,0,3) = - MAT(in,0,3);
602 MAT(out,1,3) = - MAT(in,1,3);
603 MAT(out,2,3) = - MAT(in,2,3);
604 return GL_TRUE;
605 }
606
607 if (mat->flags & MAT_FLAG_TRANSLATION) {
608 /* Do the translation part */
609 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
610 MAT(in,1,3) * MAT(out,0,1) +
611 MAT(in,2,3) * MAT(out,0,2) );
612 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
613 MAT(in,1,3) * MAT(out,1,1) +
614 MAT(in,2,3) * MAT(out,1,2) );
615 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
616 MAT(in,1,3) * MAT(out,2,1) +
617 MAT(in,2,3) * MAT(out,2,2) );
618 }
619 else {
620 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
621 }
622
623 return GL_TRUE;
624 }
625
626 /**
627 * Compute inverse of an identity transformation matrix.
628 *
629 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
630 * stored in the GLmatrix::inv attribute.
631 *
632 * \return always GL_TRUE.
633 *
634 * Simply copies Identity into GLmatrix::inv.
635 */
636 static GLboolean invert_matrix_identity( GLmatrix *mat )
637 {
638 memcpy( mat->inv, Identity, sizeof(Identity) );
639 return GL_TRUE;
640 }
641
642 /**
643 * Compute inverse of a no-rotation 3d transformation matrix.
644 *
645 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
646 * stored in the GLmatrix::inv attribute.
647 *
648 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
649 *
650 * Calculates the
651 */
652 static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
653 {
654 const GLfloat *in = mat->m;
655 GLfloat *out = mat->inv;
656
657 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
658 return GL_FALSE;
659
660 memcpy( out, Identity, sizeof(Identity) );
661 MAT(out,0,0) = 1.0F / MAT(in,0,0);
662 MAT(out,1,1) = 1.0F / MAT(in,1,1);
663 MAT(out,2,2) = 1.0F / MAT(in,2,2);
664
665 if (mat->flags & MAT_FLAG_TRANSLATION) {
666 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
667 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
668 MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
669 }
670
671 return GL_TRUE;
672 }
673
674 /**
675 * Compute inverse of a no-rotation 2d transformation matrix.
676 *
677 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
678 * stored in the GLmatrix::inv attribute.
679 *
680 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
681 *
682 * Calculates the inverse matrix by applying the inverse scaling and
683 * translation to the identity matrix.
684 */
685 static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
686 {
687 const GLfloat *in = mat->m;
688 GLfloat *out = mat->inv;
689
690 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
691 return GL_FALSE;
692
693 memcpy( out, Identity, sizeof(Identity) );
694 MAT(out,0,0) = 1.0F / MAT(in,0,0);
695 MAT(out,1,1) = 1.0F / MAT(in,1,1);
696
697 if (mat->flags & MAT_FLAG_TRANSLATION) {
698 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
699 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
700 }
701
702 return GL_TRUE;
703 }
704
705 #if 0
706 /* broken */
707 static GLboolean invert_matrix_perspective( GLmatrix *mat )
708 {
709 const GLfloat *in = mat->m;
710 GLfloat *out = mat->inv;
711
712 if (MAT(in,2,3) == 0)
713 return GL_FALSE;
714
715 memcpy( out, Identity, sizeof(Identity) );
716
717 MAT(out,0,0) = 1.0F / MAT(in,0,0);
718 MAT(out,1,1) = 1.0F / MAT(in,1,1);
719
720 MAT(out,0,3) = MAT(in,0,2);
721 MAT(out,1,3) = MAT(in,1,2);
722
723 MAT(out,2,2) = 0;
724 MAT(out,2,3) = -1;
725
726 MAT(out,3,2) = 1.0F / MAT(in,2,3);
727 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
728
729 return GL_TRUE;
730 }
731 #endif
732
733 /**
734 * Matrix inversion function pointer type.
735 */
736 typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
737
738 /**
739 * Table of the matrix inversion functions according to the matrix type.
740 */
741 static inv_mat_func inv_mat_tab[7] = {
742 invert_matrix_general,
743 invert_matrix_identity,
744 invert_matrix_3d_no_rot,
745 #if 0
746 /* Don't use this function for now - it fails when the projection matrix
747 * is premultiplied by a translation (ala Chromium's tilesort SPU).
748 */
749 invert_matrix_perspective,
750 #else
751 invert_matrix_general,
752 #endif
753 invert_matrix_3d, /* lazy! */
754 invert_matrix_2d_no_rot,
755 invert_matrix_3d
756 };
757
758 /**
759 * Compute inverse of a transformation matrix.
760 *
761 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
762 * stored in the GLmatrix::inv attribute.
763 *
764 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
765 *
766 * Calls the matrix inversion function in inv_mat_tab corresponding to the
767 * given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag,
768 * and copies the identity matrix into GLmatrix::inv.
769 */
770 static GLboolean matrix_invert( GLmatrix *mat )
771 {
772 if (inv_mat_tab[mat->type](mat)) {
773 mat->flags &= ~MAT_FLAG_SINGULAR;
774 return GL_TRUE;
775 } else {
776 mat->flags |= MAT_FLAG_SINGULAR;
777 memcpy( mat->inv, Identity, sizeof(Identity) );
778 return GL_FALSE;
779 }
780 }
781
782 /*@}*/
783
784
785 /**********************************************************************/
786 /** \name Matrix generation */
787 /*@{*/
788
789 /**
790 * Generate a 4x4 transformation matrix from glRotate parameters, and
791 * post-multiply the input matrix by it.
792 *
793 * \author
794 * This function was contributed by Erich Boleyn (erich@uruk.org).
795 * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
796 */
797 void
798 _math_matrix_rotate( GLmatrix *mat,
799 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
800 {
801 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
802 GLfloat m[16];
803 GLboolean optimized;
804
805 s = sinf( angle * M_PI / 180.0 );
806 c = cosf( angle * M_PI / 180.0 );
807
808 memcpy(m, Identity, sizeof(Identity));
809 optimized = GL_FALSE;
810
811 #define M(row,col) m[col*4+row]
812
813 if (x == 0.0F) {
814 if (y == 0.0F) {
815 if (z != 0.0F) {
816 optimized = GL_TRUE;
817 /* rotate only around z-axis */
818 M(0,0) = c;
819 M(1,1) = c;
820 if (z < 0.0F) {
821 M(0,1) = s;
822 M(1,0) = -s;
823 }
824 else {
825 M(0,1) = -s;
826 M(1,0) = s;
827 }
828 }
829 }
830 else if (z == 0.0F) {
831 optimized = GL_TRUE;
832 /* rotate only around y-axis */
833 M(0,0) = c;
834 M(2,2) = c;
835 if (y < 0.0F) {
836 M(0,2) = -s;
837 M(2,0) = s;
838 }
839 else {
840 M(0,2) = s;
841 M(2,0) = -s;
842 }
843 }
844 }
845 else if (y == 0.0F) {
846 if (z == 0.0F) {
847 optimized = GL_TRUE;
848 /* rotate only around x-axis */
849 M(1,1) = c;
850 M(2,2) = c;
851 if (x < 0.0F) {
852 M(1,2) = s;
853 M(2,1) = -s;
854 }
855 else {
856 M(1,2) = -s;
857 M(2,1) = s;
858 }
859 }
860 }
861
862 if (!optimized) {
863 const GLfloat mag = sqrtf(x * x + y * y + z * z);
864
865 if (mag <= 1.0e-4F) {
866 /* no rotation, leave mat as-is */
867 return;
868 }
869
870 x /= mag;
871 y /= mag;
872 z /= mag;
873
874
875 /*
876 * Arbitrary axis rotation matrix.
877 *
878 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
879 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
880 * (which is about the X-axis), and the two composite transforms
881 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
882 * from the arbitrary axis to the X-axis then back. They are
883 * all elementary rotations.
884 *
885 * Rz' is a rotation about the Z-axis, to bring the axis vector
886 * into the x-z plane. Then Ry' is applied, rotating about the
887 * Y-axis to bring the axis vector parallel with the X-axis. The
888 * rotation about the X-axis is then performed. Ry and Rz are
889 * simply the respective inverse transforms to bring the arbitrary
890 * axis back to its original orientation. The first transforms
891 * Rz' and Ry' are considered inverses, since the data from the
892 * arbitrary axis gives you info on how to get to it, not how
893 * to get away from it, and an inverse must be applied.
894 *
895 * The basic calculation used is to recognize that the arbitrary
896 * axis vector (x, y, z), since it is of unit length, actually
897 * represents the sines and cosines of the angles to rotate the
898 * X-axis to the same orientation, with theta being the angle about
899 * Z and phi the angle about Y (in the order described above)
900 * as follows:
901 *
902 * cos ( theta ) = x / sqrt ( 1 - z^2 )
903 * sin ( theta ) = y / sqrt ( 1 - z^2 )
904 *
905 * cos ( phi ) = sqrt ( 1 - z^2 )
906 * sin ( phi ) = z
907 *
908 * Note that cos ( phi ) can further be inserted to the above
909 * formulas:
910 *
911 * cos ( theta ) = x / cos ( phi )
912 * sin ( theta ) = y / sin ( phi )
913 *
914 * ...etc. Because of those relations and the standard trigonometric
915 * relations, it is pssible to reduce the transforms down to what
916 * is used below. It may be that any primary axis chosen will give the
917 * same results (modulo a sign convention) using thie method.
918 *
919 * Particularly nice is to notice that all divisions that might
920 * have caused trouble when parallel to certain planes or
921 * axis go away with care paid to reducing the expressions.
922 * After checking, it does perform correctly under all cases, since
923 * in all the cases of division where the denominator would have
924 * been zero, the numerator would have been zero as well, giving
925 * the expected result.
926 */
927
928 xx = x * x;
929 yy = y * y;
930 zz = z * z;
931 xy = x * y;
932 yz = y * z;
933 zx = z * x;
934 xs = x * s;
935 ys = y * s;
936 zs = z * s;
937 one_c = 1.0F - c;
938
939 /* We already hold the identity-matrix so we can skip some statements */
940 M(0,0) = (one_c * xx) + c;
941 M(0,1) = (one_c * xy) - zs;
942 M(0,2) = (one_c * zx) + ys;
943 /* M(0,3) = 0.0F; */
944
945 M(1,0) = (one_c * xy) + zs;
946 M(1,1) = (one_c * yy) + c;
947 M(1,2) = (one_c * yz) - xs;
948 /* M(1,3) = 0.0F; */
949
950 M(2,0) = (one_c * zx) - ys;
951 M(2,1) = (one_c * yz) + xs;
952 M(2,2) = (one_c * zz) + c;
953 /* M(2,3) = 0.0F; */
954
955 /*
956 M(3,0) = 0.0F;
957 M(3,1) = 0.0F;
958 M(3,2) = 0.0F;
959 M(3,3) = 1.0F;
960 */
961 }
962 #undef M
963
964 matrix_multf( mat, m, MAT_FLAG_ROTATION );
965 }
966
967 /**
968 * Apply a perspective projection matrix.
969 *
970 * \param mat matrix to apply the projection.
971 * \param left left clipping plane coordinate.
972 * \param right right clipping plane coordinate.
973 * \param bottom bottom clipping plane coordinate.
974 * \param top top clipping plane coordinate.
975 * \param nearval distance to the near clipping plane.
976 * \param farval distance to the far clipping plane.
977 *
978 * Creates the projection matrix and multiplies it with \p mat, marking the
979 * MAT_FLAG_PERSPECTIVE flag.
980 */
981 void
982 _math_matrix_frustum( GLmatrix *mat,
983 GLfloat left, GLfloat right,
984 GLfloat bottom, GLfloat top,
985 GLfloat nearval, GLfloat farval )
986 {
987 GLfloat x, y, a, b, c, d;
988 GLfloat m[16];
989
990 x = (2.0F*nearval) / (right-left);
991 y = (2.0F*nearval) / (top-bottom);
992 a = (right+left) / (right-left);
993 b = (top+bottom) / (top-bottom);
994 c = -(farval+nearval) / ( farval-nearval);
995 d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
996
997 #define M(row,col) m[col*4+row]
998 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
999 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
1000 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
1001 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
1002 #undef M
1003
1004 matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
1005 }
1006
1007 /**
1008 * Apply an orthographic projection matrix.
1009 *
1010 * \param mat matrix to apply the projection.
1011 * \param left left clipping plane coordinate.
1012 * \param right right clipping plane coordinate.
1013 * \param bottom bottom clipping plane coordinate.
1014 * \param top top clipping plane coordinate.
1015 * \param nearval distance to the near clipping plane.
1016 * \param farval distance to the far clipping plane.
1017 *
1018 * Creates the projection matrix and multiplies it with \p mat, marking the
1019 * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
1020 */
1021 void
1022 _math_matrix_ortho( GLmatrix *mat,
1023 GLfloat left, GLfloat right,
1024 GLfloat bottom, GLfloat top,
1025 GLfloat nearval, GLfloat farval )
1026 {
1027 GLfloat m[16];
1028
1029 #define M(row,col) m[col*4+row]
1030 M(0,0) = 2.0F / (right-left);
1031 M(0,1) = 0.0F;
1032 M(0,2) = 0.0F;
1033 M(0,3) = -(right+left) / (right-left);
1034
1035 M(1,0) = 0.0F;
1036 M(1,1) = 2.0F / (top-bottom);
1037 M(1,2) = 0.0F;
1038 M(1,3) = -(top+bottom) / (top-bottom);
1039
1040 M(2,0) = 0.0F;
1041 M(2,1) = 0.0F;
1042 M(2,2) = -2.0F / (farval-nearval);
1043 M(2,3) = -(farval+nearval) / (farval-nearval);
1044
1045 M(3,0) = 0.0F;
1046 M(3,1) = 0.0F;
1047 M(3,2) = 0.0F;
1048 M(3,3) = 1.0F;
1049 #undef M
1050
1051 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
1052 }
1053
1054 /**
1055 * Multiply a matrix with a general scaling matrix.
1056 *
1057 * \param mat matrix.
1058 * \param x x axis scale factor.
1059 * \param y y axis scale factor.
1060 * \param z z axis scale factor.
1061 *
1062 * Multiplies in-place the elements of \p mat by the scale factors. Checks if
1063 * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE
1064 * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and
1065 * MAT_DIRTY_INVERSE dirty flags.
1066 */
1067 void
1068 _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1069 {
1070 GLfloat *m = mat->m;
1071 m[0] *= x; m[4] *= y; m[8] *= z;
1072 m[1] *= x; m[5] *= y; m[9] *= z;
1073 m[2] *= x; m[6] *= y; m[10] *= z;
1074 m[3] *= x; m[7] *= y; m[11] *= z;
1075
1076 if (fabsf(x - y) < 1e-8F && fabsf(x - z) < 1e-8F)
1077 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1078 else
1079 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1080
1081 mat->flags |= (MAT_DIRTY_TYPE |
1082 MAT_DIRTY_INVERSE);
1083 }
1084
1085 /**
1086 * Multiply a matrix with a translation matrix.
1087 *
1088 * \param mat matrix.
1089 * \param x translation vector x coordinate.
1090 * \param y translation vector y coordinate.
1091 * \param z translation vector z coordinate.
1092 *
1093 * Adds the translation coordinates to the elements of \p mat in-place. Marks
1094 * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE
1095 * dirty flags.
1096 */
1097 void
1098 _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1099 {
1100 GLfloat *m = mat->m;
1101 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1102 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1103 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1104 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1105
1106 mat->flags |= (MAT_FLAG_TRANSLATION |
1107 MAT_DIRTY_TYPE |
1108 MAT_DIRTY_INVERSE);
1109 }
1110
1111
1112 /**
1113 * Set matrix to do viewport and depthrange mapping.
1114 * Transforms Normalized Device Coords to window/Z values.
1115 */
1116 void
1117 _math_matrix_viewport(GLmatrix *m, const float scale[3],
1118 const float translate[3], double depthMax)
1119 {
1120 m->m[MAT_SX] = scale[0];
1121 m->m[MAT_TX] = translate[0];
1122 m->m[MAT_SY] = scale[1];
1123 m->m[MAT_TY] = translate[1];
1124 m->m[MAT_SZ] = depthMax*scale[2];
1125 m->m[MAT_TZ] = depthMax*translate[2];
1126 m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION;
1127 m->type = MATRIX_3D_NO_ROT;
1128 }
1129
1130
1131 /**
1132 * Set a matrix to the identity matrix.
1133 *
1134 * \param mat matrix.
1135 *
1136 * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL.
1137 * Sets the matrix type to identity, and clear the dirty flags.
1138 */
1139 void
1140 _math_matrix_set_identity( GLmatrix *mat )
1141 {
1142 STATIC_ASSERT(MATRIX_M == offsetof(GLmatrix, m));
1143 STATIC_ASSERT(MATRIX_INV == offsetof(GLmatrix, inv));
1144
1145 memcpy( mat->m, Identity, sizeof(Identity) );
1146 memcpy( mat->inv, Identity, sizeof(Identity) );
1147
1148 mat->type = MATRIX_IDENTITY;
1149 mat->flags &= ~(MAT_DIRTY_FLAGS|
1150 MAT_DIRTY_TYPE|
1151 MAT_DIRTY_INVERSE);
1152 }
1153
1154 /*@}*/
1155
1156
1157 /**********************************************************************/
1158 /** \name Matrix analysis */
1159 /*@{*/
1160
1161 #define ZERO(x) (1<<x)
1162 #define ONE(x) (1<<(x+16))
1163
1164 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
1165 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
1166
1167 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
1168 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
1169 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1170 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1171
1172 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
1173 ZERO(1) | ZERO(9) | \
1174 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1175 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1176
1177 #define MASK_2D ( ZERO(8) | \
1178 ZERO(9) | \
1179 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1180 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1181
1182
1183 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
1184 ZERO(1) | ZERO(9) | \
1185 ZERO(2) | ZERO(6) | \
1186 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1187
1188 #define MASK_3D ( \
1189 \
1190 \
1191 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1192
1193
1194 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
1195 ZERO(1) | ZERO(13) |\
1196 ZERO(2) | ZERO(6) | \
1197 ZERO(3) | ZERO(7) | ZERO(15) )
1198
1199 #define SQ(x) ((x)*(x))
1200
1201 /**
1202 * Determine type and flags from scratch.
1203 *
1204 * \param mat matrix.
1205 *
1206 * This is expensive enough to only want to do it once.
1207 */
1208 static void analyse_from_scratch( GLmatrix *mat )
1209 {
1210 const GLfloat *m = mat->m;
1211 GLuint mask = 0;
1212 GLuint i;
1213
1214 for (i = 0 ; i < 16 ; i++) {
1215 if (m[i] == 0.0F) mask |= (1<<i);
1216 }
1217
1218 if (m[0] == 1.0F) mask |= (1<<16);
1219 if (m[5] == 1.0F) mask |= (1<<21);
1220 if (m[10] == 1.0F) mask |= (1<<26);
1221 if (m[15] == 1.0F) mask |= (1<<31);
1222
1223 mat->flags &= ~MAT_FLAGS_GEOMETRY;
1224
1225 /* Check for translation - no-one really cares
1226 */
1227 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
1228 mat->flags |= MAT_FLAG_TRANSLATION;
1229
1230 /* Do the real work
1231 */
1232 if (mask == (GLuint) MASK_IDENTITY) {
1233 mat->type = MATRIX_IDENTITY;
1234 }
1235 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
1236 mat->type = MATRIX_2D_NO_ROT;
1237
1238 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
1239 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1240 }
1241 else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
1242 GLfloat mm = DOT2(m, m);
1243 GLfloat m4m4 = DOT2(m+4,m+4);
1244 GLfloat mm4 = DOT2(m,m+4);
1245
1246 mat->type = MATRIX_2D;
1247
1248 /* Check for scale */
1249 if (SQ(mm-1) > SQ(1e-6F) ||
1250 SQ(m4m4-1) > SQ(1e-6F))
1251 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1252
1253 /* Check for rotation */
1254 if (SQ(mm4) > SQ(1e-6F))
1255 mat->flags |= MAT_FLAG_GENERAL_3D;
1256 else
1257 mat->flags |= MAT_FLAG_ROTATION;
1258
1259 }
1260 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
1261 mat->type = MATRIX_3D_NO_ROT;
1262
1263 /* Check for scale */
1264 if (SQ(m[0]-m[5]) < SQ(1e-6F) &&
1265 SQ(m[0]-m[10]) < SQ(1e-6F)) {
1266 if (SQ(m[0]-1.0F) > SQ(1e-6F)) {
1267 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1268 }
1269 }
1270 else {
1271 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1272 }
1273 }
1274 else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
1275 GLfloat c1 = DOT3(m,m);
1276 GLfloat c2 = DOT3(m+4,m+4);
1277 GLfloat c3 = DOT3(m+8,m+8);
1278 GLfloat d1 = DOT3(m, m+4);
1279 GLfloat cp[3];
1280
1281 mat->type = MATRIX_3D;
1282
1283 /* Check for scale */
1284 if (SQ(c1-c2) < SQ(1e-6F) && SQ(c1-c3) < SQ(1e-6F)) {
1285 if (SQ(c1-1.0F) > SQ(1e-6F))
1286 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1287 /* else no scale at all */
1288 }
1289 else {
1290 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1291 }
1292
1293 /* Check for rotation */
1294 if (SQ(d1) < SQ(1e-6F)) {
1295 CROSS3( cp, m, m+4 );
1296 SUB_3V( cp, cp, (m+8) );
1297 if (LEN_SQUARED_3FV(cp) < SQ(1e-6F))
1298 mat->flags |= MAT_FLAG_ROTATION;
1299 else
1300 mat->flags |= MAT_FLAG_GENERAL_3D;
1301 }
1302 else {
1303 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
1304 }
1305 }
1306 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
1307 mat->type = MATRIX_PERSPECTIVE;
1308 mat->flags |= MAT_FLAG_GENERAL;
1309 }
1310 else {
1311 mat->type = MATRIX_GENERAL;
1312 mat->flags |= MAT_FLAG_GENERAL;
1313 }
1314 }
1315
1316 /**
1317 * Analyze a matrix given that its flags are accurate.
1318 *
1319 * This is the more common operation, hopefully.
1320 */
1321 static void analyse_from_flags( GLmatrix *mat )
1322 {
1323 const GLfloat *m = mat->m;
1324
1325 if (TEST_MAT_FLAGS(mat, 0)) {
1326 mat->type = MATRIX_IDENTITY;
1327 }
1328 else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
1329 MAT_FLAG_UNIFORM_SCALE |
1330 MAT_FLAG_GENERAL_SCALE))) {
1331 if ( m[10]==1.0F && m[14]==0.0F ) {
1332 mat->type = MATRIX_2D_NO_ROT;
1333 }
1334 else {
1335 mat->type = MATRIX_3D_NO_ROT;
1336 }
1337 }
1338 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
1339 if ( m[ 8]==0.0F
1340 && m[ 9]==0.0F
1341 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
1342 mat->type = MATRIX_2D;
1343 }
1344 else {
1345 mat->type = MATRIX_3D;
1346 }
1347 }
1348 else if ( m[4]==0.0F && m[12]==0.0F
1349 && m[1]==0.0F && m[13]==0.0F
1350 && m[2]==0.0F && m[6]==0.0F
1351 && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
1352 mat->type = MATRIX_PERSPECTIVE;
1353 }
1354 else {
1355 mat->type = MATRIX_GENERAL;
1356 }
1357 }
1358
1359 /**
1360 * Analyze and update a matrix.
1361 *
1362 * \param mat matrix.
1363 *
1364 * If the matrix type is dirty then calls either analyse_from_scratch() or
1365 * analyse_from_flags() to determine its type, according to whether the flags
1366 * are dirty or not, respectively. If the matrix has an inverse and it's dirty
1367 * then calls matrix_invert(). Finally clears the dirty flags.
1368 */
1369 void
1370 _math_matrix_analyse( GLmatrix *mat )
1371 {
1372 if (mat->flags & MAT_DIRTY_TYPE) {
1373 if (mat->flags & MAT_DIRTY_FLAGS)
1374 analyse_from_scratch( mat );
1375 else
1376 analyse_from_flags( mat );
1377 }
1378
1379 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
1380 matrix_invert( mat );
1381 mat->flags &= ~MAT_DIRTY_INVERSE;
1382 }
1383
1384 mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
1385 }
1386
1387 /*@}*/
1388
1389
1390 /**
1391 * Test if the given matrix preserves vector lengths.
1392 */
1393 GLboolean
1394 _math_matrix_is_length_preserving( const GLmatrix *m )
1395 {
1396 return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING);
1397 }
1398
1399
1400 /**
1401 * Test if the given matrix does any rotation.
1402 * (or perhaps if the upper-left 3x3 is non-identity)
1403 */
1404 GLboolean
1405 _math_matrix_has_rotation( const GLmatrix *m )
1406 {
1407 if (m->flags & (MAT_FLAG_GENERAL |
1408 MAT_FLAG_ROTATION |
1409 MAT_FLAG_GENERAL_3D |
1410 MAT_FLAG_PERSPECTIVE))
1411 return GL_TRUE;
1412 else
1413 return GL_FALSE;
1414 }
1415
1416
1417 GLboolean
1418 _math_matrix_is_general_scale( const GLmatrix *m )
1419 {
1420 return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE;
1421 }
1422
1423
1424 GLboolean
1425 _math_matrix_is_dirty( const GLmatrix *m )
1426 {
1427 return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE;
1428 }
1429
1430
1431 /**********************************************************************/
1432 /** \name Matrix setup */
1433 /*@{*/
1434
1435 /**
1436 * Copy a matrix.
1437 *
1438 * \param to destination matrix.
1439 * \param from source matrix.
1440 *
1441 * Copies all fields in GLmatrix, creating an inverse array if necessary.
1442 */
1443 void
1444 _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
1445 {
1446 memcpy(to->m, from->m, 16 * sizeof(GLfloat));
1447 memcpy(to->inv, from->inv, 16 * sizeof(GLfloat));
1448 to->flags = from->flags;
1449 to->type = from->type;
1450 }
1451
1452 /**
1453 * Loads a matrix array into GLmatrix.
1454 *
1455 * \param m matrix array.
1456 * \param mat matrix.
1457 *
1458 * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
1459 * flags.
1460 */
1461 void
1462 _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1463 {
1464 memcpy( mat->m, m, 16*sizeof(GLfloat) );
1465 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1466 }
1467
1468 /**
1469 * Matrix constructor.
1470 *
1471 * \param m matrix.
1472 *
1473 * Initialize the GLmatrix fields.
1474 */
1475 void
1476 _math_matrix_ctr( GLmatrix *m )
1477 {
1478 m->m = _mesa_align_malloc( 16 * sizeof(GLfloat), 16 );
1479 if (m->m)
1480 memcpy( m->m, Identity, sizeof(Identity) );
1481 m->inv = _mesa_align_malloc( 16 * sizeof(GLfloat), 16 );
1482 if (m->inv)
1483 memcpy( m->inv, Identity, sizeof(Identity) );
1484 m->type = MATRIX_IDENTITY;
1485 m->flags = 0;
1486 }
1487
1488 /**
1489 * Matrix destructor.
1490 *
1491 * \param m matrix.
1492 *
1493 * Frees the data in a GLmatrix.
1494 */
1495 void
1496 _math_matrix_dtr( GLmatrix *m )
1497 {
1498 _mesa_align_free( m->m );
1499 m->m = NULL;
1500
1501 _mesa_align_free( m->inv );
1502 m->inv = NULL;
1503 }
1504
1505 /*@}*/
1506
1507
1508 /**********************************************************************/
1509 /** \name Matrix transpose */
1510 /*@{*/
1511
1512 /**
1513 * Transpose a GLfloat matrix.
1514 *
1515 * \param to destination array.
1516 * \param from source array.
1517 */
1518 void
1519 _math_transposef( GLfloat to[16], const GLfloat from[16] )
1520 {
1521 to[0] = from[0];
1522 to[1] = from[4];
1523 to[2] = from[8];
1524 to[3] = from[12];
1525 to[4] = from[1];
1526 to[5] = from[5];
1527 to[6] = from[9];
1528 to[7] = from[13];
1529 to[8] = from[2];
1530 to[9] = from[6];
1531 to[10] = from[10];
1532 to[11] = from[14];
1533 to[12] = from[3];
1534 to[13] = from[7];
1535 to[14] = from[11];
1536 to[15] = from[15];
1537 }
1538
1539 /**
1540 * Transpose a GLdouble matrix.
1541 *
1542 * \param to destination array.
1543 * \param from source array.
1544 */
1545 void
1546 _math_transposed( GLdouble to[16], const GLdouble from[16] )
1547 {
1548 to[0] = from[0];
1549 to[1] = from[4];
1550 to[2] = from[8];
1551 to[3] = from[12];
1552 to[4] = from[1];
1553 to[5] = from[5];
1554 to[6] = from[9];
1555 to[7] = from[13];
1556 to[8] = from[2];
1557 to[9] = from[6];
1558 to[10] = from[10];
1559 to[11] = from[14];
1560 to[12] = from[3];
1561 to[13] = from[7];
1562 to[14] = from[11];
1563 to[15] = from[15];
1564 }
1565
1566 /**
1567 * Transpose a GLdouble matrix and convert to GLfloat.
1568 *
1569 * \param to destination array.
1570 * \param from source array.
1571 */
1572 void
1573 _math_transposefd( GLfloat to[16], const GLdouble from[16] )
1574 {
1575 to[0] = (GLfloat) from[0];
1576 to[1] = (GLfloat) from[4];
1577 to[2] = (GLfloat) from[8];
1578 to[3] = (GLfloat) from[12];
1579 to[4] = (GLfloat) from[1];
1580 to[5] = (GLfloat) from[5];
1581 to[6] = (GLfloat) from[9];
1582 to[7] = (GLfloat) from[13];
1583 to[8] = (GLfloat) from[2];
1584 to[9] = (GLfloat) from[6];
1585 to[10] = (GLfloat) from[10];
1586 to[11] = (GLfloat) from[14];
1587 to[12] = (GLfloat) from[3];
1588 to[13] = (GLfloat) from[7];
1589 to[14] = (GLfloat) from[11];
1590 to[15] = (GLfloat) from[15];
1591 }
1592
1593 /*@}*/
1594
1595
1596 /**
1597 * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This
1598 * function is used for transforming clipping plane equations and spotlight
1599 * directions.
1600 * Mathematically, u = v * m.
1601 * Input: v - input vector
1602 * m - transformation matrix
1603 * Output: u - transformed vector
1604 */
1605 void
1606 _mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
1607 {
1608 const GLfloat v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
1609 #define M(row,col) m[row + col*4]
1610 u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
1611 u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
1612 u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
1613 u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);
1614 #undef M
1615 }