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"
50 * Apply a perspective projection matrix.
52 * \param left left clipping plane coordinate.
53 * \param right right clipping plane coordinate.
54 * \param bottom bottom clipping plane coordinate.
55 * \param top top clipping plane coordinate.
56 * \param nearval distance to the near clipping plane.
57 * \param farval distance to the far clipping plane.
61 * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with
62 * the top matrix of the current matrix stack and sets
63 * __struct gl_contextRec::NewState.
66 _mesa_Frustum( GLdouble left
, GLdouble right
,
67 GLdouble bottom
, GLdouble top
,
68 GLdouble nearval
, GLdouble farval
)
70 GET_CURRENT_CONTEXT(ctx
);
72 FLUSH_VERTICES(ctx
, 0);
80 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFrustum" );
84 _math_matrix_frustum( ctx
->CurrentStack
->Top
,
85 (GLfloat
) left
, (GLfloat
) right
,
86 (GLfloat
) bottom
, (GLfloat
) top
,
87 (GLfloat
) nearval
, (GLfloat
) farval
);
88 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
93 * Apply an orthographic projection matrix.
95 * \param left left clipping plane coordinate.
96 * \param right right clipping plane coordinate.
97 * \param bottom bottom clipping plane coordinate.
98 * \param top top clipping plane coordinate.
99 * \param nearval distance to the near clipping plane.
100 * \param farval distance to the far clipping plane.
104 * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
105 * the top matrix of the current matrix stack and sets
106 * __struct gl_contextRec::NewState.
109 _mesa_Ortho( GLdouble left
, GLdouble right
,
110 GLdouble bottom
, GLdouble top
,
111 GLdouble nearval
, GLdouble farval
)
113 GET_CURRENT_CONTEXT(ctx
);
115 FLUSH_VERTICES(ctx
, 0);
117 if (MESA_VERBOSE
& VERBOSE_API
)
118 _mesa_debug(ctx
, "glOrtho(%f, %f, %f, %f, %f, %f)\n",
119 left
, right
, bottom
, top
, nearval
, farval
);
125 _mesa_error( ctx
, GL_INVALID_VALUE
, "glOrtho" );
129 _math_matrix_ortho( ctx
->CurrentStack
->Top
,
130 (GLfloat
) left
, (GLfloat
) right
,
131 (GLfloat
) bottom
, (GLfloat
) top
,
132 (GLfloat
) nearval
, (GLfloat
) farval
);
133 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
138 * Set the current matrix stack.
140 * \param mode matrix stack.
142 * \sa glMatrixMode().
144 * Flushes the vertices, validates the parameter and updates
145 * __struct gl_contextRec::CurrentStack and gl_transform_attrib::MatrixMode
146 * with the specified matrix stack.
149 _mesa_MatrixMode( GLenum mode
)
151 GET_CURRENT_CONTEXT(ctx
);
153 if (ctx
->Transform
.MatrixMode
== mode
&& mode
!= GL_TEXTURE
)
158 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
161 ctx
->CurrentStack
= &ctx
->ProjectionMatrixStack
;
164 /* This error check is disabled because if we're called from
165 * glPopAttrib() when the active texture unit is >= MaxTextureCoordUnits
166 * we'll generate an unexpected error.
167 * From the GL_ARB_vertex_shader spec it sounds like we should instead
168 * do error checking in other places when we actually try to access
169 * texture matrices beyond MaxTextureCoordUnits.
172 if (ctx
->Texture
.CurrentUnit
>= ctx
->Const
.MaxTextureCoordUnits
) {
173 _mesa_error(ctx
, GL_INVALID_OPERATION
,
174 "glMatrixMode(invalid tex unit %d)",
175 ctx
->Texture
.CurrentUnit
);
179 assert(ctx
->Texture
.CurrentUnit
< ARRAY_SIZE(ctx
->TextureMatrixStack
));
180 ctx
->CurrentStack
= &ctx
->TextureMatrixStack
[ctx
->Texture
.CurrentUnit
];
190 if (ctx
->API
== API_OPENGL_COMPAT
191 && (ctx
->Extensions
.ARB_vertex_program
||
192 ctx
->Extensions
.ARB_fragment_program
)) {
193 const GLuint m
= mode
- GL_MATRIX0_ARB
;
194 if (m
> ctx
->Const
.MaxProgramMatrices
) {
195 _mesa_error(ctx
, GL_INVALID_ENUM
,
196 "glMatrixMode(GL_MATRIX%d_ARB)", m
);
199 ctx
->CurrentStack
= &ctx
->ProgramMatrixStack
[m
];
202 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode(mode)" );
207 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode(mode)" );
211 ctx
->Transform
.MatrixMode
= mode
;
216 * Push the current matrix stack.
218 * \sa glPushMatrix().
220 * Verifies the current matrix stack is not full, and duplicates the top-most
221 * matrix in the stack.
222 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
225 _mesa_PushMatrix( void )
227 GET_CURRENT_CONTEXT(ctx
);
228 struct gl_matrix_stack
*stack
= ctx
->CurrentStack
;
230 if (MESA_VERBOSE
&VERBOSE_API
)
231 _mesa_debug(ctx
, "glPushMatrix %s\n",
232 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
234 if (stack
->Depth
+ 1 >= stack
->MaxDepth
) {
235 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
236 _mesa_error(ctx
, GL_STACK_OVERFLOW
,
237 "glPushMatrix(mode=GL_TEXTURE, unit=%d)",
238 ctx
->Texture
.CurrentUnit
);
241 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "glPushMatrix(mode=%s)",
242 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
246 if (stack
->Depth
+ 1 >= stack
->StackSize
) {
247 unsigned new_stack_size
= stack
->StackSize
* 2;
249 GLmatrix
*new_stack
= realloc(stack
->Stack
,
250 sizeof(*new_stack
) * new_stack_size
);
253 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glPushMatrix()");
257 for (i
= stack
->StackSize
; i
< new_stack_size
; i
++)
258 _math_matrix_ctr(&new_stack
[i
]);
260 stack
->Stack
= new_stack
;
261 stack
->StackSize
= new_stack_size
;
264 _math_matrix_copy( &stack
->Stack
[stack
->Depth
+ 1],
265 &stack
->Stack
[stack
->Depth
] );
267 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
268 ctx
->NewState
|= stack
->DirtyFlag
;
273 * Pop the current matrix stack.
277 * Flushes the vertices, verifies the current matrix stack is not empty, and
278 * moves the stack head down.
279 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
282 _mesa_PopMatrix( void )
284 GET_CURRENT_CONTEXT(ctx
);
285 struct gl_matrix_stack
*stack
= ctx
->CurrentStack
;
287 FLUSH_VERTICES(ctx
, 0);
289 if (MESA_VERBOSE
&VERBOSE_API
)
290 _mesa_debug(ctx
, "glPopMatrix %s\n",
291 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
293 if (stack
->Depth
== 0) {
294 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
295 _mesa_error(ctx
, GL_STACK_UNDERFLOW
,
296 "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
297 ctx
->Texture
.CurrentUnit
);
300 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix(mode=%s)",
301 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
306 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
307 ctx
->NewState
|= stack
->DirtyFlag
;
312 * Replace the current matrix with the identity matrix.
314 * \sa glLoadIdentity().
316 * Flushes the vertices and calls _math_matrix_set_identity() with the
317 * top-most matrix in the current stack.
318 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
321 _mesa_LoadIdentity( void )
323 GET_CURRENT_CONTEXT(ctx
);
325 FLUSH_VERTICES(ctx
, 0);
327 if (MESA_VERBOSE
& VERBOSE_API
)
328 _mesa_debug(ctx
, "glLoadIdentity()\n");
330 _math_matrix_set_identity( ctx
->CurrentStack
->Top
);
331 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
336 * Replace the current matrix with a given matrix.
340 * \sa glLoadMatrixf().
342 * Flushes the vertices and calls _math_matrix_loadf() with the top-most
343 * matrix in the current stack and the given matrix.
344 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
347 _mesa_LoadMatrixf( const GLfloat
*m
)
349 GET_CURRENT_CONTEXT(ctx
);
351 if (MESA_VERBOSE
& VERBOSE_API
)
353 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
354 m
[0], m
[4], m
[8], m
[12],
355 m
[1], m
[5], m
[9], m
[13],
356 m
[2], m
[6], m
[10], m
[14],
357 m
[3], m
[7], m
[11], m
[15]);
359 FLUSH_VERTICES(ctx
, 0);
360 _math_matrix_loadf( ctx
->CurrentStack
->Top
, m
);
361 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
366 * Multiply the current matrix with a given matrix.
370 * \sa glMultMatrixf().
372 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
373 * matrix in the current stack and the given matrix. Marks
374 * __struct gl_contextRec::NewState with the dirty stack flag.
377 _mesa_MultMatrixf( const GLfloat
*m
)
379 GET_CURRENT_CONTEXT(ctx
);
381 if (MESA_VERBOSE
& VERBOSE_API
)
383 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
384 m
[0], m
[4], m
[8], m
[12],
385 m
[1], m
[5], m
[9], m
[13],
386 m
[2], m
[6], m
[10], m
[14],
387 m
[3], m
[7], m
[11], m
[15]);
389 FLUSH_VERTICES(ctx
, 0);
390 _math_matrix_mul_floats( ctx
->CurrentStack
->Top
, m
);
391 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
396 * Multiply the current matrix with a rotation matrix.
398 * \param angle angle of rotation, in degrees.
399 * \param x rotation vector x coordinate.
400 * \param y rotation vector y coordinate.
401 * \param z rotation vector z coordinate.
405 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
406 * matrix in the current stack and the given parameters. Marks
407 * __struct gl_contextRec::NewState with the dirty stack flag.
410 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
412 GET_CURRENT_CONTEXT(ctx
);
414 FLUSH_VERTICES(ctx
, 0);
416 _math_matrix_rotate( ctx
->CurrentStack
->Top
, angle
, x
, y
, z
);
417 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
423 * Multiply the current matrix with a general scaling matrix.
425 * \param x x axis scale factor.
426 * \param y y axis scale factor.
427 * \param z z axis scale factor.
431 * Flushes the vertices and calls _math_matrix_scale() with the top-most
432 * matrix in the current stack and the given parameters. Marks
433 * __struct gl_contextRec::NewState with the dirty stack flag.
436 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
438 GET_CURRENT_CONTEXT(ctx
);
440 FLUSH_VERTICES(ctx
, 0);
441 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
442 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
447 * Multiply the current matrix with a translation matrix.
449 * \param x translation vector x coordinate.
450 * \param y translation vector y coordinate.
451 * \param z translation vector z coordinate.
453 * \sa glTranslatef().
455 * Flushes the vertices and calls _math_matrix_translate() with the top-most
456 * matrix in the current stack and the given parameters. Marks
457 * __struct gl_contextRec::NewState with the dirty stack flag.
460 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
462 GET_CURRENT_CONTEXT(ctx
);
464 FLUSH_VERTICES(ctx
, 0);
465 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
466 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
471 _mesa_LoadMatrixd( const GLdouble
*m
)
476 for (i
= 0; i
< 16; i
++)
477 f
[i
] = (GLfloat
) m
[i
];
478 _mesa_LoadMatrixf(f
);
482 _mesa_MultMatrixd( const GLdouble
*m
)
487 for (i
= 0; i
< 16; i
++)
488 f
[i
] = (GLfloat
) m
[i
];
489 _mesa_MultMatrixf( f
);
494 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
496 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
501 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
503 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
508 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
510 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
515 _mesa_LoadTransposeMatrixf( const GLfloat
*m
)
519 _math_transposef(tm
, m
);
520 _mesa_LoadMatrixf(tm
);
525 _mesa_LoadTransposeMatrixd( const GLdouble
*m
)
529 _math_transposefd(tm
, m
);
530 _mesa_LoadMatrixf(tm
);
535 _mesa_MultTransposeMatrixf( const GLfloat
*m
)
539 _math_transposef(tm
, m
);
540 _mesa_MultMatrixf(tm
);
545 _mesa_MultTransposeMatrixd( const GLdouble
*m
)
549 _math_transposefd(tm
, m
);
550 _mesa_MultMatrixf(tm
);
555 /**********************************************************************/
556 /** \name State management */
561 * Update the projection matrix stack.
563 * \param ctx GL context.
565 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
566 * stack, and recomputes user clip positions if necessary.
568 * \note This routine references __struct gl_contextRec::Tranform attribute
569 * values to compute userclip positions in clip space, but is only called on
570 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
571 * date across changes to the __struct gl_contextRec::Transform attributes.
574 update_projection( struct gl_context
*ctx
)
578 _math_matrix_analyse( ctx
->ProjectionMatrixStack
.Top
);
580 /* Recompute clip plane positions in clipspace. This is also done
581 * in _mesa_ClipPlane().
583 mask
= ctx
->Transform
.ClipPlanesEnabled
;
585 const int p
= u_bit_scan(&mask
);
587 _mesa_transform_vector( ctx
->Transform
._ClipUserPlane
[p
],
588 ctx
->Transform
.EyeUserPlane
[p
],
589 ctx
->ProjectionMatrixStack
.Top
->inv
);
595 * Calculate the combined modelview-projection matrix.
597 * \param ctx GL context.
599 * Multiplies the top matrices of the projection and model view stacks into
600 * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
601 * and analyzes the resulting matrix via _math_matrix_analyse().
604 calculate_model_project_matrix( struct gl_context
*ctx
)
606 _math_matrix_mul_matrix( &ctx
->_ModelProjectMatrix
,
607 ctx
->ProjectionMatrixStack
.Top
,
608 ctx
->ModelviewMatrixStack
.Top
);
610 _math_matrix_analyse( &ctx
->_ModelProjectMatrix
);
615 * Updates the combined modelview-projection matrix.
617 * \param ctx GL context.
618 * \param new_state new state bit mask.
620 * If there is a new model view matrix then analyzes it. If there is a new
621 * projection matrix, updates it. Finally calls
622 * calculate_model_project_matrix() to recalculate the modelview-projection
625 void _mesa_update_modelview_project( struct gl_context
*ctx
, GLuint new_state
)
627 if (new_state
& _NEW_MODELVIEW
)
628 _math_matrix_analyse( ctx
->ModelviewMatrixStack
.Top
);
630 if (new_state
& _NEW_PROJECTION
)
631 update_projection( ctx
);
633 /* Keep ModelviewProject up to date always to allow tnl
634 * implementations that go model->clip even when eye is required.
636 calculate_model_project_matrix(ctx
);
642 /**********************************************************************/
643 /** Matrix stack initialization */
648 * Initialize a matrix stack.
650 * \param stack matrix stack.
651 * \param maxDepth maximum stack depth.
652 * \param dirtyFlag dirty flag.
654 * Allocates an array of \p maxDepth elements for the matrix stack and calls
655 * _math_matrix_ctr() for each element to initialize it.
658 init_matrix_stack( struct gl_matrix_stack
*stack
,
659 GLuint maxDepth
, GLuint dirtyFlag
)
664 stack
->MaxDepth
= maxDepth
;
665 stack
->DirtyFlag
= dirtyFlag
;
666 /* The stack will be dynamically resized at glPushMatrix() time */
667 stack
->Stack
= calloc(1, sizeof(GLmatrix
));
668 stack
->StackSize
= 1;
669 for (i
= 0; i
< stack
->StackSize
; i
++) {
670 _math_matrix_ctr(&stack
->Stack
[i
]);
672 stack
->Top
= stack
->Stack
;
678 * \param stack matrix stack.
680 * Calls _math_matrix_dtr() for each element of the matrix stack and
684 free_matrix_stack( struct gl_matrix_stack
*stack
)
687 for (i
= 0; i
< stack
->StackSize
; i
++) {
688 _math_matrix_dtr(&stack
->Stack
[i
]);
691 stack
->Stack
= stack
->Top
= NULL
;
692 stack
->StackSize
= 0;
698 /**********************************************************************/
699 /** \name Initialization */
704 * Initialize the context matrix data.
706 * \param ctx GL context.
708 * Initializes each of the matrix stacks and the combined modelview-projection
711 void _mesa_init_matrix( struct gl_context
* ctx
)
715 /* Initialize matrix stacks */
716 init_matrix_stack(&ctx
->ModelviewMatrixStack
, MAX_MODELVIEW_STACK_DEPTH
,
718 init_matrix_stack(&ctx
->ProjectionMatrixStack
, MAX_PROJECTION_STACK_DEPTH
,
720 for (i
= 0; i
< ARRAY_SIZE(ctx
->TextureMatrixStack
); i
++)
721 init_matrix_stack(&ctx
->TextureMatrixStack
[i
], MAX_TEXTURE_STACK_DEPTH
,
722 _NEW_TEXTURE_MATRIX
);
723 for (i
= 0; i
< ARRAY_SIZE(ctx
->ProgramMatrixStack
); i
++)
724 init_matrix_stack(&ctx
->ProgramMatrixStack
[i
],
725 MAX_PROGRAM_MATRIX_STACK_DEPTH
, _NEW_TRACK_MATRIX
);
726 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
728 /* Init combined Modelview*Projection matrix */
729 _math_matrix_ctr( &ctx
->_ModelProjectMatrix
);
734 * Free the context matrix data.
736 * \param ctx GL context.
738 * Frees each of the matrix stacks and the combined modelview-projection
741 void _mesa_free_matrix_data( struct gl_context
*ctx
)
745 free_matrix_stack(&ctx
->ModelviewMatrixStack
);
746 free_matrix_stack(&ctx
->ProjectionMatrixStack
);
747 for (i
= 0; i
< ARRAY_SIZE(ctx
->TextureMatrixStack
); i
++)
748 free_matrix_stack(&ctx
->TextureMatrixStack
[i
]);
749 for (i
= 0; i
< ARRAY_SIZE(ctx
->ProgramMatrixStack
); i
++)
750 free_matrix_stack(&ctx
->ProgramMatrixStack
[i
]);
751 /* combined Modelview*Projection matrix */
752 _math_matrix_dtr( &ctx
->_ModelProjectMatrix
);
758 * Initialize the context transform attribute group.
760 * \param ctx GL context.
762 * \todo Move this to a new file with other 'transform' routines.
764 void _mesa_init_transform( struct gl_context
*ctx
)
768 /* Transformation group */
769 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
770 ctx
->Transform
.Normalize
= GL_FALSE
;
771 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
772 ctx
->Transform
.RasterPositionUnclipped
= GL_FALSE
;
773 for (i
=0;i
<ctx
->Const
.MaxClipPlanes
;i
++) {
774 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
776 ctx
->Transform
.ClipPlanesEnabled
= 0;