matrix.c revision 01e04c3f
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"
4601e04c3fSmrg#include "util/bitscan.h"
477117f1b4Smrg
487117f1b4Smrg
497117f1b4Smrg/**
507117f1b4Smrg * Apply a perspective projection matrix.
517117f1b4Smrg *
527117f1b4Smrg * \param left left clipping plane coordinate.
537117f1b4Smrg * \param right right clipping plane coordinate.
547117f1b4Smrg * \param bottom bottom clipping plane coordinate.
557117f1b4Smrg * \param top top clipping plane coordinate.
567117f1b4Smrg * \param nearval distance to the near clipping plane.
577117f1b4Smrg * \param farval distance to the far clipping plane.
587117f1b4Smrg *
597117f1b4Smrg * \sa glFrustum().
607117f1b4Smrg *
617117f1b4Smrg * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with
627117f1b4Smrg * the top matrix of the current matrix stack and sets
633464ebd5Sriastradh * __struct gl_contextRec::NewState.
647117f1b4Smrg */
657117f1b4Smrgvoid GLAPIENTRY
667117f1b4Smrg_mesa_Frustum( GLdouble left, GLdouble right,
677117f1b4Smrg               GLdouble bottom, GLdouble top,
687117f1b4Smrg               GLdouble nearval, GLdouble farval )
697117f1b4Smrg{
707117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
71af69d88dSmrg
72af69d88dSmrg   FLUSH_VERTICES(ctx, 0);
737117f1b4Smrg
747117f1b4Smrg   if (nearval <= 0.0 ||
757117f1b4Smrg       farval <= 0.0 ||
767117f1b4Smrg       nearval == farval ||
777117f1b4Smrg       left == right ||
787117f1b4Smrg       top == bottom)
797117f1b4Smrg   {
807117f1b4Smrg      _mesa_error( ctx,  GL_INVALID_VALUE, "glFrustum" );
817117f1b4Smrg      return;
827117f1b4Smrg   }
837117f1b4Smrg
847117f1b4Smrg   _math_matrix_frustum( ctx->CurrentStack->Top,
857117f1b4Smrg                         (GLfloat) left, (GLfloat) right,
867117f1b4Smrg			 (GLfloat) bottom, (GLfloat) top,
877117f1b4Smrg			 (GLfloat) nearval, (GLfloat) farval );
887117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
897117f1b4Smrg}
907117f1b4Smrg
917117f1b4Smrg
927117f1b4Smrg/**
937117f1b4Smrg * Apply an orthographic projection matrix.
947117f1b4Smrg *
957117f1b4Smrg * \param left left clipping plane coordinate.
967117f1b4Smrg * \param right right clipping plane coordinate.
977117f1b4Smrg * \param bottom bottom clipping plane coordinate.
987117f1b4Smrg * \param top top clipping plane coordinate.
997117f1b4Smrg * \param nearval distance to the near clipping plane.
1007117f1b4Smrg * \param farval distance to the far clipping plane.
1017117f1b4Smrg *
1027117f1b4Smrg * \sa glOrtho().
1037117f1b4Smrg *
1047117f1b4Smrg * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
1057117f1b4Smrg * the top matrix of the current matrix stack and sets
1063464ebd5Sriastradh * __struct gl_contextRec::NewState.
1077117f1b4Smrg */
1087117f1b4Smrgvoid GLAPIENTRY
1097117f1b4Smrg_mesa_Ortho( GLdouble left, GLdouble right,
1107117f1b4Smrg             GLdouble bottom, GLdouble top,
1117117f1b4Smrg             GLdouble nearval, GLdouble farval )
1127117f1b4Smrg{
1137117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
114af69d88dSmrg
115af69d88dSmrg   FLUSH_VERTICES(ctx, 0);
1167117f1b4Smrg
1177117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
1187117f1b4Smrg      _mesa_debug(ctx, "glOrtho(%f, %f, %f, %f, %f, %f)\n",
1197117f1b4Smrg                  left, right, bottom, top, nearval, farval);
1207117f1b4Smrg
1217117f1b4Smrg   if (left == right ||
1227117f1b4Smrg       bottom == top ||
1237117f1b4Smrg       nearval == farval)
1247117f1b4Smrg   {
1257117f1b4Smrg      _mesa_error( ctx,  GL_INVALID_VALUE, "glOrtho" );
1267117f1b4Smrg      return;
1277117f1b4Smrg   }
1287117f1b4Smrg
1297117f1b4Smrg   _math_matrix_ortho( ctx->CurrentStack->Top,
1307117f1b4Smrg                       (GLfloat) left, (GLfloat) right,
1317117f1b4Smrg		       (GLfloat) bottom, (GLfloat) top,
1327117f1b4Smrg		       (GLfloat) nearval, (GLfloat) farval );
1337117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
1347117f1b4Smrg}
1357117f1b4Smrg
1367117f1b4Smrg
1377117f1b4Smrg/**
1387117f1b4Smrg * Set the current matrix stack.
1397117f1b4Smrg *
1407117f1b4Smrg * \param mode matrix stack.
1417117f1b4Smrg *
1427117f1b4Smrg * \sa glMatrixMode().
1437117f1b4Smrg *
1447117f1b4Smrg * Flushes the vertices, validates the parameter and updates
1453464ebd5Sriastradh * __struct gl_contextRec::CurrentStack and gl_transform_attrib::MatrixMode
1463464ebd5Sriastradh * with the specified matrix stack.
1477117f1b4Smrg */
1487117f1b4Smrgvoid GLAPIENTRY
1497117f1b4Smrg_mesa_MatrixMode( GLenum mode )
1507117f1b4Smrg{
1517117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
1527117f1b4Smrg
1537117f1b4Smrg   if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
1547117f1b4Smrg      return;
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
17901e04c3fSmrg      assert(ctx->Texture.CurrentUnit < ARRAY_SIZE(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",
23201e04c3fSmrg                  _mesa_enum_to_string(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)",
24201e04c3fSmrg                     _mesa_enum_to_string(ctx->Transform.MatrixMode));
2437117f1b4Smrg      }
2447117f1b4Smrg      return;
2457117f1b4Smrg   }
24601e04c3fSmrg   if (stack->Depth + 1 >= stack->StackSize) {
24701e04c3fSmrg      unsigned new_stack_size = stack->StackSize * 2;
24801e04c3fSmrg      unsigned i;
24901e04c3fSmrg      GLmatrix *new_stack = realloc(stack->Stack,
25001e04c3fSmrg                                    sizeof(*new_stack) * new_stack_size);
25101e04c3fSmrg
25201e04c3fSmrg      if (!new_stack) {
25301e04c3fSmrg         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glPushMatrix()");
25401e04c3fSmrg         return;
25501e04c3fSmrg      }
25601e04c3fSmrg
25701e04c3fSmrg      for (i = stack->StackSize; i < new_stack_size; i++)
25801e04c3fSmrg         _math_matrix_ctr(&new_stack[i]);
25901e04c3fSmrg
26001e04c3fSmrg      stack->Stack = new_stack;
26101e04c3fSmrg      stack->StackSize = new_stack_size;
26201e04c3fSmrg   }
26301e04c3fSmrg
2647117f1b4Smrg   _math_matrix_copy( &stack->Stack[stack->Depth + 1],
2657117f1b4Smrg                      &stack->Stack[stack->Depth] );
2667117f1b4Smrg   stack->Depth++;
2677117f1b4Smrg   stack->Top = &(stack->Stack[stack->Depth]);
2687117f1b4Smrg   ctx->NewState |= stack->DirtyFlag;
2697117f1b4Smrg}
2707117f1b4Smrg
2717117f1b4Smrg
2727117f1b4Smrg/**
2737117f1b4Smrg * Pop the current matrix stack.
2747117f1b4Smrg *
2757117f1b4Smrg * \sa glPopMatrix().
2767117f1b4Smrg *
2777117f1b4Smrg * Flushes the vertices, verifies the current matrix stack is not empty, and
2783464ebd5Sriastradh * moves the stack head down.
2793464ebd5Sriastradh * Marks __struct gl_contextRec::NewState with the dirty stack flag.
2807117f1b4Smrg */
2817117f1b4Smrgvoid GLAPIENTRY
2827117f1b4Smrg_mesa_PopMatrix( void )
2837117f1b4Smrg{
2847117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
2857117f1b4Smrg   struct gl_matrix_stack *stack = ctx->CurrentStack;
286af69d88dSmrg
287af69d88dSmrg   FLUSH_VERTICES(ctx, 0);
2887117f1b4Smrg
2897117f1b4Smrg   if (MESA_VERBOSE&VERBOSE_API)
2907117f1b4Smrg      _mesa_debug(ctx, "glPopMatrix %s\n",
29101e04c3fSmrg                  _mesa_enum_to_string(ctx->Transform.MatrixMode));
2927117f1b4Smrg
2937117f1b4Smrg   if (stack->Depth == 0) {
2947117f1b4Smrg      if (ctx->Transform.MatrixMode == GL_TEXTURE) {
2957117f1b4Smrg         _mesa_error(ctx,  GL_STACK_UNDERFLOW,
2967117f1b4Smrg                     "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
2977117f1b4Smrg                      ctx->Texture.CurrentUnit);
2987117f1b4Smrg      }
2997117f1b4Smrg      else {
3007117f1b4Smrg         _mesa_error(ctx,  GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)",
30101e04c3fSmrg                     _mesa_enum_to_string(ctx->Transform.MatrixMode));
3027117f1b4Smrg      }
3037117f1b4Smrg      return;
3047117f1b4Smrg   }
3057117f1b4Smrg   stack->Depth--;
3067117f1b4Smrg   stack->Top = &(stack->Stack[stack->Depth]);
3077117f1b4Smrg   ctx->NewState |= stack->DirtyFlag;
3087117f1b4Smrg}
3097117f1b4Smrg
3107117f1b4Smrg
3117117f1b4Smrg/**
3127117f1b4Smrg * Replace the current matrix with the identity matrix.
3137117f1b4Smrg *
3147117f1b4Smrg * \sa glLoadIdentity().
3157117f1b4Smrg *
3163464ebd5Sriastradh * Flushes the vertices and calls _math_matrix_set_identity() with the
3173464ebd5Sriastradh * top-most matrix in the current stack.
3183464ebd5Sriastradh * Marks __struct gl_contextRec::NewState with the stack dirty flag.
3197117f1b4Smrg */
3207117f1b4Smrgvoid GLAPIENTRY
3217117f1b4Smrg_mesa_LoadIdentity( void )
3227117f1b4Smrg{
3237117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
324af69d88dSmrg
325af69d88dSmrg   FLUSH_VERTICES(ctx, 0);
3267117f1b4Smrg
3277117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
328cdc920a0Smrg      _mesa_debug(ctx, "glLoadIdentity()\n");
3297117f1b4Smrg
3307117f1b4Smrg   _math_matrix_set_identity( ctx->CurrentStack->Top );
3317117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
3327117f1b4Smrg}
3337117f1b4Smrg
3347117f1b4Smrg
3357117f1b4Smrg/**
3367117f1b4Smrg * Replace the current matrix with a given matrix.
3377117f1b4Smrg *
3387117f1b4Smrg * \param m matrix.
3397117f1b4Smrg *
3407117f1b4Smrg * \sa glLoadMatrixf().
3417117f1b4Smrg *
3423464ebd5Sriastradh * Flushes the vertices and calls _math_matrix_loadf() with the top-most
3433464ebd5Sriastradh * matrix in the current stack and the given matrix.
3443464ebd5Sriastradh * Marks __struct gl_contextRec::NewState with the dirty stack flag.
3457117f1b4Smrg */
3467117f1b4Smrgvoid GLAPIENTRY
3477117f1b4Smrg_mesa_LoadMatrixf( const GLfloat *m )
3487117f1b4Smrg{
3497117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
3507117f1b4Smrg   if (!m) return;
3517117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
3527117f1b4Smrg      _mesa_debug(ctx,
3537117f1b4Smrg          "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
3547117f1b4Smrg          m[0], m[4], m[8], m[12],
3557117f1b4Smrg          m[1], m[5], m[9], m[13],
3567117f1b4Smrg          m[2], m[6], m[10], m[14],
3577117f1b4Smrg          m[3], m[7], m[11], m[15]);
3587117f1b4Smrg
35901e04c3fSmrg   if (memcmp(m, ctx->CurrentStack->Top->m, 16 * sizeof(GLfloat)) != 0) {
36001e04c3fSmrg      FLUSH_VERTICES(ctx, 0);
36101e04c3fSmrg      _math_matrix_loadf( ctx->CurrentStack->Top, m );
36201e04c3fSmrg      ctx->NewState |= ctx->CurrentStack->DirtyFlag;
36301e04c3fSmrg   }
3647117f1b4Smrg}
3657117f1b4Smrg
3667117f1b4Smrg
3677117f1b4Smrg/**
3687117f1b4Smrg * Multiply the current matrix with a given matrix.
3697117f1b4Smrg *
3707117f1b4Smrg * \param m matrix.
3717117f1b4Smrg *
3727117f1b4Smrg * \sa glMultMatrixf().
3737117f1b4Smrg *
3747117f1b4Smrg * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
3757117f1b4Smrg * matrix in the current stack and the given matrix. Marks
3763464ebd5Sriastradh * __struct gl_contextRec::NewState with the dirty stack flag.
3777117f1b4Smrg */
3787117f1b4Smrgvoid GLAPIENTRY
3797117f1b4Smrg_mesa_MultMatrixf( const GLfloat *m )
3807117f1b4Smrg{
3817117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
3827117f1b4Smrg   if (!m) return;
3837117f1b4Smrg   if (MESA_VERBOSE & VERBOSE_API)
3847117f1b4Smrg      _mesa_debug(ctx,
3857117f1b4Smrg          "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
3867117f1b4Smrg          m[0], m[4], m[8], m[12],
3877117f1b4Smrg          m[1], m[5], m[9], m[13],
3887117f1b4Smrg          m[2], m[6], m[10], m[14],
3897117f1b4Smrg          m[3], m[7], m[11], m[15]);
390af69d88dSmrg
391af69d88dSmrg   FLUSH_VERTICES(ctx, 0);
3927117f1b4Smrg   _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
3937117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
3947117f1b4Smrg}
3957117f1b4Smrg
3967117f1b4Smrg
3977117f1b4Smrg/**
3987117f1b4Smrg * Multiply the current matrix with a rotation matrix.
3997117f1b4Smrg *
4007117f1b4Smrg * \param angle angle of rotation, in degrees.
4017117f1b4Smrg * \param x rotation vector x coordinate.
4027117f1b4Smrg * \param y rotation vector y coordinate.
4037117f1b4Smrg * \param z rotation vector z coordinate.
4047117f1b4Smrg *
4057117f1b4Smrg * \sa glRotatef().
4067117f1b4Smrg *
4077117f1b4Smrg * Flushes the vertices and calls _math_matrix_rotate() with the top-most
4087117f1b4Smrg * matrix in the current stack and the given parameters. Marks
4093464ebd5Sriastradh * __struct gl_contextRec::NewState with the dirty stack flag.
4107117f1b4Smrg */
4117117f1b4Smrgvoid GLAPIENTRY
4127117f1b4Smrg_mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
4137117f1b4Smrg{
4147117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
415af69d88dSmrg
416af69d88dSmrg   FLUSH_VERTICES(ctx, 0);
4177117f1b4Smrg   if (angle != 0.0F) {
4187117f1b4Smrg      _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
4197117f1b4Smrg      ctx->NewState |= ctx->CurrentStack->DirtyFlag;
4207117f1b4Smrg   }
4217117f1b4Smrg}
4227117f1b4Smrg
4237117f1b4Smrg
4247117f1b4Smrg/**
4257117f1b4Smrg * Multiply the current matrix with a general scaling matrix.
4267117f1b4Smrg *
4277117f1b4Smrg * \param x x axis scale factor.
4287117f1b4Smrg * \param y y axis scale factor.
4297117f1b4Smrg * \param z z axis scale factor.
4307117f1b4Smrg *
4317117f1b4Smrg * \sa glScalef().
4327117f1b4Smrg *
4337117f1b4Smrg * Flushes the vertices and calls _math_matrix_scale() with the top-most
4347117f1b4Smrg * matrix in the current stack and the given parameters. Marks
4353464ebd5Sriastradh * __struct gl_contextRec::NewState with the dirty stack flag.
4367117f1b4Smrg */
4377117f1b4Smrgvoid GLAPIENTRY
4387117f1b4Smrg_mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
4397117f1b4Smrg{
4407117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
441af69d88dSmrg
442af69d88dSmrg   FLUSH_VERTICES(ctx, 0);
4437117f1b4Smrg   _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
4447117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
4457117f1b4Smrg}
4467117f1b4Smrg
4477117f1b4Smrg
4487117f1b4Smrg/**
4497117f1b4Smrg * Multiply the current matrix with a translation matrix.
4507117f1b4Smrg *
4517117f1b4Smrg * \param x translation vector x coordinate.
4527117f1b4Smrg * \param y translation vector y coordinate.
4537117f1b4Smrg * \param z translation vector z coordinate.
4547117f1b4Smrg *
4557117f1b4Smrg * \sa glTranslatef().
4567117f1b4Smrg *
4577117f1b4Smrg * Flushes the vertices and calls _math_matrix_translate() with the top-most
4587117f1b4Smrg * matrix in the current stack and the given parameters. Marks
4593464ebd5Sriastradh * __struct gl_contextRec::NewState with the dirty stack flag.
4607117f1b4Smrg */
4617117f1b4Smrgvoid GLAPIENTRY
4627117f1b4Smrg_mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
4637117f1b4Smrg{
4647117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
465af69d88dSmrg
466af69d88dSmrg   FLUSH_VERTICES(ctx, 0);
4677117f1b4Smrg   _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
4687117f1b4Smrg   ctx->NewState |= ctx->CurrentStack->DirtyFlag;
4697117f1b4Smrg}
4707117f1b4Smrg
4717117f1b4Smrg
4727117f1b4Smrgvoid GLAPIENTRY
4737117f1b4Smrg_mesa_LoadMatrixd( const GLdouble *m )
4747117f1b4Smrg{
4757117f1b4Smrg   GLint i;
4767117f1b4Smrg   GLfloat f[16];
4777117f1b4Smrg   if (!m) return;
4787117f1b4Smrg   for (i = 0; i < 16; i++)
4797117f1b4Smrg      f[i] = (GLfloat) m[i];
4807117f1b4Smrg   _mesa_LoadMatrixf(f);
4817117f1b4Smrg}
4827117f1b4Smrg
4837117f1b4Smrgvoid GLAPIENTRY
4847117f1b4Smrg_mesa_MultMatrixd( const GLdouble *m )
4857117f1b4Smrg{
4867117f1b4Smrg   GLint i;
4877117f1b4Smrg   GLfloat f[16];
4887117f1b4Smrg   if (!m) return;
4897117f1b4Smrg   for (i = 0; i < 16; i++)
4907117f1b4Smrg      f[i] = (GLfloat) m[i];
4917117f1b4Smrg   _mesa_MultMatrixf( f );
4927117f1b4Smrg}
4937117f1b4Smrg
4947117f1b4Smrg
4957117f1b4Smrgvoid GLAPIENTRY
4967117f1b4Smrg_mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
4977117f1b4Smrg{
4987117f1b4Smrg   _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
4997117f1b4Smrg}
5007117f1b4Smrg
5017117f1b4Smrg
5027117f1b4Smrgvoid GLAPIENTRY
5037117f1b4Smrg_mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
5047117f1b4Smrg{
5057117f1b4Smrg   _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
5067117f1b4Smrg}
5077117f1b4Smrg
5087117f1b4Smrg
5097117f1b4Smrgvoid GLAPIENTRY
5107117f1b4Smrg_mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
5117117f1b4Smrg{
5127117f1b4Smrg   _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
5137117f1b4Smrg}
5147117f1b4Smrg
5157117f1b4Smrg
5167117f1b4Smrgvoid GLAPIENTRY
517af69d88dSmrg_mesa_LoadTransposeMatrixf( const GLfloat *m )
5187117f1b4Smrg{
5197117f1b4Smrg   GLfloat tm[16];
5207117f1b4Smrg   if (!m) return;
5217117f1b4Smrg   _math_transposef(tm, m);
5227117f1b4Smrg   _mesa_LoadMatrixf(tm);
5237117f1b4Smrg}
5247117f1b4Smrg
5257117f1b4Smrg
5267117f1b4Smrgvoid GLAPIENTRY
527af69d88dSmrg_mesa_LoadTransposeMatrixd( const GLdouble *m )
5287117f1b4Smrg{
5297117f1b4Smrg   GLfloat tm[16];
5307117f1b4Smrg   if (!m) return;
5317117f1b4Smrg   _math_transposefd(tm, m);
5327117f1b4Smrg   _mesa_LoadMatrixf(tm);
5337117f1b4Smrg}
5347117f1b4Smrg
5357117f1b4Smrg
5367117f1b4Smrgvoid GLAPIENTRY
537af69d88dSmrg_mesa_MultTransposeMatrixf( const GLfloat *m )
5387117f1b4Smrg{
5397117f1b4Smrg   GLfloat tm[16];
5407117f1b4Smrg   if (!m) return;
5417117f1b4Smrg   _math_transposef(tm, m);
5427117f1b4Smrg   _mesa_MultMatrixf(tm);
5437117f1b4Smrg}
5447117f1b4Smrg
5457117f1b4Smrg
5467117f1b4Smrgvoid GLAPIENTRY
547af69d88dSmrg_mesa_MultTransposeMatrixd( const GLdouble *m )
5487117f1b4Smrg{
5497117f1b4Smrg   GLfloat tm[16];
5507117f1b4Smrg   if (!m) return;
5517117f1b4Smrg   _math_transposefd(tm, m);
5527117f1b4Smrg   _mesa_MultMatrixf(tm);
5537117f1b4Smrg}
5547117f1b4Smrg
5557117f1b4Smrg
5567117f1b4Smrg
5577117f1b4Smrg/**********************************************************************/
5587117f1b4Smrg/** \name State management */
5597117f1b4Smrg/*@{*/
5607117f1b4Smrg
5617117f1b4Smrg
5627117f1b4Smrg/**
5637117f1b4Smrg * Update the projection matrix stack.
5647117f1b4Smrg *
5657117f1b4Smrg * \param ctx GL context.
5667117f1b4Smrg *
5677117f1b4Smrg * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
5687117f1b4Smrg * stack, and recomputes user clip positions if necessary.
5697117f1b4Smrg *
5703464ebd5Sriastradh * \note This routine references __struct gl_contextRec::Tranform attribute
5713464ebd5Sriastradh * values to compute userclip positions in clip space, but is only called on
5727117f1b4Smrg * _NEW_PROJECTION.  The _mesa_ClipPlane() function keeps these values up to
5733464ebd5Sriastradh * date across changes to the __struct gl_contextRec::Transform attributes.
5747117f1b4Smrg */
5757117f1b4Smrgstatic void
5763464ebd5Sriastradhupdate_projection( struct gl_context *ctx )
5777117f1b4Smrg{
57801e04c3fSmrg   GLbitfield mask;
57901e04c3fSmrg
5807117f1b4Smrg   _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
5817117f1b4Smrg
5827117f1b4Smrg   /* Recompute clip plane positions in clipspace.  This is also done
5837117f1b4Smrg    * in _mesa_ClipPlane().
5847117f1b4Smrg    */
58501e04c3fSmrg   mask = ctx->Transform.ClipPlanesEnabled;
58601e04c3fSmrg   while (mask) {
58701e04c3fSmrg      const int p = u_bit_scan(&mask);
58801e04c3fSmrg
58901e04c3fSmrg      _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
59001e04c3fSmrg                              ctx->Transform.EyeUserPlane[p],
59101e04c3fSmrg                              ctx->ProjectionMatrixStack.Top->inv );
5927117f1b4Smrg   }
5937117f1b4Smrg}
5947117f1b4Smrg
5957117f1b4Smrg
5967117f1b4Smrg/**
5977117f1b4Smrg * Calculate the combined modelview-projection matrix.
5987117f1b4Smrg *
5997117f1b4Smrg * \param ctx GL context.
6007117f1b4Smrg *
6017117f1b4Smrg * Multiplies the top matrices of the projection and model view stacks into
6023464ebd5Sriastradh * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
6033464ebd5Sriastradh * and analyzes the resulting matrix via _math_matrix_analyse().
6047117f1b4Smrg */
6057117f1b4Smrgstatic void
6063464ebd5Sriastradhcalculate_model_project_matrix( struct gl_context *ctx )
6077117f1b4Smrg{
6087117f1b4Smrg   _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
6097117f1b4Smrg                            ctx->ProjectionMatrixStack.Top,
6107117f1b4Smrg                            ctx->ModelviewMatrixStack.Top );
6117117f1b4Smrg
6127117f1b4Smrg   _math_matrix_analyse( &ctx->_ModelProjectMatrix );
6137117f1b4Smrg}
6147117f1b4Smrg
6157117f1b4Smrg
6167117f1b4Smrg/**
6177117f1b4Smrg * Updates the combined modelview-projection matrix.
6187117f1b4Smrg *
6197117f1b4Smrg * \param ctx GL context.
6207117f1b4Smrg * \param new_state new state bit mask.
6217117f1b4Smrg *
6227117f1b4Smrg * If there is a new model view matrix then analyzes it. If there is a new
6237117f1b4Smrg * projection matrix, updates it. Finally calls
6247117f1b4Smrg * calculate_model_project_matrix() to recalculate the modelview-projection
6257117f1b4Smrg * matrix.
6267117f1b4Smrg */
6273464ebd5Sriastradhvoid _mesa_update_modelview_project( struct gl_context *ctx, GLuint new_state )
6287117f1b4Smrg{
629af69d88dSmrg   if (new_state & _NEW_MODELVIEW)
6307117f1b4Smrg      _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
6317117f1b4Smrg
6327117f1b4Smrg   if (new_state & _NEW_PROJECTION)
6337117f1b4Smrg      update_projection( ctx );
6347117f1b4Smrg
6353464ebd5Sriastradh   /* Keep ModelviewProject up to date always to allow tnl
6367117f1b4Smrg    * implementations that go model->clip even when eye is required.
6377117f1b4Smrg    */
6387117f1b4Smrg   calculate_model_project_matrix(ctx);
6397117f1b4Smrg}
6407117f1b4Smrg
6417117f1b4Smrg/*@}*/
6427117f1b4Smrg
6437117f1b4Smrg
6447117f1b4Smrg/**********************************************************************/
6457117f1b4Smrg/** Matrix stack initialization */
6467117f1b4Smrg/*@{*/
6477117f1b4Smrg
6487117f1b4Smrg
6497117f1b4Smrg/**
6507117f1b4Smrg * Initialize a matrix stack.
6517117f1b4Smrg *
6527117f1b4Smrg * \param stack matrix stack.
6537117f1b4Smrg * \param maxDepth maximum stack depth.
6547117f1b4Smrg * \param dirtyFlag dirty flag.
6557117f1b4Smrg *
6567117f1b4Smrg * Allocates an array of \p maxDepth elements for the matrix stack and calls
657af69d88dSmrg * _math_matrix_ctr() for each element to initialize it.
6587117f1b4Smrg */
6597117f1b4Smrgstatic void
66001e04c3fSmrginit_matrix_stack(struct gl_matrix_stack *stack,
66101e04c3fSmrg                  GLuint maxDepth, GLuint dirtyFlag)
6627117f1b4Smrg{
6637117f1b4Smrg   stack->Depth = 0;
6647117f1b4Smrg   stack->MaxDepth = maxDepth;
6657117f1b4Smrg   stack->DirtyFlag = dirtyFlag;
66601e04c3fSmrg   /* The stack will be dynamically resized at glPushMatrix() time */
66701e04c3fSmrg   stack->Stack = calloc(1, sizeof(GLmatrix));
66801e04c3fSmrg   stack->StackSize = 1;
66901e04c3fSmrg   _math_matrix_ctr(&stack->Stack[0]);
6707117f1b4Smrg   stack->Top = stack->Stack;
6717117f1b4Smrg}
6727117f1b4Smrg
6737117f1b4Smrg/**
6747117f1b4Smrg * Free matrix stack.
6757117f1b4Smrg *
6767117f1b4Smrg * \param stack matrix stack.
6777117f1b4Smrg *
6787117f1b4Smrg * Calls _math_matrix_dtr() for each element of the matrix stack and
6797117f1b4Smrg * frees the array.
6807117f1b4Smrg */
6817117f1b4Smrgstatic void
6827117f1b4Smrgfree_matrix_stack( struct gl_matrix_stack *stack )
6837117f1b4Smrg{
6847117f1b4Smrg   GLuint i;
68501e04c3fSmrg   for (i = 0; i < stack->StackSize; i++) {
6867117f1b4Smrg      _math_matrix_dtr(&stack->Stack[i]);
6877117f1b4Smrg   }
688af69d88dSmrg   free(stack->Stack);
6897117f1b4Smrg   stack->Stack = stack->Top = NULL;
69001e04c3fSmrg   stack->StackSize = 0;
6917117f1b4Smrg}
6927117f1b4Smrg
6937117f1b4Smrg/*@}*/
6947117f1b4Smrg
6957117f1b4Smrg
6967117f1b4Smrg/**********************************************************************/
6977117f1b4Smrg/** \name Initialization */
6987117f1b4Smrg/*@{*/
6997117f1b4Smrg
7007117f1b4Smrg
7017117f1b4Smrg/**
7027117f1b4Smrg * Initialize the context matrix data.
7037117f1b4Smrg *
7047117f1b4Smrg * \param ctx GL context.
7057117f1b4Smrg *
7067117f1b4Smrg * Initializes each of the matrix stacks and the combined modelview-projection
7077117f1b4Smrg * matrix.
7087117f1b4Smrg */
7093464ebd5Sriastradhvoid _mesa_init_matrix( struct gl_context * ctx )
7107117f1b4Smrg{
71101e04c3fSmrg   GLuint i;
7127117f1b4Smrg
7137117f1b4Smrg   /* Initialize matrix stacks */
7147117f1b4Smrg   init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
7157117f1b4Smrg                     _NEW_MODELVIEW);
7167117f1b4Smrg   init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
7177117f1b4Smrg                     _NEW_PROJECTION);
71801e04c3fSmrg   for (i = 0; i < ARRAY_SIZE(ctx->TextureMatrixStack); i++)
7197117f1b4Smrg      init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
7207117f1b4Smrg                        _NEW_TEXTURE_MATRIX);
72101e04c3fSmrg   for (i = 0; i < ARRAY_SIZE(ctx->ProgramMatrixStack); i++)
72201e04c3fSmrg      init_matrix_stack(&ctx->ProgramMatrixStack[i],
7237117f1b4Smrg		        MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
7247117f1b4Smrg   ctx->CurrentStack = &ctx->ModelviewMatrixStack;
7257117f1b4Smrg
7267117f1b4Smrg   /* Init combined Modelview*Projection matrix */
7277117f1b4Smrg   _math_matrix_ctr( &ctx->_ModelProjectMatrix );
7287117f1b4Smrg}
7297117f1b4Smrg
7307117f1b4Smrg
7317117f1b4Smrg/**
7327117f1b4Smrg * Free the context matrix data.
7337117f1b4Smrg *
7347117f1b4Smrg * \param ctx GL context.
7357117f1b4Smrg *
7367117f1b4Smrg * Frees each of the matrix stacks and the combined modelview-projection
7377117f1b4Smrg * matrix.
7387117f1b4Smrg */
7393464ebd5Sriastradhvoid _mesa_free_matrix_data( struct gl_context *ctx )
7407117f1b4Smrg{
74101e04c3fSmrg   GLuint i;
7427117f1b4Smrg
7437117f1b4Smrg   free_matrix_stack(&ctx->ModelviewMatrixStack);
7447117f1b4Smrg   free_matrix_stack(&ctx->ProjectionMatrixStack);
74501e04c3fSmrg   for (i = 0; i < ARRAY_SIZE(ctx->TextureMatrixStack); i++)
7467117f1b4Smrg      free_matrix_stack(&ctx->TextureMatrixStack[i]);
74701e04c3fSmrg   for (i = 0; i < ARRAY_SIZE(ctx->ProgramMatrixStack); i++)
7487117f1b4Smrg      free_matrix_stack(&ctx->ProgramMatrixStack[i]);
7497117f1b4Smrg   /* combined Modelview*Projection matrix */
7507117f1b4Smrg   _math_matrix_dtr( &ctx->_ModelProjectMatrix );
7517117f1b4Smrg
7527117f1b4Smrg}
7537117f1b4Smrg
7547117f1b4Smrg
7557117f1b4Smrg/**
7567117f1b4Smrg * Initialize the context transform attribute group.
7577117f1b4Smrg *
7587117f1b4Smrg * \param ctx GL context.
7597117f1b4Smrg *
7607117f1b4Smrg * \todo Move this to a new file with other 'transform' routines.
7617117f1b4Smrg */
7623464ebd5Sriastradhvoid _mesa_init_transform( struct gl_context *ctx )
7637117f1b4Smrg{
764af69d88dSmrg   GLuint i;
7657117f1b4Smrg
7667117f1b4Smrg   /* Transformation group */
7677117f1b4Smrg   ctx->Transform.MatrixMode = GL_MODELVIEW;
7687117f1b4Smrg   ctx->Transform.Normalize = GL_FALSE;
7697117f1b4Smrg   ctx->Transform.RescaleNormals = GL_FALSE;
7707117f1b4Smrg   ctx->Transform.RasterPositionUnclipped = GL_FALSE;
771af69d88dSmrg   for (i=0;i<ctx->Const.MaxClipPlanes;i++) {
7727117f1b4Smrg      ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
7737117f1b4Smrg   }
7747117f1b4Smrg   ctx->Transform.ClipPlanesEnabled = 0;
7757117f1b4Smrg}
7767117f1b4Smrg
7777117f1b4Smrg
7787117f1b4Smrg/*@}*/
779