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 * Compute inverse of 4x4 transformation matrix.
338 *
339 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
340 * stored in the GLmatrix::inv attribute.
341 *
342 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
343 *
344 * \author
345 * Code contributed by Jacques Leroy jle@star.be
346 *
347 * Calculates the inverse matrix by performing the gaussian matrix reduction
348 * with partial pivoting followed by back/substitution with the loops manually
349 * unrolled.
350 */
invert_matrix_general(GLmatrix * mat)351 static GLboolean invert_matrix_general( GLmatrix *mat )
352 {
353 return util_invert_mat4x4(mat->inv, mat->m);
354 }
355
356 /**
357 * Compute inverse of a general 3d transformation matrix.
358 *
359 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
360 * stored in the GLmatrix::inv attribute.
361 *
362 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
363 *
364 * \author Adapted from graphics gems II.
365 *
366 * Calculates the inverse of the upper left by first calculating its
367 * determinant and multiplying it to the symmetric adjust matrix of each
368 * element. Finally deals with the translation part by transforming the
369 * original translation vector using by the calculated submatrix inverse.
370 */
invert_matrix_3d_general(GLmatrix * mat)371 static GLboolean invert_matrix_3d_general( GLmatrix *mat )
372 {
373 const GLfloat *in = mat->m;
374 GLfloat *out = mat->inv;
375 GLfloat pos, neg, t;
376 GLfloat det;
377
378 /* Calculate the determinant of upper left 3x3 submatrix and
379 * determine if the matrix is singular.
380 */
381 pos = neg = 0.0;
382 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
383 if (t >= 0.0F) pos += t; else neg += t;
384
385 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
386 if (t >= 0.0F) pos += t; else neg += t;
387
388 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
389 if (t >= 0.0F) pos += t; else neg += t;
390
391 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
392 if (t >= 0.0F) pos += t; else neg += t;
393
394 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
395 if (t >= 0.0F) pos += t; else neg += t;
396
397 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
398 if (t >= 0.0F) pos += t; else neg += t;
399
400 det = pos + neg;
401
402 if (fabsf(det) < 1e-25F)
403 return GL_FALSE;
404
405 det = 1.0F / det;
406 MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
407 MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
408 MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
409 MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
410 MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
411 MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
412 MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
413 MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
414 MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
415
416 /* Do the translation part */
417 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
418 MAT(in,1,3) * MAT(out,0,1) +
419 MAT(in,2,3) * MAT(out,0,2) );
420 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
421 MAT(in,1,3) * MAT(out,1,1) +
422 MAT(in,2,3) * MAT(out,1,2) );
423 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
424 MAT(in,1,3) * MAT(out,2,1) +
425 MAT(in,2,3) * MAT(out,2,2) );
426
427 return GL_TRUE;
428 }
429
430 /**
431 * Compute inverse of a 3d transformation matrix.
432 *
433 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
434 * stored in the GLmatrix::inv attribute.
435 *
436 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
437 *
438 * If the matrix is not an angle preserving matrix then calls
439 * invert_matrix_3d_general for the actual calculation. Otherwise calculates
440 * the inverse matrix analyzing and inverting each of the scaling, rotation and
441 * translation parts.
442 */
invert_matrix_3d(GLmatrix * mat)443 static GLboolean invert_matrix_3d( GLmatrix *mat )
444 {
445 const GLfloat *in = mat->m;
446 GLfloat *out = mat->inv;
447
448 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
449 return invert_matrix_3d_general( mat );
450 }
451
452 if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
453 GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
454 MAT(in,0,1) * MAT(in,0,1) +
455 MAT(in,0,2) * MAT(in,0,2));
456
457 if (scale == 0.0F)
458 return GL_FALSE;
459
460 scale = 1.0F / scale;
461
462 /* Transpose and scale the 3 by 3 upper-left submatrix. */
463 MAT(out,0,0) = scale * MAT(in,0,0);
464 MAT(out,1,0) = scale * MAT(in,0,1);
465 MAT(out,2,0) = scale * MAT(in,0,2);
466 MAT(out,0,1) = scale * MAT(in,1,0);
467 MAT(out,1,1) = scale * MAT(in,1,1);
468 MAT(out,2,1) = scale * MAT(in,1,2);
469 MAT(out,0,2) = scale * MAT(in,2,0);
470 MAT(out,1,2) = scale * MAT(in,2,1);
471 MAT(out,2,2) = scale * MAT(in,2,2);
472 }
473 else if (mat->flags & MAT_FLAG_ROTATION) {
474 /* Transpose the 3 by 3 upper-left submatrix. */
475 MAT(out,0,0) = MAT(in,0,0);
476 MAT(out,1,0) = MAT(in,0,1);
477 MAT(out,2,0) = MAT(in,0,2);
478 MAT(out,0,1) = MAT(in,1,0);
479 MAT(out,1,1) = MAT(in,1,1);
480 MAT(out,2,1) = MAT(in,1,2);
481 MAT(out,0,2) = MAT(in,2,0);
482 MAT(out,1,2) = MAT(in,2,1);
483 MAT(out,2,2) = MAT(in,2,2);
484 }
485 else {
486 /* pure translation */
487 memcpy( out, Identity, sizeof(Identity) );
488 MAT(out,0,3) = - MAT(in,0,3);
489 MAT(out,1,3) = - MAT(in,1,3);
490 MAT(out,2,3) = - MAT(in,2,3);
491 return GL_TRUE;
492 }
493
494 if (mat->flags & MAT_FLAG_TRANSLATION) {
495 /* Do the translation part */
496 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
497 MAT(in,1,3) * MAT(out,0,1) +
498 MAT(in,2,3) * MAT(out,0,2) );
499 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
500 MAT(in,1,3) * MAT(out,1,1) +
501 MAT(in,2,3) * MAT(out,1,2) );
502 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
503 MAT(in,1,3) * MAT(out,2,1) +
504 MAT(in,2,3) * MAT(out,2,2) );
505 }
506 else {
507 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
508 }
509
510 return GL_TRUE;
511 }
512
513 /**
514 * Compute inverse of an identity transformation matrix.
515 *
516 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
517 * stored in the GLmatrix::inv attribute.
518 *
519 * \return always GL_TRUE.
520 *
521 * Simply copies Identity into GLmatrix::inv.
522 */
invert_matrix_identity(GLmatrix * mat)523 static GLboolean invert_matrix_identity( GLmatrix *mat )
524 {
525 memcpy( mat->inv, Identity, sizeof(Identity) );
526 return GL_TRUE;
527 }
528
529 /**
530 * Compute inverse of a no-rotation 3d transformation matrix.
531 *
532 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
533 * stored in the GLmatrix::inv attribute.
534 *
535 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
536 *
537 * Calculates the
538 */
invert_matrix_3d_no_rot(GLmatrix * mat)539 static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
540 {
541 const GLfloat *in = mat->m;
542 GLfloat *out = mat->inv;
543
544 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
545 return GL_FALSE;
546
547 memcpy( out, Identity, sizeof(Identity) );
548 MAT(out,0,0) = 1.0F / MAT(in,0,0);
549 MAT(out,1,1) = 1.0F / MAT(in,1,1);
550 MAT(out,2,2) = 1.0F / MAT(in,2,2);
551
552 if (mat->flags & MAT_FLAG_TRANSLATION) {
553 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
554 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
555 MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
556 }
557
558 return GL_TRUE;
559 }
560
561 /**
562 * Compute inverse of a no-rotation 2d transformation matrix.
563 *
564 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
565 * stored in the GLmatrix::inv attribute.
566 *
567 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
568 *
569 * Calculates the inverse matrix by applying the inverse scaling and
570 * translation to the identity matrix.
571 */
invert_matrix_2d_no_rot(GLmatrix * mat)572 static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
573 {
574 const GLfloat *in = mat->m;
575 GLfloat *out = mat->inv;
576
577 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
578 return GL_FALSE;
579
580 memcpy( out, Identity, sizeof(Identity) );
581 MAT(out,0,0) = 1.0F / MAT(in,0,0);
582 MAT(out,1,1) = 1.0F / MAT(in,1,1);
583
584 if (mat->flags & MAT_FLAG_TRANSLATION) {
585 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
586 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
587 }
588
589 return GL_TRUE;
590 }
591
592 #if 0
593 /* broken */
594 static GLboolean invert_matrix_perspective( GLmatrix *mat )
595 {
596 const GLfloat *in = mat->m;
597 GLfloat *out = mat->inv;
598
599 if (MAT(in,2,3) == 0)
600 return GL_FALSE;
601
602 memcpy( out, Identity, sizeof(Identity) );
603
604 MAT(out,0,0) = 1.0F / MAT(in,0,0);
605 MAT(out,1,1) = 1.0F / MAT(in,1,1);
606
607 MAT(out,0,3) = MAT(in,0,2);
608 MAT(out,1,3) = MAT(in,1,2);
609
610 MAT(out,2,2) = 0;
611 MAT(out,2,3) = -1;
612
613 MAT(out,3,2) = 1.0F / MAT(in,2,3);
614 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
615
616 return GL_TRUE;
617 }
618 #endif
619
620 /**
621 * Matrix inversion function pointer type.
622 */
623 typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
624
625 /**
626 * Table of the matrix inversion functions according to the matrix type.
627 */
628 static inv_mat_func inv_mat_tab[7] = {
629 invert_matrix_general,
630 invert_matrix_identity,
631 invert_matrix_3d_no_rot,
632 #if 0
633 /* Don't use this function for now - it fails when the projection matrix
634 * is premultiplied by a translation (ala Chromium's tilesort SPU).
635 */
636 invert_matrix_perspective,
637 #else
638 invert_matrix_general,
639 #endif
640 invert_matrix_3d, /* lazy! */
641 invert_matrix_2d_no_rot,
642 invert_matrix_3d
643 };
644
645 /**
646 * Compute inverse of a transformation matrix.
647 *
648 * \param mat pointer to a GLmatrix structure. The matrix inverse will be
649 * stored in the GLmatrix::inv attribute.
650 *
651 * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
652 *
653 * Calls the matrix inversion function in inv_mat_tab corresponding to the
654 * given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag,
655 * and copies the identity matrix into GLmatrix::inv.
656 */
matrix_invert(GLmatrix * mat)657 static GLboolean matrix_invert( GLmatrix *mat )
658 {
659 if (inv_mat_tab[mat->type](mat)) {
660 mat->flags &= ~MAT_FLAG_SINGULAR;
661 return GL_TRUE;
662 } else {
663 mat->flags |= MAT_FLAG_SINGULAR;
664 memcpy( mat->inv, Identity, sizeof(Identity) );
665 return GL_FALSE;
666 }
667 }
668
669 /*@}*/
670
671
672 /**********************************************************************/
673 /** \name Matrix generation */
674 /*@{*/
675
676 /**
677 * Generate a 4x4 transformation matrix from glRotate parameters, and
678 * post-multiply the input matrix by it.
679 *
680 * \author
681 * This function was contributed by Erich Boleyn (erich@uruk.org).
682 * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
683 */
684 void
_math_matrix_rotate(GLmatrix * mat,GLfloat angle,GLfloat x,GLfloat y,GLfloat z)685 _math_matrix_rotate( GLmatrix *mat,
686 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
687 {
688 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
689 GLfloat m[16];
690 GLboolean optimized;
691
692 s = sinf( angle * M_PI / 180.0 );
693 c = cosf( angle * M_PI / 180.0 );
694
695 memcpy(m, Identity, sizeof(Identity));
696 optimized = GL_FALSE;
697
698 #define M(row,col) m[col*4+row]
699
700 if (x == 0.0F) {
701 if (y == 0.0F) {
702 if (z != 0.0F) {
703 optimized = GL_TRUE;
704 /* rotate only around z-axis */
705 M(0,0) = c;
706 M(1,1) = c;
707 if (z < 0.0F) {
708 M(0,1) = s;
709 M(1,0) = -s;
710 }
711 else {
712 M(0,1) = -s;
713 M(1,0) = s;
714 }
715 }
716 }
717 else if (z == 0.0F) {
718 optimized = GL_TRUE;
719 /* rotate only around y-axis */
720 M(0,0) = c;
721 M(2,2) = c;
722 if (y < 0.0F) {
723 M(0,2) = -s;
724 M(2,0) = s;
725 }
726 else {
727 M(0,2) = s;
728 M(2,0) = -s;
729 }
730 }
731 }
732 else if (y == 0.0F) {
733 if (z == 0.0F) {
734 optimized = GL_TRUE;
735 /* rotate only around x-axis */
736 M(1,1) = c;
737 M(2,2) = c;
738 if (x < 0.0F) {
739 M(1,2) = s;
740 M(2,1) = -s;
741 }
742 else {
743 M(1,2) = -s;
744 M(2,1) = s;
745 }
746 }
747 }
748
749 if (!optimized) {
750 const GLfloat mag = sqrtf(x * x + y * y + z * z);
751
752 if (mag <= 1.0e-4F) {
753 /* no rotation, leave mat as-is */
754 return;
755 }
756
757 x /= mag;
758 y /= mag;
759 z /= mag;
760
761
762 /*
763 * Arbitrary axis rotation matrix.
764 *
765 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
766 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
767 * (which is about the X-axis), and the two composite transforms
768 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
769 * from the arbitrary axis to the X-axis then back. They are
770 * all elementary rotations.
771 *
772 * Rz' is a rotation about the Z-axis, to bring the axis vector
773 * into the x-z plane. Then Ry' is applied, rotating about the
774 * Y-axis to bring the axis vector parallel with the X-axis. The
775 * rotation about the X-axis is then performed. Ry and Rz are
776 * simply the respective inverse transforms to bring the arbitrary
777 * axis back to its original orientation. The first transforms
778 * Rz' and Ry' are considered inverses, since the data from the
779 * arbitrary axis gives you info on how to get to it, not how
780 * to get away from it, and an inverse must be applied.
781 *
782 * The basic calculation used is to recognize that the arbitrary
783 * axis vector (x, y, z), since it is of unit length, actually
784 * represents the sines and cosines of the angles to rotate the
785 * X-axis to the same orientation, with theta being the angle about
786 * Z and phi the angle about Y (in the order described above)
787 * as follows:
788 *
789 * cos ( theta ) = x / sqrt ( 1 - z^2 )
790 * sin ( theta ) = y / sqrt ( 1 - z^2 )
791 *
792 * cos ( phi ) = sqrt ( 1 - z^2 )
793 * sin ( phi ) = z
794 *
795 * Note that cos ( phi ) can further be inserted to the above
796 * formulas:
797 *
798 * cos ( theta ) = x / cos ( phi )
799 * sin ( theta ) = y / sin ( phi )
800 *
801 * ...etc. Because of those relations and the standard trigonometric
802 * relations, it is pssible to reduce the transforms down to what
803 * is used below. It may be that any primary axis chosen will give the
804 * same results (modulo a sign convention) using thie method.
805 *
806 * Particularly nice is to notice that all divisions that might
807 * have caused trouble when parallel to certain planes or
808 * axis go away with care paid to reducing the expressions.
809 * After checking, it does perform correctly under all cases, since
810 * in all the cases of division where the denominator would have
811 * been zero, the numerator would have been zero as well, giving
812 * the expected result.
813 */
814
815 xx = x * x;
816 yy = y * y;
817 zz = z * z;
818 xy = x * y;
819 yz = y * z;
820 zx = z * x;
821 xs = x * s;
822 ys = y * s;
823 zs = z * s;
824 one_c = 1.0F - c;
825
826 /* We already hold the identity-matrix so we can skip some statements */
827 M(0,0) = (one_c * xx) + c;
828 M(0,1) = (one_c * xy) - zs;
829 M(0,2) = (one_c * zx) + ys;
830 /* M(0,3) = 0.0F; */
831
832 M(1,0) = (one_c * xy) + zs;
833 M(1,1) = (one_c * yy) + c;
834 M(1,2) = (one_c * yz) - xs;
835 /* M(1,3) = 0.0F; */
836
837 M(2,0) = (one_c * zx) - ys;
838 M(2,1) = (one_c * yz) + xs;
839 M(2,2) = (one_c * zz) + c;
840 /* M(2,3) = 0.0F; */
841
842 /*
843 M(3,0) = 0.0F;
844 M(3,1) = 0.0F;
845 M(3,2) = 0.0F;
846 M(3,3) = 1.0F;
847 */
848 }
849 #undef M
850
851 matrix_multf( mat, m, MAT_FLAG_ROTATION );
852 }
853
854 /**
855 * Apply a perspective projection matrix.
856 *
857 * \param mat matrix to apply the projection.
858 * \param left left clipping plane coordinate.
859 * \param right right clipping plane coordinate.
860 * \param bottom bottom clipping plane coordinate.
861 * \param top top clipping plane coordinate.
862 * \param nearval distance to the near clipping plane.
863 * \param farval distance to the far clipping plane.
864 *
865 * Creates the projection matrix and multiplies it with \p mat, marking the
866 * MAT_FLAG_PERSPECTIVE flag.
867 */
868 void
_math_matrix_frustum(GLmatrix * mat,GLfloat left,GLfloat right,GLfloat bottom,GLfloat top,GLfloat nearval,GLfloat farval)869 _math_matrix_frustum( GLmatrix *mat,
870 GLfloat left, GLfloat right,
871 GLfloat bottom, GLfloat top,
872 GLfloat nearval, GLfloat farval )
873 {
874 GLfloat x, y, a, b, c, d;
875 GLfloat m[16];
876
877 x = (2.0F*nearval) / (right-left);
878 y = (2.0F*nearval) / (top-bottom);
879 a = (right+left) / (right-left);
880 b = (top+bottom) / (top-bottom);
881 c = -(farval+nearval) / ( farval-nearval);
882 d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
883
884 #define M(row,col) m[col*4+row]
885 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
886 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
887 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
888 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
889 #undef M
890
891 matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
892 }
893
894 /**
895 * Create an orthographic projection matrix.
896 *
897 * \param m float array in which to store the project matrix
898 * \param left left clipping plane coordinate.
899 * \param right right clipping plane coordinate.
900 * \param bottom bottom clipping plane coordinate.
901 * \param top top clipping plane coordinate.
902 * \param nearval distance to the near clipping plane.
903 * \param farval distance to the far clipping plane.
904 *
905 * Creates the projection matrix and stored the values in \p m. As with other
906 * OpenGL matrices, the data is stored in column-major ordering.
907 */
908 void
_math_float_ortho(float * m,float left,float right,float bottom,float top,float nearval,float farval)909 _math_float_ortho(float *m,
910 float left, float right,
911 float bottom, float top,
912 float nearval, float farval)
913 {
914 #define M(row,col) m[col*4+row]
915 M(0,0) = 2.0F / (right-left);
916 M(0,1) = 0.0F;
917 M(0,2) = 0.0F;
918 M(0,3) = -(right+left) / (right-left);
919
920 M(1,0) = 0.0F;
921 M(1,1) = 2.0F / (top-bottom);
922 M(1,2) = 0.0F;
923 M(1,3) = -(top+bottom) / (top-bottom);
924
925 M(2,0) = 0.0F;
926 M(2,1) = 0.0F;
927 M(2,2) = -2.0F / (farval-nearval);
928 M(2,3) = -(farval+nearval) / (farval-nearval);
929
930 M(3,0) = 0.0F;
931 M(3,1) = 0.0F;
932 M(3,2) = 0.0F;
933 M(3,3) = 1.0F;
934 #undef M
935 }
936
937 /**
938 * Apply an orthographic projection matrix.
939 *
940 * \param mat matrix to apply the projection.
941 * \param left left clipping plane coordinate.
942 * \param right right clipping plane coordinate.
943 * \param bottom bottom clipping plane coordinate.
944 * \param top top clipping plane coordinate.
945 * \param nearval distance to the near clipping plane.
946 * \param farval distance to the far clipping plane.
947 *
948 * Creates the projection matrix and multiplies it with \p mat, marking the
949 * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
950 */
951 void
_math_matrix_ortho(GLmatrix * mat,GLfloat left,GLfloat right,GLfloat bottom,GLfloat top,GLfloat nearval,GLfloat farval)952 _math_matrix_ortho( GLmatrix *mat,
953 GLfloat left, GLfloat right,
954 GLfloat bottom, GLfloat top,
955 GLfloat nearval, GLfloat farval )
956 {
957 GLfloat m[16];
958
959 _math_float_ortho(m, left, right, bottom, top, nearval, farval);
960 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
961 }
962
963 /**
964 * Multiply a matrix with a general scaling matrix.
965 *
966 * \param mat matrix.
967 * \param x x axis scale factor.
968 * \param y y axis scale factor.
969 * \param z z axis scale factor.
970 *
971 * Multiplies in-place the elements of \p mat by the scale factors. Checks if
972 * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE
973 * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and
974 * MAT_DIRTY_INVERSE dirty flags.
975 */
976 void
_math_matrix_scale(GLmatrix * mat,GLfloat x,GLfloat y,GLfloat z)977 _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
978 {
979 GLfloat *m = mat->m;
980 m[0] *= x; m[4] *= y; m[8] *= z;
981 m[1] *= x; m[5] *= y; m[9] *= z;
982 m[2] *= x; m[6] *= y; m[10] *= z;
983 m[3] *= x; m[7] *= y; m[11] *= z;
984
985 if (fabsf(x - y) < 1e-8F && fabsf(x - z) < 1e-8F)
986 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
987 else
988 mat->flags |= MAT_FLAG_GENERAL_SCALE;
989
990 mat->flags |= (MAT_DIRTY_TYPE |
991 MAT_DIRTY_INVERSE);
992 }
993
994 /**
995 * Multiply a matrix with a translation matrix.
996 *
997 * \param mat matrix.
998 * \param x translation vector x coordinate.
999 * \param y translation vector y coordinate.
1000 * \param z translation vector z coordinate.
1001 *
1002 * Adds the translation coordinates to the elements of \p mat in-place. Marks
1003 * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE
1004 * dirty flags.
1005 */
1006 void
_math_matrix_translate(GLmatrix * mat,GLfloat x,GLfloat y,GLfloat z)1007 _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1008 {
1009 GLfloat *m = mat->m;
1010 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1011 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1012 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1013 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1014
1015 mat->flags |= (MAT_FLAG_TRANSLATION |
1016 MAT_DIRTY_TYPE |
1017 MAT_DIRTY_INVERSE);
1018 }
1019
1020
1021 /**
1022 * Set matrix to do viewport and depthrange mapping.
1023 * Transforms Normalized Device Coords to window/Z values.
1024 */
1025 void
_math_matrix_viewport(GLmatrix * m,const float scale[3],const float translate[3],double depthMax)1026 _math_matrix_viewport(GLmatrix *m, const float scale[3],
1027 const float translate[3], double depthMax)
1028 {
1029 m->m[MAT_SX] = scale[0];
1030 m->m[MAT_TX] = translate[0];
1031 m->m[MAT_SY] = scale[1];
1032 m->m[MAT_TY] = translate[1];
1033 m->m[MAT_SZ] = depthMax*scale[2];
1034 m->m[MAT_TZ] = depthMax*translate[2];
1035 m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION;
1036 m->type = MATRIX_3D_NO_ROT;
1037 }
1038
1039
1040 /**
1041 * Set a matrix to the identity matrix.
1042 *
1043 * \param mat matrix.
1044 *
1045 * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL.
1046 * Sets the matrix type to identity, and clear the dirty flags.
1047 */
1048 void
_math_matrix_set_identity(GLmatrix * mat)1049 _math_matrix_set_identity( GLmatrix *mat )
1050 {
1051 STATIC_ASSERT(MATRIX_M == offsetof(GLmatrix, m));
1052 STATIC_ASSERT(MATRIX_INV == offsetof(GLmatrix, inv));
1053
1054 memcpy( mat->m, Identity, sizeof(Identity) );
1055 memcpy( mat->inv, Identity, sizeof(Identity) );
1056
1057 mat->type = MATRIX_IDENTITY;
1058 mat->flags &= ~(MAT_DIRTY_FLAGS|
1059 MAT_DIRTY_TYPE|
1060 MAT_DIRTY_INVERSE);
1061 }
1062
1063 /*@}*/
1064
1065
1066 /**********************************************************************/
1067 /** \name Matrix analysis */
1068 /*@{*/
1069
1070 #define ZERO(x) (1<<x)
1071 #define ONE(x) (1<<(x+16))
1072
1073 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
1074 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
1075
1076 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
1077 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
1078 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1079 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1080
1081 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
1082 ZERO(1) | ZERO(9) | \
1083 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1084 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1085
1086 #define MASK_2D ( ZERO(8) | \
1087 ZERO(9) | \
1088 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1089 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1090
1091
1092 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
1093 ZERO(1) | ZERO(9) | \
1094 ZERO(2) | ZERO(6) | \
1095 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1096
1097 #define MASK_3D ( \
1098 \
1099 \
1100 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1101
1102
1103 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
1104 ZERO(1) | ZERO(13) |\
1105 ZERO(2) | ZERO(6) | \
1106 ZERO(3) | ZERO(7) | ZERO(15) )
1107
1108 #define SQ(x) ((x)*(x))
1109
1110 /**
1111 * Determine type and flags from scratch.
1112 *
1113 * \param mat matrix.
1114 *
1115 * This is expensive enough to only want to do it once.
1116 */
analyse_from_scratch(GLmatrix * mat)1117 static void analyse_from_scratch( GLmatrix *mat )
1118 {
1119 const GLfloat *m = mat->m;
1120 GLuint mask = 0;
1121 GLuint i;
1122
1123 for (i = 0 ; i < 16 ; i++) {
1124 if (m[i] == 0.0F) mask |= (1<<i);
1125 }
1126
1127 if (m[0] == 1.0F) mask |= (1<<16);
1128 if (m[5] == 1.0F) mask |= (1<<21);
1129 if (m[10] == 1.0F) mask |= (1<<26);
1130 if (m[15] == 1.0F) mask |= (1<<31);
1131
1132 mat->flags &= ~MAT_FLAGS_GEOMETRY;
1133
1134 /* Check for translation - no-one really cares
1135 */
1136 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
1137 mat->flags |= MAT_FLAG_TRANSLATION;
1138
1139 /* Do the real work
1140 */
1141 if (mask == (GLuint) MASK_IDENTITY) {
1142 mat->type = MATRIX_IDENTITY;
1143 }
1144 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
1145 mat->type = MATRIX_2D_NO_ROT;
1146
1147 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
1148 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1149 }
1150 else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
1151 GLfloat mm = DOT2(m, m);
1152 GLfloat m4m4 = DOT2(m+4,m+4);
1153 GLfloat mm4 = DOT2(m,m+4);
1154
1155 mat->type = MATRIX_2D;
1156
1157 /* Check for scale */
1158 if (SQ(mm-1) > SQ(1e-6F) ||
1159 SQ(m4m4-1) > SQ(1e-6F))
1160 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1161
1162 /* Check for rotation */
1163 if (SQ(mm4) > SQ(1e-6F))
1164 mat->flags |= MAT_FLAG_GENERAL_3D;
1165 else
1166 mat->flags |= MAT_FLAG_ROTATION;
1167
1168 }
1169 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
1170 mat->type = MATRIX_3D_NO_ROT;
1171
1172 /* Check for scale */
1173 if (SQ(m[0]-m[5]) < SQ(1e-6F) &&
1174 SQ(m[0]-m[10]) < SQ(1e-6F)) {
1175 if (SQ(m[0]-1.0F) > SQ(1e-6F)) {
1176 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1177 }
1178 }
1179 else {
1180 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1181 }
1182 }
1183 else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
1184 GLfloat c1 = DOT3(m,m);
1185 GLfloat c2 = DOT3(m+4,m+4);
1186 GLfloat c3 = DOT3(m+8,m+8);
1187 GLfloat d1 = DOT3(m, m+4);
1188 GLfloat cp[3];
1189
1190 mat->type = MATRIX_3D;
1191
1192 /* Check for scale */
1193 if (SQ(c1-c2) < SQ(1e-6F) && SQ(c1-c3) < SQ(1e-6F)) {
1194 if (SQ(c1-1.0F) > SQ(1e-6F))
1195 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1196 /* else no scale at all */
1197 }
1198 else {
1199 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1200 }
1201
1202 /* Check for rotation */
1203 if (SQ(d1) < SQ(1e-6F)) {
1204 CROSS3( cp, m, m+4 );
1205 SUB_3V( cp, cp, (m+8) );
1206 if (LEN_SQUARED_3FV(cp) < SQ(1e-6F))
1207 mat->flags |= MAT_FLAG_ROTATION;
1208 else
1209 mat->flags |= MAT_FLAG_GENERAL_3D;
1210 }
1211 else {
1212 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
1213 }
1214 }
1215 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
1216 mat->type = MATRIX_PERSPECTIVE;
1217 mat->flags |= MAT_FLAG_GENERAL;
1218 }
1219 else {
1220 mat->type = MATRIX_GENERAL;
1221 mat->flags |= MAT_FLAG_GENERAL;
1222 }
1223 }
1224
1225 /**
1226 * Analyze a matrix given that its flags are accurate.
1227 *
1228 * This is the more common operation, hopefully.
1229 */
analyse_from_flags(GLmatrix * mat)1230 static void analyse_from_flags( GLmatrix *mat )
1231 {
1232 const GLfloat *m = mat->m;
1233
1234 if (TEST_MAT_FLAGS(mat, 0)) {
1235 mat->type = MATRIX_IDENTITY;
1236 }
1237 else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
1238 MAT_FLAG_UNIFORM_SCALE |
1239 MAT_FLAG_GENERAL_SCALE))) {
1240 if ( m[10]==1.0F && m[14]==0.0F ) {
1241 mat->type = MATRIX_2D_NO_ROT;
1242 }
1243 else {
1244 mat->type = MATRIX_3D_NO_ROT;
1245 }
1246 }
1247 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
1248 if ( m[ 8]==0.0F
1249 && m[ 9]==0.0F
1250 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
1251 mat->type = MATRIX_2D;
1252 }
1253 else {
1254 mat->type = MATRIX_3D;
1255 }
1256 }
1257 else if ( m[4]==0.0F && m[12]==0.0F
1258 && m[1]==0.0F && m[13]==0.0F
1259 && m[2]==0.0F && m[6]==0.0F
1260 && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
1261 mat->type = MATRIX_PERSPECTIVE;
1262 }
1263 else {
1264 mat->type = MATRIX_GENERAL;
1265 }
1266 }
1267
1268 /**
1269 * Analyze and update a matrix.
1270 *
1271 * \param mat matrix.
1272 *
1273 * If the matrix type is dirty then calls either analyse_from_scratch() or
1274 * analyse_from_flags() to determine its type, according to whether the flags
1275 * are dirty or not, respectively. If the matrix has an inverse and it's dirty
1276 * then calls matrix_invert(). Finally clears the dirty flags.
1277 */
1278 void
_math_matrix_analyse(GLmatrix * mat)1279 _math_matrix_analyse( GLmatrix *mat )
1280 {
1281 if (mat->flags & MAT_DIRTY_TYPE) {
1282 if (mat->flags & MAT_DIRTY_FLAGS)
1283 analyse_from_scratch( mat );
1284 else
1285 analyse_from_flags( mat );
1286 }
1287
1288 if (mat->flags & MAT_DIRTY_INVERSE) {
1289 matrix_invert( mat );
1290 mat->flags &= ~MAT_DIRTY_INVERSE;
1291 }
1292
1293 mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
1294 }
1295
1296 /*@}*/
1297
1298
1299 /**
1300 * Test if the given matrix preserves vector lengths.
1301 */
1302 GLboolean
_math_matrix_is_length_preserving(const GLmatrix * m)1303 _math_matrix_is_length_preserving( const GLmatrix *m )
1304 {
1305 return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING);
1306 }
1307
1308
1309 /**
1310 * Test if the given matrix does any rotation.
1311 * (or perhaps if the upper-left 3x3 is non-identity)
1312 */
1313 GLboolean
_math_matrix_has_rotation(const GLmatrix * m)1314 _math_matrix_has_rotation( const GLmatrix *m )
1315 {
1316 if (m->flags & (MAT_FLAG_GENERAL |
1317 MAT_FLAG_ROTATION |
1318 MAT_FLAG_GENERAL_3D |
1319 MAT_FLAG_PERSPECTIVE))
1320 return GL_TRUE;
1321 else
1322 return GL_FALSE;
1323 }
1324
1325
1326 GLboolean
_math_matrix_is_general_scale(const GLmatrix * m)1327 _math_matrix_is_general_scale( const GLmatrix *m )
1328 {
1329 return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE;
1330 }
1331
1332
1333 GLboolean
_math_matrix_is_dirty(const GLmatrix * m)1334 _math_matrix_is_dirty( const GLmatrix *m )
1335 {
1336 return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE;
1337 }
1338
1339
1340 /**********************************************************************/
1341 /** \name Matrix setup */
1342 /*@{*/
1343
1344 /**
1345 * Copy a matrix.
1346 *
1347 * \param to destination matrix.
1348 * \param from source matrix.
1349 *
1350 * Copies all fields in GLmatrix, creating an inverse array if necessary.
1351 */
1352 void
_math_matrix_copy(GLmatrix * to,const GLmatrix * from)1353 _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
1354 {
1355 memcpy(to->m, from->m, 16 * sizeof(GLfloat));
1356 memcpy(to->inv, from->inv, 16 * sizeof(GLfloat));
1357 to->flags = from->flags;
1358 to->type = from->type;
1359 }
1360
1361 /**
1362 * Copy a matrix as part of glPushMatrix.
1363 *
1364 * The makes the source matrix canonical (inverse and flags are up-to-date),
1365 * so that later glPopMatrix is evaluated as a no-op if there is no state
1366 * change.
1367 *
1368 * It this wasn't done, a draw call would canonicalize the matrix, which
1369 * would make it different from the pushed one and so glPopMatrix wouldn't be
1370 * recognized as a no-op.
1371 */
1372 void
_math_matrix_push_copy(GLmatrix * to,GLmatrix * from)1373 _math_matrix_push_copy(GLmatrix *to, GLmatrix *from)
1374 {
1375 if (from->flags & MAT_DIRTY)
1376 _math_matrix_analyse(from);
1377
1378 _math_matrix_copy(to, from);
1379 }
1380
1381 /**
1382 * Loads a matrix array into GLmatrix.
1383 *
1384 * \param m matrix array.
1385 * \param mat matrix.
1386 *
1387 * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
1388 * flags.
1389 */
1390 void
_math_matrix_loadf(GLmatrix * mat,const GLfloat * m)1391 _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1392 {
1393 memcpy( mat->m, m, 16*sizeof(GLfloat) );
1394 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1395 }
1396
1397 /**
1398 * Matrix constructor.
1399 *
1400 * \param m matrix.
1401 *
1402 * Initialize the GLmatrix fields.
1403 */
1404 void
_math_matrix_ctr(GLmatrix * m)1405 _math_matrix_ctr( GLmatrix *m )
1406 {
1407 memset(m, 0, sizeof(*m));
1408 memcpy( m->m, Identity, sizeof(Identity) );
1409 memcpy( m->inv, Identity, sizeof(Identity) );
1410 m->type = MATRIX_IDENTITY;
1411 m->flags = 0;
1412 }
1413
1414 /*@}*/
1415
1416
1417 /**********************************************************************/
1418 /** \name Matrix transpose */
1419 /*@{*/
1420
1421 /**
1422 * Transpose a GLfloat matrix.
1423 *
1424 * \param to destination array.
1425 * \param from source array.
1426 */
1427 void
_math_transposef(GLfloat to[16],const GLfloat from[16])1428 _math_transposef( GLfloat to[16], const GLfloat from[16] )
1429 {
1430 to[0] = from[0];
1431 to[1] = from[4];
1432 to[2] = from[8];
1433 to[3] = from[12];
1434 to[4] = from[1];
1435 to[5] = from[5];
1436 to[6] = from[9];
1437 to[7] = from[13];
1438 to[8] = from[2];
1439 to[9] = from[6];
1440 to[10] = from[10];
1441 to[11] = from[14];
1442 to[12] = from[3];
1443 to[13] = from[7];
1444 to[14] = from[11];
1445 to[15] = from[15];
1446 }
1447
1448 /**
1449 * Transpose a GLdouble matrix.
1450 *
1451 * \param to destination array.
1452 * \param from source array.
1453 */
1454 void
_math_transposed(GLdouble to[16],const GLdouble from[16])1455 _math_transposed( GLdouble to[16], const GLdouble from[16] )
1456 {
1457 to[0] = from[0];
1458 to[1] = from[4];
1459 to[2] = from[8];
1460 to[3] = from[12];
1461 to[4] = from[1];
1462 to[5] = from[5];
1463 to[6] = from[9];
1464 to[7] = from[13];
1465 to[8] = from[2];
1466 to[9] = from[6];
1467 to[10] = from[10];
1468 to[11] = from[14];
1469 to[12] = from[3];
1470 to[13] = from[7];
1471 to[14] = from[11];
1472 to[15] = from[15];
1473 }
1474
1475 /**
1476 * Transpose a GLdouble matrix and convert to GLfloat.
1477 *
1478 * \param to destination array.
1479 * \param from source array.
1480 */
1481 void
_math_transposefd(GLfloat to[16],const GLdouble from[16])1482 _math_transposefd( GLfloat to[16], const GLdouble from[16] )
1483 {
1484 to[0] = (GLfloat) from[0];
1485 to[1] = (GLfloat) from[4];
1486 to[2] = (GLfloat) from[8];
1487 to[3] = (GLfloat) from[12];
1488 to[4] = (GLfloat) from[1];
1489 to[5] = (GLfloat) from[5];
1490 to[6] = (GLfloat) from[9];
1491 to[7] = (GLfloat) from[13];
1492 to[8] = (GLfloat) from[2];
1493 to[9] = (GLfloat) from[6];
1494 to[10] = (GLfloat) from[10];
1495 to[11] = (GLfloat) from[14];
1496 to[12] = (GLfloat) from[3];
1497 to[13] = (GLfloat) from[7];
1498 to[14] = (GLfloat) from[11];
1499 to[15] = (GLfloat) from[15];
1500 }
1501
1502 /*@}*/
1503
1504
1505 /**
1506 * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This
1507 * function is used for transforming clipping plane equations and spotlight
1508 * directions.
1509 * Mathematically, u = v * m.
1510 * Input: v - input vector
1511 * m - transformation matrix
1512 * Output: u - transformed vector
1513 */
1514 void
_mesa_transform_vector(GLfloat u[4],const GLfloat v[4],const GLfloat m[16])1515 _mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
1516 {
1517 const GLfloat v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
1518 #define M(row,col) m[row + col*4]
1519 u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
1520 u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
1521 u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
1522 u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);
1523 #undef M
1524 }
1525