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 */ 49049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/*@{*/ 50049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_IDENTITY 0 /**< is an identity matrix flag. 51049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * (Not actually used - the identity 52049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * matrix is identified by the absense 53049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * of all other flags.) 54049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 55049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_GENERAL 0x1 /**< is a general matrix flag */ 56049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_ROTATION 0x2 /**< is a rotation matrix flag */ 57049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_TRANSLATION 0x4 /**< is a translation matrix flag */ 58049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_UNIFORM_SCALE 0x8 /**< is an uniform scaling matrix flag */ 59049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_GENERAL_SCALE 0x10 /**< is a general scaling matrix flag */ 60049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_GENERAL_3D 0x20 /**< general 3D matrix flag */ 61049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_PERSPECTIVE 0x40 /**< is a perspective proj matrix flag */ 62049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAG_SINGULAR 0x80 /**< is a singular matrix flag */ 63049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_DIRTY_TYPE 0x100 /**< matrix type is dirty */ 64049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_DIRTY_FLAGS 0x200 /**< matrix flags are dirty */ 65049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_DIRTY_INVERSE 0x400 /**< matrix inverse is dirty */ 66049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 67049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** angle preserving matrix flags mask */ 68049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \ 69049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_TRANSLATION | \ 70049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_UNIFORM_SCALE) 71049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 72049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** geometry related matrix flags mask */ 73049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \ 74049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_ROTATION | \ 75049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_TRANSLATION | \ 76049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_UNIFORM_SCALE | \ 77049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_SCALE | \ 78049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_3D | \ 79049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_PERSPECTIVE | \ 80049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_SINGULAR) 81049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 82049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** length preserving matrix flags mask */ 83049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \ 84049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_TRANSLATION) 85049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 86049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 87049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 3D (non-perspective) matrix flags mask */ 88049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \ 89049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_TRANSLATION | \ 90049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_UNIFORM_SCALE | \ 91049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_SCALE | \ 92049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_3D) 93049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 94049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** dirty matrix flags mask */ 95049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define MAT_DIRTY (MAT_DIRTY_TYPE | \ 96049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_DIRTY_FLAGS | \ 97049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_DIRTY_INVERSE) 98049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 99049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/*@}*/ 100049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 101049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 102049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 103049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Test geometry related matrix flags. 104049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * 105049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * \param mat a pointer to a GLmatrix structure. 106049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * \param a flags mask. 107049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * 108049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * \returns non-zero if all geometry related matrix flags are contained within 109049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * the mask, or zero otherwise. 110049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 111049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul#define TEST_MAT_FLAGS(mat, a) \ 112049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0) 113049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 114049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 115049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 116049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 1176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Names of the corresponding GLmatrixtype values. 1186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 11923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic const char *types[] = { 12023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_GENERAL", 12123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_IDENTITY", 12223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_3D_NO_ROT", 12323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_PERSPECTIVE", 12423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_2D", 12523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_2D_NO_ROT", 12623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell "MATRIX_3D" 12723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell}; 12823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 12923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 1316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Identity matrix. 1326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 13323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLfloat Identity[16] = { 13423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1.0, 0.0, 0.0, 0.0, 13523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 0.0, 1.0, 0.0, 0.0, 13623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 0.0, 0.0, 1.0, 0.0, 13723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 0.0, 0.0, 0.0, 1.0 13823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell}; 13923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 1436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix multiplication */ 1446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 14523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define A(row,col) a[(col<<2)+row] 14723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define B(row,col) b[(col<<2)+row] 14823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define P(row,col) product[(col<<2)+row] 14923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 1516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Perform a full 4x4 matrix multiplication. 1526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param a matrix. 1546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param b matrix. 1556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param product will receive the product of \p a and \p b. 1566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \warning Is assumed that \p product != \p b. \p product == \p a is allowed. 1586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \note KW: 4*16 = 64 multiplications 1606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \author This \c matmul was contributed by Thomas Malik 1626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 16323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b ) 16423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 16523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLint i; 16623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell for (i = 0; i < 4; i++) { 16723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); 16823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); 16923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); 17023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); 17123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); 17223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 17323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 17423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 1766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Multiply two matrices known to occupy only the top three rows, such 1776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * as typical model matrices, and orthogonal matrices. 1786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 1796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param a matrix. 1806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param b matrix. 1816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param product will receive the product of \p a and \p b. 18223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 18323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b ) 18423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 18523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLint i; 18623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell for (i = 0; i < 3; i++) { 18723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); 18823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0); 18923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1); 19023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2); 19123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3; 19223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 19323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(3,0) = 0; 19423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(3,1) = 0; 19523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(3,2) = 0; 19623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell P(3,3) = 1; 19723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 19823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 19923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef A 20023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef B 20123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef P 20223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2036dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 20423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Multiply a matrix by an array of floats with known properties. 2056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure containing the left multiplication 2076dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * matrix, and that will receive the product result. 2086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m right multiplication matrix array. 2096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param flags flags of the matrix \p m. 2106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Joins both flags and marks the type and inverse as dirty. Calls matmul34() 2126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * if both matrices are 3D, or matmul4() otherwise. 21323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 21423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags ) 21523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 21623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE); 21723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 21823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) 21923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matmul34( mat->m, mat->m, m ); 22022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes else 22122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes matmul4( mat->m, mat->m, m ); 22223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 22323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 2256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix multiplication. 2266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dest destination matrix. 2286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param a left matrix. 2296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param b right matrix. 2306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Joins both flags and marks the type and inverse as dirty. Calls matmul34() 2326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * if both matrices are 3D, or matmul4() otherwise. 2336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 2346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 2356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b ) 2366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 2376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell dest->flags = (a->flags | 2386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell b->flags | 2396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_TYPE | 2406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_INVERSE); 24123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D)) 2436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell matmul34( dest->m, a->m, b->m ); 2446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell else 2456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell matmul4( dest->m, a->m, b->m ); 2466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 2476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 2496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix multiplication. 2506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param dest left and destination matrix. 2526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m right matrix array. 2536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Marks the matrix flags with general flag, and type and inverse dirty flags. 2556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calls matmul4() for the multiplication. 2566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 2576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 2586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m ) 2596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 2606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell dest->flags |= (MAT_FLAG_GENERAL | 2616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_TYPE | 262522ea4271804b75d90f9bc72b81bfd025bb137d0Brian Paul MAT_DIRTY_INVERSE | 263522ea4271804b75d90f9bc72b81bfd025bb137d0Brian Paul MAT_DIRTY_FLAGS); 2646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell matmul4( dest->m, dest->m, m ); 2666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 2676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 2696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 2726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix output */ 2736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 2746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 2756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 2766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Print a matrix array. 2776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix array. 2796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Called by _math_matrix_print() to print a matrix or its inverse. 2816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 28223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic void print_matrix_floats( const GLfloat m[16] ) 28323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 28423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell int i; 28523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell for (i=0;i<4;i++) { 2864e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] ); 28723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 28823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 28923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 2906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 2916dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Dumps the contents of a GLmatrix structure. 2926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 2936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m pointer to the GLmatrix structure. 2946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 29522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 29623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_print( const GLmatrix *m ) 29723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 29866d9ac5ac7896538d38f57950888a0184c933925Brian Paul GLfloat prod[16]; 29966d9ac5ac7896538d38f57950888a0184c933925Brian Paul 3004e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags); 30123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell print_matrix_floats(m->m); 3024e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul _mesa_debug(NULL, "Inverse: \n"); 30366d9ac5ac7896538d38f57950888a0184c933925Brian Paul print_matrix_floats(m->inv); 30466d9ac5ac7896538d38f57950888a0184c933925Brian Paul matmul4(prod, m->m, m->inv); 30566d9ac5ac7896538d38f57950888a0184c933925Brian Paul _mesa_debug(NULL, "Mat * Inverse:\n"); 30666d9ac5ac7896538d38f57950888a0184c933925Brian Paul print_matrix_floats(prod); 30723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 30823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 3096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 3106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 3116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 3126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 3136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * References an element of 4x4 matrix. 3146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix array. 3166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param c column of the desired element. 3176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param r row of the desired element. 3186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3196dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return value of the desired element. 3206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3216dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculate the linear storage index of the element and references it. 3226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 3236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell#define MAT(m,r,c) (m)[(c)*4+(r)] 32423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 32523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 3266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 3276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix inversion */ 3286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 32923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 3306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 3311e37d54d9d323482b39bf888c09c9857a379bb1cBrian Paul * Swaps the values of two floating point variables. 3326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Used by invert_matrix_general() to swap the row pointers. 3346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 33523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; } 33623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 3376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 33823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Compute inverse of 4x4 transformation matrix. 3396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 3416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 3426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 3446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \author 34623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Code contributed by Jacques Leroy jle@star.be 3476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 3486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculates the inverse matrix by performing the gaussian matrix reduction 3496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * with partial pivoting followed by back/substitution with the loops manually 3506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * unrolled. 35123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 35223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_general( GLmatrix *mat ) 35323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 35423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *m = mat->m; 35523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 35623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat wtmp[4][8]; 35723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m0, m1, m2, m3, s; 35823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *r0, *r1, *r2, *r3; 35922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 36023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3]; 36122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 36223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1), 36323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3), 36423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0, 36522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 36623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1), 36723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3), 36823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0, 36922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 37023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1), 37123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3), 37223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0, 37322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 37423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1), 37523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3), 37623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0; 37722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 37823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* choose pivot - or die */ 379b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r3[0])>FABSF(r2[0])) SWAP_ROWS(r3, r2); 380b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r2[0])>FABSF(r1[0])) SWAP_ROWS(r2, r1); 381b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r1[0])>FABSF(r0[0])) SWAP_ROWS(r1, r0); 38223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (0.0 == r0[0]) return GL_FALSE; 38322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 38423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* eliminate first variable */ 38523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0]; 38623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s; 38723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s; 38823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s; 38923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[4]; 39023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; } 39123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[5]; 39223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; } 39323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[6]; 39423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; } 39523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r0[7]; 39623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; } 39722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 39823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* choose pivot - or die */ 399b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r3[1])>FABSF(r2[1])) SWAP_ROWS(r3, r2); 400b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r2[1])>FABSF(r1[1])) SWAP_ROWS(r2, r1); 40123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (0.0 == r1[1]) return GL_FALSE; 40222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 40323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* eliminate second variable */ 40423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1]; 40523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2]; 40623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3]; 40723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; } 40823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; } 40923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; } 41023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; } 41122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 41223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* choose pivot - or die */ 413b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(r3[2])>FABSF(r2[2])) SWAP_ROWS(r3, r2); 41423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (0.0 == r2[2]) return GL_FALSE; 41522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 41623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* eliminate third variable */ 41723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m3 = r3[2]/r2[2]; 41823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4], 41923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], 42023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[7] -= m3 * r2[7]; 42122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 42223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* last check */ 42323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (0.0 == r3[3]) return GL_FALSE; 42422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 4257b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz s = 1.0F/r3[3]; /* now back substitute row 3 */ 42623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s; 42722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 42823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m2 = r2[3]; /* now back substitute row 2 */ 4297b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz s = 1.0F/r2[2]; 43023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2), 43123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2); 43223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m1 = r1[3]; 43323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1, 43423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1; 43523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m0 = r0[3]; 43623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0, 43723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0; 43822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 43923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m1 = r1[2]; /* now back substitute row 1 */ 4407b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz s = 1.0F/r1[1]; 44123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1), 44223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1); 44323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m0 = r0[2]; 44423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0, 44523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0; 44622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 44723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m0 = r0[1]; /* now back substitute row 0 */ 4487b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz s = 1.0F/r0[0]; 44923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0), 45023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0); 45122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 45223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5], 45323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7], 45423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5], 45523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7], 45623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5], 45723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7], 45823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5], 45922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; 46022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 46123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 46223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 46323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef SWAP_ROWS 46423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 4656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 4666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a general 3d transformation matrix. 4676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 4686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 4696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 4706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 4716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 4726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 4736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \author Adapted from graphics gems II. 4746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 4756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculates the inverse of the upper left by first calculating its 4766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * determinant and multiplying it to the symmetric adjust matrix of each 4776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * element. Finally deals with the translation part by transforming the 4786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * original translation vector using by the calculated submatrix inverse. 47922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes */ 48023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_3d_general( GLmatrix *mat ) 48123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 48223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 48323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 48423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat pos, neg, t; 48523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat det; 48623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 48723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Calculate the determinant of upper left 3x3 submatrix and 48822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * determine if the matrix is singular. 48923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 49023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell pos = neg = 0.0; 49123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2); 49223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 49323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 49423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2); 49523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 49623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 49723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2); 49823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 49923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 50023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2); 50123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 50223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 50323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2); 50423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 50523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 50623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2); 50723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (t >= 0.0) pos += t; else neg += t; 50823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 50923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell det = pos + neg; 51023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 51150db8129152f3d5ea8db13d55f82673d53bf1b8fBrian Paul if (FABSF(det) < 1e-25) 51223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 51322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 5147b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz det = 1.0F / det; 51523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det); 51623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det); 51723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det); 51823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det); 51923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det); 52023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det); 52123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det); 52223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det); 52323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det); 52423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 52523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Do the translation part */ 52623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + 52723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,0,1) + 52823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,0,2) ); 52923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + 53023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,1,1) + 53123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,1,2) ); 53223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + 53323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,2,1) + 53423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,2,2) ); 53522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 53623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 53723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 53823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 5396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 5406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a 3d transformation matrix. 5416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 5426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 5436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 5446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 5456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 5466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 5476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * If the matrix is not an angle preserving matrix then calls 5486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * invert_matrix_3d_general for the actual calculation. Otherwise calculates 5496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * the inverse matrix analyzing and inverting each of the scaling, rotation and 5506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * translation parts. 5516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 55223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_3d( GLmatrix *mat ) 55323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 55423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 55523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 55623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 55723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) { 55823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return invert_matrix_3d_general( mat ); 55923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 56022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 56123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_FLAG_UNIFORM_SCALE) { 56223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) + 56323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,0,1) * MAT(in,0,1) + 56423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,0,2) * MAT(in,0,2)); 56523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 56622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (scale == 0.0) 56723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 56823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 5697b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz scale = 1.0F / scale; 57023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 57123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Transpose and scale the 3 by 3 upper-left submatrix. */ 57223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,0) = scale * MAT(in,0,0); 57323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,0) = scale * MAT(in,0,1); 57423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,0) = scale * MAT(in,0,2); 57523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,1) = scale * MAT(in,1,0); 57623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,1) = scale * MAT(in,1,1); 57723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,1) = scale * MAT(in,1,2); 57823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,2) = scale * MAT(in,2,0); 57923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,2) = scale * MAT(in,2,1); 58023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = scale * MAT(in,2,2); 58123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 58223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if (mat->flags & MAT_FLAG_ROTATION) { 58323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Transpose the 3 by 3 upper-left submatrix. */ 58423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,0) = MAT(in,0,0); 58523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,0) = MAT(in,0,1); 58623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,0) = MAT(in,0,2); 58723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,1) = MAT(in,1,0); 58823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,1) = MAT(in,1,1); 58923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,1) = MAT(in,1,2); 59023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,2) = MAT(in,2,0); 59123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,2) = MAT(in,2,1); 59223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = MAT(in,2,2); 59323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 59423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 59523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* pure translation */ 596e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( out, Identity, sizeof(Identity) ); 59723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - MAT(in,0,3); 59823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - MAT(in,1,3); 59923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = - MAT(in,2,3); 60023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 60123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 60222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 60323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_FLAG_TRANSLATION) { 60423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Do the translation part */ 60523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + 60623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,0,1) + 60723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,0,2) ); 60823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + 60923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,1,1) + 61023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,1,2) ); 61123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + 61223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,1,3) * MAT(out,2,1) + 61323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(in,2,3) * MAT(out,2,2) ); 61423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 61523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 61623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0; 61723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 61822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 61923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 62023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 62123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 6226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 6236dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of an identity transformation matrix. 6246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 6266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 6276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return always GL_TRUE. 6296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Simply copies Identity into GLmatrix::inv. 6316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 63223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_identity( GLmatrix *mat ) 63323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 634e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->inv, Identity, sizeof(Identity) ); 63523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 63623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 63723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 6386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 6396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a no-rotation 3d transformation matrix. 6406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 6426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 6436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 6456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculates the 6476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 64823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_3d_no_rot( GLmatrix *mat ) 64923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 65023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 65123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 65223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 65322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 ) 65423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 65522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 656e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( out, Identity, 16 * sizeof(GLfloat) ); 6577b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,0,0) = 1.0F / MAT(in,0,0); 6587b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,1,1) = 1.0F / MAT(in,1,1); 6597b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,2,2) = 1.0F / MAT(in,2,2); 66023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 66123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_FLAG_TRANSLATION) { 66223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); 66323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); 66423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2)); 66523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 66623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 66723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 66823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 66923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 6706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 6716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a no-rotation 2d transformation matrix. 6726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 6746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 6756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 6776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 6786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calculates the inverse matrix by applying the inverse scaling and 6796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * translation to the identity matrix. 6806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 68123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_2d_no_rot( GLmatrix *mat ) 68223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 68323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 68423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 68523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 68622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0) 68723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 68822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 689e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( out, Identity, 16 * sizeof(GLfloat) ); 6907b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,0,0) = 1.0F / MAT(in,0,0); 6917b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,1,1) = 1.0F / MAT(in,1,1); 69223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 69323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_FLAG_TRANSLATION) { 69423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); 69523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); 69623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 69723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 69823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 69923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 70023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7014e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul#if 0 7024e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul/* broken */ 70323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean invert_matrix_perspective( GLmatrix *mat ) 70423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 70523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *in = mat->m; 70623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat *out = mat->inv; 70723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 70823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (MAT(in,2,3) == 0) 70923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 71023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 711e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( out, Identity, 16 * sizeof(GLfloat) ); 71223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7137b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,0,0) = 1.0F / MAT(in,0,0); 7147b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,1,1) = 1.0F / MAT(in,1,1); 71523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 71623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,0,3) = MAT(in,0,2); 71723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,1,3) = MAT(in,1,2); 71823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 71923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,2) = 0; 72023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,2,3) = -1; 72123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7227b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz MAT(out,3,2) = 1.0F / MAT(in,2,3); 72323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2); 72423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 72523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 72623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 7274e9676fb13f60ecdbc247b120031f18cd3febcb0Brian Paul#endif 72823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 7306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix inversion function pointer type. 7316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 73223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwelltypedef GLboolean (*inv_mat_func)( GLmatrix *mat ); 73323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 7356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Table of the matrix inversion functions according to the matrix type. 7366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 73723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic inv_mat_func inv_mat_tab[7] = { 73823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_general, 73923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_identity, 74023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_3d_no_rot, 741a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul#if 0 742a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul /* Don't use this function for now - it fails when the projection matrix 743a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul * is premultiplied by a translation (ala Chromium's tilesort SPU). 744a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul */ 74523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_perspective, 746a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul#else 747a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul invert_matrix_general, 748a68b8dfd76fa25b8e4ecaf1c6961a958e0fdfd3bBrian Paul#endif 74923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_3d, /* lazy! */ 75023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_2d_no_rot, 75123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell invert_matrix_3d 75223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell}; 75323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 7556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Compute inverse of a transformation matrix. 7566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 7576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat pointer to a GLmatrix structure. The matrix inverse will be 7586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * stored in the GLmatrix::inv attribute. 7596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 7606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 7616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 7626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Calls the matrix inversion function in inv_mat_tab corresponding to the 7636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag, 7646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * and copies the identity matrix into GLmatrix::inv. 7656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 76623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellstatic GLboolean matrix_invert( GLmatrix *mat ) 76723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 76823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (inv_mat_tab[mat->type](mat)) { 76923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags &= ~MAT_FLAG_SINGULAR; 77023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_TRUE; 77123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } else { 77223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_SINGULAR; 773e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->inv, Identity, sizeof(Identity) ); 77423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell return GL_FALSE; 77522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes } 77623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 77723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 77923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 78023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 7826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix generation */ 7836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 78423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 7856dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 78623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell * Generate a 4x4 transformation matrix from glRotate parameters, and 7876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * post-multiply the input matrix by it. 7886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 7896dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \author 7906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * This function was contributed by Erich Boleyn (erich@uruk.org). 7916dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Optimizations contributed by Rudolf Opalla (rudi@khm.de). 79223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 79322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 79422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_math_matrix_rotate( GLmatrix *mat, 79523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat angle, GLfloat x, GLfloat y, GLfloat z ) 79623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 7974991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c; 79823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m[16]; 7994991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul GLboolean optimized; 80022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 801165694ad65374ff4330bd80acb398fe0428ba2e6Eric Anholt s = (GLfloat) sin( angle * DEG2RAD ); 802165694ad65374ff4330bd80acb398fe0428ba2e6Eric Anholt c = (GLfloat) cos( angle * DEG2RAD ); 80323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 804e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy(m, Identity, sizeof(GLfloat)*16); 8054991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul optimized = GL_FALSE; 80623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 80723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define M(row,col) m[col*4+row] 80823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 8094991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (x == 0.0F) { 8104991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (y == 0.0F) { 8114991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (z != 0.0F) { 8124991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul optimized = GL_TRUE; 8134991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* rotate only around z-axis */ 8144991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,0) = c; 8154991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,1) = c; 8164991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (z < 0.0F) { 8174991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,1) = s; 8184991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,0) = -s; 8194991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8204991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else { 8214991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,1) = -s; 8224991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,0) = s; 8234991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8244991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8254991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8264991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else if (z == 0.0F) { 8274991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul optimized = GL_TRUE; 8284991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* rotate only around y-axis */ 8294991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,0) = c; 8304991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,2) = c; 8314991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (y < 0.0F) { 8324991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,2) = -s; 8334991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,0) = s; 8344991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8354991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else { 8364991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,2) = s; 8374991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,0) = -s; 8384991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8394991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8404991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8414991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else if (y == 0.0F) { 8424991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (z == 0.0F) { 8434991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul optimized = GL_TRUE; 8444991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* rotate only around x-axis */ 8454991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,1) = c; 8464991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,2) = c; 8471e091f48f0434e8fb9713fbebc9d74ad68a75e34Brian Paul if (x < 0.0F) { 8484991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,2) = s; 8494991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,1) = -s; 8504991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8514991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul else { 8524991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,2) = -s; 8534991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,1) = s; 8544991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8554991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8564991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 85723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 8584991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (!optimized) { 85927558a160a9fe91745728d7626995cd88f8fe339Brian Paul const GLfloat mag = SQRTF(x * x + y * y + z * z); 8604991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 8614991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul if (mag <= 1.0e-4) { 8624991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* no rotation, leave mat as-is */ 8634991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul return; 8644991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 8654991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 8664991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul x /= mag; 8674991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul y /= mag; 8684991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul z /= mag; 8694991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 8704991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 8714991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* 8724991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Arbitrary axis rotation matrix. 8734991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 8744991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied 8754991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation 8764991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * (which is about the X-axis), and the two composite transforms 8774991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary 8784991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * from the arbitrary axis to the X-axis then back. They are 8794991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * all elementary rotations. 8804991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 8814991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Rz' is a rotation about the Z-axis, to bring the axis vector 8824991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * into the x-z plane. Then Ry' is applied, rotating about the 8834991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Y-axis to bring the axis vector parallel with the X-axis. The 8844991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * rotation about the X-axis is then performed. Ry and Rz are 8854991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * simply the respective inverse transforms to bring the arbitrary 886fab1f07d6ad01463897ae792f4b33738afb07369Jeff Smith * axis back to its original orientation. The first transforms 8874991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Rz' and Ry' are considered inverses, since the data from the 8884991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * arbitrary axis gives you info on how to get to it, not how 8894991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * to get away from it, and an inverse must be applied. 8904991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 8914991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * The basic calculation used is to recognize that the arbitrary 8924991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * axis vector (x, y, z), since it is of unit length, actually 8934991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * represents the sines and cosines of the angles to rotate the 8944991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * X-axis to the same orientation, with theta being the angle about 8954991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Z and phi the angle about Y (in the order described above) 8964991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * as follows: 8974991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 8984991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * cos ( theta ) = x / sqrt ( 1 - z^2 ) 8994991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * sin ( theta ) = y / sqrt ( 1 - z^2 ) 9004991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9014991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * cos ( phi ) = sqrt ( 1 - z^2 ) 9024991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * sin ( phi ) = z 9034991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9044991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Note that cos ( phi ) can further be inserted to the above 9054991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * formulas: 9064991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9074991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * cos ( theta ) = x / cos ( phi ) 9084991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * sin ( theta ) = y / sin ( phi ) 9094991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9104991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * ...etc. Because of those relations and the standard trigonometric 9114991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * relations, it is pssible to reduce the transforms down to what 9124991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * is used below. It may be that any primary axis chosen will give the 9134991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * same results (modulo a sign convention) using thie method. 9144991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * 9154991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * Particularly nice is to notice that all divisions that might 9164991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * have caused trouble when parallel to certain planes or 9174991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * axis go away with care paid to reducing the expressions. 9184991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * After checking, it does perform correctly under all cases, since 9194991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * in all the cases of division where the denominator would have 9204991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * been zero, the numerator would have been zero as well, giving 9214991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul * the expected result. 9224991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul */ 9234991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 9244991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul xx = x * x; 9254991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul yy = y * y; 9264991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul zz = z * z; 9274991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul xy = x * y; 9284991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul yz = y * z; 9294991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul zx = z * x; 9304991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul xs = x * s; 9314991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul ys = y * s; 9324991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul zs = z * s; 9334991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul one_c = 1.0F - c; 9344991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 9354991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul /* We already hold the identity-matrix so we can skip some statements */ 9364991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,0) = (one_c * xx) + c; 9374991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,1) = (one_c * xy) - zs; 9384991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(0,2) = (one_c * zx) + ys; 9394991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul/* M(0,3) = 0.0F; */ 9404991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 9414991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,0) = (one_c * xy) + zs; 9424991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,1) = (one_c * yy) + c; 9434991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(1,2) = (one_c * yz) - xs; 9444991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul/* M(1,3) = 0.0F; */ 9454991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul 9464991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,0) = (one_c * zx) - ys; 9474991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,1) = (one_c * yz) + xs; 9484991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(2,2) = (one_c * zz) + c; 9494991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul/* M(2,3) = 0.0F; */ 95023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 9514991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul/* 9524991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(3,0) = 0.0F; 9534991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(3,1) = 0.0F; 9544991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(3,2) = 0.0F; 9554991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul M(3,3) = 1.0F; 9564991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul*/ 9574991d0f9f39b3fca8458af77ad0a060e76eb5594Brian Paul } 95823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef M 95923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 96023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_multf( mat, m, MAT_FLAG_ROTATION ); 96123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 96223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 9636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 9646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Apply a perspective projection matrix. 9656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 9666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix to apply the projection. 9676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param left left clipping plane coordinate. 9686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param right right clipping plane coordinate. 9696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param bottom bottom clipping plane coordinate. 9706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param top top clipping plane coordinate. 9716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param nearval distance to the near clipping plane. 9726dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param farval distance to the far clipping plane. 9736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 9746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Creates the projection matrix and multiplies it with \p mat, marking the 9756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * MAT_FLAG_PERSPECTIVE flag. 9766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 97723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellvoid 97822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_math_matrix_frustum( GLmatrix *mat, 979d8bc5a9eba720ffb6a503d32715f895dbdad7197Brian Paul GLfloat left, GLfloat right, 98022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes GLfloat bottom, GLfloat top, 981d8bc5a9eba720ffb6a503d32715f895dbdad7197Brian Paul GLfloat nearval, GLfloat farval ) 98223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 98323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat x, y, a, b, c, d; 98423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m[16]; 98523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 9867b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz x = (2.0F*nearval) / (right-left); 9877b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz y = (2.0F*nearval) / (top-bottom); 98823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell a = (right+left) / (right-left); 98923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell b = (top+bottom) / (top-bottom); 99023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell c = -(farval+nearval) / ( farval-nearval); 9917b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */ 99223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 99323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define M(row,col) m[col*4+row] 99423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F; 99523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F; 99623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d; 99723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F; 99823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef M 99923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 100023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE ); 100123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 100223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 10036dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 10046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Apply an orthographic projection matrix. 10056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix to apply the projection. 10076dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param left left clipping plane coordinate. 10086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param right right clipping plane coordinate. 10096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param bottom bottom clipping plane coordinate. 10106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param top top clipping plane coordinate. 10116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param nearval distance to the near clipping plane. 10126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param farval distance to the far clipping plane. 10136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Creates the projection matrix and multiplies it with \p mat, marking the 10156dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags. 10166dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 101723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellvoid 101822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_math_matrix_ortho( GLmatrix *mat, 101923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat left, GLfloat right, 102022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes GLfloat bottom, GLfloat top, 102123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat nearval, GLfloat farval ) 102223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 102323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m[16]; 102423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 102523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define M(row,col) m[col*4+row] 1026e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(0,0) = 2.0F / (right-left); 1027e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(0,1) = 0.0F; 1028e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(0,2) = 0.0F; 1029e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(0,3) = -(right+left) / (right-left); 1030e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul 1031e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(1,0) = 0.0F; 1032e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(1,1) = 2.0F / (top-bottom); 1033e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(1,2) = 0.0F; 1034e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(1,3) = -(top+bottom) / (top-bottom); 1035e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul 1036e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(2,0) = 0.0F; 1037e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(2,1) = 0.0F; 1038e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(2,2) = -2.0F / (farval-nearval); 1039e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(2,3) = -(farval+nearval) / (farval-nearval); 1040e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul 1041e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(3,0) = 0.0F; 1042e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(3,1) = 0.0F; 1043e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(3,2) = 0.0F; 1044e2e9dc221d4f091b26713169dabfd43a3d8a635cBrian Paul M(3,3) = 1.0F; 104523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#undef M 104623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 104723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION)); 104823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 104923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 10506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 10516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Multiply a matrix with a general scaling matrix. 10526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 10546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param x x axis scale factor. 10556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param y y axis scale factor. 10566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param z z axis scale factor. 10576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Multiplies in-place the elements of \p mat by the scale factors. Checks if 10596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE 10606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and 10616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * MAT_DIRTY_INVERSE dirty flags. 10626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 10636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 10646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ) 10656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 10666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell GLfloat *m = mat->m; 10676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[0] *= x; m[4] *= y; m[8] *= z; 10686dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[1] *= x; m[5] *= y; m[9] *= z; 10696dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[2] *= x; m[6] *= y; m[10] *= z; 10706dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[3] *= x; m[7] *= y; m[11] *= z; 10716dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 1072b3aefd1cfb6aacd1695c52911dd39da50d893eceBrian Paul if (FABSF(x - y) < 1e-8 && FABSF(x - z) < 1e-8) 10736dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags |= MAT_FLAG_UNIFORM_SCALE; 10746dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell else 10756dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_SCALE; 10766dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 10776dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags |= (MAT_DIRTY_TYPE | 10786dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_INVERSE); 10796dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 10806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 10816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 10826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Multiply a matrix with a translation matrix. 10836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10846dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 10856dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param x translation vector x coordinate. 10866dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param y translation vector y coordinate. 10876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param z translation vector z coordinate. 10886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 10896dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Adds the translation coordinates to the elements of \p mat in-place. Marks 10906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE 10916dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * dirty flags. 10926dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 10936dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 10946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ) 10956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 10966dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell GLfloat *m = mat->m; 10976dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[12] = m[0] * x + m[4] * y + m[8] * z + m[12]; 10986dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[13] = m[1] * x + m[5] * y + m[9] * z + m[13]; 10996dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[14] = m[2] * x + m[6] * y + m[10] * z + m[14]; 11006dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell m[15] = m[3] * x + m[7] * y + m[11] * z + m[15]; 11016dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11026dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags |= (MAT_FLAG_TRANSLATION | 11036dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_TYPE | 11046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_INVERSE); 11056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 11066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 1107049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1108049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 1109049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Set matrix to do viewport and depthrange mapping. 1110049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Transforms Normalized Device Coords to window/Z values. 1111049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 1112049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paulvoid 1113049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_viewport(GLmatrix *m, GLint x, GLint y, GLint width, GLint height, 1114049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul GLfloat zNear, GLfloat zFar, GLfloat depthMax) 1115049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1116049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_SX] = (GLfloat) width / 2.0F; 1117049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_TX] = m->m[MAT_SX] + x; 1118049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_SY] = (GLfloat) height / 2.0F; 1119049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_TY] = m->m[MAT_SY] + y; 1120049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_SZ] = depthMax * ((zFar - zNear) / 2.0F); 1121049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->m[MAT_TZ] = depthMax * ((zFar - zNear) / 2.0F + zNear); 1122049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION; 1123049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul m->type = MATRIX_3D_NO_ROT; 1124049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1125049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1126049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 11276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 11286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Set a matrix to the identity matrix. 11296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 11306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 11316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 11326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL. 11336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Sets the matrix type to identity, and clear the dirty flags. 11346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 11356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwellvoid 11366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell_math_matrix_set_identity( GLmatrix *mat ) 11376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell{ 1138e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->m, Identity, 16*sizeof(GLfloat) ); 113966d9ac5ac7896538d38f57950888a0184c933925Brian Paul memcpy( mat->inv, Identity, 16*sizeof(GLfloat) ); 11406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11416dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->type = MATRIX_IDENTITY; 11426dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell mat->flags &= ~(MAT_DIRTY_FLAGS| 11436dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_TYPE| 11446dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell MAT_DIRTY_INVERSE); 11456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell} 11466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 11486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 11506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 11516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix analysis */ 11526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 115323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 115423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define ZERO(x) (1<<x) 115523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define ONE(x) (1<<(x+16)) 115623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 115723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14)) 115823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5)) 115923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 116023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\ 116123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\ 116223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 116323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 116423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 116523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \ 116623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(1) | ZERO(9) | \ 116723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 116823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 116923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 117023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_2D ( ZERO(8) | \ 117123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(9) | \ 117223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 117323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 117423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 117523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 117623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \ 117723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(1) | ZERO(9) | \ 117823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | \ 117923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 118023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 118123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_3D ( \ 118223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell \ 118323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell \ 118423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 118523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 118623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 118723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\ 118823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(1) | ZERO(13) |\ 118923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(2) | ZERO(6) | \ 119023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell ZERO(3) | ZERO(7) | ZERO(15) ) 119123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 119223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define SQ(x) ((x)*(x)) 119322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 11946dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 11956dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Determine type and flags from scratch. 11966dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 11976dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 11986dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 11996dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * This is expensive enough to only want to do it once. 120023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 1201ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwellstatic void analyse_from_scratch( GLmatrix *mat ) 120223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 120323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *m = mat->m; 120423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLuint mask = 0; 120523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLuint i; 120623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 120723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell for (i = 0 ; i < 16 ; i++) { 120823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[i] == 0.0) mask |= (1<<i); 120923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 121022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 121123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[0] == 1.0F) mask |= (1<<16); 121223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[5] == 1.0F) mask |= (1<<21); 121323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[10] == 1.0F) mask |= (1<<26); 121423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (m[15] == 1.0F) mask |= (1<<31); 121523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 121623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags &= ~MAT_FLAGS_GEOMETRY; 121723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 121822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes /* Check for translation - no-one really cares 121923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 122022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if ((mask & MASK_NO_TRX) != MASK_NO_TRX) 122122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes mat->flags |= MAT_FLAG_TRANSLATION; 122223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 122323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Do the real work 122423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 1225b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul if (mask == (GLuint) MASK_IDENTITY) { 122623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_IDENTITY; 122723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 1228b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) { 122923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_2D_NO_ROT; 123022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes 123123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE) 12322dab997cb9ddbe47ff414b74679fb99346bb9a06Brian Paul mat->flags |= MAT_FLAG_GENERAL_SCALE; 123323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 1234b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul else if ((mask & MASK_2D) == (GLuint) MASK_2D) { 123523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat mm = DOT2(m, m); 123623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat m4m4 = DOT2(m+4,m+4); 123723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat mm4 = DOT2(m,m+4); 123823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 123923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_2D; 124023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 124123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for scale */ 124223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(mm-1) > SQ(1e-6) || 124322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes SQ(m4m4-1) > SQ(1e-6)) 124423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_SCALE; 124523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 124623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for rotation */ 124723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(mm4) > SQ(1e-6)) 124823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_3D; 124923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else 125023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_ROTATION; 125123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 125223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 1253b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) { 125423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_3D_NO_ROT; 125523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 125623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for scale */ 125722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (SQ(m[0]-m[5]) < SQ(1e-6) && 125823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell SQ(m[0]-m[10]) < SQ(1e-6)) { 125923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(m[0]-1.0) > SQ(1e-6)) { 126023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_UNIFORM_SCALE; 126123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 126223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 126323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 126423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_SCALE; 126523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 126623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 1267b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul else if ((mask & MASK_3D) == (GLuint) MASK_3D) { 126823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat c1 = DOT3(m,m); 126923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat c2 = DOT3(m+4,m+4); 127023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat c3 = DOT3(m+8,m+8); 127123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat d1 = DOT3(m, m+4); 127223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell GLfloat cp[3]; 127323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 127423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_3D; 127523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 127623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for scale */ 127723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) { 127823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(c1-1.0) > SQ(1e-6)) 127923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_UNIFORM_SCALE; 128023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* else no scale at all */ 128123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 128223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 128323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_SCALE; 128423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 128523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 128623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell /* Check for rotation */ 128723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (SQ(d1) < SQ(1e-6)) { 128823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell CROSS3( cp, m, m+4 ); 128923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell SUB_3V( cp, cp, (m+8) ); 129022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (LEN_SQUARED_3FV(cp) < SQ(1e-6)) 129123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_ROTATION; 129223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else 129323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_3D; 129423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 129523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 129623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */ 129723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 129823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 129923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) { 130023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_PERSPECTIVE; 130123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL; 130223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 130323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 130423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_GENERAL; 130523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags |= MAT_FLAG_GENERAL; 130623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 130723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 130823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 13096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 13106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Analyze a matrix given that its flags are accurate. 13116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 13126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * This is the more common operation, hopefully. 131323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell */ 1314ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwellstatic void analyse_from_flags( GLmatrix *mat ) 131523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 131623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell const GLfloat *m = mat->m; 131723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 131823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (TEST_MAT_FLAGS(mat, 0)) { 131923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_IDENTITY; 132023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 132123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION | 132223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT_FLAG_UNIFORM_SCALE | 132323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell MAT_FLAG_GENERAL_SCALE))) { 132423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if ( m[10]==1.0F && m[14]==0.0F ) { 132523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_2D_NO_ROT; 132623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 132723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 132823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_3D_NO_ROT; 132923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 133023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 133123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) { 133222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if ( m[ 8]==0.0F 133323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[ 9]==0.0F 133423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) { 133523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_2D; 133623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 133723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 133823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_3D; 133923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 134023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 134123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else if ( m[4]==0.0F && m[12]==0.0F 134223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[1]==0.0F && m[13]==0.0F 134323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[2]==0.0F && m[6]==0.0F 134423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) { 134523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_PERSPECTIVE; 134623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 134723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else { 134823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->type = MATRIX_GENERAL; 134923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 135023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 135123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 13526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 13536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Analyze and update a matrix. 13546dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 13556dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 13566dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 13576dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * If the matrix type is dirty then calls either analyse_from_scratch() or 13586dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * analyse_from_flags() to determine its type, according to whether the flags 13596dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * are dirty or not, respectively. If the matrix has an inverse and it's dirty 13606dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * then calls matrix_invert(). Finally clears the dirty flags. 13616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 136222144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 136322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes_math_matrix_analyse( GLmatrix *mat ) 136423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 136523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->flags & MAT_DIRTY_TYPE) { 136622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes if (mat->flags & MAT_DIRTY_FLAGS) 1367ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwell analyse_from_scratch( mat ); 136823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell else 1369ad2ac216fa0cbebc36530bf9e5256e902710b892Keith Whitwell analyse_from_flags( mat ); 137023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 137123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 137223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) { 137323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell matrix_invert( mat ); 1374ce461ffc5aa2ea6941d6722e8ed473cda8c17833Brian Paul mat->flags &= ~MAT_DIRTY_INVERSE; 137523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 137623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1377ce461ffc5aa2ea6941d6722e8ed473cda8c17833Brian Paul mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE); 137823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 137923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 13806dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 13816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 138223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 1383049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 1384049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Test if the given matrix preserves vector lengths. 1385049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 1386049e320f46f3a3daaa36ef67cc680dc504c124d5Brian PaulGLboolean 1387049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_is_length_preserving( const GLmatrix *m ) 1388049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1389049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING); 1390049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1391049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1392049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1393049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul/** 1394049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * Test if the given matrix does any rotation. 1395049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul * (or perhaps if the upper-left 3x3 is non-identity) 1396049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul */ 1397049e320f46f3a3daaa36ef67cc680dc504c124d5Brian PaulGLboolean 1398049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_has_rotation( const GLmatrix *m ) 1399049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1400049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul if (m->flags & (MAT_FLAG_GENERAL | 1401049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_ROTATION | 1402049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_GENERAL_3D | 1403049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul MAT_FLAG_PERSPECTIVE)) 1404049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return GL_TRUE; 1405049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul else 1406049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return GL_FALSE; 1407049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1408049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1409049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1410049e320f46f3a3daaa36ef67cc680dc504c124d5Brian PaulGLboolean 1411049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_is_general_scale( const GLmatrix *m ) 1412049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1413049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE; 1414049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1415049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1416049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1417049e320f46f3a3daaa36ef67cc680dc504c124d5Brian PaulGLboolean 1418049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul_math_matrix_is_dirty( const GLmatrix *m ) 1419049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul{ 1420049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE; 1421049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul} 1422049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 1423049e320f46f3a3daaa36ef67cc680dc504c124d5Brian Paul 14246dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 14256dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix setup */ 14266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 14276dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 14286dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 14296dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Copy a matrix. 14306dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14316dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param to destination matrix. 14326dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param from source matrix. 14336dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14346dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Copies all fields in GLmatrix, creating an inverse array if necessary. 14356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 143622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 143723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_copy( GLmatrix *to, const GLmatrix *from ) 143823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 1439e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( to->m, from->m, sizeof(Identity) ); 1440b51be8786f681210ed865c46065770ba91bc7bcbBrian Paul memcpy(to->inv, from->inv, 16 * sizeof(GLfloat)); 144123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to->flags = from->flags; 144223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to->type = from->type; 144323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 144423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14456dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 14466dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Loads a matrix array into GLmatrix. 14476dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14486dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix array. 14496dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param mat matrix. 14506dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14516dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY 14526dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * flags. 14536dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 145422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 145523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_loadf( GLmatrix *mat, const GLfloat *m ) 145623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 1457e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( mat->m, m, 16*sizeof(GLfloat) ); 145823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY); 145923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 146023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14616dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 14626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix constructor. 14636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix. 14656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Initialize the GLmatrix fields. 14676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 146822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 146923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_ctr( GLmatrix *m ) 147023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 147199ae9e8d7d57ae37629754edd5b1e3716611827fKristian Høgsberg m->m = (GLfloat *) _mesa_align_malloc( 16 * sizeof(GLfloat), 16 ); 147230f51ae067379c2b3573c06b707d25a9704df7beBrian Paul if (m->m) 1473e197de56cdb86835f1437688a9161cd909792d80Brian Paul memcpy( m->m, Identity, sizeof(Identity) ); 147466d9ac5ac7896538d38f57950888a0184c933925Brian Paul m->inv = (GLfloat *) _mesa_align_malloc( 16 * sizeof(GLfloat), 16 ); 147566d9ac5ac7896538d38f57950888a0184c933925Brian Paul if (m->inv) 147666d9ac5ac7896538d38f57950888a0184c933925Brian Paul memcpy( m->inv, Identity, sizeof(Identity) ); 147723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m->type = MATRIX_IDENTITY; 147823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell m->flags = 0; 147923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 148023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 14816dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 14826dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Matrix destructor. 14836dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14846dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param m matrix. 14856dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 14866dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Frees the data in a GLmatrix. 14876dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 148822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 148923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_matrix_dtr( GLmatrix *m ) 149023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 149130f51ae067379c2b3573c06b707d25a9704df7beBrian Paul if (m->m) { 149299ae9e8d7d57ae37629754edd5b1e3716611827fKristian Høgsberg _mesa_align_free( m->m ); 149330f51ae067379c2b3573c06b707d25a9704df7beBrian Paul m->m = NULL; 149423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 149530f51ae067379c2b3573c06b707d25a9704df7beBrian Paul if (m->inv) { 149699ae9e8d7d57ae37629754edd5b1e3716611827fKristian Høgsberg _mesa_align_free( m->inv ); 149730f51ae067379c2b3573c06b707d25a9704df7beBrian Paul m->inv = NULL; 149823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell } 149923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 150023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15016dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 150223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 150323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15046dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/**********************************************************************/ 15056dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** \name Matrix transpose */ 15066dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@{*/ 150723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15086dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 15096dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Transpose a GLfloat matrix. 15106dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 15116dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param to destination array. 15126dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param from source array. 15136dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 151422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 151523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_transposef( GLfloat to[16], const GLfloat from[16] ) 151623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 151723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[0] = from[0]; 151823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[1] = from[4]; 151923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[2] = from[8]; 152023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[3] = from[12]; 152123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[4] = from[1]; 152223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[5] = from[5]; 152323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[6] = from[9]; 152423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[7] = from[13]; 152523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[8] = from[2]; 152623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[9] = from[6]; 152723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[10] = from[10]; 152823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[11] = from[14]; 152923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[12] = from[3]; 153023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[13] = from[7]; 153123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[14] = from[11]; 153223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[15] = from[15]; 153323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 153423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15356dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 15366dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Transpose a GLdouble matrix. 15376dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 15386dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param to destination array. 15396dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param from source array. 15406dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 154122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 154223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_transposed( GLdouble to[16], const GLdouble from[16] ) 154323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 154423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[0] = from[0]; 154523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[1] = from[4]; 154623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[2] = from[8]; 154723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[3] = from[12]; 154823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[4] = from[1]; 154923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[5] = from[5]; 155023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[6] = from[9]; 155123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[7] = from[13]; 155223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[8] = from[2]; 155323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[9] = from[6]; 155423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[10] = from[10]; 155523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[11] = from[14]; 155623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[12] = from[3]; 155723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[13] = from[7]; 155823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[14] = from[11]; 155923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell to[15] = from[15]; 156023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 156123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell 15626dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/** 15636dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * Transpose a GLdouble matrix and convert to GLfloat. 15646dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * 15656dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param to destination array. 15666dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell * \param from source array. 15676dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell */ 156822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughesvoid 156923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell_math_transposefd( GLfloat to[16], const GLdouble from[16] ) 157023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{ 15717b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[0] = (GLfloat) from[0]; 15727b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[1] = (GLfloat) from[4]; 15737b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[2] = (GLfloat) from[8]; 15747b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[3] = (GLfloat) from[12]; 15757b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[4] = (GLfloat) from[1]; 15767b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[5] = (GLfloat) from[5]; 15777b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[6] = (GLfloat) from[9]; 15787b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[7] = (GLfloat) from[13]; 15797b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[8] = (GLfloat) from[2]; 15807b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[9] = (GLfloat) from[6]; 15817b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[10] = (GLfloat) from[10]; 15827b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[11] = (GLfloat) from[14]; 15837b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[12] = (GLfloat) from[3]; 15847b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[13] = (GLfloat) from[7]; 15857b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[14] = (GLfloat) from[11]; 15867b9fe820a3fba3849864682fbb1cb512362934abKarl Schultz to[15] = (GLfloat) from[15]; 158723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell} 15886dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 15896dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell/*@}*/ 15906dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell 1591987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul 1592987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul/** 1593987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This 1594987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * function is used for transforming clipping plane equations and spotlight 1595987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * directions. 1596987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * Mathematically, u = v * m. 1597987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * Input: v - input vector 1598987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * m - transformation matrix 1599987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul * Output: u - transformed vector 1600987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul */ 1601987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paulvoid 1602987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul_mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] ) 1603987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul{ 1604987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul const GLfloat v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; 1605987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul#define M(row,col) m[row + col*4] 1606987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0); 1607987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1); 1608987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2); 1609987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3); 1610987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul#undef M 1611987aedd7dc75c095a96cb20b21bbad2f71857776Brian Paul} 1612