1 /* $Id: matrix.c,v 1.4 1999/09/19 23:06:40 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
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
57 #include "GL/xf86glx.h"
62 static const char *types
[] = {
71 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
);
74 static GLfloat Identity
[16] = {
82 static void print_matrix_floats( const GLfloat m
[16] )
86 fprintf(stderr
,"\t%f %f %f %f\n", m
[i
], m
[4+i
], m
[8+i
], m
[12+i
] );
90 void gl_print_matrix( const GLmatrix
*m
)
92 fprintf(stderr
, "Matrix type: %s, flags: %x\n", types
[m
->type
], m
->flags
);
93 print_matrix_floats(m
->m
);
95 fprintf(stderr
, "Inverse: \n");
98 print_matrix_floats(m
->inv
);
99 matmul4(prod
, m
->m
, m
->inv
);
100 fprintf(stderr
, "Mat * Inverse:\n");
101 print_matrix_floats(prod
);
103 fprintf(stderr
, " - not available\n");
110 * This matmul was contributed by Thomas Malik
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
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]
124 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
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);
139 /* Multiply two matrices known to occupy only the top three rows,
140 * such as typical modelling matrices, and ortho matrices.
144 static void matmul34( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
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
;
160 static void matmul4fd( GLfloat
*product
, const GLfloat
*a
, const GLdouble
*b
)
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);
178 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
179 #define MAT(m,r,c) (m)[(c)*4+(r)]
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)
186 static GLboolean
invert_matrix_general( GLmatrix
*mat
)
188 const GLfloat
*m
= mat
->m
;
189 GLfloat
*out
= mat
->inv
;
191 GLfloat m0
, m1
, m2
, m3
, s
;
192 GLfloat
*r0
, *r1
, *r2
, *r3
;
194 r0
= wtmp
[0], r1
= wtmp
[1], r2
= wtmp
[2], r3
= wtmp
[3];
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,
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,
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,
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;
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
;
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
;
224 if (s
!= 0.0) { r1
[4] -= m1
* s
; r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
226 if (s
!= 0.0) { r1
[5] -= m1
* s
; r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
228 if (s
!= 0.0) { r1
[6] -= m1
* s
; r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
230 if (s
!= 0.0) { r1
[7] -= m1
* s
; r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
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
;
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
; }
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
;
250 /* eliminate third variable */
252 r3
[3] -= m3
* r2
[3], r3
[4] -= m3
* r2
[4],
253 r3
[5] -= m3
* r2
[5], r3
[6] -= m3
* r2
[6],
257 if (0.0 == r3
[3]) return GL_FALSE
;
259 s
= 1.0/r3
[3]; /* now back substitute row 3 */
260 r3
[4] *= s
; r3
[5] *= s
; r3
[6] *= s
; r3
[7] *= s
;
262 m2
= r2
[3]; /* now back substitute row 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
);
267 r1
[4] -= r3
[4] * m1
, r1
[5] -= r3
[5] * m1
,
268 r1
[6] -= r3
[6] * m1
, r1
[7] -= r3
[7] * m1
;
270 r0
[4] -= r3
[4] * m0
, r0
[5] -= r3
[5] * m0
,
271 r0
[6] -= r3
[6] * m0
, r0
[7] -= r3
[7] * m0
;
273 m1
= r1
[2]; /* now back substitute row 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
);
278 r0
[4] -= r2
[4] * m0
, r0
[5] -= r2
[5] * m0
,
279 r0
[6] -= r2
[6] * m0
, r0
[7] -= r2
[7] * m0
;
281 m0
= r0
[1]; /* now back substitute row 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
);
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];
299 /* Adapted from graphics gems II.
301 GLboolean
invert_matrix_3d_general( GLmatrix
*mat
)
303 const GLfloat
*in
= mat
->m
;
304 GLfloat
*out
= mat
->inv
;
308 /* Calculate the determinant of upper left 3x3 submatrix and
309 * determine if the matrix is singular.
312 t
= MAT(in
,0,0) * MAT(in
,1,1) * MAT(in
,2,2);
313 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
315 t
= MAT(in
,1,0) * MAT(in
,2,1) * MAT(in
,0,2);
316 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
318 t
= MAT(in
,2,0) * MAT(in
,0,1) * MAT(in
,1,2);
319 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
321 t
= -MAT(in
,2,0) * MAT(in
,1,1) * MAT(in
,0,2);
322 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
324 t
= -MAT(in
,1,0) * MAT(in
,0,1) * MAT(in
,2,2);
325 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
327 t
= -MAT(in
,0,0) * MAT(in
,2,1) * MAT(in
,1,2);
328 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
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
);
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) );
361 static GLboolean
invert_matrix_3d( GLmatrix
*mat
)
363 const GLfloat
*in
= mat
->m
;
364 GLfloat
*out
= mat
->inv
;
366 if (!TEST_MAT_FLAGS(mat
, MAT_FLAGS_ANGLE_PRESERVING
))
368 return invert_matrix_3d_general( mat
);
371 if (mat
->flags
& MAT_FLAG_UNIFORM_SCALE
)
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));
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);
393 else if (mat
->flags
& MAT_FLAG_ROTATION
)
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);
406 else /* pure translation */
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);
415 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
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) );
430 MAT(out
,0,3) = MAT(out
,1,3) = MAT(out
,2,3) = 0.0;
438 static GLboolean
invert_matrix_identity( GLmatrix
*mat
)
440 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
445 static GLboolean
invert_matrix_3d_no_rot( GLmatrix
*mat
)
447 const GLfloat
*in
= mat
->m
;
448 GLfloat
*out
= mat
->inv
;
450 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0 || MAT(in
,2,2) == 0 )
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);
458 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
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));
469 static GLboolean
invert_matrix_2d_no_rot( GLmatrix
*mat
)
471 const GLfloat
*in
= mat
->m
;
472 GLfloat
*out
= mat
->inv
;
474 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0)
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);
481 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
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));
491 static GLboolean
invert_matrix_perspective( GLmatrix
*mat
)
493 const GLfloat
*in
= mat
->m
;
494 GLfloat
*out
= mat
->inv
;
496 if (MAT(in
,2,3) == 0)
499 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
501 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
502 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
504 MAT(out
,0,3) = MAT(in
,0,2);
505 MAT(out
,1,3) = MAT(in
,1,2);
510 MAT(out
,3,2) = 1.0 / MAT(in
,2,3);
511 MAT(out
,3,3) = MAT(in
,2,2) * MAT(out
,3,2);
517 typedef GLboolean (*inv_mat_func
)( GLmatrix
*mat
);
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
,
530 GLboolean
gl_matrix_invert( GLmatrix
*mat
)
532 if (inv_mat_tab
[mat
->type
](mat
)) {
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
);
542 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
550 * Generate a 4x4 transformation matrix from glRotate parameters.
552 void gl_rotation_matrix( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
,
555 /* This function contributed by Erich Boleyn (erich@uruk.org) */
557 GLfloat xx
, yy
, zz
, xy
, yz
, zx
, xs
, ys
, zs
, one_c
;
559 s
= sin( angle
* DEG2RAD
);
560 c
= cos( angle
* DEG2RAD
);
562 mag
= GL_SQRT( x
*x
+ y
*y
+ z
*z
);
565 /* generate an identity matrix and return */
566 MEMCPY(m
, Identity
, sizeof(GLfloat
)*16);
574 #define M(row,col) m[col*4+row]
577 * Arbitrary axis rotation matrix.
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.
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.
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)
603 * cos ( theta ) = x / sqrt ( 1 - z^2 )
604 * sin ( theta ) = y / sqrt ( 1 - z^2 )
606 * cos ( phi ) = sqrt ( 1 - z^2 )
609 * Note that cos ( phi ) can further be inserted to the above
612 * cos ( theta ) = x / cos ( phi )
613 * sin ( theta ) = y / sin ( phi )
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.
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.
640 M(0,0) = (one_c
* xx
) + c
;
641 M(0,1) = (one_c
* xy
) - zs
;
642 M(0,2) = (one_c
* zx
) + ys
;
645 M(1,0) = (one_c
* xy
) + zs
;
646 M(1,1) = (one_c
* yy
) + c
;
647 M(1,2) = (one_c
* yz
) - xs
;
650 M(2,0) = (one_c
* zx
) - ys
;
651 M(2,1) = (one_c
* yz
) + xs
;
652 M(2,2) = (one_c
* zz
) + c
;
663 #define ZERO(x) (1<<x)
664 #define ONE(x) (1<<(x+16))
666 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
667 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
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) )
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) )
679 #define MASK_2D ( ZERO(8) | \
681 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
682 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
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) )
693 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
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) )
701 #define SQ(x) ((x)*(x))
703 /* Determine type and flags from scratch. This is expensive enough to
704 * only want to do it once.
706 static void analyze_from_scratch( GLmatrix
*mat
)
708 const GLfloat
*m
= mat
->m
;
712 for (i
= 0 ; i
< 16 ; i
++)
714 if (m
[i
] == 0.0) mask
|= (1<<i
);
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);
722 mat
->flags
&= ~MAT_FLAGS_GEOMETRY
;
724 /* Check for translation - no-one really cares
726 if ((mask
& MASK_NO_TRX
) != MASK_NO_TRX
)
727 mat
->flags
|= MAT_FLAG_TRANSLATION
;
731 if (mask
== MASK_IDENTITY
) {
732 mat
->type
= MATRIX_IDENTITY
;
734 else if ((mask
& MASK_2D_NO_ROT
) == MASK_2D_NO_ROT
)
736 mat
->type
= MATRIX_2D_NO_ROT
;
738 if ((mask
& MASK_NO_2D_SCALE
) != MASK_NO_2D_SCALE
)
739 mat
->flags
= MAT_FLAG_GENERAL_SCALE
;
741 else if ((mask
& MASK_2D
) == MASK_2D
)
743 GLfloat mm
= DOT2(m
, m
);
744 GLfloat m4m4
= DOT2(m
+4,m
+4);
745 GLfloat mm4
= DOT2(m
,m
+4);
747 mat
->type
= MATRIX_2D
;
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
;
754 /* Check for rotation */
755 if (SQ(mm4
) > SQ(1e-6))
756 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
758 mat
->flags
|= MAT_FLAG_ROTATION
;
761 else if ((mask
& MASK_3D_NO_ROT
) == MASK_3D_NO_ROT
)
763 mat
->type
= MATRIX_3D_NO_ROT
;
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
;
771 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
773 else if ((mask
& MASK_3D
) == MASK_3D
)
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);
781 mat
->type
= MATRIX_3D
;
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 */
789 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
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
;
798 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
801 mat
->flags
|= MAT_FLAG_GENERAL_3D
; /* shear, etc */
803 else if ((mask
& MASK_PERSPECTIVE
) == MASK_PERSPECTIVE
&& m
[11]==-1.0F
)
805 mat
->type
= MATRIX_PERSPECTIVE
;
806 mat
->flags
|= MAT_FLAG_GENERAL
;
809 mat
->type
= MATRIX_GENERAL
;
810 mat
->flags
|= MAT_FLAG_GENERAL
;
815 /* Analyse a matrix given that its flags are accurate - this is the
816 * more common operation, hopefully.
818 static void analyze_from_flags( GLmatrix
*mat
)
820 const GLfloat
*m
= mat
->m
;
822 if (TEST_MAT_FLAGS(mat
, 0)) {
823 mat
->type
= MATRIX_IDENTITY
;
825 else if (TEST_MAT_FLAGS(mat
, (MAT_FLAG_TRANSLATION
|
826 MAT_FLAG_UNIFORM_SCALE
|
827 MAT_FLAG_GENERAL_SCALE
)))
829 if ( m
[10]==1.0F
&& m
[14]==0.0F
) {
830 mat
->type
= MATRIX_2D_NO_ROT
;
833 mat
->type
= MATRIX_3D_NO_ROT
;
836 else if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
)) {
839 && m
[2]==0.0F
&& m
[6]==0.0F
&& m
[10]==1.0F
&& m
[14]==0.0F
)
841 mat
->type
= MATRIX_2D
;
845 mat
->type
= MATRIX_3D
;
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
)
853 mat
->type
= MATRIX_PERSPECTIVE
;
856 mat
->type
= MATRIX_GENERAL
;
862 void gl_matrix_analyze( GLmatrix
*mat
)
864 if (mat
->flags
& MAT_DIRTY_TYPE
) {
865 if (mat
->flags
& MAT_DIRTY_FLAGS
)
866 analyze_from_scratch( mat
);
868 analyze_from_flags( mat
);
871 if (mat
->inv
&& (mat
->flags
& MAT_DIRTY_INVERSE
)) {
872 gl_matrix_invert( mat
);
875 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
881 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
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) { \
887 mat = &ctx->ModelView; \
888 flags |= NEW_MODELVIEW; \
890 case GL_PROJECTION: \
891 mat = &ctx->ProjectionMatrix; \
892 flags |= NEW_PROJECTION; \
895 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
896 flags |= NEW_TEXTURE_MATRIX; \
899 gl_problem(ctx, where); \
904 void gl_Frustum( GLcontext
*ctx
,
905 GLdouble left
, GLdouble right
,
906 GLdouble bottom
, GLdouble top
,
907 GLdouble nearval
, GLdouble farval
)
909 GLfloat x
, y
, a
, b
, c
, d
;
913 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glFrustrum" );
915 if ((nearval
<=0.0 || farval
<=0.0) || (nearval
== farval
) || (left
== right
) || (top
== bottom
)) {
916 gl_error( ctx
, GL_INVALID_VALUE
, "glFrustum(near or far)" );
920 x
= (2.0*nearval
) / (right
-left
);
921 y
= (2.0*nearval
) / (top
-bottom
);
922 a
= (right
+left
) / (right
-left
);
923 b
= (top
+bottom
) / (top
-bottom
);
924 c
= -(farval
+nearval
) / ( farval
-nearval
);
925 d
= -(2.0*farval
*nearval
) / (farval
-nearval
); /* error? */
927 #define M(row,col) m[col*4+row]
928 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
929 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
930 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
931 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
935 gl_mat_mul_floats( mat
, m
, MAT_FLAG_PERSPECTIVE
);
938 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
)
940 /* Need to keep a stack of near/far values in case the user push/pops
941 * the projection matrix stack so that we can call Driver.NearFar()
944 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = nearval
;
945 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = farval
;
947 if (ctx
->Driver
.NearFar
) {
948 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
954 void gl_Ortho( GLcontext
*ctx
,
955 GLdouble left
, GLdouble right
,
956 GLdouble bottom
, GLdouble top
,
957 GLdouble nearval
, GLdouble farval
)
964 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
966 if ((left
== right
) || (bottom
== top
) || (nearval
== farval
)) {
967 gl_error( ctx
, GL_INVALID_VALUE
, "gl_Ortho((l = r) or (b = top) or (n=f)" );
971 x
= 2.0 / (right
-left
);
972 y
= 2.0 / (top
-bottom
);
973 z
= -2.0 / (farval
-nearval
);
974 tx
= -(right
+left
) / (right
-left
);
975 ty
= -(top
+bottom
) / (top
-bottom
);
976 tz
= -(farval
+nearval
) / (farval
-nearval
);
978 #define M(row,col) m[col*4+row]
979 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = 0.0F
; M(0,3) = tx
;
980 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = 0.0F
; M(1,3) = ty
;
981 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = z
; M(2,3) = tz
;
982 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = 0.0F
; M(3,3) = 1.0F
;
985 gl_mat_mul_floats( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
987 if (ctx
->Driver
.NearFar
) {
988 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
993 void gl_MatrixMode( GLcontext
*ctx
, GLenum mode
)
995 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glMatrixMode");
1000 ctx
->Transform
.MatrixMode
= mode
;
1003 gl_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
1009 void gl_PushMatrix( GLcontext
*ctx
)
1011 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushMatrix");
1013 if (MESA_VERBOSE
&VERBOSE_API
)
1014 fprintf(stderr
, "glPushMatrix %s\n",
1015 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1017 switch (ctx
->Transform
.MatrixMode
) {
1019 if (ctx
->ModelViewStackDepth
>=MAX_MODELVIEW_STACK_DEPTH
-1) {
1020 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1023 gl_matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
1027 if (ctx
->ProjectionStackDepth
>=MAX_PROJECTION_STACK_DEPTH
) {
1028 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1031 gl_matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
1032 &ctx
->ProjectionMatrix
);
1034 /* Save near and far projection values */
1035 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0]
1036 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][0];
1037 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1]
1038 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][1];
1042 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1043 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
) {
1044 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1047 gl_matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
1048 &ctx
->TextureMatrix
[t
] );
1052 gl_problem(ctx
, "Bad matrix mode in gl_PushMatrix");
1058 void gl_PopMatrix( GLcontext
*ctx
)
1060 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopMatrix");
1062 if (MESA_VERBOSE
&VERBOSE_API
)
1063 fprintf(stderr
, "glPopMatrix %s\n",
1064 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1066 switch (ctx
->Transform
.MatrixMode
) {
1068 if (ctx
->ModelViewStackDepth
==0) {
1069 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1072 gl_matrix_copy( &ctx
->ModelView
,
1073 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
1074 ctx
->NewState
|= NEW_MODELVIEW
;
1077 if (ctx
->ProjectionStackDepth
==0) {
1078 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1082 gl_matrix_copy( &ctx
->ProjectionMatrix
,
1083 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
1084 ctx
->NewState
|= NEW_PROJECTION
;
1086 /* Device driver near/far values */
1088 GLfloat nearVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0];
1089 GLfloat farVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1];
1090 if (ctx
->Driver
.NearFar
) {
1091 (*ctx
->Driver
.NearFar
)( ctx
, nearVal
, farVal
);
1097 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1098 if (ctx
->TextureStackDepth
[t
]==0) {
1099 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1102 gl_matrix_copy(&ctx
->TextureMatrix
[t
],
1103 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
1107 gl_problem(ctx
, "Bad matrix mode in gl_PopMatrix");
1113 void gl_LoadIdentity( GLcontext
*ctx
)
1116 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
1118 MEMCPY( mat
->m
, Identity
, 16*sizeof(GLfloat
) );
1121 MEMCPY( mat
->inv
, Identity
, 16*sizeof(GLfloat
) );
1123 mat
->type
= MATRIX_IDENTITY
;
1125 /* Have to set this to dirty to make sure we recalculate the
1126 * combined matrix later. The update_matrix in this case is a
1127 * shortcircuit anyway...
1129 mat
->flags
= MAT_DIRTY_DEPENDENTS
;
1133 void gl_LoadMatrixf( GLcontext
*ctx
, const GLfloat
*m
)
1136 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
1138 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
1139 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1141 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
) {
1143 #define M(row,col) m[col*4+row]
1147 GLfloat n
= (c
== 1.0 ? 0.0 : d
/ (c
- 1.0));
1148 GLfloat f
= (c
== -1.0 ? 1.0 : d
/ (c
+ 1.0));
1150 /* Need to keep a stack of near/far values in case the user
1151 * push/pops the projection matrix stack so that we can call
1152 * Driver.NearFar() after a pop.
1154 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = n
;
1155 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = f
;
1157 if (ctx
->Driver
.NearFar
) {
1158 (*ctx
->Driver
.NearFar
)( ctx
, n
, f
);
1166 * Multiply the active matrix by an arbitary matrix.
1168 void gl_MultMatrixf( GLcontext
*ctx
, const GLfloat
*m
)
1171 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1172 matmul4( mat
->m
, mat
->m
, m
);
1173 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1178 * Multiply the active matrix by an arbitary matrix.
1180 void gl_MultMatrixd( GLcontext
*ctx
, const GLdouble
*m
)
1183 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1184 matmul4fd( mat
->m
, mat
->m
, m
);
1185 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1192 * Multiply a matrix by an array of floats with known properties.
1194 void gl_mat_mul_floats( GLmatrix
*mat
, const GLfloat
*m
, GLuint flags
)
1196 mat
->flags
|= (flags
|
1199 MAT_DIRTY_DEPENDENTS
);
1201 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1202 matmul34( mat
->m
, mat
->m
, m
);
1204 matmul4( mat
->m
, mat
->m
, m
);
1209 * Multiply a matrix by an array of floats with known properties.
1211 void gl_mat_mul_mat( GLmatrix
*mat
, const GLmatrix
*m
)
1213 mat
->flags
|= (m
->flags
|
1216 MAT_DIRTY_DEPENDENTS
);
1218 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1219 matmul34( mat
->m
, mat
->m
, m
->m
);
1221 matmul4( mat
->m
, mat
->m
, m
->m
);
1227 * Execute a glRotate call
1229 void gl_Rotatef( GLcontext
*ctx
,
1230 GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
1233 if (angle
!= 0.0F
) {
1235 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
1237 gl_rotation_matrix( angle
, x
, y
, z
, m
);
1238 gl_mat_mul_floats( mat
, m
, MAT_FLAG_ROTATION
);
1243 * Execute a glScale call
1245 void gl_Scalef( GLcontext
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
)
1249 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glScale");
1252 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
1253 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
1254 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
1255 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
1257 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
1258 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1260 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1262 mat
->flags
|= (MAT_DIRTY_TYPE
|
1264 MAT_DIRTY_DEPENDENTS
);
1268 * Execute a glTranslate call
1270 void gl_Translatef( GLcontext
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
)
1274 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glTranslate");
1276 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1277 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1278 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1279 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1281 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
1284 MAT_DIRTY_DEPENDENTS
);
1289 * Define a new viewport and reallocate auxillary buffers if the size of
1290 * the window (color buffer) has changed.
1292 void gl_Viewport( GLcontext
*ctx
,
1293 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1295 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glViewport");
1297 if (width
<0 || height
<0) {
1298 gl_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
1302 if (MESA_VERBOSE
& VERBOSE_API
)
1303 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
1305 /* clamp width, and height to implementation dependent range */
1306 width
= CLAMP( width
, 1, MAX_WIDTH
);
1307 height
= CLAMP( height
, 1, MAX_HEIGHT
);
1310 ctx
->Viewport
.X
= x
;
1311 ctx
->Viewport
.Width
= width
;
1312 ctx
->Viewport
.Y
= y
;
1313 ctx
->Viewport
.Height
= height
;
1315 /* compute scale and bias values */
1316 ctx
->Viewport
.WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
1317 ctx
->Viewport
.WindowMap
.m
[MAT_TX
] = ctx
->Viewport
.WindowMap
.m
[MAT_SX
] + x
;
1318 ctx
->Viewport
.WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
1319 ctx
->Viewport
.WindowMap
.m
[MAT_TY
] = ctx
->Viewport
.WindowMap
.m
[MAT_SY
] + y
;
1320 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = 0.5 * DEPTH_SCALE
;
1321 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = 0.5 * DEPTH_SCALE
;
1323 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
1324 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
1326 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1327 ctx
->NewState
|= NEW_VIEWPORT
;
1329 /* Check if window/buffer has been resized and if so, reallocate the
1330 * ancillary buffers.
1332 gl_ResizeBuffersMESA(ctx
);
1335 ctx
->RasterMask
&= ~WINCLIP_BIT
;
1337 if ( ctx
->Viewport
.X
<0
1338 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->Buffer
->Width
1339 || ctx
->Viewport
.Y
<0
1340 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->Buffer
->Height
) {
1341 ctx
->RasterMask
|= WINCLIP_BIT
;
1345 if (ctx
->Driver
.Viewport
) {
1346 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
1352 void gl_DepthRange( GLcontext
*ctx
, GLclampd nearval
, GLclampd farval
)
1355 * nearval - specifies mapping of the near clipping plane to window
1356 * coordinates, default is 0
1357 * farval - specifies mapping of the far clipping plane to window
1358 * coordinates, default is 1
1360 * After clipping and div by w, z coords are in -1.0 to 1.0,
1361 * corresponding to near and far clipping planes. glDepthRange
1362 * specifies a linear mapping of the normalized z coords in
1363 * this range to window z coords.
1367 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthRange");
1369 if (MESA_VERBOSE
&VERBOSE_API
)
1370 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
1372 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
1373 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
1375 ctx
->Viewport
.Near
= n
;
1376 ctx
->Viewport
.Far
= f
;
1377 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = DEPTH_SCALE
* ((f
- n
) / 2.0);
1378 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = DEPTH_SCALE
* ((f
- n
) / 2.0 + n
);
1380 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1382 if (ctx
->Driver
.DepthRange
) {
1383 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);
1388 void gl_calculate_model_project_matrix( GLcontext
*ctx
)
1390 gl_matrix_mul( &ctx
->ModelProjectMatrix
,
1391 &ctx
->ProjectionMatrix
,
1394 gl_matrix_analyze( &ctx
->ModelProjectMatrix
);
1398 void gl_matrix_ctr( GLmatrix
*m
)
1401 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1402 m
->type
= MATRIX_IDENTITY
;
1403 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1406 void gl_matrix_dtr( GLmatrix
*m
)
1414 void gl_matrix_set_identity( GLmatrix
*m
)
1416 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1417 m
->type
= MATRIX_IDENTITY
;
1418 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1422 void gl_matrix_alloc_inv( GLmatrix
*m
)
1425 m
->inv
= (GLfloat
*)malloc(16*sizeof(GLfloat
));
1426 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
1430 void gl_matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
1432 MEMCPY( to
->m
, from
->m
, sizeof(Identity
));
1433 to
->flags
= from
->flags
| MAT_DIRTY_DEPENDENTS
;
1434 to
->type
= from
->type
;
1437 if (from
->inv
== 0) {
1438 gl_matrix_invert( to
);
1440 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
1445 void gl_matrix_mul( GLmatrix
*dest
, const GLmatrix
*a
, const GLmatrix
*b
)
1447 dest
->flags
= (a
->flags
|
1451 MAT_DIRTY_DEPENDENTS
);
1453 if (TEST_MAT_FLAGS(dest
, MAT_FLAGS_3D
))
1454 matmul34( dest
->m
, a
->m
, b
->m
);
1456 matmul4( dest
->m
, a
->m
, b
->m
);