Fix up some loose ends from the last big check-in.
[mesa.git] / src / mesa / main / matrix.c
1 /**
2 * \file matrix.c
3 * Matrix operations.
4 *
5 * \note
6 * -# 4x4 transformation matrices are stored in memory in column major order.
7 * -# Points/vertices are to be thought of as column vectors.
8 * -# Transformation of a point p by a matrix M is: p' = M * p
9 */
10
11 /*
12 * Mesa 3-D graphics library
13 * Version: 5.1
14 *
15 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
23 *
24 * The above copyright notice and this permission notice shall be included
25 * in all copies or substantial portions of the Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
31 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 */
34
35
36 #include "glheader.h"
37 #include "imports.h"
38 #include "buffers.h"
39 #include "context.h"
40 #include "enums.h"
41 #include "macros.h"
42 #include "matrix.h"
43 #include "mtypes.h"
44 #include "math/m_matrix.h"
45 #include "math/m_xform.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
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
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, "glFrustum(%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
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 ctx->CurrentStack = &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
164 break;
165 case GL_COLOR:
166 ctx->CurrentStack = &ctx->ColorMatrixStack;
167 break;
168 case GL_MATRIX0_NV:
169 case GL_MATRIX1_NV:
170 case GL_MATRIX2_NV:
171 case GL_MATRIX3_NV:
172 case GL_MATRIX4_NV:
173 case GL_MATRIX5_NV:
174 case GL_MATRIX6_NV:
175 case GL_MATRIX7_NV:
176 if (ctx->Extensions.NV_vertex_program) {
177 ctx->CurrentStack = &ctx->ProgramMatrixStack[mode - GL_MATRIX0_NV];
178 }
179 else {
180 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" );
181 return;
182 }
183 break;
184 case GL_MATRIX0_ARB:
185 case GL_MATRIX1_ARB:
186 case GL_MATRIX2_ARB:
187 case GL_MATRIX3_ARB:
188 case GL_MATRIX4_ARB:
189 case GL_MATRIX5_ARB:
190 case GL_MATRIX6_ARB:
191 case GL_MATRIX7_ARB:
192 if (ctx->Extensions.ARB_vertex_program ||
193 ctx->Extensions.ARB_fragment_program) {
194 const GLint 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. Marks __GLcontextRec::NewState with the stack dirty
223 * flag.
224 */
225 void
226 _mesa_PushMatrix( void )
227 {
228 GET_CURRENT_CONTEXT(ctx);
229 struct matrix_stack *stack = ctx->CurrentStack;
230 ASSERT_OUTSIDE_BEGIN_END(ctx);
231
232 if (MESA_VERBOSE&VERBOSE_API)
233 _mesa_debug(ctx, "glPushMatrix %s\n",
234 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
235
236 if (stack->Depth + 1 >= stack->MaxDepth) {
237 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix" );
238 return;
239 }
240 _math_matrix_copy( &stack->Stack[stack->Depth + 1],
241 &stack->Stack[stack->Depth] );
242 stack->Depth++;
243 stack->Top = &(stack->Stack[stack->Depth]);
244 ctx->NewState |= stack->DirtyFlag;
245 }
246
247
248 /**
249 * Pop the current matrix stack.
250 *
251 * \sa glPopMatrix().
252 *
253 * Flushes the vertices, verifies the current matrix stack is not empty, and
254 * moves the stack head down. Marks __GLcontextRec::NewState with the dirty
255 * stack flag.
256 */
257 void
258 _mesa_PopMatrix( void )
259 {
260 GET_CURRENT_CONTEXT(ctx);
261 struct matrix_stack *stack = ctx->CurrentStack;
262 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
263
264 if (MESA_VERBOSE&VERBOSE_API)
265 _mesa_debug(ctx, "glPopMatrix %s\n",
266 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
267
268 if (stack->Depth == 0) {
269 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix" );
270 return;
271 }
272 stack->Depth--;
273 stack->Top = &(stack->Stack[stack->Depth]);
274 ctx->NewState |= stack->DirtyFlag;
275 }
276
277
278 /**
279 * Replace the current matrix with the identity matrix.
280 *
281 * \sa glLoadIdentity().
282 *
283 * Flushes the vertices and calls _math_matrix_set_identity() with the top-most
284 * matrix in the current stack. Marks __GLcontextRec::NewState with the stack
285 * dirty flag.
286 */
287 void
288 _mesa_LoadIdentity( void )
289 {
290 GET_CURRENT_CONTEXT(ctx);
291 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
292
293 if (MESA_VERBOSE & VERBOSE_API)
294 _mesa_debug(ctx, "glLoadIdentity()");
295
296 _math_matrix_set_identity( ctx->CurrentStack->Top );
297 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
298 }
299
300
301 /**
302 * Replace the current matrix with a given matrix.
303 *
304 * \param m matrix.
305 *
306 * \sa glLoadMatrixf().
307 *
308 * Flushes the vertices and calls _math_matrix_loadf() with the top-most matrix
309 * in the current stack and the given matrix. Marks __GLcontextRec::NewState
310 * with the dirty stack flag.
311 */
312 void
313 _mesa_LoadMatrixf( const GLfloat *m )
314 {
315 GET_CURRENT_CONTEXT(ctx);
316 if (!m) return;
317 if (MESA_VERBOSE & VERBOSE_API)
318 _mesa_debug(ctx,
319 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
320 m[0], m[4], m[8], m[12],
321 m[1], m[5], m[9], m[13],
322 m[2], m[6], m[10], m[14],
323 m[3], m[7], m[11], m[15]);
324
325 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
326 _math_matrix_loadf( ctx->CurrentStack->Top, m );
327 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
328 }
329
330
331 /**
332 * Multiply the current matrix with a given matrix.
333 *
334 * \param m matrix.
335 *
336 * \sa glMultMatrixf().
337 *
338 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
339 * matrix in the current stack and the given matrix. Marks
340 * __GLcontextRec::NewState with the dirty stack flag.
341 */
342 void
343 _mesa_MultMatrixf( const GLfloat *m )
344 {
345 GET_CURRENT_CONTEXT(ctx);
346 if (!m) return;
347 if (MESA_VERBOSE & VERBOSE_API)
348 _mesa_debug(ctx,
349 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
350 m[0], m[4], m[8], m[12],
351 m[1], m[5], m[9], m[13],
352 m[2], m[6], m[10], m[14],
353 m[3], m[7], m[11], m[15]);
354 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
355 _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
356 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
357 }
358
359
360 /**
361 * Multiply the current matrix with a rotation matrix.
362 *
363 * \param angle angle of rotation, in degrees.
364 * \param x rotation vector x coordinate.
365 * \param y rotation vector y coordinate.
366 * \param z rotation vector z coordinate.
367 *
368 * \sa glRotatef().
369 *
370 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
371 * matrix in the current stack and the given parameters. Marks
372 * __GLcontextRec::NewState with the dirty stack flag.
373 */
374 void
375 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
376 {
377 GET_CURRENT_CONTEXT(ctx);
378 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
379 if (angle != 0.0F) {
380 _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
381 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
382 }
383 }
384
385
386 /**
387 * Multiply the current matrix with a general scaling matrix.
388 *
389 * \param x x axis scale factor.
390 * \param y y axis scale factor.
391 * \param z z axis scale factor.
392 *
393 * \sa glScalef().
394 *
395 * Flushes the vertices and calls _math_matrix_scale() with the top-most
396 * matrix in the current stack and the given parameters. Marks
397 * __GLcontextRec::NewState with the dirty stack flag.
398 */
399 void
400 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
401 {
402 GET_CURRENT_CONTEXT(ctx);
403 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
404 _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
405 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
406 }
407
408
409 /**
410 * Multiply the current matrix with a general scaling matrix.
411 *
412 * \param x translation vector x coordinate.
413 * \param y translation vector y coordinate.
414 * \param z translation vector z coordinate.
415 *
416 * \sa glTranslatef().
417 *
418 * Flushes the vertices and calls _math_matrix_translate() with the top-most
419 * matrix in the current stack and the given parameters. Marks
420 * __GLcontextRec::NewState with the dirty stack flag.
421 */
422 void
423 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
424 {
425 GET_CURRENT_CONTEXT(ctx);
426 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
427 _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
428 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
429 }
430
431
432 #if _HAVE_FULL_GL
433 void
434 _mesa_LoadMatrixd( const GLdouble *m )
435 {
436 GLint i;
437 GLfloat f[16];
438 if (!m) return;
439 for (i = 0; i < 16; i++)
440 f[i] = (GLfloat) m[i];
441 _mesa_LoadMatrixf(f);
442 }
443
444 void
445 _mesa_MultMatrixd( const GLdouble *m )
446 {
447 GLint i;
448 GLfloat f[16];
449 if (!m) return;
450 for (i = 0; i < 16; i++)
451 f[i] = (GLfloat) m[i];
452 _mesa_MultMatrixf( f );
453 }
454
455
456 void
457 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
458 {
459 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
460 }
461
462
463 void
464 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
465 {
466 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
467 }
468
469
470 void
471 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
472 {
473 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
474 }
475 #endif
476
477
478 #if _HAVE_FULL_GL
479 void
480 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
481 {
482 GLfloat tm[16];
483 if (!m) return;
484 _math_transposef(tm, m);
485 _mesa_LoadMatrixf(tm);
486 }
487
488
489 void
490 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
491 {
492 GLfloat tm[16];
493 if (!m) return;
494 _math_transposefd(tm, m);
495 _mesa_LoadMatrixf(tm);
496 }
497
498
499 void
500 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
501 {
502 GLfloat tm[16];
503 if (!m) return;
504 _math_transposef(tm, m);
505 _mesa_MultMatrixf(tm);
506 }
507
508
509 void
510 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
511 {
512 GLfloat tm[16];
513 if (!m) return;
514 _math_transposefd(tm, m);
515 _mesa_MultMatrixf(tm);
516 }
517 #endif
518
519 /**
520 * Set the viewport.
521 *
522 * \param x, y coordinates of the lower-left corner of the viewport rectangle.
523 * \param width width of the viewport rectangle.
524 * \param height height of the viewport rectangle.
525 *
526 * \sa Called via glViewport() or display list execution.
527 *
528 * Flushes the vertices and calls _mesa_set_viewport() with the given
529 * parameters.
530 */
531 void
532 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
533 {
534 GET_CURRENT_CONTEXT(ctx);
535 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
536 _mesa_set_viewport(ctx, x, y, width, height);
537 }
538
539 /**
540 * Set new viewport parameters and update derived state (the _WindowMap
541 * matrix). Usually called from _mesa_Viewport().
542 *
543 * \note We also call _mesa_ResizeBuffersMESA() because this is a good
544 * time to check if the window has been resized. Many device drivers
545 * can't get direct notification from the window system of size changes
546 * so this is an ad-hoc solution to that problem.
547 *
548 * \param ctx GL context.
549 * \param x, y coordinates of the lower left corner of the viewport rectangle.
550 * \param width width of the viewport rectangle.
551 * \param height height of the viewport rectangle.
552 *
553 * Verifies the parameters, clamps them to the implementation dependent range
554 * and updates __GLcontextRec::Viewport. Computes the scale and bias values for
555 * the drivers and notifies the driver via the dd_function_table::Viewport
556 * callback.
557 */
558 void
559 _mesa_set_viewport( GLcontext *ctx, GLint x, GLint y,
560 GLsizei width, GLsizei height )
561 {
562 const GLfloat n = ctx->Viewport.Near;
563 const GLfloat f = ctx->Viewport.Far;
564
565 if (MESA_VERBOSE & VERBOSE_API)
566 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
567
568 if (width < 0 || height < 0) {
569 _mesa_error( ctx, GL_INVALID_VALUE,
570 "glViewport(%d, %d, %d, %d)", x, y, width, height );
571 return;
572 }
573
574 /* clamp width, and height to implementation dependent range */
575 width = CLAMP( width, 1, MAX_WIDTH );
576 height = CLAMP( height, 1, MAX_HEIGHT );
577
578 /* Save viewport */
579 ctx->Viewport.X = x;
580 ctx->Viewport.Width = width;
581 ctx->Viewport.Y = y;
582 ctx->Viewport.Height = height;
583
584 /* Check if window/buffer has been resized and if so, reallocate the
585 * ancillary buffers.
586 */
587 /* _mesa_ResizeBuffersMESA(); */
588
589 if (ctx->Driver.Viewport) {
590 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
591 }
592
593 if (ctx->_RotateMode) {
594 GLint tmp, tmps;
595 tmp = x; x = y; y = tmp;
596 tmps = width; width = height; height = tmps;
597 }
598
599 /* compute scale and bias values :: This is really driver-specific
600 * and should be maintained elsewhere if at all. NOTE: RasterPos
601 * uses this.
602 */
603 ctx->Viewport._WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
604 ctx->Viewport._WindowMap.m[MAT_TX] = ctx->Viewport._WindowMap.m[MAT_SX] + x;
605 ctx->Viewport._WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
606 ctx->Viewport._WindowMap.m[MAT_TY] = ctx->Viewport._WindowMap.m[MAT_SY] + y;
607 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0F);
608 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0F + n);
609 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
610 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
611 ctx->NewState |= _NEW_VIEWPORT;
612
613 /* Check if window/buffer has been resized and if so, reallocate the
614 * ancillary buffers. This is an ad-hoc solution to detecting window
615 * size changes. 99% of all GL apps call glViewport when a window is
616 * resized so this is a good time to check for new window dims and
617 * reallocate color buffers and ancilliary buffers.
618 */
619 _mesa_ResizeBuffersMESA();
620
621 if (ctx->Driver.Viewport) {
622 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
623 }
624 }
625
626
627 #if _HAVE_FULL_GL
628 void
629 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
630 {
631 /*
632 * nearval - specifies mapping of the near clipping plane to window
633 * coordinates, default is 0
634 * farval - specifies mapping of the far clipping plane to window
635 * coordinates, default is 1
636 *
637 * After clipping and div by w, z coords are in -1.0 to 1.0,
638 * corresponding to near and far clipping planes. glDepthRange
639 * specifies a linear mapping of the normalized z coords in
640 * this range to window z coords.
641 */
642 GLfloat n, f;
643 GET_CURRENT_CONTEXT(ctx);
644 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
645
646 if (MESA_VERBOSE&VERBOSE_API)
647 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
648
649 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
650 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
651
652 ctx->Viewport.Near = n;
653 ctx->Viewport.Far = f;
654 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0F);
655 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0F + n);
656 ctx->NewState |= _NEW_VIEWPORT;
657
658 if (ctx->Driver.DepthRange) {
659 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
660 }
661 }
662 #endif
663
664
665
666 /**********************************************************************/
667 /** \name State management */
668 /*@{*/
669
670
671 /**
672 * Update the projection matrix stack.
673 *
674 * \param ctx GL context.
675 *
676 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
677 * stack, and recomputes user clip positions if necessary.
678 *
679 * \note This routine references __GLcontextRec::Tranform attribute values to
680 * compute userclip positions in clip space, but is only called on
681 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
682 * date across changes to the __GLcontextRec::Transform attributes.
683 */
684 static void
685 update_projection( GLcontext *ctx )
686 {
687 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
688
689 #if FEATURE_userclip
690 /* Recompute clip plane positions in clipspace. This is also done
691 * in _mesa_ClipPlane().
692 */
693 if (ctx->Transform.ClipPlanesEnabled) {
694 GLuint p;
695 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
696 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
697 _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
698 ctx->Transform.EyeUserPlane[p],
699 ctx->ProjectionMatrixStack.Top->inv );
700 }
701 }
702 }
703 #endif
704 }
705
706
707 /**
708 * Calculate the combined modelview-projection matrix.
709 *
710 * \param ctx GL context.
711 *
712 * Multiplies the top matrices of the projection and model view stacks into
713 * __GLcontextRec::_ModelProjectMatrix via _math_matrix_mul_matrix() and
714 * analyzes the resulting matrix via _math_matrix_analyse().
715 */
716 static void
717 calculate_model_project_matrix( GLcontext *ctx )
718 {
719 _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
720 ctx->ProjectionMatrixStack.Top,
721 ctx->ModelviewMatrixStack.Top );
722
723 _math_matrix_analyse( &ctx->_ModelProjectMatrix );
724 }
725
726
727 /**
728 * Updates the combined modelview-projection matrix.
729 *
730 * \param ctx GL context.
731 * \param new_state new state bit mask.
732 *
733 * If there is a new model view matrix then analyzes it. If there is a new
734 * projection matrix, updates it. Finally calls
735 * calculate_model_project_matrix() to recalculate the modelview-projection
736 * matrix.
737 */
738 void _mesa_update_modelview_project( GLcontext *ctx, GLuint new_state )
739 {
740 if (new_state & _NEW_MODELVIEW)
741 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
742
743 if (new_state & _NEW_PROJECTION)
744 update_projection( ctx );
745
746 /* Keep ModelviewProject uptodate always to allow tnl
747 * implementations that go model->clip even when eye is required.
748 */
749 calculate_model_project_matrix(ctx);
750 }
751
752 /*@}*/
753
754
755 /**********************************************************************/
756 /** Matrix stack initialization */
757 /*@{*/
758
759
760 /**
761 * Initialize a matrix stack.
762 *
763 * \param stack matrix stack.
764 * \param maxDepth maximum stack depth.
765 * \param dirtyFlag dirty flag.
766 *
767 * Allocates an array of \p maxDepth elements for the matrix stack and calls
768 * _math_matrix_ctr() and _math_matrix_alloc_inv() for each element to
769 * initialize it.
770 */
771 static void
772 init_matrix_stack( struct matrix_stack *stack,
773 GLuint maxDepth, GLuint dirtyFlag )
774 {
775 GLuint i;
776
777 stack->Depth = 0;
778 stack->MaxDepth = maxDepth;
779 stack->DirtyFlag = dirtyFlag;
780 /* The stack */
781 stack->Stack = (GLmatrix *) CALLOC(maxDepth * sizeof(GLmatrix));
782 for (i = 0; i < maxDepth; i++) {
783 _math_matrix_ctr(&stack->Stack[i]);
784 _math_matrix_alloc_inv(&stack->Stack[i]);
785 }
786 stack->Top = stack->Stack;
787 }
788
789 /**
790 * Free matrix stack.
791 *
792 * \param stack matrix stack.
793 *
794 * Calls _math_matrix_dtr() for each element of the matrix stack and
795 * frees the array.
796 */
797 static void
798 free_matrix_stack( struct matrix_stack *stack )
799 {
800 GLuint i;
801 for (i = 0; i < stack->MaxDepth; i++) {
802 _math_matrix_dtr(&stack->Stack[i]);
803 }
804 FREE(stack->Stack);
805 stack->Stack = stack->Top = NULL;
806 }
807
808 /*@}*/
809
810
811 /**********************************************************************/
812 /** \name Initialization */
813 /*@{*/
814
815
816 /**
817 * Initialize the context matrix data.
818 *
819 * \param ctx GL context.
820 *
821 * Initializes each of the matrix stacks and the combined modelview-projection
822 * matrix.
823 */
824 void _mesa_init_matrix( GLcontext * ctx )
825 {
826 GLint i;
827
828 /* Initialize matrix stacks */
829 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
830 _NEW_MODELVIEW);
831 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
832 _NEW_PROJECTION);
833 init_matrix_stack(&ctx->ColorMatrixStack, MAX_COLOR_STACK_DEPTH,
834 _NEW_COLOR_MATRIX);
835 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
836 init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
837 _NEW_TEXTURE_MATRIX);
838 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
839 init_matrix_stack(&ctx->ProgramMatrixStack[i],
840 MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
841 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
842
843 /* Init combined Modelview*Projection matrix */
844 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
845 }
846
847
848 /**
849 * Free the context matrix data.
850 *
851 * \param ctx GL context.
852 *
853 * Frees each of the matrix stacks and the combined modelview-projection
854 * matrix.
855 */
856 void _mesa_free_matrix_data( GLcontext *ctx )
857 {
858 GLint i;
859
860 free_matrix_stack(&ctx->ModelviewMatrixStack);
861 free_matrix_stack(&ctx->ProjectionMatrixStack);
862 free_matrix_stack(&ctx->ColorMatrixStack);
863 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
864 free_matrix_stack(&ctx->TextureMatrixStack[i]);
865 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
866 free_matrix_stack(&ctx->ProgramMatrixStack[i]);
867 /* combined Modelview*Projection matrix */
868 _math_matrix_dtr( &ctx->_ModelProjectMatrix );
869
870 }
871
872
873 /**
874 * Initialize the context transform attribute group.
875 *
876 * \param ctx GL context.
877 *
878 * \todo Move this to a new file with other 'transform' routines.
879 */
880 void _mesa_init_transform( GLcontext *ctx )
881 {
882 GLint i;
883
884 /* Transformation group */
885 ctx->Transform.MatrixMode = GL_MODELVIEW;
886 ctx->Transform.Normalize = GL_FALSE;
887 ctx->Transform.RescaleNormals = GL_FALSE;
888 ctx->Transform.RasterPositionUnclipped = GL_FALSE;
889 for (i=0;i<MAX_CLIP_PLANES;i++) {
890 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
891 }
892 ctx->Transform.ClipPlanesEnabled = 0;
893 }
894
895
896 /**
897 * Initialize the context viewport attribute group.
898 *
899 * \param ctx GL context.
900 *
901 * \todo Move this to a new file with other 'viewport' routines.
902 */
903 void _mesa_init_viewport( GLcontext *ctx )
904 {
905 /* Viewport group */
906 ctx->Viewport.X = 0;
907 ctx->Viewport.Y = 0;
908 ctx->Viewport.Width = 0;
909 ctx->Viewport.Height = 0;
910 ctx->Viewport.Near = 0.0;
911 ctx->Viewport.Far = 1.0;
912 _math_matrix_ctr(&ctx->Viewport._WindowMap);
913
914 #define Sz 10
915 #define Tz 14
916 ctx->Viewport._WindowMap.m[Sz] = 0.5F * ctx->DepthMaxF;
917 ctx->Viewport._WindowMap.m[Tz] = 0.5F * ctx->DepthMaxF;
918 #undef Sz
919 #undef Tz
920
921 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
922 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
923 }
924
925
926 /**
927 * Free the context viewport attribute group data.
928 *
929 * \param ctx GL context.
930 *
931 * \todo Move this to a new file with other 'viewport' routines.
932 */
933 void _mesa_free_viewport_data( GLcontext *ctx )
934 {
935 _math_matrix_dtr(&ctx->Viewport._WindowMap);
936 }
937
938 /*@}*/