1 /* $Id: matrix.c,v 1.13 2000/01/13 00:27:05 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
53 static const char *types
[] = {
62 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
);
65 static GLfloat Identity
[16] = {
73 static void print_matrix_floats( const GLfloat m
[16] )
77 fprintf(stderr
,"\t%f %f %f %f\n", m
[i
], m
[4+i
], m
[8+i
], m
[12+i
] );
81 void gl_print_matrix( const GLmatrix
*m
)
83 fprintf(stderr
, "Matrix type: %s, flags: %x\n", types
[m
->type
], m
->flags
);
84 print_matrix_floats(m
->m
);
86 fprintf(stderr
, "Inverse: \n");
89 print_matrix_floats(m
->inv
);
90 matmul4(prod
, m
->m
, m
->inv
);
91 fprintf(stderr
, "Mat * Inverse:\n");
92 print_matrix_floats(prod
);
94 fprintf(stderr
, " - not available\n");
101 * This matmul was contributed by Thomas Malik
103 * Perform a 4x4 matrix multiplication (product = a x b).
104 * Input: a, b - matrices to multiply
105 * Output: product - product of a and b
106 * WARNING: (product != b) assumed
107 * NOTE: (product == a) allowed
111 #define A(row,col) a[(col<<2)+row]
112 #define B(row,col) b[(col<<2)+row]
113 #define P(row,col) product[(col<<2)+row]
115 static void matmul4( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
118 for (i
= 0; i
< 4; i
++) {
119 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
120 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
121 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
122 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
123 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
130 /* Multiply two matrices known to occupy only the top three rows,
131 * such as typical modelling matrices, and ortho matrices.
135 static void matmul34( GLfloat
*product
, const GLfloat
*a
, const GLfloat
*b
)
138 for (i
= 0; i
< 3; i
++) {
139 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
140 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0);
141 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1);
142 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2);
143 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
;
151 static void matmul4fd( GLfloat
*product
, const GLfloat
*a
, const GLdouble
*b
)
154 for (i
= 0; i
< 4; i
++) {
155 GLfloat ai0
=A(i
,0), ai1
=A(i
,1), ai2
=A(i
,2), ai3
=A(i
,3);
156 P(i
,0) = ai0
* B(0,0) + ai1
* B(1,0) + ai2
* B(2,0) + ai3
* B(3,0);
157 P(i
,1) = ai0
* B(0,1) + ai1
* B(1,1) + ai2
* B(2,1) + ai3
* B(3,1);
158 P(i
,2) = ai0
* B(0,2) + ai1
* B(1,2) + ai2
* B(2,2) + ai3
* B(3,2);
159 P(i
,3) = ai0
* B(0,3) + ai1
* B(1,3) + ai2
* B(2,3) + ai3
* B(3,3);
169 #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
170 #define MAT(m,r,c) (m)[(c)*4+(r)]
173 * Compute inverse of 4x4 transformation matrix.
174 * Code contributed by Jacques Leroy jle@star.be
175 * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
177 static GLboolean
invert_matrix_general( GLmatrix
*mat
)
179 const GLfloat
*m
= mat
->m
;
180 GLfloat
*out
= mat
->inv
;
182 GLfloat m0
, m1
, m2
, m3
, s
;
183 GLfloat
*r0
, *r1
, *r2
, *r3
;
185 r0
= wtmp
[0], r1
= wtmp
[1], r2
= wtmp
[2], r3
= wtmp
[3];
187 r0
[0] = MAT(m
,0,0), r0
[1] = MAT(m
,0,1),
188 r0
[2] = MAT(m
,0,2), r0
[3] = MAT(m
,0,3),
189 r0
[4] = 1.0, r0
[5] = r0
[6] = r0
[7] = 0.0,
191 r1
[0] = MAT(m
,1,0), r1
[1] = MAT(m
,1,1),
192 r1
[2] = MAT(m
,1,2), r1
[3] = MAT(m
,1,3),
193 r1
[5] = 1.0, r1
[4] = r1
[6] = r1
[7] = 0.0,
195 r2
[0] = MAT(m
,2,0), r2
[1] = MAT(m
,2,1),
196 r2
[2] = MAT(m
,2,2), r2
[3] = MAT(m
,2,3),
197 r2
[6] = 1.0, r2
[4] = r2
[5] = r2
[7] = 0.0,
199 r3
[0] = MAT(m
,3,0), r3
[1] = MAT(m
,3,1),
200 r3
[2] = MAT(m
,3,2), r3
[3] = MAT(m
,3,3),
201 r3
[7] = 1.0, r3
[4] = r3
[5] = r3
[6] = 0.0;
203 /* choose pivot - or die */
204 if (fabs(r3
[0])>fabs(r2
[0])) SWAP_ROWS(r3
, r2
);
205 if (fabs(r2
[0])>fabs(r1
[0])) SWAP_ROWS(r2
, r1
);
206 if (fabs(r1
[0])>fabs(r0
[0])) SWAP_ROWS(r1
, r0
);
207 if (0.0 == r0
[0]) return GL_FALSE
;
209 /* eliminate first variable */
210 m1
= r1
[0]/r0
[0]; m2
= r2
[0]/r0
[0]; m3
= r3
[0]/r0
[0];
211 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
212 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
213 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
215 if (s
!= 0.0) { r1
[4] -= m1
* s
; r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
217 if (s
!= 0.0) { r1
[5] -= m1
* s
; r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
219 if (s
!= 0.0) { r1
[6] -= m1
* s
; r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
221 if (s
!= 0.0) { r1
[7] -= m1
* s
; r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
223 /* choose pivot - or die */
224 if (fabs(r3
[1])>fabs(r2
[1])) SWAP_ROWS(r3
, r2
);
225 if (fabs(r2
[1])>fabs(r1
[1])) SWAP_ROWS(r2
, r1
);
226 if (0.0 == r1
[1]) return GL_FALSE
;
228 /* eliminate second variable */
229 m2
= r2
[1]/r1
[1]; m3
= r3
[1]/r1
[1];
230 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
231 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
232 s
= r1
[4]; if (0.0 != s
) { r2
[4] -= m2
* s
; r3
[4] -= m3
* s
; }
233 s
= r1
[5]; if (0.0 != s
) { r2
[5] -= m2
* s
; r3
[5] -= m3
* s
; }
234 s
= r1
[6]; if (0.0 != s
) { r2
[6] -= m2
* s
; r3
[6] -= m3
* s
; }
235 s
= r1
[7]; if (0.0 != s
) { r2
[7] -= m2
* s
; r3
[7] -= m3
* s
; }
237 /* choose pivot - or die */
238 if (fabs(r3
[2])>fabs(r2
[2])) SWAP_ROWS(r3
, r2
);
239 if (0.0 == r2
[2]) return GL_FALSE
;
241 /* eliminate third variable */
243 r3
[3] -= m3
* r2
[3], r3
[4] -= m3
* r2
[4],
244 r3
[5] -= m3
* r2
[5], r3
[6] -= m3
* r2
[6],
248 if (0.0 == r3
[3]) return GL_FALSE
;
250 s
= 1.0/r3
[3]; /* now back substitute row 3 */
251 r3
[4] *= s
; r3
[5] *= s
; r3
[6] *= s
; r3
[7] *= s
;
253 m2
= r2
[3]; /* now back substitute row 2 */
255 r2
[4] = s
* (r2
[4] - r3
[4] * m2
), r2
[5] = s
* (r2
[5] - r3
[5] * m2
),
256 r2
[6] = s
* (r2
[6] - r3
[6] * m2
), r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
258 r1
[4] -= r3
[4] * m1
, r1
[5] -= r3
[5] * m1
,
259 r1
[6] -= r3
[6] * m1
, r1
[7] -= r3
[7] * m1
;
261 r0
[4] -= r3
[4] * m0
, r0
[5] -= r3
[5] * m0
,
262 r0
[6] -= r3
[6] * m0
, r0
[7] -= r3
[7] * m0
;
264 m1
= r1
[2]; /* now back substitute row 1 */
266 r1
[4] = s
* (r1
[4] - r2
[4] * m1
), r1
[5] = s
* (r1
[5] - r2
[5] * m1
),
267 r1
[6] = s
* (r1
[6] - r2
[6] * m1
), r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
269 r0
[4] -= r2
[4] * m0
, r0
[5] -= r2
[5] * m0
,
270 r0
[6] -= r2
[6] * m0
, r0
[7] -= r2
[7] * m0
;
272 m0
= r0
[1]; /* now back substitute row 0 */
274 r0
[4] = s
* (r0
[4] - r1
[4] * m0
), r0
[5] = s
* (r0
[5] - r1
[5] * m0
),
275 r0
[6] = s
* (r0
[6] - r1
[6] * m0
), r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
277 MAT(out
,0,0) = r0
[4]; MAT(out
,0,1) = r0
[5],
278 MAT(out
,0,2) = r0
[6]; MAT(out
,0,3) = r0
[7],
279 MAT(out
,1,0) = r1
[4]; MAT(out
,1,1) = r1
[5],
280 MAT(out
,1,2) = r1
[6]; MAT(out
,1,3) = r1
[7],
281 MAT(out
,2,0) = r2
[4]; MAT(out
,2,1) = r2
[5],
282 MAT(out
,2,2) = r2
[6]; MAT(out
,2,3) = r2
[7],
283 MAT(out
,3,0) = r3
[4]; MAT(out
,3,1) = r3
[5],
284 MAT(out
,3,2) = r3
[6]; MAT(out
,3,3) = r3
[7];
290 /* Adapted from graphics gems II.
292 static GLboolean
invert_matrix_3d_general( GLmatrix
*mat
)
294 const GLfloat
*in
= mat
->m
;
295 GLfloat
*out
= mat
->inv
;
299 /* Calculate the determinant of upper left 3x3 submatrix and
300 * determine if the matrix is singular.
303 t
= MAT(in
,0,0) * MAT(in
,1,1) * MAT(in
,2,2);
304 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
306 t
= MAT(in
,1,0) * MAT(in
,2,1) * MAT(in
,0,2);
307 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
309 t
= MAT(in
,2,0) * MAT(in
,0,1) * MAT(in
,1,2);
310 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
312 t
= -MAT(in
,2,0) * MAT(in
,1,1) * MAT(in
,0,2);
313 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
315 t
= -MAT(in
,1,0) * MAT(in
,0,1) * MAT(in
,2,2);
316 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
318 t
= -MAT(in
,0,0) * MAT(in
,2,1) * MAT(in
,1,2);
319 if (t
>= 0.0) pos
+= t
; else neg
+= t
;
327 MAT(out
,0,0) = ( (MAT(in
,1,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,1,2) )*det
);
328 MAT(out
,0,1) = (- (MAT(in
,0,1)*MAT(in
,2,2) - MAT(in
,2,1)*MAT(in
,0,2) )*det
);
329 MAT(out
,0,2) = ( (MAT(in
,0,1)*MAT(in
,1,2) - MAT(in
,1,1)*MAT(in
,0,2) )*det
);
330 MAT(out
,1,0) = (- (MAT(in
,1,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,1,2) )*det
);
331 MAT(out
,1,1) = ( (MAT(in
,0,0)*MAT(in
,2,2) - MAT(in
,2,0)*MAT(in
,0,2) )*det
);
332 MAT(out
,1,2) = (- (MAT(in
,0,0)*MAT(in
,1,2) - MAT(in
,1,0)*MAT(in
,0,2) )*det
);
333 MAT(out
,2,0) = ( (MAT(in
,1,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,1,1) )*det
);
334 MAT(out
,2,1) = (- (MAT(in
,0,0)*MAT(in
,2,1) - MAT(in
,2,0)*MAT(in
,0,1) )*det
);
335 MAT(out
,2,2) = ( (MAT(in
,0,0)*MAT(in
,1,1) - MAT(in
,1,0)*MAT(in
,0,1) )*det
);
337 /* Do the translation part */
338 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
339 MAT(in
,1,3) * MAT(out
,0,1) +
340 MAT(in
,2,3) * MAT(out
,0,2) );
341 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
342 MAT(in
,1,3) * MAT(out
,1,1) +
343 MAT(in
,2,3) * MAT(out
,1,2) );
344 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
345 MAT(in
,1,3) * MAT(out
,2,1) +
346 MAT(in
,2,3) * MAT(out
,2,2) );
352 static GLboolean
invert_matrix_3d( GLmatrix
*mat
)
354 const GLfloat
*in
= mat
->m
;
355 GLfloat
*out
= mat
->inv
;
357 if (!TEST_MAT_FLAGS(mat
, MAT_FLAGS_ANGLE_PRESERVING
))
359 return invert_matrix_3d_general( mat
);
362 if (mat
->flags
& MAT_FLAG_UNIFORM_SCALE
)
364 GLfloat scale
= (MAT(in
,0,0) * MAT(in
,0,0) +
365 MAT(in
,0,1) * MAT(in
,0,1) +
366 MAT(in
,0,2) * MAT(in
,0,2));
373 /* Transpose and scale the 3 by 3 upper-left submatrix. */
374 MAT(out
,0,0) = scale
* MAT(in
,0,0);
375 MAT(out
,1,0) = scale
* MAT(in
,0,1);
376 MAT(out
,2,0) = scale
* MAT(in
,0,2);
377 MAT(out
,0,1) = scale
* MAT(in
,1,0);
378 MAT(out
,1,1) = scale
* MAT(in
,1,1);
379 MAT(out
,2,1) = scale
* MAT(in
,1,2);
380 MAT(out
,0,2) = scale
* MAT(in
,2,0);
381 MAT(out
,1,2) = scale
* MAT(in
,2,1);
382 MAT(out
,2,2) = scale
* MAT(in
,2,2);
384 else if (mat
->flags
& MAT_FLAG_ROTATION
)
386 /* Transpose the 3 by 3 upper-left submatrix. */
387 MAT(out
,0,0) = MAT(in
,0,0);
388 MAT(out
,1,0) = MAT(in
,0,1);
389 MAT(out
,2,0) = MAT(in
,0,2);
390 MAT(out
,0,1) = MAT(in
,1,0);
391 MAT(out
,1,1) = MAT(in
,1,1);
392 MAT(out
,2,1) = MAT(in
,1,2);
393 MAT(out
,0,2) = MAT(in
,2,0);
394 MAT(out
,1,2) = MAT(in
,2,1);
395 MAT(out
,2,2) = MAT(in
,2,2);
397 else /* pure translation */
399 MEMCPY( out
, Identity
, sizeof(Identity
) );
400 MAT(out
,0,3) = - MAT(in
,0,3);
401 MAT(out
,1,3) = - MAT(in
,1,3);
402 MAT(out
,2,3) = - MAT(in
,2,3);
406 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
408 /* Do the translation part */
409 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0) +
410 MAT(in
,1,3) * MAT(out
,0,1) +
411 MAT(in
,2,3) * MAT(out
,0,2) );
412 MAT(out
,1,3) = - (MAT(in
,0,3) * MAT(out
,1,0) +
413 MAT(in
,1,3) * MAT(out
,1,1) +
414 MAT(in
,2,3) * MAT(out
,1,2) );
415 MAT(out
,2,3) = - (MAT(in
,0,3) * MAT(out
,2,0) +
416 MAT(in
,1,3) * MAT(out
,2,1) +
417 MAT(in
,2,3) * MAT(out
,2,2) );
421 MAT(out
,0,3) = MAT(out
,1,3) = MAT(out
,2,3) = 0.0;
429 static GLboolean
invert_matrix_identity( GLmatrix
*mat
)
431 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
436 static GLboolean
invert_matrix_3d_no_rot( GLmatrix
*mat
)
438 const GLfloat
*in
= mat
->m
;
439 GLfloat
*out
= mat
->inv
;
441 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0 || MAT(in
,2,2) == 0 )
444 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
445 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
446 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
447 MAT(out
,2,2) = 1.0 / MAT(in
,2,2);
449 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
451 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
452 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
453 MAT(out
,2,3) = - (MAT(in
,2,3) * MAT(out
,2,2));
460 static GLboolean
invert_matrix_2d_no_rot( GLmatrix
*mat
)
462 const GLfloat
*in
= mat
->m
;
463 GLfloat
*out
= mat
->inv
;
465 if (MAT(in
,0,0) == 0 || MAT(in
,1,1) == 0)
468 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
469 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
470 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
472 if (mat
->flags
& MAT_FLAG_TRANSLATION
)
474 MAT(out
,0,3) = - (MAT(in
,0,3) * MAT(out
,0,0));
475 MAT(out
,1,3) = - (MAT(in
,1,3) * MAT(out
,1,1));
482 static GLboolean
invert_matrix_perspective( GLmatrix
*mat
)
484 const GLfloat
*in
= mat
->m
;
485 GLfloat
*out
= mat
->inv
;
487 if (MAT(in
,2,3) == 0)
490 MEMCPY( out
, Identity
, 16 * sizeof(GLfloat
) );
492 MAT(out
,0,0) = 1.0 / MAT(in
,0,0);
493 MAT(out
,1,1) = 1.0 / MAT(in
,1,1);
495 MAT(out
,0,3) = MAT(in
,0,2);
496 MAT(out
,1,3) = MAT(in
,1,2);
501 MAT(out
,3,2) = 1.0 / MAT(in
,2,3);
502 MAT(out
,3,3) = MAT(in
,2,2) * MAT(out
,3,2);
508 typedef GLboolean (*inv_mat_func
)( GLmatrix
*mat
);
510 static inv_mat_func inv_mat_tab
[7] = {
511 invert_matrix_general
,
512 invert_matrix_identity
,
513 invert_matrix_3d_no_rot
,
514 invert_matrix_perspective
,
515 invert_matrix_3d
, /* lazy! */
516 invert_matrix_2d_no_rot
,
521 GLboolean
gl_matrix_invert( GLmatrix
*mat
)
523 if (inv_mat_tab
[mat
->type
](mat
)) {
525 GLmatrix m
; m
.inv
= 0; m
.type
= 0; m
.flags
= 0;
526 matmul4( m
.m
, mat
->m
, mat
->inv
);
527 printf("inverted matrix of type %s:\n", types
[mat
->type
]);
528 gl_print_matrix( mat
);
529 gl_print_matrix( &m
);
533 MEMCPY( mat
->inv
, Identity
, sizeof(Identity
) );
540 void gl_matrix_transposef( GLfloat to
[16], const GLfloat from
[16] )
562 void gl_matrix_transposed( GLdouble to
[16], const GLdouble from
[16] )
585 * Generate a 4x4 transformation matrix from glRotate parameters.
587 void gl_rotation_matrix( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
,
590 /* This function contributed by Erich Boleyn (erich@uruk.org) */
592 GLfloat xx
, yy
, zz
, xy
, yz
, zx
, xs
, ys
, zs
, one_c
;
594 s
= sin( angle
* DEG2RAD
);
595 c
= cos( angle
* DEG2RAD
);
597 mag
= GL_SQRT( x
*x
+ y
*y
+ z
*z
);
600 /* generate an identity matrix and return */
601 MEMCPY(m
, Identity
, sizeof(GLfloat
)*16);
609 #define M(row,col) m[col*4+row]
612 * Arbitrary axis rotation matrix.
614 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
615 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
616 * (which is about the X-axis), and the two composite transforms
617 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
618 * from the arbitrary axis to the X-axis then back. They are
619 * all elementary rotations.
621 * Rz' is a rotation about the Z-axis, to bring the axis vector
622 * into the x-z plane. Then Ry' is applied, rotating about the
623 * Y-axis to bring the axis vector parallel with the X-axis. The
624 * rotation about the X-axis is then performed. Ry and Rz are
625 * simply the respective inverse transforms to bring the arbitrary
626 * axis back to it's original orientation. The first transforms
627 * Rz' and Ry' are considered inverses, since the data from the
628 * arbitrary axis gives you info on how to get to it, not how
629 * to get away from it, and an inverse must be applied.
631 * The basic calculation used is to recognize that the arbitrary
632 * axis vector (x, y, z), since it is of unit length, actually
633 * represents the sines and cosines of the angles to rotate the
634 * X-axis to the same orientation, with theta being the angle about
635 * Z and phi the angle about Y (in the order described above)
638 * cos ( theta ) = x / sqrt ( 1 - z^2 )
639 * sin ( theta ) = y / sqrt ( 1 - z^2 )
641 * cos ( phi ) = sqrt ( 1 - z^2 )
644 * Note that cos ( phi ) can further be inserted to the above
647 * cos ( theta ) = x / cos ( phi )
648 * sin ( theta ) = y / sin ( phi )
650 * ...etc. Because of those relations and the standard trigonometric
651 * relations, it is pssible to reduce the transforms down to what
652 * is used below. It may be that any primary axis chosen will give the
653 * same results (modulo a sign convention) using thie method.
655 * Particularly nice is to notice that all divisions that might
656 * have caused trouble when parallel to certain planes or
657 * axis go away with care paid to reducing the expressions.
658 * After checking, it does perform correctly under all cases, since
659 * in all the cases of division where the denominator would have
660 * been zero, the numerator would have been zero as well, giving
661 * the expected result.
675 M(0,0) = (one_c
* xx
) + c
;
676 M(0,1) = (one_c
* xy
) - zs
;
677 M(0,2) = (one_c
* zx
) + ys
;
680 M(1,0) = (one_c
* xy
) + zs
;
681 M(1,1) = (one_c
* yy
) + c
;
682 M(1,2) = (one_c
* yz
) - xs
;
685 M(2,0) = (one_c
* zx
) - ys
;
686 M(2,1) = (one_c
* yz
) + xs
;
687 M(2,2) = (one_c
* zz
) + c
;
698 #define ZERO(x) (1<<x)
699 #define ONE(x) (1<<(x+16))
701 #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
702 #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
704 #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
705 ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
706 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
707 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
709 #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
710 ZERO(1) | ZERO(9) | \
711 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
712 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
714 #define MASK_2D ( ZERO(8) | \
716 ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
717 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
720 #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
721 ZERO(1) | ZERO(9) | \
722 ZERO(2) | ZERO(6) | \
723 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
728 ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
731 #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
732 ZERO(1) | ZERO(13) |\
733 ZERO(2) | ZERO(6) | \
734 ZERO(3) | ZERO(7) | ZERO(15) )
736 #define SQ(x) ((x)*(x))
738 /* Determine type and flags from scratch. This is expensive enough to
739 * only want to do it once.
741 static void analyze_from_scratch( GLmatrix
*mat
)
743 const GLfloat
*m
= mat
->m
;
747 for (i
= 0 ; i
< 16 ; i
++)
749 if (m
[i
] == 0.0) mask
|= (1<<i
);
752 if (m
[0] == 1.0F
) mask
|= (1<<16);
753 if (m
[5] == 1.0F
) mask
|= (1<<21);
754 if (m
[10] == 1.0F
) mask
|= (1<<26);
755 if (m
[15] == 1.0F
) mask
|= (1<<31);
757 mat
->flags
&= ~MAT_FLAGS_GEOMETRY
;
759 /* Check for translation - no-one really cares
761 if ((mask
& MASK_NO_TRX
) != MASK_NO_TRX
)
762 mat
->flags
|= MAT_FLAG_TRANSLATION
;
766 if (mask
== MASK_IDENTITY
) {
767 mat
->type
= MATRIX_IDENTITY
;
769 else if ((mask
& MASK_2D_NO_ROT
) == MASK_2D_NO_ROT
)
771 mat
->type
= MATRIX_2D_NO_ROT
;
773 if ((mask
& MASK_NO_2D_SCALE
) != MASK_NO_2D_SCALE
)
774 mat
->flags
= MAT_FLAG_GENERAL_SCALE
;
776 else if ((mask
& MASK_2D
) == MASK_2D
)
778 GLfloat mm
= DOT2(m
, m
);
779 GLfloat m4m4
= DOT2(m
+4,m
+4);
780 GLfloat mm4
= DOT2(m
,m
+4);
782 mat
->type
= MATRIX_2D
;
784 /* Check for scale */
785 if (SQ(mm
-1) > SQ(1e-6) ||
786 SQ(m4m4
-1) > SQ(1e-6))
787 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
789 /* Check for rotation */
790 if (SQ(mm4
) > SQ(1e-6))
791 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
793 mat
->flags
|= MAT_FLAG_ROTATION
;
796 else if ((mask
& MASK_3D_NO_ROT
) == MASK_3D_NO_ROT
)
798 mat
->type
= MATRIX_3D_NO_ROT
;
800 /* Check for scale */
801 if (SQ(m
[0]-m
[5]) < SQ(1e-6) &&
802 SQ(m
[0]-m
[10]) < SQ(1e-6)) {
803 if (SQ(m
[0]-1.0) > SQ(1e-6))
804 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
806 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
808 else if ((mask
& MASK_3D
) == MASK_3D
)
810 GLfloat c1
= DOT3(m
,m
);
811 GLfloat c2
= DOT3(m
+4,m
+4);
812 GLfloat c3
= DOT3(m
+8,m
+8);
813 GLfloat d1
= DOT3(m
, m
+4);
816 mat
->type
= MATRIX_3D
;
818 /* Check for scale */
819 if (SQ(c1
-c2
) < SQ(1e-6) && SQ(c1
-c3
) < SQ(1e-6)) {
820 if (SQ(c1
-1.0) > SQ(1e-6))
821 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
822 /* else no scale at all */
824 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
826 /* Check for rotation */
827 if (SQ(d1
) < SQ(1e-6)) {
828 CROSS3( cp
, m
, m
+4 );
829 SUB_3V( cp
, cp
, (m
+8) );
830 if (LEN_SQUARED_3FV(cp
) < SQ(1e-6))
831 mat
->flags
|= MAT_FLAG_ROTATION
;
833 mat
->flags
|= MAT_FLAG_GENERAL_3D
;
836 mat
->flags
|= MAT_FLAG_GENERAL_3D
; /* shear, etc */
838 else if ((mask
& MASK_PERSPECTIVE
) == MASK_PERSPECTIVE
&& m
[11]==-1.0F
)
840 mat
->type
= MATRIX_PERSPECTIVE
;
841 mat
->flags
|= MAT_FLAG_GENERAL
;
844 mat
->type
= MATRIX_GENERAL
;
845 mat
->flags
|= MAT_FLAG_GENERAL
;
850 /* Analyse a matrix given that its flags are accurate - this is the
851 * more common operation, hopefully.
853 static void analyze_from_flags( GLmatrix
*mat
)
855 const GLfloat
*m
= mat
->m
;
857 if (TEST_MAT_FLAGS(mat
, 0)) {
858 mat
->type
= MATRIX_IDENTITY
;
860 else if (TEST_MAT_FLAGS(mat
, (MAT_FLAG_TRANSLATION
|
861 MAT_FLAG_UNIFORM_SCALE
|
862 MAT_FLAG_GENERAL_SCALE
)))
864 if ( m
[10]==1.0F
&& m
[14]==0.0F
) {
865 mat
->type
= MATRIX_2D_NO_ROT
;
868 mat
->type
= MATRIX_3D_NO_ROT
;
871 else if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
)) {
874 && m
[2]==0.0F
&& m
[6]==0.0F
&& m
[10]==1.0F
&& m
[14]==0.0F
)
876 mat
->type
= MATRIX_2D
;
880 mat
->type
= MATRIX_3D
;
883 else if ( m
[4]==0.0F
&& m
[12]==0.0F
884 && m
[1]==0.0F
&& m
[13]==0.0F
885 && m
[2]==0.0F
&& m
[6]==0.0F
886 && m
[3]==0.0F
&& m
[7]==0.0F
&& m
[11]==-1.0F
&& m
[15]==0.0F
)
888 mat
->type
= MATRIX_PERSPECTIVE
;
891 mat
->type
= MATRIX_GENERAL
;
897 void gl_matrix_analyze( GLmatrix
*mat
)
899 if (mat
->flags
& MAT_DIRTY_TYPE
) {
900 if (mat
->flags
& MAT_DIRTY_FLAGS
)
901 analyze_from_scratch( mat
);
903 analyze_from_flags( mat
);
906 if (mat
->inv
&& (mat
->flags
& MAT_DIRTY_INVERSE
)) {
907 gl_matrix_invert( mat
);
910 mat
->flags
&= ~(MAT_DIRTY_FLAGS
|
916 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
918 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where); \
919 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
920 switch (ctx->Transform.MatrixMode) { \
922 mat = &ctx->ModelView; \
923 flags |= NEW_MODELVIEW; \
925 case GL_PROJECTION: \
926 mat = &ctx->ProjectionMatrix; \
927 flags |= NEW_PROJECTION; \
930 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
931 flags |= NEW_TEXTURE_MATRIX; \
934 gl_problem(ctx, where); \
940 _mesa_Frustum( GLdouble left
, GLdouble right
,
941 GLdouble bottom
, GLdouble top
,
942 GLdouble nearval
, GLdouble farval
)
944 GET_CURRENT_CONTEXT(ctx
);
945 GLfloat x
, y
, a
, b
, c
, d
;
949 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glFrustrum" );
951 if ((nearval
<=0.0 || farval
<=0.0) || (nearval
== farval
) || (left
== right
) || (top
== bottom
)) {
952 gl_error( ctx
, GL_INVALID_VALUE
, "glFrustum(near or far)" );
956 x
= (2.0*nearval
) / (right
-left
);
957 y
= (2.0*nearval
) / (top
-bottom
);
958 a
= (right
+left
) / (right
-left
);
959 b
= (top
+bottom
) / (top
-bottom
);
960 c
= -(farval
+nearval
) / ( farval
-nearval
);
961 d
= -(2.0*farval
*nearval
) / (farval
-nearval
); /* error? */
963 #define M(row,col) m[col*4+row]
964 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = a
; M(0,3) = 0.0F
;
965 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = b
; M(1,3) = 0.0F
;
966 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = c
; M(2,3) = d
;
967 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = -1.0F
; M(3,3) = 0.0F
;
971 gl_mat_mul_floats( mat
, m
, MAT_FLAG_PERSPECTIVE
);
974 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
)
976 /* Need to keep a stack of near/far values in case the user push/pops
977 * the projection matrix stack so that we can call Driver.NearFar()
980 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = nearval
;
981 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = farval
;
983 if (ctx
->Driver
.NearFar
) {
984 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
991 _mesa_Ortho( GLdouble left
, GLdouble right
,
992 GLdouble bottom
, GLdouble top
,
993 GLdouble nearval
, GLdouble farval
)
995 GET_CURRENT_CONTEXT(ctx
);
1001 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
1003 if ((left
== right
) || (bottom
== top
) || (nearval
== farval
)) {
1004 gl_error( ctx
, GL_INVALID_VALUE
, "gl_Ortho((l = r) or (b = top) or (n=f)" );
1008 x
= 2.0 / (right
-left
);
1009 y
= 2.0 / (top
-bottom
);
1010 z
= -2.0 / (farval
-nearval
);
1011 tx
= -(right
+left
) / (right
-left
);
1012 ty
= -(top
+bottom
) / (top
-bottom
);
1013 tz
= -(farval
+nearval
) / (farval
-nearval
);
1015 #define M(row,col) m[col*4+row]
1016 M(0,0) = x
; M(0,1) = 0.0F
; M(0,2) = 0.0F
; M(0,3) = tx
;
1017 M(1,0) = 0.0F
; M(1,1) = y
; M(1,2) = 0.0F
; M(1,3) = ty
;
1018 M(2,0) = 0.0F
; M(2,1) = 0.0F
; M(2,2) = z
; M(2,3) = tz
;
1019 M(3,0) = 0.0F
; M(3,1) = 0.0F
; M(3,2) = 0.0F
; M(3,3) = 1.0F
;
1022 gl_mat_mul_floats( mat
, m
, (MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
));
1024 if (ctx
->Driver
.NearFar
) {
1025 (*ctx
->Driver
.NearFar
)( ctx
, nearval
, farval
);
1031 _mesa_MatrixMode( GLenum mode
)
1033 GET_CURRENT_CONTEXT(ctx
);
1034 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glMatrixMode");
1039 ctx
->Transform
.MatrixMode
= mode
;
1042 gl_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
1049 _mesa_PushMatrix( void )
1051 GET_CURRENT_CONTEXT(ctx
);
1052 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushMatrix");
1054 if (MESA_VERBOSE
&VERBOSE_API
)
1055 fprintf(stderr
, "glPushMatrix %s\n",
1056 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1058 switch (ctx
->Transform
.MatrixMode
) {
1060 if (ctx
->ModelViewStackDepth
>=MAX_MODELVIEW_STACK_DEPTH
-1) {
1061 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1064 gl_matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
1068 if (ctx
->ProjectionStackDepth
>=MAX_PROJECTION_STACK_DEPTH
) {
1069 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1072 gl_matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
1073 &ctx
->ProjectionMatrix
);
1075 /* Save near and far projection values */
1076 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0]
1077 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][0];
1078 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1]
1079 = ctx
->NearFarStack
[ctx
->ProjectionStackDepth
-1][1];
1083 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1084 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
) {
1085 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
1088 gl_matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
1089 &ctx
->TextureMatrix
[t
] );
1093 gl_problem(ctx
, "Bad matrix mode in gl_PushMatrix");
1100 _mesa_PopMatrix( void )
1102 GET_CURRENT_CONTEXT(ctx
);
1103 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopMatrix");
1105 if (MESA_VERBOSE
&VERBOSE_API
)
1106 fprintf(stderr
, "glPopMatrix %s\n",
1107 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
1109 switch (ctx
->Transform
.MatrixMode
) {
1111 if (ctx
->ModelViewStackDepth
==0) {
1112 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1115 gl_matrix_copy( &ctx
->ModelView
,
1116 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
1117 ctx
->NewState
|= NEW_MODELVIEW
;
1120 if (ctx
->ProjectionStackDepth
==0) {
1121 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1125 gl_matrix_copy( &ctx
->ProjectionMatrix
,
1126 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
1127 ctx
->NewState
|= NEW_PROJECTION
;
1129 /* Device driver near/far values */
1131 GLfloat nearVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0];
1132 GLfloat farVal
= ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1];
1133 if (ctx
->Driver
.NearFar
) {
1134 (*ctx
->Driver
.NearFar
)( ctx
, nearVal
, farVal
);
1140 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
1141 if (ctx
->TextureStackDepth
[t
]==0) {
1142 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
1145 gl_matrix_copy(&ctx
->TextureMatrix
[t
],
1146 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
1150 gl_problem(ctx
, "Bad matrix mode in gl_PopMatrix");
1157 _mesa_LoadIdentity( void )
1159 GET_CURRENT_CONTEXT(ctx
);
1161 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
1163 MEMCPY( mat
->m
, Identity
, 16*sizeof(GLfloat
) );
1166 MEMCPY( mat
->inv
, Identity
, 16*sizeof(GLfloat
) );
1168 mat
->type
= MATRIX_IDENTITY
;
1170 /* Have to set this to dirty to make sure we recalculate the
1171 * combined matrix later. The update_matrix in this case is a
1172 * shortcircuit anyway...
1174 mat
->flags
= MAT_DIRTY_DEPENDENTS
;
1179 _mesa_LoadMatrixf( const GLfloat
*m
)
1181 GET_CURRENT_CONTEXT(ctx
);
1183 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
1185 MEMCPY( mat
->m
, m
, 16*sizeof(GLfloat
) );
1186 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1188 if (ctx
->Transform
.MatrixMode
== GL_PROJECTION
) {
1190 #define M(row,col) m[col*4+row]
1194 GLfloat n
= (c
== 1.0 ? 0.0 : d
/ (c
- 1.0));
1195 GLfloat f
= (c
== -1.0 ? 1.0 : d
/ (c
+ 1.0));
1197 /* Need to keep a stack of near/far values in case the user
1198 * push/pops the projection matrix stack so that we can call
1199 * Driver.NearFar() after a pop.
1201 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][0] = n
;
1202 ctx
->NearFarStack
[ctx
->ProjectionStackDepth
][1] = f
;
1204 if (ctx
->Driver
.NearFar
) {
1205 (*ctx
->Driver
.NearFar
)( ctx
, n
, f
);
1212 _mesa_LoadMatrixd( const GLdouble
*m
)
1216 for (i
= 0; i
< 16; i
++)
1218 _mesa_LoadMatrixf(f
);
1224 * Multiply the active matrix by an arbitary matrix.
1227 _mesa_MultMatrixf( const GLfloat
*m
)
1229 GET_CURRENT_CONTEXT(ctx
);
1231 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1232 matmul4( mat
->m
, mat
->m
, m
);
1233 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1238 * Multiply the active matrix by an arbitary matrix.
1241 _mesa_MultMatrixd( const GLdouble
*m
)
1243 GET_CURRENT_CONTEXT(ctx
);
1245 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
1246 matmul4fd( mat
->m
, mat
->m
, m
);
1247 mat
->flags
= (MAT_FLAG_GENERAL
| MAT_DIRTY_ALL_OVER
);
1254 * Multiply a matrix by an array of floats with known properties.
1256 void gl_mat_mul_floats( GLmatrix
*mat
, const GLfloat
*m
, GLuint flags
)
1258 mat
->flags
|= (flags
|
1261 MAT_DIRTY_DEPENDENTS
);
1263 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1264 matmul34( mat
->m
, mat
->m
, m
);
1266 matmul4( mat
->m
, mat
->m
, m
);
1271 * Multiply a matrix by an array of floats with known properties.
1273 void gl_mat_mul_mat( GLmatrix
*mat
, const GLmatrix
*m
)
1275 mat
->flags
|= (m
->flags
|
1278 MAT_DIRTY_DEPENDENTS
);
1280 if (TEST_MAT_FLAGS(mat
, MAT_FLAGS_3D
))
1281 matmul34( mat
->m
, mat
->m
, m
->m
);
1283 matmul4( mat
->m
, mat
->m
, m
->m
);
1289 * Execute a glRotate call
1292 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
1294 GET_CURRENT_CONTEXT(ctx
);
1296 if (angle
!= 0.0F
) {
1298 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
1300 gl_rotation_matrix( angle
, x
, y
, z
, m
);
1301 gl_mat_mul_floats( mat
, m
, MAT_FLAG_ROTATION
);
1306 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
1308 _mesa_Rotatef(angle
, x
, y
, z
);
1313 * Execute a glScale call
1316 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
1318 GET_CURRENT_CONTEXT(ctx
);
1321 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glScale");
1324 m
[0] *= x
; m
[4] *= y
; m
[8] *= z
;
1325 m
[1] *= x
; m
[5] *= y
; m
[9] *= z
;
1326 m
[2] *= x
; m
[6] *= y
; m
[10] *= z
;
1327 m
[3] *= x
; m
[7] *= y
; m
[11] *= z
;
1329 if (fabs(x
- y
) < 1e-8 && fabs(x
- z
) < 1e-8)
1330 mat
->flags
|= MAT_FLAG_UNIFORM_SCALE
;
1332 mat
->flags
|= MAT_FLAG_GENERAL_SCALE
;
1334 mat
->flags
|= (MAT_DIRTY_TYPE
|
1336 MAT_DIRTY_DEPENDENTS
);
1341 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
1343 _mesa_Scalef(x
, y
, z
);
1348 * Execute a glTranslate call
1351 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
1353 GET_CURRENT_CONTEXT(ctx
);
1356 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glTranslate");
1358 m
[12] = m
[0] * x
+ m
[4] * y
+ m
[8] * z
+ m
[12];
1359 m
[13] = m
[1] * x
+ m
[5] * y
+ m
[9] * z
+ m
[13];
1360 m
[14] = m
[2] * x
+ m
[6] * y
+ m
[10] * z
+ m
[14];
1361 m
[15] = m
[3] * x
+ m
[7] * y
+ m
[11] * z
+ m
[15];
1363 mat
->flags
|= (MAT_FLAG_TRANSLATION
|
1366 MAT_DIRTY_DEPENDENTS
);
1371 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
1373 _mesa_Translatef(x
, y
, z
);
1379 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
1382 gl_matrix_transposef(tm
, m
);
1383 _mesa_LoadMatrixf(tm
);
1388 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
1391 gl_matrix_transposed(tm
, m
);
1392 _mesa_LoadMatrixd(tm
);
1397 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
1400 gl_matrix_transposef(tm
, m
);
1401 _mesa_MultMatrixf(tm
);
1406 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
1409 gl_matrix_transposed(tm
, m
);
1410 _mesa_MultMatrixd(tm
);
1415 * Called via glViewport or display list execution.
1418 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1420 GET_CURRENT_CONTEXT(ctx
);
1421 gl_Viewport(ctx
, x
, y
, width
, height
);
1427 * Define a new viewport and reallocate auxillary buffers if the size of
1428 * the window (color buffer) has changed.
1430 * XXX This is directly called by device drivers, BUT this function
1431 * may be renamed _mesa_Viewport (without ctx arg) in the future so
1432 * use of _mesa_Viewport is encouraged.
1435 gl_Viewport( GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
)
1437 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glViewport");
1439 if (width
<0 || height
<0) {
1440 gl_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
1444 if (MESA_VERBOSE
& VERBOSE_API
)
1445 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
1447 /* clamp width, and height to implementation dependent range */
1448 width
= CLAMP( width
, 1, MAX_WIDTH
);
1449 height
= CLAMP( height
, 1, MAX_HEIGHT
);
1452 ctx
->Viewport
.X
= x
;
1453 ctx
->Viewport
.Width
= width
;
1454 ctx
->Viewport
.Y
= y
;
1455 ctx
->Viewport
.Height
= height
;
1457 /* compute scale and bias values */
1458 ctx
->Viewport
.WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
1459 ctx
->Viewport
.WindowMap
.m
[MAT_TX
] = ctx
->Viewport
.WindowMap
.m
[MAT_SX
] + x
;
1460 ctx
->Viewport
.WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
1461 ctx
->Viewport
.WindowMap
.m
[MAT_TY
] = ctx
->Viewport
.WindowMap
.m
[MAT_SY
] + y
;
1462 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = 0.5 * DEPTH_SCALE
;
1463 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = 0.5 * DEPTH_SCALE
;
1465 ctx
->Viewport
.WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
1466 ctx
->Viewport
.WindowMap
.type
= MATRIX_3D_NO_ROT
;
1468 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1469 ctx
->NewState
|= NEW_VIEWPORT
;
1471 /* Check if window/buffer has been resized and if so, reallocate the
1472 * ancillary buffers.
1474 _mesa_ResizeBuffersMESA();
1477 ctx
->RasterMask
&= ~WINCLIP_BIT
;
1479 if ( ctx
->Viewport
.X
<0
1480 || ctx
->Viewport
.X
+ ctx
->Viewport
.Width
> ctx
->DrawBuffer
->Width
1481 || ctx
->Viewport
.Y
<0
1482 || ctx
->Viewport
.Y
+ ctx
->Viewport
.Height
> ctx
->DrawBuffer
->Height
) {
1483 ctx
->RasterMask
|= WINCLIP_BIT
;
1487 if (ctx
->Driver
.Viewport
) {
1488 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
1495 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
1498 * nearval - specifies mapping of the near clipping plane to window
1499 * coordinates, default is 0
1500 * farval - specifies mapping of the far clipping plane to window
1501 * coordinates, default is 1
1503 * After clipping and div by w, z coords are in -1.0 to 1.0,
1504 * corresponding to near and far clipping planes. glDepthRange
1505 * specifies a linear mapping of the normalized z coords in
1506 * this range to window z coords.
1509 GET_CURRENT_CONTEXT(ctx
);
1510 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthRange");
1512 if (MESA_VERBOSE
&VERBOSE_API
)
1513 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
1515 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
1516 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
1518 ctx
->Viewport
.Near
= n
;
1519 ctx
->Viewport
.Far
= f
;
1520 ctx
->Viewport
.WindowMap
.m
[MAT_SZ
] = DEPTH_SCALE
* ((f
- n
) / 2.0);
1521 ctx
->Viewport
.WindowMap
.m
[MAT_TZ
] = DEPTH_SCALE
* ((f
- n
) / 2.0 + n
);
1523 ctx
->ModelProjectWinMatrixUptodate
= GL_FALSE
;
1525 if (ctx
->Driver
.DepthRange
) {
1526 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);
1531 void gl_calculate_model_project_matrix( GLcontext
*ctx
)
1533 gl_matrix_mul( &ctx
->ModelProjectMatrix
,
1534 &ctx
->ProjectionMatrix
,
1537 gl_matrix_analyze( &ctx
->ModelProjectMatrix
);
1541 void gl_matrix_ctr( GLmatrix
*m
)
1544 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1545 m
->type
= MATRIX_IDENTITY
;
1546 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1549 void gl_matrix_dtr( GLmatrix
*m
)
1558 void gl_matrix_set_identity( GLmatrix
*m
)
1560 MEMCPY( m
->m
, Identity
, sizeof(Identity
));
1561 m
->type
= MATRIX_IDENTITY
;
1562 m
->flags
= MAT_DIRTY_DEPENDENTS
;
1566 void gl_matrix_alloc_inv( GLmatrix
*m
)
1569 m
->inv
= (GLfloat
*)MALLOC(16*sizeof(GLfloat
));
1570 MEMCPY( m
->inv
, Identity
, 16 * sizeof(GLfloat
) );
1574 void gl_matrix_copy( GLmatrix
*to
, const GLmatrix
*from
)
1576 MEMCPY( to
->m
, from
->m
, sizeof(Identity
));
1577 to
->flags
= from
->flags
| MAT_DIRTY_DEPENDENTS
;
1578 to
->type
= from
->type
;
1581 if (from
->inv
== 0) {
1582 gl_matrix_invert( to
);
1584 MEMCPY(to
->inv
, from
->inv
, sizeof(GLfloat
)*16);
1589 void gl_matrix_mul( GLmatrix
*dest
, const GLmatrix
*a
, const GLmatrix
*b
)
1591 dest
->flags
= (a
->flags
|
1595 MAT_DIRTY_DEPENDENTS
);
1597 if (TEST_MAT_FLAGS(dest
, MAT_FLAGS_3D
))
1598 matmul34( dest
->m
, a
->m
, b
->m
);
1600 matmul4( dest
->m
, a
->m
, b
->m
);