matrix.c revision cdc920a0
17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
34a49301eSmrg * Version:  7.5
47117f1b4Smrg *
54a49301eSmrg * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
64a49301eSmrg * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
77117f1b4Smrg *
87117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
97117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
107117f1b4Smrg * to deal in the Software without restriction, including without limitation
117117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
127117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
137117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
147117f1b4Smrg *
157117f1b4Smrg * The above copyright notice and this permission notice shall be included
167117f1b4Smrg * in all copies or substantial portions of the Software.
177117f1b4Smrg *
187117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
197117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
207117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
217117f1b4Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
227117f1b4Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
237117f1b4Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR 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
627117f1b4Smrg * __GLcontextRec::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);
707117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
717117f1b4Smrg
727117f1b4Smrg   if (nearval <= 0.0 ||
737117f1b4Smrg       farval <= 0.0 ||
747117f1b4Smrg       nearval == farval ||
757117f1b4Smrg       left == right ||
767117f1b4Smrg       top == bottom)
777117f1b4Smrg   {
787117f1b4Smrg      _mesa_error( ctx,  GL_INVALID_VALUE, "glFrustum" );
797117f1b4Smrg      return;
807117f1b4Smrg   }
817117f1b4Smrg
827117f1b4Smrg   _math_matrix_frustum( ctx->CurrentStack->Top,
837117f1b4Smrg                         (GLfloat) left, (GLfloat) right,
847117f1b4Smrg			 (GLfloat) bottom, (GLfloat) top,
857117f1b4Smrg			 (GLfloat) nearval, (GLfloat) farval );
867117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
877117f1b4Smrg}
887117f1b4Smrg
897117f1b4Smrg
907117f1b4Smrg/**
917117f1b4Smrg * Apply an orthographic projection matrix.
927117f1b4Smrg *
937117f1b4Smrg * \param left left clipping plane coordinate.
947117f1b4Smrg * \param right right clipping plane coordinate.
957117f1b4Smrg * \param bottom bottom clipping plane coordinate.
967117f1b4Smrg * \param top top clipping plane coordinate.
977117f1b4Smrg * \param nearval distance to the near clipping plane.
987117f1b4Smrg * \param farval distance to the far clipping plane.
997117f1b4Smrg *
1007117f1b4Smrg * \sa glOrtho().
1017117f1b4Smrg *
1027117f1b4Smrg * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
1037117f1b4Smrg * the top matrix of the current matrix stack and sets
1047117f1b4Smrg * __GLcontextRec::NewState.
1057117f1b4Smrg */
1067117f1b4Smrgvoid GLAPIENTRY
1077117f1b4Smrg_mesa_Ortho( GLdouble left, GLdouble right,
1087117f1b4Smrg             GLdouble bottom, GLdouble top,
1097117f1b4Smrg             GLdouble nearval, GLdouble farval )
1107117f1b4Smrg{
1117117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
1127117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1137117f1b4Smrg
1147117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
1157117f1b4Smrg      _mesa_debug(ctx, "glOrtho(%f, %f, %f, %f, %f, %f)\n",
1167117f1b4Smrg                  left, right, bottom, top, nearval, farval);
1177117f1b4Smrg
1187117f1b4Smrg   if (left == right ||
1197117f1b4Smrg       bottom == top ||
1207117f1b4Smrg       nearval == farval)
1217117f1b4Smrg   {
1227117f1b4Smrg      _mesa_error( ctx,  GL_INVALID_VALUE, "glOrtho" );
1237117f1b4Smrg      return;
1247117f1b4Smrg   }
1257117f1b4Smrg
1267117f1b4Smrg   _math_matrix_ortho( ctx->CurrentStack->Top,
1277117f1b4Smrg                       (GLfloat) left, (GLfloat) right,
1287117f1b4Smrg		       (GLfloat) bottom, (GLfloat) top,
1297117f1b4Smrg		       (GLfloat) nearval, (GLfloat) farval );
1307117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
1317117f1b4Smrg}
1327117f1b4Smrg
1337117f1b4Smrg
1347117f1b4Smrg/**
1357117f1b4Smrg * Set the current matrix stack.
1367117f1b4Smrg *
1377117f1b4Smrg * \param mode matrix stack.
1387117f1b4Smrg *
1397117f1b4Smrg * \sa glMatrixMode().
1407117f1b4Smrg *
1417117f1b4Smrg * Flushes the vertices, validates the parameter and updates
1427117f1b4Smrg * __GLcontextRec::CurrentStack and gl_transform_attrib::MatrixMode with the
1437117f1b4Smrg * specified matrix stack.
1447117f1b4Smrg */
1457117f1b4Smrgvoid GLAPIENTRY
1467117f1b4Smrg_mesa_MatrixMode( GLenum mode )
1477117f1b4Smrg{
1487117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
1497117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
1507117f1b4Smrg
1517117f1b4Smrg   if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
1527117f1b4Smrg      return;
1537117f1b4Smrg   FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
1547117f1b4Smrg
1557117f1b4Smrg   switch (mode) {
1567117f1b4Smrg   case GL_MODELVIEW:
1577117f1b4Smrg      ctx->CurrentStack = &ctx->ModelviewMatrixStack;
1587117f1b4Smrg      break;
1597117f1b4Smrg   case GL_PROJECTION:
1607117f1b4Smrg      ctx->CurrentStack = &ctx->ProjectionMatrixStack;
1617117f1b4Smrg      break;
1627117f1b4Smrg   case GL_TEXTURE:
1634a49301eSmrg      /* This error check is disabled because if we're called from
1644a49301eSmrg       * glPopAttrib() when the active texture unit is >= MaxTextureCoordUnits
1654a49301eSmrg       * we'll generate an unexpected error.
1664a49301eSmrg       * From the GL_ARB_vertex_shader spec it sounds like we should instead
1674a49301eSmrg       * do error checking in other places when we actually try to access
1684a49301eSmrg       * texture matrices beyond MaxTextureCoordUnits.
1694a49301eSmrg       */
1704a49301eSmrg#if 0
1717117f1b4Smrg      if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
1724a49301eSmrg         _mesa_error(ctx, GL_INVALID_OPERATION, "glMatrixMode(invalid tex unit %d)",
1734a49301eSmrg                     ctx->Texture.CurrentUnit);
1747117f1b4Smrg         return;
1757117f1b4Smrg      }
1764a49301eSmrg#endif
1774a49301eSmrg      ASSERT(ctx->Texture.CurrentUnit < Elements(ctx->TextureMatrixStack));
1787117f1b4Smrg      ctx->CurrentStack = &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
1797117f1b4Smrg      break;
1807117f1b4Smrg   case GL_COLOR:
1817117f1b4Smrg      ctx->CurrentStack = &ctx->ColorMatrixStack;
1827117f1b4Smrg      break;
1837117f1b4Smrg   case GL_MATRIX0_NV:
1847117f1b4Smrg   case GL_MATRIX1_NV:
1857117f1b4Smrg   case GL_MATRIX2_NV:
1867117f1b4Smrg   case GL_MATRIX3_NV:
1877117f1b4Smrg   case GL_MATRIX4_NV:
1887117f1b4Smrg   case GL_MATRIX5_NV:
1897117f1b4Smrg   case GL_MATRIX6_NV:
1907117f1b4Smrg   case GL_MATRIX7_NV:
1917117f1b4Smrg      if (ctx->Extensions.NV_vertex_program) {
1927117f1b4Smrg         ctx->CurrentStack = &ctx->ProgramMatrixStack[mode - GL_MATRIX0_NV];
1937117f1b4Smrg      }
1947117f1b4Smrg      else {
1957117f1b4Smrg         _mesa_error( ctx,  GL_INVALID_ENUM, "glMatrixMode(mode)" );
1967117f1b4Smrg         return;
1977117f1b4Smrg      }
1987117f1b4Smrg      break;
1997117f1b4Smrg   case GL_MATRIX0_ARB:
2007117f1b4Smrg   case GL_MATRIX1_ARB:
2017117f1b4Smrg   case GL_MATRIX2_ARB:
2027117f1b4Smrg   case GL_MATRIX3_ARB:
2037117f1b4Smrg   case GL_MATRIX4_ARB:
2047117f1b4Smrg   case GL_MATRIX5_ARB:
2057117f1b4Smrg   case GL_MATRIX6_ARB:
2067117f1b4Smrg   case GL_MATRIX7_ARB:
2077117f1b4Smrg      if (ctx->Extensions.ARB_vertex_program ||
2087117f1b4Smrg          ctx->Extensions.ARB_fragment_program) {
2097117f1b4Smrg         const GLuint m = mode - GL_MATRIX0_ARB;
2107117f1b4Smrg         if (m > ctx->Const.MaxProgramMatrices) {
2117117f1b4Smrg            _mesa_error(ctx, GL_INVALID_ENUM,
2127117f1b4Smrg                        "glMatrixMode(GL_MATRIX%d_ARB)", m);
2137117f1b4Smrg            return;
2147117f1b4Smrg         }
2157117f1b4Smrg         ctx->CurrentStack = &ctx->ProgramMatrixStack[m];
2167117f1b4Smrg      }
2177117f1b4Smrg      else {
2187117f1b4Smrg         _mesa_error( ctx,  GL_INVALID_ENUM, "glMatrixMode(mode)" );
2197117f1b4Smrg         return;
2207117f1b4Smrg      }
2217117f1b4Smrg      break;
2227117f1b4Smrg   default:
2237117f1b4Smrg      _mesa_error( ctx,  GL_INVALID_ENUM, "glMatrixMode(mode)" );
2247117f1b4Smrg      return;
2257117f1b4Smrg   }
2267117f1b4Smrg
2277117f1b4Smrg   ctx->Transform.MatrixMode = mode;
2287117f1b4Smrg}
2297117f1b4Smrg
2307117f1b4Smrg
2317117f1b4Smrg/**
2327117f1b4Smrg * Push the current matrix stack.
2337117f1b4Smrg *
2347117f1b4Smrg * \sa glPushMatrix().
2357117f1b4Smrg *
2367117f1b4Smrg * Verifies the current matrix stack is not full, and duplicates the top-most
2377117f1b4Smrg * matrix in the stack. Marks __GLcontextRec::NewState with the stack dirty
2387117f1b4Smrg * flag.
2397117f1b4Smrg */
2407117f1b4Smrgvoid GLAPIENTRY
2417117f1b4Smrg_mesa_PushMatrix( void )
2427117f1b4Smrg{
2437117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
2447117f1b4Smrg   struct gl_matrix_stack *stack = ctx->CurrentStack;
2457117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END(ctx);
2467117f1b4Smrg
2477117f1b4Smrg   if (MESA_VERBOSE&VERBOSE_API)
2487117f1b4Smrg      _mesa_debug(ctx, "glPushMatrix %s\n",
2497117f1b4Smrg                  _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
2507117f1b4Smrg
2517117f1b4Smrg   if (stack->Depth + 1 >= stack->MaxDepth) {
2527117f1b4Smrg      if (ctx->Transform.MatrixMode == GL_TEXTURE) {
2537117f1b4Smrg         _mesa_error(ctx,  GL_STACK_OVERFLOW,
2547117f1b4Smrg                     "glPushMatrix(mode=GL_TEXTURE, unit=%d)",
2557117f1b4Smrg                      ctx->Texture.CurrentUnit);
2567117f1b4Smrg      }
2577117f1b4Smrg      else {
2587117f1b4Smrg         _mesa_error(ctx,  GL_STACK_OVERFLOW, "glPushMatrix(mode=%s)",
2597117f1b4Smrg                     _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
2607117f1b4Smrg      }
2617117f1b4Smrg      return;
2627117f1b4Smrg   }
2637117f1b4Smrg   _math_matrix_copy( &stack->Stack[stack->Depth + 1],
2647117f1b4Smrg                      &stack->Stack[stack->Depth] );
2657117f1b4Smrg   stack->Depth++;
2667117f1b4Smrg   stack->Top = &(stack->Stack[stack->Depth]);
2677117f1b4Smrg   ctx->NewState |= stack->DirtyFlag;
2687117f1b4Smrg}
2697117f1b4Smrg
2707117f1b4Smrg
2717117f1b4Smrg/**
2727117f1b4Smrg * Pop the current matrix stack.
2737117f1b4Smrg *
2747117f1b4Smrg * \sa glPopMatrix().
2757117f1b4Smrg *
2767117f1b4Smrg * Flushes the vertices, verifies the current matrix stack is not empty, and
2777117f1b4Smrg * moves the stack head down. Marks __GLcontextRec::NewState with the dirty
2787117f1b4Smrg * stack flag.
2797117f1b4Smrg */
2807117f1b4Smrgvoid GLAPIENTRY
2817117f1b4Smrg_mesa_PopMatrix( void )
2827117f1b4Smrg{
2837117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
2847117f1b4Smrg   struct gl_matrix_stack *stack = ctx->CurrentStack;
2857117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
2867117f1b4Smrg
2877117f1b4Smrg   if (MESA_VERBOSE&VERBOSE_API)
2887117f1b4Smrg      _mesa_debug(ctx, "glPopMatrix %s\n",
2897117f1b4Smrg                  _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
2907117f1b4Smrg
2917117f1b4Smrg   if (stack->Depth == 0) {
2927117f1b4Smrg      if (ctx->Transform.MatrixMode == GL_TEXTURE) {
2937117f1b4Smrg         _mesa_error(ctx,  GL_STACK_UNDERFLOW,
2947117f1b4Smrg                     "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
2957117f1b4Smrg                      ctx->Texture.CurrentUnit);
2967117f1b4Smrg      }
2977117f1b4Smrg      else {
2987117f1b4Smrg         _mesa_error(ctx,  GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)",
2997117f1b4Smrg                     _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
3007117f1b4Smrg      }
3017117f1b4Smrg      return;
3027117f1b4Smrg   }
3037117f1b4Smrg   stack->Depth--;
3047117f1b4Smrg   stack->Top = &(stack->Stack[stack->Depth]);
3057117f1b4Smrg   ctx->NewState |= stack->DirtyFlag;
3067117f1b4Smrg}
3077117f1b4Smrg
3087117f1b4Smrg
3097117f1b4Smrg/**
3107117f1b4Smrg * Replace the current matrix with the identity matrix.
3117117f1b4Smrg *
3127117f1b4Smrg * \sa glLoadIdentity().
3137117f1b4Smrg *
3147117f1b4Smrg * Flushes the vertices and calls _math_matrix_set_identity() with the top-most
3157117f1b4Smrg * matrix in the current stack. Marks __GLcontextRec::NewState with the stack
3167117f1b4Smrg * dirty flag.
3177117f1b4Smrg */
3187117f1b4Smrgvoid GLAPIENTRY
3197117f1b4Smrg_mesa_LoadIdentity( void )
3207117f1b4Smrg{
3217117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
3227117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3237117f1b4Smrg
3247117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
325cdc920a0Smrg      _mesa_debug(ctx, "glLoadIdentity()\n");
3267117f1b4Smrg
3277117f1b4Smrg   _math_matrix_set_identity( ctx->CurrentStack->Top );
3287117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
3297117f1b4Smrg}
3307117f1b4Smrg
3317117f1b4Smrg
3327117f1b4Smrg/**
3337117f1b4Smrg * Replace the current matrix with a given matrix.
3347117f1b4Smrg *
3357117f1b4Smrg * \param m matrix.
3367117f1b4Smrg *
3377117f1b4Smrg * \sa glLoadMatrixf().
3387117f1b4Smrg *
3397117f1b4Smrg * Flushes the vertices and calls _math_matrix_loadf() with the top-most matrix
3407117f1b4Smrg * in the current stack and the given matrix. Marks __GLcontextRec::NewState
3417117f1b4Smrg * with the dirty stack flag.
3427117f1b4Smrg */
3437117f1b4Smrgvoid GLAPIENTRY
3447117f1b4Smrg_mesa_LoadMatrixf( const GLfloat *m )
3457117f1b4Smrg{
3467117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
3477117f1b4Smrg   if (!m) return;
3487117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
3497117f1b4Smrg      _mesa_debug(ctx,
3507117f1b4Smrg          "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
3517117f1b4Smrg          m[0], m[4], m[8], m[12],
3527117f1b4Smrg          m[1], m[5], m[9], m[13],
3537117f1b4Smrg          m[2], m[6], m[10], m[14],
3547117f1b4Smrg          m[3], m[7], m[11], m[15]);
3557117f1b4Smrg
3567117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3577117f1b4Smrg   _math_matrix_loadf( ctx->CurrentStack->Top, m );
3587117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
3597117f1b4Smrg}
3607117f1b4Smrg
3617117f1b4Smrg
3627117f1b4Smrg/**
3637117f1b4Smrg * Multiply the current matrix with a given matrix.
3647117f1b4Smrg *
3657117f1b4Smrg * \param m matrix.
3667117f1b4Smrg *
3677117f1b4Smrg * \sa glMultMatrixf().
3687117f1b4Smrg *
3697117f1b4Smrg * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
3707117f1b4Smrg * matrix in the current stack and the given matrix. Marks
3717117f1b4Smrg * __GLcontextRec::NewState with the dirty stack flag.
3727117f1b4Smrg */
3737117f1b4Smrgvoid GLAPIENTRY
3747117f1b4Smrg_mesa_MultMatrixf( const GLfloat *m )
3757117f1b4Smrg{
3767117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
3777117f1b4Smrg   if (!m) return;
3787117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
3797117f1b4Smrg      _mesa_debug(ctx,
3807117f1b4Smrg          "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
3817117f1b4Smrg          m[0], m[4], m[8], m[12],
3827117f1b4Smrg          m[1], m[5], m[9], m[13],
3837117f1b4Smrg          m[2], m[6], m[10], m[14],
3847117f1b4Smrg          m[3], m[7], m[11], m[15]);
3857117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
3867117f1b4Smrg   _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
3877117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
3887117f1b4Smrg}
3897117f1b4Smrg
3907117f1b4Smrg
3917117f1b4Smrg/**
3927117f1b4Smrg * Multiply the current matrix with a rotation matrix.
3937117f1b4Smrg *
3947117f1b4Smrg * \param angle angle of rotation, in degrees.
3957117f1b4Smrg * \param x rotation vector x coordinate.
3967117f1b4Smrg * \param y rotation vector y coordinate.
3977117f1b4Smrg * \param z rotation vector z coordinate.
3987117f1b4Smrg *
3997117f1b4Smrg * \sa glRotatef().
4007117f1b4Smrg *
4017117f1b4Smrg * Flushes the vertices and calls _math_matrix_rotate() with the top-most
4027117f1b4Smrg * matrix in the current stack and the given parameters. Marks
4037117f1b4Smrg * __GLcontextRec::NewState with the dirty stack flag.
4047117f1b4Smrg */
4057117f1b4Smrgvoid GLAPIENTRY
4067117f1b4Smrg_mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
4077117f1b4Smrg{
4087117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
4097117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
4107117f1b4Smrg   if (angle != 0.0F) {
4117117f1b4Smrg      _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
4127117f1b4Smrg      ctx->NewState |= ctx->CurrentStack->DirtyFlag;
4137117f1b4Smrg   }
4147117f1b4Smrg}
4157117f1b4Smrg
4167117f1b4Smrg
4177117f1b4Smrg/**
4187117f1b4Smrg * Multiply the current matrix with a general scaling matrix.
4197117f1b4Smrg *
4207117f1b4Smrg * \param x x axis scale factor.
4217117f1b4Smrg * \param y y axis scale factor.
4227117f1b4Smrg * \param z z axis scale factor.
4237117f1b4Smrg *
4247117f1b4Smrg * \sa glScalef().
4257117f1b4Smrg *
4267117f1b4Smrg * Flushes the vertices and calls _math_matrix_scale() with the top-most
4277117f1b4Smrg * matrix in the current stack and the given parameters. Marks
4287117f1b4Smrg * __GLcontextRec::NewState with the dirty stack flag.
4297117f1b4Smrg */
4307117f1b4Smrgvoid GLAPIENTRY
4317117f1b4Smrg_mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
4327117f1b4Smrg{
4337117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
4347117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
4357117f1b4Smrg   _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
4367117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
4377117f1b4Smrg}
4387117f1b4Smrg
4397117f1b4Smrg
4407117f1b4Smrg/**
4417117f1b4Smrg * Multiply the current matrix with a translation matrix.
4427117f1b4Smrg *
4437117f1b4Smrg * \param x translation vector x coordinate.
4447117f1b4Smrg * \param y translation vector y coordinate.
4457117f1b4Smrg * \param z translation vector z coordinate.
4467117f1b4Smrg *
4477117f1b4Smrg * \sa glTranslatef().
4487117f1b4Smrg *
4497117f1b4Smrg * Flushes the vertices and calls _math_matrix_translate() with the top-most
4507117f1b4Smrg * matrix in the current stack and the given parameters. Marks
4517117f1b4Smrg * __GLcontextRec::NewState with the dirty stack flag.
4527117f1b4Smrg */
4537117f1b4Smrgvoid GLAPIENTRY
4547117f1b4Smrg_mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
4557117f1b4Smrg{
4567117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
4577117f1b4Smrg   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
4587117f1b4Smrg   _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
4597117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
4607117f1b4Smrg}
4617117f1b4Smrg
4627117f1b4Smrg
4637117f1b4Smrg#if _HAVE_FULL_GL
4647117f1b4Smrgvoid GLAPIENTRY
4657117f1b4Smrg_mesa_LoadMatrixd( const GLdouble *m )
4667117f1b4Smrg{
4677117f1b4Smrg   GLint i;
4687117f1b4Smrg   GLfloat f[16];
4697117f1b4Smrg   if (!m) return;
4707117f1b4Smrg   for (i = 0; i < 16; i++)
4717117f1b4Smrg      f[i] = (GLfloat) m[i];
4727117f1b4Smrg   _mesa_LoadMatrixf(f);
4737117f1b4Smrg}
4747117f1b4Smrg
4757117f1b4Smrgvoid GLAPIENTRY
4767117f1b4Smrg_mesa_MultMatrixd( const GLdouble *m )
4777117f1b4Smrg{
4787117f1b4Smrg   GLint i;
4797117f1b4Smrg   GLfloat f[16];
4807117f1b4Smrg   if (!m) return;
4817117f1b4Smrg   for (i = 0; i < 16; i++)
4827117f1b4Smrg      f[i] = (GLfloat) m[i];
4837117f1b4Smrg   _mesa_MultMatrixf( f );
4847117f1b4Smrg}
4857117f1b4Smrg
4867117f1b4Smrg
4877117f1b4Smrgvoid GLAPIENTRY
4887117f1b4Smrg_mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
4897117f1b4Smrg{
4907117f1b4Smrg   _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
4917117f1b4Smrg}
4927117f1b4Smrg
4937117f1b4Smrg
4947117f1b4Smrgvoid GLAPIENTRY
4957117f1b4Smrg_mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
4967117f1b4Smrg{
4977117f1b4Smrg   _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
4987117f1b4Smrg}
4997117f1b4Smrg
5007117f1b4Smrg
5017117f1b4Smrgvoid GLAPIENTRY
5027117f1b4Smrg_mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
5037117f1b4Smrg{
5047117f1b4Smrg   _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
5057117f1b4Smrg}
5067117f1b4Smrg#endif
5077117f1b4Smrg
5087117f1b4Smrg
5097117f1b4Smrg#if _HAVE_FULL_GL
5107117f1b4Smrgvoid GLAPIENTRY
5117117f1b4Smrg_mesa_LoadTransposeMatrixfARB( const GLfloat *m )
5127117f1b4Smrg{
5137117f1b4Smrg   GLfloat tm[16];
5147117f1b4Smrg   if (!m) return;
5157117f1b4Smrg   _math_transposef(tm, m);
5167117f1b4Smrg   _mesa_LoadMatrixf(tm);
5177117f1b4Smrg}
5187117f1b4Smrg
5197117f1b4Smrg
5207117f1b4Smrgvoid GLAPIENTRY
5217117f1b4Smrg_mesa_LoadTransposeMatrixdARB( const GLdouble *m )
5227117f1b4Smrg{
5237117f1b4Smrg   GLfloat tm[16];
5247117f1b4Smrg   if (!m) return;
5257117f1b4Smrg   _math_transposefd(tm, m);
5267117f1b4Smrg   _mesa_LoadMatrixf(tm);
5277117f1b4Smrg}
5287117f1b4Smrg
5297117f1b4Smrg
5307117f1b4Smrgvoid GLAPIENTRY
5317117f1b4Smrg_mesa_MultTransposeMatrixfARB( const GLfloat *m )
5327117f1b4Smrg{
5337117f1b4Smrg   GLfloat tm[16];
5347117f1b4Smrg   if (!m) return;
5357117f1b4Smrg   _math_transposef(tm, m);
5367117f1b4Smrg   _mesa_MultMatrixf(tm);
5377117f1b4Smrg}
5387117f1b4Smrg
5397117f1b4Smrg
5407117f1b4Smrgvoid GLAPIENTRY
5417117f1b4Smrg_mesa_MultTransposeMatrixdARB( const GLdouble *m )
5427117f1b4Smrg{
5437117f1b4Smrg   GLfloat tm[16];
5447117f1b4Smrg   if (!m) return;
5457117f1b4Smrg   _math_transposefd(tm, m);
5467117f1b4Smrg   _mesa_MultMatrixf(tm);
5477117f1b4Smrg}
5487117f1b4Smrg#endif
5497117f1b4Smrg
5507117f1b4Smrg
5517117f1b4Smrg
5527117f1b4Smrg/**********************************************************************/
5537117f1b4Smrg/** \name State management */
5547117f1b4Smrg/*@{*/
5557117f1b4Smrg
5567117f1b4Smrg
5577117f1b4Smrg/**
5587117f1b4Smrg * Update the projection matrix stack.
5597117f1b4Smrg *
5607117f1b4Smrg * \param ctx GL context.
5617117f1b4Smrg *
5627117f1b4Smrg * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
5637117f1b4Smrg * stack, and recomputes user clip positions if necessary.
5647117f1b4Smrg *
5657117f1b4Smrg * \note This routine references __GLcontextRec::Tranform attribute values to
5667117f1b4Smrg * compute userclip positions in clip space, but is only called on
5677117f1b4Smrg * _NEW_PROJECTION.  The _mesa_ClipPlane() function keeps these values up to
5687117f1b4Smrg * date across changes to the __GLcontextRec::Transform attributes.
5697117f1b4Smrg */
5707117f1b4Smrgstatic void
5717117f1b4Smrgupdate_projection( GLcontext *ctx )
5727117f1b4Smrg{
5737117f1b4Smrg   _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
5747117f1b4Smrg
5757117f1b4Smrg#if FEATURE_userclip
5767117f1b4Smrg   /* Recompute clip plane positions in clipspace.  This is also done
5777117f1b4Smrg    * in _mesa_ClipPlane().
5787117f1b4Smrg    */
5797117f1b4Smrg   if (ctx->Transform.ClipPlanesEnabled) {
5807117f1b4Smrg      GLuint p;
5817117f1b4Smrg      for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
5827117f1b4Smrg	 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
5837117f1b4Smrg	    _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
5847117f1b4Smrg				 ctx->Transform.EyeUserPlane[p],
5857117f1b4Smrg				 ctx->ProjectionMatrixStack.Top->inv );
5867117f1b4Smrg	 }
5877117f1b4Smrg      }
5887117f1b4Smrg   }
5897117f1b4Smrg#endif
5907117f1b4Smrg}
5917117f1b4Smrg
5927117f1b4Smrg
5937117f1b4Smrg/**
5947117f1b4Smrg * Calculate the combined modelview-projection matrix.
5957117f1b4Smrg *
5967117f1b4Smrg * \param ctx GL context.
5977117f1b4Smrg *
5987117f1b4Smrg * Multiplies the top matrices of the projection and model view stacks into
5997117f1b4Smrg * __GLcontextRec::_ModelProjectMatrix via _math_matrix_mul_matrix() and
6007117f1b4Smrg * analyzes the resulting matrix via _math_matrix_analyse().
6017117f1b4Smrg */
6027117f1b4Smrgstatic void
6037117f1b4Smrgcalculate_model_project_matrix( GLcontext *ctx )
6047117f1b4Smrg{
6057117f1b4Smrg   _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
6067117f1b4Smrg                            ctx->ProjectionMatrixStack.Top,
6077117f1b4Smrg                            ctx->ModelviewMatrixStack.Top );
6087117f1b4Smrg
6097117f1b4Smrg   _math_matrix_analyse( &ctx->_ModelProjectMatrix );
6107117f1b4Smrg}
6117117f1b4Smrg
6127117f1b4Smrg
6137117f1b4Smrg/**
6147117f1b4Smrg * Updates the combined modelview-projection matrix.
6157117f1b4Smrg *
6167117f1b4Smrg * \param ctx GL context.
6177117f1b4Smrg * \param new_state new state bit mask.
6187117f1b4Smrg *
6197117f1b4Smrg * If there is a new model view matrix then analyzes it. If there is a new
6207117f1b4Smrg * projection matrix, updates it. Finally calls
6217117f1b4Smrg * calculate_model_project_matrix() to recalculate the modelview-projection
6227117f1b4Smrg * matrix.
6237117f1b4Smrg */
6247117f1b4Smrgvoid _mesa_update_modelview_project( GLcontext *ctx, GLuint new_state )
6257117f1b4Smrg{
6267117f1b4Smrg   if (new_state & _NEW_MODELVIEW) {
6277117f1b4Smrg      _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
6287117f1b4Smrg
6297117f1b4Smrg      /* Bring cull position uptodate.
6307117f1b4Smrg       */
6317117f1b4Smrg      TRANSFORM_POINT3( ctx->Transform.CullObjPos,
6327117f1b4Smrg			ctx->ModelviewMatrixStack.Top->inv,
6337117f1b4Smrg			ctx->Transform.CullEyePos );
6347117f1b4Smrg   }
6357117f1b4Smrg
6367117f1b4Smrg
6377117f1b4Smrg   if (new_state & _NEW_PROJECTION)
6387117f1b4Smrg      update_projection( ctx );
6397117f1b4Smrg
6407117f1b4Smrg   /* Keep ModelviewProject uptodate always to allow tnl
6417117f1b4Smrg    * implementations that go model->clip even when eye is required.
6427117f1b4Smrg    */
6437117f1b4Smrg   calculate_model_project_matrix(ctx);
6447117f1b4Smrg}
6457117f1b4Smrg
6467117f1b4Smrg/*@}*/
6477117f1b4Smrg
6487117f1b4Smrg
6497117f1b4Smrg/**********************************************************************/
6507117f1b4Smrg/** Matrix stack initialization */
6517117f1b4Smrg/*@{*/
6527117f1b4Smrg
6537117f1b4Smrg
6547117f1b4Smrg/**
6557117f1b4Smrg * Initialize a matrix stack.
6567117f1b4Smrg *
6577117f1b4Smrg * \param stack matrix stack.
6587117f1b4Smrg * \param maxDepth maximum stack depth.
6597117f1b4Smrg * \param dirtyFlag dirty flag.
6607117f1b4Smrg *
6617117f1b4Smrg * Allocates an array of \p maxDepth elements for the matrix stack and calls
6627117f1b4Smrg * _math_matrix_ctr() and _math_matrix_alloc_inv() for each element to
6637117f1b4Smrg * initialize it.
6647117f1b4Smrg */
6657117f1b4Smrgstatic void
6667117f1b4Smrginit_matrix_stack( struct gl_matrix_stack *stack,
6677117f1b4Smrg                   GLuint maxDepth, GLuint dirtyFlag )
6687117f1b4Smrg{
6697117f1b4Smrg   GLuint i;
6707117f1b4Smrg
6717117f1b4Smrg   stack->Depth = 0;
6727117f1b4Smrg   stack->MaxDepth = maxDepth;
6737117f1b4Smrg   stack->DirtyFlag = dirtyFlag;
6747117f1b4Smrg   /* The stack */
6757117f1b4Smrg   stack->Stack = (GLmatrix *) CALLOC(maxDepth * sizeof(GLmatrix));
6767117f1b4Smrg   for (i = 0; i < maxDepth; i++) {
6777117f1b4Smrg      _math_matrix_ctr(&stack->Stack[i]);
6787117f1b4Smrg      _math_matrix_alloc_inv(&stack->Stack[i]);
6797117f1b4Smrg   }
6807117f1b4Smrg   stack->Top = stack->Stack;
6817117f1b4Smrg}
6827117f1b4Smrg
6837117f1b4Smrg/**
6847117f1b4Smrg * Free matrix stack.
6857117f1b4Smrg *
6867117f1b4Smrg * \param stack matrix stack.
6877117f1b4Smrg *
6887117f1b4Smrg * Calls _math_matrix_dtr() for each element of the matrix stack and
6897117f1b4Smrg * frees the array.
6907117f1b4Smrg */
6917117f1b4Smrgstatic void
6927117f1b4Smrgfree_matrix_stack( struct gl_matrix_stack *stack )
6937117f1b4Smrg{
6947117f1b4Smrg   GLuint i;
6957117f1b4Smrg   for (i = 0; i < stack->MaxDepth; i++) {
6967117f1b4Smrg      _math_matrix_dtr(&stack->Stack[i]);
6977117f1b4Smrg   }
6987117f1b4Smrg   FREE(stack->Stack);
6997117f1b4Smrg   stack->Stack = stack->Top = NULL;
7007117f1b4Smrg}
7017117f1b4Smrg
7027117f1b4Smrg/*@}*/
7037117f1b4Smrg
7047117f1b4Smrg
7057117f1b4Smrg/**********************************************************************/
7067117f1b4Smrg/** \name Initialization */
7077117f1b4Smrg/*@{*/
7087117f1b4Smrg
7097117f1b4Smrg
7107117f1b4Smrg/**
7117117f1b4Smrg * Initialize the context matrix data.
7127117f1b4Smrg *
7137117f1b4Smrg * \param ctx GL context.
7147117f1b4Smrg *
7157117f1b4Smrg * Initializes each of the matrix stacks and the combined modelview-projection
7167117f1b4Smrg * matrix.
7177117f1b4Smrg */
7187117f1b4Smrgvoid _mesa_init_matrix( GLcontext * ctx )
7197117f1b4Smrg{
7207117f1b4Smrg   GLint i;
7217117f1b4Smrg
7227117f1b4Smrg   /* Initialize matrix stacks */
7237117f1b4Smrg   init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
7247117f1b4Smrg                     _NEW_MODELVIEW);
7257117f1b4Smrg   init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
7267117f1b4Smrg                     _NEW_PROJECTION);
7277117f1b4Smrg   init_matrix_stack(&ctx->ColorMatrixStack, MAX_COLOR_STACK_DEPTH,
7287117f1b4Smrg                     _NEW_COLOR_MATRIX);
729cdc920a0Smrg   for (i = 0; i < Elements(ctx->TextureMatrixStack); i++)
7307117f1b4Smrg      init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
7317117f1b4Smrg                        _NEW_TEXTURE_MATRIX);
732cdc920a0Smrg   for (i = 0; i < Elements(ctx->ProgramMatrixStack); i++)
7337117f1b4Smrg      init_matrix_stack(&ctx->ProgramMatrixStack[i],
7347117f1b4Smrg		        MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
7357117f1b4Smrg   ctx->CurrentStack = &ctx->ModelviewMatrixStack;
7367117f1b4Smrg
7377117f1b4Smrg   /* Init combined Modelview*Projection matrix */
7387117f1b4Smrg   _math_matrix_ctr( &ctx->_ModelProjectMatrix );
7397117f1b4Smrg}
7407117f1b4Smrg
7417117f1b4Smrg
7427117f1b4Smrg/**
7437117f1b4Smrg * Free the context matrix data.
7447117f1b4Smrg *
7457117f1b4Smrg * \param ctx GL context.
7467117f1b4Smrg *
7477117f1b4Smrg * Frees each of the matrix stacks and the combined modelview-projection
7487117f1b4Smrg * matrix.
7497117f1b4Smrg */
7507117f1b4Smrgvoid _mesa_free_matrix_data( GLcontext *ctx )
7517117f1b4Smrg{
7527117f1b4Smrg   GLint i;
7537117f1b4Smrg
7547117f1b4Smrg   free_matrix_stack(&ctx->ModelviewMatrixStack);
7557117f1b4Smrg   free_matrix_stack(&ctx->ProjectionMatrixStack);
7567117f1b4Smrg   free_matrix_stack(&ctx->ColorMatrixStack);
757cdc920a0Smrg   for (i = 0; i < Elements(ctx->TextureMatrixStack); i++)
7587117f1b4Smrg      free_matrix_stack(&ctx->TextureMatrixStack[i]);
759cdc920a0Smrg   for (i = 0; i < Elements(ctx->ProgramMatrixStack); i++)
7607117f1b4Smrg      free_matrix_stack(&ctx->ProgramMatrixStack[i]);
7617117f1b4Smrg   /* combined Modelview*Projection matrix */
7627117f1b4Smrg   _math_matrix_dtr( &ctx->_ModelProjectMatrix );
7637117f1b4Smrg
7647117f1b4Smrg}
7657117f1b4Smrg
7667117f1b4Smrg
7677117f1b4Smrg/**
7687117f1b4Smrg * Initialize the context transform attribute group.
7697117f1b4Smrg *
7707117f1b4Smrg * \param ctx GL context.
7717117f1b4Smrg *
7727117f1b4Smrg * \todo Move this to a new file with other 'transform' routines.
7737117f1b4Smrg */
7747117f1b4Smrgvoid _mesa_init_transform( GLcontext *ctx )
7757117f1b4Smrg{
7767117f1b4Smrg   GLint i;
7777117f1b4Smrg
7787117f1b4Smrg   /* Transformation group */
7797117f1b4Smrg   ctx->Transform.MatrixMode = GL_MODELVIEW;
7807117f1b4Smrg   ctx->Transform.Normalize = GL_FALSE;
7817117f1b4Smrg   ctx->Transform.RescaleNormals = GL_FALSE;
7827117f1b4Smrg   ctx->Transform.RasterPositionUnclipped = GL_FALSE;
7837117f1b4Smrg   for (i=0;i<MAX_CLIP_PLANES;i++) {
7847117f1b4Smrg      ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
7857117f1b4Smrg   }
7867117f1b4Smrg   ctx->Transform.ClipPlanesEnabled = 0;
7877117f1b4Smrg
7887117f1b4Smrg   ASSIGN_4V( ctx->Transform.CullObjPos, 0.0, 0.0, 1.0, 0.0 );
7897117f1b4Smrg   ASSIGN_4V( ctx->Transform.CullEyePos, 0.0, 0.0, 1.0, 0.0 );
7907117f1b4Smrg}
7917117f1b4Smrg
7927117f1b4Smrg
7937117f1b4Smrg/*@}*/
794