17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
37117f1b4Smrg *
47117f1b4Smrg * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
57117f1b4Smrg *
67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
87117f1b4Smrg * to deal in the Software without restriction, including without limitation
97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
117117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
127117f1b4Smrg *
137117f1b4Smrg * The above copyright notice and this permission notice shall be included
147117f1b4Smrg * in all copies or substantial portions of the Software.
157117f1b4Smrg *
167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
237117f1b4Smrg */
247117f1b4Smrg
257117f1b4Smrg
267117f1b4Smrg/**
277117f1b4Smrg * \file m_matrix.c
287117f1b4Smrg * Matrix operations.
297117f1b4Smrg *
307117f1b4Smrg * \note
317117f1b4Smrg * -# 4x4 transformation matrices are stored in memory in column major order.
327117f1b4Smrg * -# Points/vertices are to be thought of as column vectors.
337117f1b4Smrg * -# Transformation of a point p by a matrix M is: p' = M * p
347117f1b4Smrg */
357117f1b4Smrg
367ec681f3Smrg#include <stddef.h>
377117f1b4Smrg
3801e04c3fSmrg#include "c99_math.h"
3901e04c3fSmrg#include "main/errors.h"
40c1f859d4Smrg#include "main/glheader.h"
41c1f859d4Smrg#include "main/macros.h"
427ec681f3Smrg#define MATH_ASM_PTR_SIZE sizeof(void *)
437ec681f3Smrg#include "math/m_vector_asm.h"
447117f1b4Smrg
457117f1b4Smrg#include "m_matrix.h"
467117f1b4Smrg
477ec681f3Smrg#include "util/u_memory.h"
487ec681f3Smrg
497117f1b4Smrg
507117f1b4Smrg/**
517117f1b4Smrg * \defgroup MatFlags MAT_FLAG_XXX-flags
527117f1b4Smrg *
537117f1b4Smrg * Bitmasks to indicate different kinds of 4x4 matrices in GLmatrix::flags
547117f1b4Smrg */
557117f1b4Smrg/*@{*/
567117f1b4Smrg#define MAT_FLAG_IDENTITY       0     /**< is an identity matrix flag.
577117f1b4Smrg                                       *   (Not actually used - the identity
5801e04c3fSmrg                                       *   matrix is identified by the absence
597117f1b4Smrg                                       *   of all other flags.)
607117f1b4Smrg                                       */
617117f1b4Smrg#define MAT_FLAG_GENERAL        0x1   /**< is a general matrix flag */
627117f1b4Smrg#define MAT_FLAG_ROTATION       0x2   /**< is a rotation matrix flag */
637117f1b4Smrg#define MAT_FLAG_TRANSLATION    0x4   /**< is a translation matrix flag */
647117f1b4Smrg#define MAT_FLAG_UNIFORM_SCALE  0x8   /**< is an uniform scaling matrix flag */
657117f1b4Smrg#define MAT_FLAG_GENERAL_SCALE  0x10  /**< is a general scaling matrix flag */
667117f1b4Smrg#define MAT_FLAG_GENERAL_3D     0x20  /**< general 3D matrix flag */
677117f1b4Smrg#define MAT_FLAG_PERSPECTIVE    0x40  /**< is a perspective proj matrix flag */
687117f1b4Smrg#define MAT_FLAG_SINGULAR       0x80  /**< is a singular matrix flag */
697117f1b4Smrg#define MAT_DIRTY_TYPE          0x100  /**< matrix type is dirty */
707117f1b4Smrg#define MAT_DIRTY_FLAGS         0x200  /**< matrix flags are dirty */
717117f1b4Smrg#define MAT_DIRTY_INVERSE       0x400  /**< matrix inverse is dirty */
727117f1b4Smrg
737117f1b4Smrg/** angle preserving matrix flags mask */
747117f1b4Smrg#define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \
757117f1b4Smrg				    MAT_FLAG_TRANSLATION | \
767117f1b4Smrg				    MAT_FLAG_UNIFORM_SCALE)
777117f1b4Smrg
787117f1b4Smrg/** geometry related matrix flags mask */
797117f1b4Smrg#define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \
807117f1b4Smrg			    MAT_FLAG_ROTATION | \
817117f1b4Smrg			    MAT_FLAG_TRANSLATION | \
827117f1b4Smrg			    MAT_FLAG_UNIFORM_SCALE | \
837117f1b4Smrg			    MAT_FLAG_GENERAL_SCALE | \
847117f1b4Smrg			    MAT_FLAG_GENERAL_3D | \
857117f1b4Smrg			    MAT_FLAG_PERSPECTIVE | \
867117f1b4Smrg	                    MAT_FLAG_SINGULAR)
877117f1b4Smrg
887117f1b4Smrg/** length preserving matrix flags mask */
897117f1b4Smrg#define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \
907117f1b4Smrg				     MAT_FLAG_TRANSLATION)
917117f1b4Smrg
927117f1b4Smrg
937117f1b4Smrg/** 3D (non-perspective) matrix flags mask */
947117f1b4Smrg#define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \
957117f1b4Smrg		      MAT_FLAG_TRANSLATION | \
967117f1b4Smrg		      MAT_FLAG_UNIFORM_SCALE | \
977117f1b4Smrg		      MAT_FLAG_GENERAL_SCALE | \
987117f1b4Smrg		      MAT_FLAG_GENERAL_3D)
997117f1b4Smrg
1007117f1b4Smrg/** dirty matrix flags mask */
1017117f1b4Smrg#define MAT_DIRTY          (MAT_DIRTY_TYPE | \
1027117f1b4Smrg			    MAT_DIRTY_FLAGS | \
1037117f1b4Smrg			    MAT_DIRTY_INVERSE)
1047117f1b4Smrg
1057117f1b4Smrg/*@}*/
1067117f1b4Smrg
1077117f1b4Smrg
1087ec681f3Smrg/**
1097117f1b4Smrg * Test geometry related matrix flags.
1107ec681f3Smrg *
1117117f1b4Smrg * \param mat a pointer to a GLmatrix structure.
1127117f1b4Smrg * \param a flags mask.
1137117f1b4Smrg *
1147117f1b4Smrg * \returns non-zero if all geometry related matrix flags are contained within
1157117f1b4Smrg * the mask, or zero otherwise.
1167ec681f3Smrg */
1177117f1b4Smrg#define TEST_MAT_FLAGS(mat, a)  \
1187117f1b4Smrg    ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)
1197117f1b4Smrg
1207117f1b4Smrg
1217117f1b4Smrg
1227117f1b4Smrg/**
1237117f1b4Smrg * Names of the corresponding GLmatrixtype values.
1247117f1b4Smrg */
1257117f1b4Smrgstatic const char *types[] = {
1267117f1b4Smrg   "MATRIX_GENERAL",
1277117f1b4Smrg   "MATRIX_IDENTITY",
1287117f1b4Smrg   "MATRIX_3D_NO_ROT",
1297117f1b4Smrg   "MATRIX_PERSPECTIVE",
1307117f1b4Smrg   "MATRIX_2D",
1317117f1b4Smrg   "MATRIX_2D_NO_ROT",
1327117f1b4Smrg   "MATRIX_3D"
1337117f1b4Smrg};
1347117f1b4Smrg
1357117f1b4Smrg
1367117f1b4Smrg/**
1377117f1b4Smrg * Identity matrix.
1387117f1b4Smrg */
13901e04c3fSmrgstatic const GLfloat Identity[16] = {
1407117f1b4Smrg   1.0, 0.0, 0.0, 0.0,
1417117f1b4Smrg   0.0, 1.0, 0.0, 0.0,
1427117f1b4Smrg   0.0, 0.0, 1.0, 0.0,
1437117f1b4Smrg   0.0, 0.0, 0.0, 1.0
1447117f1b4Smrg};
1457117f1b4Smrg
1467117f1b4Smrg
1477117f1b4Smrg
1487117f1b4Smrg/**********************************************************************/
1497117f1b4Smrg/** \name Matrix multiplication */
1507117f1b4Smrg/*@{*/
1517117f1b4Smrg
1527117f1b4Smrg#define A(row,col)  a[(col<<2)+row]
1537117f1b4Smrg#define B(row,col)  b[(col<<2)+row]
1547117f1b4Smrg#define P(row,col)  product[(col<<2)+row]
1557117f1b4Smrg
1567117f1b4Smrg/**
1577117f1b4Smrg * Perform a full 4x4 matrix multiplication.
1587117f1b4Smrg *
1597117f1b4Smrg * \param a matrix.
1607117f1b4Smrg * \param b matrix.
1617117f1b4Smrg * \param product will receive the product of \p a and \p b.
1627117f1b4Smrg *
1637117f1b4Smrg * \warning Is assumed that \p product != \p b. \p product == \p a is allowed.
1647117f1b4Smrg *
1657117f1b4Smrg * \note KW: 4*16 = 64 multiplications
1667ec681f3Smrg *
1677117f1b4Smrg * \author This \c matmul was contributed by Thomas Malik
1687117f1b4Smrg */
1697117f1b4Smrgstatic void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
1707117f1b4Smrg{
1717117f1b4Smrg   GLint i;
1727117f1b4Smrg   for (i = 0; i < 4; i++) {
1737117f1b4Smrg      const GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);
1747117f1b4Smrg      P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
1757117f1b4Smrg      P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
1767117f1b4Smrg      P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
1777117f1b4Smrg      P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
1787117f1b4Smrg   }
1797117f1b4Smrg}
1807117f1b4Smrg
1817117f1b4Smrg/**
1827117f1b4Smrg * Multiply two matrices known to occupy only the top three rows, such
1837117f1b4Smrg * as typical model matrices, and orthogonal matrices.
1847117f1b4Smrg *
1857117f1b4Smrg * \param a matrix.
1867117f1b4Smrg * \param b matrix.
1877117f1b4Smrg * \param product will receive the product of \p a and \p b.
1887117f1b4Smrg */
1897117f1b4Smrgstatic void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
1907117f1b4Smrg{
1917117f1b4Smrg   GLint i;
1927117f1b4Smrg   for (i = 0; i < 3; i++) {
1937117f1b4Smrg      const GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);
1947117f1b4Smrg      P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
1957117f1b4Smrg      P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
1967117f1b4Smrg      P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
1977117f1b4Smrg      P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
1987117f1b4Smrg   }
1997117f1b4Smrg   P(3,0) = 0;
2007117f1b4Smrg   P(3,1) = 0;
2017117f1b4Smrg   P(3,2) = 0;
2027117f1b4Smrg   P(3,3) = 1;
2037117f1b4Smrg}
2047117f1b4Smrg
2057117f1b4Smrg#undef A
2067117f1b4Smrg#undef B
2077117f1b4Smrg#undef P
2087117f1b4Smrg
2097117f1b4Smrg/**
2107117f1b4Smrg * Multiply a matrix by an array of floats with known properties.
2117117f1b4Smrg *
2127117f1b4Smrg * \param mat pointer to a GLmatrix structure containing the left multiplication
2137117f1b4Smrg * matrix, and that will receive the product result.
2147117f1b4Smrg * \param m right multiplication matrix array.
2157117f1b4Smrg * \param flags flags of the matrix \p m.
2167ec681f3Smrg *
2177117f1b4Smrg * Joins both flags and marks the type and inverse as dirty.  Calls matmul34()
2187117f1b4Smrg * if both matrices are 3D, or matmul4() otherwise.
2197117f1b4Smrg */
2207117f1b4Smrgstatic void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
2217117f1b4Smrg{
2227117f1b4Smrg   mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
2237117f1b4Smrg
2247117f1b4Smrg   if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
2257117f1b4Smrg      matmul34( mat->m, mat->m, m );
2267117f1b4Smrg   else
2277117f1b4Smrg      matmul4( mat->m, mat->m, m );
2287117f1b4Smrg}
2297117f1b4Smrg
2307117f1b4Smrg/**
2317117f1b4Smrg * Matrix multiplication.
2327117f1b4Smrg *
2337117f1b4Smrg * \param dest destination matrix.
2347117f1b4Smrg * \param a left matrix.
2357117f1b4Smrg * \param b right matrix.
2367ec681f3Smrg *
2377117f1b4Smrg * Joins both flags and marks the type and inverse as dirty.  Calls matmul34()
2387117f1b4Smrg * if both matrices are 3D, or matmul4() otherwise.
2397117f1b4Smrg */
2407117f1b4Smrgvoid
2417117f1b4Smrg_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
2427117f1b4Smrg{
2437117f1b4Smrg   dest->flags = (a->flags |
2447117f1b4Smrg		  b->flags |
2457117f1b4Smrg		  MAT_DIRTY_TYPE |
2467117f1b4Smrg		  MAT_DIRTY_INVERSE);
2477117f1b4Smrg
2487117f1b4Smrg   if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
2497117f1b4Smrg      matmul34( dest->m, a->m, b->m );
2507117f1b4Smrg   else
2517117f1b4Smrg      matmul4( dest->m, a->m, b->m );
2527117f1b4Smrg}
2537117f1b4Smrg
2547117f1b4Smrg/**
2557117f1b4Smrg * Matrix multiplication.
2567117f1b4Smrg *
2577117f1b4Smrg * \param dest left and destination matrix.
2587117f1b4Smrg * \param m right matrix array.
2597ec681f3Smrg *
2607117f1b4Smrg * Marks the matrix flags with general flag, and type and inverse dirty flags.
2617117f1b4Smrg * Calls matmul4() for the multiplication.
2627117f1b4Smrg */
2637117f1b4Smrgvoid
2647117f1b4Smrg_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
2657117f1b4Smrg{
2667117f1b4Smrg   dest->flags |= (MAT_FLAG_GENERAL |
2677117f1b4Smrg		   MAT_DIRTY_TYPE |
2687117f1b4Smrg		   MAT_DIRTY_INVERSE |
2697117f1b4Smrg                   MAT_DIRTY_FLAGS);
2707117f1b4Smrg
2717117f1b4Smrg   matmul4( dest->m, dest->m, m );
2727117f1b4Smrg}
2737117f1b4Smrg
2747117f1b4Smrg/*@}*/
2757117f1b4Smrg
2767117f1b4Smrg
2777117f1b4Smrg/**********************************************************************/
2787117f1b4Smrg/** \name Matrix output */
2797117f1b4Smrg/*@{*/
2807117f1b4Smrg
2817117f1b4Smrg/**
2827117f1b4Smrg * Print a matrix array.
2837117f1b4Smrg *
2847117f1b4Smrg * \param m matrix array.
2857117f1b4Smrg *
2867117f1b4Smrg * Called by _math_matrix_print() to print a matrix or its inverse.
2877117f1b4Smrg */
2887117f1b4Smrgstatic void print_matrix_floats( const GLfloat m[16] )
2897117f1b4Smrg{
2907117f1b4Smrg   int i;
2917117f1b4Smrg   for (i=0;i<4;i++) {
2927117f1b4Smrg      _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
2937117f1b4Smrg   }
2947117f1b4Smrg}
2957117f1b4Smrg
2967117f1b4Smrg/**
2977117f1b4Smrg * Dumps the contents of a GLmatrix structure.
2987ec681f3Smrg *
2997117f1b4Smrg * \param m pointer to the GLmatrix structure.
3007117f1b4Smrg */
3017117f1b4Smrgvoid
3027117f1b4Smrg_math_matrix_print( const GLmatrix *m )
3037117f1b4Smrg{
304af69d88dSmrg   GLfloat prod[16];
305af69d88dSmrg
3067117f1b4Smrg   _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
3077117f1b4Smrg   print_matrix_floats(m->m);
3087117f1b4Smrg   _mesa_debug(NULL, "Inverse: \n");
309af69d88dSmrg   print_matrix_floats(m->inv);
310af69d88dSmrg   matmul4(prod, m->m, m->inv);
311af69d88dSmrg   _mesa_debug(NULL, "Mat * Inverse:\n");
312af69d88dSmrg   print_matrix_floats(prod);
3137117f1b4Smrg}
3147117f1b4Smrg
3157117f1b4Smrg/*@}*/
3167117f1b4Smrg
3177117f1b4Smrg
3187117f1b4Smrg/**
3197117f1b4Smrg * References an element of 4x4 matrix.
3207117f1b4Smrg *
3217117f1b4Smrg * \param m matrix array.
3227117f1b4Smrg * \param c column of the desired element.
3237117f1b4Smrg * \param r row of the desired element.
3247ec681f3Smrg *
3257117f1b4Smrg * \return value of the desired element.
3267117f1b4Smrg *
3277ec681f3Smrg * Calculate the linear storage index of the element and references it.
3287117f1b4Smrg */
3297117f1b4Smrg#define MAT(m,r,c) (m)[(c)*4+(r)]
3307117f1b4Smrg
3317117f1b4Smrg
3327117f1b4Smrg/**********************************************************************/
3337117f1b4Smrg/** \name Matrix inversion */
3347117f1b4Smrg/*@{*/
3357117f1b4Smrg
3367117f1b4Smrg/**
3377117f1b4Smrg * Compute inverse of 4x4 transformation matrix.
3387ec681f3Smrg *
3397117f1b4Smrg * \param mat pointer to a GLmatrix structure. The matrix inverse will be
3407117f1b4Smrg * stored in the GLmatrix::inv attribute.
3417ec681f3Smrg *
3427117f1b4Smrg * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
3437ec681f3Smrg *
3447117f1b4Smrg * \author
3457117f1b4Smrg * Code contributed by Jacques Leroy jle@star.be
3467117f1b4Smrg *
3477117f1b4Smrg * Calculates the inverse matrix by performing the gaussian matrix reduction
3487117f1b4Smrg * with partial pivoting followed by back/substitution with the loops manually
3497117f1b4Smrg * unrolled.
3507117f1b4Smrg */
3517117f1b4Smrgstatic GLboolean invert_matrix_general( GLmatrix *mat )
3527117f1b4Smrg{
3537ec681f3Smrg   return util_invert_mat4x4(mat->inv, mat->m);
3547117f1b4Smrg}
3557117f1b4Smrg
3567117f1b4Smrg/**
3577117f1b4Smrg * Compute inverse of a general 3d transformation matrix.
3587ec681f3Smrg *
3597117f1b4Smrg * \param mat pointer to a GLmatrix structure. The matrix inverse will be
3607117f1b4Smrg * stored in the GLmatrix::inv attribute.
3617ec681f3Smrg *
3627117f1b4Smrg * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
3637117f1b4Smrg *
3647117f1b4Smrg * \author Adapted from graphics gems II.
3657117f1b4Smrg *
3667117f1b4Smrg * Calculates the inverse of the upper left by first calculating its
3677117f1b4Smrg * determinant and multiplying it to the symmetric adjust matrix of each
3687117f1b4Smrg * element. Finally deals with the translation part by transforming the
3697117f1b4Smrg * original translation vector using by the calculated submatrix inverse.
3707117f1b4Smrg */
3717117f1b4Smrgstatic GLboolean invert_matrix_3d_general( GLmatrix *mat )
3727117f1b4Smrg{
3737117f1b4Smrg   const GLfloat *in = mat->m;
3747117f1b4Smrg   GLfloat *out = mat->inv;
3757117f1b4Smrg   GLfloat pos, neg, t;
3767117f1b4Smrg   GLfloat det;
3777117f1b4Smrg
3787117f1b4Smrg   /* Calculate the determinant of upper left 3x3 submatrix and
3797117f1b4Smrg    * determine if the matrix is singular.
3807117f1b4Smrg    */
3817117f1b4Smrg   pos = neg = 0.0;
3827117f1b4Smrg   t =  MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
38301e04c3fSmrg   if (t >= 0.0F) pos += t; else neg += t;
3847117f1b4Smrg
3857117f1b4Smrg   t =  MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
38601e04c3fSmrg   if (t >= 0.0F) pos += t; else neg += t;
3877117f1b4Smrg
3887117f1b4Smrg   t =  MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
38901e04c3fSmrg   if (t >= 0.0F) pos += t; else neg += t;
3907117f1b4Smrg
3917117f1b4Smrg   t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
39201e04c3fSmrg   if (t >= 0.0F) pos += t; else neg += t;
3937117f1b4Smrg
3947117f1b4Smrg   t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
39501e04c3fSmrg   if (t >= 0.0F) pos += t; else neg += t;
3967117f1b4Smrg
3977117f1b4Smrg   t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
39801e04c3fSmrg   if (t >= 0.0F) pos += t; else neg += t;
3997117f1b4Smrg
4007117f1b4Smrg   det = pos + neg;
4017117f1b4Smrg
40201e04c3fSmrg   if (fabsf(det) < 1e-25F)
4037117f1b4Smrg      return GL_FALSE;
4047117f1b4Smrg
4057117f1b4Smrg   det = 1.0F / det;
4067117f1b4Smrg   MAT(out,0,0) = (  (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
4077117f1b4Smrg   MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
4087117f1b4Smrg   MAT(out,0,2) = (  (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
4097117f1b4Smrg   MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
4107117f1b4Smrg   MAT(out,1,1) = (  (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
4117117f1b4Smrg   MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
4127117f1b4Smrg   MAT(out,2,0) = (  (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
4137117f1b4Smrg   MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
4147117f1b4Smrg   MAT(out,2,2) = (  (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
4157117f1b4Smrg
4167117f1b4Smrg   /* Do the translation part */
4177117f1b4Smrg   MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
4187117f1b4Smrg		     MAT(in,1,3) * MAT(out,0,1) +
4197117f1b4Smrg		     MAT(in,2,3) * MAT(out,0,2) );
4207117f1b4Smrg   MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
4217117f1b4Smrg		     MAT(in,1,3) * MAT(out,1,1) +
4227117f1b4Smrg		     MAT(in,2,3) * MAT(out,1,2) );
4237117f1b4Smrg   MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
4247117f1b4Smrg		     MAT(in,1,3) * MAT(out,2,1) +
4257117f1b4Smrg		     MAT(in,2,3) * MAT(out,2,2) );
4267117f1b4Smrg
4277117f1b4Smrg   return GL_TRUE;
4287117f1b4Smrg}
4297117f1b4Smrg
4307117f1b4Smrg/**
4317117f1b4Smrg * Compute inverse of a 3d transformation matrix.
4327ec681f3Smrg *
4337117f1b4Smrg * \param mat pointer to a GLmatrix structure. The matrix inverse will be
4347117f1b4Smrg * stored in the GLmatrix::inv attribute.
4357ec681f3Smrg *
4367117f1b4Smrg * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
4377117f1b4Smrg *
4387117f1b4Smrg * If the matrix is not an angle preserving matrix then calls
4397117f1b4Smrg * invert_matrix_3d_general for the actual calculation. Otherwise calculates
4407117f1b4Smrg * the inverse matrix analyzing and inverting each of the scaling, rotation and
4417117f1b4Smrg * translation parts.
4427117f1b4Smrg */
4437117f1b4Smrgstatic GLboolean invert_matrix_3d( GLmatrix *mat )
4447117f1b4Smrg{
4457117f1b4Smrg   const GLfloat *in = mat->m;
4467117f1b4Smrg   GLfloat *out = mat->inv;
4477117f1b4Smrg
4487117f1b4Smrg   if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
4497117f1b4Smrg      return invert_matrix_3d_general( mat );
4507117f1b4Smrg   }
4517117f1b4Smrg
4527117f1b4Smrg   if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
4537117f1b4Smrg      GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
4547117f1b4Smrg                       MAT(in,0,1) * MAT(in,0,1) +
4557117f1b4Smrg                       MAT(in,0,2) * MAT(in,0,2));
4567117f1b4Smrg
45701e04c3fSmrg      if (scale == 0.0F)
4587117f1b4Smrg         return GL_FALSE;
4597117f1b4Smrg
4607117f1b4Smrg      scale = 1.0F / scale;
4617117f1b4Smrg
4627117f1b4Smrg      /* Transpose and scale the 3 by 3 upper-left submatrix. */
4637117f1b4Smrg      MAT(out,0,0) = scale * MAT(in,0,0);
4647117f1b4Smrg      MAT(out,1,0) = scale * MAT(in,0,1);
4657117f1b4Smrg      MAT(out,2,0) = scale * MAT(in,0,2);
4667117f1b4Smrg      MAT(out,0,1) = scale * MAT(in,1,0);
4677117f1b4Smrg      MAT(out,1,1) = scale * MAT(in,1,1);
4687117f1b4Smrg      MAT(out,2,1) = scale * MAT(in,1,2);
4697117f1b4Smrg      MAT(out,0,2) = scale * MAT(in,2,0);
4707117f1b4Smrg      MAT(out,1,2) = scale * MAT(in,2,1);
4717117f1b4Smrg      MAT(out,2,2) = scale * MAT(in,2,2);
4727117f1b4Smrg   }
4737117f1b4Smrg   else if (mat->flags & MAT_FLAG_ROTATION) {
4747117f1b4Smrg      /* Transpose the 3 by 3 upper-left submatrix. */
4757117f1b4Smrg      MAT(out,0,0) = MAT(in,0,0);
4767117f1b4Smrg      MAT(out,1,0) = MAT(in,0,1);
4777117f1b4Smrg      MAT(out,2,0) = MAT(in,0,2);
4787117f1b4Smrg      MAT(out,0,1) = MAT(in,1,0);
4797117f1b4Smrg      MAT(out,1,1) = MAT(in,1,1);
4807117f1b4Smrg      MAT(out,2,1) = MAT(in,1,2);
4817117f1b4Smrg      MAT(out,0,2) = MAT(in,2,0);
4827117f1b4Smrg      MAT(out,1,2) = MAT(in,2,1);
4837117f1b4Smrg      MAT(out,2,2) = MAT(in,2,2);
4847117f1b4Smrg   }
4857117f1b4Smrg   else {
4867117f1b4Smrg      /* pure translation */
487cdc920a0Smrg      memcpy( out, Identity, sizeof(Identity) );
4887117f1b4Smrg      MAT(out,0,3) = - MAT(in,0,3);
4897117f1b4Smrg      MAT(out,1,3) = - MAT(in,1,3);
4907117f1b4Smrg      MAT(out,2,3) = - MAT(in,2,3);
4917117f1b4Smrg      return GL_TRUE;
4927117f1b4Smrg   }
4937117f1b4Smrg
4947117f1b4Smrg   if (mat->flags & MAT_FLAG_TRANSLATION) {
4957117f1b4Smrg      /* Do the translation part */
4967117f1b4Smrg      MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
4977117f1b4Smrg			MAT(in,1,3) * MAT(out,0,1) +
4987117f1b4Smrg			MAT(in,2,3) * MAT(out,0,2) );
4997117f1b4Smrg      MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
5007117f1b4Smrg			MAT(in,1,3) * MAT(out,1,1) +
5017117f1b4Smrg			MAT(in,2,3) * MAT(out,1,2) );
5027117f1b4Smrg      MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
5037117f1b4Smrg			MAT(in,1,3) * MAT(out,2,1) +
5047117f1b4Smrg			MAT(in,2,3) * MAT(out,2,2) );
5057117f1b4Smrg   }
5067117f1b4Smrg   else {
5077117f1b4Smrg      MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
5087117f1b4Smrg   }
5097117f1b4Smrg
5107117f1b4Smrg   return GL_TRUE;
5117117f1b4Smrg}
5127117f1b4Smrg
5137117f1b4Smrg/**
5147117f1b4Smrg * Compute inverse of an identity transformation matrix.
5157ec681f3Smrg *
5167117f1b4Smrg * \param mat pointer to a GLmatrix structure. The matrix inverse will be
5177117f1b4Smrg * stored in the GLmatrix::inv attribute.
5187ec681f3Smrg *
5197117f1b4Smrg * \return always GL_TRUE.
5207117f1b4Smrg *
5217117f1b4Smrg * Simply copies Identity into GLmatrix::inv.
5227117f1b4Smrg */
5237117f1b4Smrgstatic GLboolean invert_matrix_identity( GLmatrix *mat )
5247117f1b4Smrg{
525cdc920a0Smrg   memcpy( mat->inv, Identity, sizeof(Identity) );
5267117f1b4Smrg   return GL_TRUE;
5277117f1b4Smrg}
5287117f1b4Smrg
5297117f1b4Smrg/**
5307117f1b4Smrg * Compute inverse of a no-rotation 3d transformation matrix.
5317ec681f3Smrg *
5327117f1b4Smrg * \param mat pointer to a GLmatrix structure. The matrix inverse will be
5337117f1b4Smrg * stored in the GLmatrix::inv attribute.
5347ec681f3Smrg *
5357117f1b4Smrg * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
5367117f1b4Smrg *
5377ec681f3Smrg * Calculates the
5387117f1b4Smrg */
5397117f1b4Smrgstatic GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
5407117f1b4Smrg{
5417117f1b4Smrg   const GLfloat *in = mat->m;
5427117f1b4Smrg   GLfloat *out = mat->inv;
5437117f1b4Smrg
5447117f1b4Smrg   if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
5457117f1b4Smrg      return GL_FALSE;
5467117f1b4Smrg
54701e04c3fSmrg   memcpy( out, Identity, sizeof(Identity) );
5487117f1b4Smrg   MAT(out,0,0) = 1.0F / MAT(in,0,0);
5497117f1b4Smrg   MAT(out,1,1) = 1.0F / MAT(in,1,1);
5507117f1b4Smrg   MAT(out,2,2) = 1.0F / MAT(in,2,2);
5517117f1b4Smrg
5527117f1b4Smrg   if (mat->flags & MAT_FLAG_TRANSLATION) {
5537117f1b4Smrg      MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
5547117f1b4Smrg      MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
5557117f1b4Smrg      MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
5567117f1b4Smrg   }
5577117f1b4Smrg
5587117f1b4Smrg   return GL_TRUE;
5597117f1b4Smrg}
5607117f1b4Smrg
5617117f1b4Smrg/**
5627117f1b4Smrg * Compute inverse of a no-rotation 2d transformation matrix.
5637ec681f3Smrg *
5647117f1b4Smrg * \param mat pointer to a GLmatrix structure. The matrix inverse will be
5657117f1b4Smrg * stored in the GLmatrix::inv attribute.
5667ec681f3Smrg *
5677117f1b4Smrg * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
5687117f1b4Smrg *
5697117f1b4Smrg * Calculates the inverse matrix by applying the inverse scaling and
5707117f1b4Smrg * translation to the identity matrix.
5717117f1b4Smrg */
5727117f1b4Smrgstatic GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
5737117f1b4Smrg{
5747117f1b4Smrg   const GLfloat *in = mat->m;
5757117f1b4Smrg   GLfloat *out = mat->inv;
5767117f1b4Smrg
5777117f1b4Smrg   if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
5787117f1b4Smrg      return GL_FALSE;
5797117f1b4Smrg
58001e04c3fSmrg   memcpy( out, Identity, sizeof(Identity) );
5817117f1b4Smrg   MAT(out,0,0) = 1.0F / MAT(in,0,0);
5827117f1b4Smrg   MAT(out,1,1) = 1.0F / MAT(in,1,1);
5837117f1b4Smrg
5847117f1b4Smrg   if (mat->flags & MAT_FLAG_TRANSLATION) {
5857117f1b4Smrg      MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
5867117f1b4Smrg      MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
5877117f1b4Smrg   }
5887117f1b4Smrg
5897117f1b4Smrg   return GL_TRUE;
5907117f1b4Smrg}
5917117f1b4Smrg
5927117f1b4Smrg#if 0
5937117f1b4Smrg/* broken */
5947117f1b4Smrgstatic GLboolean invert_matrix_perspective( GLmatrix *mat )
5957117f1b4Smrg{
5967117f1b4Smrg   const GLfloat *in = mat->m;
5977117f1b4Smrg   GLfloat *out = mat->inv;
5987117f1b4Smrg
5997117f1b4Smrg   if (MAT(in,2,3) == 0)
6007117f1b4Smrg      return GL_FALSE;
6017117f1b4Smrg
60201e04c3fSmrg   memcpy( out, Identity, sizeof(Identity) );
6037117f1b4Smrg
6047117f1b4Smrg   MAT(out,0,0) = 1.0F / MAT(in,0,0);
6057117f1b4Smrg   MAT(out,1,1) = 1.0F / MAT(in,1,1);
6067117f1b4Smrg
6077117f1b4Smrg   MAT(out,0,3) = MAT(in,0,2);
6087117f1b4Smrg   MAT(out,1,3) = MAT(in,1,2);
6097117f1b4Smrg
6107117f1b4Smrg   MAT(out,2,2) = 0;
6117117f1b4Smrg   MAT(out,2,3) = -1;
6127117f1b4Smrg
6137117f1b4Smrg   MAT(out,3,2) = 1.0F / MAT(in,2,3);
6147117f1b4Smrg   MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
6157117f1b4Smrg
6167117f1b4Smrg   return GL_TRUE;
6177117f1b4Smrg}
6187117f1b4Smrg#endif
6197117f1b4Smrg
6207117f1b4Smrg/**
6217117f1b4Smrg * Matrix inversion function pointer type.
6227117f1b4Smrg */
6237117f1b4Smrgtypedef GLboolean (*inv_mat_func)( GLmatrix *mat );
6247117f1b4Smrg
6257117f1b4Smrg/**
6267117f1b4Smrg * Table of the matrix inversion functions according to the matrix type.
6277117f1b4Smrg */
6287117f1b4Smrgstatic inv_mat_func inv_mat_tab[7] = {
6297117f1b4Smrg   invert_matrix_general,
6307117f1b4Smrg   invert_matrix_identity,
6317117f1b4Smrg   invert_matrix_3d_no_rot,
6327117f1b4Smrg#if 0
6337117f1b4Smrg   /* Don't use this function for now - it fails when the projection matrix
6347117f1b4Smrg    * is premultiplied by a translation (ala Chromium's tilesort SPU).
6357117f1b4Smrg    */
6367117f1b4Smrg   invert_matrix_perspective,
6377117f1b4Smrg#else
6387117f1b4Smrg   invert_matrix_general,
6397117f1b4Smrg#endif
6407117f1b4Smrg   invert_matrix_3d,		/* lazy! */
6417117f1b4Smrg   invert_matrix_2d_no_rot,
6427117f1b4Smrg   invert_matrix_3d
6437117f1b4Smrg};
6447117f1b4Smrg
6457117f1b4Smrg/**
6467117f1b4Smrg * Compute inverse of a transformation matrix.
6477ec681f3Smrg *
6487117f1b4Smrg * \param mat pointer to a GLmatrix structure. The matrix inverse will be
6497117f1b4Smrg * stored in the GLmatrix::inv attribute.
6507ec681f3Smrg *
6517117f1b4Smrg * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
6527117f1b4Smrg *
6537117f1b4Smrg * Calls the matrix inversion function in inv_mat_tab corresponding to the
6547117f1b4Smrg * given matrix type.  In case of failure, updates the MAT_FLAG_SINGULAR flag,
6557117f1b4Smrg * and copies the identity matrix into GLmatrix::inv.
6567117f1b4Smrg */
6577117f1b4Smrgstatic GLboolean matrix_invert( GLmatrix *mat )
6587117f1b4Smrg{
6597117f1b4Smrg   if (inv_mat_tab[mat->type](mat)) {
6607117f1b4Smrg      mat->flags &= ~MAT_FLAG_SINGULAR;
6617117f1b4Smrg      return GL_TRUE;
6627117f1b4Smrg   } else {
6637117f1b4Smrg      mat->flags |= MAT_FLAG_SINGULAR;
664cdc920a0Smrg      memcpy( mat->inv, Identity, sizeof(Identity) );
6657117f1b4Smrg      return GL_FALSE;
6667117f1b4Smrg   }
6677117f1b4Smrg}
6687117f1b4Smrg
6697117f1b4Smrg/*@}*/
6707117f1b4Smrg
6717117f1b4Smrg
6727117f1b4Smrg/**********************************************************************/
6737117f1b4Smrg/** \name Matrix generation */
6747117f1b4Smrg/*@{*/
6757117f1b4Smrg
6767117f1b4Smrg/**
6777117f1b4Smrg * Generate a 4x4 transformation matrix from glRotate parameters, and
6787117f1b4Smrg * post-multiply the input matrix by it.
6797117f1b4Smrg *
6807117f1b4Smrg * \author
6817117f1b4Smrg * This function was contributed by Erich Boleyn (erich@uruk.org).
6827117f1b4Smrg * Optimizations contributed by Rudolf Opalla (rudi@khm.de).
6837117f1b4Smrg */
6847117f1b4Smrgvoid
6857117f1b4Smrg_math_matrix_rotate( GLmatrix *mat,
6867117f1b4Smrg		     GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
6877117f1b4Smrg{
6887117f1b4Smrg   GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
6897117f1b4Smrg   GLfloat m[16];
6907117f1b4Smrg   GLboolean optimized;
6917117f1b4Smrg
69201e04c3fSmrg   s = sinf( angle * M_PI / 180.0 );
69301e04c3fSmrg   c = cosf( angle * M_PI / 180.0 );
6947117f1b4Smrg
69501e04c3fSmrg   memcpy(m, Identity, sizeof(Identity));
6967117f1b4Smrg   optimized = GL_FALSE;
6977117f1b4Smrg
6987117f1b4Smrg#define M(row,col)  m[col*4+row]
6997117f1b4Smrg
7007117f1b4Smrg   if (x == 0.0F) {
7017117f1b4Smrg      if (y == 0.0F) {
7027117f1b4Smrg         if (z != 0.0F) {
7037117f1b4Smrg            optimized = GL_TRUE;
7047117f1b4Smrg            /* rotate only around z-axis */
7057117f1b4Smrg            M(0,0) = c;
7067117f1b4Smrg            M(1,1) = c;
7077117f1b4Smrg            if (z < 0.0F) {
7087117f1b4Smrg               M(0,1) = s;
7097117f1b4Smrg               M(1,0) = -s;
7107117f1b4Smrg            }
7117117f1b4Smrg            else {
7127117f1b4Smrg               M(0,1) = -s;
7137117f1b4Smrg               M(1,0) = s;
7147117f1b4Smrg            }
7157117f1b4Smrg         }
7167117f1b4Smrg      }
7177117f1b4Smrg      else if (z == 0.0F) {
7187117f1b4Smrg         optimized = GL_TRUE;
7197117f1b4Smrg         /* rotate only around y-axis */
7207117f1b4Smrg         M(0,0) = c;
7217117f1b4Smrg         M(2,2) = c;
7227117f1b4Smrg         if (y < 0.0F) {
7237117f1b4Smrg            M(0,2) = -s;
7247117f1b4Smrg            M(2,0) = s;
7257117f1b4Smrg         }
7267117f1b4Smrg         else {
7277117f1b4Smrg            M(0,2) = s;
7287117f1b4Smrg            M(2,0) = -s;
7297117f1b4Smrg         }
7307117f1b4Smrg      }
7317117f1b4Smrg   }
7327117f1b4Smrg   else if (y == 0.0F) {
7337117f1b4Smrg      if (z == 0.0F) {
7347117f1b4Smrg         optimized = GL_TRUE;
7357117f1b4Smrg         /* rotate only around x-axis */
7367117f1b4Smrg         M(1,1) = c;
7377117f1b4Smrg         M(2,2) = c;
7387117f1b4Smrg         if (x < 0.0F) {
7397117f1b4Smrg            M(1,2) = s;
7407117f1b4Smrg            M(2,1) = -s;
7417117f1b4Smrg         }
7427117f1b4Smrg         else {
7437117f1b4Smrg            M(1,2) = -s;
7447117f1b4Smrg            M(2,1) = s;
7457117f1b4Smrg         }
7467117f1b4Smrg      }
7477117f1b4Smrg   }
7487117f1b4Smrg
7497117f1b4Smrg   if (!optimized) {
750af69d88dSmrg      const GLfloat mag = sqrtf(x * x + y * y + z * z);
7517117f1b4Smrg
75201e04c3fSmrg      if (mag <= 1.0e-4F) {
7537117f1b4Smrg         /* no rotation, leave mat as-is */
7547117f1b4Smrg         return;
7557117f1b4Smrg      }
7567117f1b4Smrg
7577117f1b4Smrg      x /= mag;
7587117f1b4Smrg      y /= mag;
7597117f1b4Smrg      z /= mag;
7607117f1b4Smrg
7617117f1b4Smrg
7627117f1b4Smrg      /*
7637117f1b4Smrg       *     Arbitrary axis rotation matrix.
7647117f1b4Smrg       *
7657117f1b4Smrg       *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
7667117f1b4Smrg       *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
7677117f1b4Smrg       *  (which is about the X-axis), and the two composite transforms
7687117f1b4Smrg       *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
7697117f1b4Smrg       *  from the arbitrary axis to the X-axis then back.  They are
7707117f1b4Smrg       *  all elementary rotations.
7717117f1b4Smrg       *
7727117f1b4Smrg       *  Rz' is a rotation about the Z-axis, to bring the axis vector
7737117f1b4Smrg       *  into the x-z plane.  Then Ry' is applied, rotating about the
7747117f1b4Smrg       *  Y-axis to bring the axis vector parallel with the X-axis.  The
7757117f1b4Smrg       *  rotation about the X-axis is then performed.  Ry and Rz are
7767117f1b4Smrg       *  simply the respective inverse transforms to bring the arbitrary
777cdc920a0Smrg       *  axis back to its original orientation.  The first transforms
7787117f1b4Smrg       *  Rz' and Ry' are considered inverses, since the data from the
7797117f1b4Smrg       *  arbitrary axis gives you info on how to get to it, not how
7807117f1b4Smrg       *  to get away from it, and an inverse must be applied.
7817117f1b4Smrg       *
7827117f1b4Smrg       *  The basic calculation used is to recognize that the arbitrary
7837117f1b4Smrg       *  axis vector (x, y, z), since it is of unit length, actually
7847117f1b4Smrg       *  represents the sines and cosines of the angles to rotate the
7857117f1b4Smrg       *  X-axis to the same orientation, with theta being the angle about
7867117f1b4Smrg       *  Z and phi the angle about Y (in the order described above)
7877117f1b4Smrg       *  as follows:
7887117f1b4Smrg       *
7897117f1b4Smrg       *  cos ( theta ) = x / sqrt ( 1 - z^2 )
7907117f1b4Smrg       *  sin ( theta ) = y / sqrt ( 1 - z^2 )
7917117f1b4Smrg       *
7927117f1b4Smrg       *  cos ( phi ) = sqrt ( 1 - z^2 )
7937117f1b4Smrg       *  sin ( phi ) = z
7947117f1b4Smrg       *
7957117f1b4Smrg       *  Note that cos ( phi ) can further be inserted to the above
7967117f1b4Smrg       *  formulas:
7977117f1b4Smrg       *
7987117f1b4Smrg       *  cos ( theta ) = x / cos ( phi )
7997117f1b4Smrg       *  sin ( theta ) = y / sin ( phi )
8007117f1b4Smrg       *
8017117f1b4Smrg       *  ...etc.  Because of those relations and the standard trigonometric
8027117f1b4Smrg       *  relations, it is pssible to reduce the transforms down to what
8037117f1b4Smrg       *  is used below.  It may be that any primary axis chosen will give the
8047117f1b4Smrg       *  same results (modulo a sign convention) using thie method.
8057117f1b4Smrg       *
8067117f1b4Smrg       *  Particularly nice is to notice that all divisions that might
8077117f1b4Smrg       *  have caused trouble when parallel to certain planes or
8087117f1b4Smrg       *  axis go away with care paid to reducing the expressions.
8097117f1b4Smrg       *  After checking, it does perform correctly under all cases, since
8107117f1b4Smrg       *  in all the cases of division where the denominator would have
8117117f1b4Smrg       *  been zero, the numerator would have been zero as well, giving
8127117f1b4Smrg       *  the expected result.
8137117f1b4Smrg       */
8147117f1b4Smrg
8157117f1b4Smrg      xx = x * x;
8167117f1b4Smrg      yy = y * y;
8177117f1b4Smrg      zz = z * z;
8187117f1b4Smrg      xy = x * y;
8197117f1b4Smrg      yz = y * z;
8207117f1b4Smrg      zx = z * x;
8217117f1b4Smrg      xs = x * s;
8227117f1b4Smrg      ys = y * s;
8237117f1b4Smrg      zs = z * s;
8247117f1b4Smrg      one_c = 1.0F - c;
8257117f1b4Smrg
8267117f1b4Smrg      /* We already hold the identity-matrix so we can skip some statements */
8277117f1b4Smrg      M(0,0) = (one_c * xx) + c;
8287117f1b4Smrg      M(0,1) = (one_c * xy) - zs;
8297117f1b4Smrg      M(0,2) = (one_c * zx) + ys;
8307117f1b4Smrg/*    M(0,3) = 0.0F; */
8317117f1b4Smrg
8327117f1b4Smrg      M(1,0) = (one_c * xy) + zs;
8337117f1b4Smrg      M(1,1) = (one_c * yy) + c;
8347117f1b4Smrg      M(1,2) = (one_c * yz) - xs;
8357117f1b4Smrg/*    M(1,3) = 0.0F; */
8367117f1b4Smrg
8377117f1b4Smrg      M(2,0) = (one_c * zx) - ys;
8387117f1b4Smrg      M(2,1) = (one_c * yz) + xs;
8397117f1b4Smrg      M(2,2) = (one_c * zz) + c;
8407117f1b4Smrg/*    M(2,3) = 0.0F; */
8417117f1b4Smrg
8427117f1b4Smrg/*
8437117f1b4Smrg      M(3,0) = 0.0F;
8447117f1b4Smrg      M(3,1) = 0.0F;
8457117f1b4Smrg      M(3,2) = 0.0F;
8467117f1b4Smrg      M(3,3) = 1.0F;
8477117f1b4Smrg*/
8487117f1b4Smrg   }
8497117f1b4Smrg#undef M
8507117f1b4Smrg
8517117f1b4Smrg   matrix_multf( mat, m, MAT_FLAG_ROTATION );
8527117f1b4Smrg}
8537117f1b4Smrg
8547117f1b4Smrg/**
8557117f1b4Smrg * Apply a perspective projection matrix.
8567117f1b4Smrg *
8577117f1b4Smrg * \param mat matrix to apply the projection.
8587117f1b4Smrg * \param left left clipping plane coordinate.
8597117f1b4Smrg * \param right right clipping plane coordinate.
8607117f1b4Smrg * \param bottom bottom clipping plane coordinate.
8617117f1b4Smrg * \param top top clipping plane coordinate.
8627117f1b4Smrg * \param nearval distance to the near clipping plane.
8637117f1b4Smrg * \param farval distance to the far clipping plane.
8647117f1b4Smrg *
8657117f1b4Smrg * Creates the projection matrix and multiplies it with \p mat, marking the
8667117f1b4Smrg * MAT_FLAG_PERSPECTIVE flag.
8677117f1b4Smrg */
8687117f1b4Smrgvoid
8697117f1b4Smrg_math_matrix_frustum( GLmatrix *mat,
8707117f1b4Smrg		      GLfloat left, GLfloat right,
8717117f1b4Smrg		      GLfloat bottom, GLfloat top,
8727117f1b4Smrg		      GLfloat nearval, GLfloat farval )
8737117f1b4Smrg{
8747117f1b4Smrg   GLfloat x, y, a, b, c, d;
8757117f1b4Smrg   GLfloat m[16];
8767117f1b4Smrg
8777117f1b4Smrg   x = (2.0F*nearval) / (right-left);
8787117f1b4Smrg   y = (2.0F*nearval) / (top-bottom);
8797117f1b4Smrg   a = (right+left) / (right-left);
8807117f1b4Smrg   b = (top+bottom) / (top-bottom);
8817117f1b4Smrg   c = -(farval+nearval) / ( farval-nearval);
8827117f1b4Smrg   d = -(2.0F*farval*nearval) / (farval-nearval);  /* error? */
8837117f1b4Smrg
8847117f1b4Smrg#define M(row,col)  m[col*4+row]
8857117f1b4Smrg   M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = a;      M(0,3) = 0.0F;
8867117f1b4Smrg   M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = b;      M(1,3) = 0.0F;
8877117f1b4Smrg   M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = c;      M(2,3) = d;
8887117f1b4Smrg   M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = -1.0F;  M(3,3) = 0.0F;
8897117f1b4Smrg#undef M
8907117f1b4Smrg
8917117f1b4Smrg   matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
8927117f1b4Smrg}
8937117f1b4Smrg
8947117f1b4Smrg/**
8957ec681f3Smrg * Create an orthographic projection matrix.
8967117f1b4Smrg *
8977ec681f3Smrg * \param m float array in which to store the project matrix
8987117f1b4Smrg * \param left left clipping plane coordinate.
8997117f1b4Smrg * \param right right clipping plane coordinate.
9007117f1b4Smrg * \param bottom bottom clipping plane coordinate.
9017117f1b4Smrg * \param top top clipping plane coordinate.
9027117f1b4Smrg * \param nearval distance to the near clipping plane.
9037117f1b4Smrg * \param farval distance to the far clipping plane.
9047117f1b4Smrg *
9057ec681f3Smrg * Creates the projection matrix and stored the values in \p m.  As with other
9067ec681f3Smrg * OpenGL matrices, the data is stored in column-major ordering.
9077117f1b4Smrg */
9087117f1b4Smrgvoid
9097ec681f3Smrg_math_float_ortho(float *m,
9107ec681f3Smrg                  float left, float right,
9117ec681f3Smrg                  float bottom, float top,
9127ec681f3Smrg                  float nearval, float farval)
9137117f1b4Smrg{
9147117f1b4Smrg#define M(row,col)  m[col*4+row]
9157117f1b4Smrg   M(0,0) = 2.0F / (right-left);
9167117f1b4Smrg   M(0,1) = 0.0F;
9177117f1b4Smrg   M(0,2) = 0.0F;
9187117f1b4Smrg   M(0,3) = -(right+left) / (right-left);
9197117f1b4Smrg
9207117f1b4Smrg   M(1,0) = 0.0F;
9217117f1b4Smrg   M(1,1) = 2.0F / (top-bottom);
9227117f1b4Smrg   M(1,2) = 0.0F;
9237117f1b4Smrg   M(1,3) = -(top+bottom) / (top-bottom);
9247117f1b4Smrg
9257117f1b4Smrg   M(2,0) = 0.0F;
9267117f1b4Smrg   M(2,1) = 0.0F;
9277117f1b4Smrg   M(2,2) = -2.0F / (farval-nearval);
9287117f1b4Smrg   M(2,3) = -(farval+nearval) / (farval-nearval);
9297117f1b4Smrg
9307117f1b4Smrg   M(3,0) = 0.0F;
9317117f1b4Smrg   M(3,1) = 0.0F;
9327117f1b4Smrg   M(3,2) = 0.0F;
9337117f1b4Smrg   M(3,3) = 1.0F;
9347117f1b4Smrg#undef M
9357ec681f3Smrg}
9367117f1b4Smrg
9377ec681f3Smrg/**
9387ec681f3Smrg * Apply an orthographic projection matrix.
9397ec681f3Smrg *
9407ec681f3Smrg * \param mat matrix to apply the projection.
9417ec681f3Smrg * \param left left clipping plane coordinate.
9427ec681f3Smrg * \param right right clipping plane coordinate.
9437ec681f3Smrg * \param bottom bottom clipping plane coordinate.
9447ec681f3Smrg * \param top top clipping plane coordinate.
9457ec681f3Smrg * \param nearval distance to the near clipping plane.
9467ec681f3Smrg * \param farval distance to the far clipping plane.
9477ec681f3Smrg *
9487ec681f3Smrg * Creates the projection matrix and multiplies it with \p mat, marking the
9497ec681f3Smrg * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
9507ec681f3Smrg */
9517ec681f3Smrgvoid
9527ec681f3Smrg_math_matrix_ortho( GLmatrix *mat,
9537ec681f3Smrg		    GLfloat left, GLfloat right,
9547ec681f3Smrg		    GLfloat bottom, GLfloat top,
9557ec681f3Smrg		    GLfloat nearval, GLfloat farval )
9567ec681f3Smrg{
9577ec681f3Smrg   GLfloat m[16];
9587ec681f3Smrg
9597ec681f3Smrg   _math_float_ortho(m, left, right, bottom, top, nearval, farval);
9607117f1b4Smrg   matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
9617117f1b4Smrg}
9627117f1b4Smrg
9637117f1b4Smrg/**
9647117f1b4Smrg * Multiply a matrix with a general scaling matrix.
9657117f1b4Smrg *
9667117f1b4Smrg * \param mat matrix.
9677117f1b4Smrg * \param x x axis scale factor.
9687117f1b4Smrg * \param y y axis scale factor.
9697117f1b4Smrg * \param z z axis scale factor.
9707117f1b4Smrg *
9717117f1b4Smrg * Multiplies in-place the elements of \p mat by the scale factors. Checks if
9727117f1b4Smrg * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE
9737117f1b4Smrg * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and
9747117f1b4Smrg * MAT_DIRTY_INVERSE dirty flags.
9757117f1b4Smrg */
9767117f1b4Smrgvoid
9777117f1b4Smrg_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
9787117f1b4Smrg{
9797117f1b4Smrg   GLfloat *m = mat->m;
9807117f1b4Smrg   m[0] *= x;   m[4] *= y;   m[8]  *= z;
9817117f1b4Smrg   m[1] *= x;   m[5] *= y;   m[9]  *= z;
9827117f1b4Smrg   m[2] *= x;   m[6] *= y;   m[10] *= z;
9837117f1b4Smrg   m[3] *= x;   m[7] *= y;   m[11] *= z;
9847117f1b4Smrg
98501e04c3fSmrg   if (fabsf(x - y) < 1e-8F && fabsf(x - z) < 1e-8F)
9867117f1b4Smrg      mat->flags |= MAT_FLAG_UNIFORM_SCALE;
9877117f1b4Smrg   else
9887117f1b4Smrg      mat->flags |= MAT_FLAG_GENERAL_SCALE;
9897117f1b4Smrg
9907117f1b4Smrg   mat->flags |= (MAT_DIRTY_TYPE |
9917117f1b4Smrg		  MAT_DIRTY_INVERSE);
9927117f1b4Smrg}
9937117f1b4Smrg
9947117f1b4Smrg/**
9957117f1b4Smrg * Multiply a matrix with a translation matrix.
9967117f1b4Smrg *
9977117f1b4Smrg * \param mat matrix.
9987117f1b4Smrg * \param x translation vector x coordinate.
9997117f1b4Smrg * \param y translation vector y coordinate.
10007117f1b4Smrg * \param z translation vector z coordinate.
10017117f1b4Smrg *
10027117f1b4Smrg * Adds the translation coordinates to the elements of \p mat in-place.  Marks
10037117f1b4Smrg * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE
10047117f1b4Smrg * dirty flags.
10057117f1b4Smrg */
10067117f1b4Smrgvoid
10077117f1b4Smrg_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
10087117f1b4Smrg{
10097117f1b4Smrg   GLfloat *m = mat->m;
10107117f1b4Smrg   m[12] = m[0] * x + m[4] * y + m[8]  * z + m[12];
10117117f1b4Smrg   m[13] = m[1] * x + m[5] * y + m[9]  * z + m[13];
10127117f1b4Smrg   m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
10137117f1b4Smrg   m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
10147117f1b4Smrg
10157117f1b4Smrg   mat->flags |= (MAT_FLAG_TRANSLATION |
10167117f1b4Smrg		  MAT_DIRTY_TYPE |
10177117f1b4Smrg		  MAT_DIRTY_INVERSE);
10187117f1b4Smrg}
10197117f1b4Smrg
10207117f1b4Smrg
10217117f1b4Smrg/**
10227117f1b4Smrg * Set matrix to do viewport and depthrange mapping.
10237117f1b4Smrg * Transforms Normalized Device Coords to window/Z values.
10247117f1b4Smrg */
10257117f1b4Smrgvoid
102601e04c3fSmrg_math_matrix_viewport(GLmatrix *m, const float scale[3],
102701e04c3fSmrg                      const float translate[3], double depthMax)
10287117f1b4Smrg{
102901e04c3fSmrg   m->m[MAT_SX] = scale[0];
103001e04c3fSmrg   m->m[MAT_TX] = translate[0];
103101e04c3fSmrg   m->m[MAT_SY] = scale[1];
103201e04c3fSmrg   m->m[MAT_TY] = translate[1];
103301e04c3fSmrg   m->m[MAT_SZ] = depthMax*scale[2];
103401e04c3fSmrg   m->m[MAT_TZ] = depthMax*translate[2];
10357117f1b4Smrg   m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION;
10367117f1b4Smrg   m->type = MATRIX_3D_NO_ROT;
10377117f1b4Smrg}
10387117f1b4Smrg
10397117f1b4Smrg
10407117f1b4Smrg/**
10417117f1b4Smrg * Set a matrix to the identity matrix.
10427117f1b4Smrg *
10437117f1b4Smrg * \param mat matrix.
10447117f1b4Smrg *
10457117f1b4Smrg * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL.
10467117f1b4Smrg * Sets the matrix type to identity, and clear the dirty flags.
10477117f1b4Smrg */
10487117f1b4Smrgvoid
10497117f1b4Smrg_math_matrix_set_identity( GLmatrix *mat )
10507117f1b4Smrg{
10517ec681f3Smrg   STATIC_ASSERT(MATRIX_M == offsetof(GLmatrix, m));
10527ec681f3Smrg   STATIC_ASSERT(MATRIX_INV == offsetof(GLmatrix, inv));
10537ec681f3Smrg
105401e04c3fSmrg   memcpy( mat->m, Identity, sizeof(Identity) );
105501e04c3fSmrg   memcpy( mat->inv, Identity, sizeof(Identity) );
10567117f1b4Smrg
10577117f1b4Smrg   mat->type = MATRIX_IDENTITY;
10587117f1b4Smrg   mat->flags &= ~(MAT_DIRTY_FLAGS|
10597117f1b4Smrg		   MAT_DIRTY_TYPE|
10607117f1b4Smrg		   MAT_DIRTY_INVERSE);
10617117f1b4Smrg}
10627117f1b4Smrg
10637117f1b4Smrg/*@}*/
10647117f1b4Smrg
10657117f1b4Smrg
10667117f1b4Smrg/**********************************************************************/
10677117f1b4Smrg/** \name Matrix analysis */
10687117f1b4Smrg/*@{*/
10697117f1b4Smrg
10707117f1b4Smrg#define ZERO(x) (1<<x)
10717117f1b4Smrg#define ONE(x)  (1<<(x+16))
10727117f1b4Smrg
10737117f1b4Smrg#define MASK_NO_TRX      (ZERO(12) | ZERO(13) | ZERO(14))
10747117f1b4Smrg#define MASK_NO_2D_SCALE ( ONE(0)  | ONE(5))
10757117f1b4Smrg
10767117f1b4Smrg#define MASK_IDENTITY    ( ONE(0)  | ZERO(4)  | ZERO(8)  | ZERO(12) |\
10777117f1b4Smrg			  ZERO(1)  |  ONE(5)  | ZERO(9)  | ZERO(13) |\
10787117f1b4Smrg			  ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\
10797117f1b4Smrg			  ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
10807117f1b4Smrg
10817117f1b4Smrg#define MASK_2D_NO_ROT   (           ZERO(4)  | ZERO(8)  |           \
10827117f1b4Smrg			  ZERO(1)  |            ZERO(9)  |           \
10837117f1b4Smrg			  ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\
10847117f1b4Smrg			  ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
10857117f1b4Smrg
10867117f1b4Smrg#define MASK_2D          (                      ZERO(8)  |           \
10877117f1b4Smrg			                        ZERO(9)  |           \
10887117f1b4Smrg			  ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\
10897117f1b4Smrg			  ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
10907117f1b4Smrg
10917117f1b4Smrg
10927117f1b4Smrg#define MASK_3D_NO_ROT   (           ZERO(4)  | ZERO(8)  |           \
10937117f1b4Smrg			  ZERO(1)  |            ZERO(9)  |           \
10947117f1b4Smrg			  ZERO(2)  | ZERO(6)  |                      \
10957117f1b4Smrg			  ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
10967117f1b4Smrg
10977117f1b4Smrg#define MASK_3D          (                                           \
10987117f1b4Smrg			                                             \
10997117f1b4Smrg			                                             \
11007117f1b4Smrg			  ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) )
11017117f1b4Smrg
11027117f1b4Smrg
11037117f1b4Smrg#define MASK_PERSPECTIVE (           ZERO(4)  |            ZERO(12) |\
11047117f1b4Smrg			  ZERO(1)  |                       ZERO(13) |\
11057117f1b4Smrg			  ZERO(2)  | ZERO(6)  |                      \
11067117f1b4Smrg			  ZERO(3)  | ZERO(7)  |            ZERO(15) )
11077117f1b4Smrg
11087117f1b4Smrg#define SQ(x) ((x)*(x))
11097117f1b4Smrg
11107117f1b4Smrg/**
11117ec681f3Smrg * Determine type and flags from scratch.
11127117f1b4Smrg *
11137117f1b4Smrg * \param mat matrix.
11147ec681f3Smrg *
11157117f1b4Smrg * This is expensive enough to only want to do it once.
11167117f1b4Smrg */
11177117f1b4Smrgstatic void analyse_from_scratch( GLmatrix *mat )
11187117f1b4Smrg{
11197117f1b4Smrg   const GLfloat *m = mat->m;
11207117f1b4Smrg   GLuint mask = 0;
11217117f1b4Smrg   GLuint i;
11227117f1b4Smrg
11237117f1b4Smrg   for (i = 0 ; i < 16 ; i++) {
112401e04c3fSmrg      if (m[i] == 0.0F) mask |= (1<<i);
11257117f1b4Smrg   }
11267117f1b4Smrg
11277117f1b4Smrg   if (m[0] == 1.0F) mask |= (1<<16);
11287117f1b4Smrg   if (m[5] == 1.0F) mask |= (1<<21);
11297117f1b4Smrg   if (m[10] == 1.0F) mask |= (1<<26);
11307117f1b4Smrg   if (m[15] == 1.0F) mask |= (1<<31);
11317117f1b4Smrg
11327117f1b4Smrg   mat->flags &= ~MAT_FLAGS_GEOMETRY;
11337117f1b4Smrg
11347117f1b4Smrg   /* Check for translation - no-one really cares
11357117f1b4Smrg    */
11367117f1b4Smrg   if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
11377117f1b4Smrg      mat->flags |= MAT_FLAG_TRANSLATION;
11387117f1b4Smrg
11397117f1b4Smrg   /* Do the real work
11407117f1b4Smrg    */
11417117f1b4Smrg   if (mask == (GLuint) MASK_IDENTITY) {
11427117f1b4Smrg      mat->type = MATRIX_IDENTITY;
11437117f1b4Smrg   }
11447117f1b4Smrg   else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
11457117f1b4Smrg      mat->type = MATRIX_2D_NO_ROT;
11467117f1b4Smrg
11477117f1b4Smrg      if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
11487117f1b4Smrg	 mat->flags |= MAT_FLAG_GENERAL_SCALE;
11497117f1b4Smrg   }
11507117f1b4Smrg   else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
11517117f1b4Smrg      GLfloat mm = DOT2(m, m);
11527117f1b4Smrg      GLfloat m4m4 = DOT2(m+4,m+4);
11537117f1b4Smrg      GLfloat mm4 = DOT2(m,m+4);
11547117f1b4Smrg
11557117f1b4Smrg      mat->type = MATRIX_2D;
11567117f1b4Smrg
11577117f1b4Smrg      /* Check for scale */
115801e04c3fSmrg      if (SQ(mm-1) > SQ(1e-6F) ||
115901e04c3fSmrg	  SQ(m4m4-1) > SQ(1e-6F))
11607117f1b4Smrg	 mat->flags |= MAT_FLAG_GENERAL_SCALE;
11617117f1b4Smrg
11627117f1b4Smrg      /* Check for rotation */
116301e04c3fSmrg      if (SQ(mm4) > SQ(1e-6F))
11647117f1b4Smrg	 mat->flags |= MAT_FLAG_GENERAL_3D;
11657117f1b4Smrg      else
11667117f1b4Smrg	 mat->flags |= MAT_FLAG_ROTATION;
11677117f1b4Smrg
11687117f1b4Smrg   }
11697117f1b4Smrg   else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
11707117f1b4Smrg      mat->type = MATRIX_3D_NO_ROT;
11717117f1b4Smrg
11727117f1b4Smrg      /* Check for scale */
117301e04c3fSmrg      if (SQ(m[0]-m[5]) < SQ(1e-6F) &&
117401e04c3fSmrg	  SQ(m[0]-m[10]) < SQ(1e-6F)) {
117501e04c3fSmrg	 if (SQ(m[0]-1.0F) > SQ(1e-6F)) {
11767117f1b4Smrg	    mat->flags |= MAT_FLAG_UNIFORM_SCALE;
11777117f1b4Smrg         }
11787117f1b4Smrg      }
11797117f1b4Smrg      else {
11807117f1b4Smrg	 mat->flags |= MAT_FLAG_GENERAL_SCALE;
11817117f1b4Smrg      }
11827117f1b4Smrg   }
11837117f1b4Smrg   else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
11847117f1b4Smrg      GLfloat c1 = DOT3(m,m);
11857117f1b4Smrg      GLfloat c2 = DOT3(m+4,m+4);
11867117f1b4Smrg      GLfloat c3 = DOT3(m+8,m+8);
11877117f1b4Smrg      GLfloat d1 = DOT3(m, m+4);
11887117f1b4Smrg      GLfloat cp[3];
11897117f1b4Smrg
11907117f1b4Smrg      mat->type = MATRIX_3D;
11917117f1b4Smrg
11927117f1b4Smrg      /* Check for scale */
119301e04c3fSmrg      if (SQ(c1-c2) < SQ(1e-6F) && SQ(c1-c3) < SQ(1e-6F)) {
119401e04c3fSmrg	 if (SQ(c1-1.0F) > SQ(1e-6F))
11957117f1b4Smrg	    mat->flags |= MAT_FLAG_UNIFORM_SCALE;
11967117f1b4Smrg	 /* else no scale at all */
11977117f1b4Smrg      }
11987117f1b4Smrg      else {
11997117f1b4Smrg	 mat->flags |= MAT_FLAG_GENERAL_SCALE;
12007117f1b4Smrg      }
12017117f1b4Smrg
12027117f1b4Smrg      /* Check for rotation */
120301e04c3fSmrg      if (SQ(d1) < SQ(1e-6F)) {
12047117f1b4Smrg	 CROSS3( cp, m, m+4 );
12057117f1b4Smrg	 SUB_3V( cp, cp, (m+8) );
120601e04c3fSmrg	 if (LEN_SQUARED_3FV(cp) < SQ(1e-6F))
12077117f1b4Smrg	    mat->flags |= MAT_FLAG_ROTATION;
12087117f1b4Smrg	 else
12097117f1b4Smrg	    mat->flags |= MAT_FLAG_GENERAL_3D;
12107117f1b4Smrg      }
12117117f1b4Smrg      else {
12127117f1b4Smrg	 mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
12137117f1b4Smrg      }
12147117f1b4Smrg   }
12157117f1b4Smrg   else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
12167117f1b4Smrg      mat->type = MATRIX_PERSPECTIVE;
12177117f1b4Smrg      mat->flags |= MAT_FLAG_GENERAL;
12187117f1b4Smrg   }
12197117f1b4Smrg   else {
12207117f1b4Smrg      mat->type = MATRIX_GENERAL;
12217117f1b4Smrg      mat->flags |= MAT_FLAG_GENERAL;
12227117f1b4Smrg   }
12237117f1b4Smrg}
12247117f1b4Smrg
12257117f1b4Smrg/**
12267117f1b4Smrg * Analyze a matrix given that its flags are accurate.
12277ec681f3Smrg *
12287117f1b4Smrg * This is the more common operation, hopefully.
12297117f1b4Smrg */
12307117f1b4Smrgstatic void analyse_from_flags( GLmatrix *mat )
12317117f1b4Smrg{
12327117f1b4Smrg   const GLfloat *m = mat->m;
12337117f1b4Smrg
12347117f1b4Smrg   if (TEST_MAT_FLAGS(mat, 0)) {
12357117f1b4Smrg      mat->type = MATRIX_IDENTITY;
12367117f1b4Smrg   }
12377117f1b4Smrg   else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
12387117f1b4Smrg				 MAT_FLAG_UNIFORM_SCALE |
12397117f1b4Smrg				 MAT_FLAG_GENERAL_SCALE))) {
12407117f1b4Smrg      if ( m[10]==1.0F && m[14]==0.0F ) {
12417117f1b4Smrg	 mat->type = MATRIX_2D_NO_ROT;
12427117f1b4Smrg      }
12437117f1b4Smrg      else {
12447117f1b4Smrg	 mat->type = MATRIX_3D_NO_ROT;
12457117f1b4Smrg      }
12467117f1b4Smrg   }
12477117f1b4Smrg   else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
12487117f1b4Smrg      if (                                 m[ 8]==0.0F
12497117f1b4Smrg            &&                             m[ 9]==0.0F
12507117f1b4Smrg            && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
12517117f1b4Smrg	 mat->type = MATRIX_2D;
12527117f1b4Smrg      }
12537117f1b4Smrg      else {
12547117f1b4Smrg	 mat->type = MATRIX_3D;
12557117f1b4Smrg      }
12567117f1b4Smrg   }
12577117f1b4Smrg   else if (                 m[4]==0.0F                 && m[12]==0.0F
12587117f1b4Smrg            && m[1]==0.0F                               && m[13]==0.0F
12597117f1b4Smrg            && m[2]==0.0F && m[6]==0.0F
12607117f1b4Smrg            && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
12617117f1b4Smrg      mat->type = MATRIX_PERSPECTIVE;
12627117f1b4Smrg   }
12637117f1b4Smrg   else {
12647117f1b4Smrg      mat->type = MATRIX_GENERAL;
12657117f1b4Smrg   }
12667117f1b4Smrg}
12677117f1b4Smrg
12687117f1b4Smrg/**
12697117f1b4Smrg * Analyze and update a matrix.
12707117f1b4Smrg *
12717117f1b4Smrg * \param mat matrix.
12727117f1b4Smrg *
12737117f1b4Smrg * If the matrix type is dirty then calls either analyse_from_scratch() or
12747117f1b4Smrg * analyse_from_flags() to determine its type, according to whether the flags
12757117f1b4Smrg * are dirty or not, respectively. If the matrix has an inverse and it's dirty
12767117f1b4Smrg * then calls matrix_invert(). Finally clears the dirty flags.
12777117f1b4Smrg */
12787117f1b4Smrgvoid
12797117f1b4Smrg_math_matrix_analyse( GLmatrix *mat )
12807117f1b4Smrg{
12817117f1b4Smrg   if (mat->flags & MAT_DIRTY_TYPE) {
12827117f1b4Smrg      if (mat->flags & MAT_DIRTY_FLAGS)
12837117f1b4Smrg	 analyse_from_scratch( mat );
12847117f1b4Smrg      else
12857117f1b4Smrg	 analyse_from_flags( mat );
12867117f1b4Smrg   }
12877117f1b4Smrg
12887ec681f3Smrg   if (mat->flags & MAT_DIRTY_INVERSE) {
12897117f1b4Smrg      matrix_invert( mat );
1290c1f859d4Smrg      mat->flags &= ~MAT_DIRTY_INVERSE;
12917117f1b4Smrg   }
12927117f1b4Smrg
1293c1f859d4Smrg   mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
12947117f1b4Smrg}
12957117f1b4Smrg
12967117f1b4Smrg/*@}*/
12977117f1b4Smrg
12987117f1b4Smrg
12997117f1b4Smrg/**
13007117f1b4Smrg * Test if the given matrix preserves vector lengths.
13017117f1b4Smrg */
13027117f1b4SmrgGLboolean
13037117f1b4Smrg_math_matrix_is_length_preserving( const GLmatrix *m )
13047117f1b4Smrg{
13057117f1b4Smrg   return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING);
13067117f1b4Smrg}
13077117f1b4Smrg
13087117f1b4Smrg
13097117f1b4Smrg/**
13107117f1b4Smrg * Test if the given matrix does any rotation.
13117117f1b4Smrg * (or perhaps if the upper-left 3x3 is non-identity)
13127117f1b4Smrg */
13137117f1b4SmrgGLboolean
13147117f1b4Smrg_math_matrix_has_rotation( const GLmatrix *m )
13157117f1b4Smrg{
13167117f1b4Smrg   if (m->flags & (MAT_FLAG_GENERAL |
13177117f1b4Smrg                   MAT_FLAG_ROTATION |
13187117f1b4Smrg                   MAT_FLAG_GENERAL_3D |
13197117f1b4Smrg                   MAT_FLAG_PERSPECTIVE))
13207117f1b4Smrg      return GL_TRUE;
13217117f1b4Smrg   else
13227117f1b4Smrg      return GL_FALSE;
13237117f1b4Smrg}
13247117f1b4Smrg
13257117f1b4Smrg
13267117f1b4SmrgGLboolean
13277117f1b4Smrg_math_matrix_is_general_scale( const GLmatrix *m )
13287117f1b4Smrg{
13297117f1b4Smrg   return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE;
13307117f1b4Smrg}
13317117f1b4Smrg
13327117f1b4Smrg
13337117f1b4SmrgGLboolean
13347117f1b4Smrg_math_matrix_is_dirty( const GLmatrix *m )
13357117f1b4Smrg{
13367117f1b4Smrg   return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE;
13377117f1b4Smrg}
13387117f1b4Smrg
13397117f1b4Smrg
13407117f1b4Smrg/**********************************************************************/
13417117f1b4Smrg/** \name Matrix setup */
13427117f1b4Smrg/*@{*/
13437117f1b4Smrg
13447117f1b4Smrg/**
13457117f1b4Smrg * Copy a matrix.
13467117f1b4Smrg *
13477117f1b4Smrg * \param to destination matrix.
13487117f1b4Smrg * \param from source matrix.
13497117f1b4Smrg *
13507117f1b4Smrg * Copies all fields in GLmatrix, creating an inverse array if necessary.
13517117f1b4Smrg */
13527117f1b4Smrgvoid
13537117f1b4Smrg_math_matrix_copy( GLmatrix *to, const GLmatrix *from )
13547117f1b4Smrg{
135501e04c3fSmrg   memcpy(to->m, from->m, 16 * sizeof(GLfloat));
1356af69d88dSmrg   memcpy(to->inv, from->inv, 16 * sizeof(GLfloat));
13577117f1b4Smrg   to->flags = from->flags;
13587117f1b4Smrg   to->type = from->type;
13597117f1b4Smrg}
13607117f1b4Smrg
13617ec681f3Smrg/**
13627ec681f3Smrg * Copy a matrix as part of glPushMatrix.
13637ec681f3Smrg *
13647ec681f3Smrg * The makes the source matrix canonical (inverse and flags are up-to-date),
13657ec681f3Smrg * so that later glPopMatrix is evaluated as a no-op if there is no state
13667ec681f3Smrg * change.
13677ec681f3Smrg *
13687ec681f3Smrg * It this wasn't done, a draw call would canonicalize the matrix, which
13697ec681f3Smrg * would make it different from the pushed one and so glPopMatrix wouldn't be
13707ec681f3Smrg * recognized as a no-op.
13717ec681f3Smrg */
13727ec681f3Smrgvoid
13737ec681f3Smrg_math_matrix_push_copy(GLmatrix *to, GLmatrix *from)
13747ec681f3Smrg{
13757ec681f3Smrg   if (from->flags & MAT_DIRTY)
13767ec681f3Smrg      _math_matrix_analyse(from);
13777ec681f3Smrg
13787ec681f3Smrg   _math_matrix_copy(to, from);
13797ec681f3Smrg}
13807ec681f3Smrg
13817117f1b4Smrg/**
13827117f1b4Smrg * Loads a matrix array into GLmatrix.
13837ec681f3Smrg *
13847117f1b4Smrg * \param m matrix array.
13857117f1b4Smrg * \param mat matrix.
13867117f1b4Smrg *
13877117f1b4Smrg * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
13887117f1b4Smrg * flags.
13897117f1b4Smrg */
13907117f1b4Smrgvoid
13917117f1b4Smrg_math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
13927117f1b4Smrg{
1393cdc920a0Smrg   memcpy( mat->m, m, 16*sizeof(GLfloat) );
13947117f1b4Smrg   mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
13957117f1b4Smrg}
13967117f1b4Smrg
13977117f1b4Smrg/**
13987117f1b4Smrg * Matrix constructor.
13997117f1b4Smrg *
14007117f1b4Smrg * \param m matrix.
14017117f1b4Smrg *
14027117f1b4Smrg * Initialize the GLmatrix fields.
14037117f1b4Smrg */
14047117f1b4Smrgvoid
14057117f1b4Smrg_math_matrix_ctr( GLmatrix *m )
14067117f1b4Smrg{
14077ec681f3Smrg   memset(m, 0, sizeof(*m));
14087ec681f3Smrg   memcpy( m->m, Identity, sizeof(Identity) );
14097ec681f3Smrg   memcpy( m->inv, Identity, sizeof(Identity) );
14107117f1b4Smrg   m->type = MATRIX_IDENTITY;
14117117f1b4Smrg   m->flags = 0;
14127117f1b4Smrg}
14137117f1b4Smrg
14147117f1b4Smrg/*@}*/
14157117f1b4Smrg
14167117f1b4Smrg
14177117f1b4Smrg/**********************************************************************/
14187117f1b4Smrg/** \name Matrix transpose */
14197117f1b4Smrg/*@{*/
14207117f1b4Smrg
14217117f1b4Smrg/**
14227117f1b4Smrg * Transpose a GLfloat matrix.
14237117f1b4Smrg *
14247117f1b4Smrg * \param to destination array.
14257117f1b4Smrg * \param from source array.
14267117f1b4Smrg */
14277117f1b4Smrgvoid
14287117f1b4Smrg_math_transposef( GLfloat to[16], const GLfloat from[16] )
14297117f1b4Smrg{
14307117f1b4Smrg   to[0] = from[0];
14317117f1b4Smrg   to[1] = from[4];
14327117f1b4Smrg   to[2] = from[8];
14337117f1b4Smrg   to[3] = from[12];
14347117f1b4Smrg   to[4] = from[1];
14357117f1b4Smrg   to[5] = from[5];
14367117f1b4Smrg   to[6] = from[9];
14377117f1b4Smrg   to[7] = from[13];
14387117f1b4Smrg   to[8] = from[2];
14397117f1b4Smrg   to[9] = from[6];
14407117f1b4Smrg   to[10] = from[10];
14417117f1b4Smrg   to[11] = from[14];
14427117f1b4Smrg   to[12] = from[3];
14437117f1b4Smrg   to[13] = from[7];
14447117f1b4Smrg   to[14] = from[11];
14457117f1b4Smrg   to[15] = from[15];
14467117f1b4Smrg}
14477117f1b4Smrg
14487117f1b4Smrg/**
14497117f1b4Smrg * Transpose a GLdouble matrix.
14507117f1b4Smrg *
14517117f1b4Smrg * \param to destination array.
14527117f1b4Smrg * \param from source array.
14537117f1b4Smrg */
14547117f1b4Smrgvoid
14557117f1b4Smrg_math_transposed( GLdouble to[16], const GLdouble from[16] )
14567117f1b4Smrg{
14577117f1b4Smrg   to[0] = from[0];
14587117f1b4Smrg   to[1] = from[4];
14597117f1b4Smrg   to[2] = from[8];
14607117f1b4Smrg   to[3] = from[12];
14617117f1b4Smrg   to[4] = from[1];
14627117f1b4Smrg   to[5] = from[5];
14637117f1b4Smrg   to[6] = from[9];
14647117f1b4Smrg   to[7] = from[13];
14657117f1b4Smrg   to[8] = from[2];
14667117f1b4Smrg   to[9] = from[6];
14677117f1b4Smrg   to[10] = from[10];
14687117f1b4Smrg   to[11] = from[14];
14697117f1b4Smrg   to[12] = from[3];
14707117f1b4Smrg   to[13] = from[7];
14717117f1b4Smrg   to[14] = from[11];
14727117f1b4Smrg   to[15] = from[15];
14737117f1b4Smrg}
14747117f1b4Smrg
14757117f1b4Smrg/**
14767117f1b4Smrg * Transpose a GLdouble matrix and convert to GLfloat.
14777117f1b4Smrg *
14787117f1b4Smrg * \param to destination array.
14797117f1b4Smrg * \param from source array.
14807117f1b4Smrg */
14817117f1b4Smrgvoid
14827117f1b4Smrg_math_transposefd( GLfloat to[16], const GLdouble from[16] )
14837117f1b4Smrg{
14847117f1b4Smrg   to[0] = (GLfloat) from[0];
14857117f1b4Smrg   to[1] = (GLfloat) from[4];
14867117f1b4Smrg   to[2] = (GLfloat) from[8];
14877117f1b4Smrg   to[3] = (GLfloat) from[12];
14887117f1b4Smrg   to[4] = (GLfloat) from[1];
14897117f1b4Smrg   to[5] = (GLfloat) from[5];
14907117f1b4Smrg   to[6] = (GLfloat) from[9];
14917117f1b4Smrg   to[7] = (GLfloat) from[13];
14927117f1b4Smrg   to[8] = (GLfloat) from[2];
14937117f1b4Smrg   to[9] = (GLfloat) from[6];
14947117f1b4Smrg   to[10] = (GLfloat) from[10];
14957117f1b4Smrg   to[11] = (GLfloat) from[14];
14967117f1b4Smrg   to[12] = (GLfloat) from[3];
14977117f1b4Smrg   to[13] = (GLfloat) from[7];
14987117f1b4Smrg   to[14] = (GLfloat) from[11];
14997117f1b4Smrg   to[15] = (GLfloat) from[15];
15007117f1b4Smrg}
15017117f1b4Smrg
15027117f1b4Smrg/*@}*/
15037117f1b4Smrg
15044a49301eSmrg
15054a49301eSmrg/**
15064a49301eSmrg * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix.  This
15074a49301eSmrg * function is used for transforming clipping plane equations and spotlight
15084a49301eSmrg * directions.
15094a49301eSmrg * Mathematically,  u = v * m.
15104a49301eSmrg * Input:  v - input vector
15114a49301eSmrg *         m - transformation matrix
15124a49301eSmrg * Output:  u - transformed vector
15134a49301eSmrg */
15144a49301eSmrgvoid
15154a49301eSmrg_mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
15164a49301eSmrg{
15174a49301eSmrg   const GLfloat v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
15184a49301eSmrg#define M(row,col)  m[row + col*4]
15194a49301eSmrg   u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
15204a49301eSmrg   u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
15214a49301eSmrg   u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
15224a49301eSmrg   u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);
15234a49301eSmrg#undef M
15244a49301eSmrg}
1525