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