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