2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
32 * -# 4x4 transformation matrices are stored in memory in column major order.
33 * -# Points/vertices are to be thought of as column vectors.
34 * -# Transformation of a point p by a matrix M is: p' = M * p
45 #include "math/m_matrix.h"
46 #include "util/bitscan.h"
49 static struct gl_matrix_stack
*
50 get_named_matrix_stack(struct gl_context
*ctx
, GLenum mode
, const char* caller
)
54 return &ctx
->ModelviewMatrixStack
;
56 return &ctx
->ProjectionMatrixStack
;
58 /* This error check is disabled because if we're called from
59 * glPopAttrib() when the active texture unit is >= MaxTextureCoordUnits
60 * we'll generate an unexpected error.
61 * From the GL_ARB_vertex_shader spec it sounds like we should instead
62 * do error checking in other places when we actually try to access
63 * texture matrices beyond MaxTextureCoordUnits.
66 if (ctx
->Texture
.CurrentUnit
>= ctx
->Const
.MaxTextureCoordUnits
) {
67 _mesa_error(ctx
, GL_INVALID_OPERATION
,
68 "glMatrixMode(invalid tex unit %d)",
69 ctx
->Texture
.CurrentUnit
);
73 assert(ctx
->Texture
.CurrentUnit
< ARRAY_SIZE(ctx
->TextureMatrixStack
));
74 return &ctx
->TextureMatrixStack
[ctx
->Texture
.CurrentUnit
];
83 if (ctx
->API
== API_OPENGL_COMPAT
84 && (ctx
->Extensions
.ARB_vertex_program
||
85 ctx
->Extensions
.ARB_fragment_program
)) {
86 const GLuint m
= mode
- GL_MATRIX0_ARB
;
87 if (m
<= ctx
->Const
.MaxProgramMatrices
)
88 return &ctx
->ProgramMatrixStack
[m
];
94 if (mode
>= GL_TEXTURE0
&& mode
< (GL_TEXTURE0
+ ctx
->Const
.MaxTextureCoordUnits
)) {
95 return &ctx
->TextureMatrixStack
[mode
- GL_TEXTURE0
];
97 _mesa_error(ctx
, GL_INVALID_ENUM
, "%s", caller
);
102 static void matrix_frustum(struct gl_matrix_stack
* stack
,
103 GLdouble left
, GLdouble right
,
104 GLdouble bottom
, GLdouble top
,
105 GLdouble nearval
, GLdouble farval
,
108 GET_CURRENT_CONTEXT(ctx
);
109 if (nearval
<= 0.0 ||
114 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s", caller
);
118 FLUSH_VERTICES(ctx
, 0);
120 _math_matrix_frustum(stack
->Top
,
121 (GLfloat
) left
, (GLfloat
) right
,
122 (GLfloat
) bottom
, (GLfloat
) top
,
123 (GLfloat
) nearval
, (GLfloat
) farval
);
124 ctx
->NewState
|= stack
->DirtyFlag
;
129 * Apply a perspective projection matrix.
131 * \param left left clipping plane coordinate.
132 * \param right right clipping plane coordinate.
133 * \param bottom bottom clipping plane coordinate.
134 * \param top top clipping plane coordinate.
135 * \param nearval distance to the near clipping plane.
136 * \param farval distance to the far clipping plane.
140 * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with
141 * the top matrix of the current matrix stack and sets
142 * __struct gl_contextRec::NewState.
145 _mesa_Frustum( GLdouble left
, GLdouble right
,
146 GLdouble bottom
, GLdouble top
,
147 GLdouble nearval
, GLdouble farval
)
149 GET_CURRENT_CONTEXT(ctx
);
150 matrix_frustum(ctx
->CurrentStack
,
151 (GLfloat
) left
, (GLfloat
) right
,
152 (GLfloat
) bottom
, (GLfloat
) top
,
153 (GLfloat
) nearval
, (GLfloat
) farval
,
159 _mesa_MatrixFrustumEXT( GLenum matrixMode
,
160 GLdouble left
, GLdouble right
,
161 GLdouble bottom
, GLdouble top
,
162 GLdouble nearval
, GLdouble farval
)
164 GET_CURRENT_CONTEXT(ctx
);
165 struct gl_matrix_stack
*stack
= get_named_matrix_stack(ctx
, matrixMode
,
166 "glMatrixFrustumEXT");
170 matrix_frustum(stack
,
171 (GLfloat
) left
, (GLfloat
) right
,
172 (GLfloat
) bottom
, (GLfloat
) top
,
173 (GLfloat
) nearval
, (GLfloat
) farval
,
174 "glMatrixFrustumEXT");
179 matrix_ortho(struct gl_matrix_stack
* stack
,
180 GLdouble left
, GLdouble right
,
181 GLdouble bottom
, GLdouble top
,
182 GLdouble nearval
, GLdouble farval
,
185 GET_CURRENT_CONTEXT(ctx
);
187 if (MESA_VERBOSE
& VERBOSE_API
)
188 _mesa_debug(ctx
, "%s(%f, %f, %f, %f, %f, %f)\n", caller
,
189 left
, right
, bottom
, top
, nearval
, farval
);
195 _mesa_error( ctx
, GL_INVALID_VALUE
, "%s", caller
);
199 FLUSH_VERTICES(ctx
, 0);
201 _math_matrix_ortho( stack
->Top
,
202 (GLfloat
) left
, (GLfloat
) right
,
203 (GLfloat
) bottom
, (GLfloat
) top
,
204 (GLfloat
) nearval
, (GLfloat
) farval
);
205 ctx
->NewState
|= stack
->DirtyFlag
;
210 * Apply an orthographic projection matrix.
212 * \param left left clipping plane coordinate.
213 * \param right right clipping plane coordinate.
214 * \param bottom bottom clipping plane coordinate.
215 * \param top top clipping plane coordinate.
216 * \param nearval distance to the near clipping plane.
217 * \param farval distance to the far clipping plane.
221 * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
222 * the top matrix of the current matrix stack and sets
223 * __struct gl_contextRec::NewState.
226 _mesa_Ortho( GLdouble left
, GLdouble right
,
227 GLdouble bottom
, GLdouble top
,
228 GLdouble nearval
, GLdouble farval
)
230 GET_CURRENT_CONTEXT(ctx
);
231 matrix_ortho(ctx
->CurrentStack
,
232 (GLfloat
) left
, (GLfloat
) right
,
233 (GLfloat
) bottom
, (GLfloat
) top
,
234 (GLfloat
) nearval
, (GLfloat
) farval
,
240 _mesa_MatrixOrthoEXT( GLenum matrixMode
,
241 GLdouble left
, GLdouble right
,
242 GLdouble bottom
, GLdouble top
,
243 GLdouble nearval
, GLdouble farval
)
245 GET_CURRENT_CONTEXT(ctx
);
246 struct gl_matrix_stack
*stack
= get_named_matrix_stack(ctx
, matrixMode
,
252 (GLfloat
) left
, (GLfloat
) right
,
253 (GLfloat
) bottom
, (GLfloat
) top
,
254 (GLfloat
) nearval
, (GLfloat
) farval
,
260 * Set the current matrix stack.
262 * \param mode matrix stack.
264 * \sa glMatrixMode().
266 * Flushes the vertices, validates the parameter and updates
267 * __struct gl_contextRec::CurrentStack and gl_transform_attrib::MatrixMode
268 * with the specified matrix stack.
271 _mesa_MatrixMode( GLenum mode
)
273 struct gl_matrix_stack
* stack
;
274 GET_CURRENT_CONTEXT(ctx
);
276 if (ctx
->Transform
.MatrixMode
== mode
&& mode
!= GL_TEXTURE
)
279 if (mode
>= GL_TEXTURE0
&& mode
< (GL_TEXTURE0
+ ctx
->Const
.MaxTextureCoordUnits
)) {
282 stack
= get_named_matrix_stack(ctx
, mode
, "glMatrixMode");
286 ctx
->CurrentStack
= stack
;
287 ctx
->Transform
.MatrixMode
= mode
;
293 push_matrix(struct gl_context
*ctx
, struct gl_matrix_stack
*stack
,
294 GLenum matrixMode
, const char *func
)
296 if (stack
->Depth
+ 1 >= stack
->MaxDepth
) {
297 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
298 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s(mode=GL_TEXTURE, unit=%d)",
299 func
, ctx
->Texture
.CurrentUnit
);
301 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "%s(mode=%s)",
302 func
, _mesa_enum_to_string(matrixMode
));
307 if (stack
->Depth
+ 1 >= stack
->StackSize
) {
308 unsigned new_stack_size
= stack
->StackSize
* 2;
310 GLmatrix
*new_stack
= realloc(stack
->Stack
,
311 sizeof(*new_stack
) * new_stack_size
);
314 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", func
);
318 for (i
= stack
->StackSize
; i
< new_stack_size
; i
++)
319 _math_matrix_ctr(&new_stack
[i
]);
321 stack
->Stack
= new_stack
;
322 stack
->StackSize
= new_stack_size
;
325 _math_matrix_copy( &stack
->Stack
[stack
->Depth
+ 1],
326 &stack
->Stack
[stack
->Depth
] );
328 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
329 ctx
->NewState
|= stack
->DirtyFlag
;
334 * Push the current matrix stack.
336 * \sa glPushMatrix().
338 * Verifies the current matrix stack is not full, and duplicates the top-most
339 * matrix in the stack.
340 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
343 _mesa_PushMatrix( void )
345 GET_CURRENT_CONTEXT(ctx
);
346 struct gl_matrix_stack
*stack
= ctx
->CurrentStack
;
348 if (MESA_VERBOSE
&VERBOSE_API
)
349 _mesa_debug(ctx
, "glPushMatrix %s\n",
350 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
352 push_matrix(ctx
, stack
, ctx
->Transform
.MatrixMode
, "glPushMatrix");
357 _mesa_MatrixPushEXT( GLenum matrixMode
)
359 GET_CURRENT_CONTEXT(ctx
);
360 struct gl_matrix_stack
*stack
= get_named_matrix_stack(ctx
, matrixMode
,
362 ASSERT_OUTSIDE_BEGIN_END(ctx
);
364 push_matrix(ctx
, stack
, matrixMode
, "glMatrixPushEXT");
369 pop_matrix( struct gl_context
*ctx
, struct gl_matrix_stack
*stack
)
371 if (stack
->Depth
== 0)
375 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
376 ctx
->NewState
|= stack
->DirtyFlag
;
382 * Pop the current matrix stack.
386 * Flushes the vertices, verifies the current matrix stack is not empty, and
387 * moves the stack head down.
388 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
391 _mesa_PopMatrix( void )
393 GET_CURRENT_CONTEXT(ctx
);
394 struct gl_matrix_stack
*stack
= ctx
->CurrentStack
;
396 FLUSH_VERTICES(ctx
, 0);
398 if (MESA_VERBOSE
&VERBOSE_API
)
399 _mesa_debug(ctx
, "glPopMatrix %s\n",
400 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
402 if (!pop_matrix(ctx
, stack
)) {
403 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
404 _mesa_error(ctx
, GL_STACK_UNDERFLOW
,
405 "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
406 ctx
->Texture
.CurrentUnit
);
409 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix(mode=%s)",
410 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
417 _mesa_MatrixPopEXT( GLenum matrixMode
)
419 GET_CURRENT_CONTEXT(ctx
);
420 struct gl_matrix_stack
*stack
= get_named_matrix_stack(ctx
, matrixMode
,
425 if (!pop_matrix(ctx
, stack
)) {
426 if (matrixMode
== GL_TEXTURE
) {
427 _mesa_error(ctx
, GL_STACK_UNDERFLOW
,
428 "glMatrixPopEXT(mode=GL_TEXTURE, unit=%d)",
429 ctx
->Texture
.CurrentUnit
);
432 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "glMatrixPopEXT(mode=%s)",
433 _mesa_enum_to_string(matrixMode
));
440 matrix_load_identity(struct gl_matrix_stack
* stack
)
442 GET_CURRENT_CONTEXT(ctx
);
444 FLUSH_VERTICES(ctx
, 0);
446 _math_matrix_set_identity(stack
->Top
);
447 ctx
->NewState
|= stack
->DirtyFlag
;
450 * Replace the current matrix with the identity matrix.
452 * \sa glLoadIdentity().
454 * Flushes the vertices and calls _math_matrix_set_identity() with the
455 * top-most matrix in the current stack.
456 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
459 _mesa_LoadIdentity( void )
461 GET_CURRENT_CONTEXT(ctx
);
463 if (MESA_VERBOSE
& VERBOSE_API
)
464 _mesa_debug(ctx
, "glLoadIdentity()\n");
466 matrix_load_identity(ctx
->CurrentStack
);
471 _mesa_MatrixLoadIdentityEXT( GLenum matrixMode
)
473 struct gl_matrix_stack
*stack
;
474 GET_CURRENT_CONTEXT(ctx
);
475 stack
= get_named_matrix_stack(ctx
, matrixMode
, "glMatrixLoadIdentityEXT");
479 matrix_load_identity(stack
);
484 matrix_load(struct gl_matrix_stack
*stack
, const GLfloat
*m
, const char* caller
)
486 GET_CURRENT_CONTEXT(ctx
);
488 if (MESA_VERBOSE
& VERBOSE_API
)
490 "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
492 m
[0], m
[4], m
[8], m
[12],
493 m
[1], m
[5], m
[9], m
[13],
494 m
[2], m
[6], m
[10], m
[14],
495 m
[3], m
[7], m
[11], m
[15]);
497 if (memcmp(m
, stack
->Top
->m
, 16 * sizeof(GLfloat
)) != 0) {
498 FLUSH_VERTICES(ctx
, 0);
499 _math_matrix_loadf( stack
->Top
, m
);
500 ctx
->NewState
|= stack
->DirtyFlag
;
506 * Replace the current matrix with a given matrix.
510 * \sa glLoadMatrixf().
512 * Flushes the vertices and calls _math_matrix_loadf() with the top-most
513 * matrix in the current stack and the given matrix.
514 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
517 _mesa_LoadMatrixf( const GLfloat
*m
)
519 GET_CURRENT_CONTEXT(ctx
);
520 matrix_load(ctx
->CurrentStack
, m
, "glLoadMatrix");
525 * Replace the named matrix with a given matrix.
527 * \param matrixMode matrix to replace
530 * \sa glLoadMatrixf().
533 _mesa_MatrixLoadfEXT( GLenum matrixMode
, const GLfloat
*m
)
535 GET_CURRENT_CONTEXT(ctx
);
536 struct gl_matrix_stack
* stack
=
537 get_named_matrix_stack(ctx
, matrixMode
, "glMatrixLoadfEXT");
541 matrix_load(stack
, m
, "glMatrixLoadfEXT");
546 matrix_mult(struct gl_matrix_stack
*stack
, const GLfloat
*m
, const char* caller
)
548 GET_CURRENT_CONTEXT(ctx
);
550 if (MESA_VERBOSE
& VERBOSE_API
)
552 "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
554 m
[0], m
[4], m
[8], m
[12],
555 m
[1], m
[5], m
[9], m
[13],
556 m
[2], m
[6], m
[10], m
[14],
557 m
[3], m
[7], m
[11], m
[15]);
559 FLUSH_VERTICES(ctx
, 0);
560 _math_matrix_mul_floats(stack
->Top
, m
);
561 ctx
->NewState
|= stack
->DirtyFlag
;
566 * Multiply the current matrix with a given matrix.
570 * \sa glMultMatrixf().
572 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
573 * matrix in the current stack and the given matrix. Marks
574 * __struct gl_contextRec::NewState with the dirty stack flag.
577 _mesa_MultMatrixf( const GLfloat
*m
)
579 GET_CURRENT_CONTEXT(ctx
);
580 matrix_mult(ctx
->CurrentStack
, m
, "glMultMatrix");
585 _mesa_MatrixMultfEXT( GLenum matrixMode
, const GLfloat
*m
)
587 GET_CURRENT_CONTEXT(ctx
);
588 struct gl_matrix_stack
* stack
=
589 get_named_matrix_stack(ctx
, matrixMode
, "glMatrixMultfEXT");
593 matrix_mult(stack
, m
, "glMultMatrix");
598 matrix_rotate(struct gl_matrix_stack
*stack
, GLfloat angle
,
599 GLfloat x
, GLfloat y
, GLfloat z
, const char* caller
)
601 GET_CURRENT_CONTEXT(ctx
);
603 FLUSH_VERTICES(ctx
, 0);
605 _math_matrix_rotate(stack
->Top
, angle
, x
, y
, z
);
606 ctx
->NewState
|=stack
->DirtyFlag
;
612 * Multiply the current matrix with a rotation matrix.
614 * \param angle angle of rotation, in degrees.
615 * \param x rotation vector x coordinate.
616 * \param y rotation vector y coordinate.
617 * \param z rotation vector z coordinate.
621 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
622 * matrix in the current stack and the given parameters. Marks
623 * __struct gl_contextRec::NewState with the dirty stack flag.
626 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
628 GET_CURRENT_CONTEXT(ctx
);
629 matrix_rotate(ctx
->CurrentStack
, angle
, x
, y
, z
, "glRotatef");
634 _mesa_MatrixRotatefEXT( GLenum matrixMode
, GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
636 GET_CURRENT_CONTEXT(ctx
);
637 struct gl_matrix_stack
*stack
=
638 get_named_matrix_stack(ctx
, matrixMode
, "glMatrixRotatefEXT");
642 matrix_rotate(stack
, angle
, x
, y
, z
, "glMatrixRotatefEXT");
647 * Multiply the current matrix with a general scaling matrix.
649 * \param x x axis scale factor.
650 * \param y y axis scale factor.
651 * \param z z axis scale factor.
655 * Flushes the vertices and calls _math_matrix_scale() with the top-most
656 * matrix in the current stack and the given parameters. Marks
657 * __struct gl_contextRec::NewState with the dirty stack flag.
660 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
662 GET_CURRENT_CONTEXT(ctx
);
664 FLUSH_VERTICES(ctx
, 0);
665 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
666 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
671 _mesa_MatrixScalefEXT( GLenum matrixMode
, GLfloat x
, GLfloat y
, GLfloat z
)
673 struct gl_matrix_stack
*stack
;
674 GET_CURRENT_CONTEXT(ctx
);
676 stack
= get_named_matrix_stack(ctx
, matrixMode
, "glMatrixScalefEXT");
680 FLUSH_VERTICES(ctx
, 0);
681 _math_matrix_scale(stack
->Top
, x
, y
, z
);
682 ctx
->NewState
|= stack
->DirtyFlag
;
687 * Multiply the current matrix with a translation matrix.
689 * \param x translation vector x coordinate.
690 * \param y translation vector y coordinate.
691 * \param z translation vector z coordinate.
693 * \sa glTranslatef().
695 * Flushes the vertices and calls _math_matrix_translate() with the top-most
696 * matrix in the current stack and the given parameters. Marks
697 * __struct gl_contextRec::NewState with the dirty stack flag.
700 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
702 GET_CURRENT_CONTEXT(ctx
);
704 FLUSH_VERTICES(ctx
, 0);
705 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
706 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
711 _mesa_MatrixTranslatefEXT( GLenum matrixMode
, GLfloat x
, GLfloat y
, GLfloat z
)
713 GET_CURRENT_CONTEXT(ctx
);
714 struct gl_matrix_stack
*stack
=
715 get_named_matrix_stack(ctx
, matrixMode
, "glMatrixTranslatefEXT");
719 FLUSH_VERTICES(ctx
, 0);
720 _math_matrix_translate(stack
->Top
, x
, y
, z
);
721 ctx
->NewState
|= stack
->DirtyFlag
;
726 _mesa_LoadMatrixd( const GLdouble
*m
)
731 for (i
= 0; i
< 16; i
++)
732 f
[i
] = (GLfloat
) m
[i
];
733 _mesa_LoadMatrixf(f
);
738 _mesa_MatrixLoaddEXT( GLenum matrixMode
, const GLdouble
*m
)
742 for (unsigned i
= 0; i
< 16; i
++)
743 f
[i
] = (GLfloat
) m
[i
];
744 _mesa_MatrixLoadfEXT(matrixMode
, f
);
749 _mesa_MultMatrixd( const GLdouble
*m
)
754 for (i
= 0; i
< 16; i
++)
755 f
[i
] = (GLfloat
) m
[i
];
756 _mesa_MultMatrixf( f
);
761 _mesa_MatrixMultdEXT( GLenum matrixMode
, const GLdouble
*m
)
765 for (unsigned i
= 0; i
< 16; i
++)
766 f
[i
] = (GLfloat
) m
[i
];
767 _mesa_MatrixMultfEXT(matrixMode
, f
);
772 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
774 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
779 _mesa_MatrixRotatedEXT( GLenum matrixMode
, GLdouble angle
,
780 GLdouble x
, GLdouble y
, GLdouble z
)
782 _mesa_MatrixRotatefEXT(matrixMode
, (GLfloat
) angle
,
783 (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
788 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
790 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
795 _mesa_MatrixScaledEXT( GLenum matrixMode
, GLdouble x
, GLdouble y
, GLdouble z
)
797 _mesa_MatrixScalefEXT(matrixMode
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
802 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
804 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
809 _mesa_MatrixTranslatedEXT( GLenum matrixMode
, GLdouble x
, GLdouble y
, GLdouble z
)
811 _mesa_MatrixTranslatefEXT(matrixMode
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
816 _mesa_LoadTransposeMatrixf( const GLfloat
*m
)
820 _math_transposef(tm
, m
);
821 _mesa_LoadMatrixf(tm
);
825 _mesa_MatrixLoadTransposefEXT( GLenum matrixMode
, const GLfloat
*m
)
829 _math_transposef(tm
, m
);
830 _mesa_MatrixLoadfEXT(matrixMode
, tm
);
834 _mesa_LoadTransposeMatrixd( const GLdouble
*m
)
838 _math_transposefd(tm
, m
);
839 _mesa_LoadMatrixf(tm
);
843 _mesa_MatrixLoadTransposedEXT( GLenum matrixMode
, const GLdouble
*m
)
847 _math_transposefd(tm
, m
);
848 _mesa_MatrixLoadfEXT(matrixMode
, tm
);
852 _mesa_MultTransposeMatrixf( const GLfloat
*m
)
856 _math_transposef(tm
, m
);
857 _mesa_MultMatrixf(tm
);
861 _mesa_MatrixMultTransposefEXT( GLenum matrixMode
, const GLfloat
*m
)
865 _math_transposef(tm
, m
);
866 _mesa_MatrixMultfEXT(matrixMode
, tm
);
870 _mesa_MultTransposeMatrixd( const GLdouble
*m
)
874 _math_transposefd(tm
, m
);
875 _mesa_MultMatrixf(tm
);
879 _mesa_MatrixMultTransposedEXT( GLenum matrixMode
, const GLdouble
*m
)
883 _math_transposefd(tm
, m
);
884 _mesa_MatrixMultfEXT(matrixMode
, tm
);
887 /**********************************************************************/
888 /** \name State management */
893 * Update the projection matrix stack.
895 * \param ctx GL context.
897 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
898 * stack, and recomputes user clip positions if necessary.
900 * \note This routine references __struct gl_contextRec::Tranform attribute
901 * values to compute userclip positions in clip space, but is only called on
902 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
903 * date across changes to the __struct gl_contextRec::Transform attributes.
906 update_projection( struct gl_context
*ctx
)
910 _math_matrix_analyse( ctx
->ProjectionMatrixStack
.Top
);
912 /* Recompute clip plane positions in clipspace. This is also done
913 * in _mesa_ClipPlane().
915 mask
= ctx
->Transform
.ClipPlanesEnabled
;
917 const int p
= u_bit_scan(&mask
);
919 _mesa_transform_vector( ctx
->Transform
._ClipUserPlane
[p
],
920 ctx
->Transform
.EyeUserPlane
[p
],
921 ctx
->ProjectionMatrixStack
.Top
->inv
);
927 * Calculate the combined modelview-projection matrix.
929 * \param ctx GL context.
931 * Multiplies the top matrices of the projection and model view stacks into
932 * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
933 * and analyzes the resulting matrix via _math_matrix_analyse().
936 calculate_model_project_matrix( struct gl_context
*ctx
)
938 _math_matrix_mul_matrix( &ctx
->_ModelProjectMatrix
,
939 ctx
->ProjectionMatrixStack
.Top
,
940 ctx
->ModelviewMatrixStack
.Top
);
942 _math_matrix_analyse( &ctx
->_ModelProjectMatrix
);
947 * Updates the combined modelview-projection matrix.
949 * \param ctx GL context.
950 * \param new_state new state bit mask.
952 * If there is a new model view matrix then analyzes it. If there is a new
953 * projection matrix, updates it. Finally calls
954 * calculate_model_project_matrix() to recalculate the modelview-projection
957 void _mesa_update_modelview_project( struct gl_context
*ctx
, GLuint new_state
)
959 if (new_state
& _NEW_MODELVIEW
)
960 _math_matrix_analyse( ctx
->ModelviewMatrixStack
.Top
);
962 if (new_state
& _NEW_PROJECTION
)
963 update_projection( ctx
);
965 /* Keep ModelviewProject up to date always to allow tnl
966 * implementations that go model->clip even when eye is required.
968 calculate_model_project_matrix(ctx
);
974 /**********************************************************************/
975 /** Matrix stack initialization */
980 * Initialize a matrix stack.
982 * \param stack matrix stack.
983 * \param maxDepth maximum stack depth.
984 * \param dirtyFlag dirty flag.
986 * Allocates an array of \p maxDepth elements for the matrix stack and calls
987 * _math_matrix_ctr() for each element to initialize it.
990 init_matrix_stack(struct gl_matrix_stack
*stack
,
991 GLuint maxDepth
, GLuint dirtyFlag
)
994 stack
->MaxDepth
= maxDepth
;
995 stack
->DirtyFlag
= dirtyFlag
;
996 /* The stack will be dynamically resized at glPushMatrix() time */
997 stack
->Stack
= calloc(1, sizeof(GLmatrix
));
998 stack
->StackSize
= 1;
999 _math_matrix_ctr(&stack
->Stack
[0]);
1000 stack
->Top
= stack
->Stack
;
1004 * Free matrix stack.
1006 * \param stack matrix stack.
1008 * Calls _math_matrix_dtr() for each element of the matrix stack and
1012 free_matrix_stack( struct gl_matrix_stack
*stack
)
1015 for (i
= 0; i
< stack
->StackSize
; i
++) {
1016 _math_matrix_dtr(&stack
->Stack
[i
]);
1019 stack
->Stack
= stack
->Top
= NULL
;
1020 stack
->StackSize
= 0;
1026 /**********************************************************************/
1027 /** \name Initialization */
1032 * Initialize the context matrix data.
1034 * \param ctx GL context.
1036 * Initializes each of the matrix stacks and the combined modelview-projection
1039 void _mesa_init_matrix( struct gl_context
* ctx
)
1043 /* Initialize matrix stacks */
1044 init_matrix_stack(&ctx
->ModelviewMatrixStack
, MAX_MODELVIEW_STACK_DEPTH
,
1046 init_matrix_stack(&ctx
->ProjectionMatrixStack
, MAX_PROJECTION_STACK_DEPTH
,
1048 for (i
= 0; i
< ARRAY_SIZE(ctx
->TextureMatrixStack
); i
++)
1049 init_matrix_stack(&ctx
->TextureMatrixStack
[i
], MAX_TEXTURE_STACK_DEPTH
,
1050 _NEW_TEXTURE_MATRIX
);
1051 for (i
= 0; i
< ARRAY_SIZE(ctx
->ProgramMatrixStack
); i
++)
1052 init_matrix_stack(&ctx
->ProgramMatrixStack
[i
],
1053 MAX_PROGRAM_MATRIX_STACK_DEPTH
, _NEW_TRACK_MATRIX
);
1054 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
1056 /* Init combined Modelview*Projection matrix */
1057 _math_matrix_ctr( &ctx
->_ModelProjectMatrix
);
1062 * Free the context matrix data.
1064 * \param ctx GL context.
1066 * Frees each of the matrix stacks and the combined modelview-projection
1069 void _mesa_free_matrix_data( struct gl_context
*ctx
)
1073 free_matrix_stack(&ctx
->ModelviewMatrixStack
);
1074 free_matrix_stack(&ctx
->ProjectionMatrixStack
);
1075 for (i
= 0; i
< ARRAY_SIZE(ctx
->TextureMatrixStack
); i
++)
1076 free_matrix_stack(&ctx
->TextureMatrixStack
[i
]);
1077 for (i
= 0; i
< ARRAY_SIZE(ctx
->ProgramMatrixStack
); i
++)
1078 free_matrix_stack(&ctx
->ProgramMatrixStack
[i
]);
1079 /* combined Modelview*Projection matrix */
1080 _math_matrix_dtr( &ctx
->_ModelProjectMatrix
);
1086 * Initialize the context transform attribute group.
1088 * \param ctx GL context.
1090 * \todo Move this to a new file with other 'transform' routines.
1092 void _mesa_init_transform( struct gl_context
*ctx
)
1096 /* Transformation group */
1097 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
1098 ctx
->Transform
.Normalize
= GL_FALSE
;
1099 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
1100 ctx
->Transform
.RasterPositionUnclipped
= GL_FALSE
;
1101 for (i
=0;i
<ctx
->Const
.MaxClipPlanes
;i
++) {
1102 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
1104 ctx
->Transform
.ClipPlanesEnabled
= 0;