remove redundant call to ctx->Driver.Viewport(), and clean-ups
[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.1
14 *
15 * Copyright (C) 1999-2004 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 GLAPIENTRY
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 GLAPIENTRY
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 GLAPIENTRY
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 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. Marks __GLcontextRec::NewState with the stack dirty
223 * flag.
224 */
225 void GLAPIENTRY
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 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
238 _mesa_error(ctx, GL_STACK_OVERFLOW,
239 "glPushMatrix(mode=GL_TEXTURE, unit=%d)",
240 ctx->Texture.CurrentUnit);
241 }
242 else {
243 _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushMatrix(mode=%s)",
244 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
245 }
246 return;
247 }
248 _math_matrix_copy( &stack->Stack[stack->Depth + 1],
249 &stack->Stack[stack->Depth] );
250 stack->Depth++;
251 stack->Top = &(stack->Stack[stack->Depth]);
252 ctx->NewState |= stack->DirtyFlag;
253 }
254
255
256 /**
257 * Pop the current matrix stack.
258 *
259 * \sa glPopMatrix().
260 *
261 * Flushes the vertices, verifies the current matrix stack is not empty, and
262 * moves the stack head down. Marks __GLcontextRec::NewState with the dirty
263 * stack flag.
264 */
265 void GLAPIENTRY
266 _mesa_PopMatrix( void )
267 {
268 GET_CURRENT_CONTEXT(ctx);
269 struct matrix_stack *stack = ctx->CurrentStack;
270 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
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 top-most
300 * matrix in the current stack. Marks __GLcontextRec::NewState with the stack
301 * dirty flag.
302 */
303 void GLAPIENTRY
304 _mesa_LoadIdentity( void )
305 {
306 GET_CURRENT_CONTEXT(ctx);
307 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
308
309 if (MESA_VERBOSE & VERBOSE_API)
310 _mesa_debug(ctx, "glLoadIdentity()");
311
312 _math_matrix_set_identity( ctx->CurrentStack->Top );
313 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
314 }
315
316
317 /**
318 * Replace the current matrix with a given matrix.
319 *
320 * \param m matrix.
321 *
322 * \sa glLoadMatrixf().
323 *
324 * Flushes the vertices and calls _math_matrix_loadf() with the top-most matrix
325 * in the current stack and the given matrix. Marks __GLcontextRec::NewState
326 * with the dirty stack flag.
327 */
328 void GLAPIENTRY
329 _mesa_LoadMatrixf( const GLfloat *m )
330 {
331 GET_CURRENT_CONTEXT(ctx);
332 if (!m) return;
333 if (MESA_VERBOSE & VERBOSE_API)
334 _mesa_debug(ctx,
335 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
336 m[0], m[4], m[8], m[12],
337 m[1], m[5], m[9], m[13],
338 m[2], m[6], m[10], m[14],
339 m[3], m[7], m[11], m[15]);
340
341 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
342 _math_matrix_loadf( ctx->CurrentStack->Top, m );
343 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
344 }
345
346
347 /**
348 * Multiply the current matrix with a given matrix.
349 *
350 * \param m matrix.
351 *
352 * \sa glMultMatrixf().
353 *
354 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
355 * matrix in the current stack and the given matrix. Marks
356 * __GLcontextRec::NewState with the dirty stack flag.
357 */
358 void GLAPIENTRY
359 _mesa_MultMatrixf( const GLfloat *m )
360 {
361 GET_CURRENT_CONTEXT(ctx);
362 if (!m) return;
363 if (MESA_VERBOSE & VERBOSE_API)
364 _mesa_debug(ctx,
365 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
366 m[0], m[4], m[8], m[12],
367 m[1], m[5], m[9], m[13],
368 m[2], m[6], m[10], m[14],
369 m[3], m[7], m[11], m[15]);
370 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
371 _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
372 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
373 }
374
375
376 /**
377 * Multiply the current matrix with a rotation matrix.
378 *
379 * \param angle angle of rotation, in degrees.
380 * \param x rotation vector x coordinate.
381 * \param y rotation vector y coordinate.
382 * \param z rotation vector z coordinate.
383 *
384 * \sa glRotatef().
385 *
386 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
387 * matrix in the current stack and the given parameters. Marks
388 * __GLcontextRec::NewState with the dirty stack flag.
389 */
390 void GLAPIENTRY
391 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
392 {
393 GET_CURRENT_CONTEXT(ctx);
394 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
395 if (angle != 0.0F) {
396 _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
397 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
398 }
399 }
400
401
402 /**
403 * Multiply the current matrix with a general scaling matrix.
404 *
405 * \param x x axis scale factor.
406 * \param y y axis scale factor.
407 * \param z z axis scale factor.
408 *
409 * \sa glScalef().
410 *
411 * Flushes the vertices and calls _math_matrix_scale() with the top-most
412 * matrix in the current stack and the given parameters. Marks
413 * __GLcontextRec::NewState with the dirty stack flag.
414 */
415 void GLAPIENTRY
416 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
417 {
418 GET_CURRENT_CONTEXT(ctx);
419 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
420 _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
421 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
422 }
423
424
425 /**
426 * Multiply the current matrix with a general scaling matrix.
427 *
428 * \param x translation vector x coordinate.
429 * \param y translation vector y coordinate.
430 * \param z translation vector z coordinate.
431 *
432 * \sa glTranslatef().
433 *
434 * Flushes the vertices and calls _math_matrix_translate() with the top-most
435 * matrix in the current stack and the given parameters. Marks
436 * __GLcontextRec::NewState with the dirty stack flag.
437 */
438 void GLAPIENTRY
439 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
440 {
441 GET_CURRENT_CONTEXT(ctx);
442 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
443 _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
444 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
445 }
446
447
448 #if _HAVE_FULL_GL
449 void GLAPIENTRY
450 _mesa_LoadMatrixd( const GLdouble *m )
451 {
452 GLint i;
453 GLfloat f[16];
454 if (!m) return;
455 for (i = 0; i < 16; i++)
456 f[i] = (GLfloat) m[i];
457 _mesa_LoadMatrixf(f);
458 }
459
460 void GLAPIENTRY
461 _mesa_MultMatrixd( const GLdouble *m )
462 {
463 GLint i;
464 GLfloat f[16];
465 if (!m) return;
466 for (i = 0; i < 16; i++)
467 f[i] = (GLfloat) m[i];
468 _mesa_MultMatrixf( f );
469 }
470
471
472 void GLAPIENTRY
473 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
474 {
475 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
476 }
477
478
479 void GLAPIENTRY
480 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
481 {
482 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
483 }
484
485
486 void GLAPIENTRY
487 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
488 {
489 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
490 }
491 #endif
492
493
494 #if _HAVE_FULL_GL
495 void GLAPIENTRY
496 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
497 {
498 GLfloat tm[16];
499 if (!m) return;
500 _math_transposef(tm, m);
501 _mesa_LoadMatrixf(tm);
502 }
503
504
505 void GLAPIENTRY
506 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
507 {
508 GLfloat tm[16];
509 if (!m) return;
510 _math_transposefd(tm, m);
511 _mesa_LoadMatrixf(tm);
512 }
513
514
515 void GLAPIENTRY
516 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
517 {
518 GLfloat tm[16];
519 if (!m) return;
520 _math_transposef(tm, m);
521 _mesa_MultMatrixf(tm);
522 }
523
524
525 void GLAPIENTRY
526 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
527 {
528 GLfloat tm[16];
529 if (!m) return;
530 _math_transposefd(tm, m);
531 _mesa_MultMatrixf(tm);
532 }
533 #endif
534
535 /**
536 * Set the viewport.
537 *
538 * \param x, y coordinates of the lower-left corner of the viewport rectangle.
539 * \param width width of the viewport rectangle.
540 * \param height height of the viewport rectangle.
541 *
542 * \sa Called via glViewport() or display list execution.
543 *
544 * Flushes the vertices and calls _mesa_set_viewport() with the given
545 * parameters.
546 */
547 void GLAPIENTRY
548 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
549 {
550 GET_CURRENT_CONTEXT(ctx);
551 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
552 _mesa_set_viewport(ctx, x, y, width, height);
553 }
554
555 /**
556 * Set new viewport parameters and update derived state (the _WindowMap
557 * matrix). Usually called from _mesa_Viewport().
558 *
559 * \note We also call _mesa_ResizeBuffersMESA() because this is a good
560 * time to check if the window has been resized. Many device drivers
561 * can't get direct notification from the window system of size changes
562 * so this is an ad-hoc solution to that problem.
563 *
564 * \param ctx GL context.
565 * \param x, y coordinates of the lower left corner of the viewport rectangle.
566 * \param width width of the viewport rectangle.
567 * \param height height of the viewport rectangle.
568 *
569 * Verifies the parameters, clamps them to the implementation dependent range
570 * and updates __GLcontextRec::Viewport. Computes the scale and bias values for
571 * the drivers and notifies the driver via the dd_function_table::Viewport
572 * callback.
573 */
574 void
575 _mesa_set_viewport( GLcontext *ctx, GLint x, GLint y,
576 GLsizei width, GLsizei height )
577 {
578 const GLfloat n = ctx->Viewport.Near;
579 const GLfloat f = ctx->Viewport.Far;
580
581 if (MESA_VERBOSE & VERBOSE_API)
582 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
583
584 if (width < 0 || height < 0) {
585 _mesa_error( ctx, GL_INVALID_VALUE,
586 "glViewport(%d, %d, %d, %d)", x, y, width, height );
587 return;
588 }
589
590 /* clamp width, and height to implementation dependent range */
591 width = CLAMP( width, 1, MAX_WIDTH );
592 height = CLAMP( height, 1, MAX_HEIGHT );
593
594 /* Save viewport */
595 ctx->Viewport.X = x;
596 ctx->Viewport.Width = width;
597 ctx->Viewport.Y = y;
598 ctx->Viewport.Height = height;
599
600 /* XXX send transposed width/height to Driver.Viewport() below??? */
601 if (ctx->_RotateMode) {
602 GLint tmp, tmps;
603 tmp = x; x = y; y = tmp;
604 tmps = width; width = height; height = tmps;
605 }
606
607 /* compute scale and bias values :: This is really driver-specific
608 * and should be maintained elsewhere if at all. NOTE: RasterPos
609 * uses this.
610 */
611 ctx->Viewport._WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
612 ctx->Viewport._WindowMap.m[MAT_TX] = ctx->Viewport._WindowMap.m[MAT_SX] + x;
613 ctx->Viewport._WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
614 ctx->Viewport._WindowMap.m[MAT_TY] = ctx->Viewport._WindowMap.m[MAT_SY] + y;
615 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0F);
616 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0F + n);
617 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
618 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
619 ctx->NewState |= _NEW_VIEWPORT;
620
621 /* Check if window/buffer has been resized and if so, reallocate the
622 * ancillary buffers. This is an ad-hoc solution to detecting window
623 * size changes. 99% of all GL apps call glViewport when a window is
624 * resized so this is a good time to check for new window dims and
625 * reallocate color buffers and ancilliary buffers.
626 */
627 _mesa_ResizeBuffersMESA();
628
629 if (ctx->Driver.Viewport) {
630 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
631 }
632 }
633
634
635 #if _HAVE_FULL_GL
636 void GLAPIENTRY
637 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
638 {
639 /*
640 * nearval - specifies mapping of the near clipping plane to window
641 * coordinates, default is 0
642 * farval - specifies mapping of the far clipping plane to window
643 * coordinates, default is 1
644 *
645 * After clipping and div by w, z coords are in -1.0 to 1.0,
646 * corresponding to near and far clipping planes. glDepthRange
647 * specifies a linear mapping of the normalized z coords in
648 * this range to window z coords.
649 */
650 GLfloat n, f;
651 GET_CURRENT_CONTEXT(ctx);
652 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
653
654 if (MESA_VERBOSE&VERBOSE_API)
655 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
656
657 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
658 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
659
660 ctx->Viewport.Near = n;
661 ctx->Viewport.Far = f;
662 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0F);
663 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0F + n);
664 ctx->NewState |= _NEW_VIEWPORT;
665
666 if (ctx->Driver.DepthRange) {
667 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
668 }
669 }
670 #endif
671
672
673
674 /**********************************************************************/
675 /** \name State management */
676 /*@{*/
677
678
679 /**
680 * Update the projection matrix stack.
681 *
682 * \param ctx GL context.
683 *
684 * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
685 * stack, and recomputes user clip positions if necessary.
686 *
687 * \note This routine references __GLcontextRec::Tranform attribute values to
688 * compute userclip positions in clip space, but is only called on
689 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
690 * date across changes to the __GLcontextRec::Transform attributes.
691 */
692 static void
693 update_projection( GLcontext *ctx )
694 {
695 _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
696
697 #if FEATURE_userclip
698 /* Recompute clip plane positions in clipspace. This is also done
699 * in _mesa_ClipPlane().
700 */
701 if (ctx->Transform.ClipPlanesEnabled) {
702 GLuint p;
703 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
704 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
705 _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
706 ctx->Transform.EyeUserPlane[p],
707 ctx->ProjectionMatrixStack.Top->inv );
708 }
709 }
710 }
711 #endif
712 }
713
714
715 /**
716 * Calculate the combined modelview-projection matrix.
717 *
718 * \param ctx GL context.
719 *
720 * Multiplies the top matrices of the projection and model view stacks into
721 * __GLcontextRec::_ModelProjectMatrix via _math_matrix_mul_matrix() and
722 * analyzes the resulting matrix via _math_matrix_analyse().
723 */
724 static void
725 calculate_model_project_matrix( GLcontext *ctx )
726 {
727 _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
728 ctx->ProjectionMatrixStack.Top,
729 ctx->ModelviewMatrixStack.Top );
730
731 _math_matrix_analyse( &ctx->_ModelProjectMatrix );
732 }
733
734
735 /**
736 * Updates the combined modelview-projection matrix.
737 *
738 * \param ctx GL context.
739 * \param new_state new state bit mask.
740 *
741 * If there is a new model view matrix then analyzes it. If there is a new
742 * projection matrix, updates it. Finally calls
743 * calculate_model_project_matrix() to recalculate the modelview-projection
744 * matrix.
745 */
746 void _mesa_update_modelview_project( GLcontext *ctx, GLuint new_state )
747 {
748 if (new_state & _NEW_MODELVIEW)
749 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
750
751 if (new_state & _NEW_PROJECTION)
752 update_projection( ctx );
753
754 /* Keep ModelviewProject uptodate always to allow tnl
755 * implementations that go model->clip even when eye is required.
756 */
757 calculate_model_project_matrix(ctx);
758 }
759
760 /*@}*/
761
762
763 /**********************************************************************/
764 /** Matrix stack initialization */
765 /*@{*/
766
767
768 /**
769 * Initialize a matrix stack.
770 *
771 * \param stack matrix stack.
772 * \param maxDepth maximum stack depth.
773 * \param dirtyFlag dirty flag.
774 *
775 * Allocates an array of \p maxDepth elements for the matrix stack and calls
776 * _math_matrix_ctr() and _math_matrix_alloc_inv() for each element to
777 * initialize it.
778 */
779 static void
780 init_matrix_stack( struct matrix_stack *stack,
781 GLuint maxDepth, GLuint dirtyFlag )
782 {
783 GLuint i;
784
785 stack->Depth = 0;
786 stack->MaxDepth = maxDepth;
787 stack->DirtyFlag = dirtyFlag;
788 /* The stack */
789 stack->Stack = (GLmatrix *) CALLOC(maxDepth * sizeof(GLmatrix));
790 for (i = 0; i < maxDepth; i++) {
791 _math_matrix_ctr(&stack->Stack[i]);
792 _math_matrix_alloc_inv(&stack->Stack[i]);
793 }
794 stack->Top = stack->Stack;
795 }
796
797 /**
798 * Free matrix stack.
799 *
800 * \param stack matrix stack.
801 *
802 * Calls _math_matrix_dtr() for each element of the matrix stack and
803 * frees the array.
804 */
805 static void
806 free_matrix_stack( struct matrix_stack *stack )
807 {
808 GLuint i;
809 for (i = 0; i < stack->MaxDepth; i++) {
810 _math_matrix_dtr(&stack->Stack[i]);
811 }
812 FREE(stack->Stack);
813 stack->Stack = stack->Top = NULL;
814 }
815
816 /*@}*/
817
818
819 /**********************************************************************/
820 /** \name Initialization */
821 /*@{*/
822
823
824 /**
825 * Initialize the context matrix data.
826 *
827 * \param ctx GL context.
828 *
829 * Initializes each of the matrix stacks and the combined modelview-projection
830 * matrix.
831 */
832 void _mesa_init_matrix( GLcontext * ctx )
833 {
834 GLint i;
835
836 /* Initialize matrix stacks */
837 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
838 _NEW_MODELVIEW);
839 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
840 _NEW_PROJECTION);
841 init_matrix_stack(&ctx->ColorMatrixStack, MAX_COLOR_STACK_DEPTH,
842 _NEW_COLOR_MATRIX);
843 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
844 init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
845 _NEW_TEXTURE_MATRIX);
846 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
847 init_matrix_stack(&ctx->ProgramMatrixStack[i],
848 MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
849 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
850
851 /* Init combined Modelview*Projection matrix */
852 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
853 }
854
855
856 /**
857 * Free the context matrix data.
858 *
859 * \param ctx GL context.
860 *
861 * Frees each of the matrix stacks and the combined modelview-projection
862 * matrix.
863 */
864 void _mesa_free_matrix_data( GLcontext *ctx )
865 {
866 GLint i;
867
868 free_matrix_stack(&ctx->ModelviewMatrixStack);
869 free_matrix_stack(&ctx->ProjectionMatrixStack);
870 free_matrix_stack(&ctx->ColorMatrixStack);
871 for (i = 0; i < MAX_TEXTURE_UNITS; i++)
872 free_matrix_stack(&ctx->TextureMatrixStack[i]);
873 for (i = 0; i < MAX_PROGRAM_MATRICES; i++)
874 free_matrix_stack(&ctx->ProgramMatrixStack[i]);
875 /* combined Modelview*Projection matrix */
876 _math_matrix_dtr( &ctx->_ModelProjectMatrix );
877
878 }
879
880
881 /**
882 * Initialize the context transform attribute group.
883 *
884 * \param ctx GL context.
885 *
886 * \todo Move this to a new file with other 'transform' routines.
887 */
888 void _mesa_init_transform( GLcontext *ctx )
889 {
890 GLint i;
891
892 /* Transformation group */
893 ctx->Transform.MatrixMode = GL_MODELVIEW;
894 ctx->Transform.Normalize = GL_FALSE;
895 ctx->Transform.RescaleNormals = GL_FALSE;
896 ctx->Transform.RasterPositionUnclipped = GL_FALSE;
897 for (i=0;i<MAX_CLIP_PLANES;i++) {
898 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
899 }
900 ctx->Transform.ClipPlanesEnabled = 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 /*@}*/