1 /* $Id: matrix.c,v 1.2 1999/09/05 19:59:33 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) {
916 gl_error( ctx
, GL_INVALID_VALUE
, "glFrustum(near or far)" );
919 x
= (2.0*nearval
) / (right
-left
);
920 y
= (2.0*nearval
) / (top
-bottom
);
921 a
= (right
+left
) / (right
-left
);
922 b
= (top
+bottom
) / (top
-bottom
);
923 c
= -(farval
+nearval
) / ( farval
-nearval
);
924 d
= -(2.0*farval
*nearval
) / (farval
-nearval
); /* error? */
926 #define M(row,col) m[col*4+row]
927 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
928 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
929 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
930 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
934 gl_mat_mul_floats( mat
, m
, MAT_FLAG_PERSPECTIVE
);
937 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
)
939 /* Need to keep a stack of near/far values in case the user push/pops
940 * the projection matrix stack so that we can call Driver.NearFar()
943 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = nearval
;
944 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = farval
;
946 if (ctx
->Driver
.NearFar
) {
947 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
953 void gl_Ortho( GLcontext
*ctx
,
954 GLdouble left
, GLdouble right
,
955 GLdouble bottom
, GLdouble top
,
956 GLdouble nearval
, GLdouble farval
)
963 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
965 x
= 2.0 / (right
-left
);
966 y
= 2.0 / (top
-bottom
);
967 z
= -2.0 / (farval
-nearval
);
968 tx
= -(right
+left
) / (right
-left
);
969 ty
= -(top
+bottom
) / (top
-bottom
);
970 tz
= -(farval
+nearval
) / (farval
-nearval
);
972 #define M(row,col) m[col*4+row]
973 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = 0.0F
; M(0,3) = tx
;
974 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = 0.0F
; M(1,3) = ty
;
975 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = z
; M(2,3) = tz
;
976 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = 0.0F
; M(3,3) = 1.0F
;
979 gl_mat_mul_floats( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
981 if (ctx
->Driver
.NearFar
) {
982 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
987 void gl_MatrixMode( GLcontext
*ctx
, GLenum mode
)
989 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glMatrixMode");
994 ctx
->Transform
.MatrixMode
= mode
;
997 gl_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
1003 void gl_PushMatrix( GLcontext
*ctx
)
1005 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushMatrix");
1007 if (MESA_VERBOSE
&VERBOSE_API
)
1008 fprintf(stderr
, "glPushMatrix %s\n",
1009 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1011 switch (ctx
->Transform
.MatrixMode
) {
1013 if (ctx
->ModelViewStackDepth
>=MAX_MODELVIEW_STACK_DEPTH
-1) {
1014 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1017 gl_matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
1021 if (ctx
->ProjectionStackDepth
>=MAX_PROJECTION_STACK_DEPTH
) {
1022 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1025 gl_matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
1026 &ctx
->ProjectionMatrix
);
1028 /* Save near and far projection values */
1029 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0]
1030 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][0];
1031 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1]
1032 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][1];
1036 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1037 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
) {
1038 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1041 gl_matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
1042 &ctx
->TextureMatrix
[t
] );
1046 gl_problem(ctx
, "Bad matrix mode in gl_PushMatrix");
1052 void gl_PopMatrix( GLcontext
*ctx
)
1054 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopMatrix");
1056 if (MESA_VERBOSE
&VERBOSE_API
)
1057 fprintf(stderr
, "glPopMatrix %s\n",
1058 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1060 switch (ctx
->Transform
.MatrixMode
) {
1062 if (ctx
->ModelViewStackDepth
==0) {
1063 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1066 gl_matrix_copy( &ctx
->ModelView
,
1067 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
1068 ctx
->NewState
|= NEW_MODELVIEW
;
1071 if (ctx
->ProjectionStackDepth
==0) {
1072 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1076 gl_matrix_copy( &ctx
->ProjectionMatrix
,
1077 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
1078 ctx
->NewState
|= NEW_PROJECTION
;
1080 /* Device driver near/far values */
1082 GLfloat nearVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0];
1083 GLfloat farVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1];
1084 if (ctx
->Driver
.NearFar
) {
1085 (*ctx
->Driver
.NearFar
)( ctx
, nearVal
, farVal
);
1091 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1092 if (ctx
->TextureStackDepth
[t
]==0) {
1093 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1096 gl_matrix_copy(&ctx
->TextureMatrix
[t
],
1097 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
1101 gl_problem(ctx
, "Bad matrix mode in gl_PopMatrix");
1107 void gl_LoadIdentity( GLcontext
*ctx
)
1110 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
1112 MEMCPY( mat
->m
, Identity
, 16*sizeof(GLfloat
) );
1115 MEMCPY( mat
->inv
, Identity
, 16*sizeof(GLfloat
) );
1117 mat
->type
= MATRIX_IDENTITY
;
1119 /* Have to set this to dirty to make sure we recalculate the
1120 * combined matrix later. The update_matrix in this case is a
1121 * shortcircuit anyway...
1123 mat
->flags
= MAT_DIRTY_DEPENDENTS
;
1127 void gl_LoadMatrixf( GLcontext
*ctx
, const GLfloat
*m
)
1130 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
1132 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
1133 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1135 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
) {
1137 #define M(row,col) m[col*4+row]
1141 GLfloat n
= (c
== 1.0 ? 0.0 : d
/ (c
- 1.0));
1142 GLfloat f
= (c
== -1.0 ? 1.0 : d
/ (c
+ 1.0));
1144 /* Need to keep a stack of near/far values in case the user
1145 * push/pops the projection matrix stack so that we can call
1146 * Driver.NearFar() after a pop.
1148 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = n
;
1149 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = f
;
1151 if (ctx
->Driver
.NearFar
) {
1152 (*ctx
->Driver
.NearFar
)( ctx
, n
, f
);
1160 * Multiply the active matrix by an arbitary matrix.
1162 void gl_MultMatrixf( GLcontext
*ctx
, const GLfloat
*m
)
1165 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1166 matmul4( mat
->m
, mat
->m
, m
);
1167 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1172 * Multiply the active matrix by an arbitary matrix.
1174 void gl_MultMatrixd( GLcontext
*ctx
, const GLdouble
*m
)
1177 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1178 matmul4fd( mat
->m
, mat
->m
, m
);
1179 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1186 * Multiply a matrix by an array of floats with known properties.
1188 void gl_mat_mul_floats( GLmatrix
*mat
, const GLfloat
*m
, GLuint flags
)
1190 mat
->flags
|= (flags
|
1193 MAT_DIRTY_DEPENDENTS
);
1195 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1196 matmul34( mat
->m
, mat
->m
, m
);
1198 matmul4( mat
->m
, mat
->m
, m
);
1203 * Multiply a matrix by an array of floats with known properties.
1205 void gl_mat_mul_mat( GLmatrix
*mat
, const GLmatrix
*m
)
1207 mat
->flags
|= (m
->flags
|
1210 MAT_DIRTY_DEPENDENTS
);
1212 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1213 matmul34( mat
->m
, mat
->m
, m
->m
);
1215 matmul4( mat
->m
, mat
->m
, m
->m
);
1221 * Execute a glRotate call
1223 void gl_Rotatef( GLcontext
*ctx
,
1224 GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
1227 if (angle
!= 0.0F
) {
1229 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
1231 gl_rotation_matrix( angle
, x
, y
, z
, m
);
1232 gl_mat_mul_floats( mat
, m
, MAT_FLAG_ROTATION
);
1237 * Execute a glScale call
1239 void gl_Scalef( GLcontext
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
)
1243 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glScale");
1246 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
1247 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
1248 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
1249 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
1251 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
1252 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1254 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1256 mat
->flags
|= (MAT_DIRTY_TYPE
|
1258 MAT_DIRTY_DEPENDENTS
);
1262 * Execute a glTranslate call
1264 void gl_Translatef( GLcontext
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
)
1268 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glTranslate");
1270 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1271 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1272 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1273 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1275 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
1278 MAT_DIRTY_DEPENDENTS
);
1283 * Define a new viewport and reallocate auxillary buffers if the size of
1284 * the window (color buffer) has changed.
1286 void gl_Viewport( GLcontext
*ctx
,
1287 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1289 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glViewport");
1291 if (width
<0 || height
<0) {
1292 gl_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
1296 if (MESA_VERBOSE
& VERBOSE_API
)
1297 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
1299 /* clamp width, and height to implementation dependent range */
1300 width
= CLAMP( width
, 1, MAX_WIDTH
);
1301 height
= CLAMP( height
, 1, MAX_HEIGHT
);
1304 ctx
->Viewport
.X
= x
;
1305 ctx
->Viewport
.Width
= width
;
1306 ctx
->Viewport
.Y
= y
;
1307 ctx
->Viewport
.Height
= height
;
1309 /* compute scale and bias values */
1310 ctx
->Viewport
.WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
1311 ctx
->Viewport
.WindowMap
.m
[MAT_TX
] = ctx
->Viewport
.WindowMap
.m
[MAT_SX
] + x
;
1312 ctx
->Viewport
.WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
1313 ctx
->Viewport
.WindowMap
.m
[MAT_TY
] = ctx
->Viewport
.WindowMap
.m
[MAT_SY
] + y
;
1314 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = 0.5 * DEPTH_SCALE
;
1315 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = 0.5 * DEPTH_SCALE
;
1317 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
1318 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
1320 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1321 ctx
->NewState
|= NEW_VIEWPORT
;
1323 /* Check if window/buffer has been resized and if so, reallocate the
1324 * ancillary buffers.
1326 gl_ResizeBuffersMESA(ctx
);
1329 ctx
->RasterMask
&= WINCLIP_BIT
;
1331 if ( ctx
->Viewport
.X
<0
1332 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->Buffer
->Width
1333 || ctx
->Viewport
.Y
<0
1334 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->Buffer
->Height
) {
1335 ctx
->RasterMask
|= WINCLIP_BIT
;
1339 if (ctx
->Driver
.Viewport
) {
1340 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
1346 void gl_DepthRange( GLcontext
*ctx
, GLclampd nearval
, GLclampd farval
)
1349 * nearval - specifies mapping of the near clipping plane to window
1350 * coordinates, default is 0
1351 * farval - specifies mapping of the far clipping plane to window
1352 * coordinates, default is 1
1354 * After clipping and div by w, z coords are in -1.0 to 1.0,
1355 * corresponding to near and far clipping planes. glDepthRange
1356 * specifies a linear mapping of the normalized z coords in
1357 * this range to window z coords.
1361 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthRange");
1363 if (MESA_VERBOSE
&VERBOSE_API
)
1364 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
1366 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
1367 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
1369 ctx
->Viewport
.Near
= n
;
1370 ctx
->Viewport
.Far
= f
;
1371 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = DEPTH_SCALE
* ((f
- n
) / 2.0);
1372 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = DEPTH_SCALE
* ((f
- n
) / 2.0 + n
);
1374 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1376 if (ctx
->Driver
.DepthRange
) {
1377 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);
1382 void gl_calculate_model_project_matrix( GLcontext
*ctx
)
1384 gl_matrix_mul( &ctx
->ModelProjectMatrix
,
1385 &ctx
->ProjectionMatrix
,
1388 gl_matrix_analyze( &ctx
->ModelProjectMatrix
);
1392 void gl_matrix_ctr( GLmatrix
*m
)
1395 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1396 m
->type
= MATRIX_IDENTITY
;
1397 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1400 void gl_matrix_dtr( GLmatrix
*m
)
1408 void gl_matrix_set_identity( GLmatrix
*m
)
1410 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1411 m
->type
= MATRIX_IDENTITY
;
1412 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1416 void gl_matrix_alloc_inv( GLmatrix
*m
)
1419 m
->inv
= (GLfloat
*)malloc(16*sizeof(GLfloat
));
1420 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
1424 void gl_matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
1426 MEMCPY( to
->m
, from
->m
, sizeof(Identity
));
1427 to
->flags
= from
->flags
| MAT_DIRTY_DEPENDENTS
;
1428 to
->type
= from
->type
;
1431 if (from
->inv
== 0) {
1432 gl_matrix_invert( to
);
1434 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
1439 void gl_matrix_mul( GLmatrix
*dest
, const GLmatrix
*a
, const GLmatrix
*b
)
1441 dest
->flags
= (a
->flags
|
1445 MAT_DIRTY_DEPENDENTS
);
1447 if (TEST_MAT_FLAGS(dest
, MAT_FLAGS_3D
))
1448 matmul34( dest
->m
, a
->m
, b
->m
);
1450 matmul4( dest
->m
, a
->m
, b
->m
);