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
)
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
];
98 * Apply a perspective projection matrix.
100 * \param left left clipping plane coordinate.
101 * \param right right clipping plane coordinate.
102 * \param bottom bottom clipping plane coordinate.
103 * \param top top clipping plane coordinate.
104 * \param nearval distance to the near clipping plane.
105 * \param farval distance to the far clipping plane.
109 * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with
110 * the top matrix of the current matrix stack and sets
111 * __struct gl_contextRec::NewState.
114 _mesa_Frustum( GLdouble left
, GLdouble right
,
115 GLdouble bottom
, GLdouble top
,
116 GLdouble nearval
, GLdouble farval
)
118 GET_CURRENT_CONTEXT(ctx
);
120 FLUSH_VERTICES(ctx
, 0);
122 if (nearval
<= 0.0 ||
128 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFrustum" );
132 _math_matrix_frustum( ctx
->CurrentStack
->Top
,
133 (GLfloat
) left
, (GLfloat
) right
,
134 (GLfloat
) bottom
, (GLfloat
) top
,
135 (GLfloat
) nearval
, (GLfloat
) farval
);
136 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
141 * Apply an orthographic projection matrix.
143 * \param left left clipping plane coordinate.
144 * \param right right clipping plane coordinate.
145 * \param bottom bottom clipping plane coordinate.
146 * \param top top clipping plane coordinate.
147 * \param nearval distance to the near clipping plane.
148 * \param farval distance to the far clipping plane.
152 * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
153 * the top matrix of the current matrix stack and sets
154 * __struct gl_contextRec::NewState.
157 _mesa_Ortho( GLdouble left
, GLdouble right
,
158 GLdouble bottom
, GLdouble top
,
159 GLdouble nearval
, GLdouble farval
)
161 GET_CURRENT_CONTEXT(ctx
);
163 FLUSH_VERTICES(ctx
, 0);
165 if (MESA_VERBOSE
& VERBOSE_API
)
166 _mesa_debug(ctx
, "glOrtho(%f, %f, %f, %f, %f, %f)\n",
167 left
, right
, bottom
, top
, nearval
, farval
);
173 _mesa_error( ctx
, GL_INVALID_VALUE
, "glOrtho" );
177 _math_matrix_ortho( ctx
->CurrentStack
->Top
,
178 (GLfloat
) left
, (GLfloat
) right
,
179 (GLfloat
) bottom
, (GLfloat
) top
,
180 (GLfloat
) nearval
, (GLfloat
) farval
);
181 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
186 * Set the current matrix stack.
188 * \param mode matrix stack.
190 * \sa glMatrixMode().
192 * Flushes the vertices, validates the parameter and updates
193 * __struct gl_contextRec::CurrentStack and gl_transform_attrib::MatrixMode
194 * with the specified matrix stack.
197 _mesa_MatrixMode( GLenum mode
)
199 struct gl_matrix_stack
* stack
;
200 GET_CURRENT_CONTEXT(ctx
);
202 if (ctx
->Transform
.MatrixMode
== mode
&& mode
!= GL_TEXTURE
)
205 stack
= get_named_matrix_stack(ctx
, mode
);
208 ctx
->CurrentStack
= stack
;
209 ctx
->Transform
.MatrixMode
= mode
;
212 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode(mode)" );
218 * Push the current matrix stack.
220 * \sa glPushMatrix().
222 * Verifies the current matrix stack is not full, and duplicates the top-most
223 * matrix in the stack.
224 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
227 _mesa_PushMatrix( void )
229 GET_CURRENT_CONTEXT(ctx
);
230 struct gl_matrix_stack
*stack
= ctx
->CurrentStack
;
232 if (MESA_VERBOSE
&VERBOSE_API
)
233 _mesa_debug(ctx
, "glPushMatrix %s\n",
234 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
236 if (stack
->Depth
+ 1 >= stack
->MaxDepth
) {
237 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
238 _mesa_error(ctx
, GL_STACK_OVERFLOW
,
239 "glPushMatrix(mode=GL_TEXTURE, unit=%d)",
240 ctx
->Texture
.CurrentUnit
);
243 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "glPushMatrix(mode=%s)",
244 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
248 if (stack
->Depth
+ 1 >= stack
->StackSize
) {
249 unsigned new_stack_size
= stack
->StackSize
* 2;
251 GLmatrix
*new_stack
= realloc(stack
->Stack
,
252 sizeof(*new_stack
) * new_stack_size
);
255 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glPushMatrix()");
259 for (i
= stack
->StackSize
; i
< new_stack_size
; i
++)
260 _math_matrix_ctr(&new_stack
[i
]);
262 stack
->Stack
= new_stack
;
263 stack
->StackSize
= new_stack_size
;
266 _math_matrix_copy( &stack
->Stack
[stack
->Depth
+ 1],
267 &stack
->Stack
[stack
->Depth
] );
269 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
270 ctx
->NewState
|= stack
->DirtyFlag
;
275 * Pop the current matrix stack.
279 * Flushes the vertices, verifies the current matrix stack is not empty, and
280 * moves the stack head down.
281 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
284 _mesa_PopMatrix( void )
286 GET_CURRENT_CONTEXT(ctx
);
287 struct gl_matrix_stack
*stack
= ctx
->CurrentStack
;
289 FLUSH_VERTICES(ctx
, 0);
291 if (MESA_VERBOSE
&VERBOSE_API
)
292 _mesa_debug(ctx
, "glPopMatrix %s\n",
293 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
295 if (stack
->Depth
== 0) {
296 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
297 _mesa_error(ctx
, GL_STACK_UNDERFLOW
,
298 "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
299 ctx
->Texture
.CurrentUnit
);
302 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix(mode=%s)",
303 _mesa_enum_to_string(ctx
->Transform
.MatrixMode
));
308 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
309 ctx
->NewState
|= stack
->DirtyFlag
;
314 * Replace the current matrix with the identity matrix.
316 * \sa glLoadIdentity().
318 * Flushes the vertices and calls _math_matrix_set_identity() with the
319 * top-most matrix in the current stack.
320 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
323 _mesa_LoadIdentity( void )
325 GET_CURRENT_CONTEXT(ctx
);
327 FLUSH_VERTICES(ctx
, 0);
329 if (MESA_VERBOSE
& VERBOSE_API
)
330 _mesa_debug(ctx
, "glLoadIdentity()\n");
332 _math_matrix_set_identity( ctx
->CurrentStack
->Top
);
333 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
338 * Replace the current matrix with a given matrix.
342 * \sa glLoadMatrixf().
344 * Flushes the vertices and calls _math_matrix_loadf() with the top-most
345 * matrix in the current stack and the given matrix.
346 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
349 _mesa_LoadMatrixf( const GLfloat
*m
)
351 GET_CURRENT_CONTEXT(ctx
);
353 if (MESA_VERBOSE
& VERBOSE_API
)
355 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
356 m
[0], m
[4], m
[8], m
[12],
357 m
[1], m
[5], m
[9], m
[13],
358 m
[2], m
[6], m
[10], m
[14],
359 m
[3], m
[7], m
[11], m
[15]);
361 if (memcmp(m
, ctx
->CurrentStack
->Top
->m
, 16 * sizeof(GLfloat
)) != 0) {
362 FLUSH_VERTICES(ctx
, 0);
363 _math_matrix_loadf( ctx
->CurrentStack
->Top
, m
);
364 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
370 * Multiply the current matrix with a given matrix.
374 * \sa glMultMatrixf().
376 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
377 * matrix in the current stack and the given matrix. Marks
378 * __struct gl_contextRec::NewState with the dirty stack flag.
381 _mesa_MultMatrixf( const GLfloat
*m
)
383 GET_CURRENT_CONTEXT(ctx
);
385 if (MESA_VERBOSE
& VERBOSE_API
)
387 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
388 m
[0], m
[4], m
[8], m
[12],
389 m
[1], m
[5], m
[9], m
[13],
390 m
[2], m
[6], m
[10], m
[14],
391 m
[3], m
[7], m
[11], m
[15]);
393 FLUSH_VERTICES(ctx
, 0);
394 _math_matrix_mul_floats( ctx
->CurrentStack
->Top
, m
);
395 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
400 * Multiply the current matrix with a rotation matrix.
402 * \param angle angle of rotation, in degrees.
403 * \param x rotation vector x coordinate.
404 * \param y rotation vector y coordinate.
405 * \param z rotation vector z coordinate.
409 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
410 * matrix in the current stack and the given parameters. Marks
411 * __struct gl_contextRec::NewState with the dirty stack flag.
414 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
416 GET_CURRENT_CONTEXT(ctx
);
418 FLUSH_VERTICES(ctx
, 0);
420 _math_matrix_rotate( ctx
->CurrentStack
->Top
, angle
, x
, y
, z
);
421 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
427 * Multiply the current matrix with a general scaling matrix.
429 * \param x x axis scale factor.
430 * \param y y axis scale factor.
431 * \param z z axis scale factor.
435 * Flushes the vertices and calls _math_matrix_scale() with the top-most
436 * matrix in the current stack and the given parameters. Marks
437 * __struct gl_contextRec::NewState with the dirty stack flag.
440 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
442 GET_CURRENT_CONTEXT(ctx
);
444 FLUSH_VERTICES(ctx
, 0);
445 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
446 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
451 * Multiply the current matrix with a translation matrix.
453 * \param x translation vector x coordinate.
454 * \param y translation vector y coordinate.
455 * \param z translation vector z coordinate.
457 * \sa glTranslatef().
459 * Flushes the vertices and calls _math_matrix_translate() with the top-most
460 * matrix in the current stack and the given parameters. Marks
461 * __struct gl_contextRec::NewState with the dirty stack flag.
464 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
466 GET_CURRENT_CONTEXT(ctx
);
468 FLUSH_VERTICES(ctx
, 0);
469 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
470 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
475 _mesa_LoadMatrixd( const GLdouble
*m
)
480 for (i
= 0; i
< 16; i
++)
481 f
[i
] = (GLfloat
) m
[i
];
482 _mesa_LoadMatrixf(f
);
486 _mesa_MultMatrixd( const GLdouble
*m
)
491 for (i
= 0; i
< 16; i
++)
492 f
[i
] = (GLfloat
) m
[i
];
493 _mesa_MultMatrixf( f
);
498 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
500 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
505 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
507 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
512 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
514 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
519 _mesa_LoadTransposeMatrixf( const GLfloat
*m
)
523 _math_transposef(tm
, m
);
524 _mesa_LoadMatrixf(tm
);
529 _mesa_LoadTransposeMatrixd( const GLdouble
*m
)
533 _math_transposefd(tm
, m
);
534 _mesa_LoadMatrixf(tm
);
539 _mesa_MultTransposeMatrixf( const GLfloat
*m
)
543 _math_transposef(tm
, m
);
544 _mesa_MultMatrixf(tm
);
549 _mesa_MultTransposeMatrixd( const GLdouble
*m
)
553 _math_transposefd(tm
, m
);
554 _mesa_MultMatrixf(tm
);
559 /**********************************************************************/
560 /** \name State management */
565 * Update the projection matrix stack.
567 * \param ctx GL context.
569 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
570 * stack, and recomputes user clip positions if necessary.
572 * \note This routine references __struct gl_contextRec::Tranform attribute
573 * values to compute userclip positions in clip space, but is only called on
574 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
575 * date across changes to the __struct gl_contextRec::Transform attributes.
578 update_projection( struct gl_context
*ctx
)
582 _math_matrix_analyse( ctx
->ProjectionMatrixStack
.Top
);
584 /* Recompute clip plane positions in clipspace. This is also done
585 * in _mesa_ClipPlane().
587 mask
= ctx
->Transform
.ClipPlanesEnabled
;
589 const int p
= u_bit_scan(&mask
);
591 _mesa_transform_vector( ctx
->Transform
._ClipUserPlane
[p
],
592 ctx
->Transform
.EyeUserPlane
[p
],
593 ctx
->ProjectionMatrixStack
.Top
->inv
);
599 * Calculate the combined modelview-projection matrix.
601 * \param ctx GL context.
603 * Multiplies the top matrices of the projection and model view stacks into
604 * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
605 * and analyzes the resulting matrix via _math_matrix_analyse().
608 calculate_model_project_matrix( struct gl_context
*ctx
)
610 _math_matrix_mul_matrix( &ctx
->_ModelProjectMatrix
,
611 ctx
->ProjectionMatrixStack
.Top
,
612 ctx
->ModelviewMatrixStack
.Top
);
614 _math_matrix_analyse( &ctx
->_ModelProjectMatrix
);
619 * Updates the combined modelview-projection matrix.
621 * \param ctx GL context.
622 * \param new_state new state bit mask.
624 * If there is a new model view matrix then analyzes it. If there is a new
625 * projection matrix, updates it. Finally calls
626 * calculate_model_project_matrix() to recalculate the modelview-projection
629 void _mesa_update_modelview_project( struct gl_context
*ctx
, GLuint new_state
)
631 if (new_state
& _NEW_MODELVIEW
)
632 _math_matrix_analyse( ctx
->ModelviewMatrixStack
.Top
);
634 if (new_state
& _NEW_PROJECTION
)
635 update_projection( ctx
);
637 /* Keep ModelviewProject up to date always to allow tnl
638 * implementations that go model->clip even when eye is required.
640 calculate_model_project_matrix(ctx
);
646 /**********************************************************************/
647 /** Matrix stack initialization */
652 * Initialize a matrix stack.
654 * \param stack matrix stack.
655 * \param maxDepth maximum stack depth.
656 * \param dirtyFlag dirty flag.
658 * Allocates an array of \p maxDepth elements for the matrix stack and calls
659 * _math_matrix_ctr() for each element to initialize it.
662 init_matrix_stack(struct gl_matrix_stack
*stack
,
663 GLuint maxDepth
, GLuint dirtyFlag
)
666 stack
->MaxDepth
= maxDepth
;
667 stack
->DirtyFlag
= dirtyFlag
;
668 /* The stack will be dynamically resized at glPushMatrix() time */
669 stack
->Stack
= calloc(1, sizeof(GLmatrix
));
670 stack
->StackSize
= 1;
671 _math_matrix_ctr(&stack
->Stack
[0]);
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;