Use invert_matrix_general() instead of invert_matrix_perspective() because
[mesa.git] / src / mesa / math / m_matrix.c
1 /* $Id: m_matrix.c,v 1.11 2002/03/29 17:18:08 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.0.2
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * Matrix operations
30 *
31 * NOTES:
32 * 1. 4x4 transformation matrices are stored in memory in column major order.
33 * 2. Points/vertices are to be thought of as column vectors.
34 * 3. Transformation of a point p by a matrix M is: p' = M * p
35 */
36
37 #include <math.h>
38
39 #include "glheader.h"
40 #include "macros.h"
41 #include "mem.h"
42 #include "mmath.h"
43
44 #include "m_matrix.h"
45
46
47 static const char *types[] = {
48 "MATRIX_GENERAL",
49 "MATRIX_IDENTITY",
50 "MATRIX_3D_NO_ROT",
51 "MATRIX_PERSPECTIVE",
52 "MATRIX_2D",
53 "MATRIX_2D_NO_ROT",
54 "MATRIX_3D"
55 };
56
57
58 static GLfloat Identity[16] = {
59 1.0, 0.0, 0.0, 0.0,
60 0.0, 1.0, 0.0, 0.0,
61 0.0, 0.0, 1.0, 0.0,
62 0.0, 0.0, 0.0, 1.0
63 };
64
65
66
67
68 /*
69 * This matmul was contributed by Thomas Malik
70 *
71 * Perform a 4x4 matrix multiplication (product = a x b).
72 * Input: a, b - matrices to multiply
73 * Output: product - product of a and b
74 * WARNING: (product != b) assumed
75 * NOTE: (product == a) allowed
76 *
77 * KW: 4*16 = 64 muls
78 */
79 #define A(row,col) a[(col<<2)+row]
80 #define B(row,col) b[(col<<2)+row]
81 #define P(row,col) product[(col<<2)+row]
82
83 static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
84 {
85 GLint i;
86 for (i = 0; i < 4; i++) {
87 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
88 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
89 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
90 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
91 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
92 }
93 }
94
95
96 /* Multiply two matrices known to occupy only the top three rows, such
97 * as typical model matrices, and ortho matrices.
98 */
99 static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
100 {
101 GLint i;
102 for (i = 0; i < 3; i++) {
103 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
104 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
105 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
106 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
107 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
108 }
109 P(3,0) = 0;
110 P(3,1) = 0;
111 P(3,2) = 0;
112 P(3,3) = 1;
113 }
114
115
116 #undef A
117 #undef B
118 #undef P
119
120
121 /*
122 * Multiply a matrix by an array of floats with known properties.
123 */
124 static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
125 {
126 mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
127
128 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
129 matmul34( mat->m, mat->m, m );
130 else
131 matmul4( mat->m, mat->m, m );
132 }
133
134
135 static void print_matrix_floats( const GLfloat m[16] )
136 {
137 int i;
138 for (i=0;i<4;i++) {
139 fprintf(stderr,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
140 }
141 }
142
143 void
144 _math_matrix_print( const GLmatrix *m )
145 {
146 fprintf(stderr, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
147 print_matrix_floats(m->m);
148 fprintf(stderr, "Inverse: \n");
149 if (m->inv) {
150 GLfloat prod[16];
151 print_matrix_floats(m->inv);
152 matmul4(prod, m->m, m->inv);
153 fprintf(stderr, "Mat * Inverse:\n");
154 print_matrix_floats(prod);
155 }
156 else {
157 fprintf(stderr, " - not available\n");
158 }
159 }
160
161
162
163
164 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
165 #define MAT(m,r,c) (m)[(c)*4+(r)]
166
167 /*
168 * Compute inverse of 4x4 transformation matrix.
169 * Code contributed by Jacques Leroy jle@star.be
170 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
171 */
172 static GLboolean invert_matrix_general( GLmatrix *mat )
173 {
174 const GLfloat *m = mat->m;
175 GLfloat *out = mat->inv;
176 GLfloat wtmp[4][8];
177 GLfloat m0, m1, m2, m3, s;
178 GLfloat *r0, *r1, *r2, *r3;
179
180 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
181
182 r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
183 r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
184 r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
185
186 r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
187 r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
188 r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
189
190 r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
191 r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
192 r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
193
194 r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
195 r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
196 r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
197
198 /* choose pivot - or die */
199 if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
200 if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
201 if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
202 if (0.0 == r0[0]) return GL_FALSE;
203
204 /* eliminate first variable */
205 m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
206 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
207 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
208 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
209 s = r0[4];
210 if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
211 s = r0[5];
212 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
213 s = r0[6];
214 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
215 s = r0[7];
216 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
217
218 /* choose pivot - or die */
219 if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
220 if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
221 if (0.0 == r1[1]) return GL_FALSE;
222
223 /* eliminate second variable */
224 m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
225 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
226 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
227 s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
228 s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
229 s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
230 s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
231
232 /* choose pivot - or die */
233 if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
234 if (0.0 == r2[2]) return GL_FALSE;
235
236 /* eliminate third variable */
237 m3 = r3[2]/r2[2];
238 r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
239 r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
240 r3[7] -= m3 * r2[7];
241
242 /* last check */
243 if (0.0 == r3[3]) return GL_FALSE;
244
245 s = 1.0F/r3[3]; /* now back substitute row 3 */
246 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
247
248 m2 = r2[3]; /* now back substitute row 2 */
249 s = 1.0F/r2[2];
250 r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
251 r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
252 m1 = r1[3];
253 r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
254 r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
255 m0 = r0[3];
256 r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
257 r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
258
259 m1 = r1[2]; /* now back substitute row 1 */
260 s = 1.0F/r1[1];
261 r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
262 r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
263 m0 = r0[2];
264 r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
265 r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
266
267 m0 = r0[1]; /* now back substitute row 0 */
268 s = 1.0F/r0[0];
269 r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
270 r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
271
272 MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
273 MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
274 MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
275 MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
276 MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
277 MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
278 MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
279 MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
280
281 return GL_TRUE;
282 }
283 #undef SWAP_ROWS
284
285
286 /* Adapted from graphics gems II.
287 */
288 static GLboolean invert_matrix_3d_general( GLmatrix *mat )
289 {
290 const GLfloat *in = mat->m;
291 GLfloat *out = mat->inv;
292 GLfloat pos, neg, t;
293 GLfloat det;
294
295 /* Calculate the determinant of upper left 3x3 submatrix and
296 * determine if the matrix is singular.
297 */
298 pos = neg = 0.0;
299 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
300 if (t >= 0.0) pos += t; else neg += t;
301
302 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
303 if (t >= 0.0) pos += t; else neg += t;
304
305 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
306 if (t >= 0.0) pos += t; else neg += t;
307
308 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
309 if (t >= 0.0) pos += t; else neg += t;
310
311 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
312 if (t >= 0.0) pos += t; else neg += t;
313
314 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
315 if (t >= 0.0) pos += t; else neg += t;
316
317 det = pos + neg;
318
319 if (det*det < 1e-25)
320 return GL_FALSE;
321
322 det = 1.0F / det;
323 MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
324 MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
325 MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
326 MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
327 MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
328 MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
329 MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
330 MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
331 MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
332
333 /* Do the translation part */
334 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
335 MAT(in,1,3) * MAT(out,0,1) +
336 MAT(in,2,3) * MAT(out,0,2) );
337 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
338 MAT(in,1,3) * MAT(out,1,1) +
339 MAT(in,2,3) * MAT(out,1,2) );
340 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
341 MAT(in,1,3) * MAT(out,2,1) +
342 MAT(in,2,3) * MAT(out,2,2) );
343
344 return GL_TRUE;
345 }
346
347
348 static GLboolean invert_matrix_3d( GLmatrix *mat )
349 {
350 const GLfloat *in = mat->m;
351 GLfloat *out = mat->inv;
352
353 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
354 return invert_matrix_3d_general( mat );
355 }
356
357 if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
358 GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
359 MAT(in,0,1) * MAT(in,0,1) +
360 MAT(in,0,2) * MAT(in,0,2));
361
362 if (scale == 0.0)
363 return GL_FALSE;
364
365 scale = 1.0F / scale;
366
367 /* Transpose and scale the 3 by 3 upper-left submatrix. */
368 MAT(out,0,0) = scale * MAT(in,0,0);
369 MAT(out,1,0) = scale * MAT(in,0,1);
370 MAT(out,2,0) = scale * MAT(in,0,2);
371 MAT(out,0,1) = scale * MAT(in,1,0);
372 MAT(out,1,1) = scale * MAT(in,1,1);
373 MAT(out,2,1) = scale * MAT(in,1,2);
374 MAT(out,0,2) = scale * MAT(in,2,0);
375 MAT(out,1,2) = scale * MAT(in,2,1);
376 MAT(out,2,2) = scale * MAT(in,2,2);
377 }
378 else if (mat->flags & MAT_FLAG_ROTATION) {
379 /* Transpose the 3 by 3 upper-left submatrix. */
380 MAT(out,0,0) = MAT(in,0,0);
381 MAT(out,1,0) = MAT(in,0,1);
382 MAT(out,2,0) = MAT(in,0,2);
383 MAT(out,0,1) = MAT(in,1,0);
384 MAT(out,1,1) = MAT(in,1,1);
385 MAT(out,2,1) = MAT(in,1,2);
386 MAT(out,0,2) = MAT(in,2,0);
387 MAT(out,1,2) = MAT(in,2,1);
388 MAT(out,2,2) = MAT(in,2,2);
389 }
390 else {
391 /* pure translation */
392 MEMCPY( out, Identity, sizeof(Identity) );
393 MAT(out,0,3) = - MAT(in,0,3);
394 MAT(out,1,3) = - MAT(in,1,3);
395 MAT(out,2,3) = - MAT(in,2,3);
396 return GL_TRUE;
397 }
398
399 if (mat->flags & MAT_FLAG_TRANSLATION) {
400 /* Do the translation part */
401 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
402 MAT(in,1,3) * MAT(out,0,1) +
403 MAT(in,2,3) * MAT(out,0,2) );
404 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
405 MAT(in,1,3) * MAT(out,1,1) +
406 MAT(in,2,3) * MAT(out,1,2) );
407 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
408 MAT(in,1,3) * MAT(out,2,1) +
409 MAT(in,2,3) * MAT(out,2,2) );
410 }
411 else {
412 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
413 }
414
415 return GL_TRUE;
416 }
417
418
419
420 static GLboolean invert_matrix_identity( GLmatrix *mat )
421 {
422 MEMCPY( mat->inv, Identity, sizeof(Identity) );
423 return GL_TRUE;
424 }
425
426
427 static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
428 {
429 const GLfloat *in = mat->m;
430 GLfloat *out = mat->inv;
431
432 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
433 return GL_FALSE;
434
435 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
436 MAT(out,0,0) = 1.0F / MAT(in,0,0);
437 MAT(out,1,1) = 1.0F / MAT(in,1,1);
438 MAT(out,2,2) = 1.0F / MAT(in,2,2);
439
440 if (mat->flags & MAT_FLAG_TRANSLATION) {
441 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
442 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
443 MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
444 }
445
446 return GL_TRUE;
447 }
448
449
450 static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
451 {
452 const GLfloat *in = mat->m;
453 GLfloat *out = mat->inv;
454
455 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
456 return GL_FALSE;
457
458 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
459 MAT(out,0,0) = 1.0F / MAT(in,0,0);
460 MAT(out,1,1) = 1.0F / MAT(in,1,1);
461
462 if (mat->flags & MAT_FLAG_TRANSLATION) {
463 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
464 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
465 }
466
467 return GL_TRUE;
468 }
469
470
471 static GLboolean invert_matrix_perspective( GLmatrix *mat )
472 {
473 const GLfloat *in = mat->m;
474 GLfloat *out = mat->inv;
475
476 if (MAT(in,2,3) == 0)
477 return GL_FALSE;
478
479 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
480
481 MAT(out,0,0) = 1.0F / MAT(in,0,0);
482 MAT(out,1,1) = 1.0F / MAT(in,1,1);
483
484 MAT(out,0,3) = MAT(in,0,2);
485 MAT(out,1,3) = MAT(in,1,2);
486
487 MAT(out,2,2) = 0;
488 MAT(out,2,3) = -1;
489
490 MAT(out,3,2) = 1.0F / MAT(in,2,3);
491 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
492
493 return GL_TRUE;
494 }
495
496
497 typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
498
499
500 static inv_mat_func inv_mat_tab[7] = {
501 invert_matrix_general,
502 invert_matrix_identity,
503 invert_matrix_3d_no_rot,
504 #if 0
505 /* Don't use this function for now - it fails when the projection matrix
506 * is premultiplied by a translation (ala Chromium's tilesort SPU).
507 */
508 invert_matrix_perspective,
509 #else
510 invert_matrix_general,
511 #endif
512 invert_matrix_3d, /* lazy! */
513 invert_matrix_2d_no_rot,
514 invert_matrix_3d
515 };
516
517
518 static GLboolean matrix_invert( GLmatrix *mat )
519 {
520 if (inv_mat_tab[mat->type](mat)) {
521 mat->flags &= ~MAT_FLAG_SINGULAR;
522 return GL_TRUE;
523 } else {
524 mat->flags |= MAT_FLAG_SINGULAR;
525 MEMCPY( mat->inv, Identity, sizeof(Identity) );
526 return GL_FALSE;
527 }
528 }
529
530
531
532
533
534
535 /*
536 * Generate a 4x4 transformation matrix from glRotate parameters, and
537 * postmultiply the input matrix by it.
538 */
539 void
540 _math_matrix_rotate( GLmatrix *mat,
541 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
542 {
543 /* This function contributed by Erich Boleyn (erich@uruk.org) */
544 GLfloat mag, s, c;
545 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
546 GLfloat m[16];
547
548 s = (GLfloat) sin( angle * DEG2RAD );
549 c = (GLfloat) cos( angle * DEG2RAD );
550
551 mag = (GLfloat) GL_SQRT( x*x + y*y + z*z );
552
553 if (mag <= 1.0e-4) {
554 /* generate an identity matrix and return */
555 MEMCPY(m, Identity, sizeof(GLfloat)*16);
556 return;
557 }
558
559 x /= mag;
560 y /= mag;
561 z /= mag;
562
563 #define M(row,col) m[col*4+row]
564
565 /*
566 * Arbitrary axis rotation matrix.
567 *
568 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
569 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
570 * (which is about the X-axis), and the two composite transforms
571 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
572 * from the arbitrary axis to the X-axis then back. They are
573 * all elementary rotations.
574 *
575 * Rz' is a rotation about the Z-axis, to bring the axis vector
576 * into the x-z plane. Then Ry' is applied, rotating about the
577 * Y-axis to bring the axis vector parallel with the X-axis. The
578 * rotation about the X-axis is then performed. Ry and Rz are
579 * simply the respective inverse transforms to bring the arbitrary
580 * axis back to it's original orientation. The first transforms
581 * Rz' and Ry' are considered inverses, since the data from the
582 * arbitrary axis gives you info on how to get to it, not how
583 * to get away from it, and an inverse must be applied.
584 *
585 * The basic calculation used is to recognize that the arbitrary
586 * axis vector (x, y, z), since it is of unit length, actually
587 * represents the sines and cosines of the angles to rotate the
588 * X-axis to the same orientation, with theta being the angle about
589 * Z and phi the angle about Y (in the order described above)
590 * as follows:
591 *
592 * cos ( theta ) = x / sqrt ( 1 - z^2 )
593 * sin ( theta ) = y / sqrt ( 1 - z^2 )
594 *
595 * cos ( phi ) = sqrt ( 1 - z^2 )
596 * sin ( phi ) = z
597 *
598 * Note that cos ( phi ) can further be inserted to the above
599 * formulas:
600 *
601 * cos ( theta ) = x / cos ( phi )
602 * sin ( theta ) = y / sin ( phi )
603 *
604 * ...etc. Because of those relations and the standard trigonometric
605 * relations, it is pssible to reduce the transforms down to what
606 * is used below. It may be that any primary axis chosen will give the
607 * same results (modulo a sign convention) using thie method.
608 *
609 * Particularly nice is to notice that all divisions that might
610 * have caused trouble when parallel to certain planes or
611 * axis go away with care paid to reducing the expressions.
612 * After checking, it does perform correctly under all cases, since
613 * in all the cases of division where the denominator would have
614 * been zero, the numerator would have been zero as well, giving
615 * the expected result.
616 */
617
618 xx = x * x;
619 yy = y * y;
620 zz = z * z;
621 xy = x * y;
622 yz = y * z;
623 zx = z * x;
624 xs = x * s;
625 ys = y * s;
626 zs = z * s;
627 one_c = 1.0F - c;
628
629 M(0,0) = (one_c * xx) + c;
630 M(0,1) = (one_c * xy) - zs;
631 M(0,2) = (one_c * zx) + ys;
632 M(0,3) = 0.0F;
633
634 M(1,0) = (one_c * xy) + zs;
635 M(1,1) = (one_c * yy) + c;
636 M(1,2) = (one_c * yz) - xs;
637 M(1,3) = 0.0F;
638
639 M(2,0) = (one_c * zx) - ys;
640 M(2,1) = (one_c * yz) + xs;
641 M(2,2) = (one_c * zz) + c;
642 M(2,3) = 0.0F;
643
644 M(3,0) = 0.0F;
645 M(3,1) = 0.0F;
646 M(3,2) = 0.0F;
647 M(3,3) = 1.0F;
648
649 #undef M
650
651 matrix_multf( mat, m, MAT_FLAG_ROTATION );
652 }
653
654
655 void
656 _math_matrix_frustum( GLmatrix *mat,
657 GLfloat left, GLfloat right,
658 GLfloat bottom, GLfloat top,
659 GLfloat nearval, GLfloat farval )
660 {
661 GLfloat x, y, a, b, c, d;
662 GLfloat m[16];
663
664 x = (2.0F*nearval) / (right-left);
665 y = (2.0F*nearval) / (top-bottom);
666 a = (right+left) / (right-left);
667 b = (top+bottom) / (top-bottom);
668 c = -(farval+nearval) / ( farval-nearval);
669 d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
670
671 #define M(row,col) m[col*4+row]
672 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
673 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
674 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
675 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
676 #undef M
677
678 matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
679 }
680
681 void
682 _math_matrix_ortho( GLmatrix *mat,
683 GLfloat left, GLfloat right,
684 GLfloat bottom, GLfloat top,
685 GLfloat nearval, GLfloat farval )
686 {
687 GLfloat x, y, z;
688 GLfloat tx, ty, tz;
689 GLfloat m[16];
690
691 x = 2.0F / (right-left);
692 y = 2.0F / (top-bottom);
693 z = -2.0F / (farval-nearval);
694 tx = -(right+left) / (right-left);
695 ty = -(top+bottom) / (top-bottom);
696 tz = -(farval+nearval) / (farval-nearval);
697
698 #define M(row,col) m[col*4+row]
699 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = 0.0F; M(0,3) = tx;
700 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = 0.0F; M(1,3) = ty;
701 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = z; M(2,3) = tz;
702 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = 0.0F; M(3,3) = 1.0F;
703 #undef M
704
705 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
706 }
707
708
709 #define ZERO(x) (1<<x)
710 #define ONE(x) (1<<(x+16))
711
712 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
713 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
714
715 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
716 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
717 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
718 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
719
720 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
721 ZERO(1) | ZERO(9) | \
722 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
723 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
724
725 #define MASK_2D ( ZERO(8) | \
726 ZERO(9) | \
727 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
728 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
729
730
731 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
732 ZERO(1) | ZERO(9) | \
733 ZERO(2) | ZERO(6) | \
734 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
735
736 #define MASK_3D ( \
737 \
738 \
739 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
740
741
742 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
743 ZERO(1) | ZERO(13) |\
744 ZERO(2) | ZERO(6) | \
745 ZERO(3) | ZERO(7) | ZERO(15) )
746
747 #define SQ(x) ((x)*(x))
748
749 /* Determine type and flags from scratch. This is expensive enough to
750 * only want to do it once.
751 */
752 static void analyse_from_scratch( GLmatrix *mat )
753 {
754 const GLfloat *m = mat->m;
755 GLuint mask = 0;
756 GLuint i;
757
758 for (i = 0 ; i < 16 ; i++) {
759 if (m[i] == 0.0) mask |= (1<<i);
760 }
761
762 if (m[0] == 1.0F) mask |= (1<<16);
763 if (m[5] == 1.0F) mask |= (1<<21);
764 if (m[10] == 1.0F) mask |= (1<<26);
765 if (m[15] == 1.0F) mask |= (1<<31);
766
767 mat->flags &= ~MAT_FLAGS_GEOMETRY;
768
769 /* Check for translation - no-one really cares
770 */
771 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
772 mat->flags |= MAT_FLAG_TRANSLATION;
773
774 /* Do the real work
775 */
776 if (mask == (GLuint) MASK_IDENTITY) {
777 mat->type = MATRIX_IDENTITY;
778 }
779 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
780 mat->type = MATRIX_2D_NO_ROT;
781
782 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
783 mat->flags = MAT_FLAG_GENERAL_SCALE;
784 }
785 else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
786 GLfloat mm = DOT2(m, m);
787 GLfloat m4m4 = DOT2(m+4,m+4);
788 GLfloat mm4 = DOT2(m,m+4);
789
790 mat->type = MATRIX_2D;
791
792 /* Check for scale */
793 if (SQ(mm-1) > SQ(1e-6) ||
794 SQ(m4m4-1) > SQ(1e-6))
795 mat->flags |= MAT_FLAG_GENERAL_SCALE;
796
797 /* Check for rotation */
798 if (SQ(mm4) > SQ(1e-6))
799 mat->flags |= MAT_FLAG_GENERAL_3D;
800 else
801 mat->flags |= MAT_FLAG_ROTATION;
802
803 }
804 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
805 mat->type = MATRIX_3D_NO_ROT;
806
807 /* Check for scale */
808 if (SQ(m[0]-m[5]) < SQ(1e-6) &&
809 SQ(m[0]-m[10]) < SQ(1e-6)) {
810 if (SQ(m[0]-1.0) > SQ(1e-6)) {
811 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
812 }
813 }
814 else {
815 mat->flags |= MAT_FLAG_GENERAL_SCALE;
816 }
817 }
818 else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
819 GLfloat c1 = DOT3(m,m);
820 GLfloat c2 = DOT3(m+4,m+4);
821 GLfloat c3 = DOT3(m+8,m+8);
822 GLfloat d1 = DOT3(m, m+4);
823 GLfloat cp[3];
824
825 mat->type = MATRIX_3D;
826
827 /* Check for scale */
828 if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) {
829 if (SQ(c1-1.0) > SQ(1e-6))
830 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
831 /* else no scale at all */
832 }
833 else {
834 mat->flags |= MAT_FLAG_GENERAL_SCALE;
835 }
836
837 /* Check for rotation */
838 if (SQ(d1) < SQ(1e-6)) {
839 CROSS3( cp, m, m+4 );
840 SUB_3V( cp, cp, (m+8) );
841 if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
842 mat->flags |= MAT_FLAG_ROTATION;
843 else
844 mat->flags |= MAT_FLAG_GENERAL_3D;
845 }
846 else {
847 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
848 }
849 }
850 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
851 mat->type = MATRIX_PERSPECTIVE;
852 mat->flags |= MAT_FLAG_GENERAL;
853 }
854 else {
855 mat->type = MATRIX_GENERAL;
856 mat->flags |= MAT_FLAG_GENERAL;
857 }
858 }
859
860
861 /* Analyse a matrix given that its flags are accurate - this is the
862 * more common operation, hopefully.
863 */
864 static void analyse_from_flags( GLmatrix *mat )
865 {
866 const GLfloat *m = mat->m;
867
868 if (TEST_MAT_FLAGS(mat, 0)) {
869 mat->type = MATRIX_IDENTITY;
870 }
871 else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
872 MAT_FLAG_UNIFORM_SCALE |
873 MAT_FLAG_GENERAL_SCALE))) {
874 if ( m[10]==1.0F && m[14]==0.0F ) {
875 mat->type = MATRIX_2D_NO_ROT;
876 }
877 else {
878 mat->type = MATRIX_3D_NO_ROT;
879 }
880 }
881 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
882 if ( m[ 8]==0.0F
883 && m[ 9]==0.0F
884 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
885 mat->type = MATRIX_2D;
886 }
887 else {
888 mat->type = MATRIX_3D;
889 }
890 }
891 else if ( m[4]==0.0F && m[12]==0.0F
892 && m[1]==0.0F && m[13]==0.0F
893 && m[2]==0.0F && m[6]==0.0F
894 && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
895 mat->type = MATRIX_PERSPECTIVE;
896 }
897 else {
898 mat->type = MATRIX_GENERAL;
899 }
900 }
901
902
903 void
904 _math_matrix_analyse( GLmatrix *mat )
905 {
906 if (mat->flags & MAT_DIRTY_TYPE) {
907 if (mat->flags & MAT_DIRTY_FLAGS)
908 analyse_from_scratch( mat );
909 else
910 analyse_from_flags( mat );
911 }
912
913 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
914 matrix_invert( mat );
915 }
916
917 mat->flags &= ~(MAT_DIRTY_FLAGS|
918 MAT_DIRTY_TYPE|
919 MAT_DIRTY_INVERSE);
920 }
921
922
923 void
924 _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
925 {
926 MEMCPY( to->m, from->m, sizeof(Identity) );
927 to->flags = from->flags;
928 to->type = from->type;
929
930 if (to->inv != 0) {
931 if (from->inv == 0) {
932 matrix_invert( to );
933 }
934 else {
935 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
936 }
937 }
938 }
939
940
941 void
942 _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
943 {
944 GLfloat *m = mat->m;
945 m[0] *= x; m[4] *= y; m[8] *= z;
946 m[1] *= x; m[5] *= y; m[9] *= z;
947 m[2] *= x; m[6] *= y; m[10] *= z;
948 m[3] *= x; m[7] *= y; m[11] *= z;
949
950 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
951 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
952 else
953 mat->flags |= MAT_FLAG_GENERAL_SCALE;
954
955 mat->flags |= (MAT_DIRTY_TYPE |
956 MAT_DIRTY_INVERSE);
957 }
958
959
960 void
961 _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
962 {
963 GLfloat *m = mat->m;
964 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
965 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
966 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
967 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
968
969 mat->flags |= (MAT_FLAG_TRANSLATION |
970 MAT_DIRTY_TYPE |
971 MAT_DIRTY_INVERSE);
972 }
973
974
975 void
976 _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
977 {
978 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
979 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
980 }
981
982 void
983 _math_matrix_ctr( GLmatrix *m )
984 {
985 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
986 if (m->m)
987 MEMCPY( m->m, Identity, sizeof(Identity) );
988 m->inv = NULL;
989 m->type = MATRIX_IDENTITY;
990 m->flags = 0;
991 }
992
993 void
994 _math_matrix_dtr( GLmatrix *m )
995 {
996 if (m->m) {
997 ALIGN_FREE( m->m );
998 m->m = NULL;
999 }
1000 if (m->inv) {
1001 ALIGN_FREE( m->inv );
1002 m->inv = NULL;
1003 }
1004 }
1005
1006
1007 void
1008 _math_matrix_alloc_inv( GLmatrix *m )
1009 {
1010 if (!m->inv) {
1011 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1012 if (m->inv)
1013 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
1014 }
1015 }
1016
1017
1018 void
1019 _math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
1020 {
1021 dest->flags = (a->flags |
1022 b->flags |
1023 MAT_DIRTY_TYPE |
1024 MAT_DIRTY_INVERSE);
1025
1026 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
1027 matmul34( dest->m, a->m, b->m );
1028 else
1029 matmul4( dest->m, a->m, b->m );
1030 }
1031
1032
1033 void
1034 _math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
1035 {
1036 dest->flags |= (MAT_FLAG_GENERAL |
1037 MAT_DIRTY_TYPE |
1038 MAT_DIRTY_INVERSE);
1039
1040 matmul4( dest->m, dest->m, m );
1041 }
1042
1043 void
1044 _math_matrix_set_identity( GLmatrix *mat )
1045 {
1046 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1047
1048 if (mat->inv)
1049 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1050
1051 mat->type = MATRIX_IDENTITY;
1052 mat->flags &= ~(MAT_DIRTY_FLAGS|
1053 MAT_DIRTY_TYPE|
1054 MAT_DIRTY_INVERSE);
1055 }
1056
1057
1058
1059 void
1060 _math_transposef( GLfloat to[16], const GLfloat from[16] )
1061 {
1062 to[0] = from[0];
1063 to[1] = from[4];
1064 to[2] = from[8];
1065 to[3] = from[12];
1066 to[4] = from[1];
1067 to[5] = from[5];
1068 to[6] = from[9];
1069 to[7] = from[13];
1070 to[8] = from[2];
1071 to[9] = from[6];
1072 to[10] = from[10];
1073 to[11] = from[14];
1074 to[12] = from[3];
1075 to[13] = from[7];
1076 to[14] = from[11];
1077 to[15] = from[15];
1078 }
1079
1080
1081 void
1082 _math_transposed( GLdouble to[16], const GLdouble from[16] )
1083 {
1084 to[0] = from[0];
1085 to[1] = from[4];
1086 to[2] = from[8];
1087 to[3] = from[12];
1088 to[4] = from[1];
1089 to[5] = from[5];
1090 to[6] = from[9];
1091 to[7] = from[13];
1092 to[8] = from[2];
1093 to[9] = from[6];
1094 to[10] = from[10];
1095 to[11] = from[14];
1096 to[12] = from[3];
1097 to[13] = from[7];
1098 to[14] = from[11];
1099 to[15] = from[15];
1100 }
1101
1102 void
1103 _math_transposefd( GLfloat to[16], const GLdouble from[16] )
1104 {
1105 to[0] = (GLfloat) from[0];
1106 to[1] = (GLfloat) from[4];
1107 to[2] = (GLfloat) from[8];
1108 to[3] = (GLfloat) from[12];
1109 to[4] = (GLfloat) from[1];
1110 to[5] = (GLfloat) from[5];
1111 to[6] = (GLfloat) from[9];
1112 to[7] = (GLfloat) from[13];
1113 to[8] = (GLfloat) from[2];
1114 to[9] = (GLfloat) from[6];
1115 to[10] = (GLfloat) from[10];
1116 to[11] = (GLfloat) from[14];
1117 to[12] = (GLfloat) from[3];
1118 to[13] = (GLfloat) from[7];
1119 to[14] = (GLfloat) from[11];
1120 to[15] = (GLfloat) from[15];
1121 }