matrix.c revision af69d88d
17117f1b4Smrg/* 27117f1b4Smrg * Mesa 3-D graphics library 37117f1b4Smrg * 44a49301eSmrg * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 54a49301eSmrg * Copyright (C) 2009 VMware, Inc. All Rights Reserved. 67117f1b4Smrg * 77117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a 87117f1b4Smrg * copy of this software and associated documentation files (the "Software"), 97117f1b4Smrg * to deal in the Software without restriction, including without limitation 107117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 117117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the 127117f1b4Smrg * Software is furnished to do so, subject to the following conditions: 137117f1b4Smrg * 147117f1b4Smrg * The above copyright notice and this permission notice shall be included 157117f1b4Smrg * in all copies or substantial portions of the Software. 167117f1b4Smrg * 177117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 187117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 197117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE. 247117f1b4Smrg */ 257117f1b4Smrg 267117f1b4Smrg 277117f1b4Smrg/** 287117f1b4Smrg * \file matrix.c 297117f1b4Smrg * Matrix operations. 307117f1b4Smrg * 317117f1b4Smrg * \note 327117f1b4Smrg * -# 4x4 transformation matrices are stored in memory in column major order. 337117f1b4Smrg * -# Points/vertices are to be thought of as column vectors. 347117f1b4Smrg * -# Transformation of a point p by a matrix M is: p' = M * p 357117f1b4Smrg */ 367117f1b4Smrg 377117f1b4Smrg 387117f1b4Smrg#include "glheader.h" 397117f1b4Smrg#include "imports.h" 407117f1b4Smrg#include "context.h" 417117f1b4Smrg#include "enums.h" 427117f1b4Smrg#include "macros.h" 437117f1b4Smrg#include "matrix.h" 447117f1b4Smrg#include "mtypes.h" 457117f1b4Smrg#include "math/m_matrix.h" 467117f1b4Smrg 477117f1b4Smrg 487117f1b4Smrg/** 497117f1b4Smrg * Apply a perspective projection matrix. 507117f1b4Smrg * 517117f1b4Smrg * \param left left clipping plane coordinate. 527117f1b4Smrg * \param right right clipping plane coordinate. 537117f1b4Smrg * \param bottom bottom clipping plane coordinate. 547117f1b4Smrg * \param top top clipping plane coordinate. 557117f1b4Smrg * \param nearval distance to the near clipping plane. 567117f1b4Smrg * \param farval distance to the far clipping plane. 577117f1b4Smrg * 587117f1b4Smrg * \sa glFrustum(). 597117f1b4Smrg * 607117f1b4Smrg * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with 617117f1b4Smrg * the top matrix of the current matrix stack and sets 623464ebd5Sriastradh * __struct gl_contextRec::NewState. 637117f1b4Smrg */ 647117f1b4Smrgvoid GLAPIENTRY 657117f1b4Smrg_mesa_Frustum( GLdouble left, GLdouble right, 667117f1b4Smrg GLdouble bottom, GLdouble top, 677117f1b4Smrg GLdouble nearval, GLdouble farval ) 687117f1b4Smrg{ 697117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 70af69d88dSmrg 71af69d88dSmrg FLUSH_VERTICES(ctx, 0); 727117f1b4Smrg 737117f1b4Smrg if (nearval <= 0.0 || 747117f1b4Smrg farval <= 0.0 || 757117f1b4Smrg nearval == farval || 767117f1b4Smrg left == right || 777117f1b4Smrg top == bottom) 787117f1b4Smrg { 797117f1b4Smrg _mesa_error( ctx, GL_INVALID_VALUE, "glFrustum" ); 807117f1b4Smrg return; 817117f1b4Smrg } 827117f1b4Smrg 837117f1b4Smrg _math_matrix_frustum( ctx->CurrentStack->Top, 847117f1b4Smrg (GLfloat) left, (GLfloat) right, 857117f1b4Smrg (GLfloat) bottom, (GLfloat) top, 867117f1b4Smrg (GLfloat) nearval, (GLfloat) farval ); 877117f1b4Smrg ctx->NewState |= ctx->CurrentStack->DirtyFlag; 887117f1b4Smrg} 897117f1b4Smrg 907117f1b4Smrg 917117f1b4Smrg/** 927117f1b4Smrg * Apply an orthographic projection matrix. 937117f1b4Smrg * 947117f1b4Smrg * \param left left clipping plane coordinate. 957117f1b4Smrg * \param right right clipping plane coordinate. 967117f1b4Smrg * \param bottom bottom clipping plane coordinate. 977117f1b4Smrg * \param top top clipping plane coordinate. 987117f1b4Smrg * \param nearval distance to the near clipping plane. 997117f1b4Smrg * \param farval distance to the far clipping plane. 1007117f1b4Smrg * 1017117f1b4Smrg * \sa glOrtho(). 1027117f1b4Smrg * 1037117f1b4Smrg * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with 1047117f1b4Smrg * the top matrix of the current matrix stack and sets 1053464ebd5Sriastradh * __struct gl_contextRec::NewState. 1067117f1b4Smrg */ 1077117f1b4Smrgvoid GLAPIENTRY 1087117f1b4Smrg_mesa_Ortho( GLdouble left, GLdouble right, 1097117f1b4Smrg GLdouble bottom, GLdouble top, 1107117f1b4Smrg GLdouble nearval, GLdouble farval ) 1117117f1b4Smrg{ 1127117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 113af69d88dSmrg 114af69d88dSmrg FLUSH_VERTICES(ctx, 0); 1157117f1b4Smrg 1167117f1b4Smrg if (MESA_VERBOSE & VERBOSE_API) 1177117f1b4Smrg _mesa_debug(ctx, "glOrtho(%f, %f, %f, %f, %f, %f)\n", 1187117f1b4Smrg left, right, bottom, top, nearval, farval); 1197117f1b4Smrg 1207117f1b4Smrg if (left == right || 1217117f1b4Smrg bottom == top || 1227117f1b4Smrg nearval == farval) 1237117f1b4Smrg { 1247117f1b4Smrg _mesa_error( ctx, GL_INVALID_VALUE, "glOrtho" ); 1257117f1b4Smrg return; 1267117f1b4Smrg } 1277117f1b4Smrg 1287117f1b4Smrg _math_matrix_ortho( ctx->CurrentStack->Top, 1297117f1b4Smrg (GLfloat) left, (GLfloat) right, 1307117f1b4Smrg (GLfloat) bottom, (GLfloat) top, 1317117f1b4Smrg (GLfloat) nearval, (GLfloat) farval ); 1327117f1b4Smrg ctx->NewState |= ctx->CurrentStack->DirtyFlag; 1337117f1b4Smrg} 1347117f1b4Smrg 1357117f1b4Smrg 1367117f1b4Smrg/** 1377117f1b4Smrg * Set the current matrix stack. 1387117f1b4Smrg * 1397117f1b4Smrg * \param mode matrix stack. 1407117f1b4Smrg * 1417117f1b4Smrg * \sa glMatrixMode(). 1427117f1b4Smrg * 1437117f1b4Smrg * Flushes the vertices, validates the parameter and updates 1443464ebd5Sriastradh * __struct gl_contextRec::CurrentStack and gl_transform_attrib::MatrixMode 1453464ebd5Sriastradh * with the specified matrix stack. 1467117f1b4Smrg */ 1477117f1b4Smrgvoid GLAPIENTRY 1487117f1b4Smrg_mesa_MatrixMode( GLenum mode ) 1497117f1b4Smrg{ 1507117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 1517117f1b4Smrg 1527117f1b4Smrg if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE) 1537117f1b4Smrg return; 1547117f1b4Smrg FLUSH_VERTICES(ctx, _NEW_TRANSFORM); 1557117f1b4Smrg 1567117f1b4Smrg switch (mode) { 1577117f1b4Smrg case GL_MODELVIEW: 1587117f1b4Smrg ctx->CurrentStack = &ctx->ModelviewMatrixStack; 1597117f1b4Smrg break; 1607117f1b4Smrg case GL_PROJECTION: 1617117f1b4Smrg ctx->CurrentStack = &ctx->ProjectionMatrixStack; 1627117f1b4Smrg break; 1637117f1b4Smrg case GL_TEXTURE: 1644a49301eSmrg /* This error check is disabled because if we're called from 1654a49301eSmrg * glPopAttrib() when the active texture unit is >= MaxTextureCoordUnits 1664a49301eSmrg * we'll generate an unexpected error. 1674a49301eSmrg * From the GL_ARB_vertex_shader spec it sounds like we should instead 1684a49301eSmrg * do error checking in other places when we actually try to access 1694a49301eSmrg * texture matrices beyond MaxTextureCoordUnits. 1704a49301eSmrg */ 1714a49301eSmrg#if 0 1727117f1b4Smrg if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) { 1733464ebd5Sriastradh _mesa_error(ctx, GL_INVALID_OPERATION, 1743464ebd5Sriastradh "glMatrixMode(invalid tex unit %d)", 1754a49301eSmrg ctx->Texture.CurrentUnit); 1767117f1b4Smrg return; 1777117f1b4Smrg } 1784a49301eSmrg#endif 1794a49301eSmrg ASSERT(ctx->Texture.CurrentUnit < Elements(ctx->TextureMatrixStack)); 1807117f1b4Smrg ctx->CurrentStack = &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit]; 1817117f1b4Smrg break; 1827117f1b4Smrg case GL_MATRIX0_ARB: 1837117f1b4Smrg case GL_MATRIX1_ARB: 1847117f1b4Smrg case GL_MATRIX2_ARB: 1857117f1b4Smrg case GL_MATRIX3_ARB: 1867117f1b4Smrg case GL_MATRIX4_ARB: 1877117f1b4Smrg case GL_MATRIX5_ARB: 1887117f1b4Smrg case GL_MATRIX6_ARB: 1897117f1b4Smrg case GL_MATRIX7_ARB: 190af69d88dSmrg if (ctx->API == API_OPENGL_COMPAT 191af69d88dSmrg && (ctx->Extensions.ARB_vertex_program || 192af69d88dSmrg ctx->Extensions.ARB_fragment_program)) { 1937117f1b4Smrg const GLuint m = mode - GL_MATRIX0_ARB; 1947117f1b4Smrg if (m > ctx->Const.MaxProgramMatrices) { 1957117f1b4Smrg _mesa_error(ctx, GL_INVALID_ENUM, 1967117f1b4Smrg "glMatrixMode(GL_MATRIX%d_ARB)", m); 1977117f1b4Smrg return; 1987117f1b4Smrg } 1997117f1b4Smrg ctx->CurrentStack = &ctx->ProgramMatrixStack[m]; 2007117f1b4Smrg } 2017117f1b4Smrg else { 2027117f1b4Smrg _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" ); 2037117f1b4Smrg return; 2047117f1b4Smrg } 2057117f1b4Smrg break; 2067117f1b4Smrg default: 2077117f1b4Smrg _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode(mode)" ); 2087117f1b4Smrg return; 2097117f1b4Smrg } 2107117f1b4Smrg 2117117f1b4Smrg ctx->Transform.MatrixMode = mode; 2127117f1b4Smrg} 2137117f1b4Smrg 2147117f1b4Smrg 2157117f1b4Smrg/** 2167117f1b4Smrg * Push the current matrix stack. 2177117f1b4Smrg * 2187117f1b4Smrg * \sa glPushMatrix(). 2197117f1b4Smrg * 2207117f1b4Smrg * Verifies the current matrix stack is not full, and duplicates the top-most 2213464ebd5Sriastradh * matrix in the stack. 2223464ebd5Sriastradh * Marks __struct gl_contextRec::NewState with the stack dirty flag. 2237117f1b4Smrg */ 2247117f1b4Smrgvoid GLAPIENTRY 2257117f1b4Smrg_mesa_PushMatrix( void ) 2267117f1b4Smrg{ 2277117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 2287117f1b4Smrg struct gl_matrix_stack *stack = ctx->CurrentStack; 2297117f1b4Smrg 2307117f1b4Smrg if (MESA_VERBOSE&VERBOSE_API) 2317117f1b4Smrg _mesa_debug(ctx, "glPushMatrix %s\n", 2327117f1b4Smrg _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode)); 2337117f1b4Smrg 2347117f1b4Smrg if (stack->Depth + 1 >= stack->MaxDepth) { 2357117f1b4Smrg if (ctx->Transform.MatrixMode == GL_TEXTURE) { 2367117f1b4Smrg _mesa_error(ctx, GL_STACK_OVERFLOW, 2377117f1b4Smrg "glPushMatrix(mode=GL_TEXTURE, unit=%d)", 2387117f1b4Smrg ctx->Texture.CurrentUnit); 2397117f1b4Smrg } 2407117f1b4Smrg else { 2417117f1b4Smrg _mesa_error(ctx, GL_STACK_OVERFLOW, "glPushMatrix(mode=%s)", 2427117f1b4Smrg _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode)); 2437117f1b4Smrg } 2447117f1b4Smrg return; 2457117f1b4Smrg } 2467117f1b4Smrg _math_matrix_copy( &stack->Stack[stack->Depth + 1], 2477117f1b4Smrg &stack->Stack[stack->Depth] ); 2487117f1b4Smrg stack->Depth++; 2497117f1b4Smrg stack->Top = &(stack->Stack[stack->Depth]); 2507117f1b4Smrg ctx->NewState |= stack->DirtyFlag; 2517117f1b4Smrg} 2527117f1b4Smrg 2537117f1b4Smrg 2547117f1b4Smrg/** 2557117f1b4Smrg * Pop the current matrix stack. 2567117f1b4Smrg * 2577117f1b4Smrg * \sa glPopMatrix(). 2587117f1b4Smrg * 2597117f1b4Smrg * Flushes the vertices, verifies the current matrix stack is not empty, and 2603464ebd5Sriastradh * moves the stack head down. 2613464ebd5Sriastradh * Marks __struct gl_contextRec::NewState with the dirty stack flag. 2627117f1b4Smrg */ 2637117f1b4Smrgvoid GLAPIENTRY 2647117f1b4Smrg_mesa_PopMatrix( void ) 2657117f1b4Smrg{ 2667117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 2677117f1b4Smrg struct gl_matrix_stack *stack = ctx->CurrentStack; 268af69d88dSmrg 269af69d88dSmrg FLUSH_VERTICES(ctx, 0); 2707117f1b4Smrg 2717117f1b4Smrg if (MESA_VERBOSE&VERBOSE_API) 2727117f1b4Smrg _mesa_debug(ctx, "glPopMatrix %s\n", 2737117f1b4Smrg _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode)); 2747117f1b4Smrg 2757117f1b4Smrg if (stack->Depth == 0) { 2767117f1b4Smrg if (ctx->Transform.MatrixMode == GL_TEXTURE) { 2777117f1b4Smrg _mesa_error(ctx, GL_STACK_UNDERFLOW, 2787117f1b4Smrg "glPopMatrix(mode=GL_TEXTURE, unit=%d)", 2797117f1b4Smrg ctx->Texture.CurrentUnit); 2807117f1b4Smrg } 2817117f1b4Smrg else { 2827117f1b4Smrg _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)", 2837117f1b4Smrg _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode)); 2847117f1b4Smrg } 2857117f1b4Smrg return; 2867117f1b4Smrg } 2877117f1b4Smrg stack->Depth--; 2887117f1b4Smrg stack->Top = &(stack->Stack[stack->Depth]); 2897117f1b4Smrg ctx->NewState |= stack->DirtyFlag; 2907117f1b4Smrg} 2917117f1b4Smrg 2927117f1b4Smrg 2937117f1b4Smrg/** 2947117f1b4Smrg * Replace the current matrix with the identity matrix. 2957117f1b4Smrg * 2967117f1b4Smrg * \sa glLoadIdentity(). 2977117f1b4Smrg * 2983464ebd5Sriastradh * Flushes the vertices and calls _math_matrix_set_identity() with the 2993464ebd5Sriastradh * top-most matrix in the current stack. 3003464ebd5Sriastradh * Marks __struct gl_contextRec::NewState with the stack dirty flag. 3017117f1b4Smrg */ 3027117f1b4Smrgvoid GLAPIENTRY 3037117f1b4Smrg_mesa_LoadIdentity( void ) 3047117f1b4Smrg{ 3057117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 306af69d88dSmrg 307af69d88dSmrg FLUSH_VERTICES(ctx, 0); 3087117f1b4Smrg 3097117f1b4Smrg if (MESA_VERBOSE & VERBOSE_API) 310cdc920a0Smrg _mesa_debug(ctx, "glLoadIdentity()\n"); 3117117f1b4Smrg 3127117f1b4Smrg _math_matrix_set_identity( ctx->CurrentStack->Top ); 3137117f1b4Smrg ctx->NewState |= ctx->CurrentStack->DirtyFlag; 3147117f1b4Smrg} 3157117f1b4Smrg 3167117f1b4Smrg 3177117f1b4Smrg/** 3187117f1b4Smrg * Replace the current matrix with a given matrix. 3197117f1b4Smrg * 3207117f1b4Smrg * \param m matrix. 3217117f1b4Smrg * 3227117f1b4Smrg * \sa glLoadMatrixf(). 3237117f1b4Smrg * 3243464ebd5Sriastradh * Flushes the vertices and calls _math_matrix_loadf() with the top-most 3253464ebd5Sriastradh * matrix in the current stack and the given matrix. 3263464ebd5Sriastradh * Marks __struct gl_contextRec::NewState with the dirty stack flag. 3277117f1b4Smrg */ 3287117f1b4Smrgvoid GLAPIENTRY 3297117f1b4Smrg_mesa_LoadMatrixf( const GLfloat *m ) 3307117f1b4Smrg{ 3317117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 3327117f1b4Smrg if (!m) return; 3337117f1b4Smrg if (MESA_VERBOSE & VERBOSE_API) 3347117f1b4Smrg _mesa_debug(ctx, 3357117f1b4Smrg "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n", 3367117f1b4Smrg m[0], m[4], m[8], m[12], 3377117f1b4Smrg m[1], m[5], m[9], m[13], 3387117f1b4Smrg m[2], m[6], m[10], m[14], 3397117f1b4Smrg m[3], m[7], m[11], m[15]); 3407117f1b4Smrg 341af69d88dSmrg FLUSH_VERTICES(ctx, 0); 3427117f1b4Smrg _math_matrix_loadf( ctx->CurrentStack->Top, m ); 3437117f1b4Smrg ctx->NewState |= ctx->CurrentStack->DirtyFlag; 3447117f1b4Smrg} 3457117f1b4Smrg 3467117f1b4Smrg 3477117f1b4Smrg/** 3487117f1b4Smrg * Multiply the current matrix with a given matrix. 3497117f1b4Smrg * 3507117f1b4Smrg * \param m matrix. 3517117f1b4Smrg * 3527117f1b4Smrg * \sa glMultMatrixf(). 3537117f1b4Smrg * 3547117f1b4Smrg * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most 3557117f1b4Smrg * matrix in the current stack and the given matrix. Marks 3563464ebd5Sriastradh * __struct gl_contextRec::NewState with the dirty stack flag. 3577117f1b4Smrg */ 3587117f1b4Smrgvoid GLAPIENTRY 3597117f1b4Smrg_mesa_MultMatrixf( const GLfloat *m ) 3607117f1b4Smrg{ 3617117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 3627117f1b4Smrg if (!m) return; 3637117f1b4Smrg if (MESA_VERBOSE & VERBOSE_API) 3647117f1b4Smrg _mesa_debug(ctx, 3657117f1b4Smrg "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n", 3667117f1b4Smrg m[0], m[4], m[8], m[12], 3677117f1b4Smrg m[1], m[5], m[9], m[13], 3687117f1b4Smrg m[2], m[6], m[10], m[14], 3697117f1b4Smrg m[3], m[7], m[11], m[15]); 370af69d88dSmrg 371af69d88dSmrg FLUSH_VERTICES(ctx, 0); 3727117f1b4Smrg _math_matrix_mul_floats( ctx->CurrentStack->Top, m ); 3737117f1b4Smrg ctx->NewState |= ctx->CurrentStack->DirtyFlag; 3747117f1b4Smrg} 3757117f1b4Smrg 3767117f1b4Smrg 3777117f1b4Smrg/** 3787117f1b4Smrg * Multiply the current matrix with a rotation matrix. 3797117f1b4Smrg * 3807117f1b4Smrg * \param angle angle of rotation, in degrees. 3817117f1b4Smrg * \param x rotation vector x coordinate. 3827117f1b4Smrg * \param y rotation vector y coordinate. 3837117f1b4Smrg * \param z rotation vector z coordinate. 3847117f1b4Smrg * 3857117f1b4Smrg * \sa glRotatef(). 3867117f1b4Smrg * 3877117f1b4Smrg * Flushes the vertices and calls _math_matrix_rotate() with the top-most 3887117f1b4Smrg * matrix in the current stack and the given parameters. Marks 3893464ebd5Sriastradh * __struct gl_contextRec::NewState with the dirty stack flag. 3907117f1b4Smrg */ 3917117f1b4Smrgvoid GLAPIENTRY 3927117f1b4Smrg_mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z ) 3937117f1b4Smrg{ 3947117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 395af69d88dSmrg 396af69d88dSmrg FLUSH_VERTICES(ctx, 0); 3977117f1b4Smrg if (angle != 0.0F) { 3987117f1b4Smrg _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z); 3997117f1b4Smrg ctx->NewState |= ctx->CurrentStack->DirtyFlag; 4007117f1b4Smrg } 4017117f1b4Smrg} 4027117f1b4Smrg 4037117f1b4Smrg 4047117f1b4Smrg/** 4057117f1b4Smrg * Multiply the current matrix with a general scaling matrix. 4067117f1b4Smrg * 4077117f1b4Smrg * \param x x axis scale factor. 4087117f1b4Smrg * \param y y axis scale factor. 4097117f1b4Smrg * \param z z axis scale factor. 4107117f1b4Smrg * 4117117f1b4Smrg * \sa glScalef(). 4127117f1b4Smrg * 4137117f1b4Smrg * Flushes the vertices and calls _math_matrix_scale() with the top-most 4147117f1b4Smrg * matrix in the current stack and the given parameters. Marks 4153464ebd5Sriastradh * __struct gl_contextRec::NewState with the dirty stack flag. 4167117f1b4Smrg */ 4177117f1b4Smrgvoid GLAPIENTRY 4187117f1b4Smrg_mesa_Scalef( GLfloat x, GLfloat y, GLfloat z ) 4197117f1b4Smrg{ 4207117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 421af69d88dSmrg 422af69d88dSmrg FLUSH_VERTICES(ctx, 0); 4237117f1b4Smrg _math_matrix_scale( ctx->CurrentStack->Top, x, y, z); 4247117f1b4Smrg ctx->NewState |= ctx->CurrentStack->DirtyFlag; 4257117f1b4Smrg} 4267117f1b4Smrg 4277117f1b4Smrg 4287117f1b4Smrg/** 4297117f1b4Smrg * Multiply the current matrix with a translation matrix. 4307117f1b4Smrg * 4317117f1b4Smrg * \param x translation vector x coordinate. 4327117f1b4Smrg * \param y translation vector y coordinate. 4337117f1b4Smrg * \param z translation vector z coordinate. 4347117f1b4Smrg * 4357117f1b4Smrg * \sa glTranslatef(). 4367117f1b4Smrg * 4377117f1b4Smrg * Flushes the vertices and calls _math_matrix_translate() with the top-most 4387117f1b4Smrg * matrix in the current stack and the given parameters. Marks 4393464ebd5Sriastradh * __struct gl_contextRec::NewState with the dirty stack flag. 4407117f1b4Smrg */ 4417117f1b4Smrgvoid GLAPIENTRY 4427117f1b4Smrg_mesa_Translatef( GLfloat x, GLfloat y, GLfloat z ) 4437117f1b4Smrg{ 4447117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 445af69d88dSmrg 446af69d88dSmrg FLUSH_VERTICES(ctx, 0); 4477117f1b4Smrg _math_matrix_translate( ctx->CurrentStack->Top, x, y, z); 4487117f1b4Smrg ctx->NewState |= ctx->CurrentStack->DirtyFlag; 4497117f1b4Smrg} 4507117f1b4Smrg 4517117f1b4Smrg 4527117f1b4Smrgvoid GLAPIENTRY 4537117f1b4Smrg_mesa_LoadMatrixd( const GLdouble *m ) 4547117f1b4Smrg{ 4557117f1b4Smrg GLint i; 4567117f1b4Smrg GLfloat f[16]; 4577117f1b4Smrg if (!m) return; 4587117f1b4Smrg for (i = 0; i < 16; i++) 4597117f1b4Smrg f[i] = (GLfloat) m[i]; 4607117f1b4Smrg _mesa_LoadMatrixf(f); 4617117f1b4Smrg} 4627117f1b4Smrg 4637117f1b4Smrgvoid GLAPIENTRY 4647117f1b4Smrg_mesa_MultMatrixd( const GLdouble *m ) 4657117f1b4Smrg{ 4667117f1b4Smrg GLint i; 4677117f1b4Smrg GLfloat f[16]; 4687117f1b4Smrg if (!m) return; 4697117f1b4Smrg for (i = 0; i < 16; i++) 4707117f1b4Smrg f[i] = (GLfloat) m[i]; 4717117f1b4Smrg _mesa_MultMatrixf( f ); 4727117f1b4Smrg} 4737117f1b4Smrg 4747117f1b4Smrg 4757117f1b4Smrgvoid GLAPIENTRY 4767117f1b4Smrg_mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z ) 4777117f1b4Smrg{ 4787117f1b4Smrg _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z); 4797117f1b4Smrg} 4807117f1b4Smrg 4817117f1b4Smrg 4827117f1b4Smrgvoid GLAPIENTRY 4837117f1b4Smrg_mesa_Scaled( GLdouble x, GLdouble y, GLdouble z ) 4847117f1b4Smrg{ 4857117f1b4Smrg _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z); 4867117f1b4Smrg} 4877117f1b4Smrg 4887117f1b4Smrg 4897117f1b4Smrgvoid GLAPIENTRY 4907117f1b4Smrg_mesa_Translated( GLdouble x, GLdouble y, GLdouble z ) 4917117f1b4Smrg{ 4927117f1b4Smrg _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z); 4937117f1b4Smrg} 4947117f1b4Smrg 4957117f1b4Smrg 4967117f1b4Smrgvoid GLAPIENTRY 497af69d88dSmrg_mesa_LoadTransposeMatrixf( const GLfloat *m ) 4987117f1b4Smrg{ 4997117f1b4Smrg GLfloat tm[16]; 5007117f1b4Smrg if (!m) return; 5017117f1b4Smrg _math_transposef(tm, m); 5027117f1b4Smrg _mesa_LoadMatrixf(tm); 5037117f1b4Smrg} 5047117f1b4Smrg 5057117f1b4Smrg 5067117f1b4Smrgvoid GLAPIENTRY 507af69d88dSmrg_mesa_LoadTransposeMatrixd( const GLdouble *m ) 5087117f1b4Smrg{ 5097117f1b4Smrg GLfloat tm[16]; 5107117f1b4Smrg if (!m) return; 5117117f1b4Smrg _math_transposefd(tm, m); 5127117f1b4Smrg _mesa_LoadMatrixf(tm); 5137117f1b4Smrg} 5147117f1b4Smrg 5157117f1b4Smrg 5167117f1b4Smrgvoid GLAPIENTRY 517af69d88dSmrg_mesa_MultTransposeMatrixf( const GLfloat *m ) 5187117f1b4Smrg{ 5197117f1b4Smrg GLfloat tm[16]; 5207117f1b4Smrg if (!m) return; 5217117f1b4Smrg _math_transposef(tm, m); 5227117f1b4Smrg _mesa_MultMatrixf(tm); 5237117f1b4Smrg} 5247117f1b4Smrg 5257117f1b4Smrg 5267117f1b4Smrgvoid GLAPIENTRY 527af69d88dSmrg_mesa_MultTransposeMatrixd( const GLdouble *m ) 5287117f1b4Smrg{ 5297117f1b4Smrg GLfloat tm[16]; 5307117f1b4Smrg if (!m) return; 5317117f1b4Smrg _math_transposefd(tm, m); 5327117f1b4Smrg _mesa_MultMatrixf(tm); 5337117f1b4Smrg} 5347117f1b4Smrg 5357117f1b4Smrg 5367117f1b4Smrg 5377117f1b4Smrg/**********************************************************************/ 5387117f1b4Smrg/** \name State management */ 5397117f1b4Smrg/*@{*/ 5407117f1b4Smrg 5417117f1b4Smrg 5427117f1b4Smrg/** 5437117f1b4Smrg * Update the projection matrix stack. 5447117f1b4Smrg * 5457117f1b4Smrg * \param ctx GL context. 5467117f1b4Smrg * 5477117f1b4Smrg * Calls _math_matrix_analyse() with the top-matrix of the projection matrix 5487117f1b4Smrg * stack, and recomputes user clip positions if necessary. 5497117f1b4Smrg * 5503464ebd5Sriastradh * \note This routine references __struct gl_contextRec::Tranform attribute 5513464ebd5Sriastradh * values to compute userclip positions in clip space, but is only called on 5527117f1b4Smrg * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to 5533464ebd5Sriastradh * date across changes to the __struct gl_contextRec::Transform attributes. 5547117f1b4Smrg */ 5557117f1b4Smrgstatic void 5563464ebd5Sriastradhupdate_projection( struct gl_context *ctx ) 5577117f1b4Smrg{ 5587117f1b4Smrg _math_matrix_analyse( ctx->ProjectionMatrixStack.Top ); 5597117f1b4Smrg 5607117f1b4Smrg /* Recompute clip plane positions in clipspace. This is also done 5617117f1b4Smrg * in _mesa_ClipPlane(). 5627117f1b4Smrg */ 5637117f1b4Smrg if (ctx->Transform.ClipPlanesEnabled) { 5647117f1b4Smrg GLuint p; 5657117f1b4Smrg for (p = 0; p < ctx->Const.MaxClipPlanes; p++) { 5667117f1b4Smrg if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { 5677117f1b4Smrg _mesa_transform_vector( ctx->Transform._ClipUserPlane[p], 5687117f1b4Smrg ctx->Transform.EyeUserPlane[p], 5697117f1b4Smrg ctx->ProjectionMatrixStack.Top->inv ); 5707117f1b4Smrg } 5717117f1b4Smrg } 5727117f1b4Smrg } 5737117f1b4Smrg} 5747117f1b4Smrg 5757117f1b4Smrg 5767117f1b4Smrg/** 5777117f1b4Smrg * Calculate the combined modelview-projection matrix. 5787117f1b4Smrg * 5797117f1b4Smrg * \param ctx GL context. 5807117f1b4Smrg * 5817117f1b4Smrg * Multiplies the top matrices of the projection and model view stacks into 5823464ebd5Sriastradh * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix() 5833464ebd5Sriastradh * and analyzes the resulting matrix via _math_matrix_analyse(). 5847117f1b4Smrg */ 5857117f1b4Smrgstatic void 5863464ebd5Sriastradhcalculate_model_project_matrix( struct gl_context *ctx ) 5877117f1b4Smrg{ 5887117f1b4Smrg _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix, 5897117f1b4Smrg ctx->ProjectionMatrixStack.Top, 5907117f1b4Smrg ctx->ModelviewMatrixStack.Top ); 5917117f1b4Smrg 5927117f1b4Smrg _math_matrix_analyse( &ctx->_ModelProjectMatrix ); 5937117f1b4Smrg} 5947117f1b4Smrg 5957117f1b4Smrg 5967117f1b4Smrg/** 5977117f1b4Smrg * Updates the combined modelview-projection matrix. 5987117f1b4Smrg * 5997117f1b4Smrg * \param ctx GL context. 6007117f1b4Smrg * \param new_state new state bit mask. 6017117f1b4Smrg * 6027117f1b4Smrg * If there is a new model view matrix then analyzes it. If there is a new 6037117f1b4Smrg * projection matrix, updates it. Finally calls 6047117f1b4Smrg * calculate_model_project_matrix() to recalculate the modelview-projection 6057117f1b4Smrg * matrix. 6067117f1b4Smrg */ 6073464ebd5Sriastradhvoid _mesa_update_modelview_project( struct gl_context *ctx, GLuint new_state ) 6087117f1b4Smrg{ 609af69d88dSmrg if (new_state & _NEW_MODELVIEW) 6107117f1b4Smrg _math_matrix_analyse( ctx->ModelviewMatrixStack.Top ); 6117117f1b4Smrg 6127117f1b4Smrg if (new_state & _NEW_PROJECTION) 6137117f1b4Smrg update_projection( ctx ); 6147117f1b4Smrg 6153464ebd5Sriastradh /* Keep ModelviewProject up to date always to allow tnl 6167117f1b4Smrg * implementations that go model->clip even when eye is required. 6177117f1b4Smrg */ 6187117f1b4Smrg calculate_model_project_matrix(ctx); 6197117f1b4Smrg} 6207117f1b4Smrg 6217117f1b4Smrg/*@}*/ 6227117f1b4Smrg 6237117f1b4Smrg 6247117f1b4Smrg/**********************************************************************/ 6257117f1b4Smrg/** Matrix stack initialization */ 6267117f1b4Smrg/*@{*/ 6277117f1b4Smrg 6287117f1b4Smrg 6297117f1b4Smrg/** 6307117f1b4Smrg * Initialize a matrix stack. 6317117f1b4Smrg * 6327117f1b4Smrg * \param stack matrix stack. 6337117f1b4Smrg * \param maxDepth maximum stack depth. 6347117f1b4Smrg * \param dirtyFlag dirty flag. 6357117f1b4Smrg * 6367117f1b4Smrg * Allocates an array of \p maxDepth elements for the matrix stack and calls 637af69d88dSmrg * _math_matrix_ctr() for each element to initialize it. 6387117f1b4Smrg */ 6397117f1b4Smrgstatic void 6407117f1b4Smrginit_matrix_stack( struct gl_matrix_stack *stack, 6417117f1b4Smrg GLuint maxDepth, GLuint dirtyFlag ) 6427117f1b4Smrg{ 6437117f1b4Smrg GLuint i; 6447117f1b4Smrg 6457117f1b4Smrg stack->Depth = 0; 6467117f1b4Smrg stack->MaxDepth = maxDepth; 6477117f1b4Smrg stack->DirtyFlag = dirtyFlag; 6487117f1b4Smrg /* The stack */ 649af69d88dSmrg stack->Stack = calloc(maxDepth, sizeof(GLmatrix)); 6507117f1b4Smrg for (i = 0; i < maxDepth; i++) { 6517117f1b4Smrg _math_matrix_ctr(&stack->Stack[i]); 6527117f1b4Smrg } 6537117f1b4Smrg stack->Top = stack->Stack; 6547117f1b4Smrg} 6557117f1b4Smrg 6567117f1b4Smrg/** 6577117f1b4Smrg * Free matrix stack. 6587117f1b4Smrg * 6597117f1b4Smrg * \param stack matrix stack. 6607117f1b4Smrg * 6617117f1b4Smrg * Calls _math_matrix_dtr() for each element of the matrix stack and 6627117f1b4Smrg * frees the array. 6637117f1b4Smrg */ 6647117f1b4Smrgstatic void 6657117f1b4Smrgfree_matrix_stack( struct gl_matrix_stack *stack ) 6667117f1b4Smrg{ 6677117f1b4Smrg GLuint i; 6687117f1b4Smrg for (i = 0; i < stack->MaxDepth; i++) { 6697117f1b4Smrg _math_matrix_dtr(&stack->Stack[i]); 6707117f1b4Smrg } 671af69d88dSmrg free(stack->Stack); 6727117f1b4Smrg stack->Stack = stack->Top = NULL; 6737117f1b4Smrg} 6747117f1b4Smrg 6757117f1b4Smrg/*@}*/ 6767117f1b4Smrg 6777117f1b4Smrg 6787117f1b4Smrg/**********************************************************************/ 6797117f1b4Smrg/** \name Initialization */ 6807117f1b4Smrg/*@{*/ 6817117f1b4Smrg 6827117f1b4Smrg 6837117f1b4Smrg/** 6847117f1b4Smrg * Initialize the context matrix data. 6857117f1b4Smrg * 6867117f1b4Smrg * \param ctx GL context. 6877117f1b4Smrg * 6887117f1b4Smrg * Initializes each of the matrix stacks and the combined modelview-projection 6897117f1b4Smrg * matrix. 6907117f1b4Smrg */ 6913464ebd5Sriastradhvoid _mesa_init_matrix( struct gl_context * ctx ) 6927117f1b4Smrg{ 6937117f1b4Smrg GLint i; 6947117f1b4Smrg 6957117f1b4Smrg /* Initialize matrix stacks */ 6967117f1b4Smrg init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH, 6977117f1b4Smrg _NEW_MODELVIEW); 6987117f1b4Smrg init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH, 6997117f1b4Smrg _NEW_PROJECTION); 700cdc920a0Smrg for (i = 0; i < Elements(ctx->TextureMatrixStack); i++) 7017117f1b4Smrg init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH, 7027117f1b4Smrg _NEW_TEXTURE_MATRIX); 703cdc920a0Smrg for (i = 0; i < Elements(ctx->ProgramMatrixStack); i++) 7047117f1b4Smrg init_matrix_stack(&ctx->ProgramMatrixStack[i], 7057117f1b4Smrg MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX); 7067117f1b4Smrg ctx->CurrentStack = &ctx->ModelviewMatrixStack; 7077117f1b4Smrg 7087117f1b4Smrg /* Init combined Modelview*Projection matrix */ 7097117f1b4Smrg _math_matrix_ctr( &ctx->_ModelProjectMatrix ); 7107117f1b4Smrg} 7117117f1b4Smrg 7127117f1b4Smrg 7137117f1b4Smrg/** 7147117f1b4Smrg * Free the context matrix data. 7157117f1b4Smrg * 7167117f1b4Smrg * \param ctx GL context. 7177117f1b4Smrg * 7187117f1b4Smrg * Frees each of the matrix stacks and the combined modelview-projection 7197117f1b4Smrg * matrix. 7207117f1b4Smrg */ 7213464ebd5Sriastradhvoid _mesa_free_matrix_data( struct gl_context *ctx ) 7227117f1b4Smrg{ 7237117f1b4Smrg GLint i; 7247117f1b4Smrg 7257117f1b4Smrg free_matrix_stack(&ctx->ModelviewMatrixStack); 7267117f1b4Smrg free_matrix_stack(&ctx->ProjectionMatrixStack); 727cdc920a0Smrg for (i = 0; i < Elements(ctx->TextureMatrixStack); i++) 7287117f1b4Smrg free_matrix_stack(&ctx->TextureMatrixStack[i]); 729cdc920a0Smrg for (i = 0; i < Elements(ctx->ProgramMatrixStack); i++) 7307117f1b4Smrg free_matrix_stack(&ctx->ProgramMatrixStack[i]); 7317117f1b4Smrg /* combined Modelview*Projection matrix */ 7327117f1b4Smrg _math_matrix_dtr( &ctx->_ModelProjectMatrix ); 7337117f1b4Smrg 7347117f1b4Smrg} 7357117f1b4Smrg 7367117f1b4Smrg 7377117f1b4Smrg/** 7387117f1b4Smrg * Initialize the context transform attribute group. 7397117f1b4Smrg * 7407117f1b4Smrg * \param ctx GL context. 7417117f1b4Smrg * 7427117f1b4Smrg * \todo Move this to a new file with other 'transform' routines. 7437117f1b4Smrg */ 7443464ebd5Sriastradhvoid _mesa_init_transform( struct gl_context *ctx ) 7457117f1b4Smrg{ 746af69d88dSmrg GLuint i; 7477117f1b4Smrg 7487117f1b4Smrg /* Transformation group */ 7497117f1b4Smrg ctx->Transform.MatrixMode = GL_MODELVIEW; 7507117f1b4Smrg ctx->Transform.Normalize = GL_FALSE; 7517117f1b4Smrg ctx->Transform.RescaleNormals = GL_FALSE; 7527117f1b4Smrg ctx->Transform.RasterPositionUnclipped = GL_FALSE; 753af69d88dSmrg for (i=0;i<ctx->Const.MaxClipPlanes;i++) { 7547117f1b4Smrg ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 ); 7557117f1b4Smrg } 7567117f1b4Smrg ctx->Transform.ClipPlanesEnabled = 0; 7577117f1b4Smrg} 7587117f1b4Smrg 7597117f1b4Smrg 7607117f1b4Smrg/*@}*/ 761