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