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