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