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