blob: 95a77e6a0842f37354dba43a44ffc5967a1ae832 [file] [log] [blame]
Brian Paul27558a12003-03-01 01:50:20 +00001/* $Id: m_matrix.c,v 1.16 2003/03/01 01:50:24 brianp Exp $ */
Keith Whitwell23caf202000-11-16 21:05:34 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paul1e091f42003-01-08 16:42:47 +00005 * Version: 5.1
Gareth Hughes22144ab2001-03-12 00:48:37 +00006 *
Brian Paul1e091f42003-01-08 16:42:47 +00007 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
Gareth Hughes22144ab2001-03-12 00:48:37 +00008 *
Keith Whitwell23caf202000-11-16 21:05:34 +00009 * 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:
Gareth Hughes22144ab2001-03-12 00:48:37 +000015 *
Keith Whitwell23caf202000-11-16 21:05:34 +000016 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
Gareth Hughes22144ab2001-03-12 00:48:37 +000018 *
Keith Whitwell23caf202000-11-16 21:05:34 +000019 * 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
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 Whitwell23caf202000-11-16 21:05:34 +000045static const char *types[] = {
46 "MATRIX_GENERAL",
47 "MATRIX_IDENTITY",
48 "MATRIX_3D_NO_ROT",
49 "MATRIX_PERSPECTIVE",
50 "MATRIX_2D",
51 "MATRIX_2D_NO_ROT",
52 "MATRIX_3D"
53};
54
55
56static GLfloat Identity[16] = {
57 1.0, 0.0, 0.0, 0.0,
58 0.0, 1.0, 0.0, 0.0,
59 0.0, 0.0, 1.0, 0.0,
60 0.0, 0.0, 0.0, 1.0
61};
62
63
64
65
66/*
Gareth Hughes22144ab2001-03-12 00:48:37 +000067 * This matmul was contributed by Thomas Malik
Keith Whitwell23caf202000-11-16 21:05:34 +000068 *
69 * Perform a 4x4 matrix multiplication (product = a x b).
70 * Input: a, b - matrices to multiply
71 * Output: product - product of a and b
72 * WARNING: (product != b) assumed
Gareth Hughes22144ab2001-03-12 00:48:37 +000073 * NOTE: (product == a) allowed
Keith Whitwell23caf202000-11-16 21:05:34 +000074 *
75 * KW: 4*16 = 64 muls
76 */
77#define A(row,col) a[(col<<2)+row]
78#define B(row,col) b[(col<<2)+row]
79#define P(row,col) product[(col<<2)+row]
80
81static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
82{
83 GLint i;
84 for (i = 0; i < 4; i++) {
85 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
86 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
87 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
88 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
89 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
90 }
91}
92
93
94/* Multiply two matrices known to occupy only the top three rows, such
Gareth Hughes22144ab2001-03-12 00:48:37 +000095 * as typical model matrices, and ortho matrices.
Keith Whitwell23caf202000-11-16 21:05:34 +000096 */
97static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
98{
99 GLint i;
100 for (i = 0; i < 3; i++) {
101 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
102 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
103 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
104 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
105 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
106 }
107 P(3,0) = 0;
108 P(3,1) = 0;
109 P(3,2) = 0;
110 P(3,3) = 1;
111}
112
113
114#undef A
115#undef B
116#undef P
117
118
119/*
120 * Multiply a matrix by an array of floats with known properties.
121 */
122static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
123{
124 mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
125
126 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
127 matmul34( mat->m, mat->m, m );
Gareth Hughes22144ab2001-03-12 00:48:37 +0000128 else
129 matmul4( mat->m, mat->m, m );
Keith Whitwell23caf202000-11-16 21:05:34 +0000130}
131
132
133static void print_matrix_floats( const GLfloat m[16] )
134{
135 int i;
136 for (i=0;i<4;i++) {
Brian Paul4e9676f2002-06-29 19:48:15 +0000137 _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 +0000138 }
139}
140
Gareth Hughes22144ab2001-03-12 00:48:37 +0000141void
Keith Whitwell23caf202000-11-16 21:05:34 +0000142_math_matrix_print( const GLmatrix *m )
143{
Brian Paul4e9676f2002-06-29 19:48:15 +0000144 _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
Keith Whitwell23caf202000-11-16 21:05:34 +0000145 print_matrix_floats(m->m);
Brian Paul4e9676f2002-06-29 19:48:15 +0000146 _mesa_debug(NULL, "Inverse: \n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000147 if (m->inv) {
148 GLfloat prod[16];
149 print_matrix_floats(m->inv);
150 matmul4(prod, m->m, m->inv);
Brian Paul4e9676f2002-06-29 19:48:15 +0000151 _mesa_debug(NULL, "Mat * Inverse:\n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000152 print_matrix_floats(prod);
153 }
154 else {
Brian Paul4e9676f2002-06-29 19:48:15 +0000155 _mesa_debug(NULL, " - not available\n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000156 }
157}
158
159
160
161
162#define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
163#define MAT(m,r,c) (m)[(c)*4+(r)]
164
165/*
166 * Compute inverse of 4x4 transformation matrix.
167 * Code contributed by Jacques Leroy jle@star.be
168 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
169 */
170static GLboolean invert_matrix_general( GLmatrix *mat )
171{
172 const GLfloat *m = mat->m;
173 GLfloat *out = mat->inv;
174 GLfloat wtmp[4][8];
175 GLfloat m0, m1, m2, m3, s;
176 GLfloat *r0, *r1, *r2, *r3;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000177
Keith Whitwell23caf202000-11-16 21:05:34 +0000178 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
Gareth Hughes22144ab2001-03-12 00:48:37 +0000179
Keith Whitwell23caf202000-11-16 21:05:34 +0000180 r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
181 r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
182 r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000183
Keith Whitwell23caf202000-11-16 21:05:34 +0000184 r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
185 r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
186 r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000187
Keith Whitwell23caf202000-11-16 21:05:34 +0000188 r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
189 r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
190 r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000191
Keith Whitwell23caf202000-11-16 21:05:34 +0000192 r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
193 r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
194 r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000195
Keith Whitwell23caf202000-11-16 21:05:34 +0000196 /* choose pivot - or die */
197 if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
198 if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
199 if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
200 if (0.0 == r0[0]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000201
Keith Whitwell23caf202000-11-16 21:05:34 +0000202 /* eliminate first variable */
203 m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
204 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
205 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
206 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
207 s = r0[4];
208 if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
209 s = r0[5];
210 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
211 s = r0[6];
212 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
213 s = r0[7];
214 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000215
Keith Whitwell23caf202000-11-16 21:05:34 +0000216 /* choose pivot - or die */
217 if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
218 if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
219 if (0.0 == r1[1]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000220
Keith Whitwell23caf202000-11-16 21:05:34 +0000221 /* eliminate second variable */
222 m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
223 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
224 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
225 s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
226 s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
227 s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
228 s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000229
Keith Whitwell23caf202000-11-16 21:05:34 +0000230 /* choose pivot - or die */
231 if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
232 if (0.0 == r2[2]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000233
Keith Whitwell23caf202000-11-16 21:05:34 +0000234 /* eliminate third variable */
235 m3 = r3[2]/r2[2];
236 r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
237 r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
238 r3[7] -= m3 * r2[7];
Gareth Hughes22144ab2001-03-12 00:48:37 +0000239
Keith Whitwell23caf202000-11-16 21:05:34 +0000240 /* last check */
241 if (0.0 == r3[3]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000242
Karl Schultz7b9fe822001-09-18 23:06:14 +0000243 s = 1.0F/r3[3]; /* now back substitute row 3 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000244 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000245
Keith Whitwell23caf202000-11-16 21:05:34 +0000246 m2 = r2[3]; /* now back substitute row 2 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000247 s = 1.0F/r2[2];
Keith Whitwell23caf202000-11-16 21:05:34 +0000248 r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
249 r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
250 m1 = r1[3];
251 r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
252 r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
253 m0 = r0[3];
254 r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
255 r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000256
Keith Whitwell23caf202000-11-16 21:05:34 +0000257 m1 = r1[2]; /* now back substitute row 1 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000258 s = 1.0F/r1[1];
Keith Whitwell23caf202000-11-16 21:05:34 +0000259 r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
260 r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
261 m0 = r0[2];
262 r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
263 r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000264
Keith Whitwell23caf202000-11-16 21:05:34 +0000265 m0 = r0[1]; /* now back substitute row 0 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000266 s = 1.0F/r0[0];
Keith Whitwell23caf202000-11-16 21:05:34 +0000267 r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
268 r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
Gareth Hughes22144ab2001-03-12 00:48:37 +0000269
Keith Whitwell23caf202000-11-16 21:05:34 +0000270 MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
271 MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
272 MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
273 MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
274 MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
275 MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
276 MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
Gareth Hughes22144ab2001-03-12 00:48:37 +0000277 MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
278
Keith Whitwell23caf202000-11-16 21:05:34 +0000279 return GL_TRUE;
280}
281#undef SWAP_ROWS
282
283
284/* Adapted from graphics gems II.
Gareth Hughes22144ab2001-03-12 00:48:37 +0000285 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000286static GLboolean invert_matrix_3d_general( GLmatrix *mat )
287{
288 const GLfloat *in = mat->m;
289 GLfloat *out = mat->inv;
290 GLfloat pos, neg, t;
291 GLfloat det;
292
293 /* Calculate the determinant of upper left 3x3 submatrix and
Gareth Hughes22144ab2001-03-12 00:48:37 +0000294 * determine if the matrix is singular.
Keith Whitwell23caf202000-11-16 21:05:34 +0000295 */
296 pos = neg = 0.0;
297 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
298 if (t >= 0.0) pos += t; else neg += t;
299
300 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
301 if (t >= 0.0) pos += t; else neg += t;
302
303 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
304 if (t >= 0.0) pos += t; else neg += t;
305
306 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
307 if (t >= 0.0) pos += t; else neg += t;
308
309 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
310 if (t >= 0.0) pos += t; else neg += t;
311
312 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
313 if (t >= 0.0) pos += t; else neg += t;
314
315 det = pos + neg;
316
Gareth Hughes22144ab2001-03-12 00:48:37 +0000317 if (det*det < 1e-25)
Keith Whitwell23caf202000-11-16 21:05:34 +0000318 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000319
Karl Schultz7b9fe822001-09-18 23:06:14 +0000320 det = 1.0F / det;
Keith Whitwell23caf202000-11-16 21:05:34 +0000321 MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
322 MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
323 MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
324 MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
325 MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
326 MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
327 MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
328 MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
329 MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
330
331 /* Do the translation part */
332 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
333 MAT(in,1,3) * MAT(out,0,1) +
334 MAT(in,2,3) * MAT(out,0,2) );
335 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
336 MAT(in,1,3) * MAT(out,1,1) +
337 MAT(in,2,3) * MAT(out,1,2) );
338 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
339 MAT(in,1,3) * MAT(out,2,1) +
340 MAT(in,2,3) * MAT(out,2,2) );
Gareth Hughes22144ab2001-03-12 00:48:37 +0000341
Keith Whitwell23caf202000-11-16 21:05:34 +0000342 return GL_TRUE;
343}
344
345
346static GLboolean invert_matrix_3d( GLmatrix *mat )
347{
348 const GLfloat *in = mat->m;
349 GLfloat *out = mat->inv;
350
351 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
352 return invert_matrix_3d_general( mat );
353 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000354
Keith Whitwell23caf202000-11-16 21:05:34 +0000355 if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
356 GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
357 MAT(in,0,1) * MAT(in,0,1) +
358 MAT(in,0,2) * MAT(in,0,2));
359
Gareth Hughes22144ab2001-03-12 00:48:37 +0000360 if (scale == 0.0)
Keith Whitwell23caf202000-11-16 21:05:34 +0000361 return GL_FALSE;
362
Karl Schultz7b9fe822001-09-18 23:06:14 +0000363 scale = 1.0F / scale;
Keith Whitwell23caf202000-11-16 21:05:34 +0000364
365 /* Transpose and scale the 3 by 3 upper-left submatrix. */
366 MAT(out,0,0) = scale * MAT(in,0,0);
367 MAT(out,1,0) = scale * MAT(in,0,1);
368 MAT(out,2,0) = scale * MAT(in,0,2);
369 MAT(out,0,1) = scale * MAT(in,1,0);
370 MAT(out,1,1) = scale * MAT(in,1,1);
371 MAT(out,2,1) = scale * MAT(in,1,2);
372 MAT(out,0,2) = scale * MAT(in,2,0);
373 MAT(out,1,2) = scale * MAT(in,2,1);
374 MAT(out,2,2) = scale * MAT(in,2,2);
375 }
376 else if (mat->flags & MAT_FLAG_ROTATION) {
377 /* Transpose the 3 by 3 upper-left submatrix. */
378 MAT(out,0,0) = MAT(in,0,0);
379 MAT(out,1,0) = MAT(in,0,1);
380 MAT(out,2,0) = MAT(in,0,2);
381 MAT(out,0,1) = MAT(in,1,0);
382 MAT(out,1,1) = MAT(in,1,1);
383 MAT(out,2,1) = MAT(in,1,2);
384 MAT(out,0,2) = MAT(in,2,0);
385 MAT(out,1,2) = MAT(in,2,1);
386 MAT(out,2,2) = MAT(in,2,2);
387 }
388 else {
389 /* pure translation */
390 MEMCPY( out, Identity, sizeof(Identity) );
391 MAT(out,0,3) = - MAT(in,0,3);
392 MAT(out,1,3) = - MAT(in,1,3);
393 MAT(out,2,3) = - MAT(in,2,3);
394 return GL_TRUE;
395 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000396
Keith Whitwell23caf202000-11-16 21:05:34 +0000397 if (mat->flags & MAT_FLAG_TRANSLATION) {
398 /* Do the translation part */
399 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
400 MAT(in,1,3) * MAT(out,0,1) +
401 MAT(in,2,3) * MAT(out,0,2) );
402 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
403 MAT(in,1,3) * MAT(out,1,1) +
404 MAT(in,2,3) * MAT(out,1,2) );
405 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
406 MAT(in,1,3) * MAT(out,2,1) +
407 MAT(in,2,3) * MAT(out,2,2) );
408 }
409 else {
410 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
411 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000412
Keith Whitwell23caf202000-11-16 21:05:34 +0000413 return GL_TRUE;
414}
415
Gareth Hughes22144ab2001-03-12 00:48:37 +0000416
Keith Whitwell23caf202000-11-16 21:05:34 +0000417
418static GLboolean invert_matrix_identity( GLmatrix *mat )
419{
420 MEMCPY( mat->inv, Identity, sizeof(Identity) );
421 return GL_TRUE;
422}
423
424
425static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
426{
427 const GLfloat *in = mat->m;
428 GLfloat *out = mat->inv;
429
Gareth Hughes22144ab2001-03-12 00:48:37 +0000430 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
Keith Whitwell23caf202000-11-16 21:05:34 +0000431 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000432
Keith Whitwell23caf202000-11-16 21:05:34 +0000433 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
Karl Schultz7b9fe822001-09-18 23:06:14 +0000434 MAT(out,0,0) = 1.0F / MAT(in,0,0);
435 MAT(out,1,1) = 1.0F / MAT(in,1,1);
436 MAT(out,2,2) = 1.0F / MAT(in,2,2);
Keith Whitwell23caf202000-11-16 21:05:34 +0000437
438 if (mat->flags & MAT_FLAG_TRANSLATION) {
439 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
440 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
441 MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
442 }
443
444 return GL_TRUE;
445}
446
447
448static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
449{
450 const GLfloat *in = mat->m;
451 GLfloat *out = mat->inv;
452
Gareth Hughes22144ab2001-03-12 00:48:37 +0000453 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
Keith Whitwell23caf202000-11-16 21:05:34 +0000454 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000455
Keith Whitwell23caf202000-11-16 21:05:34 +0000456 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
Karl Schultz7b9fe822001-09-18 23:06:14 +0000457 MAT(out,0,0) = 1.0F / MAT(in,0,0);
458 MAT(out,1,1) = 1.0F / MAT(in,1,1);
Keith Whitwell23caf202000-11-16 21:05:34 +0000459
460 if (mat->flags & MAT_FLAG_TRANSLATION) {
461 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
462 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
463 }
464
465 return GL_TRUE;
466}
467
468
Brian Paul4e9676f2002-06-29 19:48:15 +0000469#if 0
470/* broken */
Keith Whitwell23caf202000-11-16 21:05:34 +0000471static 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
Karl Schultz7b9fe822001-09-18 23:06:14 +0000481 MAT(out,0,0) = 1.0F / MAT(in,0,0);
482 MAT(out,1,1) = 1.0F / MAT(in,1,1);
Keith Whitwell23caf202000-11-16 21:05:34 +0000483
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
Karl Schultz7b9fe822001-09-18 23:06:14 +0000490 MAT(out,3,2) = 1.0F / MAT(in,2,3);
Keith Whitwell23caf202000-11-16 21:05:34 +0000491 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
492
493 return GL_TRUE;
494}
Brian Paul4e9676f2002-06-29 19:48:15 +0000495#endif
Keith Whitwell23caf202000-11-16 21:05:34 +0000496
497
498typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
499
500
501static inv_mat_func inv_mat_tab[7] = {
502 invert_matrix_general,
503 invert_matrix_identity,
504 invert_matrix_3d_no_rot,
Brian Paula68b8df2002-03-29 17:18:08 +0000505#if 0
506 /* Don't use this function for now - it fails when the projection matrix
507 * is premultiplied by a translation (ala Chromium's tilesort SPU).
508 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000509 invert_matrix_perspective,
Brian Paula68b8df2002-03-29 17:18:08 +0000510#else
511 invert_matrix_general,
512#endif
Keith Whitwell23caf202000-11-16 21:05:34 +0000513 invert_matrix_3d, /* lazy! */
514 invert_matrix_2d_no_rot,
515 invert_matrix_3d
516};
517
518
519static GLboolean matrix_invert( GLmatrix *mat )
520{
521 if (inv_mat_tab[mat->type](mat)) {
522 mat->flags &= ~MAT_FLAG_SINGULAR;
523 return GL_TRUE;
524 } else {
525 mat->flags |= MAT_FLAG_SINGULAR;
526 MEMCPY( mat->inv, Identity, sizeof(Identity) );
527 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000528 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000529}
530
531
532
533
534
535
536/*
537 * Generate a 4x4 transformation matrix from glRotate parameters, and
538 * postmultiply the input matrix by it.
Brian Paul4991d0f2002-09-12 16:26:04 +0000539 * This function contributed by Erich Boleyn (erich@uruk.org).
540 * Optimizatios contributed by Rudolf Opalla (rudi@khm.de).
Keith Whitwell23caf202000-11-16 21:05:34 +0000541 */
Gareth Hughes22144ab2001-03-12 00:48:37 +0000542void
543_math_matrix_rotate( GLmatrix *mat,
Keith Whitwell23caf202000-11-16 21:05:34 +0000544 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
545{
Brian Paul4991d0f2002-09-12 16:26:04 +0000546 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
Keith Whitwell23caf202000-11-16 21:05:34 +0000547 GLfloat m[16];
Brian Paul4991d0f2002-09-12 16:26:04 +0000548 GLboolean optimized;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000549
Karl Schultz7b9fe822001-09-18 23:06:14 +0000550 s = (GLfloat) sin( angle * DEG2RAD );
551 c = (GLfloat) cos( angle * DEG2RAD );
Keith Whitwell23caf202000-11-16 21:05:34 +0000552
Brian Paul4991d0f2002-09-12 16:26:04 +0000553 MEMCPY(m, Identity, sizeof(GLfloat)*16);
554 optimized = GL_FALSE;
Keith Whitwell23caf202000-11-16 21:05:34 +0000555
556#define M(row,col) m[col*4+row]
557
Brian Paul4991d0f2002-09-12 16:26:04 +0000558 if (x == 0.0F) {
559 if (y == 0.0F) {
560 if (z != 0.0F) {
561 optimized = GL_TRUE;
562 /* rotate only around z-axis */
563 M(0,0) = c;
564 M(1,1) = c;
565 if (z < 0.0F) {
566 M(0,1) = s;
567 M(1,0) = -s;
568 }
569 else {
570 M(0,1) = -s;
571 M(1,0) = s;
572 }
573 }
574 }
575 else if (z == 0.0F) {
576 optimized = GL_TRUE;
577 /* rotate only around y-axis */
578 M(0,0) = c;
579 M(2,2) = c;
580 if (y < 0.0F) {
581 M(0,2) = -s;
582 M(2,0) = s;
583 }
584 else {
585 M(0,2) = s;
586 M(2,0) = -s;
587 }
588 }
589 }
590 else if (y == 0.0F) {
591 if (z == 0.0F) {
592 optimized = GL_TRUE;
593 /* rotate only around x-axis */
594 M(1,1) = c;
595 M(2,2) = c;
Brian Paul1e091f42003-01-08 16:42:47 +0000596 if (x < 0.0F) {
Brian Paul4991d0f2002-09-12 16:26:04 +0000597 M(1,2) = s;
598 M(2,1) = -s;
599 }
600 else {
601 M(1,2) = -s;
602 M(2,1) = s;
603 }
604 }
605 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000606
Brian Paul4991d0f2002-09-12 16:26:04 +0000607 if (!optimized) {
Brian Paul27558a12003-03-01 01:50:20 +0000608 const GLfloat mag = SQRTF(x * x + y * y + z * z);
Keith Whitwell23caf202000-11-16 21:05:34 +0000609
Brian Paul4991d0f2002-09-12 16:26:04 +0000610 if (mag <= 1.0e-4) {
611 /* no rotation, leave mat as-is */
612 return;
613 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000614
Brian Paul4991d0f2002-09-12 16:26:04 +0000615 x /= mag;
616 y /= mag;
617 z /= mag;
Keith Whitwell23caf202000-11-16 21:05:34 +0000618
Keith Whitwell23caf202000-11-16 21:05:34 +0000619
Brian Paul4991d0f2002-09-12 16:26:04 +0000620 /*
621 * Arbitrary axis rotation matrix.
622 *
623 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
624 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
625 * (which is about the X-axis), and the two composite transforms
626 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
627 * from the arbitrary axis to the X-axis then back. They are
628 * all elementary rotations.
629 *
630 * Rz' is a rotation about the Z-axis, to bring the axis vector
631 * into the x-z plane. Then Ry' is applied, rotating about the
632 * Y-axis to bring the axis vector parallel with the X-axis. The
633 * rotation about the X-axis is then performed. Ry and Rz are
634 * simply the respective inverse transforms to bring the arbitrary
635 * axis back to it's original orientation. The first transforms
636 * Rz' and Ry' are considered inverses, since the data from the
637 * arbitrary axis gives you info on how to get to it, not how
638 * to get away from it, and an inverse must be applied.
639 *
640 * The basic calculation used is to recognize that the arbitrary
641 * axis vector (x, y, z), since it is of unit length, actually
642 * represents the sines and cosines of the angles to rotate the
643 * X-axis to the same orientation, with theta being the angle about
644 * Z and phi the angle about Y (in the order described above)
645 * as follows:
646 *
647 * cos ( theta ) = x / sqrt ( 1 - z^2 )
648 * sin ( theta ) = y / sqrt ( 1 - z^2 )
649 *
650 * cos ( phi ) = sqrt ( 1 - z^2 )
651 * sin ( phi ) = z
652 *
653 * Note that cos ( phi ) can further be inserted to the above
654 * formulas:
655 *
656 * cos ( theta ) = x / cos ( phi )
657 * sin ( theta ) = y / sin ( phi )
658 *
659 * ...etc. Because of those relations and the standard trigonometric
660 * relations, it is pssible to reduce the transforms down to what
661 * is used below. It may be that any primary axis chosen will give the
662 * same results (modulo a sign convention) using thie method.
663 *
664 * Particularly nice is to notice that all divisions that might
665 * have caused trouble when parallel to certain planes or
666 * axis go away with care paid to reducing the expressions.
667 * After checking, it does perform correctly under all cases, since
668 * in all the cases of division where the denominator would have
669 * been zero, the numerator would have been zero as well, giving
670 * the expected result.
671 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000672
Brian Paul4991d0f2002-09-12 16:26:04 +0000673 xx = x * x;
674 yy = y * y;
675 zz = z * z;
676 xy = x * y;
677 yz = y * z;
678 zx = z * x;
679 xs = x * s;
680 ys = y * s;
681 zs = z * s;
682 one_c = 1.0F - c;
683
684 /* We already hold the identity-matrix so we can skip some statements */
685 M(0,0) = (one_c * xx) + c;
686 M(0,1) = (one_c * xy) - zs;
687 M(0,2) = (one_c * zx) + ys;
688/* M(0,3) = 0.0F; */
689
690 M(1,0) = (one_c * xy) + zs;
691 M(1,1) = (one_c * yy) + c;
692 M(1,2) = (one_c * yz) - xs;
693/* M(1,3) = 0.0F; */
694
695 M(2,0) = (one_c * zx) - ys;
696 M(2,1) = (one_c * yz) + xs;
697 M(2,2) = (one_c * zz) + c;
698/* M(2,3) = 0.0F; */
699
700/*
701 M(3,0) = 0.0F;
702 M(3,1) = 0.0F;
703 M(3,2) = 0.0F;
704 M(3,3) = 1.0F;
705*/
706 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000707#undef M
708
709 matrix_multf( mat, m, MAT_FLAG_ROTATION );
710}
711
712
Brian Paul4991d0f2002-09-12 16:26:04 +0000713
Keith Whitwell23caf202000-11-16 21:05:34 +0000714void
Gareth Hughes22144ab2001-03-12 00:48:37 +0000715_math_matrix_frustum( GLmatrix *mat,
Brian Pauld8bc5a92001-02-05 18:48:52 +0000716 GLfloat left, GLfloat right,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000717 GLfloat bottom, GLfloat top,
Brian Pauld8bc5a92001-02-05 18:48:52 +0000718 GLfloat nearval, GLfloat farval )
Keith Whitwell23caf202000-11-16 21:05:34 +0000719{
720 GLfloat x, y, a, b, c, d;
721 GLfloat m[16];
722
Karl Schultz7b9fe822001-09-18 23:06:14 +0000723 x = (2.0F*nearval) / (right-left);
724 y = (2.0F*nearval) / (top-bottom);
Keith Whitwell23caf202000-11-16 21:05:34 +0000725 a = (right+left) / (right-left);
726 b = (top+bottom) / (top-bottom);
727 c = -(farval+nearval) / ( farval-nearval);
Karl Schultz7b9fe822001-09-18 23:06:14 +0000728 d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
Keith Whitwell23caf202000-11-16 21:05:34 +0000729
730#define M(row,col) m[col*4+row]
731 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
732 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
733 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
734 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
735#undef M
736
737 matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
738}
739
740void
Gareth Hughes22144ab2001-03-12 00:48:37 +0000741_math_matrix_ortho( GLmatrix *mat,
Keith Whitwell23caf202000-11-16 21:05:34 +0000742 GLfloat left, GLfloat right,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000743 GLfloat bottom, GLfloat top,
Keith Whitwell23caf202000-11-16 21:05:34 +0000744 GLfloat nearval, GLfloat farval )
745{
746 GLfloat x, y, z;
747 GLfloat tx, ty, tz;
748 GLfloat m[16];
749
Karl Schultz7b9fe822001-09-18 23:06:14 +0000750 x = 2.0F / (right-left);
751 y = 2.0F / (top-bottom);
752 z = -2.0F / (farval-nearval);
Keith Whitwell23caf202000-11-16 21:05:34 +0000753 tx = -(right+left) / (right-left);
754 ty = -(top+bottom) / (top-bottom);
755 tz = -(farval+nearval) / (farval-nearval);
756
757#define M(row,col) m[col*4+row]
758 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = 0.0F; M(0,3) = tx;
759 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = 0.0F; M(1,3) = ty;
760 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = z; M(2,3) = tz;
761 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = 0.0F; M(3,3) = 1.0F;
762#undef M
763
764 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
765}
766
767
768#define ZERO(x) (1<<x)
769#define ONE(x) (1<<(x+16))
770
771#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
772#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
773
774#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
775 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
776 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
777 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
778
779#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
780 ZERO(1) | ZERO(9) | \
781 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
782 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
783
784#define MASK_2D ( ZERO(8) | \
785 ZERO(9) | \
786 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
787 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
788
789
790#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
791 ZERO(1) | ZERO(9) | \
792 ZERO(2) | ZERO(6) | \
793 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
794
795#define MASK_3D ( \
796 \
797 \
798 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
799
800
801#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
802 ZERO(1) | ZERO(13) |\
803 ZERO(2) | ZERO(6) | \
804 ZERO(3) | ZERO(7) | ZERO(15) )
805
806#define SQ(x) ((x)*(x))
Gareth Hughes22144ab2001-03-12 00:48:37 +0000807
Keith Whitwell23caf202000-11-16 21:05:34 +0000808/* Determine type and flags from scratch. This is expensive enough to
809 * only want to do it once.
810 */
Keith Whitwellad2ac212000-11-24 10:25:05 +0000811static void analyse_from_scratch( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +0000812{
813 const GLfloat *m = mat->m;
814 GLuint mask = 0;
815 GLuint i;
816
817 for (i = 0 ; i < 16 ; i++) {
818 if (m[i] == 0.0) mask |= (1<<i);
819 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000820
Keith Whitwell23caf202000-11-16 21:05:34 +0000821 if (m[0] == 1.0F) mask |= (1<<16);
822 if (m[5] == 1.0F) mask |= (1<<21);
823 if (m[10] == 1.0F) mask |= (1<<26);
824 if (m[15] == 1.0F) mask |= (1<<31);
825
826 mat->flags &= ~MAT_FLAGS_GEOMETRY;
827
Gareth Hughes22144ab2001-03-12 00:48:37 +0000828 /* Check for translation - no-one really cares
Keith Whitwell23caf202000-11-16 21:05:34 +0000829 */
Gareth Hughes22144ab2001-03-12 00:48:37 +0000830 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
831 mat->flags |= MAT_FLAG_TRANSLATION;
Keith Whitwell23caf202000-11-16 21:05:34 +0000832
833 /* Do the real work
834 */
Brian Paulb51b0a82001-03-07 05:06:11 +0000835 if (mask == (GLuint) MASK_IDENTITY) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000836 mat->type = MATRIX_IDENTITY;
837 }
Brian Paulb51b0a82001-03-07 05:06:11 +0000838 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000839 mat->type = MATRIX_2D_NO_ROT;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000840
Keith Whitwell23caf202000-11-16 21:05:34 +0000841 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
842 mat->flags = MAT_FLAG_GENERAL_SCALE;
843 }
Brian Paulb51b0a82001-03-07 05:06:11 +0000844 else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000845 GLfloat mm = DOT2(m, m);
846 GLfloat m4m4 = DOT2(m+4,m+4);
847 GLfloat mm4 = DOT2(m,m+4);
848
849 mat->type = MATRIX_2D;
850
851 /* Check for scale */
852 if (SQ(mm-1) > SQ(1e-6) ||
Gareth Hughes22144ab2001-03-12 00:48:37 +0000853 SQ(m4m4-1) > SQ(1e-6))
Keith Whitwell23caf202000-11-16 21:05:34 +0000854 mat->flags |= MAT_FLAG_GENERAL_SCALE;
855
856 /* Check for rotation */
857 if (SQ(mm4) > SQ(1e-6))
858 mat->flags |= MAT_FLAG_GENERAL_3D;
859 else
860 mat->flags |= MAT_FLAG_ROTATION;
861
862 }
Brian Paulb51b0a82001-03-07 05:06:11 +0000863 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000864 mat->type = MATRIX_3D_NO_ROT;
865
866 /* Check for scale */
Gareth Hughes22144ab2001-03-12 00:48:37 +0000867 if (SQ(m[0]-m[5]) < SQ(1e-6) &&
Keith Whitwell23caf202000-11-16 21:05:34 +0000868 SQ(m[0]-m[10]) < SQ(1e-6)) {
869 if (SQ(m[0]-1.0) > SQ(1e-6)) {
870 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
871 }
872 }
873 else {
874 mat->flags |= MAT_FLAG_GENERAL_SCALE;
875 }
876 }
Brian Paulb51b0a82001-03-07 05:06:11 +0000877 else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000878 GLfloat c1 = DOT3(m,m);
879 GLfloat c2 = DOT3(m+4,m+4);
880 GLfloat c3 = DOT3(m+8,m+8);
881 GLfloat d1 = DOT3(m, m+4);
882 GLfloat cp[3];
883
884 mat->type = MATRIX_3D;
885
886 /* Check for scale */
887 if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) {
888 if (SQ(c1-1.0) > SQ(1e-6))
889 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
890 /* else no scale at all */
891 }
892 else {
893 mat->flags |= MAT_FLAG_GENERAL_SCALE;
894 }
895
896 /* Check for rotation */
897 if (SQ(d1) < SQ(1e-6)) {
898 CROSS3( cp, m, m+4 );
899 SUB_3V( cp, cp, (m+8) );
Gareth Hughes22144ab2001-03-12 00:48:37 +0000900 if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
Keith Whitwell23caf202000-11-16 21:05:34 +0000901 mat->flags |= MAT_FLAG_ROTATION;
902 else
903 mat->flags |= MAT_FLAG_GENERAL_3D;
904 }
905 else {
906 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
907 }
908 }
909 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
910 mat->type = MATRIX_PERSPECTIVE;
911 mat->flags |= MAT_FLAG_GENERAL;
912 }
913 else {
914 mat->type = MATRIX_GENERAL;
915 mat->flags |= MAT_FLAG_GENERAL;
916 }
917}
918
919
920/* Analyse a matrix given that its flags are accurate - this is the
Gareth Hughes22144ab2001-03-12 00:48:37 +0000921 * more common operation, hopefully.
Keith Whitwell23caf202000-11-16 21:05:34 +0000922 */
Keith Whitwellad2ac212000-11-24 10:25:05 +0000923static void analyse_from_flags( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +0000924{
925 const GLfloat *m = mat->m;
926
927 if (TEST_MAT_FLAGS(mat, 0)) {
928 mat->type = MATRIX_IDENTITY;
929 }
930 else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
931 MAT_FLAG_UNIFORM_SCALE |
932 MAT_FLAG_GENERAL_SCALE))) {
933 if ( m[10]==1.0F && m[14]==0.0F ) {
934 mat->type = MATRIX_2D_NO_ROT;
935 }
936 else {
937 mat->type = MATRIX_3D_NO_ROT;
938 }
939 }
940 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
Gareth Hughes22144ab2001-03-12 00:48:37 +0000941 if ( m[ 8]==0.0F
Keith Whitwell23caf202000-11-16 21:05:34 +0000942 && m[ 9]==0.0F
943 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
944 mat->type = MATRIX_2D;
945 }
946 else {
947 mat->type = MATRIX_3D;
948 }
949 }
950 else if ( m[4]==0.0F && m[12]==0.0F
951 && m[1]==0.0F && m[13]==0.0F
952 && m[2]==0.0F && m[6]==0.0F
953 && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
954 mat->type = MATRIX_PERSPECTIVE;
955 }
956 else {
957 mat->type = MATRIX_GENERAL;
958 }
959}
960
961
Gareth Hughes22144ab2001-03-12 00:48:37 +0000962void
963_math_matrix_analyse( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +0000964{
965 if (mat->flags & MAT_DIRTY_TYPE) {
Gareth Hughes22144ab2001-03-12 00:48:37 +0000966 if (mat->flags & MAT_DIRTY_FLAGS)
Keith Whitwellad2ac212000-11-24 10:25:05 +0000967 analyse_from_scratch( mat );
Keith Whitwell23caf202000-11-16 21:05:34 +0000968 else
Keith Whitwellad2ac212000-11-24 10:25:05 +0000969 analyse_from_flags( mat );
Keith Whitwell23caf202000-11-16 21:05:34 +0000970 }
971
972 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
973 matrix_invert( mat );
974 }
975
976 mat->flags &= ~(MAT_DIRTY_FLAGS|
977 MAT_DIRTY_TYPE|
978 MAT_DIRTY_INVERSE);
979}
980
981
Gareth Hughes22144ab2001-03-12 00:48:37 +0000982void
Keith Whitwell23caf202000-11-16 21:05:34 +0000983_math_matrix_copy( GLmatrix *to, const GLmatrix *from )
984{
985 MEMCPY( to->m, from->m, sizeof(Identity) );
986 to->flags = from->flags;
987 to->type = from->type;
988
989 if (to->inv != 0) {
990 if (from->inv == 0) {
991 matrix_invert( to );
992 }
993 else {
994 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
995 }
996 }
997}
998
999
Gareth Hughes22144ab2001-03-12 00:48:37 +00001000void
Keith Whitwell23caf202000-11-16 21:05:34 +00001001_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1002{
1003 GLfloat *m = mat->m;
1004 m[0] *= x; m[4] *= y; m[8] *= z;
1005 m[1] *= x; m[5] *= y; m[9] *= z;
1006 m[2] *= x; m[6] *= y; m[10] *= z;
1007 m[3] *= x; m[7] *= y; m[11] *= z;
1008
1009 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
1010 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1011 else
1012 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1013
Gareth Hughes22144ab2001-03-12 00:48:37 +00001014 mat->flags |= (MAT_DIRTY_TYPE |
Keith Whitwell23caf202000-11-16 21:05:34 +00001015 MAT_DIRTY_INVERSE);
1016}
1017
1018
Gareth Hughes22144ab2001-03-12 00:48:37 +00001019void
Keith Whitwell23caf202000-11-16 21:05:34 +00001020_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1021{
1022 GLfloat *m = mat->m;
1023 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1024 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1025 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1026 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1027
Gareth Hughes22144ab2001-03-12 00:48:37 +00001028 mat->flags |= (MAT_FLAG_TRANSLATION |
1029 MAT_DIRTY_TYPE |
Keith Whitwell23caf202000-11-16 21:05:34 +00001030 MAT_DIRTY_INVERSE);
1031}
1032
1033
Gareth Hughes22144ab2001-03-12 00:48:37 +00001034void
Keith Whitwell23caf202000-11-16 21:05:34 +00001035_math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1036{
1037 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1038 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1039}
1040
Gareth Hughes22144ab2001-03-12 00:48:37 +00001041void
Keith Whitwell23caf202000-11-16 21:05:34 +00001042_math_matrix_ctr( GLmatrix *m )
1043{
Brian Paul30f51ae2001-12-18 04:06:44 +00001044 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1045 if (m->m)
1046 MEMCPY( m->m, Identity, sizeof(Identity) );
1047 m->inv = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001048 m->type = MATRIX_IDENTITY;
1049 m->flags = 0;
1050}
1051
Gareth Hughes22144ab2001-03-12 00:48:37 +00001052void
Keith Whitwell23caf202000-11-16 21:05:34 +00001053_math_matrix_dtr( GLmatrix *m )
1054{
Brian Paul30f51ae2001-12-18 04:06:44 +00001055 if (m->m) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001056 ALIGN_FREE( m->m );
Brian Paul30f51ae2001-12-18 04:06:44 +00001057 m->m = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001058 }
Brian Paul30f51ae2001-12-18 04:06:44 +00001059 if (m->inv) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001060 ALIGN_FREE( m->inv );
Brian Paul30f51ae2001-12-18 04:06:44 +00001061 m->inv = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001062 }
1063}
1064
1065
Gareth Hughes22144ab2001-03-12 00:48:37 +00001066void
Keith Whitwell23caf202000-11-16 21:05:34 +00001067_math_matrix_alloc_inv( GLmatrix *m )
1068{
Brian Paul30f51ae2001-12-18 04:06:44 +00001069 if (!m->inv) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001070 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
Brian Paul30f51ae2001-12-18 04:06:44 +00001071 if (m->inv)
1072 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
Keith Whitwell23caf202000-11-16 21:05:34 +00001073 }
1074}
1075
1076
Gareth Hughes22144ab2001-03-12 00:48:37 +00001077void
Keith Whitwell23caf202000-11-16 21:05:34 +00001078_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
1079{
1080 dest->flags = (a->flags |
1081 b->flags |
Gareth Hughes22144ab2001-03-12 00:48:37 +00001082 MAT_DIRTY_TYPE |
Keith Whitwell23caf202000-11-16 21:05:34 +00001083 MAT_DIRTY_INVERSE);
1084
1085 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
1086 matmul34( dest->m, a->m, b->m );
Gareth Hughes22144ab2001-03-12 00:48:37 +00001087 else
Keith Whitwell23caf202000-11-16 21:05:34 +00001088 matmul4( dest->m, a->m, b->m );
1089}
1090
1091
Gareth Hughes22144ab2001-03-12 00:48:37 +00001092void
Keith Whitwell23caf202000-11-16 21:05:34 +00001093_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
1094{
1095 dest->flags |= (MAT_FLAG_GENERAL |
Gareth Hughes22144ab2001-03-12 00:48:37 +00001096 MAT_DIRTY_TYPE |
Keith Whitwell23caf202000-11-16 21:05:34 +00001097 MAT_DIRTY_INVERSE);
1098
Gareth Hughes22144ab2001-03-12 00:48:37 +00001099 matmul4( dest->m, dest->m, m );
Keith Whitwell23caf202000-11-16 21:05:34 +00001100}
1101
Gareth Hughes22144ab2001-03-12 00:48:37 +00001102void
Keith Whitwell23caf202000-11-16 21:05:34 +00001103_math_matrix_set_identity( GLmatrix *mat )
1104{
1105 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1106
1107 if (mat->inv)
1108 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1109
1110 mat->type = MATRIX_IDENTITY;
1111 mat->flags &= ~(MAT_DIRTY_FLAGS|
1112 MAT_DIRTY_TYPE|
1113 MAT_DIRTY_INVERSE);
1114}
1115
1116
1117
Gareth Hughes22144ab2001-03-12 00:48:37 +00001118void
Keith Whitwell23caf202000-11-16 21:05:34 +00001119_math_transposef( GLfloat to[16], const GLfloat from[16] )
1120{
1121 to[0] = from[0];
1122 to[1] = from[4];
1123 to[2] = from[8];
1124 to[3] = from[12];
1125 to[4] = from[1];
1126 to[5] = from[5];
1127 to[6] = from[9];
1128 to[7] = from[13];
1129 to[8] = from[2];
1130 to[9] = from[6];
1131 to[10] = from[10];
1132 to[11] = from[14];
1133 to[12] = from[3];
1134 to[13] = from[7];
1135 to[14] = from[11];
1136 to[15] = from[15];
1137}
1138
1139
Gareth Hughes22144ab2001-03-12 00:48:37 +00001140void
Keith Whitwell23caf202000-11-16 21:05:34 +00001141_math_transposed( GLdouble to[16], const GLdouble from[16] )
1142{
1143 to[0] = from[0];
1144 to[1] = from[4];
1145 to[2] = from[8];
1146 to[3] = from[12];
1147 to[4] = from[1];
1148 to[5] = from[5];
1149 to[6] = from[9];
1150 to[7] = from[13];
1151 to[8] = from[2];
1152 to[9] = from[6];
1153 to[10] = from[10];
1154 to[11] = from[14];
1155 to[12] = from[3];
1156 to[13] = from[7];
1157 to[14] = from[11];
1158 to[15] = from[15];
1159}
1160
Gareth Hughes22144ab2001-03-12 00:48:37 +00001161void
Keith Whitwell23caf202000-11-16 21:05:34 +00001162_math_transposefd( GLfloat to[16], const GLdouble from[16] )
1163{
Karl Schultz7b9fe822001-09-18 23:06:14 +00001164 to[0] = (GLfloat) from[0];
1165 to[1] = (GLfloat) from[4];
1166 to[2] = (GLfloat) from[8];
1167 to[3] = (GLfloat) from[12];
1168 to[4] = (GLfloat) from[1];
1169 to[5] = (GLfloat) from[5];
1170 to[6] = (GLfloat) from[9];
1171 to[7] = (GLfloat) from[13];
1172 to[8] = (GLfloat) from[2];
1173 to[9] = (GLfloat) from[6];
1174 to[10] = (GLfloat) from[10];
1175 to[11] = (GLfloat) from[14];
1176 to[12] = (GLfloat) from[3];
1177 to[13] = (GLfloat) from[7];
1178 to[14] = (GLfloat) from[11];
1179 to[15] = (GLfloat) from[15];
Keith Whitwell23caf202000-11-16 21:05:34 +00001180}