- Changes for new software rasterizer modules
[mesa.git] / src / mesa / main / matrix.c
1 /* $Id: matrix.c,v 1.24 2000/11/05 18:40:58 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_calculate_model_project_matrix( GLcontext *ctx )
934 {
935 gl_matrix_mul( &ctx->_ModelProjectMatrix,
936 &ctx->ProjectionMatrix,
937 &ctx->ModelView );
938
939 gl_matrix_analyze( &ctx->_ModelProjectMatrix );
940 }
941
942
943 void gl_matrix_ctr( GLmatrix *m )
944 {
945 if ( m->m == 0 ) {
946 m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
947 }
948 MEMCPY( m->m, Identity, sizeof(Identity) );
949 m->inv = 0;
950 m->type = MATRIX_IDENTITY;
951 m->flags = MAT_DIRTY_DEPENDENTS;
952 }
953
954 void gl_matrix_dtr( GLmatrix *m )
955 {
956 if ( m->m != 0 ) {
957 ALIGN_FREE( m->m );
958 m->m = 0;
959 }
960 if ( m->inv != 0 ) {
961 ALIGN_FREE( m->inv );
962 m->inv = 0;
963 }
964 }
965
966
967 void gl_matrix_alloc_inv( GLmatrix *m )
968 {
969 if ( m->inv == 0 ) {
970 m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
971 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
972 }
973 }
974
975
976 void gl_matrix_mul( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
977 {
978 dest->flags = (a->flags |
979 b->flags |
980 MAT_DIRTY_TYPE |
981 MAT_DIRTY_INVERSE |
982 MAT_DIRTY_DEPENDENTS);
983
984 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
985 matmul34( dest->m, a->m, b->m );
986 else
987 matmul4( dest->m, a->m, b->m );
988 }
989
990
991
992 /**********************************************************************/
993 /* API functions */
994 /**********************************************************************/
995
996
997 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
998 do { \
999 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where); \
1000 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
1001 switch (ctx->Transform.MatrixMode) { \
1002 case GL_MODELVIEW: \
1003 mat = &ctx->ModelView; \
1004 flags |= _NEW_MODELVIEW; \
1005 break; \
1006 case GL_PROJECTION: \
1007 mat = &ctx->ProjectionMatrix; \
1008 flags |= _NEW_PROJECTION; \
1009 break; \
1010 case GL_TEXTURE: \
1011 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
1012 flags |= _NEW_TEXTURE_MATRIX; \
1013 break; \
1014 case GL_COLOR: \
1015 mat = &ctx->ColorMatrix; \
1016 flags |= _NEW_COLOR_MATRIX; \
1017 break; \
1018 default: \
1019 gl_problem(ctx, where); \
1020 } \
1021 } while (0)
1022
1023
1024 void
1025 _mesa_Frustum( GLdouble left, GLdouble right,
1026 GLdouble bottom, GLdouble top,
1027 GLdouble nearval, GLdouble farval )
1028 {
1029 GET_CURRENT_CONTEXT(ctx);
1030 GLfloat x, y, a, b, c, d;
1031 GLfloat m[16];
1032 GLmatrix *mat = 0;
1033
1034 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glFrustrum" );
1035
1036 if ((nearval<=0.0 || farval<=0.0) || (nearval == farval) || (left == right) || (top == bottom)) {
1037 gl_error( ctx, GL_INVALID_VALUE, "glFrustum(near or far)" );
1038 return;
1039 }
1040
1041 x = (2.0*nearval) / (right-left);
1042 y = (2.0*nearval) / (top-bottom);
1043 a = (right+left) / (right-left);
1044 b = (top+bottom) / (top-bottom);
1045 c = -(farval+nearval) / ( farval-nearval);
1046 d = -(2.0*farval*nearval) / (farval-nearval); /* error? */
1047
1048 #define M(row,col) m[col*4+row]
1049 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
1050 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
1051 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
1052 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
1053 #undef M
1054
1055 mat_mul_floats( mat, m, MAT_FLAG_PERSPECTIVE );
1056
1057 if (ctx->Transform.MatrixMode == GL_PROJECTION) {
1058 /* Need to keep a stack of near/far values in case the user push/pops
1059 * the projection matrix stack so that we can call Driver.NearFar()
1060 * after a pop.
1061 */
1062 ctx->NearFarStack[ctx->ProjectionStackDepth][0] = nearval;
1063 ctx->NearFarStack[ctx->ProjectionStackDepth][1] = farval;
1064
1065 if (ctx->Driver.NearFar) {
1066 (*ctx->Driver.NearFar)( ctx, nearval, farval );
1067 }
1068 }
1069 }
1070
1071
1072 void
1073 _mesa_Ortho( GLdouble left, GLdouble right,
1074 GLdouble bottom, GLdouble top,
1075 GLdouble nearval, GLdouble farval )
1076 {
1077 GET_CURRENT_CONTEXT(ctx);
1078 GLfloat x, y, z;
1079 GLfloat tx, ty, tz;
1080 GLfloat m[16];
1081 GLmatrix *mat = 0;
1082
1083 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glOrtho" );
1084
1085 if ((left == right) || (bottom == top) || (nearval == farval)) {
1086 gl_error( ctx, GL_INVALID_VALUE,
1087 "gl_Ortho((l = r) or (b = top) or (n=f)" );
1088 return;
1089 }
1090
1091 x = 2.0 / (right-left);
1092 y = 2.0 / (top-bottom);
1093 z = -2.0 / (farval-nearval);
1094 tx = -(right+left) / (right-left);
1095 ty = -(top+bottom) / (top-bottom);
1096 tz = -(farval+nearval) / (farval-nearval);
1097
1098 #define M(row,col) m[col*4+row]
1099 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = 0.0F; M(0,3) = tx;
1100 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = 0.0F; M(1,3) = ty;
1101 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = z; M(2,3) = tz;
1102 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = 0.0F; M(3,3) = 1.0F;
1103 #undef M
1104
1105 mat_mul_floats( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
1106
1107 if (ctx->Driver.NearFar) {
1108 (*ctx->Driver.NearFar)( ctx, nearval, farval );
1109 }
1110 }
1111
1112
1113 void
1114 _mesa_MatrixMode( GLenum mode )
1115 {
1116 GET_CURRENT_CONTEXT(ctx);
1117 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glMatrixMode");
1118 switch (mode) {
1119 case GL_MODELVIEW:
1120 case GL_PROJECTION:
1121 case GL_TEXTURE:
1122 case GL_COLOR:
1123 ctx->Transform.MatrixMode = mode;
1124 break;
1125 default:
1126 gl_error( ctx, GL_INVALID_ENUM, "glMatrixMode" );
1127 }
1128 }
1129
1130
1131
1132 void
1133 _mesa_PushMatrix( void )
1134 {
1135 GET_CURRENT_CONTEXT(ctx);
1136 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushMatrix");
1137
1138 if (MESA_VERBOSE&VERBOSE_API)
1139 fprintf(stderr, "glPushMatrix %s\n",
1140 gl_lookup_enum_by_nr(ctx->Transform.MatrixMode));
1141
1142 switch (ctx->Transform.MatrixMode) {
1143 case GL_MODELVIEW:
1144 if (ctx->ModelViewStackDepth >= MAX_MODELVIEW_STACK_DEPTH - 1) {
1145 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1146 return;
1147 }
1148 matrix_copy( &ctx->ModelViewStack[ctx->ModelViewStackDepth++],
1149 &ctx->ModelView );
1150 break;
1151 case GL_PROJECTION:
1152 if (ctx->ProjectionStackDepth >= MAX_PROJECTION_STACK_DEPTH - 1) {
1153 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1154 return;
1155 }
1156 matrix_copy( &ctx->ProjectionStack[ctx->ProjectionStackDepth++],
1157 &ctx->ProjectionMatrix );
1158
1159 /* Save near and far projection values */
1160 ctx->NearFarStack[ctx->ProjectionStackDepth][0]
1161 = ctx->NearFarStack[ctx->ProjectionStackDepth-1][0];
1162 ctx->NearFarStack[ctx->ProjectionStackDepth][1]
1163 = ctx->NearFarStack[ctx->ProjectionStackDepth-1][1];
1164 break;
1165 case GL_TEXTURE:
1166 {
1167 GLuint t = ctx->Texture.CurrentTransformUnit;
1168 if (ctx->TextureStackDepth[t] >= MAX_TEXTURE_STACK_DEPTH - 1) {
1169 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1170 return;
1171 }
1172 matrix_copy( &ctx->TextureStack[t][ctx->TextureStackDepth[t]++],
1173 &ctx->TextureMatrix[t] );
1174 }
1175 break;
1176 case GL_COLOR:
1177 if (ctx->ColorStackDepth >= MAX_COLOR_STACK_DEPTH - 1) {
1178 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1179 return;
1180 }
1181 matrix_copy( &ctx->ColorStack[ctx->ColorStackDepth++],
1182 &ctx->ColorMatrix );
1183 break;
1184 default:
1185 gl_problem(ctx, "Bad matrix mode in gl_PushMatrix");
1186 }
1187 }
1188
1189
1190
1191 void
1192 _mesa_PopMatrix( void )
1193 {
1194 GET_CURRENT_CONTEXT(ctx);
1195 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopMatrix");
1196
1197 if (MESA_VERBOSE&VERBOSE_API)
1198 fprintf(stderr, "glPopMatrix %s\n",
1199 gl_lookup_enum_by_nr(ctx->Transform.MatrixMode));
1200
1201 switch (ctx->Transform.MatrixMode) {
1202 case GL_MODELVIEW:
1203 if (ctx->ModelViewStackDepth==0) {
1204 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1205 return;
1206 }
1207 matrix_copy( &ctx->ModelView,
1208 &ctx->ModelViewStack[--ctx->ModelViewStackDepth] );
1209 ctx->NewState |= _NEW_MODELVIEW;
1210 break;
1211 case GL_PROJECTION:
1212 if (ctx->ProjectionStackDepth==0) {
1213 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1214 return;
1215 }
1216
1217 matrix_copy( &ctx->ProjectionMatrix,
1218 &ctx->ProjectionStack[--ctx->ProjectionStackDepth] );
1219 ctx->NewState |= _NEW_PROJECTION;
1220
1221 /* Device driver near/far values */
1222 {
1223 GLfloat nearVal = ctx->NearFarStack[ctx->ProjectionStackDepth][0];
1224 GLfloat farVal = ctx->NearFarStack[ctx->ProjectionStackDepth][1];
1225 if (ctx->Driver.NearFar) {
1226 (*ctx->Driver.NearFar)( ctx, nearVal, farVal );
1227 }
1228 }
1229 break;
1230 case GL_TEXTURE:
1231 {
1232 GLuint t = ctx->Texture.CurrentTransformUnit;
1233 if (ctx->TextureStackDepth[t]==0) {
1234 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1235 return;
1236 }
1237 matrix_copy(&ctx->TextureMatrix[t],
1238 &ctx->TextureStack[t][--ctx->TextureStackDepth[t]]);
1239 ctx->NewState |= _NEW_TEXTURE_MATRIX;
1240 }
1241 break;
1242 case GL_COLOR:
1243 if (ctx->ColorStackDepth==0) {
1244 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1245 return;
1246 }
1247 matrix_copy(&ctx->ColorMatrix,
1248 &ctx->ColorStack[--ctx->ColorStackDepth]);
1249 ctx->NewState |= _NEW_COLOR_MATRIX;
1250 break;
1251 default:
1252 gl_problem(ctx, "Bad matrix mode in gl_PopMatrix");
1253 }
1254 }
1255
1256
1257
1258 void
1259 _mesa_LoadIdentity( void )
1260 {
1261 GET_CURRENT_CONTEXT(ctx);
1262 GLmatrix *mat = 0;
1263 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadIdentity");
1264
1265 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1266
1267 if (mat->inv)
1268 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1269
1270 mat->type = MATRIX_IDENTITY;
1271
1272 /* Have to set this to dirty to make sure we recalculate the
1273 * combined matrix later. The update_matrix in this case is a
1274 * shortcircuit anyway...
1275 */
1276 mat->flags = MAT_DIRTY_DEPENDENTS;
1277 }
1278
1279
1280 void
1281 _mesa_LoadMatrixf( const GLfloat *m )
1282 {
1283 GET_CURRENT_CONTEXT(ctx);
1284 GLmatrix *mat = 0;
1285 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadMatrix");
1286
1287 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1288 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1289
1290 if (ctx->Transform.MatrixMode == GL_PROJECTION) {
1291
1292 #define M(row,col) m[col*4+row]
1293 GLfloat c = M(2,2);
1294 GLfloat d = M(2,3);
1295 #undef M
1296 GLfloat n = (c == 1.0 ? 0.0 : d / (c - 1.0));
1297 GLfloat f = (c == -1.0 ? 1.0 : d / (c + 1.0));
1298
1299 /* Need to keep a stack of near/far values in case the user
1300 * push/pops the projection matrix stack so that we can call
1301 * Driver.NearFar() after a pop.
1302 */
1303 ctx->NearFarStack[ctx->ProjectionStackDepth][0] = n;
1304 ctx->NearFarStack[ctx->ProjectionStackDepth][1] = f;
1305
1306 if (ctx->Driver.NearFar) {
1307 (*ctx->Driver.NearFar)( ctx, n, f );
1308 }
1309 }
1310 }
1311
1312
1313 void
1314 _mesa_LoadMatrixd( const GLdouble *m )
1315 {
1316 GLfloat f[16];
1317 GLint i;
1318 for (i = 0; i < 16; i++)
1319 f[i] = m[i];
1320 _mesa_LoadMatrixf(f);
1321 }
1322
1323
1324
1325 /*
1326 * Multiply the active matrix by an arbitary matrix.
1327 */
1328 void
1329 _mesa_MultMatrixf( const GLfloat *m )
1330 {
1331 GET_CURRENT_CONTEXT(ctx);
1332 GLmatrix *mat = 0;
1333 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glMultMatrix" );
1334 matmul4( mat->m, mat->m, m );
1335 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1336 }
1337
1338
1339 /*
1340 * Multiply the active matrix by an arbitary matrix.
1341 */
1342 void
1343 _mesa_MultMatrixd( const GLdouble *m )
1344 {
1345 GET_CURRENT_CONTEXT(ctx);
1346 GLmatrix *mat = 0;
1347 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glMultMatrix" );
1348 matmul4fd( mat->m, mat->m, m );
1349 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1350 }
1351
1352
1353
1354
1355 /*
1356 * Execute a glRotate call
1357 */
1358 void
1359 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
1360 {
1361 GET_CURRENT_CONTEXT(ctx);
1362 GLfloat m[16];
1363 if (angle != 0.0F) {
1364 GLmatrix *mat = 0;
1365 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glRotate" );
1366
1367 gl_rotation_matrix( angle, x, y, z, m );
1368 mat_mul_floats( mat, m, MAT_FLAG_ROTATION );
1369 }
1370 }
1371
1372 void
1373 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
1374 {
1375 _mesa_Rotatef(angle, x, y, z);
1376 }
1377
1378
1379 /*
1380 * Execute a glScale call
1381 */
1382 void
1383 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
1384 {
1385 GET_CURRENT_CONTEXT(ctx);
1386 GLmatrix *mat = 0;
1387 GLfloat *m;
1388 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glScale");
1389
1390 m = mat->m;
1391 m[0] *= x; m[4] *= y; m[8] *= z;
1392 m[1] *= x; m[5] *= y; m[9] *= z;
1393 m[2] *= x; m[6] *= y; m[10] *= z;
1394 m[3] *= x; m[7] *= y; m[11] *= z;
1395
1396 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
1397 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1398 else
1399 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1400
1401 mat->flags |= (MAT_DIRTY_TYPE |
1402 MAT_DIRTY_INVERSE |
1403 MAT_DIRTY_DEPENDENTS);
1404 }
1405
1406
1407 void
1408 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
1409 {
1410 _mesa_Scalef(x, y, z);
1411 }
1412
1413
1414 /*
1415 * Execute a glTranslate call
1416 */
1417 void
1418 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
1419 {
1420 GET_CURRENT_CONTEXT(ctx);
1421 GLmatrix *mat = 0;
1422 GLfloat *m;
1423 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glTranslate");
1424 m = mat->m;
1425 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1426 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1427 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1428 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1429
1430 mat->flags |= (MAT_FLAG_TRANSLATION |
1431 MAT_DIRTY_TYPE |
1432 MAT_DIRTY_INVERSE |
1433 MAT_DIRTY_DEPENDENTS);
1434 }
1435
1436
1437 void
1438 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
1439 {
1440 _mesa_Translatef(x, y, z);
1441 }
1442
1443
1444
1445 void
1446 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
1447 {
1448 GLfloat tm[16];
1449 gl_matrix_transposef(tm, m);
1450 _mesa_LoadMatrixf(tm);
1451 }
1452
1453
1454 void
1455 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
1456 {
1457 GLdouble tm[16];
1458 gl_matrix_transposed(tm, m);
1459 _mesa_LoadMatrixd(tm);
1460 }
1461
1462
1463 void
1464 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
1465 {
1466 GLfloat tm[16];
1467 gl_matrix_transposef(tm, m);
1468 _mesa_MultMatrixf(tm);
1469 }
1470
1471
1472 void
1473 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
1474 {
1475 GLdouble tm[16];
1476 gl_matrix_transposed(tm, m);
1477 _mesa_MultMatrixd(tm);
1478 }
1479
1480
1481 /*
1482 * Called via glViewport or display list execution.
1483 */
1484 void
1485 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
1486 {
1487 GET_CURRENT_CONTEXT(ctx);
1488 gl_Viewport(ctx, x, y, width, height);
1489 }
1490
1491
1492
1493 /*
1494 * Define a new viewport and reallocate auxillary buffers if the size of
1495 * the window (color buffer) has changed.
1496 *
1497 * XXX This is directly called by device drivers, BUT this function
1498 * may be renamed _mesa_Viewport (without ctx arg) in the future so
1499 * use of _mesa_Viewport is encouraged.
1500 */
1501 void
1502 gl_Viewport( GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height )
1503 {
1504 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glViewport");
1505
1506 if (width<0 || height<0) {
1507 gl_error( ctx, GL_INVALID_VALUE, "glViewport" );
1508 return;
1509 }
1510
1511 if (MESA_VERBOSE & VERBOSE_API)
1512 fprintf(stderr, "glViewport %d %d %d %d\n", x, y, width, height);
1513
1514 /* clamp width, and height to implementation dependent range */
1515 width = CLAMP( width, 1, MAX_WIDTH );
1516 height = CLAMP( height, 1, MAX_HEIGHT );
1517
1518 /* Save viewport */
1519 ctx->Viewport.X = x;
1520 ctx->Viewport.Width = width;
1521 ctx->Viewport.Y = y;
1522 ctx->Viewport.Height = height;
1523
1524 /* compute scale and bias values */
1525 ctx->Viewport._WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
1526 ctx->Viewport._WindowMap.m[MAT_TX] = ctx->Viewport._WindowMap.m[MAT_SX] + x;
1527 ctx->Viewport._WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
1528 ctx->Viewport._WindowMap.m[MAT_TY] = ctx->Viewport._WindowMap.m[MAT_SY] + y;
1529 ctx->Viewport._WindowMap.m[MAT_SZ] = 0.5 * ctx->Visual.DepthMaxF;
1530 ctx->Viewport._WindowMap.m[MAT_TZ] = 0.5 * ctx->Visual.DepthMaxF;
1531
1532 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
1533 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
1534 ctx->NewState |= _NEW_VIEWPORT;
1535
1536 /* Check if window/buffer has been resized and if so, reallocate the
1537 * ancillary buffers.
1538 */
1539 _mesa_ResizeBuffersMESA();
1540
1541 if (ctx->Driver.Viewport) {
1542 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
1543 }
1544 }
1545
1546
1547
1548 void
1549 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
1550 {
1551 /*
1552 * nearval - specifies mapping of the near clipping plane to window
1553 * coordinates, default is 0
1554 * farval - specifies mapping of the far clipping plane to window
1555 * coordinates, default is 1
1556 *
1557 * After clipping and div by w, z coords are in -1.0 to 1.0,
1558 * corresponding to near and far clipping planes. glDepthRange
1559 * specifies a linear mapping of the normalized z coords in
1560 * this range to window z coords.
1561 */
1562 GLfloat n, f;
1563 GET_CURRENT_CONTEXT(ctx);
1564 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthRange");
1565
1566 if (MESA_VERBOSE&VERBOSE_API)
1567 fprintf(stderr, "glDepthRange %f %f\n", nearval, farval);
1568
1569 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
1570 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
1571
1572 ctx->Viewport.Near = n;
1573 ctx->Viewport.Far = f;
1574 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->Visual.DepthMaxF * ((f - n) / 2.0);
1575 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->Visual.DepthMaxF * ((f - n) / 2.0 + n);
1576 ctx->NewState |= _NEW_VIEWPORT;
1577
1578 if (ctx->Driver.DepthRange) {
1579 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
1580 }
1581 }