1 /* $Id: matrix.c,v 1.25 2000/11/13 20:02:56 keithw 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.
32 * 1. 4x4 transformation matrices are stored in memory in column major order.
33 * 2. Points/vertices are to be thought of as column vectors.
34 * 3. Transformation of a point p by a matrix M is: p' = M * p
53 static const char *types
[] = {
64 static GLfloat Identity
[16] = {
73 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
);
76 static void print_matrix_floats( const GLfloat m
[16] )
80 fprintf(stderr
,"\t%f %f %f %f\n", m
[i
], m
[4+i
], m
[8+i
], m
[12+i
] );
84 void gl_print_matrix( const GLmatrix
*m
)
86 fprintf(stderr
, "Matrix type: %s, flags: %x\n", types
[m
->type
], m
->flags
);
87 print_matrix_floats(m
->m
);
88 fprintf(stderr
, "Inverse: \n");
91 print_matrix_floats(m
->inv
);
92 matmul4(prod
, m
->m
, m
->inv
);
93 fprintf(stderr
, "Mat * Inverse:\n");
94 print_matrix_floats(prod
);
97 fprintf(stderr
, " - not available\n");
104 * This matmul was contributed by Thomas Malik
106 * Perform a 4x4 matrix multiplication (product = a x b).
107 * Input: a, b - matrices to multiply
108 * Output: product - product of a and b
109 * WARNING: (product != b) assumed
110 * NOTE: (product == a) allowed
114 #define A(row,col) a[(col<<2)+row]
115 #define B(row,col) b[(col<<2)+row]
116 #define P(row,col) product[(col<<2)+row]
118 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
121 for (i
= 0; i
< 4; i
++) {
122 const GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
123 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
124 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
125 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
126 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.
134 static void matmul34( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
137 for (i
= 0; i
< 3; i
++) {
138 const GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
139 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0);
140 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1);
141 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2);
142 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
;
150 static void matmul4fd( GLfloat
*product
, const GLfloat
*a
, const GLdouble
*b
)
153 for (i
= 0; i
< 4; i
++) {
154 const GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
155 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
156 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
157 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
158 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
167 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
168 #define MAT(m,r,c) (m)[(c)*4+(r)]
171 * Compute inverse of 4x4 transformation matrix.
172 * Code contributed by Jacques Leroy jle@star.be
173 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
175 static GLboolean
invert_matrix_general( GLmatrix
*mat
)
177 const GLfloat
*m
= mat
->m
;
178 GLfloat
*out
= mat
->inv
;
180 GLfloat m0
, m1
, m2
, m3
, s
;
181 GLfloat
*r0
, *r1
, *r2
, *r3
;
183 r0
= wtmp
[0], r1
= wtmp
[1], r2
= wtmp
[2], r3
= wtmp
[3];
185 r0
[0] = MAT(m
,0,0), r0
[1] = MAT(m
,0,1),
186 r0
[2] = MAT(m
,0,2), r0
[3] = MAT(m
,0,3),
187 r0
[4] = 1.0, r0
[5] = r0
[6] = r0
[7] = 0.0,
189 r1
[0] = MAT(m
,1,0), r1
[1] = MAT(m
,1,1),
190 r1
[2] = MAT(m
,1,2), r1
[3] = MAT(m
,1,3),
191 r1
[5] = 1.0, r1
[4] = r1
[6] = r1
[7] = 0.0,
193 r2
[0] = MAT(m
,2,0), r2
[1] = MAT(m
,2,1),
194 r2
[2] = MAT(m
,2,2), r2
[3] = MAT(m
,2,3),
195 r2
[6] = 1.0, r2
[4] = r2
[5] = r2
[7] = 0.0,
197 r3
[0] = MAT(m
,3,0), r3
[1] = MAT(m
,3,1),
198 r3
[2] = MAT(m
,3,2), r3
[3] = MAT(m
,3,3),
199 r3
[7] = 1.0, r3
[4] = r3
[5] = r3
[6] = 0.0;
201 /* choose pivot - or die */
202 if (fabs(r3
[0])>fabs(r2
[0])) SWAP_ROWS(r3
, r2
);
203 if (fabs(r2
[0])>fabs(r1
[0])) SWAP_ROWS(r2
, r1
);
204 if (fabs(r1
[0])>fabs(r0
[0])) SWAP_ROWS(r1
, r0
);
205 if (0.0 == r0
[0]) return GL_FALSE
;
207 /* eliminate first variable */
208 m1
= r1
[0]/r0
[0]; m2
= r2
[0]/r0
[0]; m3
= r3
[0]/r0
[0];
209 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
210 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
211 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
213 if (s
!= 0.0) { r1
[4] -= m1
* s
; r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
215 if (s
!= 0.0) { r1
[5] -= m1
* s
; r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
217 if (s
!= 0.0) { r1
[6] -= m1
* s
; r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
219 if (s
!= 0.0) { r1
[7] -= m1
* s
; r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
221 /* choose pivot - or die */
222 if (fabs(r3
[1])>fabs(r2
[1])) SWAP_ROWS(r3
, r2
);
223 if (fabs(r2
[1])>fabs(r1
[1])) SWAP_ROWS(r2
, r1
);
224 if (0.0 == r1
[1]) return GL_FALSE
;
226 /* eliminate second variable */
227 m2
= r2
[1]/r1
[1]; m3
= r3
[1]/r1
[1];
228 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
229 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
230 s
= r1
[4]; if (0.0 != s
) { r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
231 s
= r1
[5]; if (0.0 != s
) { r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
232 s
= r1
[6]; if (0.0 != s
) { r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
233 s
= r1
[7]; if (0.0 != s
) { r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
235 /* choose pivot - or die */
236 if (fabs(r3
[2])>fabs(r2
[2])) SWAP_ROWS(r3
, r2
);
237 if (0.0 == r2
[2]) return GL_FALSE
;
239 /* eliminate third variable */
241 r3
[3] -= m3
* r2
[3], r3
[4] -= m3
* r2
[4],
242 r3
[5] -= m3
* r2
[5], r3
[6] -= m3
* r2
[6],
246 if (0.0 == r3
[3]) return GL_FALSE
;
248 s
= 1.0/r3
[3]; /* now back substitute row 3 */
249 r3
[4] *= s
; r3
[5] *= s
; r3
[6] *= s
; r3
[7] *= s
;
251 m2
= r2
[3]; /* now back substitute row 2 */
253 r2
[4] = s
* (r2
[4] - r3
[4] * m2
), r2
[5] = s
* (r2
[5] - r3
[5] * m2
),
254 r2
[6] = s
* (r2
[6] - r3
[6] * m2
), r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
256 r1
[4] -= r3
[4] * m1
, r1
[5] -= r3
[5] * m1
,
257 r1
[6] -= r3
[6] * m1
, r1
[7] -= r3
[7] * m1
;
259 r0
[4] -= r3
[4] * m0
, r0
[5] -= r3
[5] * m0
,
260 r0
[6] -= r3
[6] * m0
, r0
[7] -= r3
[7] * m0
;
262 m1
= r1
[2]; /* now back substitute row 1 */
264 r1
[4] = s
* (r1
[4] - r2
[4] * m1
), r1
[5] = s
* (r1
[5] - r2
[5] * m1
),
265 r1
[6] = s
* (r1
[6] - r2
[6] * m1
), r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
267 r0
[4] -= r2
[4] * m0
, r0
[5] -= r2
[5] * m0
,
268 r0
[6] -= r2
[6] * m0
, r0
[7] -= r2
[7] * m0
;
270 m0
= r0
[1]; /* now back substitute row 0 */
272 r0
[4] = s
* (r0
[4] - r1
[4] * m0
), r0
[5] = s
* (r0
[5] - r1
[5] * m0
),
273 r0
[6] = s
* (r0
[6] - r1
[6] * m0
), r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
275 MAT(out
,0,0) = r0
[4]; MAT(out
,0,1) = r0
[5],
276 MAT(out
,0,2) = r0
[6]; MAT(out
,0,3) = r0
[7],
277 MAT(out
,1,0) = r1
[4]; MAT(out
,1,1) = r1
[5],
278 MAT(out
,1,2) = r1
[6]; MAT(out
,1,3) = r1
[7],
279 MAT(out
,2,0) = r2
[4]; MAT(out
,2,1) = r2
[5],
280 MAT(out
,2,2) = r2
[6]; MAT(out
,2,3) = r2
[7],
281 MAT(out
,3,0) = r3
[4]; MAT(out
,3,1) = r3
[5],
282 MAT(out
,3,2) = r3
[6]; MAT(out
,3,3) = r3
[7];
289 /* Adapted from graphics gems II.
291 static GLboolean
invert_matrix_3d_general( GLmatrix
*mat
)
293 const GLfloat
*in
= mat
->m
;
294 GLfloat
*out
= mat
->inv
;
298 /* Calculate the determinant of upper left 3x3 submatrix and
299 * determine if the matrix is singular.
302 t
= MAT(in
,0,0) * MAT(in
,1,1) * MAT(in
,2,2);
303 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
305 t
= MAT(in
,1,0) * MAT(in
,2,1) * MAT(in
,0,2);
306 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
308 t
= MAT(in
,2,0) * MAT(in
,0,1) * MAT(in
,1,2);
309 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
311 t
= -MAT(in
,2,0) * MAT(in
,1,1) * MAT(in
,0,2);
312 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
314 t
= -MAT(in
,1,0) * MAT(in
,0,1) * MAT(in
,2,2);
315 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
317 t
= -MAT(in
,0,0) * MAT(in
,2,1) * MAT(in
,1,2);
318 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
326 MAT(out
,0,0) = ( (MAT(in
,1,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,1,2) )*det
);
327 MAT(out
,0,1) = (- (MAT(in
,0,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,0,2) )*det
);
328 MAT(out
,0,2) = ( (MAT(in
,0,1)*MAT(in
,1,2) - MAT(in
,1,1)*MAT(in
,0,2) )*det
);
329 MAT(out
,1,0) = (- (MAT(in
,1,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,1,2) )*det
);
330 MAT(out
,1,1) = ( (MAT(in
,0,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,0,2) )*det
);
331 MAT(out
,1,2) = (- (MAT(in
,0,0)*MAT(in
,1,2) - MAT(in
,1,0)*MAT(in
,0,2) )*det
);
332 MAT(out
,2,0) = ( (MAT(in
,1,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,1,1) )*det
);
333 MAT(out
,2,1) = (- (MAT(in
,0,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,0,1) )*det
);
334 MAT(out
,2,2) = ( (MAT(in
,0,0)*MAT(in
,1,1) - MAT(in
,1,0)*MAT(in
,0,1) )*det
);
336 /* Do the translation part */
337 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
338 MAT(in
,1,3) * MAT(out
,0,1) +
339 MAT(in
,2,3) * MAT(out
,0,2) );
340 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
341 MAT(in
,1,3) * MAT(out
,1,1) +
342 MAT(in
,2,3) * MAT(out
,1,2) );
343 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
344 MAT(in
,1,3) * MAT(out
,2,1) +
345 MAT(in
,2,3) * MAT(out
,2,2) );
351 static GLboolean
invert_matrix_3d( GLmatrix
*mat
)
353 const GLfloat
*in
= mat
->m
;
354 GLfloat
*out
= mat
->inv
;
356 if (!TEST_MAT_FLAGS(mat
, MAT_FLAGS_ANGLE_PRESERVING
)) {
357 return invert_matrix_3d_general( mat
);
360 if (mat
->flags
& MAT_FLAG_UNIFORM_SCALE
) {
361 GLfloat scale
= (MAT(in
,0,0) * MAT(in
,0,0) +
362 MAT(in
,0,1) * MAT(in
,0,1) +
363 MAT(in
,0,2) * MAT(in
,0,2));
370 /* Transpose and scale the 3 by 3 upper-left submatrix. */
371 MAT(out
,0,0) = scale
* MAT(in
,0,0);
372 MAT(out
,1,0) = scale
* MAT(in
,0,1);
373 MAT(out
,2,0) = scale
* MAT(in
,0,2);
374 MAT(out
,0,1) = scale
* MAT(in
,1,0);
375 MAT(out
,1,1) = scale
* MAT(in
,1,1);
376 MAT(out
,2,1) = scale
* MAT(in
,1,2);
377 MAT(out
,0,2) = scale
* MAT(in
,2,0);
378 MAT(out
,1,2) = scale
* MAT(in
,2,1);
379 MAT(out
,2,2) = scale
* MAT(in
,2,2);
381 else if (mat
->flags
& MAT_FLAG_ROTATION
) {
382 /* Transpose the 3 by 3 upper-left submatrix. */
383 MAT(out
,0,0) = MAT(in
,0,0);
384 MAT(out
,1,0) = MAT(in
,0,1);
385 MAT(out
,2,0) = MAT(in
,0,2);
386 MAT(out
,0,1) = MAT(in
,1,0);
387 MAT(out
,1,1) = MAT(in
,1,1);
388 MAT(out
,2,1) = MAT(in
,1,2);
389 MAT(out
,0,2) = MAT(in
,2,0);
390 MAT(out
,1,2) = MAT(in
,2,1);
391 MAT(out
,2,2) = MAT(in
,2,2);
394 /* pure translation */
395 MEMCPY( out
, Identity
, sizeof(Identity
) );
396 MAT(out
,0,3) = - MAT(in
,0,3);
397 MAT(out
,1,3) = - MAT(in
,1,3);
398 MAT(out
,2,3) = - MAT(in
,2,3);
402 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
403 /* Do the translation part */
404 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
405 MAT(in
,1,3) * MAT(out
,0,1) +
406 MAT(in
,2,3) * MAT(out
,0,2) );
407 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
408 MAT(in
,1,3) * MAT(out
,1,1) +
409 MAT(in
,2,3) * MAT(out
,1,2) );
410 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
411 MAT(in
,1,3) * MAT(out
,2,1) +
412 MAT(in
,2,3) * MAT(out
,2,2) );
415 MAT(out
,0,3) = MAT(out
,1,3) = MAT(out
,2,3) = 0.0;
423 static GLboolean
invert_matrix_identity( GLmatrix
*mat
)
425 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
430 static GLboolean
invert_matrix_3d_no_rot( GLmatrix
*mat
)
432 const GLfloat
*in
= mat
->m
;
433 GLfloat
*out
= mat
->inv
;
435 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0 || MAT(in
,2,2) == 0 )
438 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
439 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
440 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
441 MAT(out
,2,2) = 1.0 / MAT(in
,2,2);
443 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
444 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
445 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
446 MAT(out
,2,3) = - (MAT(in
,2,3) * MAT(out
,2,2));
453 static GLboolean
invert_matrix_2d_no_rot( GLmatrix
*mat
)
455 const GLfloat
*in
= mat
->m
;
456 GLfloat
*out
= mat
->inv
;
458 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0)
461 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
462 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
463 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
465 if (mat
->flags
& MAT_FLAG_TRANSLATION
) {
466 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
467 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
474 static GLboolean
invert_matrix_perspective( GLmatrix
*mat
)
476 const GLfloat
*in
= mat
->m
;
477 GLfloat
*out
= mat
->inv
;
479 if (MAT(in
,2,3) == 0)
482 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
484 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
485 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
487 MAT(out
,0,3) = MAT(in
,0,2);
488 MAT(out
,1,3) = MAT(in
,1,2);
493 MAT(out
,3,2) = 1.0 / MAT(in
,2,3);
494 MAT(out
,3,3) = MAT(in
,2,2) * MAT(out
,3,2);
500 typedef GLboolean (*inv_mat_func
)( GLmatrix
*mat
);
503 static inv_mat_func inv_mat_tab
[7] = {
504 invert_matrix_general
,
505 invert_matrix_identity
,
506 invert_matrix_3d_no_rot
,
507 invert_matrix_perspective
,
508 invert_matrix_3d
, /* lazy! */
509 invert_matrix_2d_no_rot
,
514 static GLboolean
matrix_invert( GLmatrix
*mat
)
516 if (inv_mat_tab
[mat
->type
](mat
)) {
517 mat
->flags
&= ~MAT_FLAG_SINGULAR
;
520 mat
->flags
|= MAT_FLAG_SINGULAR
;
521 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
528 void gl_matrix_transposef( GLfloat to
[16], const GLfloat from
[16] )
550 void gl_matrix_transposed( GLdouble to
[16], const GLdouble from
[16] )
573 * Generate a 4x4 transformation matrix from glRotate parameters.
575 void gl_rotation_matrix( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
,
578 /* This function contributed by Erich Boleyn (erich@uruk.org) */
580 GLfloat xx
, yy
, zz
, xy
, yz
, zx
, xs
, ys
, zs
, one_c
;
582 s
= sin( angle
* DEG2RAD
);
583 c
= cos( angle
* DEG2RAD
);
585 mag
= GL_SQRT( x
*x
+ y
*y
+ z
*z
);
588 /* generate an identity matrix and return */
589 MEMCPY(m
, Identity
, sizeof(GLfloat
)*16);
597 #define M(row,col) m[col*4+row]
600 * Arbitrary axis rotation matrix.
602 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
603 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
604 * (which is about the X-axis), and the two composite transforms
605 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
606 * from the arbitrary axis to the X-axis then back. They are
607 * all elementary rotations.
609 * Rz' is a rotation about the Z-axis, to bring the axis vector
610 * into the x-z plane. Then Ry' is applied, rotating about the
611 * Y-axis to bring the axis vector parallel with the X-axis. The
612 * rotation about the X-axis is then performed. Ry and Rz are
613 * simply the respective inverse transforms to bring the arbitrary
614 * axis back to it's original orientation. The first transforms
615 * Rz' and Ry' are considered inverses, since the data from the
616 * arbitrary axis gives you info on how to get to it, not how
617 * to get away from it, and an inverse must be applied.
619 * The basic calculation used is to recognize that the arbitrary
620 * axis vector (x, y, z), since it is of unit length, actually
621 * represents the sines and cosines of the angles to rotate the
622 * X-axis to the same orientation, with theta being the angle about
623 * Z and phi the angle about Y (in the order described above)
626 * cos ( theta ) = x / sqrt ( 1 - z^2 )
627 * sin ( theta ) = y / sqrt ( 1 - z^2 )
629 * cos ( phi ) = sqrt ( 1 - z^2 )
632 * Note that cos ( phi ) can further be inserted to the above
635 * cos ( theta ) = x / cos ( phi )
636 * sin ( theta ) = y / sin ( phi )
638 * ...etc. Because of those relations and the standard trigonometric
639 * relations, it is pssible to reduce the transforms down to what
640 * is used below. It may be that any primary axis chosen will give the
641 * same results (modulo a sign convention) using thie method.
643 * Particularly nice is to notice that all divisions that might
644 * have caused trouble when parallel to certain planes or
645 * axis go away with care paid to reducing the expressions.
646 * After checking, it does perform correctly under all cases, since
647 * in all the cases of division where the denominator would have
648 * been zero, the numerator would have been zero as well, giving
649 * the expected result.
663 M(0,0) = (one_c
* xx
) + c
;
664 M(0,1) = (one_c
* xy
) - zs
;
665 M(0,2) = (one_c
* zx
) + ys
;
668 M(1,0) = (one_c
* xy
) + zs
;
669 M(1,1) = (one_c
* yy
) + c
;
670 M(1,2) = (one_c
* yz
) - xs
;
673 M(2,0) = (one_c
* zx
) - ys
;
674 M(2,1) = (one_c
* yz
) + xs
;
675 M(2,2) = (one_c
* zz
) + c
;
686 #define ZERO(x) (1<<x)
687 #define ONE(x) (1<<(x+16))
689 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
690 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
692 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
693 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
694 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
695 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
697 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
698 ZERO(1) | ZERO(9) | \
699 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
700 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
702 #define MASK_2D ( ZERO(8) | \
704 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
705 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
708 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
709 ZERO(1) | ZERO(9) | \
710 ZERO(2) | ZERO(6) | \
711 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
716 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
719 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
720 ZERO(1) | ZERO(13) |\
721 ZERO(2) | ZERO(6) | \
722 ZERO(3) | ZERO(7) | ZERO(15) )
724 #define SQ(x) ((x)*(x))
726 /* Determine type and flags from scratch. This is expensive enough to
727 * only want to do it once.
729 static void analyze_from_scratch( GLmatrix
*mat
)
731 const GLfloat
*m
= mat
->m
;
735 for (i
= 0 ; i
< 16 ; i
++) {
736 if (m
[i
] == 0.0) mask
|= (1<<i
);
739 if (m
[0] == 1.0F
) mask
|= (1<<16);
740 if (m
[5] == 1.0F
) mask
|= (1<<21);
741 if (m
[10] == 1.0F
) mask
|= (1<<26);
742 if (m
[15] == 1.0F
) mask
|= (1<<31);
744 mat
->flags
&= ~MAT_FLAGS_GEOMETRY
;
746 /* Check for translation - no-one really cares
748 if ((mask
& MASK_NO_TRX
) != MASK_NO_TRX
)
749 mat
->flags
|= MAT_FLAG_TRANSLATION
;
753 if (mask
== MASK_IDENTITY
) {
754 mat
->type
= MATRIX_IDENTITY
;
756 else if ((mask
& MASK_2D_NO_ROT
) == MASK_2D_NO_ROT
) {
757 mat
->type
= MATRIX_2D_NO_ROT
;
759 if ((mask
& MASK_NO_2D_SCALE
) != MASK_NO_2D_SCALE
)
760 mat
->flags
= MAT_FLAG_GENERAL_SCALE
;
762 else if ((mask
& MASK_2D
) == MASK_2D
) {
763 GLfloat mm
= DOT2(m
, m
);
764 GLfloat m4m4
= DOT2(m
+4,m
+4);
765 GLfloat mm4
= DOT2(m
,m
+4);
767 mat
->type
= MATRIX_2D
;
769 /* Check for scale */
770 if (SQ(mm
-1) > SQ(1e-6) ||
771 SQ(m4m4
-1) > SQ(1e-6))
772 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
774 /* Check for rotation */
775 if (SQ(mm4
) > SQ(1e-6))
776 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
778 mat
->flags
|= MAT_FLAG_ROTATION
;
781 else if ((mask
& MASK_3D_NO_ROT
) == MASK_3D_NO_ROT
) {
782 mat
->type
= MATRIX_3D_NO_ROT
;
784 /* Check for scale */
785 if (SQ(m
[0]-m
[5]) < SQ(1e-6) &&
786 SQ(m
[0]-m
[10]) < SQ(1e-6)) {
787 if (SQ(m
[0]-1.0) > SQ(1e-6)) {
788 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
792 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
795 else if ((mask
& MASK_3D
) == MASK_3D
) {
796 GLfloat c1
= DOT3(m
,m
);
797 GLfloat c2
= DOT3(m
+4,m
+4);
798 GLfloat c3
= DOT3(m
+8,m
+8);
799 GLfloat d1
= DOT3(m
, m
+4);
802 mat
->type
= MATRIX_3D
;
804 /* Check for scale */
805 if (SQ(c1
-c2
) < SQ(1e-6) && SQ(c1
-c3
) < SQ(1e-6)) {
806 if (SQ(c1
-1.0) > SQ(1e-6))
807 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
808 /* else no scale at all */
811 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
814 /* Check for rotation */
815 if (SQ(d1
) < SQ(1e-6)) {
816 CROSS3( cp
, m
, m
+4 );
817 SUB_3V( cp
, cp
, (m
+8) );
818 if (LEN_SQUARED_3FV(cp
) < SQ(1e-6))
819 mat
->flags
|= MAT_FLAG_ROTATION
;
821 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
824 mat
->flags
|= MAT_FLAG_GENERAL_3D
; /* shear, etc */
827 else if ((mask
& MASK_PERSPECTIVE
) == MASK_PERSPECTIVE
&& m
[11]==-1.0F
) {
828 mat
->type
= MATRIX_PERSPECTIVE
;
829 mat
->flags
|= MAT_FLAG_GENERAL
;
832 mat
->type
= MATRIX_GENERAL
;
833 mat
->flags
|= MAT_FLAG_GENERAL
;
838 /* Analyse a matrix given that its flags are accurate - this is the
839 * more common operation, hopefully.
841 static void analyze_from_flags( GLmatrix
*mat
)
843 const GLfloat
*m
= mat
->m
;
845 if (TEST_MAT_FLAGS(mat
, 0)) {
846 mat
->type
= MATRIX_IDENTITY
;
848 else if (TEST_MAT_FLAGS(mat
, (MAT_FLAG_TRANSLATION
|
849 MAT_FLAG_UNIFORM_SCALE
|
850 MAT_FLAG_GENERAL_SCALE
))) {
851 if ( m
[10]==1.0F
&& m
[14]==0.0F
) {
852 mat
->type
= MATRIX_2D_NO_ROT
;
855 mat
->type
= MATRIX_3D_NO_ROT
;
858 else if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
)) {
861 && m
[2]==0.0F
&& m
[6]==0.0F
&& m
[10]==1.0F
&& m
[14]==0.0F
) {
862 mat
->type
= MATRIX_2D
;
865 mat
->type
= MATRIX_3D
;
868 else if ( m
[4]==0.0F
&& m
[12]==0.0F
869 && m
[1]==0.0F
&& m
[13]==0.0F
870 && m
[2]==0.0F
&& m
[6]==0.0F
871 && m
[3]==0.0F
&& m
[7]==0.0F
&& m
[11]==-1.0F
&& m
[15]==0.0F
) {
872 mat
->type
= MATRIX_PERSPECTIVE
;
875 mat
->type
= MATRIX_GENERAL
;
880 void gl_matrix_analyze( GLmatrix
*mat
)
882 if (mat
->flags
& MAT_DIRTY_TYPE
) {
883 if (mat
->flags
& MAT_DIRTY_FLAGS
)
884 analyze_from_scratch( mat
);
886 analyze_from_flags( mat
);
889 if (mat
->inv
&& (mat
->flags
& MAT_DIRTY_INVERSE
)) {
890 matrix_invert( mat
);
893 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
899 static void matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
901 MEMCPY( to
->m
, from
->m
, sizeof(Identity
) );
902 to
->flags
= from
->flags
| MAT_DIRTY_DEPENDENTS
;
903 to
->type
= from
->type
;
906 if (from
->inv
== 0) {
910 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
916 * Multiply a matrix by an array of floats with known properties.
918 static void mat_mul_floats( GLmatrix
*mat
, const GLfloat
*m
, GLuint flags
)
920 mat
->flags
|= (flags
|
923 MAT_DIRTY_DEPENDENTS
);
925 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
926 matmul34( mat
->m
, mat
->m
, m
);
928 matmul4( mat
->m
, mat
->m
, m
);
933 void gl_matrix_ctr( GLmatrix
*m
)
936 m
->m
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
938 MEMCPY( m
->m
, Identity
, sizeof(Identity
) );
940 m
->type
= MATRIX_IDENTITY
;
941 m
->flags
= MAT_DIRTY_DEPENDENTS
;
944 void gl_matrix_dtr( GLmatrix
*m
)
951 ALIGN_FREE( m
->inv
);
957 void gl_matrix_alloc_inv( GLmatrix
*m
)
960 m
->inv
= (GLfloat
*) ALIGN_MALLOC( 16 * sizeof(GLfloat
), 16 );
961 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
966 void gl_matrix_mul( GLmatrix
*dest
, const GLmatrix
*a
, const GLmatrix
*b
)
968 dest
->flags
= (a
->flags
|
972 MAT_DIRTY_DEPENDENTS
);
974 if (TEST_MAT_FLAGS(dest
, MAT_FLAGS_3D
))
975 matmul34( dest
->m
, a
->m
, b
->m
);
977 matmul4( dest
->m
, a
->m
, b
->m
);
982 /**********************************************************************/
984 /**********************************************************************/
987 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
989 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where); \
990 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
991 switch (ctx->Transform.MatrixMode) { \
993 mat = &ctx->ModelView; \
994 flags |= _NEW_MODELVIEW; \
996 case GL_PROJECTION: \
997 mat = &ctx->ProjectionMatrix; \
998 flags |= _NEW_PROJECTION; \
1001 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
1002 flags |= _NEW_TEXTURE_MATRIX; \
1005 mat = &ctx->ColorMatrix; \
1006 flags |= _NEW_COLOR_MATRIX; \
1009 gl_problem(ctx, where); \
1015 _mesa_Frustum( GLdouble left
, GLdouble right
,
1016 GLdouble bottom
, GLdouble top
,
1017 GLdouble nearval
, GLdouble farval
)
1019 GET_CURRENT_CONTEXT(ctx
);
1020 GLfloat x
, y
, a
, b
, c
, d
;
1024 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glFrustrum" );
1026 if ((nearval
<=0.0 || farval
<=0.0) || (nearval
== farval
) || (left
== right
) || (top
== bottom
)) {
1027 gl_error( ctx
, GL_INVALID_VALUE
, "glFrustum(near or far)" );
1031 x
= (2.0*nearval
) / (right
-left
);
1032 y
= (2.0*nearval
) / (top
-bottom
);
1033 a
= (right
+left
) / (right
-left
);
1034 b
= (top
+bottom
) / (top
-bottom
);
1035 c
= -(farval
+nearval
) / ( farval
-nearval
);
1036 d
= -(2.0*farval
*nearval
) / (farval
-nearval
); /* error? */
1038 #define M(row,col) m[col*4+row]
1039 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
1040 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
1041 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
1042 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
1045 mat_mul_floats( mat
, m
, MAT_FLAG_PERSPECTIVE
);
1047 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
) {
1048 /* Need to keep a stack of near/far values in case the user push/pops
1049 * the projection matrix stack so that we can call Driver.NearFar()
1052 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = nearval
;
1053 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = farval
;
1055 if (ctx
->Driver
.NearFar
) {
1056 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
1063 _mesa_Ortho( GLdouble left
, GLdouble right
,
1064 GLdouble bottom
, GLdouble top
,
1065 GLdouble nearval
, GLdouble farval
)
1067 GET_CURRENT_CONTEXT(ctx
);
1073 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
1075 if ((left
== right
) || (bottom
== top
) || (nearval
== farval
)) {
1076 gl_error( ctx
, GL_INVALID_VALUE
,
1077 "gl_Ortho((l = r) or (b = top) or (n=f)" );
1081 x
= 2.0 / (right
-left
);
1082 y
= 2.0 / (top
-bottom
);
1083 z
= -2.0 / (farval
-nearval
);
1084 tx
= -(right
+left
) / (right
-left
);
1085 ty
= -(top
+bottom
) / (top
-bottom
);
1086 tz
= -(farval
+nearval
) / (farval
-nearval
);
1088 #define M(row,col) m[col*4+row]
1089 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = 0.0F
; M(0,3) = tx
;
1090 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = 0.0F
; M(1,3) = ty
;
1091 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = z
; M(2,3) = tz
;
1092 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = 0.0F
; M(3,3) = 1.0F
;
1095 mat_mul_floats( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
1097 if (ctx
->Driver
.NearFar
) {
1098 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
1104 _mesa_MatrixMode( GLenum mode
)
1106 GET_CURRENT_CONTEXT(ctx
);
1107 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glMatrixMode");
1113 ctx
->Transform
.MatrixMode
= mode
;
1116 gl_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
1123 _mesa_PushMatrix( void )
1125 GET_CURRENT_CONTEXT(ctx
);
1126 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushMatrix");
1128 if (MESA_VERBOSE
&VERBOSE_API
)
1129 fprintf(stderr
, "glPushMatrix %s\n",
1130 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1132 switch (ctx
->Transform
.MatrixMode
) {
1134 if (ctx
->ModelViewStackDepth
>= MAX_MODELVIEW_STACK_DEPTH
- 1) {
1135 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1138 matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
1142 if (ctx
->ProjectionStackDepth
>= MAX_PROJECTION_STACK_DEPTH
- 1) {
1143 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1146 matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
1147 &ctx
->ProjectionMatrix
);
1149 /* Save near and far projection values */
1150 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0]
1151 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][0];
1152 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1]
1153 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][1];
1157 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1158 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
- 1) {
1159 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1162 matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
1163 &ctx
->TextureMatrix
[t
] );
1167 if (ctx
->ColorStackDepth
>= MAX_COLOR_STACK_DEPTH
- 1) {
1168 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1171 matrix_copy( &ctx
->ColorStack
[ctx
->ColorStackDepth
++],
1172 &ctx
->ColorMatrix
);
1175 gl_problem(ctx
, "Bad matrix mode in gl_PushMatrix");
1182 _mesa_PopMatrix( void )
1184 GET_CURRENT_CONTEXT(ctx
);
1185 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopMatrix");
1187 if (MESA_VERBOSE
&VERBOSE_API
)
1188 fprintf(stderr
, "glPopMatrix %s\n",
1189 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1191 switch (ctx
->Transform
.MatrixMode
) {
1193 if (ctx
->ModelViewStackDepth
==0) {
1194 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1197 matrix_copy( &ctx
->ModelView
,
1198 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
1199 ctx
->NewState
|= _NEW_MODELVIEW
;
1202 if (ctx
->ProjectionStackDepth
==0) {
1203 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1207 matrix_copy( &ctx
->ProjectionMatrix
,
1208 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
1209 ctx
->NewState
|= _NEW_PROJECTION
;
1211 /* Device driver near/far values */
1213 GLfloat nearVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0];
1214 GLfloat farVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1];
1215 if (ctx
->Driver
.NearFar
) {
1216 (*ctx
->Driver
.NearFar
)( ctx
, nearVal
, farVal
);
1222 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1223 if (ctx
->TextureStackDepth
[t
]==0) {
1224 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1227 matrix_copy(&ctx
->TextureMatrix
[t
],
1228 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
1229 ctx
->NewState
|= _NEW_TEXTURE_MATRIX
;
1233 if (ctx
->ColorStackDepth
==0) {
1234 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1237 matrix_copy(&ctx
->ColorMatrix
,
1238 &ctx
->ColorStack
[--ctx
->ColorStackDepth
]);
1239 ctx
->NewState
|= _NEW_COLOR_MATRIX
;
1242 gl_problem(ctx
, "Bad matrix mode in gl_PopMatrix");
1249 _mesa_LoadIdentity( void )
1251 GET_CURRENT_CONTEXT(ctx
);
1253 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
1255 MEMCPY( mat
->m
, Identity
, 16*sizeof(GLfloat
) );
1258 MEMCPY( mat
->inv
, Identity
, 16*sizeof(GLfloat
) );
1260 mat
->type
= MATRIX_IDENTITY
;
1262 /* Have to set this to dirty to make sure we recalculate the
1263 * combined matrix later. The update_matrix in this case is a
1264 * shortcircuit anyway...
1266 mat
->flags
= MAT_DIRTY_DEPENDENTS
;
1271 _mesa_LoadMatrixf( const GLfloat
*m
)
1273 GET_CURRENT_CONTEXT(ctx
);
1275 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
1277 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
1278 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1280 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
) {
1282 #define M(row,col) m[col*4+row]
1286 GLfloat n
= (c
== 1.0 ? 0.0 : d
/ (c
- 1.0));
1287 GLfloat f
= (c
== -1.0 ? 1.0 : d
/ (c
+ 1.0));
1289 /* Need to keep a stack of near/far values in case the user
1290 * push/pops the projection matrix stack so that we can call
1291 * Driver.NearFar() after a pop.
1293 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = n
;
1294 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = f
;
1296 if (ctx
->Driver
.NearFar
) {
1297 (*ctx
->Driver
.NearFar
)( ctx
, n
, f
);
1304 _mesa_LoadMatrixd( const GLdouble
*m
)
1308 for (i
= 0; i
< 16; i
++)
1310 _mesa_LoadMatrixf(f
);
1316 * Multiply the active matrix by an arbitary matrix.
1319 _mesa_MultMatrixf( const GLfloat
*m
)
1321 GET_CURRENT_CONTEXT(ctx
);
1323 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1324 matmul4( mat
->m
, mat
->m
, m
);
1325 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1330 * Multiply the active matrix by an arbitary matrix.
1333 _mesa_MultMatrixd( const GLdouble
*m
)
1335 GET_CURRENT_CONTEXT(ctx
);
1337 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1338 matmul4fd( mat
->m
, mat
->m
, m
);
1339 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1346 * Execute a glRotate call
1349 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
1351 GET_CURRENT_CONTEXT(ctx
);
1353 if (angle
!= 0.0F
) {
1355 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
1357 gl_rotation_matrix( angle
, x
, y
, z
, m
);
1358 mat_mul_floats( mat
, m
, MAT_FLAG_ROTATION
);
1363 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
1365 _mesa_Rotatef(angle
, x
, y
, z
);
1370 * Execute a glScale call
1373 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
1375 GET_CURRENT_CONTEXT(ctx
);
1378 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glScale");
1381 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
1382 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
1383 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
1384 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
1386 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
1387 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1389 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1391 mat
->flags
|= (MAT_DIRTY_TYPE
|
1393 MAT_DIRTY_DEPENDENTS
);
1398 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
1400 _mesa_Scalef(x
, y
, z
);
1405 * Execute a glTranslate call
1408 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
1410 GET_CURRENT_CONTEXT(ctx
);
1413 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glTranslate");
1415 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1416 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1417 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1418 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1420 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
1423 MAT_DIRTY_DEPENDENTS
);
1428 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
1430 _mesa_Translatef(x
, y
, z
);
1436 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
1439 gl_matrix_transposef(tm
, m
);
1440 _mesa_LoadMatrixf(tm
);
1445 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
1448 gl_matrix_transposed(tm
, m
);
1449 _mesa_LoadMatrixd(tm
);
1454 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
1457 gl_matrix_transposef(tm
, m
);
1458 _mesa_MultMatrixf(tm
);
1463 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
1466 gl_matrix_transposed(tm
, m
);
1467 _mesa_MultMatrixd(tm
);
1472 * Called via glViewport or display list execution.
1475 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1477 GET_CURRENT_CONTEXT(ctx
);
1478 gl_Viewport(ctx
, x
, y
, width
, height
);
1484 * Define a new viewport and reallocate auxillary buffers if the size of
1485 * the window (color buffer) has changed.
1487 * XXX This is directly called by device drivers, BUT this function
1488 * may be renamed _mesa_Viewport (without ctx arg) in the future so
1489 * use of _mesa_Viewport is encouraged.
1492 gl_Viewport( GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1494 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glViewport");
1496 if (width
<0 || height
<0) {
1497 gl_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
1501 if (MESA_VERBOSE
& VERBOSE_API
)
1502 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
1504 /* clamp width, and height to implementation dependent range */
1505 width
= CLAMP( width
, 1, MAX_WIDTH
);
1506 height
= CLAMP( height
, 1, MAX_HEIGHT
);
1509 ctx
->Viewport
.X
= x
;
1510 ctx
->Viewport
.Width
= width
;
1511 ctx
->Viewport
.Y
= y
;
1512 ctx
->Viewport
.Height
= height
;
1514 /* compute scale and bias values */
1515 ctx
->Viewport
._WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
1516 ctx
->Viewport
._WindowMap
.m
[MAT_TX
] = ctx
->Viewport
._WindowMap
.m
[MAT_SX
] + x
;
1517 ctx
->Viewport
._WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
1518 ctx
->Viewport
._WindowMap
.m
[MAT_TY
] = ctx
->Viewport
._WindowMap
.m
[MAT_SY
] + y
;
1519 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = 0.5 * ctx
->Visual
.DepthMaxF
;
1520 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = 0.5 * ctx
->Visual
.DepthMaxF
;
1522 ctx
->Viewport
._WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
1523 ctx
->Viewport
._WindowMap
.type
= MATRIX_3D_NO_ROT
;
1524 ctx
->NewState
|= _NEW_VIEWPORT
;
1526 /* Check if window/buffer has been resized and if so, reallocate the
1527 * ancillary buffers.
1529 _mesa_ResizeBuffersMESA();
1531 if (ctx
->Driver
.Viewport
) {
1532 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
1539 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
1542 * nearval - specifies mapping of the near clipping plane to window
1543 * coordinates, default is 0
1544 * farval - specifies mapping of the far clipping plane to window
1545 * coordinates, default is 1
1547 * After clipping and div by w, z coords are in -1.0 to 1.0,
1548 * corresponding to near and far clipping planes. glDepthRange
1549 * specifies a linear mapping of the normalized z coords in
1550 * this range to window z coords.
1553 GET_CURRENT_CONTEXT(ctx
);
1554 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthRange");
1556 if (MESA_VERBOSE
&VERBOSE_API
)
1557 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
1559 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
1560 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
1562 ctx
->Viewport
.Near
= n
;
1563 ctx
->Viewport
.Far
= f
;
1564 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->Visual
.DepthMaxF
* ((f
- n
) / 2.0);
1565 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->Visual
.DepthMaxF
* ((f
- n
) / 2.0 + n
);
1566 ctx
->NewState
|= _NEW_VIEWPORT
;
1568 if (ctx
->Driver
.DepthRange
) {
1569 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);