2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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
40 * Apply a perspective projection matrix.
42 * \param left left clipping plane coordinate.
43 * \param right right clipping plane coordinate.
44 * \param bottom bottom clipping plane coordinate.
45 * \param top top clipping plane coordinate.
46 * \param nearval distance to the near clipping plane.
47 * \param farval distance to the far clipping plane.
51 * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with
52 * the top matrix of the current matrix stack and sets
53 * __struct gl_contextRec::NewState.
56 _mesa_Frustum( GLdouble left
, GLdouble right
,
57 GLdouble bottom
, GLdouble top
,
58 GLdouble nearval
, GLdouble farval
)
60 GET_CURRENT_CONTEXT(ctx
);
61 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
69 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFrustum" );
73 _math_matrix_frustum( ctx
->CurrentStack
->Top
,
74 (GLfloat
) left
, (GLfloat
) right
,
75 (GLfloat
) bottom
, (GLfloat
) top
,
76 (GLfloat
) nearval
, (GLfloat
) farval
);
77 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
82 * Apply an orthographic projection matrix.
84 * \param left left clipping plane coordinate.
85 * \param right right clipping plane coordinate.
86 * \param bottom bottom clipping plane coordinate.
87 * \param top top clipping plane coordinate.
88 * \param nearval distance to the near clipping plane.
89 * \param farval distance to the far clipping plane.
93 * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
94 * the top matrix of the current matrix stack and sets
95 * __struct gl_contextRec::NewState.
98 _mesa_Ortho( GLdouble left
, GLdouble right
,
99 GLdouble bottom
, GLdouble top
,
100 GLdouble nearval
, GLdouble farval
)
102 GET_CURRENT_CONTEXT(ctx
);
103 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
105 if (MESA_VERBOSE
& VERBOSE_API
)
106 _mesa_debug(ctx
, "glOrtho(%f, %f, %f, %f, %f, %f)\n",
107 left
, right
, bottom
, top
, nearval
, farval
);
113 _mesa_error( ctx
, GL_INVALID_VALUE
, "glOrtho" );
117 _math_matrix_ortho( ctx
->CurrentStack
->Top
,
118 (GLfloat
) left
, (GLfloat
) right
,
119 (GLfloat
) bottom
, (GLfloat
) top
,
120 (GLfloat
) nearval
, (GLfloat
) farval
);
121 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
126 * Set the current matrix stack.
128 * \param mode matrix stack.
130 * \sa glMatrixMode().
132 * Flushes the vertices, validates the parameter and updates
133 * __struct gl_contextRec::CurrentStack and gl_transform_attrib::MatrixMode
134 * with the specified matrix stack.
137 _mesa_MatrixMode( GLenum mode
)
139 GET_CURRENT_CONTEXT(ctx
);
140 ASSERT_OUTSIDE_BEGIN_END(ctx
);
142 if (ctx
->Transform
.MatrixMode
== mode
&& mode
!= GL_TEXTURE
)
144 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
);
148 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
151 ctx
->CurrentStack
= &ctx
->ProjectionMatrixStack
;
154 ctx
->CurrentStack
= &ctx
->TextureMatrixStack
;
157 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode(mode)" );
161 ctx
->Transform
.MatrixMode
= mode
;
166 * Push the current matrix stack.
168 * \sa glPushMatrix().
170 * Verifies the current matrix stack is not full, and duplicates the top-most
171 * matrix in the stack.
172 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
175 _mesa_PushMatrix( void )
177 GET_CURRENT_CONTEXT(ctx
);
178 struct gl_matrix_stack
*stack
= ctx
->CurrentStack
;
179 ASSERT_OUTSIDE_BEGIN_END(ctx
);
181 if (MESA_VERBOSE
&VERBOSE_API
)
182 _mesa_debug(ctx
, "glPushMatrix %s\n",
183 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
185 if (stack
->Depth
+ 1 >= stack
->MaxDepth
) {
186 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
187 _mesa_error(ctx
, GL_STACK_OVERFLOW
,
188 "glPushMatrix(mode=GL_TEXTURE)");
191 _mesa_error(ctx
, GL_STACK_OVERFLOW
, "glPushMatrix(mode=%s)",
192 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
196 _math_matrix_copy( &stack
->Stack
[stack
->Depth
+ 1],
197 &stack
->Stack
[stack
->Depth
] );
199 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
200 ctx
->NewState
|= stack
->DirtyFlag
;
205 * Pop the current matrix stack.
209 * Flushes the vertices, verifies the current matrix stack is not empty, and
210 * moves the stack head down.
211 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
214 _mesa_PopMatrix( void )
216 GET_CURRENT_CONTEXT(ctx
);
217 struct gl_matrix_stack
*stack
= ctx
->CurrentStack
;
218 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
220 if (MESA_VERBOSE
&VERBOSE_API
)
221 _mesa_debug(ctx
, "glPopMatrix %s\n",
222 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
224 if (stack
->Depth
== 0) {
225 if (ctx
->Transform
.MatrixMode
== GL_TEXTURE
) {
226 _mesa_error(ctx
, GL_STACK_UNDERFLOW
,
227 "glPopMatrix(mode=GL_TEXTURE)");
230 _mesa_error(ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix(mode=%s)",
231 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
236 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
237 ctx
->NewState
|= stack
->DirtyFlag
;
242 * Replace the current matrix with the identity matrix.
244 * \sa glLoadIdentity().
246 * Flushes the vertices and calls _math_matrix_set_identity() with the
247 * top-most matrix in the current stack.
248 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
251 _mesa_LoadIdentity( void )
253 GET_CURRENT_CONTEXT(ctx
);
254 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
256 if (MESA_VERBOSE
& VERBOSE_API
)
257 _mesa_debug(ctx
, "glLoadIdentity()\n");
259 _math_matrix_set_identity( ctx
->CurrentStack
->Top
);
260 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
265 * Replace the current matrix with a given matrix.
269 * \sa glLoadMatrixf().
271 * Flushes the vertices and calls _math_matrix_loadf() with the top-most
272 * matrix in the current stack and the given matrix.
273 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
276 _mesa_LoadMatrixf( const GLfloat
*m
)
278 GET_CURRENT_CONTEXT(ctx
);
280 if (MESA_VERBOSE
& VERBOSE_API
)
282 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
283 m
[0], m
[4], m
[8], m
[12],
284 m
[1], m
[5], m
[9], m
[13],
285 m
[2], m
[6], m
[10], m
[14],
286 m
[3], m
[7], m
[11], m
[15]);
288 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
289 _math_matrix_loadf( ctx
->CurrentStack
->Top
, m
);
290 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
295 * Multiply the current matrix with a given matrix.
299 * \sa glMultMatrixf().
301 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
302 * matrix in the current stack and the given matrix. Marks
303 * __struct gl_contextRec::NewState with the dirty stack flag.
306 _mesa_MultMatrixf( const GLfloat
*m
)
308 GET_CURRENT_CONTEXT(ctx
);
310 if (MESA_VERBOSE
& VERBOSE_API
)
312 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
313 m
[0], m
[4], m
[8], m
[12],
314 m
[1], m
[5], m
[9], m
[13],
315 m
[2], m
[6], m
[10], m
[14],
316 m
[3], m
[7], m
[11], m
[15]);
317 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
318 _math_matrix_mul_floats( ctx
->CurrentStack
->Top
, m
);
319 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
324 * Multiply the current matrix with a rotation matrix.
326 * \param angle angle of rotation, in degrees.
327 * \param x rotation vector x coordinate.
328 * \param y rotation vector y coordinate.
329 * \param z rotation vector z coordinate.
333 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
334 * matrix in the current stack and the given parameters. Marks
335 * __struct gl_contextRec::NewState with the dirty stack flag.
338 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
340 GET_CURRENT_CONTEXT(ctx
);
341 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
343 _math_matrix_rotate( ctx
->CurrentStack
->Top
, angle
, x
, y
, z
);
344 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
350 * Multiply the current matrix with a general scaling matrix.
352 * \param x x axis scale factor.
353 * \param y y axis scale factor.
354 * \param z z axis scale factor.
358 * Flushes the vertices and calls _math_matrix_scale() with the top-most
359 * matrix in the current stack and the given parameters. Marks
360 * __struct gl_contextRec::NewState with the dirty stack flag.
363 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
365 GET_CURRENT_CONTEXT(ctx
);
366 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
367 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
368 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
373 * Multiply the current matrix with a translation matrix.
375 * \param x translation vector x coordinate.
376 * \param y translation vector y coordinate.
377 * \param z translation vector z coordinate.
379 * \sa glTranslatef().
381 * Flushes the vertices and calls _math_matrix_translate() with the top-most
382 * matrix in the current stack and the given parameters. Marks
383 * __struct gl_contextRec::NewState with the dirty stack flag.
386 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
388 GET_CURRENT_CONTEXT(ctx
);
389 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
390 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
391 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
397 _mesa_LoadMatrixd( const GLdouble
*m
)
402 for (i
= 0; i
< 16; i
++)
403 f
[i
] = (GLfloat
) m
[i
];
404 _mesa_LoadMatrixf(f
);
408 _mesa_MultMatrixd( const GLdouble
*m
)
413 for (i
= 0; i
< 16; i
++)
414 f
[i
] = (GLfloat
) m
[i
];
415 _mesa_MultMatrixf( f
);
420 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
422 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
427 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
429 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
434 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
436 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
443 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
447 _math_transposef(tm
, m
);
448 _mesa_LoadMatrixf(tm
);
453 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
457 _math_transposefd(tm
, m
);
458 _mesa_LoadMatrixf(tm
);
463 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
467 _math_transposef(tm
, m
);
468 _mesa_MultMatrixf(tm
);
473 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
477 _math_transposefd(tm
, m
);
478 _mesa_MultMatrixf(tm
);
484 /**********************************************************************/
485 /** \name State management */
490 * Update the projection matrix stack.
492 * \param ctx GL context.
494 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
495 * stack, and recomputes user clip positions if necessary.
497 * \note This routine references __struct gl_contextRec::Tranform attribute
498 * values to compute userclip positions in clip space, but is only called on
499 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
500 * date across changes to the __struct gl_contextRec::Transform attributes.
503 update_projection( struct gl_context
*ctx
)
505 _math_matrix_analyse( ctx
->ProjectionMatrixStack
.Top
);
508 /* Recompute clip plane positions in clipspace. This is also done
509 * in _mesa_ClipPlane().
511 if (ctx
->Transform
.ClipPlanesEnabled
) {
513 for (p
= 0; p
< ctx
->Const
.MaxClipPlanes
; p
++) {
514 if (ctx
->Transform
.ClipPlanesEnabled
& (1 << p
)) {
515 _mesa_transform_vector( ctx
->Transform
._ClipUserPlane
[p
],
516 ctx
->Transform
.EyeUserPlane
[p
],
517 ctx
->ProjectionMatrixStack
.Top
->inv
);
526 * Calculate the combined modelview-projection matrix.
528 * \param ctx GL context.
530 * Multiplies the top matrices of the projection and model view stacks into
531 * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
532 * and analyzes the resulting matrix via _math_matrix_analyse().
535 calculate_model_project_matrix( struct gl_context
*ctx
)
537 _math_matrix_mul_matrix( &ctx
->_ModelProjectMatrix
,
538 ctx
->ProjectionMatrixStack
.Top
,
539 ctx
->ModelviewMatrixStack
.Top
);
541 _math_matrix_analyse( &ctx
->_ModelProjectMatrix
);
546 * Updates the combined modelview-projection matrix.
548 * \param ctx GL context.
549 * \param new_state new state bit mask.
551 * If there is a new model view matrix then analyzes it. If there is a new
552 * projection matrix, updates it. Finally calls
553 * calculate_model_project_matrix() to recalculate the modelview-projection
556 void _mesa_update_modelview_project( struct gl_context
*ctx
, GLuint new_state
)
558 if (new_state
& _NEW_MODELVIEW
) {
559 _math_matrix_analyse( ctx
->ModelviewMatrixStack
.Top
);
561 /* Bring cull position up to date.
563 TRANSFORM_POINT3( ctx
->Transform
.CullObjPos
,
564 ctx
->ModelviewMatrixStack
.Top
->inv
,
565 ctx
->Transform
.CullEyePos
);
569 if (new_state
& _NEW_PROJECTION
)
570 update_projection( ctx
);
572 /* Keep ModelviewProject up to date always to allow tnl
573 * implementations that go model->clip even when eye is required.
575 calculate_model_project_matrix(ctx
);
581 /**********************************************************************/
582 /** Matrix stack initialization */
587 * Initialize a matrix stack.
589 * \param stack matrix stack.
590 * \param maxDepth maximum stack depth.
591 * \param dirtyFlag dirty flag.
593 * Allocates an array of \p maxDepth elements for the matrix stack and calls
594 * _math_matrix_ctr() and _math_matrix_alloc_inv() for each element to
598 init_matrix_stack( struct gl_matrix_stack
*stack
,
599 GLuint maxDepth
, GLuint dirtyFlag
)
604 stack
->MaxDepth
= maxDepth
;
605 stack
->DirtyFlag
= dirtyFlag
;
607 stack
->Stack
= (GLmatrix
*) CALLOC(maxDepth
* sizeof(GLmatrix
));
608 for (i
= 0; i
< maxDepth
; i
++) {
609 _math_matrix_ctr(&stack
->Stack
[i
]);
610 _math_matrix_alloc_inv(&stack
->Stack
[i
]);
612 stack
->Top
= stack
->Stack
;
618 * \param stack matrix stack.
620 * Calls _math_matrix_dtr() for each element of the matrix stack and
624 free_matrix_stack( struct gl_matrix_stack
*stack
)
627 for (i
= 0; i
< stack
->MaxDepth
; i
++) {
628 _math_matrix_dtr(&stack
->Stack
[i
]);
631 stack
->Stack
= stack
->Top
= NULL
;
637 /**********************************************************************/
638 /** \name Initialization */
643 * Initialize the context matrix data.
645 * \param ctx GL context.
647 * Initializes each of the matrix stacks and the combined modelview-projection
650 void _mesa_init_matrix( struct gl_context
* ctx
)
652 /* Initialize matrix stacks */
653 init_matrix_stack(&ctx
->ModelviewMatrixStack
, MAX_MODELVIEW_STACK_DEPTH
,
655 init_matrix_stack(&ctx
->ProjectionMatrixStack
, MAX_PROJECTION_STACK_DEPTH
,
657 init_matrix_stack(&ctx
->TextureMatrixStack
, MAX_TEXTURE_STACK_DEPTH
,
658 _NEW_TEXTURE_MATRIX
);
659 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
661 /* Init combined Modelview*Projection matrix */
662 _math_matrix_ctr( &ctx
->_ModelProjectMatrix
);
667 * Free the context matrix data.
669 * \param ctx GL context.
671 * Frees each of the matrix stacks and the combined modelview-projection
674 void _mesa_free_matrix_data( struct gl_context
*ctx
)
676 free_matrix_stack(&ctx
->ModelviewMatrixStack
);
677 free_matrix_stack(&ctx
->ProjectionMatrixStack
);
678 free_matrix_stack(&ctx
->TextureMatrixStack
);
679 /* combined Modelview*Projection matrix */
680 _math_matrix_dtr( &ctx
->_ModelProjectMatrix
);
686 * Initialize the context transform attribute group.
688 * \param ctx GL context.
690 * \todo Move this to a new file with other 'transform' routines.
692 void _mesa_init_transform( struct gl_context
*ctx
)
696 /* Transformation group */
697 ctx
->Transform
.MatrixMode
= GL_MODELVIEW
;
698 ctx
->Transform
.Normalize
= GL_FALSE
;
699 ctx
->Transform
.RescaleNormals
= GL_FALSE
;
700 ctx
->Transform
.RasterPositionUnclipped
= GL_FALSE
;
701 for (i
=0;i
<ctx
->Const
.MaxClipPlanes
;i
++) {
702 ASSIGN_4V( ctx
->Transform
.EyeUserPlane
[i
], 0.0, 0.0, 0.0, 0.0 );
704 ctx
->Transform
.ClipPlanesEnabled
= 0;
706 ASSIGN_4V( ctx
->Transform
.CullObjPos
, 0.0, 0.0, 1.0, 0.0 );
707 ASSIGN_4V( ctx
->Transform
.CullEyePos
, 0.0, 0.0, 1.0, 0.0 );