blob: 95b6ebed56935d968e5381b5a9c4a20734bcdc33 [file] [log] [blame]
Brian Paul4991d0f2002-09-12 16:26:04 +00001/* $Id: m_matrix.c,v 1.13 2002/09/12 16:26:04 brianp Exp $ */
Keith Whitwell23caf202000-11-16 21:05:34 +00002
3/*
4 * Mesa 3-D graphics library
Brian Paul4991d0f2002-09-12 16:26:04 +00005 * Version: 4.1
Gareth Hughes22144ab2001-03-12 00:48:37 +00006 *
Brian Paula68b8df2002-03-29 17:18:08 +00007 * Copyright (C) 1999-2002 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 Whitwellf4b02d12001-01-05 05:31:42 +000037#include <math.h>
38
Keith Whitwell23caf202000-11-16 21:05:34 +000039#include "glheader.h"
Brian Paul4e9676f2002-06-29 19:48:15 +000040#include "imports.h"
Keith Whitwell23caf202000-11-16 21:05:34 +000041#include "macros.h"
42#include "mem.h"
43#include "mmath.h"
44
45#include "m_matrix.h"
46
Keith Whitwellf4b02d12001-01-05 05:31:42 +000047
Keith Whitwell23caf202000-11-16 21:05:34 +000048static const char *types[] = {
49 "MATRIX_GENERAL",
50 "MATRIX_IDENTITY",
51 "MATRIX_3D_NO_ROT",
52 "MATRIX_PERSPECTIVE",
53 "MATRIX_2D",
54 "MATRIX_2D_NO_ROT",
55 "MATRIX_3D"
56};
57
58
59static GLfloat Identity[16] = {
60 1.0, 0.0, 0.0, 0.0,
61 0.0, 1.0, 0.0, 0.0,
62 0.0, 0.0, 1.0, 0.0,
63 0.0, 0.0, 0.0, 1.0
64};
65
66
67
68
69/*
Gareth Hughes22144ab2001-03-12 00:48:37 +000070 * This matmul was contributed by Thomas Malik
Keith Whitwell23caf202000-11-16 21:05:34 +000071 *
72 * Perform a 4x4 matrix multiplication (product = a x b).
73 * Input: a, b - matrices to multiply
74 * Output: product - product of a and b
75 * WARNING: (product != b) assumed
Gareth Hughes22144ab2001-03-12 00:48:37 +000076 * NOTE: (product == a) allowed
Keith Whitwell23caf202000-11-16 21:05:34 +000077 *
78 * KW: 4*16 = 64 muls
79 */
80#define A(row,col) a[(col<<2)+row]
81#define B(row,col) b[(col<<2)+row]
82#define P(row,col) product[(col<<2)+row]
83
84static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
85{
86 GLint i;
87 for (i = 0; i < 4; i++) {
88 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
89 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
90 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
91 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
92 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
93 }
94}
95
96
97/* Multiply two matrices known to occupy only the top three rows, such
Gareth Hughes22144ab2001-03-12 00:48:37 +000098 * as typical model matrices, and ortho matrices.
Keith Whitwell23caf202000-11-16 21:05:34 +000099 */
100static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
101{
102 GLint i;
103 for (i = 0; i < 3; i++) {
104 const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
105 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
106 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
107 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
108 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
109 }
110 P(3,0) = 0;
111 P(3,1) = 0;
112 P(3,2) = 0;
113 P(3,3) = 1;
114}
115
116
117#undef A
118#undef B
119#undef P
120
121
122/*
123 * Multiply a matrix by an array of floats with known properties.
124 */
125static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
126{
127 mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
128
129 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
130 matmul34( mat->m, mat->m, m );
Gareth Hughes22144ab2001-03-12 00:48:37 +0000131 else
132 matmul4( mat->m, mat->m, m );
Keith Whitwell23caf202000-11-16 21:05:34 +0000133}
134
135
136static void print_matrix_floats( const GLfloat m[16] )
137{
138 int i;
139 for (i=0;i<4;i++) {
Brian Paul4e9676f2002-06-29 19:48:15 +0000140 _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 +0000141 }
142}
143
Gareth Hughes22144ab2001-03-12 00:48:37 +0000144void
Keith Whitwell23caf202000-11-16 21:05:34 +0000145_math_matrix_print( const GLmatrix *m )
146{
Brian Paul4e9676f2002-06-29 19:48:15 +0000147 _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
Keith Whitwell23caf202000-11-16 21:05:34 +0000148 print_matrix_floats(m->m);
Brian Paul4e9676f2002-06-29 19:48:15 +0000149 _mesa_debug(NULL, "Inverse: \n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000150 if (m->inv) {
151 GLfloat prod[16];
152 print_matrix_floats(m->inv);
153 matmul4(prod, m->m, m->inv);
Brian Paul4e9676f2002-06-29 19:48:15 +0000154 _mesa_debug(NULL, "Mat * Inverse:\n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000155 print_matrix_floats(prod);
156 }
157 else {
Brian Paul4e9676f2002-06-29 19:48:15 +0000158 _mesa_debug(NULL, " - not available\n");
Keith Whitwell23caf202000-11-16 21:05:34 +0000159 }
160}
161
162
163
164
165#define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
166#define MAT(m,r,c) (m)[(c)*4+(r)]
167
168/*
169 * Compute inverse of 4x4 transformation matrix.
170 * Code contributed by Jacques Leroy jle@star.be
171 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
172 */
173static GLboolean invert_matrix_general( GLmatrix *mat )
174{
175 const GLfloat *m = mat->m;
176 GLfloat *out = mat->inv;
177 GLfloat wtmp[4][8];
178 GLfloat m0, m1, m2, m3, s;
179 GLfloat *r0, *r1, *r2, *r3;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000180
Keith Whitwell23caf202000-11-16 21:05:34 +0000181 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
Gareth Hughes22144ab2001-03-12 00:48:37 +0000182
Keith Whitwell23caf202000-11-16 21:05:34 +0000183 r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
184 r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
185 r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000186
Keith Whitwell23caf202000-11-16 21:05:34 +0000187 r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
188 r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
189 r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000190
Keith Whitwell23caf202000-11-16 21:05:34 +0000191 r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
192 r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
193 r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000194
Keith Whitwell23caf202000-11-16 21:05:34 +0000195 r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
196 r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
197 r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000198
Keith Whitwell23caf202000-11-16 21:05:34 +0000199 /* choose pivot - or die */
200 if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
201 if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
202 if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
203 if (0.0 == r0[0]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000204
Keith Whitwell23caf202000-11-16 21:05:34 +0000205 /* eliminate first variable */
206 m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
207 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
208 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
209 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
210 s = r0[4];
211 if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
212 s = r0[5];
213 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
214 s = r0[6];
215 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
216 s = r0[7];
217 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000218
Keith Whitwell23caf202000-11-16 21:05:34 +0000219 /* choose pivot - or die */
220 if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
221 if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
222 if (0.0 == r1[1]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000223
Keith Whitwell23caf202000-11-16 21:05:34 +0000224 /* eliminate second variable */
225 m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
226 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
227 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
228 s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
229 s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
230 s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
231 s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000232
Keith Whitwell23caf202000-11-16 21:05:34 +0000233 /* choose pivot - or die */
234 if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
235 if (0.0 == r2[2]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000236
Keith Whitwell23caf202000-11-16 21:05:34 +0000237 /* eliminate third variable */
238 m3 = r3[2]/r2[2];
239 r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
240 r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
241 r3[7] -= m3 * r2[7];
Gareth Hughes22144ab2001-03-12 00:48:37 +0000242
Keith Whitwell23caf202000-11-16 21:05:34 +0000243 /* last check */
244 if (0.0 == r3[3]) return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000245
Karl Schultz7b9fe822001-09-18 23:06:14 +0000246 s = 1.0F/r3[3]; /* now back substitute row 3 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000247 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000248
Keith Whitwell23caf202000-11-16 21:05:34 +0000249 m2 = r2[3]; /* now back substitute row 2 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000250 s = 1.0F/r2[2];
Keith Whitwell23caf202000-11-16 21:05:34 +0000251 r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
252 r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
253 m1 = r1[3];
254 r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
255 r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
256 m0 = r0[3];
257 r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
258 r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000259
Keith Whitwell23caf202000-11-16 21:05:34 +0000260 m1 = r1[2]; /* now back substitute row 1 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000261 s = 1.0F/r1[1];
Keith Whitwell23caf202000-11-16 21:05:34 +0000262 r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
263 r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
264 m0 = r0[2];
265 r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
266 r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000267
Keith Whitwell23caf202000-11-16 21:05:34 +0000268 m0 = r0[1]; /* now back substitute row 0 */
Karl Schultz7b9fe822001-09-18 23:06:14 +0000269 s = 1.0F/r0[0];
Keith Whitwell23caf202000-11-16 21:05:34 +0000270 r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
271 r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
Gareth Hughes22144ab2001-03-12 00:48:37 +0000272
Keith Whitwell23caf202000-11-16 21:05:34 +0000273 MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
274 MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
275 MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
276 MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
277 MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
278 MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
279 MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
Gareth Hughes22144ab2001-03-12 00:48:37 +0000280 MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
281
Keith Whitwell23caf202000-11-16 21:05:34 +0000282 return GL_TRUE;
283}
284#undef SWAP_ROWS
285
286
287/* Adapted from graphics gems II.
Gareth Hughes22144ab2001-03-12 00:48:37 +0000288 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000289static GLboolean invert_matrix_3d_general( GLmatrix *mat )
290{
291 const GLfloat *in = mat->m;
292 GLfloat *out = mat->inv;
293 GLfloat pos, neg, t;
294 GLfloat det;
295
296 /* Calculate the determinant of upper left 3x3 submatrix and
Gareth Hughes22144ab2001-03-12 00:48:37 +0000297 * determine if the matrix is singular.
Keith Whitwell23caf202000-11-16 21:05:34 +0000298 */
299 pos = neg = 0.0;
300 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
301 if (t >= 0.0) pos += t; else neg += t;
302
303 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
304 if (t >= 0.0) pos += t; else neg += t;
305
306 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
307 if (t >= 0.0) pos += t; else neg += t;
308
309 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
310 if (t >= 0.0) pos += t; else neg += t;
311
312 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
313 if (t >= 0.0) pos += t; else neg += t;
314
315 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
316 if (t >= 0.0) pos += t; else neg += t;
317
318 det = pos + neg;
319
Gareth Hughes22144ab2001-03-12 00:48:37 +0000320 if (det*det < 1e-25)
Keith Whitwell23caf202000-11-16 21:05:34 +0000321 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000322
Karl Schultz7b9fe822001-09-18 23:06:14 +0000323 det = 1.0F / det;
Keith Whitwell23caf202000-11-16 21:05:34 +0000324 MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
325 MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
326 MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
327 MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
328 MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
329 MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
330 MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
331 MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
332 MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
333
334 /* Do the translation part */
335 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
336 MAT(in,1,3) * MAT(out,0,1) +
337 MAT(in,2,3) * MAT(out,0,2) );
338 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
339 MAT(in,1,3) * MAT(out,1,1) +
340 MAT(in,2,3) * MAT(out,1,2) );
341 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
342 MAT(in,1,3) * MAT(out,2,1) +
343 MAT(in,2,3) * MAT(out,2,2) );
Gareth Hughes22144ab2001-03-12 00:48:37 +0000344
Keith Whitwell23caf202000-11-16 21:05:34 +0000345 return GL_TRUE;
346}
347
348
349static GLboolean invert_matrix_3d( GLmatrix *mat )
350{
351 const GLfloat *in = mat->m;
352 GLfloat *out = mat->inv;
353
354 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
355 return invert_matrix_3d_general( mat );
356 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000357
Keith Whitwell23caf202000-11-16 21:05:34 +0000358 if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
359 GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
360 MAT(in,0,1) * MAT(in,0,1) +
361 MAT(in,0,2) * MAT(in,0,2));
362
Gareth Hughes22144ab2001-03-12 00:48:37 +0000363 if (scale == 0.0)
Keith Whitwell23caf202000-11-16 21:05:34 +0000364 return GL_FALSE;
365
Karl Schultz7b9fe822001-09-18 23:06:14 +0000366 scale = 1.0F / scale;
Keith Whitwell23caf202000-11-16 21:05:34 +0000367
368 /* Transpose and scale the 3 by 3 upper-left submatrix. */
369 MAT(out,0,0) = scale * MAT(in,0,0);
370 MAT(out,1,0) = scale * MAT(in,0,1);
371 MAT(out,2,0) = scale * MAT(in,0,2);
372 MAT(out,0,1) = scale * MAT(in,1,0);
373 MAT(out,1,1) = scale * MAT(in,1,1);
374 MAT(out,2,1) = scale * MAT(in,1,2);
375 MAT(out,0,2) = scale * MAT(in,2,0);
376 MAT(out,1,2) = scale * MAT(in,2,1);
377 MAT(out,2,2) = scale * MAT(in,2,2);
378 }
379 else if (mat->flags & MAT_FLAG_ROTATION) {
380 /* Transpose the 3 by 3 upper-left submatrix. */
381 MAT(out,0,0) = MAT(in,0,0);
382 MAT(out,1,0) = MAT(in,0,1);
383 MAT(out,2,0) = MAT(in,0,2);
384 MAT(out,0,1) = MAT(in,1,0);
385 MAT(out,1,1) = MAT(in,1,1);
386 MAT(out,2,1) = MAT(in,1,2);
387 MAT(out,0,2) = MAT(in,2,0);
388 MAT(out,1,2) = MAT(in,2,1);
389 MAT(out,2,2) = MAT(in,2,2);
390 }
391 else {
392 /* pure translation */
393 MEMCPY( out, Identity, sizeof(Identity) );
394 MAT(out,0,3) = - MAT(in,0,3);
395 MAT(out,1,3) = - MAT(in,1,3);
396 MAT(out,2,3) = - MAT(in,2,3);
397 return GL_TRUE;
398 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000399
Keith Whitwell23caf202000-11-16 21:05:34 +0000400 if (mat->flags & MAT_FLAG_TRANSLATION) {
401 /* Do the translation part */
402 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
403 MAT(in,1,3) * MAT(out,0,1) +
404 MAT(in,2,3) * MAT(out,0,2) );
405 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
406 MAT(in,1,3) * MAT(out,1,1) +
407 MAT(in,2,3) * MAT(out,1,2) );
408 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
409 MAT(in,1,3) * MAT(out,2,1) +
410 MAT(in,2,3) * MAT(out,2,2) );
411 }
412 else {
413 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
414 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000415
Keith Whitwell23caf202000-11-16 21:05:34 +0000416 return GL_TRUE;
417}
418
Gareth Hughes22144ab2001-03-12 00:48:37 +0000419
Keith Whitwell23caf202000-11-16 21:05:34 +0000420
421static GLboolean invert_matrix_identity( GLmatrix *mat )
422{
423 MEMCPY( mat->inv, Identity, sizeof(Identity) );
424 return GL_TRUE;
425}
426
427
428static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
429{
430 const GLfloat *in = mat->m;
431 GLfloat *out = mat->inv;
432
Gareth Hughes22144ab2001-03-12 00:48:37 +0000433 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
Keith Whitwell23caf202000-11-16 21:05:34 +0000434 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000435
Keith Whitwell23caf202000-11-16 21:05:34 +0000436 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
Karl Schultz7b9fe822001-09-18 23:06:14 +0000437 MAT(out,0,0) = 1.0F / MAT(in,0,0);
438 MAT(out,1,1) = 1.0F / MAT(in,1,1);
439 MAT(out,2,2) = 1.0F / MAT(in,2,2);
Keith Whitwell23caf202000-11-16 21:05:34 +0000440
441 if (mat->flags & MAT_FLAG_TRANSLATION) {
442 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
443 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
444 MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
445 }
446
447 return GL_TRUE;
448}
449
450
451static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
452{
453 const GLfloat *in = mat->m;
454 GLfloat *out = mat->inv;
455
Gareth Hughes22144ab2001-03-12 00:48:37 +0000456 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
Keith Whitwell23caf202000-11-16 21:05:34 +0000457 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000458
Keith Whitwell23caf202000-11-16 21:05:34 +0000459 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
Karl Schultz7b9fe822001-09-18 23:06:14 +0000460 MAT(out,0,0) = 1.0F / MAT(in,0,0);
461 MAT(out,1,1) = 1.0F / MAT(in,1,1);
Keith Whitwell23caf202000-11-16 21:05:34 +0000462
463 if (mat->flags & MAT_FLAG_TRANSLATION) {
464 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
465 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
466 }
467
468 return GL_TRUE;
469}
470
471
Brian Paul4e9676f2002-06-29 19:48:15 +0000472#if 0
473/* broken */
Keith Whitwell23caf202000-11-16 21:05:34 +0000474static GLboolean invert_matrix_perspective( GLmatrix *mat )
475{
476 const GLfloat *in = mat->m;
477 GLfloat *out = mat->inv;
478
479 if (MAT(in,2,3) == 0)
480 return GL_FALSE;
481
482 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
483
Karl Schultz7b9fe822001-09-18 23:06:14 +0000484 MAT(out,0,0) = 1.0F / MAT(in,0,0);
485 MAT(out,1,1) = 1.0F / MAT(in,1,1);
Keith Whitwell23caf202000-11-16 21:05:34 +0000486
487 MAT(out,0,3) = MAT(in,0,2);
488 MAT(out,1,3) = MAT(in,1,2);
489
490 MAT(out,2,2) = 0;
491 MAT(out,2,3) = -1;
492
Karl Schultz7b9fe822001-09-18 23:06:14 +0000493 MAT(out,3,2) = 1.0F / MAT(in,2,3);
Keith Whitwell23caf202000-11-16 21:05:34 +0000494 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
495
496 return GL_TRUE;
497}
Brian Paul4e9676f2002-06-29 19:48:15 +0000498#endif
Keith Whitwell23caf202000-11-16 21:05:34 +0000499
500
501typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
502
503
504static inv_mat_func inv_mat_tab[7] = {
505 invert_matrix_general,
506 invert_matrix_identity,
507 invert_matrix_3d_no_rot,
Brian Paula68b8df2002-03-29 17:18:08 +0000508#if 0
509 /* Don't use this function for now - it fails when the projection matrix
510 * is premultiplied by a translation (ala Chromium's tilesort SPU).
511 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000512 invert_matrix_perspective,
Brian Paula68b8df2002-03-29 17:18:08 +0000513#else
514 invert_matrix_general,
515#endif
Keith Whitwell23caf202000-11-16 21:05:34 +0000516 invert_matrix_3d, /* lazy! */
517 invert_matrix_2d_no_rot,
518 invert_matrix_3d
519};
520
521
522static GLboolean matrix_invert( GLmatrix *mat )
523{
524 if (inv_mat_tab[mat->type](mat)) {
525 mat->flags &= ~MAT_FLAG_SINGULAR;
526 return GL_TRUE;
527 } else {
528 mat->flags |= MAT_FLAG_SINGULAR;
529 MEMCPY( mat->inv, Identity, sizeof(Identity) );
530 return GL_FALSE;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000531 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000532}
533
534
535
536
537
538
539/*
540 * Generate a 4x4 transformation matrix from glRotate parameters, and
541 * postmultiply the input matrix by it.
Brian Paul4991d0f2002-09-12 16:26:04 +0000542 * This function contributed by Erich Boleyn (erich@uruk.org).
543 * Optimizatios contributed by Rudolf Opalla (rudi@khm.de).
Keith Whitwell23caf202000-11-16 21:05:34 +0000544 */
Gareth Hughes22144ab2001-03-12 00:48:37 +0000545void
546_math_matrix_rotate( GLmatrix *mat,
Keith Whitwell23caf202000-11-16 21:05:34 +0000547 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
548{
Brian Paul4991d0f2002-09-12 16:26:04 +0000549 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
Keith Whitwell23caf202000-11-16 21:05:34 +0000550 GLfloat m[16];
Brian Paul4991d0f2002-09-12 16:26:04 +0000551 GLboolean optimized;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000552
Karl Schultz7b9fe822001-09-18 23:06:14 +0000553 s = (GLfloat) sin( angle * DEG2RAD );
554 c = (GLfloat) cos( angle * DEG2RAD );
Keith Whitwell23caf202000-11-16 21:05:34 +0000555
Brian Paul4991d0f2002-09-12 16:26:04 +0000556 MEMCPY(m, Identity, sizeof(GLfloat)*16);
557 optimized = GL_FALSE;
Keith Whitwell23caf202000-11-16 21:05:34 +0000558
559#define M(row,col) m[col*4+row]
560
Brian Paul4991d0f2002-09-12 16:26:04 +0000561 if (x == 0.0F) {
562 if (y == 0.0F) {
563 if (z != 0.0F) {
564 optimized = GL_TRUE;
565 /* rotate only around z-axis */
566 M(0,0) = c;
567 M(1,1) = c;
568 if (z < 0.0F) {
569 M(0,1) = s;
570 M(1,0) = -s;
571 }
572 else {
573 M(0,1) = -s;
574 M(1,0) = s;
575 }
576 }
577 }
578 else if (z == 0.0F) {
579 optimized = GL_TRUE;
580 /* rotate only around y-axis */
581 M(0,0) = c;
582 M(2,2) = c;
583 if (y < 0.0F) {
584 M(0,2) = -s;
585 M(2,0) = s;
586 }
587 else {
588 M(0,2) = s;
589 M(2,0) = -s;
590 }
591 }
592 }
593 else if (y == 0.0F) {
594 if (z == 0.0F) {
595 optimized = GL_TRUE;
596 /* rotate only around x-axis */
597 M(1,1) = c;
598 M(2,2) = c;
599 if (y < 0.0F) {
600 M(1,2) = s;
601 M(2,1) = -s;
602 }
603 else {
604 M(1,2) = -s;
605 M(2,1) = s;
606 }
607 }
608 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000609
Brian Paul4991d0f2002-09-12 16:26:04 +0000610 if (!optimized) {
611 const GLfloat mag = (GLfloat) GL_SQRT(x * x + y * y + z * z);
Keith Whitwell23caf202000-11-16 21:05:34 +0000612
Brian Paul4991d0f2002-09-12 16:26:04 +0000613 if (mag <= 1.0e-4) {
614 /* no rotation, leave mat as-is */
615 return;
616 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000617
Brian Paul4991d0f2002-09-12 16:26:04 +0000618 x /= mag;
619 y /= mag;
620 z /= mag;
Keith Whitwell23caf202000-11-16 21:05:34 +0000621
Keith Whitwell23caf202000-11-16 21:05:34 +0000622
Brian Paul4991d0f2002-09-12 16:26:04 +0000623 /*
624 * Arbitrary axis rotation matrix.
625 *
626 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
627 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
628 * (which is about the X-axis), and the two composite transforms
629 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
630 * from the arbitrary axis to the X-axis then back. They are
631 * all elementary rotations.
632 *
633 * Rz' is a rotation about the Z-axis, to bring the axis vector
634 * into the x-z plane. Then Ry' is applied, rotating about the
635 * Y-axis to bring the axis vector parallel with the X-axis. The
636 * rotation about the X-axis is then performed. Ry and Rz are
637 * simply the respective inverse transforms to bring the arbitrary
638 * axis back to it's original orientation. The first transforms
639 * Rz' and Ry' are considered inverses, since the data from the
640 * arbitrary axis gives you info on how to get to it, not how
641 * to get away from it, and an inverse must be applied.
642 *
643 * The basic calculation used is to recognize that the arbitrary
644 * axis vector (x, y, z), since it is of unit length, actually
645 * represents the sines and cosines of the angles to rotate the
646 * X-axis to the same orientation, with theta being the angle about
647 * Z and phi the angle about Y (in the order described above)
648 * as follows:
649 *
650 * cos ( theta ) = x / sqrt ( 1 - z^2 )
651 * sin ( theta ) = y / sqrt ( 1 - z^2 )
652 *
653 * cos ( phi ) = sqrt ( 1 - z^2 )
654 * sin ( phi ) = z
655 *
656 * Note that cos ( phi ) can further be inserted to the above
657 * formulas:
658 *
659 * cos ( theta ) = x / cos ( phi )
660 * sin ( theta ) = y / sin ( phi )
661 *
662 * ...etc. Because of those relations and the standard trigonometric
663 * relations, it is pssible to reduce the transforms down to what
664 * is used below. It may be that any primary axis chosen will give the
665 * same results (modulo a sign convention) using thie method.
666 *
667 * Particularly nice is to notice that all divisions that might
668 * have caused trouble when parallel to certain planes or
669 * axis go away with care paid to reducing the expressions.
670 * After checking, it does perform correctly under all cases, since
671 * in all the cases of division where the denominator would have
672 * been zero, the numerator would have been zero as well, giving
673 * the expected result.
674 */
Keith Whitwell23caf202000-11-16 21:05:34 +0000675
Brian Paul4991d0f2002-09-12 16:26:04 +0000676 xx = x * x;
677 yy = y * y;
678 zz = z * z;
679 xy = x * y;
680 yz = y * z;
681 zx = z * x;
682 xs = x * s;
683 ys = y * s;
684 zs = z * s;
685 one_c = 1.0F - c;
686
687 /* We already hold the identity-matrix so we can skip some statements */
688 M(0,0) = (one_c * xx) + c;
689 M(0,1) = (one_c * xy) - zs;
690 M(0,2) = (one_c * zx) + ys;
691/* M(0,3) = 0.0F; */
692
693 M(1,0) = (one_c * xy) + zs;
694 M(1,1) = (one_c * yy) + c;
695 M(1,2) = (one_c * yz) - xs;
696/* M(1,3) = 0.0F; */
697
698 M(2,0) = (one_c * zx) - ys;
699 M(2,1) = (one_c * yz) + xs;
700 M(2,2) = (one_c * zz) + c;
701/* M(2,3) = 0.0F; */
702
703/*
704 M(3,0) = 0.0F;
705 M(3,1) = 0.0F;
706 M(3,2) = 0.0F;
707 M(3,3) = 1.0F;
708*/
709 }
Keith Whitwell23caf202000-11-16 21:05:34 +0000710#undef M
711
712 matrix_multf( mat, m, MAT_FLAG_ROTATION );
713}
714
715
Brian Paul4991d0f2002-09-12 16:26:04 +0000716
Keith Whitwell23caf202000-11-16 21:05:34 +0000717void
Gareth Hughes22144ab2001-03-12 00:48:37 +0000718_math_matrix_frustum( GLmatrix *mat,
Brian Pauld8bc5a92001-02-05 18:48:52 +0000719 GLfloat left, GLfloat right,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000720 GLfloat bottom, GLfloat top,
Brian Pauld8bc5a92001-02-05 18:48:52 +0000721 GLfloat nearval, GLfloat farval )
Keith Whitwell23caf202000-11-16 21:05:34 +0000722{
723 GLfloat x, y, a, b, c, d;
724 GLfloat m[16];
725
Karl Schultz7b9fe822001-09-18 23:06:14 +0000726 x = (2.0F*nearval) / (right-left);
727 y = (2.0F*nearval) / (top-bottom);
Keith Whitwell23caf202000-11-16 21:05:34 +0000728 a = (right+left) / (right-left);
729 b = (top+bottom) / (top-bottom);
730 c = -(farval+nearval) / ( farval-nearval);
Karl Schultz7b9fe822001-09-18 23:06:14 +0000731 d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
Keith Whitwell23caf202000-11-16 21:05:34 +0000732
733#define M(row,col) m[col*4+row]
734 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
735 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
736 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
737 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
738#undef M
739
740 matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
741}
742
743void
Gareth Hughes22144ab2001-03-12 00:48:37 +0000744_math_matrix_ortho( GLmatrix *mat,
Keith Whitwell23caf202000-11-16 21:05:34 +0000745 GLfloat left, GLfloat right,
Gareth Hughes22144ab2001-03-12 00:48:37 +0000746 GLfloat bottom, GLfloat top,
Keith Whitwell23caf202000-11-16 21:05:34 +0000747 GLfloat nearval, GLfloat farval )
748{
749 GLfloat x, y, z;
750 GLfloat tx, ty, tz;
751 GLfloat m[16];
752
Karl Schultz7b9fe822001-09-18 23:06:14 +0000753 x = 2.0F / (right-left);
754 y = 2.0F / (top-bottom);
755 z = -2.0F / (farval-nearval);
Keith Whitwell23caf202000-11-16 21:05:34 +0000756 tx = -(right+left) / (right-left);
757 ty = -(top+bottom) / (top-bottom);
758 tz = -(farval+nearval) / (farval-nearval);
759
760#define M(row,col) m[col*4+row]
761 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = 0.0F; M(0,3) = tx;
762 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = 0.0F; M(1,3) = ty;
763 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = z; M(2,3) = tz;
764 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = 0.0F; M(3,3) = 1.0F;
765#undef M
766
767 matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
768}
769
770
771#define ZERO(x) (1<<x)
772#define ONE(x) (1<<(x+16))
773
774#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
775#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
776
777#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
778 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
779 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
780 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
781
782#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
783 ZERO(1) | ZERO(9) | \
784 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
785 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
786
787#define MASK_2D ( ZERO(8) | \
788 ZERO(9) | \
789 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
790 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
791
792
793#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
794 ZERO(1) | ZERO(9) | \
795 ZERO(2) | ZERO(6) | \
796 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
797
798#define MASK_3D ( \
799 \
800 \
801 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
802
803
804#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
805 ZERO(1) | ZERO(13) |\
806 ZERO(2) | ZERO(6) | \
807 ZERO(3) | ZERO(7) | ZERO(15) )
808
809#define SQ(x) ((x)*(x))
Gareth Hughes22144ab2001-03-12 00:48:37 +0000810
Keith Whitwell23caf202000-11-16 21:05:34 +0000811/* Determine type and flags from scratch. This is expensive enough to
812 * only want to do it once.
813 */
Keith Whitwellad2ac212000-11-24 10:25:05 +0000814static void analyse_from_scratch( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +0000815{
816 const GLfloat *m = mat->m;
817 GLuint mask = 0;
818 GLuint i;
819
820 for (i = 0 ; i < 16 ; i++) {
821 if (m[i] == 0.0) mask |= (1<<i);
822 }
Gareth Hughes22144ab2001-03-12 00:48:37 +0000823
Keith Whitwell23caf202000-11-16 21:05:34 +0000824 if (m[0] == 1.0F) mask |= (1<<16);
825 if (m[5] == 1.0F) mask |= (1<<21);
826 if (m[10] == 1.0F) mask |= (1<<26);
827 if (m[15] == 1.0F) mask |= (1<<31);
828
829 mat->flags &= ~MAT_FLAGS_GEOMETRY;
830
Gareth Hughes22144ab2001-03-12 00:48:37 +0000831 /* Check for translation - no-one really cares
Keith Whitwell23caf202000-11-16 21:05:34 +0000832 */
Gareth Hughes22144ab2001-03-12 00:48:37 +0000833 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
834 mat->flags |= MAT_FLAG_TRANSLATION;
Keith Whitwell23caf202000-11-16 21:05:34 +0000835
836 /* Do the real work
837 */
Brian Paulb51b0a82001-03-07 05:06:11 +0000838 if (mask == (GLuint) MASK_IDENTITY) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000839 mat->type = MATRIX_IDENTITY;
840 }
Brian Paulb51b0a82001-03-07 05:06:11 +0000841 else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000842 mat->type = MATRIX_2D_NO_ROT;
Gareth Hughes22144ab2001-03-12 00:48:37 +0000843
Keith Whitwell23caf202000-11-16 21:05:34 +0000844 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
845 mat->flags = MAT_FLAG_GENERAL_SCALE;
846 }
Brian Paulb51b0a82001-03-07 05:06:11 +0000847 else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000848 GLfloat mm = DOT2(m, m);
849 GLfloat m4m4 = DOT2(m+4,m+4);
850 GLfloat mm4 = DOT2(m,m+4);
851
852 mat->type = MATRIX_2D;
853
854 /* Check for scale */
855 if (SQ(mm-1) > SQ(1e-6) ||
Gareth Hughes22144ab2001-03-12 00:48:37 +0000856 SQ(m4m4-1) > SQ(1e-6))
Keith Whitwell23caf202000-11-16 21:05:34 +0000857 mat->flags |= MAT_FLAG_GENERAL_SCALE;
858
859 /* Check for rotation */
860 if (SQ(mm4) > SQ(1e-6))
861 mat->flags |= MAT_FLAG_GENERAL_3D;
862 else
863 mat->flags |= MAT_FLAG_ROTATION;
864
865 }
Brian Paulb51b0a82001-03-07 05:06:11 +0000866 else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000867 mat->type = MATRIX_3D_NO_ROT;
868
869 /* Check for scale */
Gareth Hughes22144ab2001-03-12 00:48:37 +0000870 if (SQ(m[0]-m[5]) < SQ(1e-6) &&
Keith Whitwell23caf202000-11-16 21:05:34 +0000871 SQ(m[0]-m[10]) < SQ(1e-6)) {
872 if (SQ(m[0]-1.0) > SQ(1e-6)) {
873 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
874 }
875 }
876 else {
877 mat->flags |= MAT_FLAG_GENERAL_SCALE;
878 }
879 }
Brian Paulb51b0a82001-03-07 05:06:11 +0000880 else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
Keith Whitwell23caf202000-11-16 21:05:34 +0000881 GLfloat c1 = DOT3(m,m);
882 GLfloat c2 = DOT3(m+4,m+4);
883 GLfloat c3 = DOT3(m+8,m+8);
884 GLfloat d1 = DOT3(m, m+4);
885 GLfloat cp[3];
886
887 mat->type = MATRIX_3D;
888
889 /* Check for scale */
890 if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) {
891 if (SQ(c1-1.0) > SQ(1e-6))
892 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
893 /* else no scale at all */
894 }
895 else {
896 mat->flags |= MAT_FLAG_GENERAL_SCALE;
897 }
898
899 /* Check for rotation */
900 if (SQ(d1) < SQ(1e-6)) {
901 CROSS3( cp, m, m+4 );
902 SUB_3V( cp, cp, (m+8) );
Gareth Hughes22144ab2001-03-12 00:48:37 +0000903 if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
Keith Whitwell23caf202000-11-16 21:05:34 +0000904 mat->flags |= MAT_FLAG_ROTATION;
905 else
906 mat->flags |= MAT_FLAG_GENERAL_3D;
907 }
908 else {
909 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
910 }
911 }
912 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
913 mat->type = MATRIX_PERSPECTIVE;
914 mat->flags |= MAT_FLAG_GENERAL;
915 }
916 else {
917 mat->type = MATRIX_GENERAL;
918 mat->flags |= MAT_FLAG_GENERAL;
919 }
920}
921
922
923/* Analyse a matrix given that its flags are accurate - this is the
Gareth Hughes22144ab2001-03-12 00:48:37 +0000924 * more common operation, hopefully.
Keith Whitwell23caf202000-11-16 21:05:34 +0000925 */
Keith Whitwellad2ac212000-11-24 10:25:05 +0000926static void analyse_from_flags( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +0000927{
928 const GLfloat *m = mat->m;
929
930 if (TEST_MAT_FLAGS(mat, 0)) {
931 mat->type = MATRIX_IDENTITY;
932 }
933 else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
934 MAT_FLAG_UNIFORM_SCALE |
935 MAT_FLAG_GENERAL_SCALE))) {
936 if ( m[10]==1.0F && m[14]==0.0F ) {
937 mat->type = MATRIX_2D_NO_ROT;
938 }
939 else {
940 mat->type = MATRIX_3D_NO_ROT;
941 }
942 }
943 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
Gareth Hughes22144ab2001-03-12 00:48:37 +0000944 if ( m[ 8]==0.0F
Keith Whitwell23caf202000-11-16 21:05:34 +0000945 && m[ 9]==0.0F
946 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
947 mat->type = MATRIX_2D;
948 }
949 else {
950 mat->type = MATRIX_3D;
951 }
952 }
953 else if ( m[4]==0.0F && m[12]==0.0F
954 && m[1]==0.0F && m[13]==0.0F
955 && m[2]==0.0F && m[6]==0.0F
956 && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
957 mat->type = MATRIX_PERSPECTIVE;
958 }
959 else {
960 mat->type = MATRIX_GENERAL;
961 }
962}
963
964
Gareth Hughes22144ab2001-03-12 00:48:37 +0000965void
966_math_matrix_analyse( GLmatrix *mat )
Keith Whitwell23caf202000-11-16 21:05:34 +0000967{
968 if (mat->flags & MAT_DIRTY_TYPE) {
Gareth Hughes22144ab2001-03-12 00:48:37 +0000969 if (mat->flags & MAT_DIRTY_FLAGS)
Keith Whitwellad2ac212000-11-24 10:25:05 +0000970 analyse_from_scratch( mat );
Keith Whitwell23caf202000-11-16 21:05:34 +0000971 else
Keith Whitwellad2ac212000-11-24 10:25:05 +0000972 analyse_from_flags( mat );
Keith Whitwell23caf202000-11-16 21:05:34 +0000973 }
974
975 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
976 matrix_invert( mat );
977 }
978
979 mat->flags &= ~(MAT_DIRTY_FLAGS|
980 MAT_DIRTY_TYPE|
981 MAT_DIRTY_INVERSE);
982}
983
984
Gareth Hughes22144ab2001-03-12 00:48:37 +0000985void
Keith Whitwell23caf202000-11-16 21:05:34 +0000986_math_matrix_copy( GLmatrix *to, const GLmatrix *from )
987{
988 MEMCPY( to->m, from->m, sizeof(Identity) );
989 to->flags = from->flags;
990 to->type = from->type;
991
992 if (to->inv != 0) {
993 if (from->inv == 0) {
994 matrix_invert( to );
995 }
996 else {
997 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
998 }
999 }
1000}
1001
1002
Gareth Hughes22144ab2001-03-12 00:48:37 +00001003void
Keith Whitwell23caf202000-11-16 21:05:34 +00001004_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1005{
1006 GLfloat *m = mat->m;
1007 m[0] *= x; m[4] *= y; m[8] *= z;
1008 m[1] *= x; m[5] *= y; m[9] *= z;
1009 m[2] *= x; m[6] *= y; m[10] *= z;
1010 m[3] *= x; m[7] *= y; m[11] *= z;
1011
1012 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
1013 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1014 else
1015 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1016
Gareth Hughes22144ab2001-03-12 00:48:37 +00001017 mat->flags |= (MAT_DIRTY_TYPE |
Keith Whitwell23caf202000-11-16 21:05:34 +00001018 MAT_DIRTY_INVERSE);
1019}
1020
1021
Gareth Hughes22144ab2001-03-12 00:48:37 +00001022void
Keith Whitwell23caf202000-11-16 21:05:34 +00001023_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1024{
1025 GLfloat *m = mat->m;
1026 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1027 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1028 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1029 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1030
Gareth Hughes22144ab2001-03-12 00:48:37 +00001031 mat->flags |= (MAT_FLAG_TRANSLATION |
1032 MAT_DIRTY_TYPE |
Keith Whitwell23caf202000-11-16 21:05:34 +00001033 MAT_DIRTY_INVERSE);
1034}
1035
1036
Gareth Hughes22144ab2001-03-12 00:48:37 +00001037void
Keith Whitwell23caf202000-11-16 21:05:34 +00001038_math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1039{
1040 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1041 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1042}
1043
Gareth Hughes22144ab2001-03-12 00:48:37 +00001044void
Keith Whitwell23caf202000-11-16 21:05:34 +00001045_math_matrix_ctr( GLmatrix *m )
1046{
Brian Paul30f51ae2001-12-18 04:06:44 +00001047 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1048 if (m->m)
1049 MEMCPY( m->m, Identity, sizeof(Identity) );
1050 m->inv = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001051 m->type = MATRIX_IDENTITY;
1052 m->flags = 0;
1053}
1054
Gareth Hughes22144ab2001-03-12 00:48:37 +00001055void
Keith Whitwell23caf202000-11-16 21:05:34 +00001056_math_matrix_dtr( GLmatrix *m )
1057{
Brian Paul30f51ae2001-12-18 04:06:44 +00001058 if (m->m) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001059 ALIGN_FREE( m->m );
Brian Paul30f51ae2001-12-18 04:06:44 +00001060 m->m = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001061 }
Brian Paul30f51ae2001-12-18 04:06:44 +00001062 if (m->inv) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001063 ALIGN_FREE( m->inv );
Brian Paul30f51ae2001-12-18 04:06:44 +00001064 m->inv = NULL;
Keith Whitwell23caf202000-11-16 21:05:34 +00001065 }
1066}
1067
1068
Gareth Hughes22144ab2001-03-12 00:48:37 +00001069void
Keith Whitwell23caf202000-11-16 21:05:34 +00001070_math_matrix_alloc_inv( GLmatrix *m )
1071{
Brian Paul30f51ae2001-12-18 04:06:44 +00001072 if (!m->inv) {
Keith Whitwell23caf202000-11-16 21:05:34 +00001073 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
Brian Paul30f51ae2001-12-18 04:06:44 +00001074 if (m->inv)
1075 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
Keith Whitwell23caf202000-11-16 21:05:34 +00001076 }
1077}
1078
1079
Gareth Hughes22144ab2001-03-12 00:48:37 +00001080void
Keith Whitwell23caf202000-11-16 21:05:34 +00001081_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
1082{
1083 dest->flags = (a->flags |
1084 b->flags |
Gareth Hughes22144ab2001-03-12 00:48:37 +00001085 MAT_DIRTY_TYPE |
Keith Whitwell23caf202000-11-16 21:05:34 +00001086 MAT_DIRTY_INVERSE);
1087
1088 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
1089 matmul34( dest->m, a->m, b->m );
Gareth Hughes22144ab2001-03-12 00:48:37 +00001090 else
Keith Whitwell23caf202000-11-16 21:05:34 +00001091 matmul4( dest->m, a->m, b->m );
1092}
1093
1094
Gareth Hughes22144ab2001-03-12 00:48:37 +00001095void
Keith Whitwell23caf202000-11-16 21:05:34 +00001096_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
1097{
1098 dest->flags |= (MAT_FLAG_GENERAL |
Gareth Hughes22144ab2001-03-12 00:48:37 +00001099 MAT_DIRTY_TYPE |
Keith Whitwell23caf202000-11-16 21:05:34 +00001100 MAT_DIRTY_INVERSE);
1101
Gareth Hughes22144ab2001-03-12 00:48:37 +00001102 matmul4( dest->m, dest->m, m );
Keith Whitwell23caf202000-11-16 21:05:34 +00001103}
1104
Gareth Hughes22144ab2001-03-12 00:48:37 +00001105void
Keith Whitwell23caf202000-11-16 21:05:34 +00001106_math_matrix_set_identity( GLmatrix *mat )
1107{
1108 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1109
1110 if (mat->inv)
1111 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1112
1113 mat->type = MATRIX_IDENTITY;
1114 mat->flags &= ~(MAT_DIRTY_FLAGS|
1115 MAT_DIRTY_TYPE|
1116 MAT_DIRTY_INVERSE);
1117}
1118
1119
1120
Gareth Hughes22144ab2001-03-12 00:48:37 +00001121void
Keith Whitwell23caf202000-11-16 21:05:34 +00001122_math_transposef( GLfloat to[16], const GLfloat from[16] )
1123{
1124 to[0] = from[0];
1125 to[1] = from[4];
1126 to[2] = from[8];
1127 to[3] = from[12];
1128 to[4] = from[1];
1129 to[5] = from[5];
1130 to[6] = from[9];
1131 to[7] = from[13];
1132 to[8] = from[2];
1133 to[9] = from[6];
1134 to[10] = from[10];
1135 to[11] = from[14];
1136 to[12] = from[3];
1137 to[13] = from[7];
1138 to[14] = from[11];
1139 to[15] = from[15];
1140}
1141
1142
Gareth Hughes22144ab2001-03-12 00:48:37 +00001143void
Keith Whitwell23caf202000-11-16 21:05:34 +00001144_math_transposed( GLdouble to[16], const GLdouble from[16] )
1145{
1146 to[0] = from[0];
1147 to[1] = from[4];
1148 to[2] = from[8];
1149 to[3] = from[12];
1150 to[4] = from[1];
1151 to[5] = from[5];
1152 to[6] = from[9];
1153 to[7] = from[13];
1154 to[8] = from[2];
1155 to[9] = from[6];
1156 to[10] = from[10];
1157 to[11] = from[14];
1158 to[12] = from[3];
1159 to[13] = from[7];
1160 to[14] = from[11];
1161 to[15] = from[15];
1162}
1163
Gareth Hughes22144ab2001-03-12 00:48:37 +00001164void
Keith Whitwell23caf202000-11-16 21:05:34 +00001165_math_transposefd( GLfloat to[16], const GLdouble from[16] )
1166{
Karl Schultz7b9fe822001-09-18 23:06:14 +00001167 to[0] = (GLfloat) from[0];
1168 to[1] = (GLfloat) from[4];
1169 to[2] = (GLfloat) from[8];
1170 to[3] = (GLfloat) from[12];
1171 to[4] = (GLfloat) from[1];
1172 to[5] = (GLfloat) from[5];
1173 to[6] = (GLfloat) from[9];
1174 to[7] = (GLfloat) from[13];
1175 to[8] = (GLfloat) from[2];
1176 to[9] = (GLfloat) from[6];
1177 to[10] = (GLfloat) from[10];
1178 to[11] = (GLfloat) from[14];
1179 to[12] = (GLfloat) from[3];
1180 to[13] = (GLfloat) from[7];
1181 to[14] = (GLfloat) from[11];
1182 to[15] = (GLfloat) from[15];
Keith Whitwell23caf202000-11-16 21:05:34 +00001183}