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 if (memcmp(m
, ctx
->CurrentStack
->Top
->m
, 16 * sizeof(GLfloat
)) != 0) {
360 FLUSH_VERTICES(ctx
, 0);
361 _math_matrix_loadf( ctx
->CurrentStack
->Top
, m
);
362 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
368 * Multiply the current matrix with a given matrix.
372 * \sa glMultMatrixf().
374 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
375 * matrix in the current stack and the given matrix. Marks
376 * __struct gl_contextRec::NewState with the dirty stack flag.
379 _mesa_MultMatrixf( const GLfloat
*m
)
381 GET_CURRENT_CONTEXT(ctx
);
383 if (MESA_VERBOSE
& VERBOSE_API
)
385 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
386 m
[0], m
[4], m
[8], m
[12],
387 m
[1], m
[5], m
[9], m
[13],
388 m
[2], m
[6], m
[10], m
[14],
389 m
[3], m
[7], m
[11], m
[15]);
391 FLUSH_VERTICES(ctx
, 0);
392 _math_matrix_mul_floats( ctx
->CurrentStack
->Top
, m
);
393 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
398 * Multiply the current matrix with a rotation matrix.
400 * \param angle angle of rotation, in degrees.
401 * \param x rotation vector x coordinate.
402 * \param y rotation vector y coordinate.
403 * \param z rotation vector z coordinate.
407 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
408 * matrix in the current stack and the given parameters. Marks
409 * __struct gl_contextRec::NewState with the dirty stack flag.
412 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
414 GET_CURRENT_CONTEXT(ctx
);
416 FLUSH_VERTICES(ctx
, 0);
418 _math_matrix_rotate( ctx
->CurrentStack
->Top
, angle
, x
, y
, z
);
419 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
425 * Multiply the current matrix with a general scaling matrix.
427 * \param x x axis scale factor.
428 * \param y y axis scale factor.
429 * \param z z axis scale factor.
433 * Flushes the vertices and calls _math_matrix_scale() with the top-most
434 * matrix in the current stack and the given parameters. Marks
435 * __struct gl_contextRec::NewState with the dirty stack flag.
438 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
440 GET_CURRENT_CONTEXT(ctx
);
442 FLUSH_VERTICES(ctx
, 0);
443 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
444 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
449 * Multiply the current matrix with a translation matrix.
451 * \param x translation vector x coordinate.
452 * \param y translation vector y coordinate.
453 * \param z translation vector z coordinate.
455 * \sa glTranslatef().
457 * Flushes the vertices and calls _math_matrix_translate() with the top-most
458 * matrix in the current stack and the given parameters. Marks
459 * __struct gl_contextRec::NewState with the dirty stack flag.
462 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
464 GET_CURRENT_CONTEXT(ctx
);
466 FLUSH_VERTICES(ctx
, 0);
467 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
468 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
473 _mesa_LoadMatrixd( const GLdouble
*m
)
478 for (i
= 0; i
< 16; i
++)
479 f
[i
] = (GLfloat
) m
[i
];
480 _mesa_LoadMatrixf(f
);
484 _mesa_MultMatrixd( const GLdouble
*m
)
489 for (i
= 0; i
< 16; i
++)
490 f
[i
] = (GLfloat
) m
[i
];
491 _mesa_MultMatrixf( f
);
496 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
498 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
503 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
505 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
510 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
512 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
517 _mesa_LoadTransposeMatrixf( const GLfloat
*m
)
521 _math_transposef(tm
, m
);
522 _mesa_LoadMatrixf(tm
);
527 _mesa_LoadTransposeMatrixd( const GLdouble
*m
)
531 _math_transposefd(tm
, m
);
532 _mesa_LoadMatrixf(tm
);
537 _mesa_MultTransposeMatrixf( const GLfloat
*m
)
541 _math_transposef(tm
, m
);
542 _mesa_MultMatrixf(tm
);
547 _mesa_MultTransposeMatrixd( const GLdouble
*m
)
551 _math_transposefd(tm
, m
);
552 _mesa_MultMatrixf(tm
);
557 /**********************************************************************/
558 /** \name State management */
563 * Update the projection matrix stack.
565 * \param ctx GL context.
567 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
568 * stack, and recomputes user clip positions if necessary.
570 * \note This routine references __struct gl_contextRec::Tranform attribute
571 * values to compute userclip positions in clip space, but is only called on
572 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
573 * date across changes to the __struct gl_contextRec::Transform attributes.
576 update_projection( struct gl_context
*ctx
)
580 _math_matrix_analyse( ctx
->ProjectionMatrixStack
.Top
);
582 /* Recompute clip plane positions in clipspace. This is also done
583 * in _mesa_ClipPlane().
585 mask
= ctx
->Transform
.ClipPlanesEnabled
;
587 const int p
= u_bit_scan(&mask
);
589 _mesa_transform_vector( ctx
->Transform
._ClipUserPlane
[p
],
590 ctx
->Transform
.EyeUserPlane
[p
],
591 ctx
->ProjectionMatrixStack
.Top
->inv
);
597 * Calculate the combined modelview-projection matrix.
599 * \param ctx GL context.
601 * Multiplies the top matrices of the projection and model view stacks into
602 * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
603 * and analyzes the resulting matrix via _math_matrix_analyse().
606 calculate_model_project_matrix( struct gl_context
*ctx
)
608 _math_matrix_mul_matrix( &ctx
->_ModelProjectMatrix
,
609 ctx
->ProjectionMatrixStack
.Top
,
610 ctx
->ModelviewMatrixStack
.Top
);
612 _math_matrix_analyse( &ctx
->_ModelProjectMatrix
);
617 * Updates the combined modelview-projection matrix.
619 * \param ctx GL context.
620 * \param new_state new state bit mask.
622 * If there is a new model view matrix then analyzes it. If there is a new
623 * projection matrix, updates it. Finally calls
624 * calculate_model_project_matrix() to recalculate the modelview-projection
627 void _mesa_update_modelview_project( struct gl_context
*ctx
, GLuint new_state
)
629 if (new_state
& _NEW_MODELVIEW
)
630 _math_matrix_analyse( ctx
->ModelviewMatrixStack
.Top
);
632 if (new_state
& _NEW_PROJECTION
)
633 update_projection( ctx
);
635 /* Keep ModelviewProject up to date always to allow tnl
636 * implementations that go model->clip even when eye is required.
638 calculate_model_project_matrix(ctx
);
644 /**********************************************************************/
645 /** Matrix stack initialization */
650 * Initialize a matrix stack.
652 * \param stack matrix stack.
653 * \param maxDepth maximum stack depth.
654 * \param dirtyFlag dirty flag.
656 * Allocates an array of \p maxDepth elements for the matrix stack and calls
657 * _math_matrix_ctr() for each element to initialize it.
660 init_matrix_stack( struct gl_matrix_stack
*stack
,
661 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 for (i
= 0; i
< stack
->StackSize
; i
++) {
672 _math_matrix_ctr(&stack
->Stack
[i
]);
674 stack
->Top
= stack
->Stack
;
680 * \param stack matrix stack.
682 * Calls _math_matrix_dtr() for each element of the matrix stack and
686 free_matrix_stack( struct gl_matrix_stack
*stack
)
689 for (i
= 0; i
< stack
->StackSize
; i
++) {
690 _math_matrix_dtr(&stack
->Stack
[i
]);
693 stack
->Stack
= stack
->Top
= NULL
;
694 stack
->StackSize
= 0;
700 /**********************************************************************/
701 /** \name Initialization */
706 * Initialize the context matrix data.
708 * \param ctx GL context.
710 * Initializes each of the matrix stacks and the combined modelview-projection
713 void _mesa_init_matrix( struct gl_context
* ctx
)
717 /* Initialize matrix stacks */
718 init_matrix_stack(&ctx
->ModelviewMatrixStack
, MAX_MODELVIEW_STACK_DEPTH
,
720 init_matrix_stack(&ctx
->ProjectionMatrixStack
, MAX_PROJECTION_STACK_DEPTH
,
722 for (i
= 0; i
< ARRAY_SIZE(ctx
->TextureMatrixStack
); i
++)
723 init_matrix_stack(&ctx
->TextureMatrixStack
[i
], MAX_TEXTURE_STACK_DEPTH
,
724 _NEW_TEXTURE_MATRIX
);
725 for (i
= 0; i
< ARRAY_SIZE(ctx
->ProgramMatrixStack
); i
++)
726 init_matrix_stack(&ctx
->ProgramMatrixStack
[i
],
727 MAX_PROGRAM_MATRIX_STACK_DEPTH
, _NEW_TRACK_MATRIX
);
728 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
730 /* Init combined Modelview*Projection matrix */
731 _math_matrix_ctr( &ctx
->_ModelProjectMatrix
);
736 * Free the context matrix data.
738 * \param ctx GL context.
740 * Frees each of the matrix stacks and the combined modelview-projection
743 void _mesa_free_matrix_data( struct gl_context
*ctx
)
747 free_matrix_stack(&ctx
->ModelviewMatrixStack
);
748 free_matrix_stack(&ctx
->ProjectionMatrixStack
);
749 for (i
= 0; i
< ARRAY_SIZE(ctx
->TextureMatrixStack
); i
++)
750 free_matrix_stack(&ctx
->TextureMatrixStack
[i
]);
751 for (i
= 0; i
< ARRAY_SIZE(ctx
->ProgramMatrixStack
); i
++)
752 free_matrix_stack(&ctx
->ProgramMatrixStack
[i
]);
753 /* combined Modelview*Projection matrix */
754 _math_matrix_dtr( &ctx
->_ModelProjectMatrix
);
760 * Initialize the context transform attribute group.
762 * \param ctx GL context.
764 * \todo Move this to a new file with other 'transform' routines.
766 void _mesa_init_transform( struct gl_context
*ctx
)
770 /* Transformation group */
771 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
772 ctx
->Transform
.Normalize
= GL_FALSE
;
773 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
774 ctx
->Transform
.RasterPositionUnclipped
= GL_FALSE
;
775 for (i
=0;i
<ctx
->Const
.MaxClipPlanes
;i
++) {
776 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
778 ctx
->Transform
.ClipPlanesEnabled
= 0;