added aligned memory allocations (Gareth Hughes)
[mesa.git] / src / mesa / main / matrix.c
1 /* $Id: matrix.c,v 1.18 2000/06/27 22:10:00 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 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 *
32 * NOTES:
33 * 1. 4x4 transformation matrices are stored in memory in column major order.
34 * 2. Points/vertices are to be thought of as column vectors.
35 * 3. Transformation of a point p by a matrix M is: p' = M * p
36 *
37 */
38
39
40 #ifdef PC_HEADER
41 #include "all.h"
42 #else
43 #include "glheader.h"
44 #include "buffers.h"
45 #include "context.h"
46 #include "enums.h"
47 #include "matrix.h"
48 #include "mem.h"
49 #include "mmath.h"
50 #include "types.h"
51 #endif
52
53
54 static const char *types[] = {
55 "MATRIX_GENERAL",
56 "MATRIX_IDENTITY",
57 "MATRIX_3D_NO_ROT",
58 "MATRIX_PERSPECTIVE",
59 "MATRIX_2D",
60 "MATRIX_2D_NO_ROT",
61 "MATRIX_3D"
62 };
63 static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b );
64
65
66 static GLfloat Identity[16] = {
67 1.0, 0.0, 0.0, 0.0,
68 0.0, 1.0, 0.0, 0.0,
69 0.0, 0.0, 1.0, 0.0,
70 0.0, 0.0, 0.0, 1.0
71 };
72
73
74 static void print_matrix_floats( const GLfloat m[16] )
75 {
76 int i;
77 for (i=0;i<4;i++) {
78 fprintf(stderr,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
79 }
80 }
81
82 void gl_print_matrix( const GLmatrix *m )
83 {
84 fprintf(stderr, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
85 print_matrix_floats(m->m);
86 #if 1
87 fprintf(stderr, "Inverse: \n");
88 if (m->inv) {
89 GLfloat prod[16];
90 print_matrix_floats(m->inv);
91 matmul4(prod, m->m, m->inv);
92 fprintf(stderr, "Mat * Inverse:\n");
93 print_matrix_floats(prod);
94 } else
95 fprintf(stderr, " - not available\n");
96 #endif
97 }
98
99
100
101 /*
102 * This matmul was contributed by Thomas Malik
103 *
104 * Perform a 4x4 matrix multiplication (product = a x b).
105 * Input: a, b - matrices to multiply
106 * Output: product - product of a and b
107 * WARNING: (product != b) assumed
108 * NOTE: (product == a) allowed
109 *
110 * KW: 4*16 = 64 muls
111 */
112 #define A(row,col) a[(col<<2)+row]
113 #define B(row,col) b[(col<<2)+row]
114 #define P(row,col) product[(col<<2)+row]
115
116 static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
117 {
118 GLint i;
119 for (i = 0; i < 4; i++) {
120 GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
121 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
122 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
123 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
124 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
125 }
126 }
127
128
129
130
131 /* Multiply two matrices known to occupy only the top three rows,
132 * such as typical modelling matrices, and ortho matrices.
133 *
134 * KW: 3*9 = 27 muls
135 */
136 static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
137 {
138 GLint i;
139 for (i = 0; i < 3; i++) {
140 GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
141 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
142 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
143 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
144 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
145 }
146 P(3,0) = 0;
147 P(3,1) = 0;
148 P(3,2) = 0;
149 P(3,3) = 1;
150 }
151
152 static void matmul4fd( GLfloat *product, const GLfloat *a, const GLdouble *b )
153 {
154 GLint i;
155 for (i = 0; i < 4; i++) {
156 GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
157 P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
158 P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
159 P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
160 P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
161 }
162 }
163
164 #undef A
165 #undef B
166 #undef P
167
168
169
170 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
171 #define MAT(m,r,c) (m)[(c)*4+(r)]
172
173 /*
174 * Compute inverse of 4x4 transformation matrix.
175 * Code contributed by Jacques Leroy jle@star.be
176 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
177 */
178 static GLboolean invert_matrix_general( GLmatrix *mat )
179 {
180 const GLfloat *m = mat->m;
181 GLfloat *out = mat->inv;
182 GLfloat wtmp[4][8];
183 GLfloat m0, m1, m2, m3, s;
184 GLfloat *r0, *r1, *r2, *r3;
185
186 r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
187
188 r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
189 r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
190 r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
191
192 r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
193 r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
194 r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
195
196 r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
197 r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
198 r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
199
200 r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
201 r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
202 r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
203
204 /* choose pivot - or die */
205 if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
206 if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
207 if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
208 if (0.0 == r0[0]) return GL_FALSE;
209
210 /* eliminate first variable */
211 m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
212 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
213 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
214 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
215 s = r0[4];
216 if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
217 s = r0[5];
218 if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
219 s = r0[6];
220 if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
221 s = r0[7];
222 if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
223
224 /* choose pivot - or die */
225 if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
226 if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
227 if (0.0 == r1[1]) return GL_FALSE;
228
229 /* eliminate second variable */
230 m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
231 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
232 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
233 s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
234 s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
235 s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
236 s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
237
238 /* choose pivot - or die */
239 if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
240 if (0.0 == r2[2]) return GL_FALSE;
241
242 /* eliminate third variable */
243 m3 = r3[2]/r2[2];
244 r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
245 r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
246 r3[7] -= m3 * r2[7];
247
248 /* last check */
249 if (0.0 == r3[3]) return GL_FALSE;
250
251 s = 1.0/r3[3]; /* now back substitute row 3 */
252 r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
253
254 m2 = r2[3]; /* now back substitute row 2 */
255 s = 1.0/r2[2];
256 r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
257 r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
258 m1 = r1[3];
259 r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
260 r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
261 m0 = r0[3];
262 r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
263 r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
264
265 m1 = r1[2]; /* now back substitute row 1 */
266 s = 1.0/r1[1];
267 r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
268 r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
269 m0 = r0[2];
270 r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
271 r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
272
273 m0 = r0[1]; /* now back substitute row 0 */
274 s = 1.0/r0[0];
275 r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
276 r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
277
278 MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
279 MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
280 MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
281 MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
282 MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
283 MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
284 MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
285 MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
286
287 return GL_TRUE;
288 }
289 #undef SWAP_ROWS
290
291 /* Adapted from graphics gems II.
292 */
293 static GLboolean invert_matrix_3d_general( GLmatrix *mat )
294 {
295 const GLfloat *in = mat->m;
296 GLfloat *out = mat->inv;
297 GLfloat pos, neg, t;
298 GLfloat det;
299
300 /* Calculate the determinant of upper left 3x3 submatrix and
301 * determine if the matrix is singular.
302 */
303 pos = neg = 0.0;
304 t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
305 if (t >= 0.0) pos += t; else neg += t;
306
307 t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
308 if (t >= 0.0) pos += t; else neg += t;
309
310 t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
311 if (t >= 0.0) pos += t; else neg += t;
312
313 t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
314 if (t >= 0.0) pos += t; else neg += t;
315
316 t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
317 if (t >= 0.0) pos += t; else neg += t;
318
319 t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
320 if (t >= 0.0) pos += t; else neg += t;
321
322 det = pos + neg;
323
324 if (det*det < 1e-25)
325 return GL_FALSE;
326
327 det = 1.0 / det;
328 MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
329 MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
330 MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
331 MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
332 MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
333 MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
334 MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
335 MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
336 MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
337
338 /* Do the translation part */
339 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
340 MAT(in,1,3) * MAT(out,0,1) +
341 MAT(in,2,3) * MAT(out,0,2) );
342 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
343 MAT(in,1,3) * MAT(out,1,1) +
344 MAT(in,2,3) * MAT(out,1,2) );
345 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
346 MAT(in,1,3) * MAT(out,2,1) +
347 MAT(in,2,3) * MAT(out,2,2) );
348
349 return GL_TRUE;
350 }
351
352
353 static GLboolean invert_matrix_3d( GLmatrix *mat )
354 {
355 const GLfloat *in = mat->m;
356 GLfloat *out = mat->inv;
357
358 if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING))
359 {
360 return invert_matrix_3d_general( mat );
361 }
362
363 if (mat->flags & MAT_FLAG_UNIFORM_SCALE)
364 {
365 GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
366 MAT(in,0,1) * MAT(in,0,1) +
367 MAT(in,0,2) * MAT(in,0,2));
368
369 if (scale == 0.0)
370 return GL_FALSE;
371
372 scale = 1.0 / scale;
373
374 /* Transpose and scale the 3 by 3 upper-left submatrix. */
375 MAT(out,0,0) = scale * MAT(in,0,0);
376 MAT(out,1,0) = scale * MAT(in,0,1);
377 MAT(out,2,0) = scale * MAT(in,0,2);
378 MAT(out,0,1) = scale * MAT(in,1,0);
379 MAT(out,1,1) = scale * MAT(in,1,1);
380 MAT(out,2,1) = scale * MAT(in,1,2);
381 MAT(out,0,2) = scale * MAT(in,2,0);
382 MAT(out,1,2) = scale * MAT(in,2,1);
383 MAT(out,2,2) = scale * MAT(in,2,2);
384 }
385 else if (mat->flags & MAT_FLAG_ROTATION)
386 {
387 /* Transpose the 3 by 3 upper-left submatrix. */
388 MAT(out,0,0) = MAT(in,0,0);
389 MAT(out,1,0) = MAT(in,0,1);
390 MAT(out,2,0) = MAT(in,0,2);
391 MAT(out,0,1) = MAT(in,1,0);
392 MAT(out,1,1) = MAT(in,1,1);
393 MAT(out,2,1) = MAT(in,1,2);
394 MAT(out,0,2) = MAT(in,2,0);
395 MAT(out,1,2) = MAT(in,2,1);
396 MAT(out,2,2) = MAT(in,2,2);
397 }
398 else /* pure translation */
399 {
400 MEMCPY( out, Identity, sizeof(Identity) );
401 MAT(out,0,3) = - MAT(in,0,3);
402 MAT(out,1,3) = - MAT(in,1,3);
403 MAT(out,2,3) = - MAT(in,2,3);
404 return GL_TRUE;
405 }
406
407 if (mat->flags & MAT_FLAG_TRANSLATION)
408 {
409 /* Do the translation part */
410 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
411 MAT(in,1,3) * MAT(out,0,1) +
412 MAT(in,2,3) * MAT(out,0,2) );
413 MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
414 MAT(in,1,3) * MAT(out,1,1) +
415 MAT(in,2,3) * MAT(out,1,2) );
416 MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
417 MAT(in,1,3) * MAT(out,2,1) +
418 MAT(in,2,3) * MAT(out,2,2) );
419 }
420 else
421 {
422 MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
423 }
424
425 return GL_TRUE;
426 }
427
428
429
430 static GLboolean invert_matrix_identity( GLmatrix *mat )
431 {
432 MEMCPY( mat->inv, Identity, sizeof(Identity) );
433 return GL_TRUE;
434 }
435
436
437 static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
438 {
439 const GLfloat *in = mat->m;
440 GLfloat *out = mat->inv;
441
442 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
443 return GL_FALSE;
444
445 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
446 MAT(out,0,0) = 1.0 / MAT(in,0,0);
447 MAT(out,1,1) = 1.0 / MAT(in,1,1);
448 MAT(out,2,2) = 1.0 / MAT(in,2,2);
449
450 if (mat->flags & MAT_FLAG_TRANSLATION)
451 {
452 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
453 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
454 MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
455 }
456
457 return GL_TRUE;
458 }
459
460
461 static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
462 {
463 const GLfloat *in = mat->m;
464 GLfloat *out = mat->inv;
465
466 if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
467 return GL_FALSE;
468
469 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
470 MAT(out,0,0) = 1.0 / MAT(in,0,0);
471 MAT(out,1,1) = 1.0 / MAT(in,1,1);
472
473 if (mat->flags & MAT_FLAG_TRANSLATION)
474 {
475 MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
476 MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
477 }
478
479 return GL_TRUE;
480 }
481
482
483 static GLboolean invert_matrix_perspective( GLmatrix *mat )
484 {
485 const GLfloat *in = mat->m;
486 GLfloat *out = mat->inv;
487
488 if (MAT(in,2,3) == 0)
489 return GL_FALSE;
490
491 MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
492
493 MAT(out,0,0) = 1.0 / MAT(in,0,0);
494 MAT(out,1,1) = 1.0 / MAT(in,1,1);
495
496 MAT(out,0,3) = MAT(in,0,2);
497 MAT(out,1,3) = MAT(in,1,2);
498
499 MAT(out,2,2) = 0;
500 MAT(out,2,3) = -1;
501
502 MAT(out,3,2) = 1.0 / MAT(in,2,3);
503 MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
504
505 return GL_TRUE;
506 }
507
508
509 typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
510
511 static inv_mat_func inv_mat_tab[7] = {
512 invert_matrix_general,
513 invert_matrix_identity,
514 invert_matrix_3d_no_rot,
515 invert_matrix_perspective,
516 invert_matrix_3d, /* lazy! */
517 invert_matrix_2d_no_rot,
518 invert_matrix_3d
519 };
520
521
522 GLboolean gl_matrix_invert( GLmatrix *mat )
523 {
524 if (inv_mat_tab[mat->type](mat)) {
525 #if 0
526 GLmatrix m; m.inv = 0; m.type = 0; m.flags = 0;
527 matmul4( m.m, mat->m, mat->inv );
528 printf("inverted matrix of type %s:\n", types[mat->type]);
529 gl_print_matrix( mat );
530 gl_print_matrix( &m );
531 #endif
532 return GL_TRUE;
533 } else {
534 MEMCPY( mat->inv, Identity, sizeof(Identity) );
535 return GL_FALSE;
536 }
537 }
538
539
540
541 void gl_matrix_transposef( GLfloat to[16], const GLfloat from[16] )
542 {
543 to[0] = from[0];
544 to[1] = from[4];
545 to[2] = from[8];
546 to[3] = from[12];
547 to[4] = from[1];
548 to[5] = from[5];
549 to[6] = from[9];
550 to[7] = from[13];
551 to[8] = from[2];
552 to[9] = from[6];
553 to[10] = from[10];
554 to[11] = from[14];
555 to[12] = from[3];
556 to[13] = from[7];
557 to[14] = from[11];
558 to[15] = from[15];
559 }
560
561
562
563 void gl_matrix_transposed( GLdouble to[16], const GLdouble from[16] )
564 {
565 to[0] = from[0];
566 to[1] = from[4];
567 to[2] = from[8];
568 to[3] = from[12];
569 to[4] = from[1];
570 to[5] = from[5];
571 to[6] = from[9];
572 to[7] = from[13];
573 to[8] = from[2];
574 to[9] = from[6];
575 to[10] = from[10];
576 to[11] = from[14];
577 to[12] = from[3];
578 to[13] = from[7];
579 to[14] = from[11];
580 to[15] = from[15];
581 }
582
583
584
585 /*
586 * Generate a 4x4 transformation matrix from glRotate parameters.
587 */
588 void gl_rotation_matrix( GLfloat angle, GLfloat x, GLfloat y, GLfloat z,
589 GLfloat m[] )
590 {
591 /* This function contributed by Erich Boleyn (erich@uruk.org) */
592 GLfloat mag, s, c;
593 GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
594
595 s = sin( angle * DEG2RAD );
596 c = cos( angle * DEG2RAD );
597
598 mag = GL_SQRT( x*x + y*y + z*z );
599
600 if (mag == 0.0) {
601 /* generate an identity matrix and return */
602 MEMCPY(m, Identity, sizeof(GLfloat)*16);
603 return;
604 }
605
606 x /= mag;
607 y /= mag;
608 z /= mag;
609
610 #define M(row,col) m[col*4+row]
611
612 /*
613 * Arbitrary axis rotation matrix.
614 *
615 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
616 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
617 * (which is about the X-axis), and the two composite transforms
618 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
619 * from the arbitrary axis to the X-axis then back. They are
620 * all elementary rotations.
621 *
622 * Rz' is a rotation about the Z-axis, to bring the axis vector
623 * into the x-z plane. Then Ry' is applied, rotating about the
624 * Y-axis to bring the axis vector parallel with the X-axis. The
625 * rotation about the X-axis is then performed. Ry and Rz are
626 * simply the respective inverse transforms to bring the arbitrary
627 * axis back to it's original orientation. The first transforms
628 * Rz' and Ry' are considered inverses, since the data from the
629 * arbitrary axis gives you info on how to get to it, not how
630 * to get away from it, and an inverse must be applied.
631 *
632 * The basic calculation used is to recognize that the arbitrary
633 * axis vector (x, y, z), since it is of unit length, actually
634 * represents the sines and cosines of the angles to rotate the
635 * X-axis to the same orientation, with theta being the angle about
636 * Z and phi the angle about Y (in the order described above)
637 * as follows:
638 *
639 * cos ( theta ) = x / sqrt ( 1 - z^2 )
640 * sin ( theta ) = y / sqrt ( 1 - z^2 )
641 *
642 * cos ( phi ) = sqrt ( 1 - z^2 )
643 * sin ( phi ) = z
644 *
645 * Note that cos ( phi ) can further be inserted to the above
646 * formulas:
647 *
648 * cos ( theta ) = x / cos ( phi )
649 * sin ( theta ) = y / sin ( phi )
650 *
651 * ...etc. Because of those relations and the standard trigonometric
652 * relations, it is pssible to reduce the transforms down to what
653 * is used below. It may be that any primary axis chosen will give the
654 * same results (modulo a sign convention) using thie method.
655 *
656 * Particularly nice is to notice that all divisions that might
657 * have caused trouble when parallel to certain planes or
658 * axis go away with care paid to reducing the expressions.
659 * After checking, it does perform correctly under all cases, since
660 * in all the cases of division where the denominator would have
661 * been zero, the numerator would have been zero as well, giving
662 * the expected result.
663 */
664
665 xx = x * x;
666 yy = y * y;
667 zz = z * z;
668 xy = x * y;
669 yz = y * z;
670 zx = z * x;
671 xs = x * s;
672 ys = y * s;
673 zs = z * s;
674 one_c = 1.0F - c;
675
676 M(0,0) = (one_c * xx) + c;
677 M(0,1) = (one_c * xy) - zs;
678 M(0,2) = (one_c * zx) + ys;
679 M(0,3) = 0.0F;
680
681 M(1,0) = (one_c * xy) + zs;
682 M(1,1) = (one_c * yy) + c;
683 M(1,2) = (one_c * yz) - xs;
684 M(1,3) = 0.0F;
685
686 M(2,0) = (one_c * zx) - ys;
687 M(2,1) = (one_c * yz) + xs;
688 M(2,2) = (one_c * zz) + c;
689 M(2,3) = 0.0F;
690
691 M(3,0) = 0.0F;
692 M(3,1) = 0.0F;
693 M(3,2) = 0.0F;
694 M(3,3) = 1.0F;
695
696 #undef M
697 }
698
699 #define ZERO(x) (1<<x)
700 #define ONE(x) (1<<(x+16))
701
702 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
703 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
704
705 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
706 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
707 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
708 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
709
710 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
711 ZERO(1) | ZERO(9) | \
712 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
713 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
714
715 #define MASK_2D ( ZERO(8) | \
716 ZERO(9) | \
717 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
718 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
719
720
721 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
722 ZERO(1) | ZERO(9) | \
723 ZERO(2) | ZERO(6) | \
724 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
725
726 #define MASK_3D ( \
727 \
728 \
729 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
730
731
732 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
733 ZERO(1) | ZERO(13) |\
734 ZERO(2) | ZERO(6) | \
735 ZERO(3) | ZERO(7) | ZERO(15) )
736
737 #define SQ(x) ((x)*(x))
738
739 /* Determine type and flags from scratch. This is expensive enough to
740 * only want to do it once.
741 */
742 static void analyze_from_scratch( GLmatrix *mat )
743 {
744 const GLfloat *m = mat->m;
745 GLuint mask = 0;
746 GLuint i;
747
748 for (i = 0 ; i < 16 ; i++)
749 {
750 if (m[i] == 0.0) mask |= (1<<i);
751 }
752
753 if (m[0] == 1.0F) mask |= (1<<16);
754 if (m[5] == 1.0F) mask |= (1<<21);
755 if (m[10] == 1.0F) mask |= (1<<26);
756 if (m[15] == 1.0F) mask |= (1<<31);
757
758 mat->flags &= ~MAT_FLAGS_GEOMETRY;
759
760 /* Check for translation - no-one really cares
761 */
762 if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
763 mat->flags |= MAT_FLAG_TRANSLATION;
764
765 /* Do the real work
766 */
767 if (mask == MASK_IDENTITY) {
768 mat->type = MATRIX_IDENTITY;
769 }
770 else if ((mask & MASK_2D_NO_ROT) == MASK_2D_NO_ROT)
771 {
772 mat->type = MATRIX_2D_NO_ROT;
773
774 if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
775 mat->flags = MAT_FLAG_GENERAL_SCALE;
776 }
777 else if ((mask & MASK_2D) == MASK_2D)
778 {
779 GLfloat mm = DOT2(m, m);
780 GLfloat m4m4 = DOT2(m+4,m+4);
781 GLfloat mm4 = DOT2(m,m+4);
782
783 mat->type = MATRIX_2D;
784
785 /* Check for scale */
786 if (SQ(mm-1) > SQ(1e-6) ||
787 SQ(m4m4-1) > SQ(1e-6))
788 mat->flags |= MAT_FLAG_GENERAL_SCALE;
789
790 /* Check for rotation */
791 if (SQ(mm4) > SQ(1e-6))
792 mat->flags |= MAT_FLAG_GENERAL_3D;
793 else
794 mat->flags |= MAT_FLAG_ROTATION;
795
796 }
797 else if ((mask & MASK_3D_NO_ROT) == MASK_3D_NO_ROT)
798 {
799 mat->type = MATRIX_3D_NO_ROT;
800
801 /* Check for scale */
802 if (SQ(m[0]-m[5]) < SQ(1e-6) &&
803 SQ(m[0]-m[10]) < SQ(1e-6)) {
804 if (SQ(m[0]-1.0) > SQ(1e-6))
805 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
806 } else
807 mat->flags |= MAT_FLAG_GENERAL_SCALE;
808 }
809 else if ((mask & MASK_3D) == MASK_3D)
810 {
811 GLfloat c1 = DOT3(m,m);
812 GLfloat c2 = DOT3(m+4,m+4);
813 GLfloat c3 = DOT3(m+8,m+8);
814 GLfloat d1 = DOT3(m, m+4);
815 GLfloat cp[3];
816
817 mat->type = MATRIX_3D;
818
819 /* Check for scale */
820 if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) {
821 if (SQ(c1-1.0) > SQ(1e-6))
822 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
823 /* else no scale at all */
824 } else
825 mat->flags |= MAT_FLAG_GENERAL_SCALE;
826
827 /* Check for rotation */
828 if (SQ(d1) < SQ(1e-6)) {
829 CROSS3( cp, m, m+4 );
830 SUB_3V( cp, cp, (m+8) );
831 if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
832 mat->flags |= MAT_FLAG_ROTATION;
833 else
834 mat->flags |= MAT_FLAG_GENERAL_3D;
835 }
836 else
837 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
838 }
839 else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F)
840 {
841 mat->type = MATRIX_PERSPECTIVE;
842 mat->flags |= MAT_FLAG_GENERAL;
843 }
844 else {
845 mat->type = MATRIX_GENERAL;
846 mat->flags |= MAT_FLAG_GENERAL;
847 }
848 }
849
850
851 /* Analyse a matrix given that its flags are accurate - this is the
852 * more common operation, hopefully.
853 */
854 static void analyze_from_flags( GLmatrix *mat )
855 {
856 const GLfloat *m = mat->m;
857
858 if (TEST_MAT_FLAGS(mat, 0)) {
859 mat->type = MATRIX_IDENTITY;
860 }
861 else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
862 MAT_FLAG_UNIFORM_SCALE |
863 MAT_FLAG_GENERAL_SCALE)))
864 {
865 if ( m[10]==1.0F && m[14]==0.0F ) {
866 mat->type = MATRIX_2D_NO_ROT;
867 }
868 else {
869 mat->type = MATRIX_3D_NO_ROT;
870 }
871 }
872 else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
873 if ( m[ 8]==0.0F
874 && m[ 9]==0.0F
875 && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F)
876 {
877 mat->type = MATRIX_2D;
878 }
879 else
880 {
881 mat->type = MATRIX_3D;
882 }
883 }
884 else if ( m[4]==0.0F && m[12]==0.0F
885 && m[1]==0.0F && m[13]==0.0F
886 && m[2]==0.0F && m[6]==0.0F
887 && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F)
888 {
889 mat->type = MATRIX_PERSPECTIVE;
890 }
891 else {
892 mat->type = MATRIX_GENERAL;
893 }
894
895 }
896
897
898 void gl_matrix_analyze( GLmatrix *mat )
899 {
900 if (mat->flags & MAT_DIRTY_TYPE) {
901 if (mat->flags & MAT_DIRTY_FLAGS)
902 analyze_from_scratch( mat );
903 else
904 analyze_from_flags( mat );
905 }
906
907 if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
908 gl_matrix_invert( mat );
909 }
910
911 mat->flags &= ~(MAT_DIRTY_FLAGS|
912 MAT_DIRTY_TYPE|
913 MAT_DIRTY_INVERSE);
914 }
915
916
917 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
918 do { \
919 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where); \
920 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
921 switch (ctx->Transform.MatrixMode) { \
922 case GL_MODELVIEW: \
923 mat = &ctx->ModelView; \
924 flags |= NEW_MODELVIEW; \
925 break; \
926 case GL_PROJECTION: \
927 mat = &ctx->ProjectionMatrix; \
928 flags |= NEW_PROJECTION; \
929 break; \
930 case GL_TEXTURE: \
931 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
932 flags |= NEW_TEXTURE_MATRIX; \
933 break; \
934 case GL_COLOR: \
935 mat = &ctx->ColorMatrix; \
936 flags |= NEW_COLOR_MATRIX; \
937 break; \
938 default: \
939 gl_problem(ctx, where); \
940 } \
941 } while (0)
942
943
944 void
945 _mesa_Frustum( GLdouble left, GLdouble right,
946 GLdouble bottom, GLdouble top,
947 GLdouble nearval, GLdouble farval )
948 {
949 GET_CURRENT_CONTEXT(ctx);
950 GLfloat x, y, a, b, c, d;
951 GLfloat m[16];
952 GLmatrix *mat = 0;
953
954 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glFrustrum" );
955
956 if ((nearval<=0.0 || farval<=0.0) || (nearval == farval) || (left == right) || (top == bottom)) {
957 gl_error( ctx, GL_INVALID_VALUE, "glFrustum(near or far)" );
958 return;
959 }
960
961 x = (2.0*nearval) / (right-left);
962 y = (2.0*nearval) / (top-bottom);
963 a = (right+left) / (right-left);
964 b = (top+bottom) / (top-bottom);
965 c = -(farval+nearval) / ( farval-nearval);
966 d = -(2.0*farval*nearval) / (farval-nearval); /* error? */
967
968 #define M(row,col) m[col*4+row]
969 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
970 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
971 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
972 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
973 #undef M
974
975
976 gl_mat_mul_floats( mat, m, MAT_FLAG_PERSPECTIVE );
977
978
979 if (ctx->Transform.MatrixMode == GL_PROJECTION)
980 {
981 /* Need to keep a stack of near/far values in case the user push/pops
982 * the projection matrix stack so that we can call Driver.NearFar()
983 * after a pop.
984 */
985 ctx->NearFarStack[ctx->ProjectionStackDepth][0] = nearval;
986 ctx->NearFarStack[ctx->ProjectionStackDepth][1] = farval;
987
988 if (ctx->Driver.NearFar) {
989 (*ctx->Driver.NearFar)( ctx, nearval, farval );
990 }
991 }
992 }
993
994
995 void
996 _mesa_Ortho( GLdouble left, GLdouble right,
997 GLdouble bottom, GLdouble top,
998 GLdouble nearval, GLdouble farval )
999 {
1000 GET_CURRENT_CONTEXT(ctx);
1001 GLfloat x, y, z;
1002 GLfloat tx, ty, tz;
1003 GLfloat m[16];
1004 GLmatrix *mat = 0;
1005
1006 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glOrtho" );
1007
1008 if ((left == right) || (bottom == top) || (nearval == farval)) {
1009 gl_error( ctx, GL_INVALID_VALUE, "gl_Ortho((l = r) or (b = top) or (n=f)" );
1010 return;
1011 }
1012
1013 x = 2.0 / (right-left);
1014 y = 2.0 / (top-bottom);
1015 z = -2.0 / (farval-nearval);
1016 tx = -(right+left) / (right-left);
1017 ty = -(top+bottom) / (top-bottom);
1018 tz = -(farval+nearval) / (farval-nearval);
1019
1020 #define M(row,col) m[col*4+row]
1021 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = 0.0F; M(0,3) = tx;
1022 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = 0.0F; M(1,3) = ty;
1023 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = z; M(2,3) = tz;
1024 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = 0.0F; M(3,3) = 1.0F;
1025 #undef M
1026
1027 gl_mat_mul_floats( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
1028
1029 if (ctx->Driver.NearFar) {
1030 (*ctx->Driver.NearFar)( ctx, nearval, farval );
1031 }
1032 }
1033
1034
1035 void
1036 _mesa_MatrixMode( GLenum mode )
1037 {
1038 GET_CURRENT_CONTEXT(ctx);
1039 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glMatrixMode");
1040 switch (mode) {
1041 case GL_MODELVIEW:
1042 case GL_PROJECTION:
1043 case GL_TEXTURE:
1044 case GL_COLOR:
1045 ctx->Transform.MatrixMode = mode;
1046 break;
1047 default:
1048 gl_error( ctx, GL_INVALID_ENUM, "glMatrixMode" );
1049 }
1050 }
1051
1052
1053
1054 void
1055 _mesa_PushMatrix( void )
1056 {
1057 GET_CURRENT_CONTEXT(ctx);
1058 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushMatrix");
1059
1060 if (MESA_VERBOSE&VERBOSE_API)
1061 fprintf(stderr, "glPushMatrix %s\n",
1062 gl_lookup_enum_by_nr(ctx->Transform.MatrixMode));
1063
1064 switch (ctx->Transform.MatrixMode) {
1065 case GL_MODELVIEW:
1066 if (ctx->ModelViewStackDepth >= MAX_MODELVIEW_STACK_DEPTH - 1) {
1067 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1068 return;
1069 }
1070 gl_matrix_copy( &ctx->ModelViewStack[ctx->ModelViewStackDepth++],
1071 &ctx->ModelView );
1072 break;
1073 case GL_PROJECTION:
1074 if (ctx->ProjectionStackDepth >= MAX_PROJECTION_STACK_DEPTH - 1) {
1075 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1076 return;
1077 }
1078 gl_matrix_copy( &ctx->ProjectionStack[ctx->ProjectionStackDepth++],
1079 &ctx->ProjectionMatrix );
1080
1081 /* Save near and far projection values */
1082 ctx->NearFarStack[ctx->ProjectionStackDepth][0]
1083 = ctx->NearFarStack[ctx->ProjectionStackDepth-1][0];
1084 ctx->NearFarStack[ctx->ProjectionStackDepth][1]
1085 = ctx->NearFarStack[ctx->ProjectionStackDepth-1][1];
1086 break;
1087 case GL_TEXTURE:
1088 {
1089 GLuint t = ctx->Texture.CurrentTransformUnit;
1090 if (ctx->TextureStackDepth[t] >= MAX_TEXTURE_STACK_DEPTH - 1) {
1091 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1092 return;
1093 }
1094 gl_matrix_copy( &ctx->TextureStack[t][ctx->TextureStackDepth[t]++],
1095 &ctx->TextureMatrix[t] );
1096 }
1097 break;
1098 case GL_COLOR:
1099 if (ctx->ColorStackDepth >= MAX_COLOR_STACK_DEPTH - 1) {
1100 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1101 return;
1102 }
1103 gl_matrix_copy( &ctx->ColorStack[ctx->ColorStackDepth++],
1104 &ctx->ColorMatrix );
1105 break;
1106 default:
1107 gl_problem(ctx, "Bad matrix mode in gl_PushMatrix");
1108 }
1109 }
1110
1111
1112
1113 void
1114 _mesa_PopMatrix( void )
1115 {
1116 GET_CURRENT_CONTEXT(ctx);
1117 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopMatrix");
1118
1119 if (MESA_VERBOSE&VERBOSE_API)
1120 fprintf(stderr, "glPopMatrix %s\n",
1121 gl_lookup_enum_by_nr(ctx->Transform.MatrixMode));
1122
1123 switch (ctx->Transform.MatrixMode) {
1124 case GL_MODELVIEW:
1125 if (ctx->ModelViewStackDepth==0) {
1126 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1127 return;
1128 }
1129 gl_matrix_copy( &ctx->ModelView,
1130 &ctx->ModelViewStack[--ctx->ModelViewStackDepth] );
1131 ctx->NewState |= NEW_MODELVIEW;
1132 break;
1133 case GL_PROJECTION:
1134 if (ctx->ProjectionStackDepth==0) {
1135 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1136 return;
1137 }
1138
1139 gl_matrix_copy( &ctx->ProjectionMatrix,
1140 &ctx->ProjectionStack[--ctx->ProjectionStackDepth] );
1141 ctx->NewState |= NEW_PROJECTION;
1142
1143 /* Device driver near/far values */
1144 {
1145 GLfloat nearVal = ctx->NearFarStack[ctx->ProjectionStackDepth][0];
1146 GLfloat farVal = ctx->NearFarStack[ctx->ProjectionStackDepth][1];
1147 if (ctx->Driver.NearFar) {
1148 (*ctx->Driver.NearFar)( ctx, nearVal, farVal );
1149 }
1150 }
1151 break;
1152 case GL_TEXTURE:
1153 {
1154 GLuint t = ctx->Texture.CurrentTransformUnit;
1155 if (ctx->TextureStackDepth[t]==0) {
1156 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1157 return;
1158 }
1159 gl_matrix_copy(&ctx->TextureMatrix[t],
1160 &ctx->TextureStack[t][--ctx->TextureStackDepth[t]]);
1161 }
1162 break;
1163 case GL_COLOR:
1164 if (ctx->ColorStackDepth==0) {
1165 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1166 return;
1167 }
1168 gl_matrix_copy(&ctx->ColorMatrix,
1169 &ctx->ColorStack[--ctx->ColorStackDepth]);
1170 break;
1171 default:
1172 gl_problem(ctx, "Bad matrix mode in gl_PopMatrix");
1173 }
1174 }
1175
1176
1177
1178 void
1179 _mesa_LoadIdentity( void )
1180 {
1181 GET_CURRENT_CONTEXT(ctx);
1182 GLmatrix *mat = 0;
1183 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadIdentity");
1184
1185 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1186
1187 if (mat->inv)
1188 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1189
1190 mat->type = MATRIX_IDENTITY;
1191
1192 /* Have to set this to dirty to make sure we recalculate the
1193 * combined matrix later. The update_matrix in this case is a
1194 * shortcircuit anyway...
1195 */
1196 mat->flags = MAT_DIRTY_DEPENDENTS;
1197 }
1198
1199
1200 void
1201 _mesa_LoadMatrixf( const GLfloat *m )
1202 {
1203 GET_CURRENT_CONTEXT(ctx);
1204 GLmatrix *mat = 0;
1205 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadMatrix");
1206
1207 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1208 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1209
1210 if (ctx->Transform.MatrixMode == GL_PROJECTION) {
1211
1212 #define M(row,col) m[col*4+row]
1213 GLfloat c = M(2,2);
1214 GLfloat d = M(2,3);
1215 #undef M
1216 GLfloat n = (c == 1.0 ? 0.0 : d / (c - 1.0));
1217 GLfloat f = (c == -1.0 ? 1.0 : d / (c + 1.0));
1218
1219 /* Need to keep a stack of near/far values in case the user
1220 * push/pops the projection matrix stack so that we can call
1221 * Driver.NearFar() after a pop.
1222 */
1223 ctx->NearFarStack[ctx->ProjectionStackDepth][0] = n;
1224 ctx->NearFarStack[ctx->ProjectionStackDepth][1] = f;
1225
1226 if (ctx->Driver.NearFar) {
1227 (*ctx->Driver.NearFar)( ctx, n, f );
1228 }
1229 }
1230 }
1231
1232
1233 void
1234 _mesa_LoadMatrixd( const GLdouble *m )
1235 {
1236 GLfloat f[16];
1237 GLint i;
1238 for (i = 0; i < 16; i++)
1239 f[i] = m[i];
1240 _mesa_LoadMatrixf(f);
1241 }
1242
1243
1244
1245 /*
1246 * Multiply the active matrix by an arbitary matrix.
1247 */
1248 void
1249 _mesa_MultMatrixf( const GLfloat *m )
1250 {
1251 GET_CURRENT_CONTEXT(ctx);
1252 GLmatrix *mat = 0;
1253 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glMultMatrix" );
1254 matmul4( mat->m, mat->m, m );
1255 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1256 }
1257
1258
1259 /*
1260 * Multiply the active matrix by an arbitary matrix.
1261 */
1262 void
1263 _mesa_MultMatrixd( const GLdouble *m )
1264 {
1265 GET_CURRENT_CONTEXT(ctx);
1266 GLmatrix *mat = 0;
1267 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glMultMatrix" );
1268 matmul4fd( mat->m, mat->m, m );
1269 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1270 }
1271
1272
1273
1274
1275 /*
1276 * Multiply a matrix by an array of floats with known properties.
1277 */
1278 void gl_mat_mul_floats( GLmatrix *mat, const GLfloat *m, GLuint flags )
1279 {
1280 mat->flags |= (flags |
1281 MAT_DIRTY_TYPE |
1282 MAT_DIRTY_INVERSE |
1283 MAT_DIRTY_DEPENDENTS);
1284
1285 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
1286 matmul34( mat->m, mat->m, m );
1287 else
1288 matmul4( mat->m, mat->m, m );
1289
1290 }
1291
1292 /*
1293 * Multiply a matrix by an array of floats with known properties.
1294 */
1295 void gl_mat_mul_mat( GLmatrix *mat, const GLmatrix *m )
1296 {
1297 mat->flags |= (m->flags |
1298 MAT_DIRTY_TYPE |
1299 MAT_DIRTY_INVERSE |
1300 MAT_DIRTY_DEPENDENTS);
1301
1302 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
1303 matmul34( mat->m, mat->m, m->m );
1304 else
1305 matmul4( mat->m, mat->m, m->m );
1306 }
1307
1308
1309
1310 /*
1311 * Execute a glRotate call
1312 */
1313 void
1314 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
1315 {
1316 GET_CURRENT_CONTEXT(ctx);
1317 GLfloat m[16];
1318 if (angle != 0.0F) {
1319 GLmatrix *mat = 0;
1320 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glRotate" );
1321
1322 gl_rotation_matrix( angle, x, y, z, m );
1323 gl_mat_mul_floats( mat, m, MAT_FLAG_ROTATION );
1324 }
1325 }
1326
1327 void
1328 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
1329 {
1330 _mesa_Rotatef(angle, x, y, z);
1331 }
1332
1333
1334 /*
1335 * Execute a glScale call
1336 */
1337 void
1338 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
1339 {
1340 GET_CURRENT_CONTEXT(ctx);
1341 GLmatrix *mat = 0;
1342 GLfloat *m;
1343 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glScale");
1344
1345 m = mat->m;
1346 m[0] *= x; m[4] *= y; m[8] *= z;
1347 m[1] *= x; m[5] *= y; m[9] *= z;
1348 m[2] *= x; m[6] *= y; m[10] *= z;
1349 m[3] *= x; m[7] *= y; m[11] *= z;
1350
1351 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
1352 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1353 else
1354 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1355
1356 mat->flags |= (MAT_DIRTY_TYPE |
1357 MAT_DIRTY_INVERSE |
1358 MAT_DIRTY_DEPENDENTS);
1359 }
1360
1361
1362 void
1363 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
1364 {
1365 _mesa_Scalef(x, y, z);
1366 }
1367
1368
1369 /*
1370 * Execute a glTranslate call
1371 */
1372 void
1373 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
1374 {
1375 GET_CURRENT_CONTEXT(ctx);
1376 GLmatrix *mat = 0;
1377 GLfloat *m;
1378 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glTranslate");
1379 m = mat->m;
1380 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1381 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1382 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1383 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1384
1385 mat->flags |= (MAT_FLAG_TRANSLATION |
1386 MAT_DIRTY_TYPE |
1387 MAT_DIRTY_INVERSE |
1388 MAT_DIRTY_DEPENDENTS);
1389 }
1390
1391
1392 void
1393 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
1394 {
1395 _mesa_Translatef(x, y, z);
1396 }
1397
1398
1399
1400 void
1401 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
1402 {
1403 GLfloat tm[16];
1404 gl_matrix_transposef(tm, m);
1405 _mesa_LoadMatrixf(tm);
1406 }
1407
1408
1409 void
1410 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
1411 {
1412 GLdouble tm[16];
1413 gl_matrix_transposed(tm, m);
1414 _mesa_LoadMatrixd(tm);
1415 }
1416
1417
1418 void
1419 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
1420 {
1421 GLfloat tm[16];
1422 gl_matrix_transposef(tm, m);
1423 _mesa_MultMatrixf(tm);
1424 }
1425
1426
1427 void
1428 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
1429 {
1430 GLdouble tm[16];
1431 gl_matrix_transposed(tm, m);
1432 _mesa_MultMatrixd(tm);
1433 }
1434
1435
1436 /*
1437 * Called via glViewport or display list execution.
1438 */
1439 void
1440 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
1441 {
1442 GET_CURRENT_CONTEXT(ctx);
1443 gl_Viewport(ctx, x, y, width, height);
1444 }
1445
1446
1447
1448 /*
1449 * Define a new viewport and reallocate auxillary buffers if the size of
1450 * the window (color buffer) has changed.
1451 *
1452 * XXX This is directly called by device drivers, BUT this function
1453 * may be renamed _mesa_Viewport (without ctx arg) in the future so
1454 * use of _mesa_Viewport is encouraged.
1455 */
1456 void
1457 gl_Viewport( GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height )
1458 {
1459 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glViewport");
1460
1461 if (width<0 || height<0) {
1462 gl_error( ctx, GL_INVALID_VALUE, "glViewport" );
1463 return;
1464 }
1465
1466 if (MESA_VERBOSE & VERBOSE_API)
1467 fprintf(stderr, "glViewport %d %d %d %d\n", x, y, width, height);
1468
1469 /* clamp width, and height to implementation dependent range */
1470 width = CLAMP( width, 1, MAX_WIDTH );
1471 height = CLAMP( height, 1, MAX_HEIGHT );
1472
1473 /* Save viewport */
1474 ctx->Viewport.X = x;
1475 ctx->Viewport.Width = width;
1476 ctx->Viewport.Y = y;
1477 ctx->Viewport.Height = height;
1478
1479 /* compute scale and bias values */
1480 ctx->Viewport.WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
1481 ctx->Viewport.WindowMap.m[MAT_TX] = ctx->Viewport.WindowMap.m[MAT_SX] + x;
1482 ctx->Viewport.WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
1483 ctx->Viewport.WindowMap.m[MAT_TY] = ctx->Viewport.WindowMap.m[MAT_SY] + y;
1484 ctx->Viewport.WindowMap.m[MAT_SZ] = 0.5 * ctx->Visual->DepthMaxF;
1485 ctx->Viewport.WindowMap.m[MAT_TZ] = 0.5 * ctx->Visual->DepthMaxF;
1486
1487 ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
1488 ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
1489
1490 ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
1491 ctx->NewState |= NEW_VIEWPORT;
1492
1493 /* Check if window/buffer has been resized and if so, reallocate the
1494 * ancillary buffers.
1495 */
1496 _mesa_ResizeBuffersMESA();
1497
1498
1499 ctx->RasterMask &= ~WINCLIP_BIT;
1500
1501 if ( ctx->Viewport.X<0
1502 || ctx->Viewport.X + ctx->Viewport.Width > ctx->DrawBuffer->Width
1503 || ctx->Viewport.Y<0
1504 || ctx->Viewport.Y + ctx->Viewport.Height > ctx->DrawBuffer->Height) {
1505 ctx->RasterMask |= WINCLIP_BIT;
1506 }
1507
1508
1509 if (ctx->Driver.Viewport) {
1510 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
1511 }
1512 }
1513
1514
1515
1516 void
1517 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
1518 {
1519 /*
1520 * nearval - specifies mapping of the near clipping plane to window
1521 * coordinates, default is 0
1522 * farval - specifies mapping of the far clipping plane to window
1523 * coordinates, default is 1
1524 *
1525 * After clipping and div by w, z coords are in -1.0 to 1.0,
1526 * corresponding to near and far clipping planes. glDepthRange
1527 * specifies a linear mapping of the normalized z coords in
1528 * this range to window z coords.
1529 */
1530 GLfloat n, f;
1531 GET_CURRENT_CONTEXT(ctx);
1532 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthRange");
1533
1534 if (MESA_VERBOSE&VERBOSE_API)
1535 fprintf(stderr, "glDepthRange %f %f\n", nearval, farval);
1536
1537 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
1538 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
1539
1540 ctx->Viewport.Near = n;
1541 ctx->Viewport.Far = f;
1542 ctx->Viewport.WindowMap.m[MAT_SZ] = ctx->Visual->DepthMaxF * ((f - n) / 2.0);
1543 ctx->Viewport.WindowMap.m[MAT_TZ] = ctx->Visual->DepthMaxF * ((f - n) / 2.0 + n);
1544
1545 ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
1546
1547 if (ctx->Driver.DepthRange) {
1548 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
1549 }
1550 }
1551
1552
1553 void gl_calculate_model_project_matrix( GLcontext *ctx )
1554 {
1555 gl_matrix_mul( &ctx->ModelProjectMatrix,
1556 &ctx->ProjectionMatrix,
1557 &ctx->ModelView );
1558
1559 gl_matrix_analyze( &ctx->ModelProjectMatrix );
1560 }
1561
1562
1563 void gl_matrix_ctr( GLmatrix *m )
1564 {
1565 if ( m->m == 0 ) {
1566 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1567 }
1568 MEMCPY( m->m, Identity, sizeof(Identity) );
1569 m->inv = 0;
1570 m->type = MATRIX_IDENTITY;
1571 m->flags = MAT_DIRTY_DEPENDENTS;
1572 }
1573
1574 void gl_matrix_dtr( GLmatrix *m )
1575 {
1576 if ( m->m != 0 ) {
1577 ALIGN_FREE( m->m );
1578 m->m = 0;
1579 }
1580 if ( m->inv != 0 ) {
1581 ALIGN_FREE( m->inv );
1582 m->inv = 0;
1583 }
1584 }
1585
1586 #if 0
1587 void gl_matrix_set_identity( GLmatrix *m )
1588 {
1589 MEMCPY( m->m, Identity, sizeof(Identity) );
1590 m->type = MATRIX_IDENTITY;
1591 m->flags = MAT_DIRTY_DEPENDENTS;
1592 }
1593 #endif
1594
1595 void gl_matrix_alloc_inv( GLmatrix *m )
1596 {
1597 if ( m->inv == 0 ) {
1598 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
1599 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
1600 }
1601 }
1602
1603 void gl_matrix_copy( GLmatrix *to, const GLmatrix *from )
1604 {
1605 MEMCPY( to->m, from->m, sizeof(Identity) );
1606 to->flags = from->flags | MAT_DIRTY_DEPENDENTS;
1607 to->type = from->type;
1608
1609 if (to->inv != 0) {
1610 if (from->inv == 0) {
1611 gl_matrix_invert( to );
1612 } else {
1613 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
1614 }
1615 }
1616 }
1617
1618 void gl_matrix_mul( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
1619 {
1620 dest->flags = (a->flags |
1621 b->flags |
1622 MAT_DIRTY_TYPE |
1623 MAT_DIRTY_INVERSE |
1624 MAT_DIRTY_DEPENDENTS);
1625
1626 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
1627 matmul34( dest->m, a->m, b->m );
1628 else
1629 matmul4( dest->m, a->m, b->m );
1630 }