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