clamp viewport against ctx->Const.MaxViewportWidth/Height instead of MAX_WIDTH/HEIGHT
[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 n = ctx->Viewport.Near;
573 const GLfloat f = ctx->Viewport.Far;
574
575 if (MESA_VERBOSE & VERBOSE_API)
576 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
577
578 if (width < 0 || height < 0) {
579 _mesa_error( ctx, GL_INVALID_VALUE,
580 "glViewport(%d, %d, %d, %d)", x, y, width, height );
581 return;
582 }
583
584 /* clamp width, and height to implementation dependent range */
585 width = CLAMP( width, 1, ctx->Const.MaxViewportWidth );
586 height = CLAMP( height, 1, ctx->Const.MaxViewportHeight );
587
588 /* Save viewport */
589 ctx->Viewport.X = x;
590 ctx->Viewport.Width = width;
591 ctx->Viewport.Y = y;
592 ctx->Viewport.Height = height;
593
594 /* XXX send transposed width/height to Driver.Viewport() below??? */
595 if (ctx->_RotateMode) {
596 GLint tmp, tmps;
597 tmp = x; x = y; y = tmp;
598 tmps = width; width = height; height = tmps;
599 }
600
601 /* compute scale and bias values :: This is really driver-specific
602 * and should be maintained elsewhere if at all. NOTE: RasterPos
603 * uses this.
604 */
605 ctx->Viewport._WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
606 ctx->Viewport._WindowMap.m[MAT_TX] = ctx->Viewport._WindowMap.m[MAT_SX] + x;
607 ctx->Viewport._WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
608 ctx->Viewport._WindowMap.m[MAT_TY] = ctx->Viewport._WindowMap.m[MAT_SY] + y;
609 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0F);
610 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0F + n);
611 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
612 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
613 ctx->NewState |= _NEW_VIEWPORT;
614
615 if (ctx->Driver.Viewport) {
616 /* Many drivers will use this call to check for window size changes
617 * and reallocate the z/stencil/accum/etc buffers if needed.
618 */
619 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
620 }
621 }
622
623
624 #if _HAVE_FULL_GL
625 void GLAPIENTRY
626 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
627 {
628 /*
629 * nearval - specifies mapping of the near clipping plane to window
630 * coordinates, default is 0
631 * farval - specifies mapping of the far clipping plane to window
632 * coordinates, default is 1
633 *
634 * After clipping and div by w, z coords are in -1.0 to 1.0,
635 * corresponding to near and far clipping planes. glDepthRange
636 * specifies a linear mapping of the normalized z coords in
637 * this range to window z coords.
638 */
639 GLfloat n, f;
640 GET_CURRENT_CONTEXT(ctx);
641 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
642
643 if (MESA_VERBOSE&VERBOSE_API)
644 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
645
646 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
647 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
648
649 ctx->Viewport.Near = n;
650 ctx->Viewport.Far = f;
651 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0F);
652 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0F + n);
653 ctx->NewState |= _NEW_VIEWPORT;
654
655 if (ctx->Driver.DepthRange) {
656 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
657 }
658 }
659 #endif
660
661
662
663 /**********************************************************************/
664 /** \name State management */
665 /*@{*/
666
667
668 /**
669 * Update the projection matrix stack.
670 *
671 * \param ctx GL context.
672 *
673 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
674 * stack, and recomputes user clip positions if necessary.
675 *
676 * \note This routine references __GLcontextRec::Tranform attribute values to
677 * compute userclip positions in clip space, but is only called on
678 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
679 * date across changes to the __GLcontextRec::Transform attributes.
680 */
681 static void
682 update_projection( GLcontext *ctx )
683 {
684 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
685
686 #if FEATURE_userclip
687 /* Recompute clip plane positions in clipspace. This is also done
688 * in _mesa_ClipPlane().
689 */
690 if (ctx->Transform.ClipPlanesEnabled) {
691 GLuint p;
692 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
693 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
694 _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
695 ctx->Transform.EyeUserPlane[p],
696 ctx->ProjectionMatrixStack.Top->inv );
697 }
698 }
699 }
700 #endif
701 }
702
703
704 /**
705 * Calculate the combined modelview-projection matrix.
706 *
707 * \param ctx GL context.
708 *
709 * Multiplies the top matrices of the projection and model view stacks into
710 * __GLcontextRec::_ModelProjectMatrix via _math_matrix_mul_matrix() and
711 * analyzes the resulting matrix via _math_matrix_analyse().
712 */
713 static void
714 calculate_model_project_matrix( GLcontext *ctx )
715 {
716 _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
717 ctx->ProjectionMatrixStack.Top,
718 ctx->ModelviewMatrixStack.Top );
719
720 _math_matrix_analyse( &ctx->_ModelProjectMatrix );
721 }
722
723
724 /**
725 * Updates the combined modelview-projection matrix.
726 *
727 * \param ctx GL context.
728 * \param new_state new state bit mask.
729 *
730 * If there is a new model view matrix then analyzes it. If there is a new
731 * projection matrix, updates it. Finally calls
732 * calculate_model_project_matrix() to recalculate the modelview-projection
733 * matrix.
734 */
735 void _mesa_update_modelview_project( GLcontext *ctx, GLuint new_state )
736 {
737 if (new_state & _NEW_MODELVIEW) {
738 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
739
740 /* Bring cull position uptodate.
741 */
742 TRANSFORM_POINT3( ctx->Transform.CullObjPos,
743 ctx->ModelviewMatrixStack.Top->inv,
744 ctx->Transform.CullEyePos );
745 }
746
747
748 if (new_state & _NEW_PROJECTION)
749 update_projection( ctx );
750
751 /* Keep ModelviewProject uptodate always to allow tnl
752 * implementations that go model->clip even when eye is required.
753 */
754 calculate_model_project_matrix(ctx);
755 }
756
757 /*@}*/
758
759
760 /**********************************************************************/
761 /** Matrix stack initialization */
762 /*@{*/
763
764
765 /**
766 * Initialize a matrix stack.
767 *
768 * \param stack matrix stack.
769 * \param maxDepth maximum stack depth.
770 * \param dirtyFlag dirty flag.
771 *
772 * Allocates an array of \p maxDepth elements for the matrix stack and calls
773 * _math_matrix_ctr() and _math_matrix_alloc_inv() for each element to
774 * initialize it.
775 */
776 static void
777 init_matrix_stack( struct matrix_stack *stack,
778 GLuint maxDepth, GLuint dirtyFlag )
779 {
780 GLuint i;
781
782 stack->Depth = 0;
783 stack->MaxDepth = maxDepth;
784 stack->DirtyFlag = dirtyFlag;
785 /* The stack */
786 stack->Stack = (GLmatrix *) CALLOC(maxDepth * sizeof(GLmatrix));
787 for (i = 0; i < maxDepth; i++) {
788 _math_matrix_ctr(&stack->Stack[i]);
789 _math_matrix_alloc_inv(&stack->Stack[i]);
790 }
791 stack->Top = stack->Stack;
792 }
793
794 /**
795 * Free matrix stack.
796 *
797 * \param stack matrix stack.
798 *
799 * Calls _math_matrix_dtr() for each element of the matrix stack and
800 * frees the array.
801 */
802 static void
803 free_matrix_stack( struct matrix_stack *stack )
804 {
805 GLuint i;
806 for (i = 0; i < stack->MaxDepth; i++) {
807 _math_matrix_dtr(&stack->Stack[i]);
808 }
809 FREE(stack->Stack);
810 stack->Stack = stack->Top = NULL;
811 }
812
813 /*@}*/
814
815
816 /**********************************************************************/
817 /** \name Initialization */
818 /*@{*/
819
820
821 /**
822 * Initialize the context matrix data.
823 *
824 * \param ctx GL context.
825 *
826 * Initializes each of the matrix stacks and the combined modelview-projection
827 * matrix.
828 */
829 void _mesa_init_matrix( GLcontext * ctx )
830 {
831 GLint i;
832
833 /* Initialize matrix stacks */
834 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
835 _NEW_MODELVIEW);
836 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
837 _NEW_PROJECTION);
838 init_matrix_stack(&ctx->ColorMatrixStack, MAX_COLOR_STACK_DEPTH,
839 _NEW_COLOR_MATRIX);
840 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
841 init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
842 _NEW_TEXTURE_MATRIX);
843 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
844 init_matrix_stack(&ctx->ProgramMatrixStack[i],
845 MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
846 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
847
848 /* Init combined Modelview*Projection matrix */
849 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
850 }
851
852
853 /**
854 * Free the context matrix data.
855 *
856 * \param ctx GL context.
857 *
858 * Frees each of the matrix stacks and the combined modelview-projection
859 * matrix.
860 */
861 void _mesa_free_matrix_data( GLcontext *ctx )
862 {
863 GLint i;
864
865 free_matrix_stack(&ctx->ModelviewMatrixStack);
866 free_matrix_stack(&ctx->ProjectionMatrixStack);
867 free_matrix_stack(&ctx->ColorMatrixStack);
868 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
869 free_matrix_stack(&ctx->TextureMatrixStack[i]);
870 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
871 free_matrix_stack(&ctx->ProgramMatrixStack[i]);
872 /* combined Modelview*Projection matrix */
873 _math_matrix_dtr( &ctx->_ModelProjectMatrix );
874
875 }
876
877
878 /**
879 * Initialize the context transform attribute group.
880 *
881 * \param ctx GL context.
882 *
883 * \todo Move this to a new file with other 'transform' routines.
884 */
885 void _mesa_init_transform( GLcontext *ctx )
886 {
887 GLint i;
888
889 /* Transformation group */
890 ctx->Transform.MatrixMode = GL_MODELVIEW;
891 ctx->Transform.Normalize = GL_FALSE;
892 ctx->Transform.RescaleNormals = GL_FALSE;
893 ctx->Transform.RasterPositionUnclipped = GL_FALSE;
894 for (i=0;i<MAX_CLIP_PLANES;i++) {
895 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
896 }
897 ctx->Transform.ClipPlanesEnabled = 0;
898
899 ASSIGN_4V( ctx->Transform.CullObjPos, 0.0, 0.0, 1.0, 0.0 );
900 ASSIGN_4V( ctx->Transform.CullEyePos, 0.0, 0.0, 1.0, 0.0 );
901 }
902
903
904 /**
905 * Initialize the context viewport attribute group.
906 *
907 * \param ctx GL context.
908 *
909 * \todo Move this to a new file with other 'viewport' routines.
910 */
911 void _mesa_init_viewport( GLcontext *ctx )
912 {
913 /* Viewport group */
914 ctx->Viewport.X = 0;
915 ctx->Viewport.Y = 0;
916 ctx->Viewport.Width = 0;
917 ctx->Viewport.Height = 0;
918 ctx->Viewport.Near = 0.0;
919 ctx->Viewport.Far = 1.0;
920 _math_matrix_ctr(&ctx->Viewport._WindowMap);
921
922 #define Sz 10
923 #define Tz 14
924 ctx->Viewport._WindowMap.m[Sz] = 0.5F * ctx->DepthMaxF;
925 ctx->Viewport._WindowMap.m[Tz] = 0.5F * ctx->DepthMaxF;
926 #undef Sz
927 #undef Tz
928
929 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
930 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
931 }
932
933
934 /**
935 * Free the context viewport attribute group data.
936 *
937 * \param ctx GL context.
938 *
939 * \todo Move this to a new file with other 'viewport' routines.
940 */
941 void _mesa_free_viewport_data( GLcontext *ctx )
942 {
943 _math_matrix_dtr(&ctx->Viewport._WindowMap);
944 }
945
946 /*@}*/