1 /* $Id: matrix.c,v 1.5 1999/10/08 09:27:11 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
52 #include "GL/xf86glx.h"
61 #include "GL/xf86glx.h"
66 static const char *types
[] = {
75 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
);
78 static GLfloat Identity
[16] = {
86 static void print_matrix_floats( const GLfloat m
[16] )
90 fprintf(stderr
,"\t%f %f %f %f\n", m
[i
], m
[4+i
], m
[8+i
], m
[12+i
] );
94 void gl_print_matrix( const GLmatrix
*m
)
96 fprintf(stderr
, "Matrix type: %s, flags: %x\n", types
[m
->type
], m
->flags
);
97 print_matrix_floats(m
->m
);
99 fprintf(stderr
, "Inverse: \n");
102 print_matrix_floats(m
->inv
);
103 matmul4(prod
, m
->m
, m
->inv
);
104 fprintf(stderr
, "Mat * Inverse:\n");
105 print_matrix_floats(prod
);
107 fprintf(stderr
, " - not available\n");
114 * This matmul was contributed by Thomas Malik
116 * Perform a 4x4 matrix multiplication (product = a x b).
117 * Input: a, b - matrices to multiply
118 * Output: product - product of a and b
119 * WARNING: (product != b) assumed
120 * NOTE: (product == a) allowed
124 #define A(row,col) a[(col<<2)+row]
125 #define B(row,col) b[(col<<2)+row]
126 #define P(row,col) product[(col<<2)+row]
128 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
131 for (i
= 0; i
< 4; i
++) {
132 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
133 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
134 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
135 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
136 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
143 /* Multiply two matrices known to occupy only the top three rows,
144 * such as typical modelling matrices, and ortho matrices.
148 static void matmul34( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
151 for (i
= 0; i
< 3; i
++) {
152 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
153 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0);
154 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1);
155 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2);
156 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
;
164 static void matmul4fd( GLfloat
*product
, const GLfloat
*a
, const GLdouble
*b
)
167 for (i
= 0; i
< 4; i
++) {
168 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
169 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
170 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
171 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
172 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
182 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
183 #define MAT(m,r,c) (m)[(c)*4+(r)]
186 * Compute inverse of 4x4 transformation matrix.
187 * Code contributed by Jacques Leroy jle@star.be
188 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
190 static GLboolean
invert_matrix_general( GLmatrix
*mat
)
192 const GLfloat
*m
= mat
->m
;
193 GLfloat
*out
= mat
->inv
;
195 GLfloat m0
, m1
, m2
, m3
, s
;
196 GLfloat
*r0
, *r1
, *r2
, *r3
;
198 r0
= wtmp
[0], r1
= wtmp
[1], r2
= wtmp
[2], r3
= wtmp
[3];
200 r0
[0] = MAT(m
,0,0), r0
[1] = MAT(m
,0,1),
201 r0
[2] = MAT(m
,0,2), r0
[3] = MAT(m
,0,3),
202 r0
[4] = 1.0, r0
[5] = r0
[6] = r0
[7] = 0.0,
204 r1
[0] = MAT(m
,1,0), r1
[1] = MAT(m
,1,1),
205 r1
[2] = MAT(m
,1,2), r1
[3] = MAT(m
,1,3),
206 r1
[5] = 1.0, r1
[4] = r1
[6] = r1
[7] = 0.0,
208 r2
[0] = MAT(m
,2,0), r2
[1] = MAT(m
,2,1),
209 r2
[2] = MAT(m
,2,2), r2
[3] = MAT(m
,2,3),
210 r2
[6] = 1.0, r2
[4] = r2
[5] = r2
[7] = 0.0,
212 r3
[0] = MAT(m
,3,0), r3
[1] = MAT(m
,3,1),
213 r3
[2] = MAT(m
,3,2), r3
[3] = MAT(m
,3,3),
214 r3
[7] = 1.0, r3
[4] = r3
[5] = r3
[6] = 0.0;
216 /* choose pivot - or die */
217 if (fabs(r3
[0])>fabs(r2
[0])) SWAP_ROWS(r3
, r2
);
218 if (fabs(r2
[0])>fabs(r1
[0])) SWAP_ROWS(r2
, r1
);
219 if (fabs(r1
[0])>fabs(r0
[0])) SWAP_ROWS(r1
, r0
);
220 if (0.0 == r0
[0]) return GL_FALSE
;
222 /* eliminate first variable */
223 m1
= r1
[0]/r0
[0]; m2
= r2
[0]/r0
[0]; m3
= r3
[0]/r0
[0];
224 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
225 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
226 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
228 if (s
!= 0.0) { r1
[4] -= m1
* s
; r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
230 if (s
!= 0.0) { r1
[5] -= m1
* s
; r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
232 if (s
!= 0.0) { r1
[6] -= m1
* s
; r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
234 if (s
!= 0.0) { r1
[7] -= m1
* s
; r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
236 /* choose pivot - or die */
237 if (fabs(r3
[1])>fabs(r2
[1])) SWAP_ROWS(r3
, r2
);
238 if (fabs(r2
[1])>fabs(r1
[1])) SWAP_ROWS(r2
, r1
);
239 if (0.0 == r1
[1]) return GL_FALSE
;
241 /* eliminate second variable */
242 m2
= r2
[1]/r1
[1]; m3
= r3
[1]/r1
[1];
243 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
244 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
245 s
= r1
[4]; if (0.0 != s
) { r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
246 s
= r1
[5]; if (0.0 != s
) { r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
247 s
= r1
[6]; if (0.0 != s
) { r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
248 s
= r1
[7]; if (0.0 != s
) { r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
250 /* choose pivot - or die */
251 if (fabs(r3
[2])>fabs(r2
[2])) SWAP_ROWS(r3
, r2
);
252 if (0.0 == r2
[2]) return GL_FALSE
;
254 /* eliminate third variable */
256 r3
[3] -= m3
* r2
[3], r3
[4] -= m3
* r2
[4],
257 r3
[5] -= m3
* r2
[5], r3
[6] -= m3
* r2
[6],
261 if (0.0 == r3
[3]) return GL_FALSE
;
263 s
= 1.0/r3
[3]; /* now back substitute row 3 */
264 r3
[4] *= s
; r3
[5] *= s
; r3
[6] *= s
; r3
[7] *= s
;
266 m2
= r2
[3]; /* now back substitute row 2 */
268 r2
[4] = s
* (r2
[4] - r3
[4] * m2
), r2
[5] = s
* (r2
[5] - r3
[5] * m2
),
269 r2
[6] = s
* (r2
[6] - r3
[6] * m2
), r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
271 r1
[4] -= r3
[4] * m1
, r1
[5] -= r3
[5] * m1
,
272 r1
[6] -= r3
[6] * m1
, r1
[7] -= r3
[7] * m1
;
274 r0
[4] -= r3
[4] * m0
, r0
[5] -= r3
[5] * m0
,
275 r0
[6] -= r3
[6] * m0
, r0
[7] -= r3
[7] * m0
;
277 m1
= r1
[2]; /* now back substitute row 1 */
279 r1
[4] = s
* (r1
[4] - r2
[4] * m1
), r1
[5] = s
* (r1
[5] - r2
[5] * m1
),
280 r1
[6] = s
* (r1
[6] - r2
[6] * m1
), r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
282 r0
[4] -= r2
[4] * m0
, r0
[5] -= r2
[5] * m0
,
283 r0
[6] -= r2
[6] * m0
, r0
[7] -= r2
[7] * m0
;
285 m0
= r0
[1]; /* now back substitute row 0 */
287 r0
[4] = s
* (r0
[4] - r1
[4] * m0
), r0
[5] = s
* (r0
[5] - r1
[5] * m0
),
288 r0
[6] = s
* (r0
[6] - r1
[6] * m0
), r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
290 MAT(out
,0,0) = r0
[4]; MAT(out
,0,1) = r0
[5],
291 MAT(out
,0,2) = r0
[6]; MAT(out
,0,3) = r0
[7],
292 MAT(out
,1,0) = r1
[4]; MAT(out
,1,1) = r1
[5],
293 MAT(out
,1,2) = r1
[6]; MAT(out
,1,3) = r1
[7],
294 MAT(out
,2,0) = r2
[4]; MAT(out
,2,1) = r2
[5],
295 MAT(out
,2,2) = r2
[6]; MAT(out
,2,3) = r2
[7],
296 MAT(out
,3,0) = r3
[4]; MAT(out
,3,1) = r3
[5],
297 MAT(out
,3,2) = r3
[6]; MAT(out
,3,3) = r3
[7];
303 /* Adapted from graphics gems II.
305 static GLboolean
invert_matrix_3d_general( GLmatrix
*mat
)
307 const GLfloat
*in
= mat
->m
;
308 GLfloat
*out
= mat
->inv
;
312 /* Calculate the determinant of upper left 3x3 submatrix and
313 * determine if the matrix is singular.
316 t
= MAT(in
,0,0) * MAT(in
,1,1) * MAT(in
,2,2);
317 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
319 t
= MAT(in
,1,0) * MAT(in
,2,1) * MAT(in
,0,2);
320 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
322 t
= MAT(in
,2,0) * MAT(in
,0,1) * MAT(in
,1,2);
323 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
325 t
= -MAT(in
,2,0) * MAT(in
,1,1) * MAT(in
,0,2);
326 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
328 t
= -MAT(in
,1,0) * MAT(in
,0,1) * MAT(in
,2,2);
329 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
331 t
= -MAT(in
,0,0) * MAT(in
,2,1) * MAT(in
,1,2);
332 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
340 MAT(out
,0,0) = ( (MAT(in
,1,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,1,2) )*det
);
341 MAT(out
,0,1) = (- (MAT(in
,0,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,0,2) )*det
);
342 MAT(out
,0,2) = ( (MAT(in
,0,1)*MAT(in
,1,2) - MAT(in
,1,1)*MAT(in
,0,2) )*det
);
343 MAT(out
,1,0) = (- (MAT(in
,1,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,1,2) )*det
);
344 MAT(out
,1,1) = ( (MAT(in
,0,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,0,2) )*det
);
345 MAT(out
,1,2) = (- (MAT(in
,0,0)*MAT(in
,1,2) - MAT(in
,1,0)*MAT(in
,0,2) )*det
);
346 MAT(out
,2,0) = ( (MAT(in
,1,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,1,1) )*det
);
347 MAT(out
,2,1) = (- (MAT(in
,0,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,0,1) )*det
);
348 MAT(out
,2,2) = ( (MAT(in
,0,0)*MAT(in
,1,1) - MAT(in
,1,0)*MAT(in
,0,1) )*det
);
350 /* Do the translation part */
351 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
352 MAT(in
,1,3) * MAT(out
,0,1) +
353 MAT(in
,2,3) * MAT(out
,0,2) );
354 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
355 MAT(in
,1,3) * MAT(out
,1,1) +
356 MAT(in
,2,3) * MAT(out
,1,2) );
357 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
358 MAT(in
,1,3) * MAT(out
,2,1) +
359 MAT(in
,2,3) * MAT(out
,2,2) );
365 static GLboolean
invert_matrix_3d( GLmatrix
*mat
)
367 const GLfloat
*in
= mat
->m
;
368 GLfloat
*out
= mat
->inv
;
370 if (!TEST_MAT_FLAGS(mat
, MAT_FLAGS_ANGLE_PRESERVING
))
372 return invert_matrix_3d_general( mat
);
375 if (mat
->flags
& MAT_FLAG_UNIFORM_SCALE
)
377 GLfloat scale
= (MAT(in
,0,0) * MAT(in
,0,0) +
378 MAT(in
,0,1) * MAT(in
,0,1) +
379 MAT(in
,0,2) * MAT(in
,0,2));
386 /* Transpose and scale the 3 by 3 upper-left submatrix. */
387 MAT(out
,0,0) = scale
* MAT(in
,0,0);
388 MAT(out
,1,0) = scale
* MAT(in
,0,1);
389 MAT(out
,2,0) = scale
* MAT(in
,0,2);
390 MAT(out
,0,1) = scale
* MAT(in
,1,0);
391 MAT(out
,1,1) = scale
* MAT(in
,1,1);
392 MAT(out
,2,1) = scale
* MAT(in
,1,2);
393 MAT(out
,0,2) = scale
* MAT(in
,2,0);
394 MAT(out
,1,2) = scale
* MAT(in
,2,1);
395 MAT(out
,2,2) = scale
* MAT(in
,2,2);
397 else if (mat
->flags
& MAT_FLAG_ROTATION
)
399 /* Transpose the 3 by 3 upper-left submatrix. */
400 MAT(out
,0,0) = MAT(in
,0,0);
401 MAT(out
,1,0) = MAT(in
,0,1);
402 MAT(out
,2,0) = MAT(in
,0,2);
403 MAT(out
,0,1) = MAT(in
,1,0);
404 MAT(out
,1,1) = MAT(in
,1,1);
405 MAT(out
,2,1) = MAT(in
,1,2);
406 MAT(out
,0,2) = MAT(in
,2,0);
407 MAT(out
,1,2) = MAT(in
,2,1);
408 MAT(out
,2,2) = MAT(in
,2,2);
410 else /* pure translation */
412 MEMCPY( out
, Identity
, sizeof(Identity
) );
413 MAT(out
,0,3) = - MAT(in
,0,3);
414 MAT(out
,1,3) = - MAT(in
,1,3);
415 MAT(out
,2,3) = - MAT(in
,2,3);
419 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
421 /* Do the translation part */
422 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
423 MAT(in
,1,3) * MAT(out
,0,1) +
424 MAT(in
,2,3) * MAT(out
,0,2) );
425 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
426 MAT(in
,1,3) * MAT(out
,1,1) +
427 MAT(in
,2,3) * MAT(out
,1,2) );
428 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
429 MAT(in
,1,3) * MAT(out
,2,1) +
430 MAT(in
,2,3) * MAT(out
,2,2) );
434 MAT(out
,0,3) = MAT(out
,1,3) = MAT(out
,2,3) = 0.0;
442 static GLboolean
invert_matrix_identity( GLmatrix
*mat
)
444 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
449 static GLboolean
invert_matrix_3d_no_rot( GLmatrix
*mat
)
451 const GLfloat
*in
= mat
->m
;
452 GLfloat
*out
= mat
->inv
;
454 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0 || MAT(in
,2,2) == 0 )
457 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
458 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
459 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
460 MAT(out
,2,2) = 1.0 / MAT(in
,2,2);
462 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
464 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
465 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
466 MAT(out
,2,3) = - (MAT(in
,2,3) * MAT(out
,2,2));
473 static GLboolean
invert_matrix_2d_no_rot( GLmatrix
*mat
)
475 const GLfloat
*in
= mat
->m
;
476 GLfloat
*out
= mat
->inv
;
478 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0)
481 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
482 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
483 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
485 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
487 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
488 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
495 static GLboolean
invert_matrix_perspective( GLmatrix
*mat
)
497 const GLfloat
*in
= mat
->m
;
498 GLfloat
*out
= mat
->inv
;
500 if (MAT(in
,2,3) == 0)
503 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
505 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
506 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
508 MAT(out
,0,3) = MAT(in
,0,2);
509 MAT(out
,1,3) = MAT(in
,1,2);
514 MAT(out
,3,2) = 1.0 / MAT(in
,2,3);
515 MAT(out
,3,3) = MAT(in
,2,2) * MAT(out
,3,2);
521 typedef GLboolean (*inv_mat_func
)( GLmatrix
*mat
);
523 static inv_mat_func inv_mat_tab
[7] = {
524 invert_matrix_general
,
525 invert_matrix_identity
,
526 invert_matrix_3d_no_rot
,
527 invert_matrix_perspective
,
528 invert_matrix_3d
, /* lazy! */
529 invert_matrix_2d_no_rot
,
534 GLboolean
gl_matrix_invert( GLmatrix
*mat
)
536 if (inv_mat_tab
[mat
->type
](mat
)) {
538 GLmatrix m
; m
.inv
= 0; m
.type
= 0; m
.flags
= 0;
539 matmul4( m
.m
, mat
->m
, mat
->inv
);
540 printf("inverted matrix of type %s:\n", types
[mat
->type
]);
541 gl_print_matrix( mat
);
542 gl_print_matrix( &m
);
546 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
554 * Generate a 4x4 transformation matrix from glRotate parameters.
556 void gl_rotation_matrix( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
,
559 /* This function contributed by Erich Boleyn (erich@uruk.org) */
561 GLfloat xx
, yy
, zz
, xy
, yz
, zx
, xs
, ys
, zs
, one_c
;
563 s
= sin( angle
* DEG2RAD
);
564 c
= cos( angle
* DEG2RAD
);
566 mag
= GL_SQRT( x
*x
+ y
*y
+ z
*z
);
569 /* generate an identity matrix and return */
570 MEMCPY(m
, Identity
, sizeof(GLfloat
)*16);
578 #define M(row,col) m[col*4+row]
581 * Arbitrary axis rotation matrix.
583 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
584 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
585 * (which is about the X-axis), and the two composite transforms
586 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
587 * from the arbitrary axis to the X-axis then back. They are
588 * all elementary rotations.
590 * Rz' is a rotation about the Z-axis, to bring the axis vector
591 * into the x-z plane. Then Ry' is applied, rotating about the
592 * Y-axis to bring the axis vector parallel with the X-axis. The
593 * rotation about the X-axis is then performed. Ry and Rz are
594 * simply the respective inverse transforms to bring the arbitrary
595 * axis back to it's original orientation. The first transforms
596 * Rz' and Ry' are considered inverses, since the data from the
597 * arbitrary axis gives you info on how to get to it, not how
598 * to get away from it, and an inverse must be applied.
600 * The basic calculation used is to recognize that the arbitrary
601 * axis vector (x, y, z), since it is of unit length, actually
602 * represents the sines and cosines of the angles to rotate the
603 * X-axis to the same orientation, with theta being the angle about
604 * Z and phi the angle about Y (in the order described above)
607 * cos ( theta ) = x / sqrt ( 1 - z^2 )
608 * sin ( theta ) = y / sqrt ( 1 - z^2 )
610 * cos ( phi ) = sqrt ( 1 - z^2 )
613 * Note that cos ( phi ) can further be inserted to the above
616 * cos ( theta ) = x / cos ( phi )
617 * sin ( theta ) = y / sin ( phi )
619 * ...etc. Because of those relations and the standard trigonometric
620 * relations, it is pssible to reduce the transforms down to what
621 * is used below. It may be that any primary axis chosen will give the
622 * same results (modulo a sign convention) using thie method.
624 * Particularly nice is to notice that all divisions that might
625 * have caused trouble when parallel to certain planes or
626 * axis go away with care paid to reducing the expressions.
627 * After checking, it does perform correctly under all cases, since
628 * in all the cases of division where the denominator would have
629 * been zero, the numerator would have been zero as well, giving
630 * the expected result.
644 M(0,0) = (one_c
* xx
) + c
;
645 M(0,1) = (one_c
* xy
) - zs
;
646 M(0,2) = (one_c
* zx
) + ys
;
649 M(1,0) = (one_c
* xy
) + zs
;
650 M(1,1) = (one_c
* yy
) + c
;
651 M(1,2) = (one_c
* yz
) - xs
;
654 M(2,0) = (one_c
* zx
) - ys
;
655 M(2,1) = (one_c
* yz
) + xs
;
656 M(2,2) = (one_c
* zz
) + c
;
667 #define ZERO(x) (1<<x)
668 #define ONE(x) (1<<(x+16))
670 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
671 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
673 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
674 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
675 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
676 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
678 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
679 ZERO(1) | ZERO(9) | \
680 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
681 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
683 #define MASK_2D ( ZERO(8) | \
685 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
686 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
689 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
690 ZERO(1) | ZERO(9) | \
691 ZERO(2) | ZERO(6) | \
692 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
697 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
700 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
701 ZERO(1) | ZERO(13) |\
702 ZERO(2) | ZERO(6) | \
703 ZERO(3) | ZERO(7) | ZERO(15) )
705 #define SQ(x) ((x)*(x))
707 /* Determine type and flags from scratch. This is expensive enough to
708 * only want to do it once.
710 static void analyze_from_scratch( GLmatrix
*mat
)
712 const GLfloat
*m
= mat
->m
;
716 for (i
= 0 ; i
< 16 ; i
++)
718 if (m
[i
] == 0.0) mask
|= (1<<i
);
721 if (m
[0] == 1.0F
) mask
|= (1<<16);
722 if (m
[5] == 1.0F
) mask
|= (1<<21);
723 if (m
[10] == 1.0F
) mask
|= (1<<26);
724 if (m
[15] == 1.0F
) mask
|= (1<<31);
726 mat
->flags
&= ~MAT_FLAGS_GEOMETRY
;
728 /* Check for translation - no-one really cares
730 if ((mask
& MASK_NO_TRX
) != MASK_NO_TRX
)
731 mat
->flags
|= MAT_FLAG_TRANSLATION
;
735 if (mask
== MASK_IDENTITY
) {
736 mat
->type
= MATRIX_IDENTITY
;
738 else if ((mask
& MASK_2D_NO_ROT
) == MASK_2D_NO_ROT
)
740 mat
->type
= MATRIX_2D_NO_ROT
;
742 if ((mask
& MASK_NO_2D_SCALE
) != MASK_NO_2D_SCALE
)
743 mat
->flags
= MAT_FLAG_GENERAL_SCALE
;
745 else if ((mask
& MASK_2D
) == MASK_2D
)
747 GLfloat mm
= DOT2(m
, m
);
748 GLfloat m4m4
= DOT2(m
+4,m
+4);
749 GLfloat mm4
= DOT2(m
,m
+4);
751 mat
->type
= MATRIX_2D
;
753 /* Check for scale */
754 if (SQ(mm
-1) > SQ(1e-6) ||
755 SQ(m4m4
-1) > SQ(1e-6))
756 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
758 /* Check for rotation */
759 if (SQ(mm4
) > SQ(1e-6))
760 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
762 mat
->flags
|= MAT_FLAG_ROTATION
;
765 else if ((mask
& MASK_3D_NO_ROT
) == MASK_3D_NO_ROT
)
767 mat
->type
= MATRIX_3D_NO_ROT
;
769 /* Check for scale */
770 if (SQ(m
[0]-m
[5]) < SQ(1e-6) &&
771 SQ(m
[0]-m
[10]) < SQ(1e-6)) {
772 if (SQ(m
[0]-1.0) > SQ(1e-6))
773 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
775 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
777 else if ((mask
& MASK_3D
) == MASK_3D
)
779 GLfloat c1
= DOT3(m
,m
);
780 GLfloat c2
= DOT3(m
+4,m
+4);
781 GLfloat c3
= DOT3(m
+8,m
+8);
782 GLfloat d1
= DOT3(m
, m
+4);
785 mat
->type
= MATRIX_3D
;
787 /* Check for scale */
788 if (SQ(c1
-c2
) < SQ(1e-6) && SQ(c1
-c3
) < SQ(1e-6)) {
789 if (SQ(c1
-1.0) > SQ(1e-6))
790 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
791 /* else no scale at all */
793 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
795 /* Check for rotation */
796 if (SQ(d1
) < SQ(1e-6)) {
797 CROSS3( cp
, m
, m
+4 );
798 SUB_3V( cp
, cp
, (m
+8) );
799 if (LEN_SQUARED_3FV(cp
) < SQ(1e-6))
800 mat
->flags
|= MAT_FLAG_ROTATION
;
802 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
805 mat
->flags
|= MAT_FLAG_GENERAL_3D
; /* shear, etc */
807 else if ((mask
& MASK_PERSPECTIVE
) == MASK_PERSPECTIVE
&& m
[11]==-1.0F
)
809 mat
->type
= MATRIX_PERSPECTIVE
;
810 mat
->flags
|= MAT_FLAG_GENERAL
;
813 mat
->type
= MATRIX_GENERAL
;
814 mat
->flags
|= MAT_FLAG_GENERAL
;
819 /* Analyse a matrix given that its flags are accurate - this is the
820 * more common operation, hopefully.
822 static void analyze_from_flags( GLmatrix
*mat
)
824 const GLfloat
*m
= mat
->m
;
826 if (TEST_MAT_FLAGS(mat
, 0)) {
827 mat
->type
= MATRIX_IDENTITY
;
829 else if (TEST_MAT_FLAGS(mat
, (MAT_FLAG_TRANSLATION
|
830 MAT_FLAG_UNIFORM_SCALE
|
831 MAT_FLAG_GENERAL_SCALE
)))
833 if ( m
[10]==1.0F
&& m
[14]==0.0F
) {
834 mat
->type
= MATRIX_2D_NO_ROT
;
837 mat
->type
= MATRIX_3D_NO_ROT
;
840 else if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
)) {
843 && m
[2]==0.0F
&& m
[6]==0.0F
&& m
[10]==1.0F
&& m
[14]==0.0F
)
845 mat
->type
= MATRIX_2D
;
849 mat
->type
= MATRIX_3D
;
852 else if ( m
[4]==0.0F
&& m
[12]==0.0F
853 && m
[1]==0.0F
&& m
[13]==0.0F
854 && m
[2]==0.0F
&& m
[6]==0.0F
855 && m
[3]==0.0F
&& m
[7]==0.0F
&& m
[11]==-1.0F
&& m
[15]==0.0F
)
857 mat
->type
= MATRIX_PERSPECTIVE
;
860 mat
->type
= MATRIX_GENERAL
;
866 void gl_matrix_analyze( GLmatrix
*mat
)
868 if (mat
->flags
& MAT_DIRTY_TYPE
) {
869 if (mat
->flags
& MAT_DIRTY_FLAGS
)
870 analyze_from_scratch( mat
);
872 analyze_from_flags( mat
);
875 if (mat
->inv
&& (mat
->flags
& MAT_DIRTY_INVERSE
)) {
876 gl_matrix_invert( mat
);
879 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
885 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
887 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where); \
888 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
889 switch (ctx->Transform.MatrixMode) { \
891 mat = &ctx->ModelView; \
892 flags |= NEW_MODELVIEW; \
894 case GL_PROJECTION: \
895 mat = &ctx->ProjectionMatrix; \
896 flags |= NEW_PROJECTION; \
899 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
900 flags |= NEW_TEXTURE_MATRIX; \
903 gl_problem(ctx, where); \
908 void gl_Frustum( GLcontext
*ctx
,
909 GLdouble left
, GLdouble right
,
910 GLdouble bottom
, GLdouble top
,
911 GLdouble nearval
, GLdouble farval
)
913 GLfloat x
, y
, a
, b
, c
, d
;
917 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glFrustrum" );
919 if ((nearval
<=0.0 || farval
<=0.0) || (nearval
== farval
) || (left
== right
) || (top
== bottom
)) {
920 gl_error( ctx
, GL_INVALID_VALUE
, "glFrustum(near or far)" );
924 x
= (2.0*nearval
) / (right
-left
);
925 y
= (2.0*nearval
) / (top
-bottom
);
926 a
= (right
+left
) / (right
-left
);
927 b
= (top
+bottom
) / (top
-bottom
);
928 c
= -(farval
+nearval
) / ( farval
-nearval
);
929 d
= -(2.0*farval
*nearval
) / (farval
-nearval
); /* error? */
931 #define M(row,col) m[col*4+row]
932 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
933 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
934 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
935 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
939 gl_mat_mul_floats( mat
, m
, MAT_FLAG_PERSPECTIVE
);
942 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
)
944 /* Need to keep a stack of near/far values in case the user push/pops
945 * the projection matrix stack so that we can call Driver.NearFar()
948 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = nearval
;
949 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = farval
;
951 if (ctx
->Driver
.NearFar
) {
952 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
958 void gl_Ortho( GLcontext
*ctx
,
959 GLdouble left
, GLdouble right
,
960 GLdouble bottom
, GLdouble top
,
961 GLdouble nearval
, GLdouble farval
)
968 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
970 if ((left
== right
) || (bottom
== top
) || (nearval
== farval
)) {
971 gl_error( ctx
, GL_INVALID_VALUE
, "gl_Ortho((l = r) or (b = top) or (n=f)" );
975 x
= 2.0 / (right
-left
);
976 y
= 2.0 / (top
-bottom
);
977 z
= -2.0 / (farval
-nearval
);
978 tx
= -(right
+left
) / (right
-left
);
979 ty
= -(top
+bottom
) / (top
-bottom
);
980 tz
= -(farval
+nearval
) / (farval
-nearval
);
982 #define M(row,col) m[col*4+row]
983 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = 0.0F
; M(0,3) = tx
;
984 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = 0.0F
; M(1,3) = ty
;
985 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = z
; M(2,3) = tz
;
986 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = 0.0F
; M(3,3) = 1.0F
;
989 gl_mat_mul_floats( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
991 if (ctx
->Driver
.NearFar
) {
992 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
997 void gl_MatrixMode( GLcontext
*ctx
, GLenum mode
)
999 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glMatrixMode");
1004 ctx
->Transform
.MatrixMode
= mode
;
1007 gl_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
1013 void gl_PushMatrix( GLcontext
*ctx
)
1015 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushMatrix");
1017 if (MESA_VERBOSE
&VERBOSE_API
)
1018 fprintf(stderr
, "glPushMatrix %s\n",
1019 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1021 switch (ctx
->Transform
.MatrixMode
) {
1023 if (ctx
->ModelViewStackDepth
>=MAX_MODELVIEW_STACK_DEPTH
-1) {
1024 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1027 gl_matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
1031 if (ctx
->ProjectionStackDepth
>=MAX_PROJECTION_STACK_DEPTH
) {
1032 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1035 gl_matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
1036 &ctx
->ProjectionMatrix
);
1038 /* Save near and far projection values */
1039 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0]
1040 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][0];
1041 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1]
1042 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][1];
1046 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1047 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
) {
1048 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1051 gl_matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
1052 &ctx
->TextureMatrix
[t
] );
1056 gl_problem(ctx
, "Bad matrix mode in gl_PushMatrix");
1062 void gl_PopMatrix( GLcontext
*ctx
)
1064 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopMatrix");
1066 if (MESA_VERBOSE
&VERBOSE_API
)
1067 fprintf(stderr
, "glPopMatrix %s\n",
1068 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1070 switch (ctx
->Transform
.MatrixMode
) {
1072 if (ctx
->ModelViewStackDepth
==0) {
1073 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1076 gl_matrix_copy( &ctx
->ModelView
,
1077 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
1078 ctx
->NewState
|= NEW_MODELVIEW
;
1081 if (ctx
->ProjectionStackDepth
==0) {
1082 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1086 gl_matrix_copy( &ctx
->ProjectionMatrix
,
1087 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
1088 ctx
->NewState
|= NEW_PROJECTION
;
1090 /* Device driver near/far values */
1092 GLfloat nearVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0];
1093 GLfloat farVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1];
1094 if (ctx
->Driver
.NearFar
) {
1095 (*ctx
->Driver
.NearFar
)( ctx
, nearVal
, farVal
);
1101 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1102 if (ctx
->TextureStackDepth
[t
]==0) {
1103 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1106 gl_matrix_copy(&ctx
->TextureMatrix
[t
],
1107 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
1111 gl_problem(ctx
, "Bad matrix mode in gl_PopMatrix");
1117 void gl_LoadIdentity( GLcontext
*ctx
)
1120 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
1122 MEMCPY( mat
->m
, Identity
, 16*sizeof(GLfloat
) );
1125 MEMCPY( mat
->inv
, Identity
, 16*sizeof(GLfloat
) );
1127 mat
->type
= MATRIX_IDENTITY
;
1129 /* Have to set this to dirty to make sure we recalculate the
1130 * combined matrix later. The update_matrix in this case is a
1131 * shortcircuit anyway...
1133 mat
->flags
= MAT_DIRTY_DEPENDENTS
;
1137 void gl_LoadMatrixf( GLcontext
*ctx
, const GLfloat
*m
)
1140 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
1142 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
1143 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1145 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
) {
1147 #define M(row,col) m[col*4+row]
1151 GLfloat n
= (c
== 1.0 ? 0.0 : d
/ (c
- 1.0));
1152 GLfloat f
= (c
== -1.0 ? 1.0 : d
/ (c
+ 1.0));
1154 /* Need to keep a stack of near/far values in case the user
1155 * push/pops the projection matrix stack so that we can call
1156 * Driver.NearFar() after a pop.
1158 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = n
;
1159 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = f
;
1161 if (ctx
->Driver
.NearFar
) {
1162 (*ctx
->Driver
.NearFar
)( ctx
, n
, f
);
1170 * Multiply the active matrix by an arbitary matrix.
1172 void gl_MultMatrixf( GLcontext
*ctx
, const GLfloat
*m
)
1175 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1176 matmul4( mat
->m
, mat
->m
, m
);
1177 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1182 * Multiply the active matrix by an arbitary matrix.
1184 void gl_MultMatrixd( GLcontext
*ctx
, const GLdouble
*m
)
1187 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1188 matmul4fd( mat
->m
, mat
->m
, m
);
1189 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1196 * Multiply a matrix by an array of floats with known properties.
1198 void gl_mat_mul_floats( GLmatrix
*mat
, const GLfloat
*m
, GLuint flags
)
1200 mat
->flags
|= (flags
|
1203 MAT_DIRTY_DEPENDENTS
);
1205 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1206 matmul34( mat
->m
, mat
->m
, m
);
1208 matmul4( mat
->m
, mat
->m
, m
);
1213 * Multiply a matrix by an array of floats with known properties.
1215 void gl_mat_mul_mat( GLmatrix
*mat
, const GLmatrix
*m
)
1217 mat
->flags
|= (m
->flags
|
1220 MAT_DIRTY_DEPENDENTS
);
1222 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1223 matmul34( mat
->m
, mat
->m
, m
->m
);
1225 matmul4( mat
->m
, mat
->m
, m
->m
);
1231 * Execute a glRotate call
1233 void gl_Rotatef( GLcontext
*ctx
,
1234 GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
1237 if (angle
!= 0.0F
) {
1239 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
1241 gl_rotation_matrix( angle
, x
, y
, z
, m
);
1242 gl_mat_mul_floats( mat
, m
, MAT_FLAG_ROTATION
);
1247 * Execute a glScale call
1249 void gl_Scalef( GLcontext
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
)
1253 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glScale");
1256 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
1257 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
1258 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
1259 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
1261 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
1262 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1264 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1266 mat
->flags
|= (MAT_DIRTY_TYPE
|
1268 MAT_DIRTY_DEPENDENTS
);
1272 * Execute a glTranslate call
1274 void gl_Translatef( GLcontext
*ctx
, GLfloat x
, GLfloat y
, GLfloat z
)
1278 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glTranslate");
1280 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1281 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1282 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1283 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1285 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
1288 MAT_DIRTY_DEPENDENTS
);
1293 * Define a new viewport and reallocate auxillary buffers if the size of
1294 * the window (color buffer) has changed.
1296 void gl_Viewport( GLcontext
*ctx
,
1297 GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1299 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glViewport");
1301 if (width
<0 || height
<0) {
1302 gl_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
1306 if (MESA_VERBOSE
& VERBOSE_API
)
1307 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
1309 /* clamp width, and height to implementation dependent range */
1310 width
= CLAMP( width
, 1, MAX_WIDTH
);
1311 height
= CLAMP( height
, 1, MAX_HEIGHT
);
1314 ctx
->Viewport
.X
= x
;
1315 ctx
->Viewport
.Width
= width
;
1316 ctx
->Viewport
.Y
= y
;
1317 ctx
->Viewport
.Height
= height
;
1319 /* compute scale and bias values */
1320 ctx
->Viewport
.WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
1321 ctx
->Viewport
.WindowMap
.m
[MAT_TX
] = ctx
->Viewport
.WindowMap
.m
[MAT_SX
] + x
;
1322 ctx
->Viewport
.WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
1323 ctx
->Viewport
.WindowMap
.m
[MAT_TY
] = ctx
->Viewport
.WindowMap
.m
[MAT_SY
] + y
;
1324 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = 0.5 * DEPTH_SCALE
;
1325 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = 0.5 * DEPTH_SCALE
;
1327 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
1328 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
1330 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1331 ctx
->NewState
|= NEW_VIEWPORT
;
1333 /* Check if window/buffer has been resized and if so, reallocate the
1334 * ancillary buffers.
1336 gl_ResizeBuffersMESA(ctx
);
1339 ctx
->RasterMask
&= ~WINCLIP_BIT
;
1341 if ( ctx
->Viewport
.X
<0
1342 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->Buffer
->Width
1343 || ctx
->Viewport
.Y
<0
1344 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->Buffer
->Height
) {
1345 ctx
->RasterMask
|= WINCLIP_BIT
;
1349 if (ctx
->Driver
.Viewport
) {
1350 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
1356 void gl_DepthRange( GLcontext
*ctx
, GLclampd nearval
, GLclampd farval
)
1359 * nearval - specifies mapping of the near clipping plane to window
1360 * coordinates, default is 0
1361 * farval - specifies mapping of the far clipping plane to window
1362 * coordinates, default is 1
1364 * After clipping and div by w, z coords are in -1.0 to 1.0,
1365 * corresponding to near and far clipping planes. glDepthRange
1366 * specifies a linear mapping of the normalized z coords in
1367 * this range to window z coords.
1371 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthRange");
1373 if (MESA_VERBOSE
&VERBOSE_API
)
1374 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
1376 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
1377 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
1379 ctx
->Viewport
.Near
= n
;
1380 ctx
->Viewport
.Far
= f
;
1381 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = DEPTH_SCALE
* ((f
- n
) / 2.0);
1382 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = DEPTH_SCALE
* ((f
- n
) / 2.0 + n
);
1384 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1386 if (ctx
->Driver
.DepthRange
) {
1387 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);
1392 void gl_calculate_model_project_matrix( GLcontext
*ctx
)
1394 gl_matrix_mul( &ctx
->ModelProjectMatrix
,
1395 &ctx
->ProjectionMatrix
,
1398 gl_matrix_analyze( &ctx
->ModelProjectMatrix
);
1402 void gl_matrix_ctr( GLmatrix
*m
)
1405 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1406 m
->type
= MATRIX_IDENTITY
;
1407 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1410 void gl_matrix_dtr( GLmatrix
*m
)
1418 void gl_matrix_set_identity( GLmatrix
*m
)
1420 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1421 m
->type
= MATRIX_IDENTITY
;
1422 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1426 void gl_matrix_alloc_inv( GLmatrix
*m
)
1429 m
->inv
= (GLfloat
*)malloc(16*sizeof(GLfloat
));
1430 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
1434 void gl_matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
1436 MEMCPY( to
->m
, from
->m
, sizeof(Identity
));
1437 to
->flags
= from
->flags
| MAT_DIRTY_DEPENDENTS
;
1438 to
->type
= from
->type
;
1441 if (from
->inv
== 0) {
1442 gl_matrix_invert( to
);
1444 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
1449 void gl_matrix_mul( GLmatrix
*dest
, const GLmatrix
*a
, const GLmatrix
*b
)
1451 dest
->flags
= (a
->flags
|
1455 MAT_DIRTY_DEPENDENTS
);
1457 if (TEST_MAT_FLAGS(dest
, MAT_FLAGS_3D
))
1458 matmul34( dest
->m
, a
->m
, b
->m
);
1460 matmul4( dest
->m
, a
->m
, b
->m
);