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