fixes for the mga driver
[mesa.git] / src / mesa / main / matrix.c
1 /* $Id: matrix.c,v 1.2 1999/09/05 19:59:33 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) {
916 gl_error( ctx, GL_INVALID_VALUE, "glFrustum(near or far)" );
917 }
918
919 x = (2.0*nearval) / (right-left);
920 y = (2.0*nearval) / (top-bottom);
921 a = (right+left) / (right-left);
922 b = (top+bottom) / (top-bottom);
923 c = -(farval+nearval) / ( farval-nearval);
924 d = -(2.0*farval*nearval) / (farval-nearval); /* error? */
925
926 #define M(row,col) m[col*4+row]
927 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
928 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
929 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
930 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
931 #undef M
932
933
934 gl_mat_mul_floats( mat, m, MAT_FLAG_PERSPECTIVE );
935
936
937 if (ctx->Transform.MatrixMode == GL_PROJECTION)
938 {
939 /* Need to keep a stack of near/far values in case the user push/pops
940 * the projection matrix stack so that we can call Driver.NearFar()
941 * after a pop.
942 */
943 ctx->NearFarStack[ctx->ProjectionStackDepth][0] = nearval;
944 ctx->NearFarStack[ctx->ProjectionStackDepth][1] = farval;
945
946 if (ctx->Driver.NearFar) {
947 (*ctx->Driver.NearFar)( ctx, nearval, farval );
948 }
949 }
950 }
951
952
953 void gl_Ortho( GLcontext *ctx,
954 GLdouble left, GLdouble right,
955 GLdouble bottom, GLdouble top,
956 GLdouble nearval, GLdouble farval )
957 {
958 GLfloat x, y, z;
959 GLfloat tx, ty, tz;
960 GLfloat m[16];
961 GLmatrix *mat = 0;
962
963 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glOrtho" );
964
965 x = 2.0 / (right-left);
966 y = 2.0 / (top-bottom);
967 z = -2.0 / (farval-nearval);
968 tx = -(right+left) / (right-left);
969 ty = -(top+bottom) / (top-bottom);
970 tz = -(farval+nearval) / (farval-nearval);
971
972 #define M(row,col) m[col*4+row]
973 M(0,0) = x; M(0,1) = 0.0F; M(0,2) = 0.0F; M(0,3) = tx;
974 M(1,0) = 0.0F; M(1,1) = y; M(1,2) = 0.0F; M(1,3) = ty;
975 M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = z; M(2,3) = tz;
976 M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = 0.0F; M(3,3) = 1.0F;
977 #undef M
978
979 gl_mat_mul_floats( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
980
981 if (ctx->Driver.NearFar) {
982 (*ctx->Driver.NearFar)( ctx, nearval, farval );
983 }
984 }
985
986
987 void gl_MatrixMode( GLcontext *ctx, GLenum mode )
988 {
989 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glMatrixMode");
990 switch (mode) {
991 case GL_MODELVIEW:
992 case GL_PROJECTION:
993 case GL_TEXTURE:
994 ctx->Transform.MatrixMode = mode;
995 break;
996 default:
997 gl_error( ctx, GL_INVALID_ENUM, "glMatrixMode" );
998 }
999 }
1000
1001
1002
1003 void gl_PushMatrix( GLcontext *ctx )
1004 {
1005 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushMatrix");
1006
1007 if (MESA_VERBOSE&VERBOSE_API)
1008 fprintf(stderr, "glPushMatrix %s\n",
1009 gl_lookup_enum_by_nr(ctx->Transform.MatrixMode));
1010
1011 switch (ctx->Transform.MatrixMode) {
1012 case GL_MODELVIEW:
1013 if (ctx->ModelViewStackDepth>=MAX_MODELVIEW_STACK_DEPTH-1) {
1014 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1015 return;
1016 }
1017 gl_matrix_copy( &ctx->ModelViewStack[ctx->ModelViewStackDepth++],
1018 &ctx->ModelView );
1019 break;
1020 case GL_PROJECTION:
1021 if (ctx->ProjectionStackDepth>=MAX_PROJECTION_STACK_DEPTH) {
1022 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1023 return;
1024 }
1025 gl_matrix_copy( &ctx->ProjectionStack[ctx->ProjectionStackDepth++],
1026 &ctx->ProjectionMatrix );
1027
1028 /* Save near and far projection values */
1029 ctx->NearFarStack[ctx->ProjectionStackDepth][0]
1030 = ctx->NearFarStack[ctx->ProjectionStackDepth-1][0];
1031 ctx->NearFarStack[ctx->ProjectionStackDepth][1]
1032 = ctx->NearFarStack[ctx->ProjectionStackDepth-1][1];
1033 break;
1034 case GL_TEXTURE:
1035 {
1036 GLuint t = ctx->Texture.CurrentTransformUnit;
1037 if (ctx->TextureStackDepth[t] >= MAX_TEXTURE_STACK_DEPTH) {
1038 gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
1039 return;
1040 }
1041 gl_matrix_copy( &ctx->TextureStack[t][ctx->TextureStackDepth[t]++],
1042 &ctx->TextureMatrix[t] );
1043 }
1044 break;
1045 default:
1046 gl_problem(ctx, "Bad matrix mode in gl_PushMatrix");
1047 }
1048 }
1049
1050
1051
1052 void gl_PopMatrix( GLcontext *ctx )
1053 {
1054 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopMatrix");
1055
1056 if (MESA_VERBOSE&VERBOSE_API)
1057 fprintf(stderr, "glPopMatrix %s\n",
1058 gl_lookup_enum_by_nr(ctx->Transform.MatrixMode));
1059
1060 switch (ctx->Transform.MatrixMode) {
1061 case GL_MODELVIEW:
1062 if (ctx->ModelViewStackDepth==0) {
1063 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1064 return;
1065 }
1066 gl_matrix_copy( &ctx->ModelView,
1067 &ctx->ModelViewStack[--ctx->ModelViewStackDepth] );
1068 ctx->NewState |= NEW_MODELVIEW;
1069 break;
1070 case GL_PROJECTION:
1071 if (ctx->ProjectionStackDepth==0) {
1072 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1073 return;
1074 }
1075
1076 gl_matrix_copy( &ctx->ProjectionMatrix,
1077 &ctx->ProjectionStack[--ctx->ProjectionStackDepth] );
1078 ctx->NewState |= NEW_PROJECTION;
1079
1080 /* Device driver near/far values */
1081 {
1082 GLfloat nearVal = ctx->NearFarStack[ctx->ProjectionStackDepth][0];
1083 GLfloat farVal = ctx->NearFarStack[ctx->ProjectionStackDepth][1];
1084 if (ctx->Driver.NearFar) {
1085 (*ctx->Driver.NearFar)( ctx, nearVal, farVal );
1086 }
1087 }
1088 break;
1089 case GL_TEXTURE:
1090 {
1091 GLuint t = ctx->Texture.CurrentTransformUnit;
1092 if (ctx->TextureStackDepth[t]==0) {
1093 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
1094 return;
1095 }
1096 gl_matrix_copy(&ctx->TextureMatrix[t],
1097 &ctx->TextureStack[t][--ctx->TextureStackDepth[t]]);
1098 }
1099 break;
1100 default:
1101 gl_problem(ctx, "Bad matrix mode in gl_PopMatrix");
1102 }
1103 }
1104
1105
1106
1107 void gl_LoadIdentity( GLcontext *ctx )
1108 {
1109 GLmatrix *mat = 0;
1110 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadIdentity");
1111
1112 MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
1113
1114 if (mat->inv)
1115 MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) );
1116
1117 mat->type = MATRIX_IDENTITY;
1118
1119 /* Have to set this to dirty to make sure we recalculate the
1120 * combined matrix later. The update_matrix in this case is a
1121 * shortcircuit anyway...
1122 */
1123 mat->flags = MAT_DIRTY_DEPENDENTS;
1124 }
1125
1126
1127 void gl_LoadMatrixf( GLcontext *ctx, const GLfloat *m )
1128 {
1129 GLmatrix *mat = 0;
1130 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadMatrix");
1131
1132 MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
1133 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1134
1135 if (ctx->Transform.MatrixMode == GL_PROJECTION) {
1136
1137 #define M(row,col) m[col*4+row]
1138 GLfloat c = M(2,2);
1139 GLfloat d = M(2,3);
1140 #undef M
1141 GLfloat n = (c == 1.0 ? 0.0 : d / (c - 1.0));
1142 GLfloat f = (c == -1.0 ? 1.0 : d / (c + 1.0));
1143
1144 /* Need to keep a stack of near/far values in case the user
1145 * push/pops the projection matrix stack so that we can call
1146 * Driver.NearFar() after a pop.
1147 */
1148 ctx->NearFarStack[ctx->ProjectionStackDepth][0] = n;
1149 ctx->NearFarStack[ctx->ProjectionStackDepth][1] = f;
1150
1151 if (ctx->Driver.NearFar) {
1152 (*ctx->Driver.NearFar)( ctx, n, f );
1153 }
1154 }
1155 }
1156
1157
1158
1159 /*
1160 * Multiply the active matrix by an arbitary matrix.
1161 */
1162 void gl_MultMatrixf( GLcontext *ctx, const GLfloat *m )
1163 {
1164 GLmatrix *mat = 0;
1165 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glMultMatrix" );
1166 matmul4( mat->m, mat->m, m );
1167 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1168 }
1169
1170
1171 /*
1172 * Multiply the active matrix by an arbitary matrix.
1173 */
1174 void gl_MultMatrixd( GLcontext *ctx, const GLdouble *m )
1175 {
1176 GLmatrix *mat = 0;
1177 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glMultMatrix" );
1178 matmul4fd( mat->m, mat->m, m );
1179 mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER);
1180 }
1181
1182
1183
1184
1185 /*
1186 * Multiply a matrix by an array of floats with known properties.
1187 */
1188 void gl_mat_mul_floats( GLmatrix *mat, const GLfloat *m, GLuint flags )
1189 {
1190 mat->flags |= (flags |
1191 MAT_DIRTY_TYPE |
1192 MAT_DIRTY_INVERSE |
1193 MAT_DIRTY_DEPENDENTS);
1194
1195 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
1196 matmul34( mat->m, mat->m, m );
1197 else
1198 matmul4( mat->m, mat->m, m );
1199
1200 }
1201
1202 /*
1203 * Multiply a matrix by an array of floats with known properties.
1204 */
1205 void gl_mat_mul_mat( GLmatrix *mat, const GLmatrix *m )
1206 {
1207 mat->flags |= (m->flags |
1208 MAT_DIRTY_TYPE |
1209 MAT_DIRTY_INVERSE |
1210 MAT_DIRTY_DEPENDENTS);
1211
1212 if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
1213 matmul34( mat->m, mat->m, m->m );
1214 else
1215 matmul4( mat->m, mat->m, m->m );
1216 }
1217
1218
1219
1220 /*
1221 * Execute a glRotate call
1222 */
1223 void gl_Rotatef( GLcontext *ctx,
1224 GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
1225 {
1226 GLfloat m[16];
1227 if (angle != 0.0F) {
1228 GLmatrix *mat = 0;
1229 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glRotate" );
1230
1231 gl_rotation_matrix( angle, x, y, z, m );
1232 gl_mat_mul_floats( mat, m, MAT_FLAG_ROTATION );
1233 }
1234 }
1235
1236 /*
1237 * Execute a glScale call
1238 */
1239 void gl_Scalef( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z )
1240 {
1241 GLmatrix *mat = 0;
1242 GLfloat *m;
1243 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glScale");
1244
1245 m = mat->m;
1246 m[0] *= x; m[4] *= y; m[8] *= z;
1247 m[1] *= x; m[5] *= y; m[9] *= z;
1248 m[2] *= x; m[6] *= y; m[10] *= z;
1249 m[3] *= x; m[7] *= y; m[11] *= z;
1250
1251 if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8)
1252 mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1253 else
1254 mat->flags |= MAT_FLAG_GENERAL_SCALE;
1255
1256 mat->flags |= (MAT_DIRTY_TYPE |
1257 MAT_DIRTY_INVERSE |
1258 MAT_DIRTY_DEPENDENTS);
1259 }
1260
1261 /*
1262 * Execute a glTranslate call
1263 */
1264 void gl_Translatef( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z )
1265 {
1266 GLmatrix *mat = 0;
1267 GLfloat *m;
1268 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glTranslate");
1269 m = mat->m;
1270 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1271 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1272 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1273 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1274
1275 mat->flags |= (MAT_FLAG_TRANSLATION |
1276 MAT_DIRTY_TYPE |
1277 MAT_DIRTY_INVERSE |
1278 MAT_DIRTY_DEPENDENTS);
1279 }
1280
1281
1282 /*
1283 * Define a new viewport and reallocate auxillary buffers if the size of
1284 * the window (color buffer) has changed.
1285 */
1286 void gl_Viewport( GLcontext *ctx,
1287 GLint x, GLint y, GLsizei width, GLsizei height )
1288 {
1289 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glViewport");
1290
1291 if (width<0 || height<0) {
1292 gl_error( ctx, GL_INVALID_VALUE, "glViewport" );
1293 return;
1294 }
1295
1296 if (MESA_VERBOSE & VERBOSE_API)
1297 fprintf(stderr, "glViewport %d %d %d %d\n", x, y, width, height);
1298
1299 /* clamp width, and height to implementation dependent range */
1300 width = CLAMP( width, 1, MAX_WIDTH );
1301 height = CLAMP( height, 1, MAX_HEIGHT );
1302
1303 /* Save viewport */
1304 ctx->Viewport.X = x;
1305 ctx->Viewport.Width = width;
1306 ctx->Viewport.Y = y;
1307 ctx->Viewport.Height = height;
1308
1309 /* compute scale and bias values */
1310 ctx->Viewport.WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
1311 ctx->Viewport.WindowMap.m[MAT_TX] = ctx->Viewport.WindowMap.m[MAT_SX] + x;
1312 ctx->Viewport.WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
1313 ctx->Viewport.WindowMap.m[MAT_TY] = ctx->Viewport.WindowMap.m[MAT_SY] + y;
1314 ctx->Viewport.WindowMap.m[MAT_SZ] = 0.5 * DEPTH_SCALE;
1315 ctx->Viewport.WindowMap.m[MAT_TZ] = 0.5 * DEPTH_SCALE;
1316
1317 ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
1318 ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT;
1319
1320 ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
1321 ctx->NewState |= NEW_VIEWPORT;
1322
1323 /* Check if window/buffer has been resized and if so, reallocate the
1324 * ancillary buffers.
1325 */
1326 gl_ResizeBuffersMESA(ctx);
1327
1328
1329 ctx->RasterMask &= WINCLIP_BIT;
1330
1331 if ( ctx->Viewport.X<0
1332 || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
1333 || ctx->Viewport.Y<0
1334 || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
1335 ctx->RasterMask |= WINCLIP_BIT;
1336 }
1337
1338
1339 if (ctx->Driver.Viewport) {
1340 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
1341 }
1342 }
1343
1344
1345
1346 void gl_DepthRange( GLcontext *ctx, GLclampd nearval, GLclampd farval )
1347 {
1348 /*
1349 * nearval - specifies mapping of the near clipping plane to window
1350 * coordinates, default is 0
1351 * farval - specifies mapping of the far clipping plane to window
1352 * coordinates, default is 1
1353 *
1354 * After clipping and div by w, z coords are in -1.0 to 1.0,
1355 * corresponding to near and far clipping planes. glDepthRange
1356 * specifies a linear mapping of the normalized z coords in
1357 * this range to window z coords.
1358 */
1359 GLfloat n, f;
1360
1361 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthRange");
1362
1363 if (MESA_VERBOSE&VERBOSE_API)
1364 fprintf(stderr, "glDepthRange %f %f\n", nearval, farval);
1365
1366 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
1367 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
1368
1369 ctx->Viewport.Near = n;
1370 ctx->Viewport.Far = f;
1371 ctx->Viewport.WindowMap.m[MAT_SZ] = DEPTH_SCALE * ((f - n) / 2.0);
1372 ctx->Viewport.WindowMap.m[MAT_TZ] = DEPTH_SCALE * ((f - n) / 2.0 + n);
1373
1374 ctx->ModelProjectWinMatrixUptodate = GL_FALSE;
1375
1376 if (ctx->Driver.DepthRange) {
1377 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
1378 }
1379 }
1380
1381
1382 void gl_calculate_model_project_matrix( GLcontext *ctx )
1383 {
1384 gl_matrix_mul( &ctx->ModelProjectMatrix,
1385 &ctx->ProjectionMatrix,
1386 &ctx->ModelView );
1387
1388 gl_matrix_analyze( &ctx->ModelProjectMatrix );
1389 }
1390
1391
1392 void gl_matrix_ctr( GLmatrix *m )
1393 {
1394 m->inv = 0;
1395 MEMCPY( m->m, Identity, sizeof(Identity));
1396 m->type = MATRIX_IDENTITY;
1397 m->flags = MAT_DIRTY_DEPENDENTS;
1398 }
1399
1400 void gl_matrix_dtr( GLmatrix *m )
1401 {
1402 if (m->inv != 0) {
1403 free(m->inv);
1404 m->inv = 0;
1405 }
1406 }
1407
1408 void gl_matrix_set_identity( GLmatrix *m )
1409 {
1410 MEMCPY( m->m, Identity, sizeof(Identity));
1411 m->type = MATRIX_IDENTITY;
1412 m->flags = MAT_DIRTY_DEPENDENTS;
1413 }
1414
1415
1416 void gl_matrix_alloc_inv( GLmatrix *m )
1417 {
1418 if (m->inv == 0) {
1419 m->inv = (GLfloat *)malloc(16*sizeof(GLfloat));
1420 MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
1421 }
1422 }
1423
1424 void gl_matrix_copy( GLmatrix *to, const GLmatrix *from )
1425 {
1426 MEMCPY( to->m, from->m, sizeof(Identity));
1427 to->flags = from->flags | MAT_DIRTY_DEPENDENTS;
1428 to->type = from->type;
1429
1430 if (to->inv != 0) {
1431 if (from->inv == 0) {
1432 gl_matrix_invert( to );
1433 } else {
1434 MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16);
1435 }
1436 }
1437 }
1438
1439 void gl_matrix_mul( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
1440 {
1441 dest->flags = (a->flags |
1442 b->flags |
1443 MAT_DIRTY_TYPE |
1444 MAT_DIRTY_INVERSE |
1445 MAT_DIRTY_DEPENDENTS);
1446
1447 if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
1448 matmul34( dest->m, a->m, b->m );
1449 else
1450 matmul4( dest->m, a->m, b->m );
1451 }