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