mesa: factor out enum -> matrix stack lookup
[mesa.git] / src / mesa / main / matrix.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
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 "matrix.h"
44 #include "mtypes.h"
45 #include "math/m_matrix.h"
46 #include "util/bitscan.h"
47
48
49 static struct gl_matrix_stack *
50 get_named_matrix_stack(struct gl_context *ctx, GLenum mode)
51 {
52 switch (mode) {
53 case GL_MODELVIEW:
54 return &ctx->ModelviewMatrixStack;
55 case GL_PROJECTION:
56 return &ctx->ProjectionMatrixStack;
57 case GL_TEXTURE:
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.
64 */
65 #if 0
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);
70 return;
71 }
72 #endif
73 assert(ctx->Texture.CurrentUnit < ARRAY_SIZE(ctx->TextureMatrixStack));
74 return &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
75 case GL_MATRIX0_ARB:
76 case GL_MATRIX1_ARB:
77 case GL_MATRIX2_ARB:
78 case GL_MATRIX3_ARB:
79 case GL_MATRIX4_ARB:
80 case GL_MATRIX5_ARB:
81 case GL_MATRIX6_ARB:
82 case GL_MATRIX7_ARB:
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];
89 }
90 /* fallthrough */
91 default:
92 return NULL;
93 }
94 }
95
96
97 /**
98 * Apply a perspective projection matrix.
99 *
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.
106 *
107 * \sa glFrustum().
108 *
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.
112 */
113 void GLAPIENTRY
114 _mesa_Frustum( GLdouble left, GLdouble right,
115 GLdouble bottom, GLdouble top,
116 GLdouble nearval, GLdouble farval )
117 {
118 GET_CURRENT_CONTEXT(ctx);
119
120 FLUSH_VERTICES(ctx, 0);
121
122 if (nearval <= 0.0 ||
123 farval <= 0.0 ||
124 nearval == farval ||
125 left == right ||
126 top == bottom)
127 {
128 _mesa_error( ctx, GL_INVALID_VALUE, "glFrustum" );
129 return;
130 }
131
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;
137 }
138
139
140 /**
141 * Apply an orthographic projection matrix.
142 *
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.
149 *
150 * \sa glOrtho().
151 *
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.
155 */
156 void GLAPIENTRY
157 _mesa_Ortho( GLdouble left, GLdouble right,
158 GLdouble bottom, GLdouble top,
159 GLdouble nearval, GLdouble farval )
160 {
161 GET_CURRENT_CONTEXT(ctx);
162
163 FLUSH_VERTICES(ctx, 0);
164
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);
168
169 if (left == right ||
170 bottom == top ||
171 nearval == farval)
172 {
173 _mesa_error( ctx, GL_INVALID_VALUE, "glOrtho" );
174 return;
175 }
176
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;
182 }
183
184
185 /**
186 * Set the current matrix stack.
187 *
188 * \param mode matrix stack.
189 *
190 * \sa glMatrixMode().
191 *
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.
195 */
196 void GLAPIENTRY
197 _mesa_MatrixMode( GLenum mode )
198 {
199 struct gl_matrix_stack * stack;
200 GET_CURRENT_CONTEXT(ctx);
201
202 if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
203 return;
204
205 stack = get_named_matrix_stack(ctx, mode);
206
207 if (stack) {
208 ctx->CurrentStack = stack;
209 ctx->Transform.MatrixMode = mode;
210 }
211 else {
212 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" );
213 }
214 }
215
216
217 /**
218 * Push the current matrix stack.
219 *
220 * \sa glPushMatrix().
221 *
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.
225 */
226 void GLAPIENTRY
227 _mesa_PushMatrix( void )
228 {
229 GET_CURRENT_CONTEXT(ctx);
230 struct gl_matrix_stack *stack = ctx->CurrentStack;
231
232 if (MESA_VERBOSE&VERBOSE_API)
233 _mesa_debug(ctx, "glPushMatrix %s\n",
234 _mesa_enum_to_string(ctx->Transform.MatrixMode));
235
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);
241 }
242 else {
243 _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushMatrix(mode=%s)",
244 _mesa_enum_to_string(ctx->Transform.MatrixMode));
245 }
246 return;
247 }
248 if (stack->Depth + 1 >= stack->StackSize) {
249 unsigned new_stack_size = stack->StackSize * 2;
250 unsigned i;
251 GLmatrix *new_stack = realloc(stack->Stack,
252 sizeof(*new_stack) * new_stack_size);
253
254 if (!new_stack) {
255 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushMatrix()");
256 return;
257 }
258
259 for (i = stack->StackSize; i < new_stack_size; i++)
260 _math_matrix_ctr(&new_stack[i]);
261
262 stack->Stack = new_stack;
263 stack->StackSize = new_stack_size;
264 }
265
266 _math_matrix_copy( &stack->Stack[stack->Depth + 1],
267 &stack->Stack[stack->Depth] );
268 stack->Depth++;
269 stack->Top = &(stack->Stack[stack->Depth]);
270 ctx->NewState |= stack->DirtyFlag;
271 }
272
273
274 /**
275 * Pop the current matrix stack.
276 *
277 * \sa glPopMatrix().
278 *
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.
282 */
283 void GLAPIENTRY
284 _mesa_PopMatrix( void )
285 {
286 GET_CURRENT_CONTEXT(ctx);
287 struct gl_matrix_stack *stack = ctx->CurrentStack;
288
289 FLUSH_VERTICES(ctx, 0);
290
291 if (MESA_VERBOSE&VERBOSE_API)
292 _mesa_debug(ctx, "glPopMatrix %s\n",
293 _mesa_enum_to_string(ctx->Transform.MatrixMode));
294
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);
300 }
301 else {
302 _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)",
303 _mesa_enum_to_string(ctx->Transform.MatrixMode));
304 }
305 return;
306 }
307 stack->Depth--;
308 stack->Top = &(stack->Stack[stack->Depth]);
309 ctx->NewState |= stack->DirtyFlag;
310 }
311
312
313 /**
314 * Replace the current matrix with the identity matrix.
315 *
316 * \sa glLoadIdentity().
317 *
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.
321 */
322 void GLAPIENTRY
323 _mesa_LoadIdentity( void )
324 {
325 GET_CURRENT_CONTEXT(ctx);
326
327 FLUSH_VERTICES(ctx, 0);
328
329 if (MESA_VERBOSE & VERBOSE_API)
330 _mesa_debug(ctx, "glLoadIdentity()\n");
331
332 _math_matrix_set_identity( ctx->CurrentStack->Top );
333 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
334 }
335
336
337 /**
338 * Replace the current matrix with a given matrix.
339 *
340 * \param m matrix.
341 *
342 * \sa glLoadMatrixf().
343 *
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.
347 */
348 void GLAPIENTRY
349 _mesa_LoadMatrixf( const GLfloat *m )
350 {
351 GET_CURRENT_CONTEXT(ctx);
352 if (!m) return;
353 if (MESA_VERBOSE & VERBOSE_API)
354 _mesa_debug(ctx,
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]);
360
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;
365 }
366 }
367
368
369 /**
370 * Multiply the current matrix with a given matrix.
371 *
372 * \param m matrix.
373 *
374 * \sa glMultMatrixf().
375 *
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.
379 */
380 void GLAPIENTRY
381 _mesa_MultMatrixf( const GLfloat *m )
382 {
383 GET_CURRENT_CONTEXT(ctx);
384 if (!m) return;
385 if (MESA_VERBOSE & VERBOSE_API)
386 _mesa_debug(ctx,
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]);
392
393 FLUSH_VERTICES(ctx, 0);
394 _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
395 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
396 }
397
398
399 /**
400 * Multiply the current matrix with a rotation matrix.
401 *
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.
406 *
407 * \sa glRotatef().
408 *
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.
412 */
413 void GLAPIENTRY
414 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
415 {
416 GET_CURRENT_CONTEXT(ctx);
417
418 FLUSH_VERTICES(ctx, 0);
419 if (angle != 0.0F) {
420 _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
421 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
422 }
423 }
424
425
426 /**
427 * Multiply the current matrix with a general scaling matrix.
428 *
429 * \param x x axis scale factor.
430 * \param y y axis scale factor.
431 * \param z z axis scale factor.
432 *
433 * \sa glScalef().
434 *
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.
438 */
439 void GLAPIENTRY
440 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
441 {
442 GET_CURRENT_CONTEXT(ctx);
443
444 FLUSH_VERTICES(ctx, 0);
445 _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
446 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
447 }
448
449
450 /**
451 * Multiply the current matrix with a translation matrix.
452 *
453 * \param x translation vector x coordinate.
454 * \param y translation vector y coordinate.
455 * \param z translation vector z coordinate.
456 *
457 * \sa glTranslatef().
458 *
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.
462 */
463 void GLAPIENTRY
464 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
465 {
466 GET_CURRENT_CONTEXT(ctx);
467
468 FLUSH_VERTICES(ctx, 0);
469 _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
470 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
471 }
472
473
474 void GLAPIENTRY
475 _mesa_LoadMatrixd( const GLdouble *m )
476 {
477 GLint i;
478 GLfloat f[16];
479 if (!m) return;
480 for (i = 0; i < 16; i++)
481 f[i] = (GLfloat) m[i];
482 _mesa_LoadMatrixf(f);
483 }
484
485 void GLAPIENTRY
486 _mesa_MultMatrixd( const GLdouble *m )
487 {
488 GLint i;
489 GLfloat f[16];
490 if (!m) return;
491 for (i = 0; i < 16; i++)
492 f[i] = (GLfloat) m[i];
493 _mesa_MultMatrixf( f );
494 }
495
496
497 void GLAPIENTRY
498 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
499 {
500 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
501 }
502
503
504 void GLAPIENTRY
505 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
506 {
507 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
508 }
509
510
511 void GLAPIENTRY
512 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
513 {
514 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
515 }
516
517
518 void GLAPIENTRY
519 _mesa_LoadTransposeMatrixf( const GLfloat *m )
520 {
521 GLfloat tm[16];
522 if (!m) return;
523 _math_transposef(tm, m);
524 _mesa_LoadMatrixf(tm);
525 }
526
527
528 void GLAPIENTRY
529 _mesa_LoadTransposeMatrixd( const GLdouble *m )
530 {
531 GLfloat tm[16];
532 if (!m) return;
533 _math_transposefd(tm, m);
534 _mesa_LoadMatrixf(tm);
535 }
536
537
538 void GLAPIENTRY
539 _mesa_MultTransposeMatrixf( const GLfloat *m )
540 {
541 GLfloat tm[16];
542 if (!m) return;
543 _math_transposef(tm, m);
544 _mesa_MultMatrixf(tm);
545 }
546
547
548 void GLAPIENTRY
549 _mesa_MultTransposeMatrixd( const GLdouble *m )
550 {
551 GLfloat tm[16];
552 if (!m) return;
553 _math_transposefd(tm, m);
554 _mesa_MultMatrixf(tm);
555 }
556
557
558
559 /**********************************************************************/
560 /** \name State management */
561 /*@{*/
562
563
564 /**
565 * Update the projection matrix stack.
566 *
567 * \param ctx GL context.
568 *
569 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
570 * stack, and recomputes user clip positions if necessary.
571 *
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.
576 */
577 static void
578 update_projection( struct gl_context *ctx )
579 {
580 GLbitfield mask;
581
582 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
583
584 /* Recompute clip plane positions in clipspace. This is also done
585 * in _mesa_ClipPlane().
586 */
587 mask = ctx->Transform.ClipPlanesEnabled;
588 while (mask) {
589 const int p = u_bit_scan(&mask);
590
591 _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
592 ctx->Transform.EyeUserPlane[p],
593 ctx->ProjectionMatrixStack.Top->inv );
594 }
595 }
596
597
598 /**
599 * Calculate the combined modelview-projection matrix.
600 *
601 * \param ctx GL context.
602 *
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().
606 */
607 static void
608 calculate_model_project_matrix( struct gl_context *ctx )
609 {
610 _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
611 ctx->ProjectionMatrixStack.Top,
612 ctx->ModelviewMatrixStack.Top );
613
614 _math_matrix_analyse( &ctx->_ModelProjectMatrix );
615 }
616
617
618 /**
619 * Updates the combined modelview-projection matrix.
620 *
621 * \param ctx GL context.
622 * \param new_state new state bit mask.
623 *
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
627 * matrix.
628 */
629 void _mesa_update_modelview_project( struct gl_context *ctx, GLuint new_state )
630 {
631 if (new_state & _NEW_MODELVIEW)
632 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
633
634 if (new_state & _NEW_PROJECTION)
635 update_projection( ctx );
636
637 /* Keep ModelviewProject up to date always to allow tnl
638 * implementations that go model->clip even when eye is required.
639 */
640 calculate_model_project_matrix(ctx);
641 }
642
643 /*@}*/
644
645
646 /**********************************************************************/
647 /** Matrix stack initialization */
648 /*@{*/
649
650
651 /**
652 * Initialize a matrix stack.
653 *
654 * \param stack matrix stack.
655 * \param maxDepth maximum stack depth.
656 * \param dirtyFlag dirty flag.
657 *
658 * Allocates an array of \p maxDepth elements for the matrix stack and calls
659 * _math_matrix_ctr() for each element to initialize it.
660 */
661 static void
662 init_matrix_stack(struct gl_matrix_stack *stack,
663 GLuint maxDepth, GLuint dirtyFlag)
664 {
665 stack->Depth = 0;
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;
673 }
674
675 /**
676 * Free matrix stack.
677 *
678 * \param stack matrix stack.
679 *
680 * Calls _math_matrix_dtr() for each element of the matrix stack and
681 * frees the array.
682 */
683 static void
684 free_matrix_stack( struct gl_matrix_stack *stack )
685 {
686 GLuint i;
687 for (i = 0; i < stack->StackSize; i++) {
688 _math_matrix_dtr(&stack->Stack[i]);
689 }
690 free(stack->Stack);
691 stack->Stack = stack->Top = NULL;
692 stack->StackSize = 0;
693 }
694
695 /*@}*/
696
697
698 /**********************************************************************/
699 /** \name Initialization */
700 /*@{*/
701
702
703 /**
704 * Initialize the context matrix data.
705 *
706 * \param ctx GL context.
707 *
708 * Initializes each of the matrix stacks and the combined modelview-projection
709 * matrix.
710 */
711 void _mesa_init_matrix( struct gl_context * ctx )
712 {
713 GLuint i;
714
715 /* Initialize matrix stacks */
716 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
717 _NEW_MODELVIEW);
718 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
719 _NEW_PROJECTION);
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;
727
728 /* Init combined Modelview*Projection matrix */
729 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
730 }
731
732
733 /**
734 * Free the context matrix data.
735 *
736 * \param ctx GL context.
737 *
738 * Frees each of the matrix stacks and the combined modelview-projection
739 * matrix.
740 */
741 void _mesa_free_matrix_data( struct gl_context *ctx )
742 {
743 GLuint i;
744
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 );
753
754 }
755
756
757 /**
758 * Initialize the context transform attribute group.
759 *
760 * \param ctx GL context.
761 *
762 * \todo Move this to a new file with other 'transform' routines.
763 */
764 void _mesa_init_transform( struct gl_context *ctx )
765 {
766 GLuint i;
767
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 );
775 }
776 ctx->Transform.ClipPlanesEnabled = 0;
777 }
778
779
780 /*@}*/