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 _mesa_load_identity_matrix(struct gl_context
*ctx
, struct gl_matrix_stack
*stack
)
442 FLUSH_VERTICES(ctx
, 0);
444 _math_matrix_set_identity(stack
->Top
);
445 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 _mesa_load_identity_matrix(ctx
, 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 _mesa_load_identity_matrix(ctx
, stack
);
484 _mesa_load_matrix(struct gl_context
*ctx
, struct gl_matrix_stack
*stack
,
487 if (memcmp(m
, stack
->Top
->m
, 16 * sizeof(GLfloat
)) != 0) {
488 FLUSH_VERTICES(ctx
, 0);
489 _math_matrix_loadf(stack
->Top
, m
);
490 ctx
->NewState
|= stack
->DirtyFlag
;
496 matrix_load(struct gl_context
*ctx
, struct gl_matrix_stack
*stack
,
497 const GLfloat
*m
, const char* caller
)
500 if (MESA_VERBOSE
& VERBOSE_API
)
502 "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
504 m
[0], m
[4], m
[8], m
[12],
505 m
[1], m
[5], m
[9], m
[13],
506 m
[2], m
[6], m
[10], m
[14],
507 m
[3], m
[7], m
[11], m
[15]);
509 _mesa_load_matrix(ctx
, stack
, m
);
514 * Replace the current matrix with a given matrix.
518 * \sa glLoadMatrixf().
520 * Flushes the vertices and calls _math_matrix_loadf() with the top-most
521 * matrix in the current stack and the given matrix.
522 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
525 _mesa_LoadMatrixf( const GLfloat
*m
)
527 GET_CURRENT_CONTEXT(ctx
);
528 matrix_load(ctx
, ctx
->CurrentStack
, m
, "glLoadMatrix");
533 * Replace the named matrix with a given matrix.
535 * \param matrixMode matrix to replace
538 * \sa glLoadMatrixf().
541 _mesa_MatrixLoadfEXT( GLenum matrixMode
, const GLfloat
*m
)
543 GET_CURRENT_CONTEXT(ctx
);
544 struct gl_matrix_stack
* stack
=
545 get_named_matrix_stack(ctx
, matrixMode
, "glMatrixLoadfEXT");
549 matrix_load(ctx
, stack
, m
, "glMatrixLoadfEXT");
554 matrix_mult(struct gl_matrix_stack
*stack
, const GLfloat
*m
, const char* caller
)
556 GET_CURRENT_CONTEXT(ctx
);
558 if (MESA_VERBOSE
& VERBOSE_API
)
560 "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
562 m
[0], m
[4], m
[8], m
[12],
563 m
[1], m
[5], m
[9], m
[13],
564 m
[2], m
[6], m
[10], m
[14],
565 m
[3], m
[7], m
[11], m
[15]);
567 FLUSH_VERTICES(ctx
, 0);
568 _math_matrix_mul_floats(stack
->Top
, m
);
569 ctx
->NewState
|= stack
->DirtyFlag
;
574 * Multiply the current matrix with a given matrix.
578 * \sa glMultMatrixf().
580 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
581 * matrix in the current stack and the given matrix. Marks
582 * __struct gl_contextRec::NewState with the dirty stack flag.
585 _mesa_MultMatrixf( const GLfloat
*m
)
587 GET_CURRENT_CONTEXT(ctx
);
588 matrix_mult(ctx
->CurrentStack
, m
, "glMultMatrix");
593 _mesa_MatrixMultfEXT( GLenum matrixMode
, const GLfloat
*m
)
595 GET_CURRENT_CONTEXT(ctx
);
596 struct gl_matrix_stack
* stack
=
597 get_named_matrix_stack(ctx
, matrixMode
, "glMatrixMultfEXT");
601 matrix_mult(stack
, m
, "glMultMatrix");
606 matrix_rotate(struct gl_matrix_stack
*stack
, GLfloat angle
,
607 GLfloat x
, GLfloat y
, GLfloat z
, const char* caller
)
609 GET_CURRENT_CONTEXT(ctx
);
611 FLUSH_VERTICES(ctx
, 0);
613 _math_matrix_rotate(stack
->Top
, angle
, x
, y
, z
);
614 ctx
->NewState
|=stack
->DirtyFlag
;
620 * Multiply the current matrix with a rotation matrix.
622 * \param angle angle of rotation, in degrees.
623 * \param x rotation vector x coordinate.
624 * \param y rotation vector y coordinate.
625 * \param z rotation vector z coordinate.
629 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
630 * matrix in the current stack and the given parameters. Marks
631 * __struct gl_contextRec::NewState with the dirty stack flag.
634 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
636 GET_CURRENT_CONTEXT(ctx
);
637 matrix_rotate(ctx
->CurrentStack
, angle
, x
, y
, z
, "glRotatef");
642 _mesa_MatrixRotatefEXT( GLenum matrixMode
, GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
644 GET_CURRENT_CONTEXT(ctx
);
645 struct gl_matrix_stack
*stack
=
646 get_named_matrix_stack(ctx
, matrixMode
, "glMatrixRotatefEXT");
650 matrix_rotate(stack
, angle
, x
, y
, z
, "glMatrixRotatefEXT");
655 * Multiply the current matrix with a general scaling matrix.
657 * \param x x axis scale factor.
658 * \param y y axis scale factor.
659 * \param z z axis scale factor.
663 * Flushes the vertices and calls _math_matrix_scale() with the top-most
664 * matrix in the current stack and the given parameters. Marks
665 * __struct gl_contextRec::NewState with the dirty stack flag.
668 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
670 GET_CURRENT_CONTEXT(ctx
);
672 FLUSH_VERTICES(ctx
, 0);
673 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
674 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
679 _mesa_MatrixScalefEXT( GLenum matrixMode
, GLfloat x
, GLfloat y
, GLfloat z
)
681 struct gl_matrix_stack
*stack
;
682 GET_CURRENT_CONTEXT(ctx
);
684 stack
= get_named_matrix_stack(ctx
, matrixMode
, "glMatrixScalefEXT");
688 FLUSH_VERTICES(ctx
, 0);
689 _math_matrix_scale(stack
->Top
, x
, y
, z
);
690 ctx
->NewState
|= stack
->DirtyFlag
;
695 * Multiply the current matrix with a translation matrix.
697 * \param x translation vector x coordinate.
698 * \param y translation vector y coordinate.
699 * \param z translation vector z coordinate.
701 * \sa glTranslatef().
703 * Flushes the vertices and calls _math_matrix_translate() with the top-most
704 * matrix in the current stack and the given parameters. Marks
705 * __struct gl_contextRec::NewState with the dirty stack flag.
708 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
710 GET_CURRENT_CONTEXT(ctx
);
712 FLUSH_VERTICES(ctx
, 0);
713 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
714 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
719 _mesa_MatrixTranslatefEXT( GLenum matrixMode
, GLfloat x
, GLfloat y
, GLfloat z
)
721 GET_CURRENT_CONTEXT(ctx
);
722 struct gl_matrix_stack
*stack
=
723 get_named_matrix_stack(ctx
, matrixMode
, "glMatrixTranslatefEXT");
727 FLUSH_VERTICES(ctx
, 0);
728 _math_matrix_translate(stack
->Top
, x
, y
, z
);
729 ctx
->NewState
|= stack
->DirtyFlag
;
734 _mesa_LoadMatrixd( const GLdouble
*m
)
739 for (i
= 0; i
< 16; i
++)
740 f
[i
] = (GLfloat
) m
[i
];
741 _mesa_LoadMatrixf(f
);
746 _mesa_MatrixLoaddEXT( GLenum matrixMode
, const GLdouble
*m
)
750 for (unsigned i
= 0; i
< 16; i
++)
751 f
[i
] = (GLfloat
) m
[i
];
752 _mesa_MatrixLoadfEXT(matrixMode
, f
);
757 _mesa_MultMatrixd( const GLdouble
*m
)
762 for (i
= 0; i
< 16; i
++)
763 f
[i
] = (GLfloat
) m
[i
];
764 _mesa_MultMatrixf( f
);
769 _mesa_MatrixMultdEXT( GLenum matrixMode
, const GLdouble
*m
)
773 for (unsigned i
= 0; i
< 16; i
++)
774 f
[i
] = (GLfloat
) m
[i
];
775 _mesa_MatrixMultfEXT(matrixMode
, f
);
780 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
782 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
787 _mesa_MatrixRotatedEXT( GLenum matrixMode
, GLdouble angle
,
788 GLdouble x
, GLdouble y
, GLdouble z
)
790 _mesa_MatrixRotatefEXT(matrixMode
, (GLfloat
) angle
,
791 (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
796 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
798 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
803 _mesa_MatrixScaledEXT( GLenum matrixMode
, GLdouble x
, GLdouble y
, GLdouble z
)
805 _mesa_MatrixScalefEXT(matrixMode
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
810 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
812 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
817 _mesa_MatrixTranslatedEXT( GLenum matrixMode
, GLdouble x
, GLdouble y
, GLdouble z
)
819 _mesa_MatrixTranslatefEXT(matrixMode
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
824 _mesa_LoadTransposeMatrixf( const GLfloat
*m
)
828 _math_transposef(tm
, m
);
829 _mesa_LoadMatrixf(tm
);
833 _mesa_MatrixLoadTransposefEXT( GLenum matrixMode
, const GLfloat
*m
)
837 _math_transposef(tm
, m
);
838 _mesa_MatrixLoadfEXT(matrixMode
, tm
);
842 _mesa_LoadTransposeMatrixd( const GLdouble
*m
)
846 _math_transposefd(tm
, m
);
847 _mesa_LoadMatrixf(tm
);
851 _mesa_MatrixLoadTransposedEXT( GLenum matrixMode
, const GLdouble
*m
)
855 _math_transposefd(tm
, m
);
856 _mesa_MatrixLoadfEXT(matrixMode
, tm
);
860 _mesa_MultTransposeMatrixf( const GLfloat
*m
)
864 _math_transposef(tm
, m
);
865 _mesa_MultMatrixf(tm
);
869 _mesa_MatrixMultTransposefEXT( GLenum matrixMode
, const GLfloat
*m
)
873 _math_transposef(tm
, m
);
874 _mesa_MatrixMultfEXT(matrixMode
, tm
);
878 _mesa_MultTransposeMatrixd( const GLdouble
*m
)
882 _math_transposefd(tm
, m
);
883 _mesa_MultMatrixf(tm
);
887 _mesa_MatrixMultTransposedEXT( GLenum matrixMode
, const GLdouble
*m
)
891 _math_transposefd(tm
, m
);
892 _mesa_MatrixMultfEXT(matrixMode
, tm
);
895 /**********************************************************************/
896 /** \name State management */
901 * Update the projection matrix stack.
903 * \param ctx GL context.
905 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
906 * stack, and recomputes user clip positions if necessary.
908 * \note This routine references __struct gl_contextRec::Tranform attribute
909 * values to compute userclip positions in clip space, but is only called on
910 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
911 * date across changes to the __struct gl_contextRec::Transform attributes.
914 update_projection( struct gl_context
*ctx
)
918 _math_matrix_analyse( ctx
->ProjectionMatrixStack
.Top
);
920 /* Recompute clip plane positions in clipspace. This is also done
921 * in _mesa_ClipPlane().
923 mask
= ctx
->Transform
.ClipPlanesEnabled
;
925 const int p
= u_bit_scan(&mask
);
927 _mesa_transform_vector( ctx
->Transform
._ClipUserPlane
[p
],
928 ctx
->Transform
.EyeUserPlane
[p
],
929 ctx
->ProjectionMatrixStack
.Top
->inv
);
935 * Calculate the combined modelview-projection matrix.
937 * \param ctx GL context.
939 * Multiplies the top matrices of the projection and model view stacks into
940 * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
941 * and analyzes the resulting matrix via _math_matrix_analyse().
944 calculate_model_project_matrix( struct gl_context
*ctx
)
946 _math_matrix_mul_matrix( &ctx
->_ModelProjectMatrix
,
947 ctx
->ProjectionMatrixStack
.Top
,
948 ctx
->ModelviewMatrixStack
.Top
);
950 _math_matrix_analyse( &ctx
->_ModelProjectMatrix
);
955 * Updates the combined modelview-projection matrix.
957 * \param ctx GL context.
958 * \param new_state new state bit mask.
960 * If there is a new model view matrix then analyzes it. If there is a new
961 * projection matrix, updates it. Finally calls
962 * calculate_model_project_matrix() to recalculate the modelview-projection
965 void _mesa_update_modelview_project( struct gl_context
*ctx
, GLuint new_state
)
967 if (new_state
& _NEW_MODELVIEW
)
968 _math_matrix_analyse( ctx
->ModelviewMatrixStack
.Top
);
970 if (new_state
& _NEW_PROJECTION
)
971 update_projection( ctx
);
973 /* Keep ModelviewProject up to date always to allow tnl
974 * implementations that go model->clip even when eye is required.
976 calculate_model_project_matrix(ctx
);
982 /**********************************************************************/
983 /** Matrix stack initialization */
988 * Initialize a matrix stack.
990 * \param stack matrix stack.
991 * \param maxDepth maximum stack depth.
992 * \param dirtyFlag dirty flag.
994 * Allocates an array of \p maxDepth elements for the matrix stack and calls
995 * _math_matrix_ctr() for each element to initialize it.
998 init_matrix_stack(struct gl_matrix_stack
*stack
,
999 GLuint maxDepth
, GLuint dirtyFlag
)
1002 stack
->MaxDepth
= maxDepth
;
1003 stack
->DirtyFlag
= dirtyFlag
;
1004 /* The stack will be dynamically resized at glPushMatrix() time */
1005 stack
->Stack
= calloc(1, sizeof(GLmatrix
));
1006 stack
->StackSize
= 1;
1007 _math_matrix_ctr(&stack
->Stack
[0]);
1008 stack
->Top
= stack
->Stack
;
1012 * Free matrix stack.
1014 * \param stack matrix stack.
1016 * Calls _math_matrix_dtr() for each element of the matrix stack and
1020 free_matrix_stack( struct gl_matrix_stack
*stack
)
1023 for (i
= 0; i
< stack
->StackSize
; i
++) {
1024 _math_matrix_dtr(&stack
->Stack
[i
]);
1027 stack
->Stack
= stack
->Top
= NULL
;
1028 stack
->StackSize
= 0;
1034 /**********************************************************************/
1035 /** \name Initialization */
1040 * Initialize the context matrix data.
1042 * \param ctx GL context.
1044 * Initializes each of the matrix stacks and the combined modelview-projection
1047 void _mesa_init_matrix( struct gl_context
* ctx
)
1051 /* Initialize matrix stacks */
1052 init_matrix_stack(&ctx
->ModelviewMatrixStack
, MAX_MODELVIEW_STACK_DEPTH
,
1054 init_matrix_stack(&ctx
->ProjectionMatrixStack
, MAX_PROJECTION_STACK_DEPTH
,
1056 for (i
= 0; i
< ARRAY_SIZE(ctx
->TextureMatrixStack
); i
++)
1057 init_matrix_stack(&ctx
->TextureMatrixStack
[i
], MAX_TEXTURE_STACK_DEPTH
,
1058 _NEW_TEXTURE_MATRIX
);
1059 for (i
= 0; i
< ARRAY_SIZE(ctx
->ProgramMatrixStack
); i
++)
1060 init_matrix_stack(&ctx
->ProgramMatrixStack
[i
],
1061 MAX_PROGRAM_MATRIX_STACK_DEPTH
, _NEW_TRACK_MATRIX
);
1062 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
1064 /* Init combined Modelview*Projection matrix */
1065 _math_matrix_ctr( &ctx
->_ModelProjectMatrix
);
1070 * Free the context matrix data.
1072 * \param ctx GL context.
1074 * Frees each of the matrix stacks and the combined modelview-projection
1077 void _mesa_free_matrix_data( struct gl_context
*ctx
)
1081 free_matrix_stack(&ctx
->ModelviewMatrixStack
);
1082 free_matrix_stack(&ctx
->ProjectionMatrixStack
);
1083 for (i
= 0; i
< ARRAY_SIZE(ctx
->TextureMatrixStack
); i
++)
1084 free_matrix_stack(&ctx
->TextureMatrixStack
[i
]);
1085 for (i
= 0; i
< ARRAY_SIZE(ctx
->ProgramMatrixStack
); i
++)
1086 free_matrix_stack(&ctx
->ProgramMatrixStack
[i
]);
1087 /* combined Modelview*Projection matrix */
1088 _math_matrix_dtr( &ctx
->_ModelProjectMatrix
);
1094 * Initialize the context transform attribute group.
1096 * \param ctx GL context.
1098 * \todo Move this to a new file with other 'transform' routines.
1100 void _mesa_init_transform( struct gl_context
*ctx
)
1104 /* Transformation group */
1105 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
1106 ctx
->Transform
.Normalize
= GL_FALSE
;
1107 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
1108 ctx
->Transform
.RasterPositionUnclipped
= GL_FALSE
;
1109 for (i
=0;i
<ctx
->Const
.MaxClipPlanes
;i
++) {
1110 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
1112 ctx
->Transform
.ClipPlanesEnabled
= 0;