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