blob: b4ba1bc2a08b97cd56137229a9c034bff996ec89 [file] [log] [blame]
Keith Whitwell23caf202000-11-16 21:05:34 +00001/*
2 * Mesa 3-D graphics library
Brian Paul522ea422005-03-11 14:54:51 +00003 * Version: 6.3
Gareth Hughes22144ab2001-03-12 00:48:37 +00004 *
Brian Paul522ea422005-03-11 14:54:51 +00005 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
Gareth Hughes22144ab2001-03-12 00:48:37 +00006 *
Keith Whitwell23caf202000-11-16 21:05:34 +00007 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
Gareth Hughes22144ab2001-03-12 00:48:37 +000013 *
Keith Whitwell23caf202000-11-16 21:05:34 +000014 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
Gareth Hughes22144ab2001-03-12 00:48:37 +000016 *
Keith Whitwell23caf202000-11-16 21:05:34 +000017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
Brian Paul2dab9972004-09-09 19:58:03 +000026/**
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
Keith Whitwell23caf202000-11-16 21:05:34 +000037#include "glheader.h"
Brian Paul4e9676f2002-06-29 19:48:15 +000038#include "imports.h"
Keith Whitwell23caf202000-11-16 21:05:34 +000039#include "macros.h"
Brian Paul3c634522002-10-24 23:57:19 +000040#include "imports.h"
Keith Whitwell23caf202000-11-16 21:05:34 +000041
42#include "m_matrix.h"
43
Keith Whitwellf4b02d12001-01-05 05:31:42 +000044
Keith Whitwell6dc85572003-07-17 13:43:59 +000045/**
Brian Paul049e3202005-06-30 14:22:23 +000046 * \defgroup MatFlags MAT_FLAG_XXX-flags
47 *
48 * Bitmasks to indicate different kinds of 4x4 matrices in GLmatrix::flags
49 * It would be nice to make all these flags private to m_matrix.c
50 */
51/*@{*/
52#define MAT_FLAG_IDENTITY 0 /**< is an identity matrix flag.
53 * (Not actually used - the identity
54 * matrix is identified by the absense
55 * of all other flags.)
56 */
57#define MAT_FLAG_GENERAL 0x1 /**< is a general matrix flag */
58#define MAT_FLAG_ROTATION 0x2 /**< is a rotation matrix flag */
59#define MAT_FLAG_TRANSLATION 0x4 /**< is a translation matrix flag */
60#define MAT_FLAG_UNIFORM_SCALE 0x8 /**< is an uniform scaling matrix flag */
61#define MAT_FLAG_GENERAL_SCALE 0x10 /**< is a general scaling matrix flag */
62#define MAT_FLAG_GENERAL_3D 0x20 /**< general 3D matrix flag */
63#define MAT_FLAG_PERSPECTIVE 0x40 /**< is a perspective proj matrix flag */
64#define MAT_FLAG_SINGULAR 0x80 /**< is a singular matrix flag */
65#define MAT_DIRTY_TYPE 0x100 /**< matrix type is dirty */
66#define MAT_DIRTY_FLAGS 0x200 /**< matrix flags are dirty */
67#define MAT_DIRTY_INVERSE 0x400 /**< matrix inverse is dirty */
68
69/** angle preserving matrix flags mask */
70#define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \
71 MAT_FLAG_TRANSLATION | \
72 MAT_FLAG_UNIFORM_SCALE)
73
74/** geometry related matrix flags mask */
75#define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \
76 MAT_FLAG_ROTATION | \
77 MAT_FLAG_TRANSLATION | \
78 MAT_FLAG_UNIFORM_SCALE | \
79 MAT_FLAG_GENERAL_SCALE | \
80 MAT_FLAG_GENERAL_3D | \
81 MAT_FLAG_PERSPECTIVE | \
82 MAT_FLAG_SINGULAR)
83
84/** length preserving matrix flags mask */
85#define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \
86 MAT_FLAG_TRANSLATION)
87
88
89/** 3D (non-perspective) matrix flags mask */
90#define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \
91 MAT_FLAG_TRANSLATION | \
92 MAT_FLAG_UNIFORM_SCALE | \
93 MAT_FLAG_GENERAL_SCALE | \
94 MAT_FLAG_GENERAL_3D)
95
96/** dirty matrix flags mask */
97#define MAT_DIRTY (MAT_DIRTY_TYPE | \
98 MAT_DIRTY_FLAGS | \
99 MAT_DIRTY_INVERSE)
100
101/*@}*/
102
103
104/**
105 * Test geometry related matrix flags.
106 *
107 * \param mat a pointer to a GLmatrix structure.
108 * \param a flags mask.
109 *
110 * \returns non-zero if all geometry related matrix flags are contained within
111 * the mask, or zero otherwise.
112 */
113#define TEST_MAT_FLAGS(mat, a) \
114 ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)
115
116
117
118/**
Keith Whitwell6dc85572003-07-17 13:43:59 +0000119 * Names of the corresponding GLmatrixtype values.
120 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000121static const char *types[] = {
122 "MATRIX_GENERAL",
123 "MATRIX_IDENTITY",
124 "MATRIX_3D_NO_ROT",
125 "MATRIX_PERSPECTIVE",
126 "MATRIX_2D",
127 "MATRIX_2D_NO_ROT",
128 "MATRIX_3D"
129};
130
131
Keith Whitwell6dc85572003-07-17 13:43:59 +0000132/**
133 * Identity matrix.
134 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000135static GLfloat Identity[16] = {
136 1.0, 0.0, 0.0, 0.0,
137 0.0, 1.0, 0.0, 0.0,
138 0.0, 0.0, 1.0, 0.0,
139 0.0, 0.0, 0.0, 1.0
140};
141
142
143
Keith Whitwell6dc85572003-07-17 13:43:59 +0000144/**********************************************************************/
145/** \name Matrix multiplication */
146/*@{*/
Keith Whitwell23caf202000-11-16 21:05:34 +0000147
Keith Whitwell23caf202000-11-16 21:05:34 +0000148#define A(row,col) a[(col<<2)+row]
149#define B(row,col) b[(col<<2)+row]
150#define P(row,col) product[(col<<2)+row]
151
Keith Whitwell6dc85572003-07-17 13:43:59 +0000152/**
153 * Perform a full 4x4 matrix multiplication.
154 *
155 * \param a matrix.
156 * \param b matrix.
157 * \param product will receive the product of \p a and \p b.
158 *
159 * \warning Is assumed that \p product != \p b. \p product == \p a is allowed.
160 *
161 * \note KW: 4*16 = 64 multiplications
162 *
163 * \author This \c matmul was contributed by Thomas Malik
164 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000165static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
166{
167 GLint i;
168 for (i = 0; i < 4; i++) {
169 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
170 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
171 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
172 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
173 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
174 }
175}
176
Keith Whitwell6dc85572003-07-17 13:43:59 +0000177/**
178 * Multiply two matrices known to occupy only the top three rows, such
179 * as typical model matrices, and orthogonal matrices.
180 *
181 * \param a matrix.
182 * \param b matrix.
183 * \param product will receive the product of \p a and \p b.
Keith Whitwell23caf202000-11-16 21:05:34 +0000184 */
185static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
186{
187 GLint i;
188 for (i = 0; i < 3; i++) {
189 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
190 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
191 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
192 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
193 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
194 }
195 P(3,0) = 0;
196 P(3,1) = 0;
197 P(3,2) = 0;
198 P(3,3) = 1;
199}
200
Keith Whitwell23caf202000-11-16 21:05:34 +0000201#undef A
202#undef B
203#undef P
204
Keith Whitwell6dc85572003-07-17 13:43:59 +0000205/**
Keith Whitwell23caf202000-11-16 21:05:34 +0000206 * Multiply a matrix by an array of floats with known properties.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000207 *
208 * \param mat pointer to a GLmatrix structure containing the left multiplication
209 * matrix, and that will receive the product result.
210 * \param m right multiplication matrix array.
211 * \param flags flags of the matrix \p m.
212 *
213 * Joins both flags and marks the type and inverse as dirty. Calls matmul34()
214 * if both matrices are 3D, or matmul4() otherwise.
Keith Whitwell23caf202000-11-16 21:05:34 +0000215 */
216static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
217{
218 mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
219
220 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
221 matmul34( mat->m, mat->m, m );
Gareth Hughes22144ab2001-03-12 00:48:37 +0000222 else
223 matmul4( mat->m, mat->m, m );
Keith Whitwell23caf202000-11-16 21:05:34 +0000224}
225
Keith Whitwell6dc85572003-07-17 13:43:59 +0000226/**
227 * Matrix multiplication.
228 *
229 * \param dest destination matrix.
230 * \param a left matrix.
231 * \param b right matrix.
232 *
233 * Joins both flags and marks the type and inverse as dirty. Calls matmul34()
234 * if both matrices are 3D, or matmul4() otherwise.
235 */
236void
237_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
238{
239 dest->flags = (a->flags |
240 b->flags |
241 MAT_DIRTY_TYPE |
242 MAT_DIRTY_INVERSE);
Keith Whitwell23caf202000-11-16 21:05:34 +0000243
Keith Whitwell6dc85572003-07-17 13:43:59 +0000244 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
245 matmul34( dest->m, a->m, b->m );
246 else
247 matmul4( dest->m, a->m, b->m );
248}
249
250/**
251 * Matrix multiplication.
252 *
253 * \param dest left and destination matrix.
254 * \param m right matrix array.
255 *
256 * Marks the matrix flags with general flag, and type and inverse dirty flags.
257 * Calls matmul4() for the multiplication.
258 */
259void
260_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
261{
262 dest->flags |= (MAT_FLAG_GENERAL |
263 MAT_DIRTY_TYPE |
Brian Paul522ea422005-03-11 14:54:51 +0000264 MAT_DIRTY_INVERSE |
265 MAT_DIRTY_FLAGS);
Keith Whitwell6dc85572003-07-17 13:43:59 +0000266
267 matmul4( dest->m, dest->m, m );
268}
269
270/*@}*/
271
272
273/**********************************************************************/
274/** \name Matrix output */
275/*@{*/
276
277/**
278 * Print a matrix array.
279 *
280 * \param m matrix array.
281 *
282 * Called by _math_matrix_print() to print a matrix or its inverse.
283 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000284static void print_matrix_floats( const GLfloat m[16] )
285{
286 int i;
287 for (i=0;i<4;i++) {
Brian Paul4e9676f2002-06-29 19:48:15 +0000288 _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
Keith Whitwell23caf202000-11-16 21:05:34 +0000289 }
290}
291
Keith Whitwell6dc85572003-07-17 13:43:59 +0000292/**
293 * Dumps the contents of a GLmatrix structure.
294 *
295 * \param m pointer to the GLmatrix structure.
296 */
Gareth Hughes22144ab2001-03-12 00:48:37 +0000297void
Keith Whitwell23caf202000-11-16 21:05:34 +0000298_math_matrix_print( const GLmatrix *m )
299{
Brian Paul4e9676f2002-06-29 19:48:15 +0000300 _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
Keith Whitwell23caf202000-11-16 21:05:34 +0000301 print_matrix_floats(m->m);
Brian Paul4e9676f2002-06-29 19:48:15 +0000302 _mesa_debug(NULL, "Inverse: \n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000303 if (m->inv) {
304 GLfloat prod[16];
305 print_matrix_floats(m->inv);
306 matmul4(prod, m->m, m->inv);
Brian Paul4e9676f2002-06-29 19:48:15 +0000307 _mesa_debug(NULL, "Mat * Inverse:\n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000308 print_matrix_floats(prod);
309 }
310 else {
Brian Paul4e9676f2002-06-29 19:48:15 +0000311 _mesa_debug(NULL, " - not available\n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000312 }
313}
314
Keith Whitwell6dc85572003-07-17 13:43:59 +0000315/*@}*/
Keith Whitwell23caf202000-11-16 21:05:34 +0000316
317
Keith Whitwell6dc85572003-07-17 13:43:59 +0000318/**
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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000329#define MAT(m,r,c) (m)[(c)*4+(r)]
330
Keith Whitwell6dc85572003-07-17 13:43:59 +0000331
332/**********************************************************************/
333/** \name Matrix inversion */
334/*@{*/
335
336/**
337 * Swaps the values of two floating pointer 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/**
Keith Whitwell23caf202000-11-16 21:05:34 +0000344 * Compute inverse of 4x4 transformation matrix.
Keith Whitwell6dc85572003-07-17 13:43:59 +0000345 *
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
Keith Whitwell23caf202000-11-16 21:05:34 +0000352 * Code contributed by Jacques Leroy jle@star.be
Keith Whitwell6dc85572003-07-17 13:43:59 +0000353 *
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.
Keith Whitwell23caf202000-11-16 21:05:34 +0000357 */
358static 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;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000365
Keith Whitwell23caf202000-11-16 21:05:34 +0000366 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
Gareth Hughes22144ab2001-03-12 00:48:37 +0000367
Keith Whitwell23caf202000-11-16 21:05:34 +0000368 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,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000371
Keith Whitwell23caf202000-11-16 21:05:34 +0000372 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,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000375
Keith Whitwell23caf202000-11-16 21:05:34 +0000376 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,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000379
Keith Whitwell23caf202000-11-16 21:05:34 +0000380 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;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000383
Keith Whitwell23caf202000-11-16 21:05:34 +0000384 /* choose pivot - or die */
Brian Paulb3aefd12005-09-19 20:12:32 +0000385 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);
Keith Whitwell23caf202000-11-16 21:05:34 +0000388 if (0.0 == r0[0]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000389
Keith Whitwell23caf202000-11-16 21:05:34 +0000390 /* 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.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
397 s = r0[5];
398 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
399 s = r0[6];
400 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
401 s = r0[7];
402 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000403
Keith Whitwell23caf202000-11-16 21:05:34 +0000404 /* choose pivot - or die */
Brian Paulb3aefd12005-09-19 20:12:32 +0000405 if (FABSF(r3[1])>FABSF(r2[1])) SWAP_ROWS(r3, r2);
406 if (FABSF(r2[1])>FABSF(r1[1])) SWAP_ROWS(r2, r1);
Keith Whitwell23caf202000-11-16 21:05:34 +0000407 if (0.0 == r1[1]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000408
Keith Whitwell23caf202000-11-16 21:05:34 +0000409 /* 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.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
414 s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
415 s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
416 s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000417
Keith Whitwell23caf202000-11-16 21:05:34 +0000418 /* choose pivot - or die */
Brian Paulb3aefd12005-09-19 20:12:32 +0000419 if (FABSF(r3[2])>FABSF(r2[2])) SWAP_ROWS(r3, r2);
Keith Whitwell23caf202000-11-16 21:05:34 +0000420 if (0.0 == r2[2]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000421
Keith Whitwell23caf202000-11-16 21:05:34 +0000422 /* 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];
Gareth Hughes22144ab2001-03-12 00:48:37 +0000427
Keith Whitwell23caf202000-11-16 21:05:34 +0000428 /* last check */
429 if (0.0 == r3[3]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000430
Karl Schultz7b9fe822001-09-18 23:06:14 +0000431 s = 1.0F/r3[3]; /* now back substitute row 3 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000432 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000433
Keith Whitwell23caf202000-11-16 21:05:34 +0000434 m2 = r2[3]; /* now back substitute row 2 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000435 s = 1.0F/r2[2];
Keith Whitwell23caf202000-11-16 21:05:34 +0000436 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;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000444
Keith Whitwell23caf202000-11-16 21:05:34 +0000445 m1 = r1[2]; /* now back substitute row 1 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000446 s = 1.0F/r1[1];
Keith Whitwell23caf202000-11-16 21:05:34 +0000447 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;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000452
Keith Whitwell23caf202000-11-16 21:05:34 +0000453 m0 = r0[1]; /* now back substitute row 0 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000454 s = 1.0F/r0[0];
Keith Whitwell23caf202000-11-16 21:05:34 +0000455 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);
Gareth Hughes22144ab2001-03-12 00:48:37 +0000457
Keith Whitwell23caf202000-11-16 21:05:34 +0000458 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],
Gareth Hughes22144ab2001-03-12 00:48:37 +0000465 MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
466
Keith Whitwell23caf202000-11-16 21:05:34 +0000467 return GL_TRUE;
468}
469#undef SWAP_ROWS
470
Keith Whitwell6dc85572003-07-17 13:43:59 +0000471/**
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.
Gareth Hughes22144ab2001-03-12 00:48:37 +0000485 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000486static 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
Gareth Hughes22144ab2001-03-12 00:48:37 +0000494 * determine if the matrix is singular.
Keith Whitwell23caf202000-11-16 21:05:34 +0000495 */
496 pos = neg = 0.0;
497 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
498 if (t >= 0.0) pos += t; else neg += t;
499
500 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
501 if (t >= 0.0) pos += t; else neg += t;
502
503 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
504 if (t >= 0.0) pos += t; else neg += t;
505
506 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
507 if (t >= 0.0) pos += t; else neg += t;
508
509 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
510 if (t >= 0.0) pos += t; else neg += t;
511
512 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
513 if (t >= 0.0) pos += t; else neg += t;
514
515 det = pos + neg;
516
Gareth Hughes22144ab2001-03-12 00:48:37 +0000517 if (det*det < 1e-25)
Keith Whitwell23caf202000-11-16 21:05:34 +0000518 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000519
Karl Schultz7b9fe822001-09-18 23:06:14 +0000520 det = 1.0F / det;
Keith Whitwell23caf202000-11-16 21:05:34 +0000521 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) );
Gareth Hughes22144ab2001-03-12 00:48:37 +0000541
Keith Whitwell23caf202000-11-16 21:05:34 +0000542 return GL_TRUE;
543}
544
Keith Whitwell6dc85572003-07-17 13:43:59 +0000545/**
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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000558static 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 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000566
Keith Whitwell23caf202000-11-16 21:05:34 +0000567 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
Gareth Hughes22144ab2001-03-12 00:48:37 +0000572 if (scale == 0.0)
Keith Whitwell23caf202000-11-16 21:05:34 +0000573 return GL_FALSE;
574
Karl Schultz7b9fe822001-09-18 23:06:14 +0000575 scale = 1.0F / scale;
Keith Whitwell23caf202000-11-16 21:05:34 +0000576
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 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000608
Keith Whitwell23caf202000-11-16 21:05:34 +0000609 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 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000624
Keith Whitwell23caf202000-11-16 21:05:34 +0000625 return GL_TRUE;
626}
627
Keith Whitwell6dc85572003-07-17 13:43:59 +0000628/**
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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000638static GLboolean invert_matrix_identity( GLmatrix *mat )
639{
640 MEMCPY( mat->inv, Identity, sizeof(Identity) );
641 return GL_TRUE;
642}
643
Keith Whitwell6dc85572003-07-17 13:43:59 +0000644/**
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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000654static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
655{
656 const GLfloat *in = mat->m;
657 GLfloat *out = mat->inv;
658
Gareth Hughes22144ab2001-03-12 00:48:37 +0000659 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
Keith Whitwell23caf202000-11-16 21:05:34 +0000660 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000661
Keith Whitwell23caf202000-11-16 21:05:34 +0000662 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
Karl Schultz7b9fe822001-09-18 23:06:14 +0000663 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);
Keith Whitwell23caf202000-11-16 21:05:34 +0000666
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
Keith Whitwell6dc85572003-07-17 13:43:59 +0000676/**
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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000687static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
688{
689 const GLfloat *in = mat->m;
690 GLfloat *out = mat->inv;
691
Gareth Hughes22144ab2001-03-12 00:48:37 +0000692 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
Keith Whitwell23caf202000-11-16 21:05:34 +0000693 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000694
Keith Whitwell23caf202000-11-16 21:05:34 +0000695 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
Karl Schultz7b9fe822001-09-18 23:06:14 +0000696 MAT(out,0,0) = 1.0F / MAT(in,0,0);
697 MAT(out,1,1) = 1.0F / MAT(in,1,1);
Keith Whitwell23caf202000-11-16 21:05:34 +0000698
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
Brian Paul4e9676f2002-06-29 19:48:15 +0000707#if 0
708/* broken */
Keith Whitwell23caf202000-11-16 21:05:34 +0000709static 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, 16 * sizeof(GLfloat) );
718
Karl Schultz7b9fe822001-09-18 23:06:14 +0000719 MAT(out,0,0) = 1.0F / MAT(in,0,0);
720 MAT(out,1,1) = 1.0F / MAT(in,1,1);
Keith Whitwell23caf202000-11-16 21:05:34 +0000721
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
Karl Schultz7b9fe822001-09-18 23:06:14 +0000728 MAT(out,3,2) = 1.0F / MAT(in,2,3);
Keith Whitwell23caf202000-11-16 21:05:34 +0000729 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
730
731 return GL_TRUE;
732}
Brian Paul4e9676f2002-06-29 19:48:15 +0000733#endif
Keith Whitwell23caf202000-11-16 21:05:34 +0000734
Keith Whitwell6dc85572003-07-17 13:43:59 +0000735/**
736 * Matrix inversion function pointer type.
737 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000738typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
739
Keith Whitwell6dc85572003-07-17 13:43:59 +0000740/**
741 * Table of the matrix inversion functions according to the matrix type.
742 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000743static inv_mat_func inv_mat_tab[7] = {
744 invert_matrix_general,
745 invert_matrix_identity,
746 invert_matrix_3d_no_rot,
Brian Paula68b8df2002-03-29 17:18:08 +0000747#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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000751 invert_matrix_perspective,
Brian Paula68b8df2002-03-29 17:18:08 +0000752#else
753 invert_matrix_general,
754#endif
Keith Whitwell23caf202000-11-16 21:05:34 +0000755 invert_matrix_3d, /* lazy! */
756 invert_matrix_2d_no_rot,
757 invert_matrix_3d
758};
759
Keith Whitwell6dc85572003-07-17 13:43:59 +0000760/**
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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000772static 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;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000781 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000782}
783
Keith Whitwell6dc85572003-07-17 13:43:59 +0000784/*@}*/
Keith Whitwell23caf202000-11-16 21:05:34 +0000785
786
Keith Whitwell6dc85572003-07-17 13:43:59 +0000787/**********************************************************************/
788/** \name Matrix generation */
789/*@{*/
Keith Whitwell23caf202000-11-16 21:05:34 +0000790
Keith Whitwell6dc85572003-07-17 13:43:59 +0000791/**
Keith Whitwell23caf202000-11-16 21:05:34 +0000792 * Generate a 4x4 transformation matrix from glRotate parameters, and
Keith Whitwell6dc85572003-07-17 13:43:59 +0000793 * 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).
Keith Whitwell23caf202000-11-16 21:05:34 +0000798 */
Gareth Hughes22144ab2001-03-12 00:48:37 +0000799void
800_math_matrix_rotate( GLmatrix *mat,
Keith Whitwell23caf202000-11-16 21:05:34 +0000801 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
802{
Brian Paul4991d0f2002-09-12 16:26:04 +0000803 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
Keith Whitwell23caf202000-11-16 21:05:34 +0000804 GLfloat m[16];
Brian Paul4991d0f2002-09-12 16:26:04 +0000805 GLboolean optimized;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000806
Brian Paulaa206952005-09-16 18:14:24 +0000807 s = (GLfloat) _mesa_sin( angle * DEG2RAD );
808 c = (GLfloat) _mesa_cos( angle * DEG2RAD );
Keith Whitwell23caf202000-11-16 21:05:34 +0000809
Brian Paul4991d0f2002-09-12 16:26:04 +0000810 MEMCPY(m, Identity, sizeof(GLfloat)*16);
811 optimized = GL_FALSE;
Keith Whitwell23caf202000-11-16 21:05:34 +0000812
813#define M(row,col) m[col*4+row]
814
Brian Paul4991d0f2002-09-12 16:26:04 +0000815 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;
Brian Paul1e091f42003-01-08 16:42:47 +0000853 if (x < 0.0F) {
Brian Paul4991d0f2002-09-12 16:26:04 +0000854 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 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000863
Brian Paul4991d0f2002-09-12 16:26:04 +0000864 if (!optimized) {
Brian Paul27558a12003-03-01 01:50:20 +0000865 const GLfloat mag = SQRTF(x * x + y * y + z * z);
Keith Whitwell23caf202000-11-16 21:05:34 +0000866
Brian Paul4991d0f2002-09-12 16:26:04 +0000867 if (mag <= 1.0e-4) {
868 /* no rotation, leave mat as-is */
869 return;
870 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000871
Brian Paul4991d0f2002-09-12 16:26:04 +0000872 x /= mag;
873 y /= mag;
874 z /= mag;
Keith Whitwell23caf202000-11-16 21:05:34 +0000875
Keith Whitwell23caf202000-11-16 21:05:34 +0000876
Brian Paul4991d0f2002-09-12 16:26:04 +0000877 /*
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 it's 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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000929
Brian Paul4991d0f2002-09-12 16:26:04 +0000930 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 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000964#undef M
965
966 matrix_multf( mat, m, MAT_FLAG_ROTATION );
967}
968
Keith Whitwell6dc85572003-07-17 13:43:59 +0000969/**
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 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000983void
Gareth Hughes22144ab2001-03-12 00:48:37 +0000984_math_matrix_frustum( GLmatrix *mat,
Brian Pauld8bc5a92001-02-05 18:48:52 +0000985 GLfloat left, GLfloat right,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000986 GLfloat bottom, GLfloat top,
Brian Pauld8bc5a92001-02-05 18:48:52 +0000987 GLfloat nearval, GLfloat farval )
Keith Whitwell23caf202000-11-16 21:05:34 +0000988{
989 GLfloat x, y, a, b, c, d;
990 GLfloat m[16];
991
Karl Schultz7b9fe822001-09-18 23:06:14 +0000992 x = (2.0F*nearval) / (right-left);
993 y = (2.0F*nearval) / (top-bottom);
Keith Whitwell23caf202000-11-16 21:05:34 +0000994 a = (right+left) / (right-left);
995 b = (top+bottom) / (top-bottom);
996 c = -(farval+nearval) / ( farval-nearval);
Karl Schultz7b9fe822001-09-18 23:06:14 +0000997 d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
Keith Whitwell23caf202000-11-16 21:05:34 +0000998
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
Keith Whitwell6dc85572003-07-17 13:43:59 +00001009/**
1010 * Apply an orthographic projection matrix.
1011 *
1012 * \param mat matrix to apply the projection.
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 multiplies it with \p mat, marking the
1021 * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
1022 */
Keith Whitwell23caf202000-11-16 21:05:34 +00001023void
Gareth Hughes22144ab2001-03-12 00:48:37 +00001024_math_matrix_ortho( GLmatrix *mat,
Keith Whitwell23caf202000-11-16 21:05:34 +00001025 GLfloat left, GLfloat right,
Gareth Hughes22144ab2001-03-12 00:48:37 +00001026 GLfloat bottom, GLfloat top,
Keith Whitwell23caf202000-11-16 21:05:34 +00001027 GLfloat nearval, GLfloat farval )
1028{
Keith Whitwell23caf202000-11-16 21:05:34 +00001029 GLfloat m[16];
1030
Keith Whitwell23caf202000-11-16 21:05:34 +00001031#define M(row,col) m[col*4+row]
Brian Paule2e9dc22004-02-05 15:05:09 +00001032 M(0,0) = 2.0F / (right-left);
1033 M(0,1) = 0.0F;
1034 M(0,2) = 0.0F;
1035 M(0,3) = -(right+left) / (right-left);
1036
1037 M(1,0) = 0.0F;
1038 M(1,1) = 2.0F / (top-bottom);
1039 M(1,2) = 0.0F;
1040 M(1,3) = -(top+bottom) / (top-bottom);
1041
1042 M(2,0) = 0.0F;
1043 M(2,1) = 0.0F;
1044 M(2,2) = -2.0F / (farval-nearval);
1045 M(2,3) = -(farval+nearval) / (farval-nearval);
1046
1047 M(3,0) = 0.0F;
1048 M(3,1) = 0.0F;
1049 M(3,2) = 0.0F;
1050 M(3,3) = 1.0F;
Keith Whitwell23caf202000-11-16 21:05:34 +00001051#undef M
1052
1053 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
1054}
1055
Keith Whitwell6dc85572003-07-17 13:43:59 +00001056/**
1057 * Multiply a matrix with a general scaling matrix.
1058 *
1059 * \param mat matrix.
1060 * \param x x axis scale factor.
1061 * \param y y axis scale factor.
1062 * \param z z axis scale factor.
1063 *
1064 * Multiplies in-place the elements of \p mat by the scale factors. Checks if
1065 * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE
1066 * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and
1067 * MAT_DIRTY_INVERSE dirty flags.
1068 */
1069void
1070_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1071{
1072 GLfloat *m = mat->m;
1073 m[0] *= x; m[4] *= y; m[8] *= z;
1074 m[1] *= x; m[5] *= y; m[9] *= z;
1075 m[2] *= x; m[6] *= y; m[10] *= z;
1076 m[3] *= x; m[7] *= y; m[11] *= z;
1077
Brian Paulb3aefd12005-09-19 20:12:32 +00001078 if (FABSF(x - y) < 1e-8 && FABSF(x - z) < 1e-8)
Keith Whitwell6dc85572003-07-17 13:43:59 +00001079 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1080 else
1081 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1082
1083 mat->flags |= (MAT_DIRTY_TYPE |
1084 MAT_DIRTY_INVERSE);
1085}
1086
1087/**
1088 * Multiply a matrix with a translation matrix.
1089 *
1090 * \param mat matrix.
1091 * \param x translation vector x coordinate.
1092 * \param y translation vector y coordinate.
1093 * \param z translation vector z coordinate.
1094 *
1095 * Adds the translation coordinates to the elements of \p mat in-place. Marks
1096 * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE
1097 * dirty flags.
1098 */
1099void
1100_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1101{
1102 GLfloat *m = mat->m;
1103 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1104 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1105 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1106 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1107
1108 mat->flags |= (MAT_FLAG_TRANSLATION |
1109 MAT_DIRTY_TYPE |
1110 MAT_DIRTY_INVERSE);
1111}
1112
Brian Paul049e3202005-06-30 14:22:23 +00001113
1114/**
1115 * Set matrix to do viewport and depthrange mapping.
1116 * Transforms Normalized Device Coords to window/Z values.
1117 */
1118void
1119_math_matrix_viewport(GLmatrix *m, GLint x, GLint y, GLint width, GLint height,
1120 GLfloat zNear, GLfloat zFar, GLfloat depthMax)
1121{
1122 m->m[MAT_SX] = (GLfloat) width / 2.0F;
1123 m->m[MAT_TX] = m->m[MAT_SX] + x;
1124 m->m[MAT_SY] = (GLfloat) height / 2.0F;
1125 m->m[MAT_TY] = m->m[MAT_SY] + y;
1126 m->m[MAT_SZ] = depthMax * ((zFar - zNear) / 2.0F);
1127 m->m[MAT_TZ] = depthMax * ((zFar - zNear) / 2.0F + zNear);
1128 m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION;
1129 m->type = MATRIX_3D_NO_ROT;
1130}
1131
1132
Keith Whitwell6dc85572003-07-17 13:43:59 +00001133/**
1134 * Set a matrix to the identity matrix.
1135 *
1136 * \param mat matrix.
1137 *
1138 * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL.
1139 * Sets the matrix type to identity, and clear the dirty flags.
1140 */
1141void
1142_math_matrix_set_identity( GLmatrix *mat )
1143{
1144 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1145
1146 if (mat->inv)
1147 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1148
1149 mat->type = MATRIX_IDENTITY;
1150 mat->flags &= ~(MAT_DIRTY_FLAGS|
1151 MAT_DIRTY_TYPE|
1152 MAT_DIRTY_INVERSE);
1153}
1154
1155/*@}*/
1156
1157
1158/**********************************************************************/
1159/** \name Matrix analysis */
1160/*@{*/
Keith Whitwell23caf202000-11-16 21:05:34 +00001161
1162#define ZERO(x) (1<<x)
1163#define ONE(x) (1<<(x+16))
1164
1165#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
1166#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
1167
1168#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
1169 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
1170 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1171 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1172
1173#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
1174 ZERO(1) | ZERO(9) | \
1175 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1176 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1177
1178#define MASK_2D ( ZERO(8) | \
1179 ZERO(9) | \
1180 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1181 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1182
1183
1184#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
1185 ZERO(1) | ZERO(9) | \
1186 ZERO(2) | ZERO(6) | \
1187 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1188
1189#define MASK_3D ( \
1190 \
1191 \
1192 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1193
1194
1195#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
1196 ZERO(1) | ZERO(13) |\
1197 ZERO(2) | ZERO(6) | \
1198 ZERO(3) | ZERO(7) | ZERO(15) )
1199
1200#define SQ(x) ((x)*(x))
Gareth Hughes22144ab2001-03-12 00:48:37 +00001201
Keith Whitwell6dc85572003-07-17 13:43:59 +00001202/**
1203 * Determine type and flags from scratch.
1204 *
1205 * \param mat matrix.
1206 *
1207 * This is expensive enough to only want to do it once.
Keith Whitwell23caf202000-11-16 21:05:34 +00001208 */
Keith Whitwellad2ac212000-11-24 10:25:05 +00001209static void analyse_from_scratch( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +00001210{
1211 const GLfloat *m = mat->m;
1212 GLuint mask = 0;
1213 GLuint i;
1214
1215 for (i = 0 ; i < 16 ; i++) {
1216 if (m[i] == 0.0) mask |= (1<<i);
1217 }
Gareth Hughes22144ab2001-03-12 00:48:37 +00001218
Keith Whitwell23caf202000-11-16 21:05:34 +00001219 if (m[0] == 1.0F) mask |= (1<<16);
1220 if (m[5] == 1.0F) mask |= (1<<21);
1221 if (m[10] == 1.0F) mask |= (1<<26);
1222 if (m[15] == 1.0F) mask |= (1<<31);
1223
1224 mat->flags &= ~MAT_FLAGS_GEOMETRY;
1225
Gareth Hughes22144ab2001-03-12 00:48:37 +00001226 /* Check for translation - no-one really cares
Keith Whitwell23caf202000-11-16 21:05:34 +00001227 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001228 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
1229 mat->flags |= MAT_FLAG_TRANSLATION;
Keith Whitwell23caf202000-11-16 21:05:34 +00001230
1231 /* Do the real work
1232 */
Brian Paulb51b0a82001-03-07 05:06:11 +00001233 if (mask == (GLuint) MASK_IDENTITY) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001234 mat->type = MATRIX_IDENTITY;
1235 }
Brian Paulb51b0a82001-03-07 05:06:11 +00001236 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001237 mat->type = MATRIX_2D_NO_ROT;
Gareth Hughes22144ab2001-03-12 00:48:37 +00001238
Keith Whitwell23caf202000-11-16 21:05:34 +00001239 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
Brian Paul2dab9972004-09-09 19:58:03 +00001240 mat->flags |= MAT_FLAG_GENERAL_SCALE;
Keith Whitwell23caf202000-11-16 21:05:34 +00001241 }
Brian Paulb51b0a82001-03-07 05:06:11 +00001242 else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001243 GLfloat mm = DOT2(m, m);
1244 GLfloat m4m4 = DOT2(m+4,m+4);
1245 GLfloat mm4 = DOT2(m,m+4);
1246
1247 mat->type = MATRIX_2D;
1248
1249 /* Check for scale */
1250 if (SQ(mm-1) > SQ(1e-6) ||
Gareth Hughes22144ab2001-03-12 00:48:37 +00001251 SQ(m4m4-1) > SQ(1e-6))
Keith Whitwell23caf202000-11-16 21:05:34 +00001252 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1253
1254 /* Check for rotation */
1255 if (SQ(mm4) > SQ(1e-6))
1256 mat->flags |= MAT_FLAG_GENERAL_3D;
1257 else
1258 mat->flags |= MAT_FLAG_ROTATION;
1259
1260 }
Brian Paulb51b0a82001-03-07 05:06:11 +00001261 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001262 mat->type = MATRIX_3D_NO_ROT;
1263
1264 /* Check for scale */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001265 if (SQ(m[0]-m[5]) < SQ(1e-6) &&
Keith Whitwell23caf202000-11-16 21:05:34 +00001266 SQ(m[0]-m[10]) < SQ(1e-6)) {
1267 if (SQ(m[0]-1.0) > SQ(1e-6)) {
1268 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1269 }
1270 }
1271 else {
1272 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1273 }
1274 }
Brian Paulb51b0a82001-03-07 05:06:11 +00001275 else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001276 GLfloat c1 = DOT3(m,m);
1277 GLfloat c2 = DOT3(m+4,m+4);
1278 GLfloat c3 = DOT3(m+8,m+8);
1279 GLfloat d1 = DOT3(m, m+4);
1280 GLfloat cp[3];
1281
1282 mat->type = MATRIX_3D;
1283
1284 /* Check for scale */
1285 if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) {
1286 if (SQ(c1-1.0) > SQ(1e-6))
1287 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1288 /* else no scale at all */
1289 }
1290 else {
1291 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1292 }
1293
1294 /* Check for rotation */
1295 if (SQ(d1) < SQ(1e-6)) {
1296 CROSS3( cp, m, m+4 );
1297 SUB_3V( cp, cp, (m+8) );
Gareth Hughes22144ab2001-03-12 00:48:37 +00001298 if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
Keith Whitwell23caf202000-11-16 21:05:34 +00001299 mat->flags |= MAT_FLAG_ROTATION;
1300 else
1301 mat->flags |= MAT_FLAG_GENERAL_3D;
1302 }
1303 else {
1304 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
1305 }
1306 }
1307 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
1308 mat->type = MATRIX_PERSPECTIVE;
1309 mat->flags |= MAT_FLAG_GENERAL;
1310 }
1311 else {
1312 mat->type = MATRIX_GENERAL;
1313 mat->flags |= MAT_FLAG_GENERAL;
1314 }
1315}
1316
Keith Whitwell6dc85572003-07-17 13:43:59 +00001317/**
1318 * Analyze a matrix given that its flags are accurate.
1319 *
1320 * This is the more common operation, hopefully.
Keith Whitwell23caf202000-11-16 21:05:34 +00001321 */
Keith Whitwellad2ac212000-11-24 10:25:05 +00001322static void analyse_from_flags( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +00001323{
1324 const GLfloat *m = mat->m;
1325
1326 if (TEST_MAT_FLAGS(mat, 0)) {
1327 mat->type = MATRIX_IDENTITY;
1328 }
1329 else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
1330 MAT_FLAG_UNIFORM_SCALE |
1331 MAT_FLAG_GENERAL_SCALE))) {
1332 if ( m[10]==1.0F && m[14]==0.0F ) {
1333 mat->type = MATRIX_2D_NO_ROT;
1334 }
1335 else {
1336 mat->type = MATRIX_3D_NO_ROT;
1337 }
1338 }
1339 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
Gareth Hughes22144ab2001-03-12 00:48:37 +00001340 if ( m[ 8]==0.0F
Keith Whitwell23caf202000-11-16 21:05:34 +00001341 && m[ 9]==0.0F
1342 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
1343 mat->type = MATRIX_2D;
1344 }
1345 else {
1346 mat->type = MATRIX_3D;
1347 }
1348 }
1349 else if ( m[4]==0.0F && m[12]==0.0F
1350 && m[1]==0.0F && m[13]==0.0F
1351 && m[2]==0.0F && m[6]==0.0F
1352 && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
1353 mat->type = MATRIX_PERSPECTIVE;
1354 }
1355 else {
1356 mat->type = MATRIX_GENERAL;
1357 }
1358}
1359
Keith Whitwell6dc85572003-07-17 13:43:59 +00001360/**
1361 * Analyze and update a matrix.
1362 *
1363 * \param mat matrix.
1364 *
1365 * If the matrix type is dirty then calls either analyse_from_scratch() or
1366 * analyse_from_flags() to determine its type, according to whether the flags
1367 * are dirty or not, respectively. If the matrix has an inverse and it's dirty
1368 * then calls matrix_invert(). Finally clears the dirty flags.
1369 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001370void
1371_math_matrix_analyse( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +00001372{
1373 if (mat->flags & MAT_DIRTY_TYPE) {
Gareth Hughes22144ab2001-03-12 00:48:37 +00001374 if (mat->flags & MAT_DIRTY_FLAGS)
Keith Whitwellad2ac212000-11-24 10:25:05 +00001375 analyse_from_scratch( mat );
Keith Whitwell23caf202000-11-16 21:05:34 +00001376 else
Keith Whitwellad2ac212000-11-24 10:25:05 +00001377 analyse_from_flags( mat );
Keith Whitwell23caf202000-11-16 21:05:34 +00001378 }
1379
1380 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
1381 matrix_invert( mat );
1382 }
1383
1384 mat->flags &= ~(MAT_DIRTY_FLAGS|
1385 MAT_DIRTY_TYPE|
1386 MAT_DIRTY_INVERSE);
1387}
1388
Keith Whitwell6dc85572003-07-17 13:43:59 +00001389/*@}*/
Keith Whitwell23caf202000-11-16 21:05:34 +00001390
Keith Whitwell6dc85572003-07-17 13:43:59 +00001391
Brian Paul049e3202005-06-30 14:22:23 +00001392/**
1393 * Test if the given matrix preserves vector lengths.
1394 */
1395GLboolean
1396_math_matrix_is_length_preserving( const GLmatrix *m )
1397{
1398 return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING);
1399}
1400
1401
1402/**
1403 * Test if the given matrix does any rotation.
1404 * (or perhaps if the upper-left 3x3 is non-identity)
1405 */
1406GLboolean
1407_math_matrix_has_rotation( const GLmatrix *m )
1408{
1409 if (m->flags & (MAT_FLAG_GENERAL |
1410 MAT_FLAG_ROTATION |
1411 MAT_FLAG_GENERAL_3D |
1412 MAT_FLAG_PERSPECTIVE))
1413 return GL_TRUE;
1414 else
1415 return GL_FALSE;
1416}
1417
1418
1419GLboolean
1420_math_matrix_is_general_scale( const GLmatrix *m )
1421{
1422 return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE;
1423}
1424
1425
1426GLboolean
1427_math_matrix_is_dirty( const GLmatrix *m )
1428{
1429 return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE;
1430}
1431
1432
Keith Whitwell6dc85572003-07-17 13:43:59 +00001433/**********************************************************************/
1434/** \name Matrix setup */
1435/*@{*/
1436
1437/**
1438 * Copy a matrix.
1439 *
1440 * \param to destination matrix.
1441 * \param from source matrix.
1442 *
1443 * Copies all fields in GLmatrix, creating an inverse array if necessary.
1444 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001445void
Keith Whitwell23caf202000-11-16 21:05:34 +00001446_math_matrix_copy( GLmatrix *to, const GLmatrix *from )
1447{
1448 MEMCPY( to->m, from->m, sizeof(Identity) );
1449 to->flags = from->flags;
1450 to->type = from->type;
1451
1452 if (to->inv != 0) {
1453 if (from->inv == 0) {
1454 matrix_invert( to );
1455 }
1456 else {
1457 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
1458 }
1459 }
1460}
1461
Keith Whitwell6dc85572003-07-17 13:43:59 +00001462/**
1463 * Loads a matrix array into GLmatrix.
1464 *
1465 * \param m matrix array.
1466 * \param mat matrix.
1467 *
1468 * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
1469 * flags.
1470 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001471void
Keith Whitwell23caf202000-11-16 21:05:34 +00001472_math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1473{
1474 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1475 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1476}
1477
Keith Whitwell6dc85572003-07-17 13:43:59 +00001478/**
1479 * Matrix constructor.
1480 *
1481 * \param m matrix.
1482 *
1483 * Initialize the GLmatrix fields.
1484 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001485void
Keith Whitwell23caf202000-11-16 21:05:34 +00001486_math_matrix_ctr( GLmatrix *m )
1487{
Brian Paul30f51ae2001-12-18 04:06:44 +00001488 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1489 if (m->m)
1490 MEMCPY( m->m, Identity, sizeof(Identity) );
1491 m->inv = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001492 m->type = MATRIX_IDENTITY;
1493 m->flags = 0;
1494}
1495
Keith Whitwell6dc85572003-07-17 13:43:59 +00001496/**
1497 * Matrix destructor.
1498 *
1499 * \param m matrix.
1500 *
1501 * Frees the data in a GLmatrix.
1502 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001503void
Keith Whitwell23caf202000-11-16 21:05:34 +00001504_math_matrix_dtr( GLmatrix *m )
1505{
Brian Paul30f51ae2001-12-18 04:06:44 +00001506 if (m->m) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001507 ALIGN_FREE( m->m );
Brian Paul30f51ae2001-12-18 04:06:44 +00001508 m->m = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001509 }
Brian Paul30f51ae2001-12-18 04:06:44 +00001510 if (m->inv) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001511 ALIGN_FREE( m->inv );
Brian Paul30f51ae2001-12-18 04:06:44 +00001512 m->inv = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001513 }
1514}
1515
Keith Whitwell6dc85572003-07-17 13:43:59 +00001516/**
1517 * Allocate a matrix inverse.
1518 *
1519 * \param m matrix.
1520 *
1521 * Allocates the matrix inverse, GLmatrix::inv, and sets it to Identity.
1522 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001523void
Keith Whitwell23caf202000-11-16 21:05:34 +00001524_math_matrix_alloc_inv( GLmatrix *m )
1525{
Brian Paul30f51ae2001-12-18 04:06:44 +00001526 if (!m->inv) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001527 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
Brian Paul30f51ae2001-12-18 04:06:44 +00001528 if (m->inv)
1529 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
Keith Whitwell23caf202000-11-16 21:05:34 +00001530 }
1531}
1532
Keith Whitwell6dc85572003-07-17 13:43:59 +00001533/*@}*/
Keith Whitwell23caf202000-11-16 21:05:34 +00001534
1535
Keith Whitwell6dc85572003-07-17 13:43:59 +00001536/**********************************************************************/
1537/** \name Matrix transpose */
1538/*@{*/
Keith Whitwell23caf202000-11-16 21:05:34 +00001539
Keith Whitwell6dc85572003-07-17 13:43:59 +00001540/**
1541 * Transpose a GLfloat matrix.
1542 *
1543 * \param to destination array.
1544 * \param from source array.
1545 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001546void
Keith Whitwell23caf202000-11-16 21:05:34 +00001547_math_transposef( GLfloat to[16], const GLfloat from[16] )
1548{
1549 to[0] = from[0];
1550 to[1] = from[4];
1551 to[2] = from[8];
1552 to[3] = from[12];
1553 to[4] = from[1];
1554 to[5] = from[5];
1555 to[6] = from[9];
1556 to[7] = from[13];
1557 to[8] = from[2];
1558 to[9] = from[6];
1559 to[10] = from[10];
1560 to[11] = from[14];
1561 to[12] = from[3];
1562 to[13] = from[7];
1563 to[14] = from[11];
1564 to[15] = from[15];
1565}
1566
Keith Whitwell6dc85572003-07-17 13:43:59 +00001567/**
1568 * Transpose a GLdouble matrix.
1569 *
1570 * \param to destination array.
1571 * \param from source array.
1572 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001573void
Keith Whitwell23caf202000-11-16 21:05:34 +00001574_math_transposed( GLdouble to[16], const GLdouble from[16] )
1575{
1576 to[0] = from[0];
1577 to[1] = from[4];
1578 to[2] = from[8];
1579 to[3] = from[12];
1580 to[4] = from[1];
1581 to[5] = from[5];
1582 to[6] = from[9];
1583 to[7] = from[13];
1584 to[8] = from[2];
1585 to[9] = from[6];
1586 to[10] = from[10];
1587 to[11] = from[14];
1588 to[12] = from[3];
1589 to[13] = from[7];
1590 to[14] = from[11];
1591 to[15] = from[15];
1592}
1593
Keith Whitwell6dc85572003-07-17 13:43:59 +00001594/**
1595 * Transpose a GLdouble matrix and convert to GLfloat.
1596 *
1597 * \param to destination array.
1598 * \param from source array.
1599 */
Gareth Hughes22144ab2001-03-12 00:48:37 +00001600void
Keith Whitwell23caf202000-11-16 21:05:34 +00001601_math_transposefd( GLfloat to[16], const GLdouble from[16] )
1602{
Karl Schultz7b9fe822001-09-18 23:06:14 +00001603 to[0] = (GLfloat) from[0];
1604 to[1] = (GLfloat) from[4];
1605 to[2] = (GLfloat) from[8];
1606 to[3] = (GLfloat) from[12];
1607 to[4] = (GLfloat) from[1];
1608 to[5] = (GLfloat) from[5];
1609 to[6] = (GLfloat) from[9];
1610 to[7] = (GLfloat) from[13];
1611 to[8] = (GLfloat) from[2];
1612 to[9] = (GLfloat) from[6];
1613 to[10] = (GLfloat) from[10];
1614 to[11] = (GLfloat) from[14];
1615 to[12] = (GLfloat) from[3];
1616 to[13] = (GLfloat) from[7];
1617 to[14] = (GLfloat) from[11];
1618 to[15] = (GLfloat) from[15];
Keith Whitwell23caf202000-11-16 21:05:34 +00001619}
Keith Whitwell6dc85572003-07-17 13:43:59 +00001620
1621/*@}*/
1622