new MESA_DEBUG option: disassem
[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: 6.3
14 *
15 * Copyright (C) 1999-2005 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 "context.h"
39 #include "enums.h"
40 #include "macros.h"
41 #include "matrix.h"
42 #include "mtypes.h"
43 #include "math/m_matrix.h"
44 #include "math/m_xform.h"
45
46
47 /**
48 * Apply a perspective projection matrix.
49 *
50 * \param left left clipping plane coordinate.
51 * \param right right clipping plane coordinate.
52 * \param bottom bottom clipping plane coordinate.
53 * \param top top clipping plane coordinate.
54 * \param nearval distance to the near clipping plane.
55 * \param farval distance to the far clipping plane.
56 *
57 * \sa glFrustum().
58 *
59 * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with
60 * the top matrix of the current matrix stack and sets
61 * __GLcontextRec::NewState.
62 */
63 void GLAPIENTRY
64 _mesa_Frustum( GLdouble left, GLdouble right,
65 GLdouble bottom, GLdouble top,
66 GLdouble nearval, GLdouble farval )
67 {
68 GET_CURRENT_CONTEXT(ctx);
69 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
70
71 if (nearval <= 0.0 ||
72 farval <= 0.0 ||
73 nearval == farval ||
74 left == right ||
75 top == bottom)
76 {
77 _mesa_error( ctx, GL_INVALID_VALUE, "glFrustum" );
78 return;
79 }
80
81 _math_matrix_frustum( ctx->CurrentStack->Top,
82 (GLfloat) left, (GLfloat) right,
83 (GLfloat) bottom, (GLfloat) top,
84 (GLfloat) nearval, (GLfloat) farval );
85 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
86 }
87
88
89 /**
90 * Apply an orthographic projection matrix.
91 *
92 * \param left left clipping plane coordinate.
93 * \param right right clipping plane coordinate.
94 * \param bottom bottom clipping plane coordinate.
95 * \param top top clipping plane coordinate.
96 * \param nearval distance to the near clipping plane.
97 * \param farval distance to the far clipping plane.
98 *
99 * \sa glOrtho().
100 *
101 * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
102 * the top matrix of the current matrix stack and sets
103 * __GLcontextRec::NewState.
104 */
105 void GLAPIENTRY
106 _mesa_Ortho( GLdouble left, GLdouble right,
107 GLdouble bottom, GLdouble top,
108 GLdouble nearval, GLdouble farval )
109 {
110 GET_CURRENT_CONTEXT(ctx);
111 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
112
113 if (MESA_VERBOSE & VERBOSE_API)
114 _mesa_debug(ctx, "glOrtho(%f, %f, %f, %f, %f, %f)\n",
115 left, right, bottom, top, nearval, farval);
116
117 if (left == right ||
118 bottom == top ||
119 nearval == farval)
120 {
121 _mesa_error( ctx, GL_INVALID_VALUE, "glOrtho" );
122 return;
123 }
124
125 _math_matrix_ortho( ctx->CurrentStack->Top,
126 (GLfloat) left, (GLfloat) right,
127 (GLfloat) bottom, (GLfloat) top,
128 (GLfloat) nearval, (GLfloat) farval );
129 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
130 }
131
132
133 /**
134 * Set the current matrix stack.
135 *
136 * \param mode matrix stack.
137 *
138 * \sa glMatrixMode().
139 *
140 * Flushes the vertices, validates the parameter and updates
141 * __GLcontextRec::CurrentStack and gl_transform_attrib::MatrixMode with the
142 * specified matrix stack.
143 */
144 void GLAPIENTRY
145 _mesa_MatrixMode( GLenum mode )
146 {
147 GET_CURRENT_CONTEXT(ctx);
148 ASSERT_OUTSIDE_BEGIN_END(ctx);
149
150 if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
151 return;
152 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
153
154 switch (mode) {
155 case GL_MODELVIEW:
156 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
157 break;
158 case GL_PROJECTION:
159 ctx->CurrentStack = &ctx->ProjectionMatrixStack;
160 break;
161 case GL_TEXTURE:
162 ctx->CurrentStack = &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
163 break;
164 case GL_COLOR:
165 ctx->CurrentStack = &ctx->ColorMatrixStack;
166 break;
167 case GL_MATRIX0_NV:
168 case GL_MATRIX1_NV:
169 case GL_MATRIX2_NV:
170 case GL_MATRIX3_NV:
171 case GL_MATRIX4_NV:
172 case GL_MATRIX5_NV:
173 case GL_MATRIX6_NV:
174 case GL_MATRIX7_NV:
175 if (ctx->Extensions.NV_vertex_program) {
176 ctx->CurrentStack = &ctx->ProgramMatrixStack[mode - GL_MATRIX0_NV];
177 }
178 else {
179 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" );
180 return;
181 }
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->Extensions.ARB_vertex_program ||
192 ctx->Extensions.ARB_fragment_program) {
193 const GLuint m = mode - GL_MATRIX0_ARB;
194 if (m > ctx->Const.MaxProgramMatrices) {
195 _mesa_error(ctx, GL_INVALID_ENUM,
196 "glMatrixMode(GL_MATRIX%d_ARB)", m);
197 return;
198 }
199 ctx->CurrentStack = &ctx->ProgramMatrixStack[m];
200 }
201 else {
202 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" );
203 return;
204 }
205 break;
206 default:
207 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" );
208 return;
209 }
210
211 ctx->Transform.MatrixMode = mode;
212 }
213
214
215 /**
216 * Push the current matrix stack.
217 *
218 * \sa glPushMatrix().
219 *
220 * Verifies the current matrix stack is not full, and duplicates the top-most
221 * matrix in the stack. Marks __GLcontextRec::NewState with the stack dirty
222 * flag.
223 */
224 void GLAPIENTRY
225 _mesa_PushMatrix( void )
226 {
227 GET_CURRENT_CONTEXT(ctx);
228 struct matrix_stack *stack = ctx->CurrentStack;
229 ASSERT_OUTSIDE_BEGIN_END(ctx);
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. Marks __GLcontextRec::NewState with the dirty
262 * stack flag.
263 */
264 void GLAPIENTRY
265 _mesa_PopMatrix( void )
266 {
267 GET_CURRENT_CONTEXT(ctx);
268 struct matrix_stack *stack = ctx->CurrentStack;
269 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
270
271 if (MESA_VERBOSE&VERBOSE_API)
272 _mesa_debug(ctx, "glPopMatrix %s\n",
273 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
274
275 if (stack->Depth == 0) {
276 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
277 _mesa_error(ctx, GL_STACK_UNDERFLOW,
278 "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
279 ctx->Texture.CurrentUnit);
280 }
281 else {
282 _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)",
283 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
284 }
285 return;
286 }
287 stack->Depth--;
288 stack->Top = &(stack->Stack[stack->Depth]);
289 ctx->NewState |= stack->DirtyFlag;
290 }
291
292
293 /**
294 * Replace the current matrix with the identity matrix.
295 *
296 * \sa glLoadIdentity().
297 *
298 * Flushes the vertices and calls _math_matrix_set_identity() with the top-most
299 * matrix in the current stack. Marks __GLcontextRec::NewState with the stack
300 * dirty flag.
301 */
302 void GLAPIENTRY
303 _mesa_LoadIdentity( void )
304 {
305 GET_CURRENT_CONTEXT(ctx);
306 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
307
308 if (MESA_VERBOSE & VERBOSE_API)
309 _mesa_debug(ctx, "glLoadIdentity()");
310
311 _math_matrix_set_identity( ctx->CurrentStack->Top );
312 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
313 }
314
315
316 /**
317 * Replace the current matrix with a given matrix.
318 *
319 * \param m matrix.
320 *
321 * \sa glLoadMatrixf().
322 *
323 * Flushes the vertices and calls _math_matrix_loadf() with the top-most matrix
324 * in the current stack and the given matrix. Marks __GLcontextRec::NewState
325 * with the dirty stack flag.
326 */
327 void GLAPIENTRY
328 _mesa_LoadMatrixf( const GLfloat *m )
329 {
330 GET_CURRENT_CONTEXT(ctx);
331 if (!m) return;
332 if (MESA_VERBOSE & VERBOSE_API)
333 _mesa_debug(ctx,
334 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
335 m[0], m[4], m[8], m[12],
336 m[1], m[5], m[9], m[13],
337 m[2], m[6], m[10], m[14],
338 m[3], m[7], m[11], m[15]);
339
340 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
341 _math_matrix_loadf( ctx->CurrentStack->Top, m );
342 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
343 }
344
345
346 /**
347 * Multiply the current matrix with a given matrix.
348 *
349 * \param m matrix.
350 *
351 * \sa glMultMatrixf().
352 *
353 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
354 * matrix in the current stack and the given matrix. Marks
355 * __GLcontextRec::NewState with the dirty stack flag.
356 */
357 void GLAPIENTRY
358 _mesa_MultMatrixf( const GLfloat *m )
359 {
360 GET_CURRENT_CONTEXT(ctx);
361 if (!m) return;
362 if (MESA_VERBOSE & VERBOSE_API)
363 _mesa_debug(ctx,
364 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
365 m[0], m[4], m[8], m[12],
366 m[1], m[5], m[9], m[13],
367 m[2], m[6], m[10], m[14],
368 m[3], m[7], m[11], m[15]);
369 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
370 _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
371 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
372 }
373
374
375 /**
376 * Multiply the current matrix with a rotation matrix.
377 *
378 * \param angle angle of rotation, in degrees.
379 * \param x rotation vector x coordinate.
380 * \param y rotation vector y coordinate.
381 * \param z rotation vector z coordinate.
382 *
383 * \sa glRotatef().
384 *
385 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
386 * matrix in the current stack and the given parameters. Marks
387 * __GLcontextRec::NewState with the dirty stack flag.
388 */
389 void GLAPIENTRY
390 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
391 {
392 GET_CURRENT_CONTEXT(ctx);
393 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
394 if (angle != 0.0F) {
395 _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
396 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
397 }
398 }
399
400
401 /**
402 * Multiply the current matrix with a general scaling matrix.
403 *
404 * \param x x axis scale factor.
405 * \param y y axis scale factor.
406 * \param z z axis scale factor.
407 *
408 * \sa glScalef().
409 *
410 * Flushes the vertices and calls _math_matrix_scale() with the top-most
411 * matrix in the current stack and the given parameters. Marks
412 * __GLcontextRec::NewState with the dirty stack flag.
413 */
414 void GLAPIENTRY
415 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
416 {
417 GET_CURRENT_CONTEXT(ctx);
418 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
419 _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
420 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
421 }
422
423
424 /**
425 * Multiply the current matrix with a general scaling matrix.
426 *
427 * \param x translation vector x coordinate.
428 * \param y translation vector y coordinate.
429 * \param z translation vector z coordinate.
430 *
431 * \sa glTranslatef().
432 *
433 * Flushes the vertices and calls _math_matrix_translate() with the top-most
434 * matrix in the current stack and the given parameters. Marks
435 * __GLcontextRec::NewState with the dirty stack flag.
436 */
437 void GLAPIENTRY
438 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
439 {
440 GET_CURRENT_CONTEXT(ctx);
441 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
442 _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
443 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
444 }
445
446
447 #if _HAVE_FULL_GL
448 void GLAPIENTRY
449 _mesa_LoadMatrixd( const GLdouble *m )
450 {
451 GLint i;
452 GLfloat f[16];
453 if (!m) return;
454 for (i = 0; i < 16; i++)
455 f[i] = (GLfloat) m[i];
456 _mesa_LoadMatrixf(f);
457 }
458
459 void GLAPIENTRY
460 _mesa_MultMatrixd( const GLdouble *m )
461 {
462 GLint i;
463 GLfloat f[16];
464 if (!m) return;
465 for (i = 0; i < 16; i++)
466 f[i] = (GLfloat) m[i];
467 _mesa_MultMatrixf( f );
468 }
469
470
471 void GLAPIENTRY
472 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
473 {
474 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
475 }
476
477
478 void GLAPIENTRY
479 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
480 {
481 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
482 }
483
484
485 void GLAPIENTRY
486 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
487 {
488 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
489 }
490 #endif
491
492
493 #if _HAVE_FULL_GL
494 void GLAPIENTRY
495 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
496 {
497 GLfloat tm[16];
498 if (!m) return;
499 _math_transposef(tm, m);
500 _mesa_LoadMatrixf(tm);
501 }
502
503
504 void GLAPIENTRY
505 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
506 {
507 GLfloat tm[16];
508 if (!m) return;
509 _math_transposefd(tm, m);
510 _mesa_LoadMatrixf(tm);
511 }
512
513
514 void GLAPIENTRY
515 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
516 {
517 GLfloat tm[16];
518 if (!m) return;
519 _math_transposef(tm, m);
520 _mesa_MultMatrixf(tm);
521 }
522
523
524 void GLAPIENTRY
525 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
526 {
527 GLfloat tm[16];
528 if (!m) return;
529 _math_transposefd(tm, m);
530 _mesa_MultMatrixf(tm);
531 }
532 #endif
533
534 /**
535 * Set the viewport.
536 *
537 * \param x, y coordinates of the lower-left corner of the viewport rectangle.
538 * \param width width of the viewport rectangle.
539 * \param height height of the viewport rectangle.
540 *
541 * \sa Called via glViewport() or display list execution.
542 *
543 * Flushes the vertices and calls _mesa_set_viewport() with the given
544 * parameters.
545 */
546 void GLAPIENTRY
547 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
548 {
549 GET_CURRENT_CONTEXT(ctx);
550 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
551 _mesa_set_viewport(ctx, x, y, width, height);
552 }
553
554 /**
555 * Set new viewport parameters and update derived state (the _WindowMap
556 * matrix). Usually called from _mesa_Viewport().
557 *
558 * \param ctx GL context.
559 * \param x, y coordinates of the lower left corner of the viewport rectangle.
560 * \param width width of the viewport rectangle.
561 * \param height height of the viewport rectangle.
562 *
563 * Verifies the parameters, clamps them to the implementation dependent range
564 * and updates __GLcontextRec::Viewport. Computes the scale and bias values for
565 * the drivers and notifies the driver via the dd_function_table::Viewport
566 * callback.
567 */
568 void
569 _mesa_set_viewport( GLcontext *ctx, GLint x, GLint y,
570 GLsizei width, GLsizei height )
571 {
572 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
573 const GLfloat n = ctx->Viewport.Near;
574 const GLfloat f = ctx->Viewport.Far;
575
576 ASSERT(depthMax > 0);
577
578 if (MESA_VERBOSE & VERBOSE_API)
579 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
580
581 if (width < 0 || height < 0) {
582 _mesa_error( ctx, GL_INVALID_VALUE,
583 "glViewport(%d, %d, %d, %d)", x, y, width, height );
584 return;
585 }
586
587 /* clamp width, and height to implementation dependent range */
588 width = CLAMP( width, 1, ctx->Const.MaxViewportWidth );
589 height = CLAMP( height, 1, ctx->Const.MaxViewportHeight );
590
591 /* Save viewport */
592 ctx->Viewport.X = x;
593 ctx->Viewport.Width = width;
594 ctx->Viewport.Y = y;
595 ctx->Viewport.Height = height;
596
597 /* XXX send transposed width/height to Driver.Viewport() below??? */
598 if (ctx->_RotateMode) {
599 GLint tmp, tmps;
600 tmp = x; x = y; y = tmp;
601 tmps = width; width = height; height = tmps;
602 }
603
604 /* compute scale and bias values :: This is really driver-specific
605 * and should be maintained elsewhere if at all. NOTE: RasterPos
606 * uses this.
607 */
608 ctx->Viewport._WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
609 ctx->Viewport._WindowMap.m[MAT_TX] = ctx->Viewport._WindowMap.m[MAT_SX] + x;
610 ctx->Viewport._WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
611 ctx->Viewport._WindowMap.m[MAT_TY] = ctx->Viewport._WindowMap.m[MAT_SY] + y;
612 ctx->Viewport._WindowMap.m[MAT_SZ] = depthMax * ((f - n) / 2.0F);
613 ctx->Viewport._WindowMap.m[MAT_TZ] = depthMax * ((f - n) / 2.0F + n);
614 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
615 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
616 ctx->NewState |= _NEW_VIEWPORT;
617
618 if (ctx->Driver.Viewport) {
619 /* Many drivers will use this call to check for window size changes
620 * and reallocate the z/stencil/accum/etc buffers if needed.
621 */
622 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
623 }
624 }
625
626
627 #if _HAVE_FULL_GL
628 void GLAPIENTRY
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 depthMax;
643 GLfloat n, f;
644 GET_CURRENT_CONTEXT(ctx);
645 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
646
647 depthMax = ctx->DrawBuffer->_DepthMaxF;
648
649 if (MESA_VERBOSE&VERBOSE_API)
650 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
651
652 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
653 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
654
655 ctx->Viewport.Near = n;
656 ctx->Viewport.Far = f;
657 ctx->Viewport._WindowMap.m[MAT_SZ] = depthMax * ((f - n) / 2.0F);
658 ctx->Viewport._WindowMap.m[MAT_TZ] = depthMax * ((f - n) / 2.0F + n);
659 ctx->NewState |= _NEW_VIEWPORT;
660
661 if (ctx->Driver.DepthRange) {
662 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
663 }
664 }
665 #endif
666
667
668
669 /**********************************************************************/
670 /** \name State management */
671 /*@{*/
672
673
674 /**
675 * Update the projection matrix stack.
676 *
677 * \param ctx GL context.
678 *
679 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
680 * stack, and recomputes user clip positions if necessary.
681 *
682 * \note This routine references __GLcontextRec::Tranform attribute values to
683 * compute userclip positions in clip space, but is only called on
684 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
685 * date across changes to the __GLcontextRec::Transform attributes.
686 */
687 static void
688 update_projection( GLcontext *ctx )
689 {
690 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
691
692 #if FEATURE_userclip
693 /* Recompute clip plane positions in clipspace. This is also done
694 * in _mesa_ClipPlane().
695 */
696 if (ctx->Transform.ClipPlanesEnabled) {
697 GLuint p;
698 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
699 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
700 _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
701 ctx->Transform.EyeUserPlane[p],
702 ctx->ProjectionMatrixStack.Top->inv );
703 }
704 }
705 }
706 #endif
707 }
708
709
710 /**
711 * Calculate the combined modelview-projection matrix.
712 *
713 * \param ctx GL context.
714 *
715 * Multiplies the top matrices of the projection and model view stacks into
716 * __GLcontextRec::_ModelProjectMatrix via _math_matrix_mul_matrix() and
717 * analyzes the resulting matrix via _math_matrix_analyse().
718 */
719 static void
720 calculate_model_project_matrix( GLcontext *ctx )
721 {
722 _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
723 ctx->ProjectionMatrixStack.Top,
724 ctx->ModelviewMatrixStack.Top );
725
726 _math_matrix_analyse( &ctx->_ModelProjectMatrix );
727 }
728
729
730 /**
731 * Updates the combined modelview-projection matrix.
732 *
733 * \param ctx GL context.
734 * \param new_state new state bit mask.
735 *
736 * If there is a new model view matrix then analyzes it. If there is a new
737 * projection matrix, updates it. Finally calls
738 * calculate_model_project_matrix() to recalculate the modelview-projection
739 * matrix.
740 */
741 void _mesa_update_modelview_project( GLcontext *ctx, GLuint new_state )
742 {
743 if (new_state & _NEW_MODELVIEW) {
744 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
745
746 /* Bring cull position uptodate.
747 */
748 TRANSFORM_POINT3( ctx->Transform.CullObjPos,
749 ctx->ModelviewMatrixStack.Top->inv,
750 ctx->Transform.CullEyePos );
751 }
752
753
754 if (new_state & _NEW_PROJECTION)
755 update_projection( ctx );
756
757 /* Keep ModelviewProject uptodate always to allow tnl
758 * implementations that go model->clip even when eye is required.
759 */
760 calculate_model_project_matrix(ctx);
761 }
762
763 /*@}*/
764
765
766 /**********************************************************************/
767 /** Matrix stack initialization */
768 /*@{*/
769
770
771 /**
772 * Initialize a matrix stack.
773 *
774 * \param stack matrix stack.
775 * \param maxDepth maximum stack depth.
776 * \param dirtyFlag dirty flag.
777 *
778 * Allocates an array of \p maxDepth elements for the matrix stack and calls
779 * _math_matrix_ctr() and _math_matrix_alloc_inv() for each element to
780 * initialize it.
781 */
782 static void
783 init_matrix_stack( struct matrix_stack *stack,
784 GLuint maxDepth, GLuint dirtyFlag )
785 {
786 GLuint i;
787
788 stack->Depth = 0;
789 stack->MaxDepth = maxDepth;
790 stack->DirtyFlag = dirtyFlag;
791 /* The stack */
792 stack->Stack = (GLmatrix *) CALLOC(maxDepth * sizeof(GLmatrix));
793 for (i = 0; i < maxDepth; i++) {
794 _math_matrix_ctr(&stack->Stack[i]);
795 _math_matrix_alloc_inv(&stack->Stack[i]);
796 }
797 stack->Top = stack->Stack;
798 }
799
800 /**
801 * Free matrix stack.
802 *
803 * \param stack matrix stack.
804 *
805 * Calls _math_matrix_dtr() for each element of the matrix stack and
806 * frees the array.
807 */
808 static void
809 free_matrix_stack( struct matrix_stack *stack )
810 {
811 GLuint i;
812 for (i = 0; i < stack->MaxDepth; i++) {
813 _math_matrix_dtr(&stack->Stack[i]);
814 }
815 FREE(stack->Stack);
816 stack->Stack = stack->Top = NULL;
817 }
818
819 /*@}*/
820
821
822 /**********************************************************************/
823 /** \name Initialization */
824 /*@{*/
825
826
827 /**
828 * Initialize the context matrix data.
829 *
830 * \param ctx GL context.
831 *
832 * Initializes each of the matrix stacks and the combined modelview-projection
833 * matrix.
834 */
835 void _mesa_init_matrix( GLcontext * ctx )
836 {
837 GLint i;
838
839 /* Initialize matrix stacks */
840 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
841 _NEW_MODELVIEW);
842 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
843 _NEW_PROJECTION);
844 init_matrix_stack(&ctx->ColorMatrixStack, MAX_COLOR_STACK_DEPTH,
845 _NEW_COLOR_MATRIX);
846 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
847 init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
848 _NEW_TEXTURE_MATRIX);
849 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
850 init_matrix_stack(&ctx->ProgramMatrixStack[i],
851 MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
852 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
853
854 /* Init combined Modelview*Projection matrix */
855 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
856 }
857
858
859 /**
860 * Free the context matrix data.
861 *
862 * \param ctx GL context.
863 *
864 * Frees each of the matrix stacks and the combined modelview-projection
865 * matrix.
866 */
867 void _mesa_free_matrix_data( GLcontext *ctx )
868 {
869 GLint i;
870
871 free_matrix_stack(&ctx->ModelviewMatrixStack);
872 free_matrix_stack(&ctx->ProjectionMatrixStack);
873 free_matrix_stack(&ctx->ColorMatrixStack);
874 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
875 free_matrix_stack(&ctx->TextureMatrixStack[i]);
876 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
877 free_matrix_stack(&ctx->ProgramMatrixStack[i]);
878 /* combined Modelview*Projection matrix */
879 _math_matrix_dtr( &ctx->_ModelProjectMatrix );
880
881 }
882
883
884 /**
885 * Initialize the context transform attribute group.
886 *
887 * \param ctx GL context.
888 *
889 * \todo Move this to a new file with other 'transform' routines.
890 */
891 void _mesa_init_transform( GLcontext *ctx )
892 {
893 GLint i;
894
895 /* Transformation group */
896 ctx->Transform.MatrixMode = GL_MODELVIEW;
897 ctx->Transform.Normalize = GL_FALSE;
898 ctx->Transform.RescaleNormals = GL_FALSE;
899 ctx->Transform.RasterPositionUnclipped = GL_FALSE;
900 for (i=0;i<MAX_CLIP_PLANES;i++) {
901 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
902 }
903 ctx->Transform.ClipPlanesEnabled = 0;
904
905 ASSIGN_4V( ctx->Transform.CullObjPos, 0.0, 0.0, 1.0, 0.0 );
906 ASSIGN_4V( ctx->Transform.CullEyePos, 0.0, 0.0, 1.0, 0.0 );
907 }
908
909
910 /**
911 * Initialize the context viewport attribute group.
912 *
913 * \param ctx GL context.
914 *
915 * \todo Move this to a new file with other 'viewport' routines.
916 */
917 void _mesa_init_viewport( GLcontext *ctx )
918 {
919 /* Viewport group */
920 ctx->Viewport.X = 0;
921 ctx->Viewport.Y = 0;
922 ctx->Viewport.Width = 0;
923 ctx->Viewport.Height = 0;
924 ctx->Viewport.Near = 0.0;
925 ctx->Viewport.Far = 1.0;
926 _math_matrix_ctr(&ctx->Viewport._WindowMap);
927
928 #if 0000
929 #define Sz 10
930 #define Tz 14
931 ctx->Viewport._WindowMap.m[Sz] = 0.5F * ctx->DepthMaxF;
932 ctx->Viewport._WindowMap.m[Tz] = 0.5F * ctx->DepthMaxF;
933 #undef Sz
934 #undef Tz
935 #endif
936
937 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
938 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
939 }
940
941
942 /**
943 * Free the context viewport attribute group data.
944 *
945 * \param ctx GL context.
946 *
947 * \todo Move this to a new file with other 'viewport' routines.
948 */
949 void _mesa_free_viewport_data( GLcontext *ctx )
950 {
951 _math_matrix_dtr(&ctx->Viewport._WindowMap);
952 }
953
954 /*@}*/