mesa: Remove support for NV_vertex_program's tracked matrices.
[mesa.git] / src / mesa / main / matrix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
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.
24 */
25
26
27 /**
28 * \file matrix.c
29 * Matrix operations.
30 *
31 * \note
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
35 */
36
37
38 #include "glheader.h"
39 #include "imports.h"
40 #include "context.h"
41 #include "enums.h"
42 #include "macros.h"
43 #include "mfeatures.h"
44 #include "matrix.h"
45 #include "mtypes.h"
46 #include "math/m_matrix.h"
47
48
49 /**
50 * Apply a perspective projection matrix.
51 *
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.
58 *
59 * \sa glFrustum().
60 *
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.
64 */
65 void GLAPIENTRY
66 _mesa_Frustum( GLdouble left, GLdouble right,
67 GLdouble bottom, GLdouble top,
68 GLdouble nearval, GLdouble farval )
69 {
70 GET_CURRENT_CONTEXT(ctx);
71 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
72
73 if (nearval <= 0.0 ||
74 farval <= 0.0 ||
75 nearval == farval ||
76 left == right ||
77 top == bottom)
78 {
79 _mesa_error( ctx, GL_INVALID_VALUE, "glFrustum" );
80 return;
81 }
82
83 _math_matrix_frustum( ctx->CurrentStack->Top,
84 (GLfloat) left, (GLfloat) right,
85 (GLfloat) bottom, (GLfloat) top,
86 (GLfloat) nearval, (GLfloat) farval );
87 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
88 }
89
90
91 /**
92 * Apply an orthographic projection matrix.
93 *
94 * \param left left clipping plane coordinate.
95 * \param right right clipping plane coordinate.
96 * \param bottom bottom clipping plane coordinate.
97 * \param top top clipping plane coordinate.
98 * \param nearval distance to the near clipping plane.
99 * \param farval distance to the far clipping plane.
100 *
101 * \sa glOrtho().
102 *
103 * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
104 * the top matrix of the current matrix stack and sets
105 * __struct gl_contextRec::NewState.
106 */
107 void GLAPIENTRY
108 _mesa_Ortho( GLdouble left, GLdouble right,
109 GLdouble bottom, GLdouble top,
110 GLdouble nearval, GLdouble farval )
111 {
112 GET_CURRENT_CONTEXT(ctx);
113 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
114
115 if (MESA_VERBOSE & VERBOSE_API)
116 _mesa_debug(ctx, "glOrtho(%f, %f, %f, %f, %f, %f)\n",
117 left, right, bottom, top, nearval, farval);
118
119 if (left == right ||
120 bottom == top ||
121 nearval == farval)
122 {
123 _mesa_error( ctx, GL_INVALID_VALUE, "glOrtho" );
124 return;
125 }
126
127 _math_matrix_ortho( ctx->CurrentStack->Top,
128 (GLfloat) left, (GLfloat) right,
129 (GLfloat) bottom, (GLfloat) top,
130 (GLfloat) nearval, (GLfloat) farval );
131 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
132 }
133
134
135 /**
136 * Set the current matrix stack.
137 *
138 * \param mode matrix stack.
139 *
140 * \sa glMatrixMode().
141 *
142 * Flushes the vertices, validates the parameter and updates
143 * __struct gl_contextRec::CurrentStack and gl_transform_attrib::MatrixMode
144 * with the specified matrix stack.
145 */
146 void GLAPIENTRY
147 _mesa_MatrixMode( GLenum mode )
148 {
149 GET_CURRENT_CONTEXT(ctx);
150 ASSERT_OUTSIDE_BEGIN_END(ctx);
151
152 if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
153 return;
154 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
155
156 switch (mode) {
157 case GL_MODELVIEW:
158 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
159 break;
160 case GL_PROJECTION:
161 ctx->CurrentStack = &ctx->ProjectionMatrixStack;
162 break;
163 case GL_TEXTURE:
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.
170 */
171 #if 0
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);
176 return;
177 }
178 #endif
179 ASSERT(ctx->Texture.CurrentUnit < Elements(ctx->TextureMatrixStack));
180 ctx->CurrentStack = &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
181 break;
182 case GL_MATRIX0_ARB:
183 case GL_MATRIX1_ARB:
184 case GL_MATRIX2_ARB:
185 case GL_MATRIX3_ARB:
186 case GL_MATRIX4_ARB:
187 case GL_MATRIX5_ARB:
188 case GL_MATRIX6_ARB:
189 case GL_MATRIX7_ARB:
190 if (ctx->API == API_OPENGL
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);
197 return;
198 }
199 ctx->CurrentStack = &ctx->ProgramMatrixStack[m];
200 }
201 else {
202 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" );
203 return;
204 }
205 break;
206 default:
207 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" );
208 return;
209 }
210
211 ctx->Transform.MatrixMode = mode;
212 }
213
214
215 /**
216 * Push the current matrix stack.
217 *
218 * \sa glPushMatrix().
219 *
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.
223 */
224 void GLAPIENTRY
225 _mesa_PushMatrix( void )
226 {
227 GET_CURRENT_CONTEXT(ctx);
228 struct gl_matrix_stack *stack = ctx->CurrentStack;
229 ASSERT_OUTSIDE_BEGIN_END(ctx);
230
231 if (MESA_VERBOSE&VERBOSE_API)
232 _mesa_debug(ctx, "glPushMatrix %s\n",
233 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
234
235 if (stack->Depth + 1 >= stack->MaxDepth) {
236 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
237 _mesa_error(ctx, GL_STACK_OVERFLOW,
238 "glPushMatrix(mode=GL_TEXTURE, unit=%d)",
239 ctx->Texture.CurrentUnit);
240 }
241 else {
242 _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushMatrix(mode=%s)",
243 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
244 }
245 return;
246 }
247 _math_matrix_copy( &stack->Stack[stack->Depth + 1],
248 &stack->Stack[stack->Depth] );
249 stack->Depth++;
250 stack->Top = &(stack->Stack[stack->Depth]);
251 ctx->NewState |= stack->DirtyFlag;
252 }
253
254
255 /**
256 * Pop the current matrix stack.
257 *
258 * \sa glPopMatrix().
259 *
260 * Flushes the vertices, verifies the current matrix stack is not empty, and
261 * moves the stack head down.
262 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
263 */
264 void GLAPIENTRY
265 _mesa_PopMatrix( void )
266 {
267 GET_CURRENT_CONTEXT(ctx);
268 struct gl_matrix_stack *stack = ctx->CurrentStack;
269 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
270
271 if (MESA_VERBOSE&VERBOSE_API)
272 _mesa_debug(ctx, "glPopMatrix %s\n",
273 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
274
275 if (stack->Depth == 0) {
276 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
277 _mesa_error(ctx, GL_STACK_UNDERFLOW,
278 "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
279 ctx->Texture.CurrentUnit);
280 }
281 else {
282 _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)",
283 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
284 }
285 return;
286 }
287 stack->Depth--;
288 stack->Top = &(stack->Stack[stack->Depth]);
289 ctx->NewState |= stack->DirtyFlag;
290 }
291
292
293 /**
294 * Replace the current matrix with the identity matrix.
295 *
296 * \sa glLoadIdentity().
297 *
298 * Flushes the vertices and calls _math_matrix_set_identity() with the
299 * top-most matrix in the current stack.
300 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
301 */
302 void GLAPIENTRY
303 _mesa_LoadIdentity( void )
304 {
305 GET_CURRENT_CONTEXT(ctx);
306 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
307
308 if (MESA_VERBOSE & VERBOSE_API)
309 _mesa_debug(ctx, "glLoadIdentity()\n");
310
311 _math_matrix_set_identity( ctx->CurrentStack->Top );
312 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
313 }
314
315
316 /**
317 * Replace the current matrix with a given matrix.
318 *
319 * \param m matrix.
320 *
321 * \sa glLoadMatrixf().
322 *
323 * Flushes the vertices and calls _math_matrix_loadf() with the top-most
324 * matrix in the current stack and the given matrix.
325 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
326 */
327 void GLAPIENTRY
328 _mesa_LoadMatrixf( const GLfloat *m )
329 {
330 GET_CURRENT_CONTEXT(ctx);
331 if (!m) return;
332 if (MESA_VERBOSE & VERBOSE_API)
333 _mesa_debug(ctx,
334 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
335 m[0], m[4], m[8], m[12],
336 m[1], m[5], m[9], m[13],
337 m[2], m[6], m[10], m[14],
338 m[3], m[7], m[11], m[15]);
339
340 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
341 _math_matrix_loadf( ctx->CurrentStack->Top, m );
342 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
343 }
344
345
346 /**
347 * Multiply the current matrix with a given matrix.
348 *
349 * \param m matrix.
350 *
351 * \sa glMultMatrixf().
352 *
353 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
354 * matrix in the current stack and the given matrix. Marks
355 * __struct gl_contextRec::NewState with the dirty stack flag.
356 */
357 void GLAPIENTRY
358 _mesa_MultMatrixf( const GLfloat *m )
359 {
360 GET_CURRENT_CONTEXT(ctx);
361 if (!m) return;
362 if (MESA_VERBOSE & VERBOSE_API)
363 _mesa_debug(ctx,
364 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
365 m[0], m[4], m[8], m[12],
366 m[1], m[5], m[9], m[13],
367 m[2], m[6], m[10], m[14],
368 m[3], m[7], m[11], m[15]);
369 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
370 _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
371 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
372 }
373
374
375 /**
376 * Multiply the current matrix with a rotation matrix.
377 *
378 * \param angle angle of rotation, in degrees.
379 * \param x rotation vector x coordinate.
380 * \param y rotation vector y coordinate.
381 * \param z rotation vector z coordinate.
382 *
383 * \sa glRotatef().
384 *
385 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
386 * matrix in the current stack and the given parameters. Marks
387 * __struct gl_contextRec::NewState with the dirty stack flag.
388 */
389 void GLAPIENTRY
390 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
391 {
392 GET_CURRENT_CONTEXT(ctx);
393 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
394 if (angle != 0.0F) {
395 _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
396 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
397 }
398 }
399
400
401 /**
402 * Multiply the current matrix with a general scaling matrix.
403 *
404 * \param x x axis scale factor.
405 * \param y y axis scale factor.
406 * \param z z axis scale factor.
407 *
408 * \sa glScalef().
409 *
410 * Flushes the vertices and calls _math_matrix_scale() with the top-most
411 * matrix in the current stack and the given parameters. Marks
412 * __struct gl_contextRec::NewState with the dirty stack flag.
413 */
414 void GLAPIENTRY
415 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
416 {
417 GET_CURRENT_CONTEXT(ctx);
418 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
419 _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
420 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
421 }
422
423
424 /**
425 * Multiply the current matrix with a translation matrix.
426 *
427 * \param x translation vector x coordinate.
428 * \param y translation vector y coordinate.
429 * \param z translation vector z coordinate.
430 *
431 * \sa glTranslatef().
432 *
433 * Flushes the vertices and calls _math_matrix_translate() 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.
436 */
437 void GLAPIENTRY
438 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
439 {
440 GET_CURRENT_CONTEXT(ctx);
441 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
442 _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
443 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
444 }
445
446
447 #if _HAVE_FULL_GL
448 void GLAPIENTRY
449 _mesa_LoadMatrixd( const GLdouble *m )
450 {
451 GLint i;
452 GLfloat f[16];
453 if (!m) return;
454 for (i = 0; i < 16; i++)
455 f[i] = (GLfloat) m[i];
456 _mesa_LoadMatrixf(f);
457 }
458
459 void GLAPIENTRY
460 _mesa_MultMatrixd( const GLdouble *m )
461 {
462 GLint i;
463 GLfloat f[16];
464 if (!m) return;
465 for (i = 0; i < 16; i++)
466 f[i] = (GLfloat) m[i];
467 _mesa_MultMatrixf( f );
468 }
469
470
471 void GLAPIENTRY
472 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
473 {
474 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
475 }
476
477
478 void GLAPIENTRY
479 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
480 {
481 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
482 }
483
484
485 void GLAPIENTRY
486 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
487 {
488 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
489 }
490 #endif
491
492
493 #if _HAVE_FULL_GL
494 void GLAPIENTRY
495 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
496 {
497 GLfloat tm[16];
498 if (!m) return;
499 _math_transposef(tm, m);
500 _mesa_LoadMatrixf(tm);
501 }
502
503
504 void GLAPIENTRY
505 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
506 {
507 GLfloat tm[16];
508 if (!m) return;
509 _math_transposefd(tm, m);
510 _mesa_LoadMatrixf(tm);
511 }
512
513
514 void GLAPIENTRY
515 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
516 {
517 GLfloat tm[16];
518 if (!m) return;
519 _math_transposef(tm, m);
520 _mesa_MultMatrixf(tm);
521 }
522
523
524 void GLAPIENTRY
525 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
526 {
527 GLfloat tm[16];
528 if (!m) return;
529 _math_transposefd(tm, m);
530 _mesa_MultMatrixf(tm);
531 }
532 #endif
533
534
535
536 /**********************************************************************/
537 /** \name State management */
538 /*@{*/
539
540
541 /**
542 * Update the projection matrix stack.
543 *
544 * \param ctx GL context.
545 *
546 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
547 * stack, and recomputes user clip positions if necessary.
548 *
549 * \note This routine references __struct gl_contextRec::Tranform attribute
550 * values to compute userclip positions in clip space, but is only called on
551 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
552 * date across changes to the __struct gl_contextRec::Transform attributes.
553 */
554 static void
555 update_projection( struct gl_context *ctx )
556 {
557 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
558
559 /* Recompute clip plane positions in clipspace. This is also done
560 * in _mesa_ClipPlane().
561 */
562 if (ctx->Transform.ClipPlanesEnabled) {
563 GLuint p;
564 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
565 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
566 _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
567 ctx->Transform.EyeUserPlane[p],
568 ctx->ProjectionMatrixStack.Top->inv );
569 }
570 }
571 }
572 }
573
574
575 /**
576 * Calculate the combined modelview-projection matrix.
577 *
578 * \param ctx GL context.
579 *
580 * Multiplies the top matrices of the projection and model view stacks into
581 * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
582 * and analyzes the resulting matrix via _math_matrix_analyse().
583 */
584 static void
585 calculate_model_project_matrix( struct gl_context *ctx )
586 {
587 _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
588 ctx->ProjectionMatrixStack.Top,
589 ctx->ModelviewMatrixStack.Top );
590
591 _math_matrix_analyse( &ctx->_ModelProjectMatrix );
592 }
593
594
595 /**
596 * Updates the combined modelview-projection matrix.
597 *
598 * \param ctx GL context.
599 * \param new_state new state bit mask.
600 *
601 * If there is a new model view matrix then analyzes it. If there is a new
602 * projection matrix, updates it. Finally calls
603 * calculate_model_project_matrix() to recalculate the modelview-projection
604 * matrix.
605 */
606 void _mesa_update_modelview_project( struct gl_context *ctx, GLuint new_state )
607 {
608 if (new_state & _NEW_MODELVIEW) {
609 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
610
611 /* Bring cull position up to date.
612 */
613 TRANSFORM_POINT3( ctx->Transform.CullObjPos,
614 ctx->ModelviewMatrixStack.Top->inv,
615 ctx->Transform.CullEyePos );
616 }
617
618
619 if (new_state & _NEW_PROJECTION)
620 update_projection( ctx );
621
622 /* Keep ModelviewProject up to date always to allow tnl
623 * implementations that go model->clip even when eye is required.
624 */
625 calculate_model_project_matrix(ctx);
626 }
627
628 /*@}*/
629
630
631 /**********************************************************************/
632 /** Matrix stack initialization */
633 /*@{*/
634
635
636 /**
637 * Initialize a matrix stack.
638 *
639 * \param stack matrix stack.
640 * \param maxDepth maximum stack depth.
641 * \param dirtyFlag dirty flag.
642 *
643 * Allocates an array of \p maxDepth elements for the matrix stack and calls
644 * _math_matrix_ctr() for each element to initialize it.
645 */
646 static void
647 init_matrix_stack( struct gl_matrix_stack *stack,
648 GLuint maxDepth, GLuint dirtyFlag )
649 {
650 GLuint i;
651
652 stack->Depth = 0;
653 stack->MaxDepth = maxDepth;
654 stack->DirtyFlag = dirtyFlag;
655 /* The stack */
656 stack->Stack = calloc(maxDepth, sizeof(GLmatrix));
657 for (i = 0; i < maxDepth; i++) {
658 _math_matrix_ctr(&stack->Stack[i]);
659 }
660 stack->Top = stack->Stack;
661 }
662
663 /**
664 * Free matrix stack.
665 *
666 * \param stack matrix stack.
667 *
668 * Calls _math_matrix_dtr() for each element of the matrix stack and
669 * frees the array.
670 */
671 static void
672 free_matrix_stack( struct gl_matrix_stack *stack )
673 {
674 GLuint i;
675 for (i = 0; i < stack->MaxDepth; i++) {
676 _math_matrix_dtr(&stack->Stack[i]);
677 }
678 free(stack->Stack);
679 stack->Stack = stack->Top = NULL;
680 }
681
682 /*@}*/
683
684
685 /**********************************************************************/
686 /** \name Initialization */
687 /*@{*/
688
689
690 /**
691 * Initialize the context matrix data.
692 *
693 * \param ctx GL context.
694 *
695 * Initializes each of the matrix stacks and the combined modelview-projection
696 * matrix.
697 */
698 void _mesa_init_matrix( struct gl_context * ctx )
699 {
700 GLint i;
701
702 /* Initialize matrix stacks */
703 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
704 _NEW_MODELVIEW);
705 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
706 _NEW_PROJECTION);
707 for (i = 0; i < Elements(ctx->TextureMatrixStack); i++)
708 init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
709 _NEW_TEXTURE_MATRIX);
710 for (i = 0; i < Elements(ctx->ProgramMatrixStack); i++)
711 init_matrix_stack(&ctx->ProgramMatrixStack[i],
712 MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
713 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
714
715 /* Init combined Modelview*Projection matrix */
716 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
717 }
718
719
720 /**
721 * Free the context matrix data.
722 *
723 * \param ctx GL context.
724 *
725 * Frees each of the matrix stacks and the combined modelview-projection
726 * matrix.
727 */
728 void _mesa_free_matrix_data( struct gl_context *ctx )
729 {
730 GLint i;
731
732 free_matrix_stack(&ctx->ModelviewMatrixStack);
733 free_matrix_stack(&ctx->ProjectionMatrixStack);
734 for (i = 0; i < Elements(ctx->TextureMatrixStack); i++)
735 free_matrix_stack(&ctx->TextureMatrixStack[i]);
736 for (i = 0; i < Elements(ctx->ProgramMatrixStack); i++)
737 free_matrix_stack(&ctx->ProgramMatrixStack[i]);
738 /* combined Modelview*Projection matrix */
739 _math_matrix_dtr( &ctx->_ModelProjectMatrix );
740
741 }
742
743
744 /**
745 * Initialize the context transform attribute group.
746 *
747 * \param ctx GL context.
748 *
749 * \todo Move this to a new file with other 'transform' routines.
750 */
751 void _mesa_init_transform( struct gl_context *ctx )
752 {
753 GLint i;
754
755 /* Transformation group */
756 ctx->Transform.MatrixMode = GL_MODELVIEW;
757 ctx->Transform.Normalize = GL_FALSE;
758 ctx->Transform.RescaleNormals = GL_FALSE;
759 ctx->Transform.RasterPositionUnclipped = GL_FALSE;
760 for (i=0;i<ctx->Const.MaxClipPlanes;i++) {
761 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
762 }
763 ctx->Transform.ClipPlanesEnabled = 0;
764
765 ASSIGN_4V( ctx->Transform.CullObjPos, 0.0, 0.0, 1.0, 0.0 );
766 ASSIGN_4V( ctx->Transform.CullEyePos, 0.0, 0.0, 1.0, 0.0 );
767 }
768
769
770 /*@}*/