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