1 /* $Id: matrix.c,v 1.16 2000/03/03 17:47:39 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
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
54 static const char *types
[] = {
63 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
);
66 static GLfloat Identity
[16] = {
74 static void print_matrix_floats( const GLfloat m
[16] )
78 fprintf(stderr
,"\t%f %f %f %f\n", m
[i
], m
[4+i
], m
[8+i
], m
[12+i
] );
82 void gl_print_matrix( const GLmatrix
*m
)
84 fprintf(stderr
, "Matrix type: %s, flags: %x\n", types
[m
->type
], m
->flags
);
85 print_matrix_floats(m
->m
);
87 fprintf(stderr
, "Inverse: \n");
90 print_matrix_floats(m
->inv
);
91 matmul4(prod
, m
->m
, m
->inv
);
92 fprintf(stderr
, "Mat * Inverse:\n");
93 print_matrix_floats(prod
);
95 fprintf(stderr
, " - not available\n");
102 * This matmul was contributed by Thomas Malik
104 * Perform a 4x4 matrix multiplication (product = a x b).
105 * Input: a, b - matrices to multiply
106 * Output: product - product of a and b
107 * WARNING: (product != b) assumed
108 * NOTE: (product == a) allowed
112 #define A(row,col) a[(col<<2)+row]
113 #define B(row,col) b[(col<<2)+row]
114 #define P(row,col) product[(col<<2)+row]
116 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
119 for (i
= 0; i
< 4; i
++) {
120 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
121 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
122 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
123 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
124 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
131 /* Multiply two matrices known to occupy only the top three rows,
132 * such as typical modelling matrices, and ortho matrices.
136 static void matmul34( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
139 for (i
= 0; i
< 3; i
++) {
140 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
141 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0);
142 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1);
143 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2);
144 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
;
152 static void matmul4fd( GLfloat
*product
, const GLfloat
*a
, const GLdouble
*b
)
155 for (i
= 0; i
< 4; i
++) {
156 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
157 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
158 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
159 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
160 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
170 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
171 #define MAT(m,r,c) (m)[(c)*4+(r)]
174 * Compute inverse of 4x4 transformation matrix.
175 * Code contributed by Jacques Leroy jle@star.be
176 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
178 static GLboolean
invert_matrix_general( GLmatrix
*mat
)
180 const GLfloat
*m
= mat
->m
;
181 GLfloat
*out
= mat
->inv
;
183 GLfloat m0
, m1
, m2
, m3
, s
;
184 GLfloat
*r0
, *r1
, *r2
, *r3
;
186 r0
= wtmp
[0], r1
= wtmp
[1], r2
= wtmp
[2], r3
= wtmp
[3];
188 r0
[0] = MAT(m
,0,0), r0
[1] = MAT(m
,0,1),
189 r0
[2] = MAT(m
,0,2), r0
[3] = MAT(m
,0,3),
190 r0
[4] = 1.0, r0
[5] = r0
[6] = r0
[7] = 0.0,
192 r1
[0] = MAT(m
,1,0), r1
[1] = MAT(m
,1,1),
193 r1
[2] = MAT(m
,1,2), r1
[3] = MAT(m
,1,3),
194 r1
[5] = 1.0, r1
[4] = r1
[6] = r1
[7] = 0.0,
196 r2
[0] = MAT(m
,2,0), r2
[1] = MAT(m
,2,1),
197 r2
[2] = MAT(m
,2,2), r2
[3] = MAT(m
,2,3),
198 r2
[6] = 1.0, r2
[4] = r2
[5] = r2
[7] = 0.0,
200 r3
[0] = MAT(m
,3,0), r3
[1] = MAT(m
,3,1),
201 r3
[2] = MAT(m
,3,2), r3
[3] = MAT(m
,3,3),
202 r3
[7] = 1.0, r3
[4] = r3
[5] = r3
[6] = 0.0;
204 /* choose pivot - or die */
205 if (fabs(r3
[0])>fabs(r2
[0])) SWAP_ROWS(r3
, r2
);
206 if (fabs(r2
[0])>fabs(r1
[0])) SWAP_ROWS(r2
, r1
);
207 if (fabs(r1
[0])>fabs(r0
[0])) SWAP_ROWS(r1
, r0
);
208 if (0.0 == r0
[0]) return GL_FALSE
;
210 /* eliminate first variable */
211 m1
= r1
[0]/r0
[0]; m2
= r2
[0]/r0
[0]; m3
= r3
[0]/r0
[0];
212 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
213 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
214 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
216 if (s
!= 0.0) { r1
[4] -= m1
* s
; r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
218 if (s
!= 0.0) { r1
[5] -= m1
* s
; r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
220 if (s
!= 0.0) { r1
[6] -= m1
* s
; r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
222 if (s
!= 0.0) { r1
[7] -= m1
* s
; r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
224 /* choose pivot - or die */
225 if (fabs(r3
[1])>fabs(r2
[1])) SWAP_ROWS(r3
, r2
);
226 if (fabs(r2
[1])>fabs(r1
[1])) SWAP_ROWS(r2
, r1
);
227 if (0.0 == r1
[1]) return GL_FALSE
;
229 /* eliminate second variable */
230 m2
= r2
[1]/r1
[1]; m3
= r3
[1]/r1
[1];
231 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
232 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
233 s
= r1
[4]; if (0.0 != s
) { r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
234 s
= r1
[5]; if (0.0 != s
) { r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
235 s
= r1
[6]; if (0.0 != s
) { r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
236 s
= r1
[7]; if (0.0 != s
) { r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
238 /* choose pivot - or die */
239 if (fabs(r3
[2])>fabs(r2
[2])) SWAP_ROWS(r3
, r2
);
240 if (0.0 == r2
[2]) return GL_FALSE
;
242 /* eliminate third variable */
244 r3
[3] -= m3
* r2
[3], r3
[4] -= m3
* r2
[4],
245 r3
[5] -= m3
* r2
[5], r3
[6] -= m3
* r2
[6],
249 if (0.0 == r3
[3]) return GL_FALSE
;
251 s
= 1.0/r3
[3]; /* now back substitute row 3 */
252 r3
[4] *= s
; r3
[5] *= s
; r3
[6] *= s
; r3
[7] *= s
;
254 m2
= r2
[3]; /* now back substitute row 2 */
256 r2
[4] = s
* (r2
[4] - r3
[4] * m2
), r2
[5] = s
* (r2
[5] - r3
[5] * m2
),
257 r2
[6] = s
* (r2
[6] - r3
[6] * m2
), r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
259 r1
[4] -= r3
[4] * m1
, r1
[5] -= r3
[5] * m1
,
260 r1
[6] -= r3
[6] * m1
, r1
[7] -= r3
[7] * m1
;
262 r0
[4] -= r3
[4] * m0
, r0
[5] -= r3
[5] * m0
,
263 r0
[6] -= r3
[6] * m0
, r0
[7] -= r3
[7] * m0
;
265 m1
= r1
[2]; /* now back substitute row 1 */
267 r1
[4] = s
* (r1
[4] - r2
[4] * m1
), r1
[5] = s
* (r1
[5] - r2
[5] * m1
),
268 r1
[6] = s
* (r1
[6] - r2
[6] * m1
), r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
270 r0
[4] -= r2
[4] * m0
, r0
[5] -= r2
[5] * m0
,
271 r0
[6] -= r2
[6] * m0
, r0
[7] -= r2
[7] * m0
;
273 m0
= r0
[1]; /* now back substitute row 0 */
275 r0
[4] = s
* (r0
[4] - r1
[4] * m0
), r0
[5] = s
* (r0
[5] - r1
[5] * m0
),
276 r0
[6] = s
* (r0
[6] - r1
[6] * m0
), r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
278 MAT(out
,0,0) = r0
[4]; MAT(out
,0,1) = r0
[5],
279 MAT(out
,0,2) = r0
[6]; MAT(out
,0,3) = r0
[7],
280 MAT(out
,1,0) = r1
[4]; MAT(out
,1,1) = r1
[5],
281 MAT(out
,1,2) = r1
[6]; MAT(out
,1,3) = r1
[7],
282 MAT(out
,2,0) = r2
[4]; MAT(out
,2,1) = r2
[5],
283 MAT(out
,2,2) = r2
[6]; MAT(out
,2,3) = r2
[7],
284 MAT(out
,3,0) = r3
[4]; MAT(out
,3,1) = r3
[5],
285 MAT(out
,3,2) = r3
[6]; MAT(out
,3,3) = r3
[7];
291 /* Adapted from graphics gems II.
293 static GLboolean
invert_matrix_3d_general( GLmatrix
*mat
)
295 const GLfloat
*in
= mat
->m
;
296 GLfloat
*out
= mat
->inv
;
300 /* Calculate the determinant of upper left 3x3 submatrix and
301 * determine if the matrix is singular.
304 t
= MAT(in
,0,0) * MAT(in
,1,1) * MAT(in
,2,2);
305 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
307 t
= MAT(in
,1,0) * MAT(in
,2,1) * MAT(in
,0,2);
308 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
310 t
= MAT(in
,2,0) * MAT(in
,0,1) * MAT(in
,1,2);
311 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
313 t
= -MAT(in
,2,0) * MAT(in
,1,1) * MAT(in
,0,2);
314 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
316 t
= -MAT(in
,1,0) * MAT(in
,0,1) * MAT(in
,2,2);
317 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
319 t
= -MAT(in
,0,0) * MAT(in
,2,1) * MAT(in
,1,2);
320 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
328 MAT(out
,0,0) = ( (MAT(in
,1,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,1,2) )*det
);
329 MAT(out
,0,1) = (- (MAT(in
,0,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,0,2) )*det
);
330 MAT(out
,0,2) = ( (MAT(in
,0,1)*MAT(in
,1,2) - MAT(in
,1,1)*MAT(in
,0,2) )*det
);
331 MAT(out
,1,0) = (- (MAT(in
,1,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,1,2) )*det
);
332 MAT(out
,1,1) = ( (MAT(in
,0,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,0,2) )*det
);
333 MAT(out
,1,2) = (- (MAT(in
,0,0)*MAT(in
,1,2) - MAT(in
,1,0)*MAT(in
,0,2) )*det
);
334 MAT(out
,2,0) = ( (MAT(in
,1,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,1,1) )*det
);
335 MAT(out
,2,1) = (- (MAT(in
,0,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,0,1) )*det
);
336 MAT(out
,2,2) = ( (MAT(in
,0,0)*MAT(in
,1,1) - MAT(in
,1,0)*MAT(in
,0,1) )*det
);
338 /* Do the translation part */
339 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
340 MAT(in
,1,3) * MAT(out
,0,1) +
341 MAT(in
,2,3) * MAT(out
,0,2) );
342 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
343 MAT(in
,1,3) * MAT(out
,1,1) +
344 MAT(in
,2,3) * MAT(out
,1,2) );
345 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
346 MAT(in
,1,3) * MAT(out
,2,1) +
347 MAT(in
,2,3) * MAT(out
,2,2) );
353 static GLboolean
invert_matrix_3d( GLmatrix
*mat
)
355 const GLfloat
*in
= mat
->m
;
356 GLfloat
*out
= mat
->inv
;
358 if (!TEST_MAT_FLAGS(mat
, MAT_FLAGS_ANGLE_PRESERVING
))
360 return invert_matrix_3d_general( mat
);
363 if (mat
->flags
& MAT_FLAG_UNIFORM_SCALE
)
365 GLfloat scale
= (MAT(in
,0,0) * MAT(in
,0,0) +
366 MAT(in
,0,1) * MAT(in
,0,1) +
367 MAT(in
,0,2) * MAT(in
,0,2));
374 /* Transpose and scale the 3 by 3 upper-left submatrix. */
375 MAT(out
,0,0) = scale
* MAT(in
,0,0);
376 MAT(out
,1,0) = scale
* MAT(in
,0,1);
377 MAT(out
,2,0) = scale
* MAT(in
,0,2);
378 MAT(out
,0,1) = scale
* MAT(in
,1,0);
379 MAT(out
,1,1) = scale
* MAT(in
,1,1);
380 MAT(out
,2,1) = scale
* MAT(in
,1,2);
381 MAT(out
,0,2) = scale
* MAT(in
,2,0);
382 MAT(out
,1,2) = scale
* MAT(in
,2,1);
383 MAT(out
,2,2) = scale
* MAT(in
,2,2);
385 else if (mat
->flags
& MAT_FLAG_ROTATION
)
387 /* Transpose the 3 by 3 upper-left submatrix. */
388 MAT(out
,0,0) = MAT(in
,0,0);
389 MAT(out
,1,0) = MAT(in
,0,1);
390 MAT(out
,2,0) = MAT(in
,0,2);
391 MAT(out
,0,1) = MAT(in
,1,0);
392 MAT(out
,1,1) = MAT(in
,1,1);
393 MAT(out
,2,1) = MAT(in
,1,2);
394 MAT(out
,0,2) = MAT(in
,2,0);
395 MAT(out
,1,2) = MAT(in
,2,1);
396 MAT(out
,2,2) = MAT(in
,2,2);
398 else /* pure translation */
400 MEMCPY( out
, Identity
, sizeof(Identity
) );
401 MAT(out
,0,3) = - MAT(in
,0,3);
402 MAT(out
,1,3) = - MAT(in
,1,3);
403 MAT(out
,2,3) = - MAT(in
,2,3);
407 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
409 /* Do the translation part */
410 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
411 MAT(in
,1,3) * MAT(out
,0,1) +
412 MAT(in
,2,3) * MAT(out
,0,2) );
413 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
414 MAT(in
,1,3) * MAT(out
,1,1) +
415 MAT(in
,2,3) * MAT(out
,1,2) );
416 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
417 MAT(in
,1,3) * MAT(out
,2,1) +
418 MAT(in
,2,3) * MAT(out
,2,2) );
422 MAT(out
,0,3) = MAT(out
,1,3) = MAT(out
,2,3) = 0.0;
430 static GLboolean
invert_matrix_identity( GLmatrix
*mat
)
432 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
437 static GLboolean
invert_matrix_3d_no_rot( GLmatrix
*mat
)
439 const GLfloat
*in
= mat
->m
;
440 GLfloat
*out
= mat
->inv
;
442 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0 || MAT(in
,2,2) == 0 )
445 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
446 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
447 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
448 MAT(out
,2,2) = 1.0 / MAT(in
,2,2);
450 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
452 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
453 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
454 MAT(out
,2,3) = - (MAT(in
,2,3) * MAT(out
,2,2));
461 static GLboolean
invert_matrix_2d_no_rot( GLmatrix
*mat
)
463 const GLfloat
*in
= mat
->m
;
464 GLfloat
*out
= mat
->inv
;
466 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0)
469 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
470 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
471 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
473 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
475 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
476 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
483 static GLboolean
invert_matrix_perspective( GLmatrix
*mat
)
485 const GLfloat
*in
= mat
->m
;
486 GLfloat
*out
= mat
->inv
;
488 if (MAT(in
,2,3) == 0)
491 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
493 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
494 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
496 MAT(out
,0,3) = MAT(in
,0,2);
497 MAT(out
,1,3) = MAT(in
,1,2);
502 MAT(out
,3,2) = 1.0 / MAT(in
,2,3);
503 MAT(out
,3,3) = MAT(in
,2,2) * MAT(out
,3,2);
509 typedef GLboolean (*inv_mat_func
)( GLmatrix
*mat
);
511 static inv_mat_func inv_mat_tab
[7] = {
512 invert_matrix_general
,
513 invert_matrix_identity
,
514 invert_matrix_3d_no_rot
,
515 invert_matrix_perspective
,
516 invert_matrix_3d
, /* lazy! */
517 invert_matrix_2d_no_rot
,
522 GLboolean
gl_matrix_invert( GLmatrix
*mat
)
524 if (inv_mat_tab
[mat
->type
](mat
)) {
526 GLmatrix m
; m
.inv
= 0; m
.type
= 0; m
.flags
= 0;
527 matmul4( m
.m
, mat
->m
, mat
->inv
);
528 printf("inverted matrix of type %s:\n", types
[mat
->type
]);
529 gl_print_matrix( mat
);
530 gl_print_matrix( &m
);
534 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
541 void gl_matrix_transposef( GLfloat to
[16], const GLfloat from
[16] )
563 void gl_matrix_transposed( GLdouble to
[16], const GLdouble from
[16] )
586 * Generate a 4x4 transformation matrix from glRotate parameters.
588 void gl_rotation_matrix( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
,
591 /* This function contributed by Erich Boleyn (erich@uruk.org) */
593 GLfloat xx
, yy
, zz
, xy
, yz
, zx
, xs
, ys
, zs
, one_c
;
595 s
= sin( angle
* DEG2RAD
);
596 c
= cos( angle
* DEG2RAD
);
598 mag
= GL_SQRT( x
*x
+ y
*y
+ z
*z
);
601 /* generate an identity matrix and return */
602 MEMCPY(m
, Identity
, sizeof(GLfloat
)*16);
610 #define M(row,col) m[col*4+row]
613 * Arbitrary axis rotation matrix.
615 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
616 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
617 * (which is about the X-axis), and the two composite transforms
618 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
619 * from the arbitrary axis to the X-axis then back. They are
620 * all elementary rotations.
622 * Rz' is a rotation about the Z-axis, to bring the axis vector
623 * into the x-z plane. Then Ry' is applied, rotating about the
624 * Y-axis to bring the axis vector parallel with the X-axis. The
625 * rotation about the X-axis is then performed. Ry and Rz are
626 * simply the respective inverse transforms to bring the arbitrary
627 * axis back to it's original orientation. The first transforms
628 * Rz' and Ry' are considered inverses, since the data from the
629 * arbitrary axis gives you info on how to get to it, not how
630 * to get away from it, and an inverse must be applied.
632 * The basic calculation used is to recognize that the arbitrary
633 * axis vector (x, y, z), since it is of unit length, actually
634 * represents the sines and cosines of the angles to rotate the
635 * X-axis to the same orientation, with theta being the angle about
636 * Z and phi the angle about Y (in the order described above)
639 * cos ( theta ) = x / sqrt ( 1 - z^2 )
640 * sin ( theta ) = y / sqrt ( 1 - z^2 )
642 * cos ( phi ) = sqrt ( 1 - z^2 )
645 * Note that cos ( phi ) can further be inserted to the above
648 * cos ( theta ) = x / cos ( phi )
649 * sin ( theta ) = y / sin ( phi )
651 * ...etc. Because of those relations and the standard trigonometric
652 * relations, it is pssible to reduce the transforms down to what
653 * is used below. It may be that any primary axis chosen will give the
654 * same results (modulo a sign convention) using thie method.
656 * Particularly nice is to notice that all divisions that might
657 * have caused trouble when parallel to certain planes or
658 * axis go away with care paid to reducing the expressions.
659 * After checking, it does perform correctly under all cases, since
660 * in all the cases of division where the denominator would have
661 * been zero, the numerator would have been zero as well, giving
662 * the expected result.
676 M(0,0) = (one_c
* xx
) + c
;
677 M(0,1) = (one_c
* xy
) - zs
;
678 M(0,2) = (one_c
* zx
) + ys
;
681 M(1,0) = (one_c
* xy
) + zs
;
682 M(1,1) = (one_c
* yy
) + c
;
683 M(1,2) = (one_c
* yz
) - xs
;
686 M(2,0) = (one_c
* zx
) - ys
;
687 M(2,1) = (one_c
* yz
) + xs
;
688 M(2,2) = (one_c
* zz
) + c
;
699 #define ZERO(x) (1<<x)
700 #define ONE(x) (1<<(x+16))
702 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
703 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
705 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
706 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
707 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
708 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
710 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
711 ZERO(1) | ZERO(9) | \
712 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
713 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
715 #define MASK_2D ( ZERO(8) | \
717 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
718 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
721 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
722 ZERO(1) | ZERO(9) | \
723 ZERO(2) | ZERO(6) | \
724 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
729 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
732 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
733 ZERO(1) | ZERO(13) |\
734 ZERO(2) | ZERO(6) | \
735 ZERO(3) | ZERO(7) | ZERO(15) )
737 #define SQ(x) ((x)*(x))
739 /* Determine type and flags from scratch. This is expensive enough to
740 * only want to do it once.
742 static void analyze_from_scratch( GLmatrix
*mat
)
744 const GLfloat
*m
= mat
->m
;
748 for (i
= 0 ; i
< 16 ; i
++)
750 if (m
[i
] == 0.0) mask
|= (1<<i
);
753 if (m
[0] == 1.0F
) mask
|= (1<<16);
754 if (m
[5] == 1.0F
) mask
|= (1<<21);
755 if (m
[10] == 1.0F
) mask
|= (1<<26);
756 if (m
[15] == 1.0F
) mask
|= (1<<31);
758 mat
->flags
&= ~MAT_FLAGS_GEOMETRY
;
760 /* Check for translation - no-one really cares
762 if ((mask
& MASK_NO_TRX
) != MASK_NO_TRX
)
763 mat
->flags
|= MAT_FLAG_TRANSLATION
;
767 if (mask
== MASK_IDENTITY
) {
768 mat
->type
= MATRIX_IDENTITY
;
770 else if ((mask
& MASK_2D_NO_ROT
) == MASK_2D_NO_ROT
)
772 mat
->type
= MATRIX_2D_NO_ROT
;
774 if ((mask
& MASK_NO_2D_SCALE
) != MASK_NO_2D_SCALE
)
775 mat
->flags
= MAT_FLAG_GENERAL_SCALE
;
777 else if ((mask
& MASK_2D
) == MASK_2D
)
779 GLfloat mm
= DOT2(m
, m
);
780 GLfloat m4m4
= DOT2(m
+4,m
+4);
781 GLfloat mm4
= DOT2(m
,m
+4);
783 mat
->type
= MATRIX_2D
;
785 /* Check for scale */
786 if (SQ(mm
-1) > SQ(1e-6) ||
787 SQ(m4m4
-1) > SQ(1e-6))
788 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
790 /* Check for rotation */
791 if (SQ(mm4
) > SQ(1e-6))
792 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
794 mat
->flags
|= MAT_FLAG_ROTATION
;
797 else if ((mask
& MASK_3D_NO_ROT
) == MASK_3D_NO_ROT
)
799 mat
->type
= MATRIX_3D_NO_ROT
;
801 /* Check for scale */
802 if (SQ(m
[0]-m
[5]) < SQ(1e-6) &&
803 SQ(m
[0]-m
[10]) < SQ(1e-6)) {
804 if (SQ(m
[0]-1.0) > SQ(1e-6))
805 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
807 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
809 else if ((mask
& MASK_3D
) == MASK_3D
)
811 GLfloat c1
= DOT3(m
,m
);
812 GLfloat c2
= DOT3(m
+4,m
+4);
813 GLfloat c3
= DOT3(m
+8,m
+8);
814 GLfloat d1
= DOT3(m
, m
+4);
817 mat
->type
= MATRIX_3D
;
819 /* Check for scale */
820 if (SQ(c1
-c2
) < SQ(1e-6) && SQ(c1
-c3
) < SQ(1e-6)) {
821 if (SQ(c1
-1.0) > SQ(1e-6))
822 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
823 /* else no scale at all */
825 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
827 /* Check for rotation */
828 if (SQ(d1
) < SQ(1e-6)) {
829 CROSS3( cp
, m
, m
+4 );
830 SUB_3V( cp
, cp
, (m
+8) );
831 if (LEN_SQUARED_3FV(cp
) < SQ(1e-6))
832 mat
->flags
|= MAT_FLAG_ROTATION
;
834 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
837 mat
->flags
|= MAT_FLAG_GENERAL_3D
; /* shear, etc */
839 else if ((mask
& MASK_PERSPECTIVE
) == MASK_PERSPECTIVE
&& m
[11]==-1.0F
)
841 mat
->type
= MATRIX_PERSPECTIVE
;
842 mat
->flags
|= MAT_FLAG_GENERAL
;
845 mat
->type
= MATRIX_GENERAL
;
846 mat
->flags
|= MAT_FLAG_GENERAL
;
851 /* Analyse a matrix given that its flags are accurate - this is the
852 * more common operation, hopefully.
854 static void analyze_from_flags( GLmatrix
*mat
)
856 const GLfloat
*m
= mat
->m
;
858 if (TEST_MAT_FLAGS(mat
, 0)) {
859 mat
->type
= MATRIX_IDENTITY
;
861 else if (TEST_MAT_FLAGS(mat
, (MAT_FLAG_TRANSLATION
|
862 MAT_FLAG_UNIFORM_SCALE
|
863 MAT_FLAG_GENERAL_SCALE
)))
865 if ( m
[10]==1.0F
&& m
[14]==0.0F
) {
866 mat
->type
= MATRIX_2D_NO_ROT
;
869 mat
->type
= MATRIX_3D_NO_ROT
;
872 else if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
)) {
875 && m
[2]==0.0F
&& m
[6]==0.0F
&& m
[10]==1.0F
&& m
[14]==0.0F
)
877 mat
->type
= MATRIX_2D
;
881 mat
->type
= MATRIX_3D
;
884 else if ( m
[4]==0.0F
&& m
[12]==0.0F
885 && m
[1]==0.0F
&& m
[13]==0.0F
886 && m
[2]==0.0F
&& m
[6]==0.0F
887 && m
[3]==0.0F
&& m
[7]==0.0F
&& m
[11]==-1.0F
&& m
[15]==0.0F
)
889 mat
->type
= MATRIX_PERSPECTIVE
;
892 mat
->type
= MATRIX_GENERAL
;
898 void gl_matrix_analyze( GLmatrix
*mat
)
900 if (mat
->flags
& MAT_DIRTY_TYPE
) {
901 if (mat
->flags
& MAT_DIRTY_FLAGS
)
902 analyze_from_scratch( mat
);
904 analyze_from_flags( mat
);
907 if (mat
->inv
&& (mat
->flags
& MAT_DIRTY_INVERSE
)) {
908 gl_matrix_invert( mat
);
911 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
917 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
919 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where); \
920 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
921 switch (ctx->Transform.MatrixMode) { \
923 mat = &ctx->ModelView; \
924 flags |= NEW_MODELVIEW; \
926 case GL_PROJECTION: \
927 mat = &ctx->ProjectionMatrix; \
928 flags |= NEW_PROJECTION; \
931 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
932 flags |= NEW_TEXTURE_MATRIX; \
935 gl_problem(ctx, where); \
941 _mesa_Frustum( GLdouble left
, GLdouble right
,
942 GLdouble bottom
, GLdouble top
,
943 GLdouble nearval
, GLdouble farval
)
945 GET_CURRENT_CONTEXT(ctx
);
946 GLfloat x
, y
, a
, b
, c
, d
;
950 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glFrustrum" );
952 if ((nearval
<=0.0 || farval
<=0.0) || (nearval
== farval
) || (left
== right
) || (top
== bottom
)) {
953 gl_error( ctx
, GL_INVALID_VALUE
, "glFrustum(near or far)" );
957 x
= (2.0*nearval
) / (right
-left
);
958 y
= (2.0*nearval
) / (top
-bottom
);
959 a
= (right
+left
) / (right
-left
);
960 b
= (top
+bottom
) / (top
-bottom
);
961 c
= -(farval
+nearval
) / ( farval
-nearval
);
962 d
= -(2.0*farval
*nearval
) / (farval
-nearval
); /* error? */
964 #define M(row,col) m[col*4+row]
965 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
966 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
967 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
968 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
972 gl_mat_mul_floats( mat
, m
, MAT_FLAG_PERSPECTIVE
);
975 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
)
977 /* Need to keep a stack of near/far values in case the user push/pops
978 * the projection matrix stack so that we can call Driver.NearFar()
981 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = nearval
;
982 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = farval
;
984 if (ctx
->Driver
.NearFar
) {
985 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
992 _mesa_Ortho( GLdouble left
, GLdouble right
,
993 GLdouble bottom
, GLdouble top
,
994 GLdouble nearval
, GLdouble farval
)
996 GET_CURRENT_CONTEXT(ctx
);
1002 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
1004 if ((left
== right
) || (bottom
== top
) || (nearval
== farval
)) {
1005 gl_error( ctx
, GL_INVALID_VALUE
, "gl_Ortho((l = r) or (b = top) or (n=f)" );
1009 x
= 2.0 / (right
-left
);
1010 y
= 2.0 / (top
-bottom
);
1011 z
= -2.0 / (farval
-nearval
);
1012 tx
= -(right
+left
) / (right
-left
);
1013 ty
= -(top
+bottom
) / (top
-bottom
);
1014 tz
= -(farval
+nearval
) / (farval
-nearval
);
1016 #define M(row,col) m[col*4+row]
1017 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = 0.0F
; M(0,3) = tx
;
1018 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = 0.0F
; M(1,3) = ty
;
1019 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = z
; M(2,3) = tz
;
1020 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = 0.0F
; M(3,3) = 1.0F
;
1023 gl_mat_mul_floats( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
1025 if (ctx
->Driver
.NearFar
) {
1026 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
1032 _mesa_MatrixMode( GLenum mode
)
1034 GET_CURRENT_CONTEXT(ctx
);
1035 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glMatrixMode");
1040 ctx
->Transform
.MatrixMode
= mode
;
1043 gl_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
1050 _mesa_PushMatrix( void )
1052 GET_CURRENT_CONTEXT(ctx
);
1053 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushMatrix");
1055 if (MESA_VERBOSE
&VERBOSE_API
)
1056 fprintf(stderr
, "glPushMatrix %s\n",
1057 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1059 switch (ctx
->Transform
.MatrixMode
) {
1061 if (ctx
->ModelViewStackDepth
>= MAX_MODELVIEW_STACK_DEPTH
- 1) {
1062 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1065 gl_matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
1069 if (ctx
->ProjectionStackDepth
>= MAX_PROJECTION_STACK_DEPTH
- 1) {
1070 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1073 gl_matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
1074 &ctx
->ProjectionMatrix
);
1076 /* Save near and far projection values */
1077 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0]
1078 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][0];
1079 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1]
1080 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][1];
1084 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1085 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
- 1) {
1086 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1089 gl_matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
1090 &ctx
->TextureMatrix
[t
] );
1094 gl_problem(ctx
, "Bad matrix mode in gl_PushMatrix");
1101 _mesa_PopMatrix( void )
1103 GET_CURRENT_CONTEXT(ctx
);
1104 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopMatrix");
1106 if (MESA_VERBOSE
&VERBOSE_API
)
1107 fprintf(stderr
, "glPopMatrix %s\n",
1108 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1110 switch (ctx
->Transform
.MatrixMode
) {
1112 if (ctx
->ModelViewStackDepth
==0) {
1113 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1116 gl_matrix_copy( &ctx
->ModelView
,
1117 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
1118 ctx
->NewState
|= NEW_MODELVIEW
;
1121 if (ctx
->ProjectionStackDepth
==0) {
1122 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1126 gl_matrix_copy( &ctx
->ProjectionMatrix
,
1127 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
1128 ctx
->NewState
|= NEW_PROJECTION
;
1130 /* Device driver near/far values */
1132 GLfloat nearVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0];
1133 GLfloat farVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1];
1134 if (ctx
->Driver
.NearFar
) {
1135 (*ctx
->Driver
.NearFar
)( ctx
, nearVal
, farVal
);
1141 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1142 if (ctx
->TextureStackDepth
[t
]==0) {
1143 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1146 gl_matrix_copy(&ctx
->TextureMatrix
[t
],
1147 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
1151 gl_problem(ctx
, "Bad matrix mode in gl_PopMatrix");
1158 _mesa_LoadIdentity( void )
1160 GET_CURRENT_CONTEXT(ctx
);
1162 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
1164 MEMCPY( mat
->m
, Identity
, 16*sizeof(GLfloat
) );
1167 MEMCPY( mat
->inv
, Identity
, 16*sizeof(GLfloat
) );
1169 mat
->type
= MATRIX_IDENTITY
;
1171 /* Have to set this to dirty to make sure we recalculate the
1172 * combined matrix later. The update_matrix in this case is a
1173 * shortcircuit anyway...
1175 mat
->flags
= MAT_DIRTY_DEPENDENTS
;
1180 _mesa_LoadMatrixf( const GLfloat
*m
)
1182 GET_CURRENT_CONTEXT(ctx
);
1184 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
1186 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
1187 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1189 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
) {
1191 #define M(row,col) m[col*4+row]
1195 GLfloat n
= (c
== 1.0 ? 0.0 : d
/ (c
- 1.0));
1196 GLfloat f
= (c
== -1.0 ? 1.0 : d
/ (c
+ 1.0));
1198 /* Need to keep a stack of near/far values in case the user
1199 * push/pops the projection matrix stack so that we can call
1200 * Driver.NearFar() after a pop.
1202 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = n
;
1203 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = f
;
1205 if (ctx
->Driver
.NearFar
) {
1206 (*ctx
->Driver
.NearFar
)( ctx
, n
, f
);
1213 _mesa_LoadMatrixd( const GLdouble
*m
)
1217 for (i
= 0; i
< 16; i
++)
1219 _mesa_LoadMatrixf(f
);
1225 * Multiply the active matrix by an arbitary matrix.
1228 _mesa_MultMatrixf( const GLfloat
*m
)
1230 GET_CURRENT_CONTEXT(ctx
);
1232 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1233 matmul4( mat
->m
, mat
->m
, m
);
1234 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1239 * Multiply the active matrix by an arbitary matrix.
1242 _mesa_MultMatrixd( const GLdouble
*m
)
1244 GET_CURRENT_CONTEXT(ctx
);
1246 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1247 matmul4fd( mat
->m
, mat
->m
, m
);
1248 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1255 * Multiply a matrix by an array of floats with known properties.
1257 void gl_mat_mul_floats( GLmatrix
*mat
, const GLfloat
*m
, GLuint flags
)
1259 mat
->flags
|= (flags
|
1262 MAT_DIRTY_DEPENDENTS
);
1264 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1265 matmul34( mat
->m
, mat
->m
, m
);
1267 matmul4( mat
->m
, mat
->m
, m
);
1272 * Multiply a matrix by an array of floats with known properties.
1274 void gl_mat_mul_mat( GLmatrix
*mat
, const GLmatrix
*m
)
1276 mat
->flags
|= (m
->flags
|
1279 MAT_DIRTY_DEPENDENTS
);
1281 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1282 matmul34( mat
->m
, mat
->m
, m
->m
);
1284 matmul4( mat
->m
, mat
->m
, m
->m
);
1290 * Execute a glRotate call
1293 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
1295 GET_CURRENT_CONTEXT(ctx
);
1297 if (angle
!= 0.0F
) {
1299 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
1301 gl_rotation_matrix( angle
, x
, y
, z
, m
);
1302 gl_mat_mul_floats( mat
, m
, MAT_FLAG_ROTATION
);
1307 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
1309 _mesa_Rotatef(angle
, x
, y
, z
);
1314 * Execute a glScale call
1317 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
1319 GET_CURRENT_CONTEXT(ctx
);
1322 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glScale");
1325 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
1326 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
1327 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
1328 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
1330 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
1331 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1333 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1335 mat
->flags
|= (MAT_DIRTY_TYPE
|
1337 MAT_DIRTY_DEPENDENTS
);
1342 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
1344 _mesa_Scalef(x
, y
, z
);
1349 * Execute a glTranslate call
1352 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
1354 GET_CURRENT_CONTEXT(ctx
);
1357 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glTranslate");
1359 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1360 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1361 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1362 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1364 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
1367 MAT_DIRTY_DEPENDENTS
);
1372 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
1374 _mesa_Translatef(x
, y
, z
);
1380 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
1383 gl_matrix_transposef(tm
, m
);
1384 _mesa_LoadMatrixf(tm
);
1389 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
1392 gl_matrix_transposed(tm
, m
);
1393 _mesa_LoadMatrixd(tm
);
1398 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
1401 gl_matrix_transposef(tm
, m
);
1402 _mesa_MultMatrixf(tm
);
1407 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
1410 gl_matrix_transposed(tm
, m
);
1411 _mesa_MultMatrixd(tm
);
1416 * Called via glViewport or display list execution.
1419 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1421 GET_CURRENT_CONTEXT(ctx
);
1422 gl_Viewport(ctx
, x
, y
, width
, height
);
1428 * Define a new viewport and reallocate auxillary buffers if the size of
1429 * the window (color buffer) has changed.
1431 * XXX This is directly called by device drivers, BUT this function
1432 * may be renamed _mesa_Viewport (without ctx arg) in the future so
1433 * use of _mesa_Viewport is encouraged.
1436 gl_Viewport( GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1438 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glViewport");
1440 if (width
<0 || height
<0) {
1441 gl_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
1445 if (MESA_VERBOSE
& VERBOSE_API
)
1446 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
1448 /* clamp width, and height to implementation dependent range */
1449 width
= CLAMP( width
, 1, MAX_WIDTH
);
1450 height
= CLAMP( height
, 1, MAX_HEIGHT
);
1453 ctx
->Viewport
.X
= x
;
1454 ctx
->Viewport
.Width
= width
;
1455 ctx
->Viewport
.Y
= y
;
1456 ctx
->Viewport
.Height
= height
;
1458 /* compute scale and bias values */
1459 ctx
->Viewport
.WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
1460 ctx
->Viewport
.WindowMap
.m
[MAT_TX
] = ctx
->Viewport
.WindowMap
.m
[MAT_SX
] + x
;
1461 ctx
->Viewport
.WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
1462 ctx
->Viewport
.WindowMap
.m
[MAT_TY
] = ctx
->Viewport
.WindowMap
.m
[MAT_SY
] + y
;
1463 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = 0.5 * ctx
->Visual
->DepthMaxF
;
1464 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = 0.5 * ctx
->Visual
->DepthMaxF
;
1466 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
1467 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
1469 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1470 ctx
->NewState
|= NEW_VIEWPORT
;
1472 /* Check if window/buffer has been resized and if so, reallocate the
1473 * ancillary buffers.
1475 _mesa_ResizeBuffersMESA();
1478 ctx
->RasterMask
&= ~WINCLIP_BIT
;
1480 if ( ctx
->Viewport
.X
<0
1481 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->DrawBuffer
->Width
1482 || ctx
->Viewport
.Y
<0
1483 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->DrawBuffer
->Height
) {
1484 ctx
->RasterMask
|= WINCLIP_BIT
;
1488 if (ctx
->Driver
.Viewport
) {
1489 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
1496 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
1499 * nearval - specifies mapping of the near clipping plane to window
1500 * coordinates, default is 0
1501 * farval - specifies mapping of the far clipping plane to window
1502 * coordinates, default is 1
1504 * After clipping and div by w, z coords are in -1.0 to 1.0,
1505 * corresponding to near and far clipping planes. glDepthRange
1506 * specifies a linear mapping of the normalized z coords in
1507 * this range to window z coords.
1510 GET_CURRENT_CONTEXT(ctx
);
1511 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthRange");
1513 if (MESA_VERBOSE
&VERBOSE_API
)
1514 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
1516 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
1517 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
1519 ctx
->Viewport
.Near
= n
;
1520 ctx
->Viewport
.Far
= f
;
1521 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = ctx
->Visual
->DepthMaxF
* ((f
- n
) / 2.0);
1522 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = ctx
->Visual
->DepthMaxF
* ((f
- n
) / 2.0 + n
);
1524 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1526 if (ctx
->Driver
.DepthRange
) {
1527 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);
1532 void gl_calculate_model_project_matrix( GLcontext
*ctx
)
1534 gl_matrix_mul( &ctx
->ModelProjectMatrix
,
1535 &ctx
->ProjectionMatrix
,
1538 gl_matrix_analyze( &ctx
->ModelProjectMatrix
);
1542 void gl_matrix_ctr( GLmatrix
*m
)
1545 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1546 m
->type
= MATRIX_IDENTITY
;
1547 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1550 void gl_matrix_dtr( GLmatrix
*m
)
1559 void gl_matrix_set_identity( GLmatrix
*m
)
1561 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1562 m
->type
= MATRIX_IDENTITY
;
1563 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1567 void gl_matrix_alloc_inv( GLmatrix
*m
)
1570 m
->inv
= (GLfloat
*)MALLOC(16*sizeof(GLfloat
));
1571 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
1575 void gl_matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
1577 MEMCPY( to
->m
, from
->m
, sizeof(Identity
));
1578 to
->flags
= from
->flags
| MAT_DIRTY_DEPENDENTS
;
1579 to
->type
= from
->type
;
1582 if (from
->inv
== 0) {
1583 gl_matrix_invert( to
);
1585 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
1590 void gl_matrix_mul( GLmatrix
*dest
, const GLmatrix
*a
, const GLmatrix
*b
)
1592 dest
->flags
= (a
->flags
|
1596 MAT_DIRTY_DEPENDENTS
);
1598 if (TEST_MAT_FLAGS(dest
, MAT_FLAGS_3D
))
1599 matmul34( dest
->m
, a
->m
, b
->m
);
1601 matmul4( dest
->m
, a
->m
, b
->m
);