m_matrix.c revision 50db8129152f3d5ea8db13d55f82673d53bf1b8f
123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell/* 223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Mesa 3-D graphics library 3522ea4271804b75d90f9bc72b81bfd025bb137d0Brian Paul * Version: 6.3 422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * 5522ea4271804b75d90f9bc72b81bfd025bb137d0Brian Paul * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. 622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * 723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a 823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * copy of this software and associated documentation files (the "Software"), 923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * to deal in the Software without restriction, including without limitation 1023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense, 1123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * and/or sell copies of the Software, and to permit persons to whom the 1223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Software is furnished to do so, subject to the following conditions: 1322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * 1423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * The above copyright notice and this permission notice shall be included 1523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * in all copies or substantial portions of the Software. 1622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * 1723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 1823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 2423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 262dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul/** 272dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul * \file m_matrix.c 282dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul * Matrix operations. 292dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul * 302dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul * \note 312dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul * -# 4x4 transformation matrices are stored in memory in column major order. 322dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul * -# Points/vertices are to be thought of as column vectors. 332dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul * -# Transformation of a point p by a matrix M is: p' = M * p 342dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul */ 352dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul 362dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul 37bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/glheader.h" 38bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/imports.h" 39bbd287103dad776d8a45c87c4e51fbc26d9b80d5Brian Paul#include "main/macros.h" 4023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 4123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#include "m_matrix.h" 4223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 43f4b02d1a2675d4a0699b8995a422fbd413c32301Keith Whitwell 446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 45049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * \defgroup MatFlags MAT_FLAG_XXX-flags 46049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * 47049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Bitmasks to indicate different kinds of 4x4 matrices in GLmatrix::flags 48049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * It would be nice to make all these flags private to m_matrix.c 49049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 50049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/*@{*/ 51049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_IDENTITY 0 /**< is an identity matrix flag. 52049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * (Not actually used - the identity 53049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * matrix is identified by the absense 54049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * of all other flags.) 55049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 56049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_GENERAL 0x1 /**< is a general matrix flag */ 57049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_ROTATION 0x2 /**< is a rotation matrix flag */ 58049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_TRANSLATION 0x4 /**< is a translation matrix flag */ 59049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_UNIFORM_SCALE 0x8 /**< is an uniform scaling matrix flag */ 60049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_GENERAL_SCALE 0x10 /**< is a general scaling matrix flag */ 61049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_GENERAL_3D 0x20 /**< general 3D matrix flag */ 62049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_PERSPECTIVE 0x40 /**< is a perspective proj matrix flag */ 63049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_SINGULAR 0x80 /**< is a singular matrix flag */ 64049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_DIRTY_TYPE 0x100 /**< matrix type is dirty */ 65049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_DIRTY_FLAGS 0x200 /**< matrix flags are dirty */ 66049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_DIRTY_INVERSE 0x400 /**< matrix inverse is dirty */ 67049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 68049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** angle preserving matrix flags mask */ 69049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \ 70049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_TRANSLATION | \ 71049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_UNIFORM_SCALE) 72049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 73049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** geometry related matrix flags mask */ 74049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \ 75049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_ROTATION | \ 76049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_TRANSLATION | \ 77049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_UNIFORM_SCALE | \ 78049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_SCALE | \ 79049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_3D | \ 80049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_PERSPECTIVE | \ 81049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_SINGULAR) 82049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 83049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** length preserving matrix flags mask */ 84049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \ 85049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_TRANSLATION) 86049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 87049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 88049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 3D (non-perspective) matrix flags mask */ 89049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \ 90049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_TRANSLATION | \ 91049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_UNIFORM_SCALE | \ 92049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_SCALE | \ 93049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_3D) 94049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 95049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** dirty matrix flags mask */ 96049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_DIRTY (MAT_DIRTY_TYPE | \ 97049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_DIRTY_FLAGS | \ 98049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_DIRTY_INVERSE) 99049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 100049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/*@}*/ 101049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 102049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 103049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 104049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Test geometry related matrix flags. 105049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * 106049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * \param mat a pointer to a GLmatrix structure. 107049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * \param a flags mask. 108049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * 109049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * \returns non-zero if all geometry related matrix flags are contained within 110049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * the mask, or zero otherwise. 111049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 112049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define TEST_MAT_FLAGS(mat, a) \ 113049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0) 114049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 115049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 116049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 117049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 1186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Names of the corresponding GLmatrixtype values. 1196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 12023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic const char *types[] = { 12123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_GENERAL", 12223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_IDENTITY", 12323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_3D_NO_ROT", 12423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_PERSPECTIVE", 12523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_2D", 12623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_2D_NO_ROT", 12723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_3D" 12823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell}; 12923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 13023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 1326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Identity matrix. 1336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 13423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLfloat Identity[16] = { 13523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1.0, 0.0, 0.0, 0.0, 13623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 0.0, 1.0, 0.0, 0.0, 13723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 0.0, 0.0, 1.0, 0.0, 13823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 0.0, 0.0, 0.0, 1.0 13923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell}; 14023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 1446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix multiplication */ 1456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 14623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define A(row,col) a[(col<<2)+row] 14823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define B(row,col) b[(col<<2)+row] 14923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define P(row,col) product[(col<<2)+row] 15023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 1526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Perform a full 4x4 matrix multiplication. 1536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param a matrix. 1556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param b matrix. 1566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param product will receive the product of \p a and \p b. 1576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \warning Is assumed that \p product != \p b. \p product == \p a is allowed. 1596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \note KW: 4*16 = 64 multiplications 1616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \author This \c matmul was contributed by Thomas Malik 1636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 16423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b ) 16523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 16623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLint i; 16723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell for (i = 0; i < 4; i++) { 16823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); 16923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); 17023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); 17123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); 17223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); 17323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 17423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 17523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 1776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Multiply two matrices known to occupy only the top three rows, such 1786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * as typical model matrices, and orthogonal matrices. 1796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param a matrix. 1816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param b matrix. 1826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param product will receive the product of \p a and \p b. 18323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 18423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b ) 18523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 18623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLint i; 18723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell for (i = 0; i < 3; i++) { 18823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); 18923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0); 19023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1); 19123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2); 19223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3; 19323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 19423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(3,0) = 0; 19523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(3,1) = 0; 19623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(3,2) = 0; 19723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(3,3) = 1; 19823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 19923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 20023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef A 20123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef B 20223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef P 20323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 20523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Multiply a matrix by an array of floats with known properties. 2066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2076dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure containing the left multiplication 2086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * matrix, and that will receive the product result. 2096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m right multiplication matrix array. 2106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param flags flags of the matrix \p m. 2116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Joins both flags and marks the type and inverse as dirty. Calls matmul34() 2136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * if both matrices are 3D, or matmul4() otherwise. 21423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 21523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags ) 21623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 21723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE); 21823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 21923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) 22023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matmul34( mat->m, mat->m, m ); 22122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes else 22222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes matmul4( mat->m, mat->m, m ); 22323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 22423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 2266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix multiplication. 2276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dest destination matrix. 2296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param a left matrix. 2306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param b right matrix. 2316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Joins both flags and marks the type and inverse as dirty. Calls matmul34() 2336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * if both matrices are 3D, or matmul4() otherwise. 2346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 2356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 2366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b ) 2376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 2386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell dest->flags = (a->flags | 2396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell b->flags | 2406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_TYPE | 2416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_INVERSE); 24223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D)) 2446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell matmul34( dest->m, a->m, b->m ); 2456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell else 2466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell matmul4( dest->m, a->m, b->m ); 2476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 2486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 2506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix multiplication. 2516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dest left and destination matrix. 2536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m right matrix array. 2546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Marks the matrix flags with general flag, and type and inverse dirty flags. 2566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calls matmul4() for the multiplication. 2576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 2586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 2596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m ) 2606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 2616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell dest->flags |= (MAT_FLAG_GENERAL | 2626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_TYPE | 263522ea4271804b75d90f9bc72b81bfd025bb137d0Brian Paul MAT_DIRTY_INVERSE | 264522ea4271804b75d90f9bc72b81bfd025bb137d0Brian Paul MAT_DIRTY_FLAGS); 2656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell matmul4( dest->m, dest->m, m ); 2676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 2686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 2706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 2736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix output */ 2746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 2756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 2776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Print a matrix array. 2786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix array. 2806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Called by _math_matrix_print() to print a matrix or its inverse. 2826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 28323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic void print_matrix_floats( const GLfloat m[16] ) 28423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 28523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell int i; 28623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell for (i=0;i<4;i++) { 2874e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] ); 28823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 28923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 29023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2916dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 2926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Dumps the contents of a GLmatrix structure. 2936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m pointer to the GLmatrix structure. 2956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 29622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 29723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_print( const GLmatrix *m ) 29823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 2994e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags); 30023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell print_matrix_floats(m->m); 3014e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul _mesa_debug(NULL, "Inverse: \n"); 30223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m->inv) { 30323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat prod[16]; 30423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell print_matrix_floats(m->inv); 30523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matmul4(prod, m->m, m->inv); 3064e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul _mesa_debug(NULL, "Mat * Inverse:\n"); 30723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell print_matrix_floats(prod); 30823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 30923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 3104e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul _mesa_debug(NULL, " - not available\n"); 31123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 31223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 31323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 3146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 3156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 3166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 3176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 3186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * References an element of 4x4 matrix. 3196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix array. 3216dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param c column of the desired element. 3226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param r row of the desired element. 3236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return value of the desired element. 3256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculate the linear storage index of the element and references it. 3276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 3286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell#define MAT(m,r,c) (m)[(c)*4+(r)] 32923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 33023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 3316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 3326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix inversion */ 3336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 33423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 3356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 3366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Swaps the values of two floating pointer variables. 3376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Used by invert_matrix_general() to swap the row pointers. 3396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 34023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; } 34123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 3426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 34323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Compute inverse of 4x4 transformation matrix. 3446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 3466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 3476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 3496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \author 35123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Code contributed by Jacques Leroy jle@star.be 3526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculates the inverse matrix by performing the gaussian matrix reduction 3546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * with partial pivoting followed by back/substitution with the loops manually 3556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * unrolled. 35623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 35723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_general( GLmatrix *mat ) 35823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 35923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *m = mat->m; 36023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 36123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat wtmp[4][8]; 36223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m0, m1, m2, m3, s; 36323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *r0, *r1, *r2, *r3; 36422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 36523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3]; 36622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 36723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1), 36823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3), 36923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0, 37022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 37123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1), 37223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3), 37323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0, 37422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 37523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1), 37623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3), 37723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0, 37822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 37923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1), 38023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3), 38123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0; 38222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 38323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* choose pivot - or die */ 384b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r3[0])>FABSF(r2[0])) SWAP_ROWS(r3, r2); 385b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r2[0])>FABSF(r1[0])) SWAP_ROWS(r2, r1); 386b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r1[0])>FABSF(r0[0])) SWAP_ROWS(r1, r0); 38723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (0.0 == r0[0]) return GL_FALSE; 38822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 38923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* eliminate first variable */ 39023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0]; 39123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s; 39223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s; 39323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s; 39423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[4]; 39523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; } 39623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[5]; 39723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; } 39823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[6]; 39923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; } 40023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[7]; 40123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; } 40222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 40323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* choose pivot - or die */ 404b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r3[1])>FABSF(r2[1])) SWAP_ROWS(r3, r2); 405b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r2[1])>FABSF(r1[1])) SWAP_ROWS(r2, r1); 40623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (0.0 == r1[1]) return GL_FALSE; 40722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 40823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* eliminate second variable */ 40923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1]; 41023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2]; 41123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3]; 41223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; } 41323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; } 41423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; } 41523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; } 41622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 41723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* choose pivot - or die */ 418b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r3[2])>FABSF(r2[2])) SWAP_ROWS(r3, r2); 41923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (0.0 == r2[2]) return GL_FALSE; 42022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 42123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* eliminate third variable */ 42223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m3 = r3[2]/r2[2]; 42323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4], 42423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], 42523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[7] -= m3 * r2[7]; 42622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 42723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* last check */ 42823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (0.0 == r3[3]) return GL_FALSE; 42922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 4307b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz s = 1.0F/r3[3]; /* now back substitute row 3 */ 43123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s; 43222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 43323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m2 = r2[3]; /* now back substitute row 2 */ 4347b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz s = 1.0F/r2[2]; 43523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2), 43623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2); 43723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m1 = r1[3]; 43823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1, 43923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1; 44023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m0 = r0[3]; 44123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0, 44223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0; 44322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 44423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m1 = r1[2]; /* now back substitute row 1 */ 4457b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz s = 1.0F/r1[1]; 44623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1), 44723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1); 44823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m0 = r0[2]; 44923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0, 45023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0; 45122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 45223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m0 = r0[1]; /* now back substitute row 0 */ 4537b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz s = 1.0F/r0[0]; 45423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0), 45523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0); 45622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 45723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5], 45823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7], 45923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5], 46023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7], 46123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5], 46223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7], 46323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5], 46422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; 46522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 46623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 46723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 46823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef SWAP_ROWS 46923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 4706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 4716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a general 3d transformation matrix. 4726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 4736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 4746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 4756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 4766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 4776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 4786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \author Adapted from graphics gems II. 4796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 4806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculates the inverse of the upper left by first calculating its 4816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * determinant and multiplying it to the symmetric adjust matrix of each 4826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * element. Finally deals with the translation part by transforming the 4836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * original translation vector using by the calculated submatrix inverse. 48422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes */ 48523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_3d_general( GLmatrix *mat ) 48623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 48723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 48823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 48923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat pos, neg, t; 49023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat det; 49123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 49223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Calculate the determinant of upper left 3x3 submatrix and 49322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * determine if the matrix is singular. 49423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 49523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell pos = neg = 0.0; 49623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2); 49723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 49823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 49923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2); 50023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 50123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 50223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2); 50323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 50423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 50523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2); 50623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 50723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 50823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2); 50923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 51023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 51123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2); 51223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 51323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 51423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell det = pos + neg; 51523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 51650db8129152f3d5ea8db13d55f82673d53bf1b8fBrian Paul if (FABSF(det) < 1e-25) 51723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 51822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 5197b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz det = 1.0F / det; 52023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det); 52123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det); 52223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det); 52323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det); 52423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det); 52523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det); 52623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det); 52723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det); 52823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det); 52923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 53023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Do the translation part */ 53123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + 53223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,0,1) + 53323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,0,2) ); 53423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + 53523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,1,1) + 53623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,1,2) ); 53723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + 53823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,2,1) + 53923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,2,2) ); 54022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 54123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 54223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 54323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 5446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 5456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a 3d transformation matrix. 5466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 5476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 5486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 5496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 5506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 5516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 5526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * If the matrix is not an angle preserving matrix then calls 5536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * invert_matrix_3d_general for the actual calculation. Otherwise calculates 5546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * the inverse matrix analyzing and inverting each of the scaling, rotation and 5556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * translation parts. 5566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 55723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_3d( GLmatrix *mat ) 55823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 55923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 56023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 56123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 56223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) { 56323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return invert_matrix_3d_general( mat ); 56423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 56522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 56623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_FLAG_UNIFORM_SCALE) { 56723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) + 56823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,0,1) * MAT(in,0,1) + 56923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,0,2) * MAT(in,0,2)); 57023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 57122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (scale == 0.0) 57223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 57323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 5747b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz scale = 1.0F / scale; 57523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 57623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Transpose and scale the 3 by 3 upper-left submatrix. */ 57723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,0) = scale * MAT(in,0,0); 57823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,0) = scale * MAT(in,0,1); 57923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,0) = scale * MAT(in,0,2); 58023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,1) = scale * MAT(in,1,0); 58123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,1) = scale * MAT(in,1,1); 58223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,1) = scale * MAT(in,1,2); 58323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,2) = scale * MAT(in,2,0); 58423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,2) = scale * MAT(in,2,1); 58523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = scale * MAT(in,2,2); 58623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 58723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if (mat->flags & MAT_FLAG_ROTATION) { 58823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Transpose the 3 by 3 upper-left submatrix. */ 58923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,0) = MAT(in,0,0); 59023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,0) = MAT(in,0,1); 59123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,0) = MAT(in,0,2); 59223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,1) = MAT(in,1,0); 59323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,1) = MAT(in,1,1); 59423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,1) = MAT(in,1,2); 59523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,2) = MAT(in,2,0); 59623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,2) = MAT(in,2,1); 59723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = MAT(in,2,2); 59823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 59923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 60023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* pure translation */ 601e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( out, Identity, sizeof(Identity) ); 60223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - MAT(in,0,3); 60323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - MAT(in,1,3); 60423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = - MAT(in,2,3); 60523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 60623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 60722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 60823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_FLAG_TRANSLATION) { 60923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Do the translation part */ 61023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + 61123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,0,1) + 61223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,0,2) ); 61323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + 61423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,1,1) + 61523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,1,2) ); 61623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + 61723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,2,1) + 61823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,2,2) ); 61923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 62023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 62123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0; 62223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 62322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 62423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 62523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 62623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 6276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 6286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of an identity transformation matrix. 6296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 6316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 6326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return always GL_TRUE. 6346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Simply copies Identity into GLmatrix::inv. 6366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 63723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_identity( GLmatrix *mat ) 63823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 639e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->inv, Identity, sizeof(Identity) ); 64023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 64123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 64223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 6436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 6446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a no-rotation 3d transformation matrix. 6456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 6476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 6486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 6506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculates the 6526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 65323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_3d_no_rot( GLmatrix *mat ) 65423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 65523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 65623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 65723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 65822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 ) 65923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 66022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 661e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( out, Identity, 16 * sizeof(GLfloat) ); 6627b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,0,0) = 1.0F / MAT(in,0,0); 6637b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,1,1) = 1.0F / MAT(in,1,1); 6647b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,2,2) = 1.0F / MAT(in,2,2); 66523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 66623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_FLAG_TRANSLATION) { 66723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); 66823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); 66923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2)); 67023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 67123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 67223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 67323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 67423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 6756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 6766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a no-rotation 2d transformation matrix. 6776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 6796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 6806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 6826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculates the inverse matrix by applying the inverse scaling and 6846dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * translation to the identity matrix. 6856dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 68623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_2d_no_rot( GLmatrix *mat ) 68723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 68823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 68923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 69023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 69122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0) 69223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 69322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 694e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( out, Identity, 16 * sizeof(GLfloat) ); 6957b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,0,0) = 1.0F / MAT(in,0,0); 6967b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,1,1) = 1.0F / MAT(in,1,1); 69723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 69823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_FLAG_TRANSLATION) { 69923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); 70023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); 70123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 70223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 70323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 70423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 70523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7064e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul#if 0 7074e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul/* broken */ 70823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_perspective( GLmatrix *mat ) 70923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 71023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 71123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 71223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 71323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (MAT(in,2,3) == 0) 71423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 71523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 716e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( out, Identity, 16 * sizeof(GLfloat) ); 71723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7187b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,0,0) = 1.0F / MAT(in,0,0); 7197b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,1,1) = 1.0F / MAT(in,1,1); 72023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 72123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = MAT(in,0,2); 72223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = MAT(in,1,2); 72323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 72423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = 0; 72523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = -1; 72623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7277b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,3,2) = 1.0F / MAT(in,2,3); 72823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2); 72923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 73023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 73123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 7324e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul#endif 73323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 7356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix inversion function pointer type. 7366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 73723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwelltypedef GLboolean (*inv_mat_func)( GLmatrix *mat ); 73823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 7406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Table of the matrix inversion functions according to the matrix type. 7416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 74223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic inv_mat_func inv_mat_tab[7] = { 74323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_general, 74423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_identity, 74523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_3d_no_rot, 746a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul#if 0 747a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul /* Don't use this function for now - it fails when the projection matrix 748a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul * is premultiplied by a translation (ala Chromium's tilesort SPU). 749a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul */ 75023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_perspective, 751a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul#else 752a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul invert_matrix_general, 753a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul#endif 75423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_3d, /* lazy! */ 75523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_2d_no_rot, 75623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_3d 75723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell}; 75823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 7606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a transformation matrix. 7616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 7626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 7636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 7646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 7656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 7666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 7676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calls the matrix inversion function in inv_mat_tab corresponding to the 7686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag, 7696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * and copies the identity matrix into GLmatrix::inv. 7706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 77123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean matrix_invert( GLmatrix *mat ) 77223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 77323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (inv_mat_tab[mat->type](mat)) { 77423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags &= ~MAT_FLAG_SINGULAR; 77523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 77623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } else { 77723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_SINGULAR; 778e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->inv, Identity, sizeof(Identity) ); 77923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 78022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes } 78123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 78223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 78423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 78523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7866dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 7876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix generation */ 7886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 78923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 79123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Generate a 4x4 transformation matrix from glRotate parameters, and 7926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * post-multiply the input matrix by it. 7936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 7946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \author 7956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * This function was contributed by Erich Boleyn (erich@uruk.org). 7966dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Optimizations contributed by Rudolf Opalla (rudi@khm.de). 79723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 79822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 79922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_math_matrix_rotate( GLmatrix *mat, 80023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat angle, GLfloat x, GLfloat y, GLfloat z ) 80123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 8024991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c; 80323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m[16]; 8044991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul GLboolean optimized; 80522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 806165694ad65374ff4330bd80acb398fe0428ba2e6Eric Anholt s = (GLfloat) sin( angle * DEG2RAD ); 807165694ad65374ff4330bd80acb398fe0428ba2e6Eric Anholt c = (GLfloat) cos( angle * DEG2RAD ); 80823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 809e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy(m, Identity, sizeof(GLfloat)*16); 8104991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul optimized = GL_FALSE; 81123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 81223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define M(row,col) m[col*4+row] 81323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 8144991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (x == 0.0F) { 8154991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (y == 0.0F) { 8164991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (z != 0.0F) { 8174991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul optimized = GL_TRUE; 8184991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* rotate only around z-axis */ 8194991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,0) = c; 8204991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,1) = c; 8214991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (z < 0.0F) { 8224991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,1) = s; 8234991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,0) = -s; 8244991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8254991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else { 8264991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,1) = -s; 8274991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,0) = s; 8284991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8294991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8304991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8314991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else if (z == 0.0F) { 8324991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul optimized = GL_TRUE; 8334991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* rotate only around y-axis */ 8344991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,0) = c; 8354991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,2) = c; 8364991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (y < 0.0F) { 8374991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,2) = -s; 8384991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,0) = s; 8394991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8404991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else { 8414991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,2) = s; 8424991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,0) = -s; 8434991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8444991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8454991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8464991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else if (y == 0.0F) { 8474991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (z == 0.0F) { 8484991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul optimized = GL_TRUE; 8494991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* rotate only around x-axis */ 8504991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,1) = c; 8514991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,2) = c; 8521e091f48f0434e8fb9713fbebc9d74ad68a75e34Brian Paul if (x < 0.0F) { 8534991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,2) = s; 8544991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,1) = -s; 8554991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8564991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else { 8574991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,2) = -s; 8584991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,1) = s; 8594991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8604991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8614991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 86223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 8634991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (!optimized) { 86427558a160a9fe91745728d7626995cd88f8fe339Brian Paul const GLfloat mag = SQRTF(x * x + y * y + z * z); 8654991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 8664991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (mag <= 1.0e-4) { 8674991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* no rotation, leave mat as-is */ 8684991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul return; 8694991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8704991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 8714991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul x /= mag; 8724991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul y /= mag; 8734991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul z /= mag; 8744991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 8754991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 8764991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* 8774991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Arbitrary axis rotation matrix. 8784991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 8794991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied 8804991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation 8814991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * (which is about the X-axis), and the two composite transforms 8824991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary 8834991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * from the arbitrary axis to the X-axis then back. They are 8844991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * all elementary rotations. 8854991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 8864991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Rz' is a rotation about the Z-axis, to bring the axis vector 8874991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * into the x-z plane. Then Ry' is applied, rotating about the 8884991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Y-axis to bring the axis vector parallel with the X-axis. The 8894991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * rotation about the X-axis is then performed. Ry and Rz are 8904991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * simply the respective inverse transforms to bring the arbitrary 891fab1f07d6ad01463897ae792f4b33738afb07369Jeff Smith * axis back to its original orientation. The first transforms 8924991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Rz' and Ry' are considered inverses, since the data from the 8934991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * arbitrary axis gives you info on how to get to it, not how 8944991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * to get away from it, and an inverse must be applied. 8954991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 8964991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * The basic calculation used is to recognize that the arbitrary 8974991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * axis vector (x, y, z), since it is of unit length, actually 8984991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * represents the sines and cosines of the angles to rotate the 8994991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * X-axis to the same orientation, with theta being the angle about 9004991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Z and phi the angle about Y (in the order described above) 9014991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * as follows: 9024991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9034991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * cos ( theta ) = x / sqrt ( 1 - z^2 ) 9044991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * sin ( theta ) = y / sqrt ( 1 - z^2 ) 9054991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9064991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * cos ( phi ) = sqrt ( 1 - z^2 ) 9074991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * sin ( phi ) = z 9084991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9094991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Note that cos ( phi ) can further be inserted to the above 9104991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * formulas: 9114991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9124991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * cos ( theta ) = x / cos ( phi ) 9134991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * sin ( theta ) = y / sin ( phi ) 9144991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9154991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * ...etc. Because of those relations and the standard trigonometric 9164991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * relations, it is pssible to reduce the transforms down to what 9174991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * is used below. It may be that any primary axis chosen will give the 9184991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * same results (modulo a sign convention) using thie method. 9194991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9204991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Particularly nice is to notice that all divisions that might 9214991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * have caused trouble when parallel to certain planes or 9224991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * axis go away with care paid to reducing the expressions. 9234991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * After checking, it does perform correctly under all cases, since 9244991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * in all the cases of division where the denominator would have 9254991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * been zero, the numerator would have been zero as well, giving 9264991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * the expected result. 9274991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul */ 9284991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 9294991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul xx = x * x; 9304991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul yy = y * y; 9314991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul zz = z * z; 9324991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul xy = x * y; 9334991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul yz = y * z; 9344991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul zx = z * x; 9354991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul xs = x * s; 9364991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul ys = y * s; 9374991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul zs = z * s; 9384991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul one_c = 1.0F - c; 9394991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 9404991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* We already hold the identity-matrix so we can skip some statements */ 9414991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,0) = (one_c * xx) + c; 9424991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,1) = (one_c * xy) - zs; 9434991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,2) = (one_c * zx) + ys; 9444991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul/* M(0,3) = 0.0F; */ 9454991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 9464991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,0) = (one_c * xy) + zs; 9474991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,1) = (one_c * yy) + c; 9484991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,2) = (one_c * yz) - xs; 9494991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul/* M(1,3) = 0.0F; */ 9504991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 9514991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,0) = (one_c * zx) - ys; 9524991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,1) = (one_c * yz) + xs; 9534991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,2) = (one_c * zz) + c; 9544991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul/* M(2,3) = 0.0F; */ 95523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 9564991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul/* 9574991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(3,0) = 0.0F; 9584991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(3,1) = 0.0F; 9594991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(3,2) = 0.0F; 9604991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(3,3) = 1.0F; 9614991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul*/ 9624991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 96323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef M 96423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 96523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_multf( mat, m, MAT_FLAG_ROTATION ); 96623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 96723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 9686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 9696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Apply a perspective projection matrix. 9706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 9716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix to apply the projection. 9726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param left left clipping plane coordinate. 9736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param right right clipping plane coordinate. 9746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param bottom bottom clipping plane coordinate. 9756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param top top clipping plane coordinate. 9766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param nearval distance to the near clipping plane. 9776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param farval distance to the far clipping plane. 9786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 9796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Creates the projection matrix and multiplies it with \p mat, marking the 9806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * MAT_FLAG_PERSPECTIVE flag. 9816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 98223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellvoid 98322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_math_matrix_frustum( GLmatrix *mat, 984d8bc5a9eba720ffb6a503d32715f895dbdad7197Brian Paul GLfloat left, GLfloat right, 98522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes GLfloat bottom, GLfloat top, 986d8bc5a9eba720ffb6a503d32715f895dbdad7197Brian Paul GLfloat nearval, GLfloat farval ) 98723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 98823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat x, y, a, b, c, d; 98923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m[16]; 99023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 9917b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz x = (2.0F*nearval) / (right-left); 9927b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz y = (2.0F*nearval) / (top-bottom); 99323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell a = (right+left) / (right-left); 99423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell b = (top+bottom) / (top-bottom); 99523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell c = -(farval+nearval) / ( farval-nearval); 9967b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */ 99723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 99823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define M(row,col) m[col*4+row] 99923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F; 100023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F; 100123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d; 100223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F; 100323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef M 100423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 100523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE ); 100623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 100723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 10086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 10096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Apply an orthographic projection matrix. 10106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix to apply the projection. 10126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param left left clipping plane coordinate. 10136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param right right clipping plane coordinate. 10146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param bottom bottom clipping plane coordinate. 10156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param top top clipping plane coordinate. 10166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param nearval distance to the near clipping plane. 10176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param farval distance to the far clipping plane. 10186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Creates the projection matrix and multiplies it with \p mat, marking the 10206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags. 10216dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 102223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellvoid 102322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_math_matrix_ortho( GLmatrix *mat, 102423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat left, GLfloat right, 102522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes GLfloat bottom, GLfloat top, 102623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat nearval, GLfloat farval ) 102723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 102823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m[16]; 102923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 103023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define M(row,col) m[col*4+row] 1031e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(0,0) = 2.0F / (right-left); 1032e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(0,1) = 0.0F; 1033e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(0,2) = 0.0F; 1034e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(0,3) = -(right+left) / (right-left); 1035e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul 1036e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(1,0) = 0.0F; 1037e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(1,1) = 2.0F / (top-bottom); 1038e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(1,2) = 0.0F; 1039e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(1,3) = -(top+bottom) / (top-bottom); 1040e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul 1041e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(2,0) = 0.0F; 1042e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(2,1) = 0.0F; 1043e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(2,2) = -2.0F / (farval-nearval); 1044e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(2,3) = -(farval+nearval) / (farval-nearval); 1045e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul 1046e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(3,0) = 0.0F; 1047e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(3,1) = 0.0F; 1048e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(3,2) = 0.0F; 1049e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(3,3) = 1.0F; 105023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef M 105123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 105223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION)); 105323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 105423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 10556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 10566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Multiply a matrix with a general scaling matrix. 10576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 10596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param x x axis scale factor. 10606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param y y axis scale factor. 10616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param z z axis scale factor. 10626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Multiplies in-place the elements of \p mat by the scale factors. Checks if 10646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE 10656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and 10666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * MAT_DIRTY_INVERSE dirty flags. 10676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 10686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 10696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ) 10706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 10716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell GLfloat *m = mat->m; 10726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[0] *= x; m[4] *= y; m[8] *= z; 10736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[1] *= x; m[5] *= y; m[9] *= z; 10746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[2] *= x; m[6] *= y; m[10] *= z; 10756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[3] *= x; m[7] *= y; m[11] *= z; 10766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 1077b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(x - y) < 1e-8 && FABSF(x - z) < 1e-8) 10786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags |= MAT_FLAG_UNIFORM_SCALE; 10796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell else 10806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_SCALE; 10816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 10826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags |= (MAT_DIRTY_TYPE | 10836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_INVERSE); 10846dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 10856dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 10866dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 10876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Multiply a matrix with a translation matrix. 10886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10896dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 10906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param x translation vector x coordinate. 10916dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param y translation vector y coordinate. 10926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param z translation vector z coordinate. 10936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Adds the translation coordinates to the elements of \p mat in-place. Marks 10956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE 10966dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * dirty flags. 10976dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 10986dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 10996dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ) 11006dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 11016dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell GLfloat *m = mat->m; 11026dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[12] = m[0] * x + m[4] * y + m[8] * z + m[12]; 11036dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[13] = m[1] * x + m[5] * y + m[9] * z + m[13]; 11046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[14] = m[2] * x + m[6] * y + m[10] * z + m[14]; 11056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[15] = m[3] * x + m[7] * y + m[11] * z + m[15]; 11066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11076dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags |= (MAT_FLAG_TRANSLATION | 11086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_TYPE | 11096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_INVERSE); 11106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 11116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 1112049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1113049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 1114049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Set matrix to do viewport and depthrange mapping. 1115049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Transforms Normalized Device Coords to window/Z values. 1116049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 1117049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paulvoid 1118049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_viewport(GLmatrix *m, GLint x, GLint y, GLint width, GLint height, 1119049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul GLfloat zNear, GLfloat zFar, GLfloat depthMax) 1120049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1121049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_SX] = (GLfloat) width / 2.0F; 1122049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_TX] = m->m[MAT_SX] + x; 1123049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_SY] = (GLfloat) height / 2.0F; 1124049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_TY] = m->m[MAT_SY] + y; 1125049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_SZ] = depthMax * ((zFar - zNear) / 2.0F); 1126049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_TZ] = depthMax * ((zFar - zNear) / 2.0F + zNear); 1127049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION; 1128049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->type = MATRIX_3D_NO_ROT; 1129049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1130049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1131049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 11326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 11336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Set a matrix to the identity matrix. 11346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 11356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 11366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 11376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL. 11386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Sets the matrix type to identity, and clear the dirty flags. 11396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 11406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 11416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_set_identity( GLmatrix *mat ) 11426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 1143e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->m, Identity, 16*sizeof(GLfloat) ); 11446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (mat->inv) 1146e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->inv, Identity, 16*sizeof(GLfloat) ); 11476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->type = MATRIX_IDENTITY; 11496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags &= ~(MAT_DIRTY_FLAGS| 11506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_TYPE| 11516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_INVERSE); 11526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 11536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 11556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 11586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix analysis */ 11596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 116023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 116123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define ZERO(x) (1<<x) 116223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define ONE(x) (1<<(x+16)) 116323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 116423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14)) 116523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5)) 116623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 116723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\ 116823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\ 116923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 117023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 117123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 117223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \ 117323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(1) | ZERO(9) | \ 117423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 117523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 117623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 117723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_2D ( ZERO(8) | \ 117823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(9) | \ 117923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 118023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 118123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 118223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 118323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \ 118423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(1) | ZERO(9) | \ 118523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | \ 118623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 118723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 118823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_3D ( \ 118923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell \ 119023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell \ 119123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 119223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 119323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 119423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\ 119523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(1) | ZERO(13) |\ 119623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | \ 119723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(15) ) 119823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 119923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define SQ(x) ((x)*(x)) 120022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 12016dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 12026dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Determine type and flags from scratch. 12036dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 12046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 12056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 12066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * This is expensive enough to only want to do it once. 120723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 1208ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwellstatic void analyse_from_scratch( GLmatrix *mat ) 120923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 121023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *m = mat->m; 121123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLuint mask = 0; 121223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLuint i; 121323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 121423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell for (i = 0 ; i < 16 ; i++) { 121523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[i] == 0.0) mask |= (1<<i); 121623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 121722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 121823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[0] == 1.0F) mask |= (1<<16); 121923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[5] == 1.0F) mask |= (1<<21); 122023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[10] == 1.0F) mask |= (1<<26); 122123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[15] == 1.0F) mask |= (1<<31); 122223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 122323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags &= ~MAT_FLAGS_GEOMETRY; 122423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 122522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes /* Check for translation - no-one really cares 122623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 122722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if ((mask & MASK_NO_TRX) != MASK_NO_TRX) 122822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes mat->flags |= MAT_FLAG_TRANSLATION; 122923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 123023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Do the real work 123123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 1232b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul if (mask == (GLuint) MASK_IDENTITY) { 123323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_IDENTITY; 123423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 1235b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) { 123623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_2D_NO_ROT; 123722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 123823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE) 12392dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul mat->flags |= MAT_FLAG_GENERAL_SCALE; 124023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 1241b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul else if ((mask & MASK_2D) == (GLuint) MASK_2D) { 124223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat mm = DOT2(m, m); 124323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m4m4 = DOT2(m+4,m+4); 124423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat mm4 = DOT2(m,m+4); 124523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 124623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_2D; 124723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 124823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for scale */ 124923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(mm-1) > SQ(1e-6) || 125022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes SQ(m4m4-1) > SQ(1e-6)) 125123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_SCALE; 125223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 125323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for rotation */ 125423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(mm4) > SQ(1e-6)) 125523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_3D; 125623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else 125723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_ROTATION; 125823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 125923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 1260b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) { 126123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_3D_NO_ROT; 126223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 126323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for scale */ 126422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (SQ(m[0]-m[5]) < SQ(1e-6) && 126523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell SQ(m[0]-m[10]) < SQ(1e-6)) { 126623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(m[0]-1.0) > SQ(1e-6)) { 126723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_UNIFORM_SCALE; 126823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 126923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 127023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 127123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_SCALE; 127223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 127323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 1274b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul else if ((mask & MASK_3D) == (GLuint) MASK_3D) { 127523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat c1 = DOT3(m,m); 127623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat c2 = DOT3(m+4,m+4); 127723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat c3 = DOT3(m+8,m+8); 127823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat d1 = DOT3(m, m+4); 127923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat cp[3]; 128023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 128123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_3D; 128223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 128323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for scale */ 128423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) { 128523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(c1-1.0) > SQ(1e-6)) 128623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_UNIFORM_SCALE; 128723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* else no scale at all */ 128823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 128923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 129023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_SCALE; 129123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 129223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 129323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for rotation */ 129423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(d1) < SQ(1e-6)) { 129523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell CROSS3( cp, m, m+4 ); 129623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell SUB_3V( cp, cp, (m+8) ); 129722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (LEN_SQUARED_3FV(cp) < SQ(1e-6)) 129823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_ROTATION; 129923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else 130023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_3D; 130123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 130223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 130323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */ 130423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 130523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 130623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) { 130723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_PERSPECTIVE; 130823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL; 130923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 131023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 131123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_GENERAL; 131223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL; 131323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 131423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 131523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 13166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 13176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Analyze a matrix given that its flags are accurate. 13186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 13196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * This is the more common operation, hopefully. 132023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 1321ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwellstatic void analyse_from_flags( GLmatrix *mat ) 132223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 132323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *m = mat->m; 132423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 132523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (TEST_MAT_FLAGS(mat, 0)) { 132623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_IDENTITY; 132723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 132823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION | 132923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT_FLAG_UNIFORM_SCALE | 133023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT_FLAG_GENERAL_SCALE))) { 133123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if ( m[10]==1.0F && m[14]==0.0F ) { 133223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_2D_NO_ROT; 133323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 133423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 133523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_3D_NO_ROT; 133623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 133723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 133823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) { 133922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if ( m[ 8]==0.0F 134023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[ 9]==0.0F 134123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) { 134223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_2D; 134323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 134423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 134523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_3D; 134623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 134723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 134823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if ( m[4]==0.0F && m[12]==0.0F 134923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[1]==0.0F && m[13]==0.0F 135023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[2]==0.0F && m[6]==0.0F 135123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) { 135223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_PERSPECTIVE; 135323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 135423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 135523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_GENERAL; 135623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 135723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 135823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 13596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 13606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Analyze and update a matrix. 13616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 13626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 13636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 13646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * If the matrix type is dirty then calls either analyse_from_scratch() or 13656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * analyse_from_flags() to determine its type, according to whether the flags 13666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * are dirty or not, respectively. If the matrix has an inverse and it's dirty 13676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * then calls matrix_invert(). Finally clears the dirty flags. 13686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 136922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 137022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_math_matrix_analyse( GLmatrix *mat ) 137123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 137223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_DIRTY_TYPE) { 137322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (mat->flags & MAT_DIRTY_FLAGS) 1374ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwell analyse_from_scratch( mat ); 137523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else 1376ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwell analyse_from_flags( mat ); 137723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 137823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 137923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) { 138023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_invert( mat ); 1381ce461ffc5aa2ea6941d6722e8ed473cda8c17833Brian Paul mat->flags &= ~MAT_DIRTY_INVERSE; 138223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 138323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1384ce461ffc5aa2ea6941d6722e8ed473cda8c17833Brian Paul mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE); 138523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 138623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 13876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 13886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 138923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1390049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 1391049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Test if the given matrix preserves vector lengths. 1392049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 1393049e320f46f3a3daaa36ef67cc680dc504c124d5Brian PaulGLboolean 1394049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_is_length_preserving( const GLmatrix *m ) 1395049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1396049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING); 1397049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1398049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1399049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1400049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 1401049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Test if the given matrix does any rotation. 1402049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * (or perhaps if the upper-left 3x3 is non-identity) 1403049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 1404049e320f46f3a3daaa36ef67cc680dc504c124d5Brian PaulGLboolean 1405049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_has_rotation( const GLmatrix *m ) 1406049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1407049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul if (m->flags & (MAT_FLAG_GENERAL | 1408049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_ROTATION | 1409049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_3D | 1410049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_PERSPECTIVE)) 1411049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return GL_TRUE; 1412049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul else 1413049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return GL_FALSE; 1414049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1415049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1416049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1417049e320f46f3a3daaa36ef67cc680dc504c124d5Brian PaulGLboolean 1418049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_is_general_scale( const GLmatrix *m ) 1419049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1420049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE; 1421049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1422049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1423049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1424049e320f46f3a3daaa36ef67cc680dc504c124d5Brian PaulGLboolean 1425049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_is_dirty( const GLmatrix *m ) 1426049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1427049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE; 1428049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1429049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1430049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 14316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 14326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix setup */ 14336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 14346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 14356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 14366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Copy a matrix. 14376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param to destination matrix. 14396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param from source matrix. 14406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Copies all fields in GLmatrix, creating an inverse array if necessary. 14426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 144322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 144423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_copy( GLmatrix *to, const GLmatrix *from ) 144523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 1446e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( to->m, from->m, sizeof(Identity) ); 144723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to->flags = from->flags; 144823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to->type = from->type; 144923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 145023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (to->inv != 0) { 145123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (from->inv == 0) { 145223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_invert( to ); 145323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 145423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 1455e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy(to->inv, from->inv, sizeof(GLfloat)*16); 145623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 145723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 145823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 145923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 14616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Loads a matrix array into GLmatrix. 14626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix array. 14646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 14656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY 14676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * flags. 14686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 146922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 147023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_loadf( GLmatrix *mat, const GLfloat *m ) 147123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 1472e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->m, m, 16*sizeof(GLfloat) ); 147323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY); 147423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 147523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 14776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix constructor. 14786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix. 14806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Initialize the GLmatrix fields. 14826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 148322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 148423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_ctr( GLmatrix *m ) 148523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 148699ae9e8d7d57ae37629754edd5b1e3716611827fKristian Høgsberg m->m = (GLfloat *) _mesa_align_malloc( 16 * sizeof(GLfloat), 16 ); 148730f51ae067379c2b3573c06b707d25a9704df7beBrian Paul if (m->m) 1488e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( m->m, Identity, sizeof(Identity) ); 148930f51ae067379c2b3573c06b707d25a9704df7beBrian Paul m->inv = NULL; 149023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m->type = MATRIX_IDENTITY; 149123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m->flags = 0; 149223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 149323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 14956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix destructor. 14966dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14976dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix. 14986dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14996dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Frees the data in a GLmatrix. 15006dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 150122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 150223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_dtr( GLmatrix *m ) 150323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 150430f51ae067379c2b3573c06b707d25a9704df7beBrian Paul if (m->m) { 150599ae9e8d7d57ae37629754edd5b1e3716611827fKristian Høgsberg _mesa_align_free( m->m ); 150630f51ae067379c2b3573c06b707d25a9704df7beBrian Paul m->m = NULL; 150723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 150830f51ae067379c2b3573c06b707d25a9704df7beBrian Paul if (m->inv) { 150999ae9e8d7d57ae37629754edd5b1e3716611827fKristian Høgsberg _mesa_align_free( m->inv ); 151030f51ae067379c2b3573c06b707d25a9704df7beBrian Paul m->inv = NULL; 151123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 151223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 151323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 15156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Allocate a matrix inverse. 15166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 15176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix. 15186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 15196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Allocates the matrix inverse, GLmatrix::inv, and sets it to Identity. 15206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 152122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 152223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_alloc_inv( GLmatrix *m ) 152323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 152430f51ae067379c2b3573c06b707d25a9704df7beBrian Paul if (!m->inv) { 152599ae9e8d7d57ae37629754edd5b1e3716611827fKristian Høgsberg m->inv = (GLfloat *) _mesa_align_malloc( 16 * sizeof(GLfloat), 16 ); 152630f51ae067379c2b3573c06b707d25a9704df7beBrian Paul if (m->inv) 1527e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( m->inv, Identity, 16 * sizeof(GLfloat) ); 152823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 152923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 153023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 153223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 153323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 15356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix transpose */ 15366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 153723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 15396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Transpose a GLfloat matrix. 15406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 15416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param to destination array. 15426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param from source array. 15436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 154422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 154523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_transposef( GLfloat to[16], const GLfloat from[16] ) 154623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 154723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[0] = from[0]; 154823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[1] = from[4]; 154923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[2] = from[8]; 155023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[3] = from[12]; 155123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[4] = from[1]; 155223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[5] = from[5]; 155323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[6] = from[9]; 155423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[7] = from[13]; 155523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[8] = from[2]; 155623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[9] = from[6]; 155723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[10] = from[10]; 155823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[11] = from[14]; 155923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[12] = from[3]; 156023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[13] = from[7]; 156123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[14] = from[11]; 156223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[15] = from[15]; 156323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 156423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 15666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Transpose a GLdouble matrix. 15676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 15686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param to destination array. 15696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param from source array. 15706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 157122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 157223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_transposed( GLdouble to[16], const GLdouble from[16] ) 157323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 157423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[0] = from[0]; 157523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[1] = from[4]; 157623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[2] = from[8]; 157723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[3] = from[12]; 157823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[4] = from[1]; 157923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[5] = from[5]; 158023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[6] = from[9]; 158123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[7] = from[13]; 158223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[8] = from[2]; 158323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[9] = from[6]; 158423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[10] = from[10]; 158523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[11] = from[14]; 158623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[12] = from[3]; 158723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[13] = from[7]; 158823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[14] = from[11]; 158923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[15] = from[15]; 159023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 159123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 15936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Transpose a GLdouble matrix and convert to GLfloat. 15946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 15956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param to destination array. 15966dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param from source array. 15976dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 159822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 159923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_transposefd( GLfloat to[16], const GLdouble from[16] ) 160023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 16017b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[0] = (GLfloat) from[0]; 16027b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[1] = (GLfloat) from[4]; 16037b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[2] = (GLfloat) from[8]; 16047b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[3] = (GLfloat) from[12]; 16057b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[4] = (GLfloat) from[1]; 16067b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[5] = (GLfloat) from[5]; 16077b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[6] = (GLfloat) from[9]; 16087b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[7] = (GLfloat) from[13]; 16097b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[8] = (GLfloat) from[2]; 16107b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[9] = (GLfloat) from[6]; 16117b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[10] = (GLfloat) from[10]; 16127b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[11] = (GLfloat) from[14]; 16137b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[12] = (GLfloat) from[3]; 16147b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[13] = (GLfloat) from[7]; 16157b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[14] = (GLfloat) from[11]; 16167b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[15] = (GLfloat) from[15]; 161723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 16186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 16196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 16206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 1621987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul 1622987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul/** 1623987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This 1624987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * function is used for transforming clipping plane equations and spotlight 1625987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * directions. 1626987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * Mathematically, u = v * m. 1627987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * Input: v - input vector 1628987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * m - transformation matrix 1629987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * Output: u - transformed vector 1630987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul */ 1631987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paulvoid 1632987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul_mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] ) 1633987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul{ 1634987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul const GLfloat v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; 1635987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul#define M(row,col) m[row + col*4] 1636987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0); 1637987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1); 1638987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2); 1639987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3); 1640987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul#undef M 1641987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul} 1642