1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Mesa 3-D graphics library 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 12bf215546Sopenharmony_ci * 13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included 14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci/** 27bf215546Sopenharmony_ci * \file m_matrix.c 28bf215546Sopenharmony_ci * Matrix operations. 29bf215546Sopenharmony_ci * 30bf215546Sopenharmony_ci * \note 31bf215546Sopenharmony_ci * -# 4x4 transformation matrices are stored in memory in column major order. 32bf215546Sopenharmony_ci * -# Points/vertices are to be thought of as column vectors. 33bf215546Sopenharmony_ci * -# Transformation of a point p by a matrix M is: p' = M * p 34bf215546Sopenharmony_ci */ 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci#include <stddef.h> 37bf215546Sopenharmony_ci#include <math.h> 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci#include "main/errors.h" 40bf215546Sopenharmony_ci#include "main/glheader.h" 41bf215546Sopenharmony_ci#include "main/macros.h" 42bf215546Sopenharmony_ci#define MATH_ASM_PTR_SIZE sizeof(void *) 43bf215546Sopenharmony_ci#include "math/m_vector_asm.h" 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci#include "m_matrix.h" 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci#include "util/u_memory.h" 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci/** 51bf215546Sopenharmony_ci * \defgroup MatFlags MAT_FLAG_XXX-flags 52bf215546Sopenharmony_ci * 53bf215546Sopenharmony_ci * Bitmasks to indicate different kinds of 4x4 matrices in GLmatrix::flags 54bf215546Sopenharmony_ci */ 55bf215546Sopenharmony_ci/*@{*/ 56bf215546Sopenharmony_ci#define MAT_FLAG_IDENTITY 0 /**< is an identity matrix flag. 57bf215546Sopenharmony_ci * (Not actually used - the identity 58bf215546Sopenharmony_ci * matrix is identified by the absence 59bf215546Sopenharmony_ci * of all other flags.) 60bf215546Sopenharmony_ci */ 61bf215546Sopenharmony_ci#define MAT_FLAG_GENERAL 0x1 /**< is a general matrix flag */ 62bf215546Sopenharmony_ci#define MAT_FLAG_ROTATION 0x2 /**< is a rotation matrix flag */ 63bf215546Sopenharmony_ci#define MAT_FLAG_TRANSLATION 0x4 /**< is a translation matrix flag */ 64bf215546Sopenharmony_ci#define MAT_FLAG_UNIFORM_SCALE 0x8 /**< is an uniform scaling matrix flag */ 65bf215546Sopenharmony_ci#define MAT_FLAG_GENERAL_SCALE 0x10 /**< is a general scaling matrix flag */ 66bf215546Sopenharmony_ci#define MAT_FLAG_GENERAL_3D 0x20 /**< general 3D matrix flag */ 67bf215546Sopenharmony_ci#define MAT_FLAG_PERSPECTIVE 0x40 /**< is a perspective proj matrix flag */ 68bf215546Sopenharmony_ci#define MAT_FLAG_SINGULAR 0x80 /**< is a singular matrix flag */ 69bf215546Sopenharmony_ci#define MAT_DIRTY_TYPE 0x100 /**< matrix type is dirty */ 70bf215546Sopenharmony_ci#define MAT_DIRTY_FLAGS 0x200 /**< matrix flags are dirty */ 71bf215546Sopenharmony_ci#define MAT_DIRTY_INVERSE 0x400 /**< matrix inverse is dirty */ 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci/** angle preserving matrix flags mask */ 74bf215546Sopenharmony_ci#define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \ 75bf215546Sopenharmony_ci MAT_FLAG_TRANSLATION | \ 76bf215546Sopenharmony_ci MAT_FLAG_UNIFORM_SCALE) 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci/** geometry related matrix flags mask */ 79bf215546Sopenharmony_ci#define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \ 80bf215546Sopenharmony_ci MAT_FLAG_ROTATION | \ 81bf215546Sopenharmony_ci MAT_FLAG_TRANSLATION | \ 82bf215546Sopenharmony_ci MAT_FLAG_UNIFORM_SCALE | \ 83bf215546Sopenharmony_ci MAT_FLAG_GENERAL_SCALE | \ 84bf215546Sopenharmony_ci MAT_FLAG_GENERAL_3D | \ 85bf215546Sopenharmony_ci MAT_FLAG_PERSPECTIVE | \ 86bf215546Sopenharmony_ci MAT_FLAG_SINGULAR) 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci/** length preserving matrix flags mask */ 89bf215546Sopenharmony_ci#define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \ 90bf215546Sopenharmony_ci MAT_FLAG_TRANSLATION) 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci/** 3D (non-perspective) matrix flags mask */ 94bf215546Sopenharmony_ci#define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \ 95bf215546Sopenharmony_ci MAT_FLAG_TRANSLATION | \ 96bf215546Sopenharmony_ci MAT_FLAG_UNIFORM_SCALE | \ 97bf215546Sopenharmony_ci MAT_FLAG_GENERAL_SCALE | \ 98bf215546Sopenharmony_ci MAT_FLAG_GENERAL_3D) 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci/** dirty matrix flags mask */ 101bf215546Sopenharmony_ci#define MAT_DIRTY (MAT_DIRTY_TYPE | \ 102bf215546Sopenharmony_ci MAT_DIRTY_FLAGS | \ 103bf215546Sopenharmony_ci MAT_DIRTY_INVERSE) 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci/*@}*/ 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci/** 109bf215546Sopenharmony_ci * Test geometry related matrix flags. 110bf215546Sopenharmony_ci * 111bf215546Sopenharmony_ci * \param mat a pointer to a GLmatrix structure. 112bf215546Sopenharmony_ci * \param a flags mask. 113bf215546Sopenharmony_ci * 114bf215546Sopenharmony_ci * \returns non-zero if all geometry related matrix flags are contained within 115bf215546Sopenharmony_ci * the mask, or zero otherwise. 116bf215546Sopenharmony_ci */ 117bf215546Sopenharmony_ci#define TEST_MAT_FLAGS(mat, a) \ 118bf215546Sopenharmony_ci ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0) 119bf215546Sopenharmony_ci 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci/** 123bf215546Sopenharmony_ci * Names of the corresponding GLmatrixtype values. 124bf215546Sopenharmony_ci */ 125bf215546Sopenharmony_cistatic const char *types[] = { 126bf215546Sopenharmony_ci "MATRIX_GENERAL", 127bf215546Sopenharmony_ci "MATRIX_IDENTITY", 128bf215546Sopenharmony_ci "MATRIX_3D_NO_ROT", 129bf215546Sopenharmony_ci "MATRIX_PERSPECTIVE", 130bf215546Sopenharmony_ci "MATRIX_2D", 131bf215546Sopenharmony_ci "MATRIX_2D_NO_ROT", 132bf215546Sopenharmony_ci "MATRIX_3D" 133bf215546Sopenharmony_ci}; 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci/** 137bf215546Sopenharmony_ci * Identity matrix. 138bf215546Sopenharmony_ci */ 139bf215546Sopenharmony_cistatic const GLfloat Identity[16] = { 140bf215546Sopenharmony_ci 1.0, 0.0, 0.0, 0.0, 141bf215546Sopenharmony_ci 0.0, 1.0, 0.0, 0.0, 142bf215546Sopenharmony_ci 0.0, 0.0, 1.0, 0.0, 143bf215546Sopenharmony_ci 0.0, 0.0, 0.0, 1.0 144bf215546Sopenharmony_ci}; 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci/**********************************************************************/ 149bf215546Sopenharmony_ci/** \name Matrix multiplication */ 150bf215546Sopenharmony_ci/*@{*/ 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci#define A(row,col) a[(col<<2)+row] 153bf215546Sopenharmony_ci#define B(row,col) b[(col<<2)+row] 154bf215546Sopenharmony_ci#define P(row,col) product[(col<<2)+row] 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci/** 157bf215546Sopenharmony_ci * Perform a full 4x4 matrix multiplication. 158bf215546Sopenharmony_ci * 159bf215546Sopenharmony_ci * \param a matrix. 160bf215546Sopenharmony_ci * \param b matrix. 161bf215546Sopenharmony_ci * \param product will receive the product of \p a and \p b. 162bf215546Sopenharmony_ci * 163bf215546Sopenharmony_ci * \warning Is assumed that \p product != \p b. \p product == \p a is allowed. 164bf215546Sopenharmony_ci * 165bf215546Sopenharmony_ci * \note KW: 4*16 = 64 multiplications 166bf215546Sopenharmony_ci * 167bf215546Sopenharmony_ci * \author This \c matmul was contributed by Thomas Malik 168bf215546Sopenharmony_ci */ 169bf215546Sopenharmony_cistatic void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b ) 170bf215546Sopenharmony_ci{ 171bf215546Sopenharmony_ci GLint i; 172bf215546Sopenharmony_ci for (i = 0; i < 4; i++) { 173bf215546Sopenharmony_ci const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); 174bf215546Sopenharmony_ci P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); 175bf215546Sopenharmony_ci P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); 176bf215546Sopenharmony_ci P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); 177bf215546Sopenharmony_ci P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); 178bf215546Sopenharmony_ci } 179bf215546Sopenharmony_ci} 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci/** 182bf215546Sopenharmony_ci * Multiply two matrices known to occupy only the top three rows, such 183bf215546Sopenharmony_ci * as typical model matrices, and orthogonal matrices. 184bf215546Sopenharmony_ci * 185bf215546Sopenharmony_ci * \param a matrix. 186bf215546Sopenharmony_ci * \param b matrix. 187bf215546Sopenharmony_ci * \param product will receive the product of \p a and \p b. 188bf215546Sopenharmony_ci */ 189bf215546Sopenharmony_cistatic void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b ) 190bf215546Sopenharmony_ci{ 191bf215546Sopenharmony_ci GLint i; 192bf215546Sopenharmony_ci for (i = 0; i < 3; i++) { 193bf215546Sopenharmony_ci const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); 194bf215546Sopenharmony_ci P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0); 195bf215546Sopenharmony_ci P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1); 196bf215546Sopenharmony_ci P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2); 197bf215546Sopenharmony_ci P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3; 198bf215546Sopenharmony_ci } 199bf215546Sopenharmony_ci P(3,0) = 0; 200bf215546Sopenharmony_ci P(3,1) = 0; 201bf215546Sopenharmony_ci P(3,2) = 0; 202bf215546Sopenharmony_ci P(3,3) = 1; 203bf215546Sopenharmony_ci} 204bf215546Sopenharmony_ci 205bf215546Sopenharmony_ci#undef A 206bf215546Sopenharmony_ci#undef B 207bf215546Sopenharmony_ci#undef P 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci/** 210bf215546Sopenharmony_ci * Multiply a matrix by an array of floats with known properties. 211bf215546Sopenharmony_ci * 212bf215546Sopenharmony_ci * \param mat pointer to a GLmatrix structure containing the left multiplication 213bf215546Sopenharmony_ci * matrix, and that will receive the product result. 214bf215546Sopenharmony_ci * \param m right multiplication matrix array. 215bf215546Sopenharmony_ci * \param flags flags of the matrix \p m. 216bf215546Sopenharmony_ci * 217bf215546Sopenharmony_ci * Joins both flags and marks the type and inverse as dirty. Calls matmul34() 218bf215546Sopenharmony_ci * if both matrices are 3D, or matmul4() otherwise. 219bf215546Sopenharmony_ci */ 220bf215546Sopenharmony_cistatic void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags ) 221bf215546Sopenharmony_ci{ 222bf215546Sopenharmony_ci mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE); 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_ci if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) 225bf215546Sopenharmony_ci matmul34( mat->m, mat->m, m ); 226bf215546Sopenharmony_ci else 227bf215546Sopenharmony_ci matmul4( mat->m, mat->m, m ); 228bf215546Sopenharmony_ci} 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci/** 231bf215546Sopenharmony_ci * Matrix multiplication. 232bf215546Sopenharmony_ci * 233bf215546Sopenharmony_ci * \param dest destination matrix. 234bf215546Sopenharmony_ci * \param a left matrix. 235bf215546Sopenharmony_ci * \param b right matrix. 236bf215546Sopenharmony_ci * 237bf215546Sopenharmony_ci * Joins both flags and marks the type and inverse as dirty. Calls matmul34() 238bf215546Sopenharmony_ci * if both matrices are 3D, or matmul4() otherwise. 239bf215546Sopenharmony_ci */ 240bf215546Sopenharmony_civoid 241bf215546Sopenharmony_ci_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b ) 242bf215546Sopenharmony_ci{ 243bf215546Sopenharmony_ci dest->flags = (a->flags | 244bf215546Sopenharmony_ci b->flags | 245bf215546Sopenharmony_ci MAT_DIRTY_TYPE | 246bf215546Sopenharmony_ci MAT_DIRTY_INVERSE); 247bf215546Sopenharmony_ci 248bf215546Sopenharmony_ci if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D)) 249bf215546Sopenharmony_ci matmul34( dest->m, a->m, b->m ); 250bf215546Sopenharmony_ci else 251bf215546Sopenharmony_ci matmul4( dest->m, a->m, b->m ); 252bf215546Sopenharmony_ci} 253bf215546Sopenharmony_ci 254bf215546Sopenharmony_ci/** 255bf215546Sopenharmony_ci * Matrix multiplication. 256bf215546Sopenharmony_ci * 257bf215546Sopenharmony_ci * \param dest left and destination matrix. 258bf215546Sopenharmony_ci * \param m right matrix array. 259bf215546Sopenharmony_ci * 260bf215546Sopenharmony_ci * Marks the matrix flags with general flag, and type and inverse dirty flags. 261bf215546Sopenharmony_ci * Calls matmul4() for the multiplication. 262bf215546Sopenharmony_ci */ 263bf215546Sopenharmony_civoid 264bf215546Sopenharmony_ci_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m ) 265bf215546Sopenharmony_ci{ 266bf215546Sopenharmony_ci dest->flags |= (MAT_FLAG_GENERAL | 267bf215546Sopenharmony_ci MAT_DIRTY_TYPE | 268bf215546Sopenharmony_ci MAT_DIRTY_INVERSE | 269bf215546Sopenharmony_ci MAT_DIRTY_FLAGS); 270bf215546Sopenharmony_ci 271bf215546Sopenharmony_ci matmul4( dest->m, dest->m, m ); 272bf215546Sopenharmony_ci} 273bf215546Sopenharmony_ci 274bf215546Sopenharmony_ci/*@}*/ 275bf215546Sopenharmony_ci 276bf215546Sopenharmony_ci 277bf215546Sopenharmony_ci/**********************************************************************/ 278bf215546Sopenharmony_ci/** \name Matrix output */ 279bf215546Sopenharmony_ci/*@{*/ 280bf215546Sopenharmony_ci 281bf215546Sopenharmony_ci/** 282bf215546Sopenharmony_ci * Print a matrix array. 283bf215546Sopenharmony_ci * 284bf215546Sopenharmony_ci * \param m matrix array. 285bf215546Sopenharmony_ci * 286bf215546Sopenharmony_ci * Called by _math_matrix_print() to print a matrix or its inverse. 287bf215546Sopenharmony_ci */ 288bf215546Sopenharmony_cistatic void print_matrix_floats( const GLfloat m[16] ) 289bf215546Sopenharmony_ci{ 290bf215546Sopenharmony_ci int i; 291bf215546Sopenharmony_ci for (i=0;i<4;i++) { 292bf215546Sopenharmony_ci _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] ); 293bf215546Sopenharmony_ci } 294bf215546Sopenharmony_ci} 295bf215546Sopenharmony_ci 296bf215546Sopenharmony_ci/** 297bf215546Sopenharmony_ci * Dumps the contents of a GLmatrix structure. 298bf215546Sopenharmony_ci * 299bf215546Sopenharmony_ci * \param m pointer to the GLmatrix structure. 300bf215546Sopenharmony_ci */ 301bf215546Sopenharmony_civoid 302bf215546Sopenharmony_ci_math_matrix_print( const GLmatrix *m ) 303bf215546Sopenharmony_ci{ 304bf215546Sopenharmony_ci GLfloat prod[16]; 305bf215546Sopenharmony_ci 306bf215546Sopenharmony_ci _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags); 307bf215546Sopenharmony_ci print_matrix_floats(m->m); 308bf215546Sopenharmony_ci _mesa_debug(NULL, "Inverse: \n"); 309bf215546Sopenharmony_ci print_matrix_floats(m->inv); 310bf215546Sopenharmony_ci matmul4(prod, m->m, m->inv); 311bf215546Sopenharmony_ci _mesa_debug(NULL, "Mat * Inverse:\n"); 312bf215546Sopenharmony_ci print_matrix_floats(prod); 313bf215546Sopenharmony_ci} 314bf215546Sopenharmony_ci 315bf215546Sopenharmony_ci/*@}*/ 316bf215546Sopenharmony_ci 317bf215546Sopenharmony_ci 318bf215546Sopenharmony_ci/** 319bf215546Sopenharmony_ci * References an element of 4x4 matrix. 320bf215546Sopenharmony_ci * 321bf215546Sopenharmony_ci * \param m matrix array. 322bf215546Sopenharmony_ci * \param c column of the desired element. 323bf215546Sopenharmony_ci * \param r row of the desired element. 324bf215546Sopenharmony_ci * 325bf215546Sopenharmony_ci * \return value of the desired element. 326bf215546Sopenharmony_ci * 327bf215546Sopenharmony_ci * Calculate the linear storage index of the element and references it. 328bf215546Sopenharmony_ci */ 329bf215546Sopenharmony_ci#define MAT(m,r,c) (m)[(c)*4+(r)] 330bf215546Sopenharmony_ci 331bf215546Sopenharmony_ci 332bf215546Sopenharmony_ci/**********************************************************************/ 333bf215546Sopenharmony_ci/** \name Matrix inversion */ 334bf215546Sopenharmony_ci/*@{*/ 335bf215546Sopenharmony_ci 336bf215546Sopenharmony_ci/** 337bf215546Sopenharmony_ci * Compute inverse of 4x4 transformation matrix. 338bf215546Sopenharmony_ci * 339bf215546Sopenharmony_ci * \param mat pointer to a GLmatrix structure. The matrix inverse will be 340bf215546Sopenharmony_ci * stored in the GLmatrix::inv attribute. 341bf215546Sopenharmony_ci * 342bf215546Sopenharmony_ci * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 343bf215546Sopenharmony_ci * 344bf215546Sopenharmony_ci * \author 345bf215546Sopenharmony_ci * Code contributed by Jacques Leroy jle@star.be 346bf215546Sopenharmony_ci * 347bf215546Sopenharmony_ci * Calculates the inverse matrix by performing the gaussian matrix reduction 348bf215546Sopenharmony_ci * with partial pivoting followed by back/substitution with the loops manually 349bf215546Sopenharmony_ci * unrolled. 350bf215546Sopenharmony_ci */ 351bf215546Sopenharmony_cistatic GLboolean invert_matrix_general( GLmatrix *mat ) 352bf215546Sopenharmony_ci{ 353bf215546Sopenharmony_ci return util_invert_mat4x4(mat->inv, mat->m); 354bf215546Sopenharmony_ci} 355bf215546Sopenharmony_ci 356bf215546Sopenharmony_ci/** 357bf215546Sopenharmony_ci * Compute inverse of a general 3d transformation matrix. 358bf215546Sopenharmony_ci * 359bf215546Sopenharmony_ci * \param mat pointer to a GLmatrix structure. The matrix inverse will be 360bf215546Sopenharmony_ci * stored in the GLmatrix::inv attribute. 361bf215546Sopenharmony_ci * 362bf215546Sopenharmony_ci * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 363bf215546Sopenharmony_ci * 364bf215546Sopenharmony_ci * \author Adapted from graphics gems II. 365bf215546Sopenharmony_ci * 366bf215546Sopenharmony_ci * Calculates the inverse of the upper left by first calculating its 367bf215546Sopenharmony_ci * determinant and multiplying it to the symmetric adjust matrix of each 368bf215546Sopenharmony_ci * element. Finally deals with the translation part by transforming the 369bf215546Sopenharmony_ci * original translation vector using by the calculated submatrix inverse. 370bf215546Sopenharmony_ci */ 371bf215546Sopenharmony_cistatic GLboolean invert_matrix_3d_general( GLmatrix *mat ) 372bf215546Sopenharmony_ci{ 373bf215546Sopenharmony_ci const GLfloat *in = mat->m; 374bf215546Sopenharmony_ci GLfloat *out = mat->inv; 375bf215546Sopenharmony_ci GLfloat pos, neg, t; 376bf215546Sopenharmony_ci GLfloat det; 377bf215546Sopenharmony_ci 378bf215546Sopenharmony_ci /* Calculate the determinant of upper left 3x3 submatrix and 379bf215546Sopenharmony_ci * determine if the matrix is singular. 380bf215546Sopenharmony_ci */ 381bf215546Sopenharmony_ci pos = neg = 0.0; 382bf215546Sopenharmony_ci t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2); 383bf215546Sopenharmony_ci if (t >= 0.0F) pos += t; else neg += t; 384bf215546Sopenharmony_ci 385bf215546Sopenharmony_ci t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2); 386bf215546Sopenharmony_ci if (t >= 0.0F) pos += t; else neg += t; 387bf215546Sopenharmony_ci 388bf215546Sopenharmony_ci t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2); 389bf215546Sopenharmony_ci if (t >= 0.0F) pos += t; else neg += t; 390bf215546Sopenharmony_ci 391bf215546Sopenharmony_ci t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2); 392bf215546Sopenharmony_ci if (t >= 0.0F) pos += t; else neg += t; 393bf215546Sopenharmony_ci 394bf215546Sopenharmony_ci t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2); 395bf215546Sopenharmony_ci if (t >= 0.0F) pos += t; else neg += t; 396bf215546Sopenharmony_ci 397bf215546Sopenharmony_ci t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2); 398bf215546Sopenharmony_ci if (t >= 0.0F) pos += t; else neg += t; 399bf215546Sopenharmony_ci 400bf215546Sopenharmony_ci det = pos + neg; 401bf215546Sopenharmony_ci 402bf215546Sopenharmony_ci if (fabsf(det) < 1e-25F) 403bf215546Sopenharmony_ci return GL_FALSE; 404bf215546Sopenharmony_ci 405bf215546Sopenharmony_ci det = 1.0F / det; 406bf215546Sopenharmony_ci MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det); 407bf215546Sopenharmony_ci MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det); 408bf215546Sopenharmony_ci MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det); 409bf215546Sopenharmony_ci MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det); 410bf215546Sopenharmony_ci MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det); 411bf215546Sopenharmony_ci MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det); 412bf215546Sopenharmony_ci MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det); 413bf215546Sopenharmony_ci MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det); 414bf215546Sopenharmony_ci MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det); 415bf215546Sopenharmony_ci 416bf215546Sopenharmony_ci /* Do the translation part */ 417bf215546Sopenharmony_ci MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + 418bf215546Sopenharmony_ci MAT(in,1,3) * MAT(out,0,1) + 419bf215546Sopenharmony_ci MAT(in,2,3) * MAT(out,0,2) ); 420bf215546Sopenharmony_ci MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + 421bf215546Sopenharmony_ci MAT(in,1,3) * MAT(out,1,1) + 422bf215546Sopenharmony_ci MAT(in,2,3) * MAT(out,1,2) ); 423bf215546Sopenharmony_ci MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + 424bf215546Sopenharmony_ci MAT(in,1,3) * MAT(out,2,1) + 425bf215546Sopenharmony_ci MAT(in,2,3) * MAT(out,2,2) ); 426bf215546Sopenharmony_ci 427bf215546Sopenharmony_ci return GL_TRUE; 428bf215546Sopenharmony_ci} 429bf215546Sopenharmony_ci 430bf215546Sopenharmony_ci/** 431bf215546Sopenharmony_ci * Compute inverse of a 3d transformation matrix. 432bf215546Sopenharmony_ci * 433bf215546Sopenharmony_ci * \param mat pointer to a GLmatrix structure. The matrix inverse will be 434bf215546Sopenharmony_ci * stored in the GLmatrix::inv attribute. 435bf215546Sopenharmony_ci * 436bf215546Sopenharmony_ci * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 437bf215546Sopenharmony_ci * 438bf215546Sopenharmony_ci * If the matrix is not an angle preserving matrix then calls 439bf215546Sopenharmony_ci * invert_matrix_3d_general for the actual calculation. Otherwise calculates 440bf215546Sopenharmony_ci * the inverse matrix analyzing and inverting each of the scaling, rotation and 441bf215546Sopenharmony_ci * translation parts. 442bf215546Sopenharmony_ci */ 443bf215546Sopenharmony_cistatic GLboolean invert_matrix_3d( GLmatrix *mat ) 444bf215546Sopenharmony_ci{ 445bf215546Sopenharmony_ci const GLfloat *in = mat->m; 446bf215546Sopenharmony_ci GLfloat *out = mat->inv; 447bf215546Sopenharmony_ci 448bf215546Sopenharmony_ci if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) { 449bf215546Sopenharmony_ci return invert_matrix_3d_general( mat ); 450bf215546Sopenharmony_ci } 451bf215546Sopenharmony_ci 452bf215546Sopenharmony_ci if (mat->flags & MAT_FLAG_UNIFORM_SCALE) { 453bf215546Sopenharmony_ci GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) + 454bf215546Sopenharmony_ci MAT(in,0,1) * MAT(in,0,1) + 455bf215546Sopenharmony_ci MAT(in,0,2) * MAT(in,0,2)); 456bf215546Sopenharmony_ci 457bf215546Sopenharmony_ci if (scale == 0.0F) 458bf215546Sopenharmony_ci return GL_FALSE; 459bf215546Sopenharmony_ci 460bf215546Sopenharmony_ci scale = 1.0F / scale; 461bf215546Sopenharmony_ci 462bf215546Sopenharmony_ci /* Transpose and scale the 3 by 3 upper-left submatrix. */ 463bf215546Sopenharmony_ci MAT(out,0,0) = scale * MAT(in,0,0); 464bf215546Sopenharmony_ci MAT(out,1,0) = scale * MAT(in,0,1); 465bf215546Sopenharmony_ci MAT(out,2,0) = scale * MAT(in,0,2); 466bf215546Sopenharmony_ci MAT(out,0,1) = scale * MAT(in,1,0); 467bf215546Sopenharmony_ci MAT(out,1,1) = scale * MAT(in,1,1); 468bf215546Sopenharmony_ci MAT(out,2,1) = scale * MAT(in,1,2); 469bf215546Sopenharmony_ci MAT(out,0,2) = scale * MAT(in,2,0); 470bf215546Sopenharmony_ci MAT(out,1,2) = scale * MAT(in,2,1); 471bf215546Sopenharmony_ci MAT(out,2,2) = scale * MAT(in,2,2); 472bf215546Sopenharmony_ci } 473bf215546Sopenharmony_ci else if (mat->flags & MAT_FLAG_ROTATION) { 474bf215546Sopenharmony_ci /* Transpose the 3 by 3 upper-left submatrix. */ 475bf215546Sopenharmony_ci MAT(out,0,0) = MAT(in,0,0); 476bf215546Sopenharmony_ci MAT(out,1,0) = MAT(in,0,1); 477bf215546Sopenharmony_ci MAT(out,2,0) = MAT(in,0,2); 478bf215546Sopenharmony_ci MAT(out,0,1) = MAT(in,1,0); 479bf215546Sopenharmony_ci MAT(out,1,1) = MAT(in,1,1); 480bf215546Sopenharmony_ci MAT(out,2,1) = MAT(in,1,2); 481bf215546Sopenharmony_ci MAT(out,0,2) = MAT(in,2,0); 482bf215546Sopenharmony_ci MAT(out,1,2) = MAT(in,2,1); 483bf215546Sopenharmony_ci MAT(out,2,2) = MAT(in,2,2); 484bf215546Sopenharmony_ci } 485bf215546Sopenharmony_ci else { 486bf215546Sopenharmony_ci /* pure translation */ 487bf215546Sopenharmony_ci memcpy( out, Identity, sizeof(Identity) ); 488bf215546Sopenharmony_ci MAT(out,0,3) = - MAT(in,0,3); 489bf215546Sopenharmony_ci MAT(out,1,3) = - MAT(in,1,3); 490bf215546Sopenharmony_ci MAT(out,2,3) = - MAT(in,2,3); 491bf215546Sopenharmony_ci return GL_TRUE; 492bf215546Sopenharmony_ci } 493bf215546Sopenharmony_ci 494bf215546Sopenharmony_ci if (mat->flags & MAT_FLAG_TRANSLATION) { 495bf215546Sopenharmony_ci /* Do the translation part */ 496bf215546Sopenharmony_ci MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + 497bf215546Sopenharmony_ci MAT(in,1,3) * MAT(out,0,1) + 498bf215546Sopenharmony_ci MAT(in,2,3) * MAT(out,0,2) ); 499bf215546Sopenharmony_ci MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + 500bf215546Sopenharmony_ci MAT(in,1,3) * MAT(out,1,1) + 501bf215546Sopenharmony_ci MAT(in,2,3) * MAT(out,1,2) ); 502bf215546Sopenharmony_ci MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + 503bf215546Sopenharmony_ci MAT(in,1,3) * MAT(out,2,1) + 504bf215546Sopenharmony_ci MAT(in,2,3) * MAT(out,2,2) ); 505bf215546Sopenharmony_ci } 506bf215546Sopenharmony_ci else { 507bf215546Sopenharmony_ci MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0; 508bf215546Sopenharmony_ci } 509bf215546Sopenharmony_ci 510bf215546Sopenharmony_ci return GL_TRUE; 511bf215546Sopenharmony_ci} 512bf215546Sopenharmony_ci 513bf215546Sopenharmony_ci/** 514bf215546Sopenharmony_ci * Compute inverse of an identity transformation matrix. 515bf215546Sopenharmony_ci * 516bf215546Sopenharmony_ci * \param mat pointer to a GLmatrix structure. The matrix inverse will be 517bf215546Sopenharmony_ci * stored in the GLmatrix::inv attribute. 518bf215546Sopenharmony_ci * 519bf215546Sopenharmony_ci * \return always GL_TRUE. 520bf215546Sopenharmony_ci * 521bf215546Sopenharmony_ci * Simply copies Identity into GLmatrix::inv. 522bf215546Sopenharmony_ci */ 523bf215546Sopenharmony_cistatic GLboolean invert_matrix_identity( GLmatrix *mat ) 524bf215546Sopenharmony_ci{ 525bf215546Sopenharmony_ci memcpy( mat->inv, Identity, sizeof(Identity) ); 526bf215546Sopenharmony_ci return GL_TRUE; 527bf215546Sopenharmony_ci} 528bf215546Sopenharmony_ci 529bf215546Sopenharmony_ci/** 530bf215546Sopenharmony_ci * Compute inverse of a no-rotation 3d transformation matrix. 531bf215546Sopenharmony_ci * 532bf215546Sopenharmony_ci * \param mat pointer to a GLmatrix structure. The matrix inverse will be 533bf215546Sopenharmony_ci * stored in the GLmatrix::inv attribute. 534bf215546Sopenharmony_ci * 535bf215546Sopenharmony_ci * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 536bf215546Sopenharmony_ci * 537bf215546Sopenharmony_ci * Calculates the 538bf215546Sopenharmony_ci */ 539bf215546Sopenharmony_cistatic GLboolean invert_matrix_3d_no_rot( GLmatrix *mat ) 540bf215546Sopenharmony_ci{ 541bf215546Sopenharmony_ci const GLfloat *in = mat->m; 542bf215546Sopenharmony_ci GLfloat *out = mat->inv; 543bf215546Sopenharmony_ci 544bf215546Sopenharmony_ci if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 ) 545bf215546Sopenharmony_ci return GL_FALSE; 546bf215546Sopenharmony_ci 547bf215546Sopenharmony_ci memcpy( out, Identity, sizeof(Identity) ); 548bf215546Sopenharmony_ci MAT(out,0,0) = 1.0F / MAT(in,0,0); 549bf215546Sopenharmony_ci MAT(out,1,1) = 1.0F / MAT(in,1,1); 550bf215546Sopenharmony_ci MAT(out,2,2) = 1.0F / MAT(in,2,2); 551bf215546Sopenharmony_ci 552bf215546Sopenharmony_ci if (mat->flags & MAT_FLAG_TRANSLATION) { 553bf215546Sopenharmony_ci MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); 554bf215546Sopenharmony_ci MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); 555bf215546Sopenharmony_ci MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2)); 556bf215546Sopenharmony_ci } 557bf215546Sopenharmony_ci 558bf215546Sopenharmony_ci return GL_TRUE; 559bf215546Sopenharmony_ci} 560bf215546Sopenharmony_ci 561bf215546Sopenharmony_ci/** 562bf215546Sopenharmony_ci * Compute inverse of a no-rotation 2d transformation matrix. 563bf215546Sopenharmony_ci * 564bf215546Sopenharmony_ci * \param mat pointer to a GLmatrix structure. The matrix inverse will be 565bf215546Sopenharmony_ci * stored in the GLmatrix::inv attribute. 566bf215546Sopenharmony_ci * 567bf215546Sopenharmony_ci * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 568bf215546Sopenharmony_ci * 569bf215546Sopenharmony_ci * Calculates the inverse matrix by applying the inverse scaling and 570bf215546Sopenharmony_ci * translation to the identity matrix. 571bf215546Sopenharmony_ci */ 572bf215546Sopenharmony_cistatic GLboolean invert_matrix_2d_no_rot( GLmatrix *mat ) 573bf215546Sopenharmony_ci{ 574bf215546Sopenharmony_ci const GLfloat *in = mat->m; 575bf215546Sopenharmony_ci GLfloat *out = mat->inv; 576bf215546Sopenharmony_ci 577bf215546Sopenharmony_ci if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0) 578bf215546Sopenharmony_ci return GL_FALSE; 579bf215546Sopenharmony_ci 580bf215546Sopenharmony_ci memcpy( out, Identity, sizeof(Identity) ); 581bf215546Sopenharmony_ci MAT(out,0,0) = 1.0F / MAT(in,0,0); 582bf215546Sopenharmony_ci MAT(out,1,1) = 1.0F / MAT(in,1,1); 583bf215546Sopenharmony_ci 584bf215546Sopenharmony_ci if (mat->flags & MAT_FLAG_TRANSLATION) { 585bf215546Sopenharmony_ci MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); 586bf215546Sopenharmony_ci MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); 587bf215546Sopenharmony_ci } 588bf215546Sopenharmony_ci 589bf215546Sopenharmony_ci return GL_TRUE; 590bf215546Sopenharmony_ci} 591bf215546Sopenharmony_ci 592bf215546Sopenharmony_ci#if 0 593bf215546Sopenharmony_ci/* broken */ 594bf215546Sopenharmony_cistatic GLboolean invert_matrix_perspective( GLmatrix *mat ) 595bf215546Sopenharmony_ci{ 596bf215546Sopenharmony_ci const GLfloat *in = mat->m; 597bf215546Sopenharmony_ci GLfloat *out = mat->inv; 598bf215546Sopenharmony_ci 599bf215546Sopenharmony_ci if (MAT(in,2,3) == 0) 600bf215546Sopenharmony_ci return GL_FALSE; 601bf215546Sopenharmony_ci 602bf215546Sopenharmony_ci memcpy( out, Identity, sizeof(Identity) ); 603bf215546Sopenharmony_ci 604bf215546Sopenharmony_ci MAT(out,0,0) = 1.0F / MAT(in,0,0); 605bf215546Sopenharmony_ci MAT(out,1,1) = 1.0F / MAT(in,1,1); 606bf215546Sopenharmony_ci 607bf215546Sopenharmony_ci MAT(out,0,3) = MAT(in,0,2); 608bf215546Sopenharmony_ci MAT(out,1,3) = MAT(in,1,2); 609bf215546Sopenharmony_ci 610bf215546Sopenharmony_ci MAT(out,2,2) = 0; 611bf215546Sopenharmony_ci MAT(out,2,3) = -1; 612bf215546Sopenharmony_ci 613bf215546Sopenharmony_ci MAT(out,3,2) = 1.0F / MAT(in,2,3); 614bf215546Sopenharmony_ci MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2); 615bf215546Sopenharmony_ci 616bf215546Sopenharmony_ci return GL_TRUE; 617bf215546Sopenharmony_ci} 618bf215546Sopenharmony_ci#endif 619bf215546Sopenharmony_ci 620bf215546Sopenharmony_ci/** 621bf215546Sopenharmony_ci * Matrix inversion function pointer type. 622bf215546Sopenharmony_ci */ 623bf215546Sopenharmony_citypedef GLboolean (*inv_mat_func)( GLmatrix *mat ); 624bf215546Sopenharmony_ci 625bf215546Sopenharmony_ci/** 626bf215546Sopenharmony_ci * Table of the matrix inversion functions according to the matrix type. 627bf215546Sopenharmony_ci */ 628bf215546Sopenharmony_cistatic inv_mat_func inv_mat_tab[7] = { 629bf215546Sopenharmony_ci invert_matrix_general, 630bf215546Sopenharmony_ci invert_matrix_identity, 631bf215546Sopenharmony_ci invert_matrix_3d_no_rot, 632bf215546Sopenharmony_ci#if 0 633bf215546Sopenharmony_ci /* Don't use this function for now - it fails when the projection matrix 634bf215546Sopenharmony_ci * is premultiplied by a translation (ala Chromium's tilesort SPU). 635bf215546Sopenharmony_ci */ 636bf215546Sopenharmony_ci invert_matrix_perspective, 637bf215546Sopenharmony_ci#else 638bf215546Sopenharmony_ci invert_matrix_general, 639bf215546Sopenharmony_ci#endif 640bf215546Sopenharmony_ci invert_matrix_3d, /* lazy! */ 641bf215546Sopenharmony_ci invert_matrix_2d_no_rot, 642bf215546Sopenharmony_ci invert_matrix_3d 643bf215546Sopenharmony_ci}; 644bf215546Sopenharmony_ci 645bf215546Sopenharmony_ci/** 646bf215546Sopenharmony_ci * Compute inverse of a transformation matrix. 647bf215546Sopenharmony_ci * 648bf215546Sopenharmony_ci * \param mat pointer to a GLmatrix structure. The matrix inverse will be 649bf215546Sopenharmony_ci * stored in the GLmatrix::inv attribute. 650bf215546Sopenharmony_ci * 651bf215546Sopenharmony_ci * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). 652bf215546Sopenharmony_ci * 653bf215546Sopenharmony_ci * Calls the matrix inversion function in inv_mat_tab corresponding to the 654bf215546Sopenharmony_ci * given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag, 655bf215546Sopenharmony_ci * and copies the identity matrix into GLmatrix::inv. 656bf215546Sopenharmony_ci */ 657bf215546Sopenharmony_cistatic GLboolean matrix_invert( GLmatrix *mat ) 658bf215546Sopenharmony_ci{ 659bf215546Sopenharmony_ci if (inv_mat_tab[mat->type](mat)) { 660bf215546Sopenharmony_ci mat->flags &= ~MAT_FLAG_SINGULAR; 661bf215546Sopenharmony_ci return GL_TRUE; 662bf215546Sopenharmony_ci } else { 663bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_SINGULAR; 664bf215546Sopenharmony_ci memcpy( mat->inv, Identity, sizeof(Identity) ); 665bf215546Sopenharmony_ci return GL_FALSE; 666bf215546Sopenharmony_ci } 667bf215546Sopenharmony_ci} 668bf215546Sopenharmony_ci 669bf215546Sopenharmony_ci/*@}*/ 670bf215546Sopenharmony_ci 671bf215546Sopenharmony_ci 672bf215546Sopenharmony_ci/**********************************************************************/ 673bf215546Sopenharmony_ci/** \name Matrix generation */ 674bf215546Sopenharmony_ci/*@{*/ 675bf215546Sopenharmony_ci 676bf215546Sopenharmony_ci/** 677bf215546Sopenharmony_ci * Generate a 4x4 transformation matrix from glRotate parameters, and 678bf215546Sopenharmony_ci * post-multiply the input matrix by it. 679bf215546Sopenharmony_ci * 680bf215546Sopenharmony_ci * \author 681bf215546Sopenharmony_ci * This function was contributed by Erich Boleyn (erich@uruk.org). 682bf215546Sopenharmony_ci * Optimizations contributed by Rudolf Opalla (rudi@khm.de). 683bf215546Sopenharmony_ci */ 684bf215546Sopenharmony_civoid 685bf215546Sopenharmony_ci_math_matrix_rotate( GLmatrix *mat, 686bf215546Sopenharmony_ci GLfloat angle, GLfloat x, GLfloat y, GLfloat z ) 687bf215546Sopenharmony_ci{ 688bf215546Sopenharmony_ci GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c; 689bf215546Sopenharmony_ci GLfloat m[16]; 690bf215546Sopenharmony_ci GLboolean optimized; 691bf215546Sopenharmony_ci 692bf215546Sopenharmony_ci s = sinf( angle * M_PI / 180.0 ); 693bf215546Sopenharmony_ci c = cosf( angle * M_PI / 180.0 ); 694bf215546Sopenharmony_ci 695bf215546Sopenharmony_ci memcpy(m, Identity, sizeof(Identity)); 696bf215546Sopenharmony_ci optimized = GL_FALSE; 697bf215546Sopenharmony_ci 698bf215546Sopenharmony_ci#define M(row,col) m[col*4+row] 699bf215546Sopenharmony_ci 700bf215546Sopenharmony_ci if (x == 0.0F) { 701bf215546Sopenharmony_ci if (y == 0.0F) { 702bf215546Sopenharmony_ci if (z != 0.0F) { 703bf215546Sopenharmony_ci optimized = GL_TRUE; 704bf215546Sopenharmony_ci /* rotate only around z-axis */ 705bf215546Sopenharmony_ci M(0,0) = c; 706bf215546Sopenharmony_ci M(1,1) = c; 707bf215546Sopenharmony_ci if (z < 0.0F) { 708bf215546Sopenharmony_ci M(0,1) = s; 709bf215546Sopenharmony_ci M(1,0) = -s; 710bf215546Sopenharmony_ci } 711bf215546Sopenharmony_ci else { 712bf215546Sopenharmony_ci M(0,1) = -s; 713bf215546Sopenharmony_ci M(1,0) = s; 714bf215546Sopenharmony_ci } 715bf215546Sopenharmony_ci } 716bf215546Sopenharmony_ci } 717bf215546Sopenharmony_ci else if (z == 0.0F) { 718bf215546Sopenharmony_ci optimized = GL_TRUE; 719bf215546Sopenharmony_ci /* rotate only around y-axis */ 720bf215546Sopenharmony_ci M(0,0) = c; 721bf215546Sopenharmony_ci M(2,2) = c; 722bf215546Sopenharmony_ci if (y < 0.0F) { 723bf215546Sopenharmony_ci M(0,2) = -s; 724bf215546Sopenharmony_ci M(2,0) = s; 725bf215546Sopenharmony_ci } 726bf215546Sopenharmony_ci else { 727bf215546Sopenharmony_ci M(0,2) = s; 728bf215546Sopenharmony_ci M(2,0) = -s; 729bf215546Sopenharmony_ci } 730bf215546Sopenharmony_ci } 731bf215546Sopenharmony_ci } 732bf215546Sopenharmony_ci else if (y == 0.0F) { 733bf215546Sopenharmony_ci if (z == 0.0F) { 734bf215546Sopenharmony_ci optimized = GL_TRUE; 735bf215546Sopenharmony_ci /* rotate only around x-axis */ 736bf215546Sopenharmony_ci M(1,1) = c; 737bf215546Sopenharmony_ci M(2,2) = c; 738bf215546Sopenharmony_ci if (x < 0.0F) { 739bf215546Sopenharmony_ci M(1,2) = s; 740bf215546Sopenharmony_ci M(2,1) = -s; 741bf215546Sopenharmony_ci } 742bf215546Sopenharmony_ci else { 743bf215546Sopenharmony_ci M(1,2) = -s; 744bf215546Sopenharmony_ci M(2,1) = s; 745bf215546Sopenharmony_ci } 746bf215546Sopenharmony_ci } 747bf215546Sopenharmony_ci } 748bf215546Sopenharmony_ci 749bf215546Sopenharmony_ci if (!optimized) { 750bf215546Sopenharmony_ci const GLfloat mag = sqrtf(x * x + y * y + z * z); 751bf215546Sopenharmony_ci 752bf215546Sopenharmony_ci if (mag <= 1.0e-4F) { 753bf215546Sopenharmony_ci /* no rotation, leave mat as-is */ 754bf215546Sopenharmony_ci return; 755bf215546Sopenharmony_ci } 756bf215546Sopenharmony_ci 757bf215546Sopenharmony_ci x /= mag; 758bf215546Sopenharmony_ci y /= mag; 759bf215546Sopenharmony_ci z /= mag; 760bf215546Sopenharmony_ci 761bf215546Sopenharmony_ci 762bf215546Sopenharmony_ci /* 763bf215546Sopenharmony_ci * Arbitrary axis rotation matrix. 764bf215546Sopenharmony_ci * 765bf215546Sopenharmony_ci * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied 766bf215546Sopenharmony_ci * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation 767bf215546Sopenharmony_ci * (which is about the X-axis), and the two composite transforms 768bf215546Sopenharmony_ci * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary 769bf215546Sopenharmony_ci * from the arbitrary axis to the X-axis then back. They are 770bf215546Sopenharmony_ci * all elementary rotations. 771bf215546Sopenharmony_ci * 772bf215546Sopenharmony_ci * Rz' is a rotation about the Z-axis, to bring the axis vector 773bf215546Sopenharmony_ci * into the x-z plane. Then Ry' is applied, rotating about the 774bf215546Sopenharmony_ci * Y-axis to bring the axis vector parallel with the X-axis. The 775bf215546Sopenharmony_ci * rotation about the X-axis is then performed. Ry and Rz are 776bf215546Sopenharmony_ci * simply the respective inverse transforms to bring the arbitrary 777bf215546Sopenharmony_ci * axis back to its original orientation. The first transforms 778bf215546Sopenharmony_ci * Rz' and Ry' are considered inverses, since the data from the 779bf215546Sopenharmony_ci * arbitrary axis gives you info on how to get to it, not how 780bf215546Sopenharmony_ci * to get away from it, and an inverse must be applied. 781bf215546Sopenharmony_ci * 782bf215546Sopenharmony_ci * The basic calculation used is to recognize that the arbitrary 783bf215546Sopenharmony_ci * axis vector (x, y, z), since it is of unit length, actually 784bf215546Sopenharmony_ci * represents the sines and cosines of the angles to rotate the 785bf215546Sopenharmony_ci * X-axis to the same orientation, with theta being the angle about 786bf215546Sopenharmony_ci * Z and phi the angle about Y (in the order described above) 787bf215546Sopenharmony_ci * as follows: 788bf215546Sopenharmony_ci * 789bf215546Sopenharmony_ci * cos ( theta ) = x / sqrt ( 1 - z^2 ) 790bf215546Sopenharmony_ci * sin ( theta ) = y / sqrt ( 1 - z^2 ) 791bf215546Sopenharmony_ci * 792bf215546Sopenharmony_ci * cos ( phi ) = sqrt ( 1 - z^2 ) 793bf215546Sopenharmony_ci * sin ( phi ) = z 794bf215546Sopenharmony_ci * 795bf215546Sopenharmony_ci * Note that cos ( phi ) can further be inserted to the above 796bf215546Sopenharmony_ci * formulas: 797bf215546Sopenharmony_ci * 798bf215546Sopenharmony_ci * cos ( theta ) = x / cos ( phi ) 799bf215546Sopenharmony_ci * sin ( theta ) = y / sin ( phi ) 800bf215546Sopenharmony_ci * 801bf215546Sopenharmony_ci * ...etc. Because of those relations and the standard trigonometric 802bf215546Sopenharmony_ci * relations, it is pssible to reduce the transforms down to what 803bf215546Sopenharmony_ci * is used below. It may be that any primary axis chosen will give the 804bf215546Sopenharmony_ci * same results (modulo a sign convention) using thie method. 805bf215546Sopenharmony_ci * 806bf215546Sopenharmony_ci * Particularly nice is to notice that all divisions that might 807bf215546Sopenharmony_ci * have caused trouble when parallel to certain planes or 808bf215546Sopenharmony_ci * axis go away with care paid to reducing the expressions. 809bf215546Sopenharmony_ci * After checking, it does perform correctly under all cases, since 810bf215546Sopenharmony_ci * in all the cases of division where the denominator would have 811bf215546Sopenharmony_ci * been zero, the numerator would have been zero as well, giving 812bf215546Sopenharmony_ci * the expected result. 813bf215546Sopenharmony_ci */ 814bf215546Sopenharmony_ci 815bf215546Sopenharmony_ci xx = x * x; 816bf215546Sopenharmony_ci yy = y * y; 817bf215546Sopenharmony_ci zz = z * z; 818bf215546Sopenharmony_ci xy = x * y; 819bf215546Sopenharmony_ci yz = y * z; 820bf215546Sopenharmony_ci zx = z * x; 821bf215546Sopenharmony_ci xs = x * s; 822bf215546Sopenharmony_ci ys = y * s; 823bf215546Sopenharmony_ci zs = z * s; 824bf215546Sopenharmony_ci one_c = 1.0F - c; 825bf215546Sopenharmony_ci 826bf215546Sopenharmony_ci /* We already hold the identity-matrix so we can skip some statements */ 827bf215546Sopenharmony_ci M(0,0) = (one_c * xx) + c; 828bf215546Sopenharmony_ci M(0,1) = (one_c * xy) - zs; 829bf215546Sopenharmony_ci M(0,2) = (one_c * zx) + ys; 830bf215546Sopenharmony_ci/* M(0,3) = 0.0F; */ 831bf215546Sopenharmony_ci 832bf215546Sopenharmony_ci M(1,0) = (one_c * xy) + zs; 833bf215546Sopenharmony_ci M(1,1) = (one_c * yy) + c; 834bf215546Sopenharmony_ci M(1,2) = (one_c * yz) - xs; 835bf215546Sopenharmony_ci/* M(1,3) = 0.0F; */ 836bf215546Sopenharmony_ci 837bf215546Sopenharmony_ci M(2,0) = (one_c * zx) - ys; 838bf215546Sopenharmony_ci M(2,1) = (one_c * yz) + xs; 839bf215546Sopenharmony_ci M(2,2) = (one_c * zz) + c; 840bf215546Sopenharmony_ci/* M(2,3) = 0.0F; */ 841bf215546Sopenharmony_ci 842bf215546Sopenharmony_ci/* 843bf215546Sopenharmony_ci M(3,0) = 0.0F; 844bf215546Sopenharmony_ci M(3,1) = 0.0F; 845bf215546Sopenharmony_ci M(3,2) = 0.0F; 846bf215546Sopenharmony_ci M(3,3) = 1.0F; 847bf215546Sopenharmony_ci*/ 848bf215546Sopenharmony_ci } 849bf215546Sopenharmony_ci#undef M 850bf215546Sopenharmony_ci 851bf215546Sopenharmony_ci matrix_multf( mat, m, MAT_FLAG_ROTATION ); 852bf215546Sopenharmony_ci} 853bf215546Sopenharmony_ci 854bf215546Sopenharmony_ci/** 855bf215546Sopenharmony_ci * Apply a perspective projection matrix. 856bf215546Sopenharmony_ci * 857bf215546Sopenharmony_ci * \param mat matrix to apply the projection. 858bf215546Sopenharmony_ci * \param left left clipping plane coordinate. 859bf215546Sopenharmony_ci * \param right right clipping plane coordinate. 860bf215546Sopenharmony_ci * \param bottom bottom clipping plane coordinate. 861bf215546Sopenharmony_ci * \param top top clipping plane coordinate. 862bf215546Sopenharmony_ci * \param nearval distance to the near clipping plane. 863bf215546Sopenharmony_ci * \param farval distance to the far clipping plane. 864bf215546Sopenharmony_ci * 865bf215546Sopenharmony_ci * Creates the projection matrix and multiplies it with \p mat, marking the 866bf215546Sopenharmony_ci * MAT_FLAG_PERSPECTIVE flag. 867bf215546Sopenharmony_ci */ 868bf215546Sopenharmony_civoid 869bf215546Sopenharmony_ci_math_matrix_frustum( GLmatrix *mat, 870bf215546Sopenharmony_ci GLfloat left, GLfloat right, 871bf215546Sopenharmony_ci GLfloat bottom, GLfloat top, 872bf215546Sopenharmony_ci GLfloat nearval, GLfloat farval ) 873bf215546Sopenharmony_ci{ 874bf215546Sopenharmony_ci GLfloat x, y, a, b, c, d; 875bf215546Sopenharmony_ci GLfloat m[16]; 876bf215546Sopenharmony_ci 877bf215546Sopenharmony_ci x = (2.0F*nearval) / (right-left); 878bf215546Sopenharmony_ci y = (2.0F*nearval) / (top-bottom); 879bf215546Sopenharmony_ci a = (right+left) / (right-left); 880bf215546Sopenharmony_ci b = (top+bottom) / (top-bottom); 881bf215546Sopenharmony_ci c = -(farval+nearval) / ( farval-nearval); 882bf215546Sopenharmony_ci d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */ 883bf215546Sopenharmony_ci 884bf215546Sopenharmony_ci#define M(row,col) m[col*4+row] 885bf215546Sopenharmony_ci M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F; 886bf215546Sopenharmony_ci M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F; 887bf215546Sopenharmony_ci M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d; 888bf215546Sopenharmony_ci M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F; 889bf215546Sopenharmony_ci#undef M 890bf215546Sopenharmony_ci 891bf215546Sopenharmony_ci matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE ); 892bf215546Sopenharmony_ci} 893bf215546Sopenharmony_ci 894bf215546Sopenharmony_ci/** 895bf215546Sopenharmony_ci * Create an orthographic projection matrix. 896bf215546Sopenharmony_ci * 897bf215546Sopenharmony_ci * \param m float array in which to store the project matrix 898bf215546Sopenharmony_ci * \param left left clipping plane coordinate. 899bf215546Sopenharmony_ci * \param right right clipping plane coordinate. 900bf215546Sopenharmony_ci * \param bottom bottom clipping plane coordinate. 901bf215546Sopenharmony_ci * \param top top clipping plane coordinate. 902bf215546Sopenharmony_ci * \param nearval distance to the near clipping plane. 903bf215546Sopenharmony_ci * \param farval distance to the far clipping plane. 904bf215546Sopenharmony_ci * 905bf215546Sopenharmony_ci * Creates the projection matrix and stored the values in \p m. As with other 906bf215546Sopenharmony_ci * OpenGL matrices, the data is stored in column-major ordering. 907bf215546Sopenharmony_ci */ 908bf215546Sopenharmony_civoid 909bf215546Sopenharmony_ci_math_float_ortho(float *m, 910bf215546Sopenharmony_ci float left, float right, 911bf215546Sopenharmony_ci float bottom, float top, 912bf215546Sopenharmony_ci float nearval, float farval) 913bf215546Sopenharmony_ci{ 914bf215546Sopenharmony_ci#define M(row,col) m[col*4+row] 915bf215546Sopenharmony_ci M(0,0) = 2.0F / (right-left); 916bf215546Sopenharmony_ci M(0,1) = 0.0F; 917bf215546Sopenharmony_ci M(0,2) = 0.0F; 918bf215546Sopenharmony_ci M(0,3) = -(right+left) / (right-left); 919bf215546Sopenharmony_ci 920bf215546Sopenharmony_ci M(1,0) = 0.0F; 921bf215546Sopenharmony_ci M(1,1) = 2.0F / (top-bottom); 922bf215546Sopenharmony_ci M(1,2) = 0.0F; 923bf215546Sopenharmony_ci M(1,3) = -(top+bottom) / (top-bottom); 924bf215546Sopenharmony_ci 925bf215546Sopenharmony_ci M(2,0) = 0.0F; 926bf215546Sopenharmony_ci M(2,1) = 0.0F; 927bf215546Sopenharmony_ci M(2,2) = -2.0F / (farval-nearval); 928bf215546Sopenharmony_ci M(2,3) = -(farval+nearval) / (farval-nearval); 929bf215546Sopenharmony_ci 930bf215546Sopenharmony_ci M(3,0) = 0.0F; 931bf215546Sopenharmony_ci M(3,1) = 0.0F; 932bf215546Sopenharmony_ci M(3,2) = 0.0F; 933bf215546Sopenharmony_ci M(3,3) = 1.0F; 934bf215546Sopenharmony_ci#undef M 935bf215546Sopenharmony_ci} 936bf215546Sopenharmony_ci 937bf215546Sopenharmony_ci/** 938bf215546Sopenharmony_ci * Apply an orthographic projection matrix. 939bf215546Sopenharmony_ci * 940bf215546Sopenharmony_ci * \param mat matrix to apply the projection. 941bf215546Sopenharmony_ci * \param left left clipping plane coordinate. 942bf215546Sopenharmony_ci * \param right right clipping plane coordinate. 943bf215546Sopenharmony_ci * \param bottom bottom clipping plane coordinate. 944bf215546Sopenharmony_ci * \param top top clipping plane coordinate. 945bf215546Sopenharmony_ci * \param nearval distance to the near clipping plane. 946bf215546Sopenharmony_ci * \param farval distance to the far clipping plane. 947bf215546Sopenharmony_ci * 948bf215546Sopenharmony_ci * Creates the projection matrix and multiplies it with \p mat, marking the 949bf215546Sopenharmony_ci * MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags. 950bf215546Sopenharmony_ci */ 951bf215546Sopenharmony_civoid 952bf215546Sopenharmony_ci_math_matrix_ortho( GLmatrix *mat, 953bf215546Sopenharmony_ci GLfloat left, GLfloat right, 954bf215546Sopenharmony_ci GLfloat bottom, GLfloat top, 955bf215546Sopenharmony_ci GLfloat nearval, GLfloat farval ) 956bf215546Sopenharmony_ci{ 957bf215546Sopenharmony_ci GLfloat m[16]; 958bf215546Sopenharmony_ci 959bf215546Sopenharmony_ci _math_float_ortho(m, left, right, bottom, top, nearval, farval); 960bf215546Sopenharmony_ci matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION)); 961bf215546Sopenharmony_ci} 962bf215546Sopenharmony_ci 963bf215546Sopenharmony_ci/** 964bf215546Sopenharmony_ci * Multiply a matrix with a general scaling matrix. 965bf215546Sopenharmony_ci * 966bf215546Sopenharmony_ci * \param mat matrix. 967bf215546Sopenharmony_ci * \param x x axis scale factor. 968bf215546Sopenharmony_ci * \param y y axis scale factor. 969bf215546Sopenharmony_ci * \param z z axis scale factor. 970bf215546Sopenharmony_ci * 971bf215546Sopenharmony_ci * Multiplies in-place the elements of \p mat by the scale factors. Checks if 972bf215546Sopenharmony_ci * the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE 973bf215546Sopenharmony_ci * flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and 974bf215546Sopenharmony_ci * MAT_DIRTY_INVERSE dirty flags. 975bf215546Sopenharmony_ci */ 976bf215546Sopenharmony_civoid 977bf215546Sopenharmony_ci_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ) 978bf215546Sopenharmony_ci{ 979bf215546Sopenharmony_ci GLfloat *m = mat->m; 980bf215546Sopenharmony_ci m[0] *= x; m[4] *= y; m[8] *= z; 981bf215546Sopenharmony_ci m[1] *= x; m[5] *= y; m[9] *= z; 982bf215546Sopenharmony_ci m[2] *= x; m[6] *= y; m[10] *= z; 983bf215546Sopenharmony_ci m[3] *= x; m[7] *= y; m[11] *= z; 984bf215546Sopenharmony_ci 985bf215546Sopenharmony_ci if (fabsf(x - y) < 1e-8F && fabsf(x - z) < 1e-8F) 986bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_UNIFORM_SCALE; 987bf215546Sopenharmony_ci else 988bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL_SCALE; 989bf215546Sopenharmony_ci 990bf215546Sopenharmony_ci mat->flags |= (MAT_DIRTY_TYPE | 991bf215546Sopenharmony_ci MAT_DIRTY_INVERSE); 992bf215546Sopenharmony_ci} 993bf215546Sopenharmony_ci 994bf215546Sopenharmony_ci/** 995bf215546Sopenharmony_ci * Multiply a matrix with a translation matrix. 996bf215546Sopenharmony_ci * 997bf215546Sopenharmony_ci * \param mat matrix. 998bf215546Sopenharmony_ci * \param x translation vector x coordinate. 999bf215546Sopenharmony_ci * \param y translation vector y coordinate. 1000bf215546Sopenharmony_ci * \param z translation vector z coordinate. 1001bf215546Sopenharmony_ci * 1002bf215546Sopenharmony_ci * Adds the translation coordinates to the elements of \p mat in-place. Marks 1003bf215546Sopenharmony_ci * the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE 1004bf215546Sopenharmony_ci * dirty flags. 1005bf215546Sopenharmony_ci */ 1006bf215546Sopenharmony_civoid 1007bf215546Sopenharmony_ci_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ) 1008bf215546Sopenharmony_ci{ 1009bf215546Sopenharmony_ci GLfloat *m = mat->m; 1010bf215546Sopenharmony_ci m[12] = m[0] * x + m[4] * y + m[8] * z + m[12]; 1011bf215546Sopenharmony_ci m[13] = m[1] * x + m[5] * y + m[9] * z + m[13]; 1012bf215546Sopenharmony_ci m[14] = m[2] * x + m[6] * y + m[10] * z + m[14]; 1013bf215546Sopenharmony_ci m[15] = m[3] * x + m[7] * y + m[11] * z + m[15]; 1014bf215546Sopenharmony_ci 1015bf215546Sopenharmony_ci mat->flags |= (MAT_FLAG_TRANSLATION | 1016bf215546Sopenharmony_ci MAT_DIRTY_TYPE | 1017bf215546Sopenharmony_ci MAT_DIRTY_INVERSE); 1018bf215546Sopenharmony_ci} 1019bf215546Sopenharmony_ci 1020bf215546Sopenharmony_ci 1021bf215546Sopenharmony_ci/** 1022bf215546Sopenharmony_ci * Set matrix to do viewport and depthrange mapping. 1023bf215546Sopenharmony_ci * Transforms Normalized Device Coords to window/Z values. 1024bf215546Sopenharmony_ci */ 1025bf215546Sopenharmony_civoid 1026bf215546Sopenharmony_ci_math_matrix_viewport(GLmatrix *m, const float scale[3], 1027bf215546Sopenharmony_ci const float translate[3], double depthMax) 1028bf215546Sopenharmony_ci{ 1029bf215546Sopenharmony_ci m->m[MAT_SX] = scale[0]; 1030bf215546Sopenharmony_ci m->m[MAT_TX] = translate[0]; 1031bf215546Sopenharmony_ci m->m[MAT_SY] = scale[1]; 1032bf215546Sopenharmony_ci m->m[MAT_TY] = translate[1]; 1033bf215546Sopenharmony_ci m->m[MAT_SZ] = depthMax*scale[2]; 1034bf215546Sopenharmony_ci m->m[MAT_TZ] = depthMax*translate[2]; 1035bf215546Sopenharmony_ci m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION; 1036bf215546Sopenharmony_ci m->type = MATRIX_3D_NO_ROT; 1037bf215546Sopenharmony_ci} 1038bf215546Sopenharmony_ci 1039bf215546Sopenharmony_ci 1040bf215546Sopenharmony_ci/** 1041bf215546Sopenharmony_ci * Set a matrix to the identity matrix. 1042bf215546Sopenharmony_ci * 1043bf215546Sopenharmony_ci * \param mat matrix. 1044bf215546Sopenharmony_ci * 1045bf215546Sopenharmony_ci * Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL. 1046bf215546Sopenharmony_ci * Sets the matrix type to identity, and clear the dirty flags. 1047bf215546Sopenharmony_ci */ 1048bf215546Sopenharmony_civoid 1049bf215546Sopenharmony_ci_math_matrix_set_identity( GLmatrix *mat ) 1050bf215546Sopenharmony_ci{ 1051bf215546Sopenharmony_ci STATIC_ASSERT(MATRIX_M == offsetof(GLmatrix, m)); 1052bf215546Sopenharmony_ci STATIC_ASSERT(MATRIX_INV == offsetof(GLmatrix, inv)); 1053bf215546Sopenharmony_ci 1054bf215546Sopenharmony_ci memcpy( mat->m, Identity, sizeof(Identity) ); 1055bf215546Sopenharmony_ci memcpy( mat->inv, Identity, sizeof(Identity) ); 1056bf215546Sopenharmony_ci 1057bf215546Sopenharmony_ci mat->type = MATRIX_IDENTITY; 1058bf215546Sopenharmony_ci mat->flags &= ~(MAT_DIRTY_FLAGS| 1059bf215546Sopenharmony_ci MAT_DIRTY_TYPE| 1060bf215546Sopenharmony_ci MAT_DIRTY_INVERSE); 1061bf215546Sopenharmony_ci} 1062bf215546Sopenharmony_ci 1063bf215546Sopenharmony_ci/*@}*/ 1064bf215546Sopenharmony_ci 1065bf215546Sopenharmony_ci 1066bf215546Sopenharmony_ci/**********************************************************************/ 1067bf215546Sopenharmony_ci/** \name Matrix analysis */ 1068bf215546Sopenharmony_ci/*@{*/ 1069bf215546Sopenharmony_ci 1070bf215546Sopenharmony_ci#define ZERO(x) (1<<x) 1071bf215546Sopenharmony_ci#define ONE(x) (1<<(x+16)) 1072bf215546Sopenharmony_ci 1073bf215546Sopenharmony_ci#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14)) 1074bf215546Sopenharmony_ci#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5)) 1075bf215546Sopenharmony_ci 1076bf215546Sopenharmony_ci#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\ 1077bf215546Sopenharmony_ci ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\ 1078bf215546Sopenharmony_ci ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 1079bf215546Sopenharmony_ci ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 1080bf215546Sopenharmony_ci 1081bf215546Sopenharmony_ci#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \ 1082bf215546Sopenharmony_ci ZERO(1) | ZERO(9) | \ 1083bf215546Sopenharmony_ci ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 1084bf215546Sopenharmony_ci ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 1085bf215546Sopenharmony_ci 1086bf215546Sopenharmony_ci#define MASK_2D ( ZERO(8) | \ 1087bf215546Sopenharmony_ci ZERO(9) | \ 1088bf215546Sopenharmony_ci ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ 1089bf215546Sopenharmony_ci ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 1090bf215546Sopenharmony_ci 1091bf215546Sopenharmony_ci 1092bf215546Sopenharmony_ci#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \ 1093bf215546Sopenharmony_ci ZERO(1) | ZERO(9) | \ 1094bf215546Sopenharmony_ci ZERO(2) | ZERO(6) | \ 1095bf215546Sopenharmony_ci ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 1096bf215546Sopenharmony_ci 1097bf215546Sopenharmony_ci#define MASK_3D ( \ 1098bf215546Sopenharmony_ci \ 1099bf215546Sopenharmony_ci \ 1100bf215546Sopenharmony_ci ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) 1101bf215546Sopenharmony_ci 1102bf215546Sopenharmony_ci 1103bf215546Sopenharmony_ci#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\ 1104bf215546Sopenharmony_ci ZERO(1) | ZERO(13) |\ 1105bf215546Sopenharmony_ci ZERO(2) | ZERO(6) | \ 1106bf215546Sopenharmony_ci ZERO(3) | ZERO(7) | ZERO(15) ) 1107bf215546Sopenharmony_ci 1108bf215546Sopenharmony_ci#define SQ(x) ((x)*(x)) 1109bf215546Sopenharmony_ci 1110bf215546Sopenharmony_ci/** 1111bf215546Sopenharmony_ci * Determine type and flags from scratch. 1112bf215546Sopenharmony_ci * 1113bf215546Sopenharmony_ci * \param mat matrix. 1114bf215546Sopenharmony_ci * 1115bf215546Sopenharmony_ci * This is expensive enough to only want to do it once. 1116bf215546Sopenharmony_ci */ 1117bf215546Sopenharmony_cistatic void analyse_from_scratch( GLmatrix *mat ) 1118bf215546Sopenharmony_ci{ 1119bf215546Sopenharmony_ci const GLfloat *m = mat->m; 1120bf215546Sopenharmony_ci GLuint mask = 0; 1121bf215546Sopenharmony_ci GLuint i; 1122bf215546Sopenharmony_ci 1123bf215546Sopenharmony_ci for (i = 0 ; i < 16 ; i++) { 1124bf215546Sopenharmony_ci if (m[i] == 0.0F) mask |= (1<<i); 1125bf215546Sopenharmony_ci } 1126bf215546Sopenharmony_ci 1127bf215546Sopenharmony_ci if (m[0] == 1.0F) mask |= (1<<16); 1128bf215546Sopenharmony_ci if (m[5] == 1.0F) mask |= (1<<21); 1129bf215546Sopenharmony_ci if (m[10] == 1.0F) mask |= (1<<26); 1130bf215546Sopenharmony_ci if (m[15] == 1.0F) mask |= (1<<31); 1131bf215546Sopenharmony_ci 1132bf215546Sopenharmony_ci mat->flags &= ~MAT_FLAGS_GEOMETRY; 1133bf215546Sopenharmony_ci 1134bf215546Sopenharmony_ci /* Check for translation - no-one really cares 1135bf215546Sopenharmony_ci */ 1136bf215546Sopenharmony_ci if ((mask & MASK_NO_TRX) != MASK_NO_TRX) 1137bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_TRANSLATION; 1138bf215546Sopenharmony_ci 1139bf215546Sopenharmony_ci /* Do the real work 1140bf215546Sopenharmony_ci */ 1141bf215546Sopenharmony_ci if (mask == (GLuint) MASK_IDENTITY) { 1142bf215546Sopenharmony_ci mat->type = MATRIX_IDENTITY; 1143bf215546Sopenharmony_ci } 1144bf215546Sopenharmony_ci else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) { 1145bf215546Sopenharmony_ci mat->type = MATRIX_2D_NO_ROT; 1146bf215546Sopenharmony_ci 1147bf215546Sopenharmony_ci if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE) 1148bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL_SCALE; 1149bf215546Sopenharmony_ci } 1150bf215546Sopenharmony_ci else if ((mask & MASK_2D) == (GLuint) MASK_2D) { 1151bf215546Sopenharmony_ci GLfloat mm = DOT2(m, m); 1152bf215546Sopenharmony_ci GLfloat m4m4 = DOT2(m+4,m+4); 1153bf215546Sopenharmony_ci GLfloat mm4 = DOT2(m,m+4); 1154bf215546Sopenharmony_ci 1155bf215546Sopenharmony_ci mat->type = MATRIX_2D; 1156bf215546Sopenharmony_ci 1157bf215546Sopenharmony_ci /* Check for scale */ 1158bf215546Sopenharmony_ci if (SQ(mm-1) > SQ(1e-6F) || 1159bf215546Sopenharmony_ci SQ(m4m4-1) > SQ(1e-6F)) 1160bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL_SCALE; 1161bf215546Sopenharmony_ci 1162bf215546Sopenharmony_ci /* Check for rotation */ 1163bf215546Sopenharmony_ci if (SQ(mm4) > SQ(1e-6F)) 1164bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL_3D; 1165bf215546Sopenharmony_ci else 1166bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_ROTATION; 1167bf215546Sopenharmony_ci 1168bf215546Sopenharmony_ci } 1169bf215546Sopenharmony_ci else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) { 1170bf215546Sopenharmony_ci mat->type = MATRIX_3D_NO_ROT; 1171bf215546Sopenharmony_ci 1172bf215546Sopenharmony_ci /* Check for scale */ 1173bf215546Sopenharmony_ci if (SQ(m[0]-m[5]) < SQ(1e-6F) && 1174bf215546Sopenharmony_ci SQ(m[0]-m[10]) < SQ(1e-6F)) { 1175bf215546Sopenharmony_ci if (SQ(m[0]-1.0F) > SQ(1e-6F)) { 1176bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_UNIFORM_SCALE; 1177bf215546Sopenharmony_ci } 1178bf215546Sopenharmony_ci } 1179bf215546Sopenharmony_ci else { 1180bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL_SCALE; 1181bf215546Sopenharmony_ci } 1182bf215546Sopenharmony_ci } 1183bf215546Sopenharmony_ci else if ((mask & MASK_3D) == (GLuint) MASK_3D) { 1184bf215546Sopenharmony_ci GLfloat c1 = DOT3(m,m); 1185bf215546Sopenharmony_ci GLfloat c2 = DOT3(m+4,m+4); 1186bf215546Sopenharmony_ci GLfloat c3 = DOT3(m+8,m+8); 1187bf215546Sopenharmony_ci GLfloat d1 = DOT3(m, m+4); 1188bf215546Sopenharmony_ci GLfloat cp[3]; 1189bf215546Sopenharmony_ci 1190bf215546Sopenharmony_ci mat->type = MATRIX_3D; 1191bf215546Sopenharmony_ci 1192bf215546Sopenharmony_ci /* Check for scale */ 1193bf215546Sopenharmony_ci if (SQ(c1-c2) < SQ(1e-6F) && SQ(c1-c3) < SQ(1e-6F)) { 1194bf215546Sopenharmony_ci if (SQ(c1-1.0F) > SQ(1e-6F)) 1195bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_UNIFORM_SCALE; 1196bf215546Sopenharmony_ci /* else no scale at all */ 1197bf215546Sopenharmony_ci } 1198bf215546Sopenharmony_ci else { 1199bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL_SCALE; 1200bf215546Sopenharmony_ci } 1201bf215546Sopenharmony_ci 1202bf215546Sopenharmony_ci /* Check for rotation */ 1203bf215546Sopenharmony_ci if (SQ(d1) < SQ(1e-6F)) { 1204bf215546Sopenharmony_ci CROSS3( cp, m, m+4 ); 1205bf215546Sopenharmony_ci SUB_3V( cp, cp, (m+8) ); 1206bf215546Sopenharmony_ci if (LEN_SQUARED_3FV(cp) < SQ(1e-6F)) 1207bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_ROTATION; 1208bf215546Sopenharmony_ci else 1209bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL_3D; 1210bf215546Sopenharmony_ci } 1211bf215546Sopenharmony_ci else { 1212bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */ 1213bf215546Sopenharmony_ci } 1214bf215546Sopenharmony_ci } 1215bf215546Sopenharmony_ci else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) { 1216bf215546Sopenharmony_ci mat->type = MATRIX_PERSPECTIVE; 1217bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL; 1218bf215546Sopenharmony_ci } 1219bf215546Sopenharmony_ci else { 1220bf215546Sopenharmony_ci mat->type = MATRIX_GENERAL; 1221bf215546Sopenharmony_ci mat->flags |= MAT_FLAG_GENERAL; 1222bf215546Sopenharmony_ci } 1223bf215546Sopenharmony_ci} 1224bf215546Sopenharmony_ci 1225bf215546Sopenharmony_ci/** 1226bf215546Sopenharmony_ci * Analyze a matrix given that its flags are accurate. 1227bf215546Sopenharmony_ci * 1228bf215546Sopenharmony_ci * This is the more common operation, hopefully. 1229bf215546Sopenharmony_ci */ 1230bf215546Sopenharmony_cistatic void analyse_from_flags( GLmatrix *mat ) 1231bf215546Sopenharmony_ci{ 1232bf215546Sopenharmony_ci const GLfloat *m = mat->m; 1233bf215546Sopenharmony_ci 1234bf215546Sopenharmony_ci if (TEST_MAT_FLAGS(mat, 0)) { 1235bf215546Sopenharmony_ci mat->type = MATRIX_IDENTITY; 1236bf215546Sopenharmony_ci } 1237bf215546Sopenharmony_ci else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION | 1238bf215546Sopenharmony_ci MAT_FLAG_UNIFORM_SCALE | 1239bf215546Sopenharmony_ci MAT_FLAG_GENERAL_SCALE))) { 1240bf215546Sopenharmony_ci if ( m[10]==1.0F && m[14]==0.0F ) { 1241bf215546Sopenharmony_ci mat->type = MATRIX_2D_NO_ROT; 1242bf215546Sopenharmony_ci } 1243bf215546Sopenharmony_ci else { 1244bf215546Sopenharmony_ci mat->type = MATRIX_3D_NO_ROT; 1245bf215546Sopenharmony_ci } 1246bf215546Sopenharmony_ci } 1247bf215546Sopenharmony_ci else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) { 1248bf215546Sopenharmony_ci if ( m[ 8]==0.0F 1249bf215546Sopenharmony_ci && m[ 9]==0.0F 1250bf215546Sopenharmony_ci && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) { 1251bf215546Sopenharmony_ci mat->type = MATRIX_2D; 1252bf215546Sopenharmony_ci } 1253bf215546Sopenharmony_ci else { 1254bf215546Sopenharmony_ci mat->type = MATRIX_3D; 1255bf215546Sopenharmony_ci } 1256bf215546Sopenharmony_ci } 1257bf215546Sopenharmony_ci else if ( m[4]==0.0F && m[12]==0.0F 1258bf215546Sopenharmony_ci && m[1]==0.0F && m[13]==0.0F 1259bf215546Sopenharmony_ci && m[2]==0.0F && m[6]==0.0F 1260bf215546Sopenharmony_ci && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) { 1261bf215546Sopenharmony_ci mat->type = MATRIX_PERSPECTIVE; 1262bf215546Sopenharmony_ci } 1263bf215546Sopenharmony_ci else { 1264bf215546Sopenharmony_ci mat->type = MATRIX_GENERAL; 1265bf215546Sopenharmony_ci } 1266bf215546Sopenharmony_ci} 1267bf215546Sopenharmony_ci 1268bf215546Sopenharmony_ci/** 1269bf215546Sopenharmony_ci * Analyze and update a matrix. 1270bf215546Sopenharmony_ci * 1271bf215546Sopenharmony_ci * \param mat matrix. 1272bf215546Sopenharmony_ci * 1273bf215546Sopenharmony_ci * If the matrix type is dirty then calls either analyse_from_scratch() or 1274bf215546Sopenharmony_ci * analyse_from_flags() to determine its type, according to whether the flags 1275bf215546Sopenharmony_ci * are dirty or not, respectively. If the matrix has an inverse and it's dirty 1276bf215546Sopenharmony_ci * then calls matrix_invert(). Finally clears the dirty flags. 1277bf215546Sopenharmony_ci */ 1278bf215546Sopenharmony_civoid 1279bf215546Sopenharmony_ci_math_matrix_analyse( GLmatrix *mat ) 1280bf215546Sopenharmony_ci{ 1281bf215546Sopenharmony_ci if (mat->flags & MAT_DIRTY_TYPE) { 1282bf215546Sopenharmony_ci if (mat->flags & MAT_DIRTY_FLAGS) 1283bf215546Sopenharmony_ci analyse_from_scratch( mat ); 1284bf215546Sopenharmony_ci else 1285bf215546Sopenharmony_ci analyse_from_flags( mat ); 1286bf215546Sopenharmony_ci } 1287bf215546Sopenharmony_ci 1288bf215546Sopenharmony_ci if (mat->flags & MAT_DIRTY_INVERSE) { 1289bf215546Sopenharmony_ci matrix_invert( mat ); 1290bf215546Sopenharmony_ci mat->flags &= ~MAT_DIRTY_INVERSE; 1291bf215546Sopenharmony_ci } 1292bf215546Sopenharmony_ci 1293bf215546Sopenharmony_ci mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE); 1294bf215546Sopenharmony_ci} 1295bf215546Sopenharmony_ci 1296bf215546Sopenharmony_ci/*@}*/ 1297bf215546Sopenharmony_ci 1298bf215546Sopenharmony_ci 1299bf215546Sopenharmony_ci/** 1300bf215546Sopenharmony_ci * Test if the given matrix preserves vector lengths. 1301bf215546Sopenharmony_ci */ 1302bf215546Sopenharmony_ciGLboolean 1303bf215546Sopenharmony_ci_math_matrix_is_length_preserving( const GLmatrix *m ) 1304bf215546Sopenharmony_ci{ 1305bf215546Sopenharmony_ci return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING); 1306bf215546Sopenharmony_ci} 1307bf215546Sopenharmony_ci 1308bf215546Sopenharmony_ci 1309bf215546Sopenharmony_ci/** 1310bf215546Sopenharmony_ci * Test if the given matrix does any rotation. 1311bf215546Sopenharmony_ci * (or perhaps if the upper-left 3x3 is non-identity) 1312bf215546Sopenharmony_ci */ 1313bf215546Sopenharmony_ciGLboolean 1314bf215546Sopenharmony_ci_math_matrix_has_rotation( const GLmatrix *m ) 1315bf215546Sopenharmony_ci{ 1316bf215546Sopenharmony_ci if (m->flags & (MAT_FLAG_GENERAL | 1317bf215546Sopenharmony_ci MAT_FLAG_ROTATION | 1318bf215546Sopenharmony_ci MAT_FLAG_GENERAL_3D | 1319bf215546Sopenharmony_ci MAT_FLAG_PERSPECTIVE)) 1320bf215546Sopenharmony_ci return GL_TRUE; 1321bf215546Sopenharmony_ci else 1322bf215546Sopenharmony_ci return GL_FALSE; 1323bf215546Sopenharmony_ci} 1324bf215546Sopenharmony_ci 1325bf215546Sopenharmony_ci 1326bf215546Sopenharmony_ciGLboolean 1327bf215546Sopenharmony_ci_math_matrix_is_general_scale( const GLmatrix *m ) 1328bf215546Sopenharmony_ci{ 1329bf215546Sopenharmony_ci return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE; 1330bf215546Sopenharmony_ci} 1331bf215546Sopenharmony_ci 1332bf215546Sopenharmony_ci 1333bf215546Sopenharmony_ciGLboolean 1334bf215546Sopenharmony_ci_math_matrix_is_dirty( const GLmatrix *m ) 1335bf215546Sopenharmony_ci{ 1336bf215546Sopenharmony_ci return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE; 1337bf215546Sopenharmony_ci} 1338bf215546Sopenharmony_ci 1339bf215546Sopenharmony_ci 1340bf215546Sopenharmony_ci/**********************************************************************/ 1341bf215546Sopenharmony_ci/** \name Matrix setup */ 1342bf215546Sopenharmony_ci/*@{*/ 1343bf215546Sopenharmony_ci 1344bf215546Sopenharmony_ci/** 1345bf215546Sopenharmony_ci * Copy a matrix. 1346bf215546Sopenharmony_ci * 1347bf215546Sopenharmony_ci * \param to destination matrix. 1348bf215546Sopenharmony_ci * \param from source matrix. 1349bf215546Sopenharmony_ci * 1350bf215546Sopenharmony_ci * Copies all fields in GLmatrix, creating an inverse array if necessary. 1351bf215546Sopenharmony_ci */ 1352bf215546Sopenharmony_civoid 1353bf215546Sopenharmony_ci_math_matrix_copy( GLmatrix *to, const GLmatrix *from ) 1354bf215546Sopenharmony_ci{ 1355bf215546Sopenharmony_ci memcpy(to->m, from->m, 16 * sizeof(GLfloat)); 1356bf215546Sopenharmony_ci memcpy(to->inv, from->inv, 16 * sizeof(GLfloat)); 1357bf215546Sopenharmony_ci to->flags = from->flags; 1358bf215546Sopenharmony_ci to->type = from->type; 1359bf215546Sopenharmony_ci} 1360bf215546Sopenharmony_ci 1361bf215546Sopenharmony_ci/** 1362bf215546Sopenharmony_ci * Copy a matrix as part of glPushMatrix. 1363bf215546Sopenharmony_ci * 1364bf215546Sopenharmony_ci * The makes the source matrix canonical (inverse and flags are up-to-date), 1365bf215546Sopenharmony_ci * so that later glPopMatrix is evaluated as a no-op if there is no state 1366bf215546Sopenharmony_ci * change. 1367bf215546Sopenharmony_ci * 1368bf215546Sopenharmony_ci * It this wasn't done, a draw call would canonicalize the matrix, which 1369bf215546Sopenharmony_ci * would make it different from the pushed one and so glPopMatrix wouldn't be 1370bf215546Sopenharmony_ci * recognized as a no-op. 1371bf215546Sopenharmony_ci */ 1372bf215546Sopenharmony_civoid 1373bf215546Sopenharmony_ci_math_matrix_push_copy(GLmatrix *to, GLmatrix *from) 1374bf215546Sopenharmony_ci{ 1375bf215546Sopenharmony_ci if (from->flags & MAT_DIRTY) 1376bf215546Sopenharmony_ci _math_matrix_analyse(from); 1377bf215546Sopenharmony_ci 1378bf215546Sopenharmony_ci _math_matrix_copy(to, from); 1379bf215546Sopenharmony_ci} 1380bf215546Sopenharmony_ci 1381bf215546Sopenharmony_ci/** 1382bf215546Sopenharmony_ci * Loads a matrix array into GLmatrix. 1383bf215546Sopenharmony_ci * 1384bf215546Sopenharmony_ci * \param m matrix array. 1385bf215546Sopenharmony_ci * \param mat matrix. 1386bf215546Sopenharmony_ci * 1387bf215546Sopenharmony_ci * Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY 1388bf215546Sopenharmony_ci * flags. 1389bf215546Sopenharmony_ci */ 1390bf215546Sopenharmony_civoid 1391bf215546Sopenharmony_ci_math_matrix_loadf( GLmatrix *mat, const GLfloat *m ) 1392bf215546Sopenharmony_ci{ 1393bf215546Sopenharmony_ci memcpy( mat->m, m, 16*sizeof(GLfloat) ); 1394bf215546Sopenharmony_ci mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY); 1395bf215546Sopenharmony_ci} 1396bf215546Sopenharmony_ci 1397bf215546Sopenharmony_ci/** 1398bf215546Sopenharmony_ci * Matrix constructor. 1399bf215546Sopenharmony_ci * 1400bf215546Sopenharmony_ci * \param m matrix. 1401bf215546Sopenharmony_ci * 1402bf215546Sopenharmony_ci * Initialize the GLmatrix fields. 1403bf215546Sopenharmony_ci */ 1404bf215546Sopenharmony_civoid 1405bf215546Sopenharmony_ci_math_matrix_ctr( GLmatrix *m ) 1406bf215546Sopenharmony_ci{ 1407bf215546Sopenharmony_ci memset(m, 0, sizeof(*m)); 1408bf215546Sopenharmony_ci memcpy( m->m, Identity, sizeof(Identity) ); 1409bf215546Sopenharmony_ci memcpy( m->inv, Identity, sizeof(Identity) ); 1410bf215546Sopenharmony_ci m->type = MATRIX_IDENTITY; 1411bf215546Sopenharmony_ci m->flags = 0; 1412bf215546Sopenharmony_ci} 1413bf215546Sopenharmony_ci 1414bf215546Sopenharmony_ci/*@}*/ 1415bf215546Sopenharmony_ci 1416bf215546Sopenharmony_ci 1417bf215546Sopenharmony_ci/**********************************************************************/ 1418bf215546Sopenharmony_ci/** \name Matrix transpose */ 1419bf215546Sopenharmony_ci/*@{*/ 1420bf215546Sopenharmony_ci 1421bf215546Sopenharmony_ci/** 1422bf215546Sopenharmony_ci * Transpose a GLfloat matrix. 1423bf215546Sopenharmony_ci * 1424bf215546Sopenharmony_ci * \param to destination array. 1425bf215546Sopenharmony_ci * \param from source array. 1426bf215546Sopenharmony_ci */ 1427bf215546Sopenharmony_civoid 1428bf215546Sopenharmony_ci_math_transposef( GLfloat to[16], const GLfloat from[16] ) 1429bf215546Sopenharmony_ci{ 1430bf215546Sopenharmony_ci to[0] = from[0]; 1431bf215546Sopenharmony_ci to[1] = from[4]; 1432bf215546Sopenharmony_ci to[2] = from[8]; 1433bf215546Sopenharmony_ci to[3] = from[12]; 1434bf215546Sopenharmony_ci to[4] = from[1]; 1435bf215546Sopenharmony_ci to[5] = from[5]; 1436bf215546Sopenharmony_ci to[6] = from[9]; 1437bf215546Sopenharmony_ci to[7] = from[13]; 1438bf215546Sopenharmony_ci to[8] = from[2]; 1439bf215546Sopenharmony_ci to[9] = from[6]; 1440bf215546Sopenharmony_ci to[10] = from[10]; 1441bf215546Sopenharmony_ci to[11] = from[14]; 1442bf215546Sopenharmony_ci to[12] = from[3]; 1443bf215546Sopenharmony_ci to[13] = from[7]; 1444bf215546Sopenharmony_ci to[14] = from[11]; 1445bf215546Sopenharmony_ci to[15] = from[15]; 1446bf215546Sopenharmony_ci} 1447bf215546Sopenharmony_ci 1448bf215546Sopenharmony_ci/** 1449bf215546Sopenharmony_ci * Transpose a GLdouble matrix. 1450bf215546Sopenharmony_ci * 1451bf215546Sopenharmony_ci * \param to destination array. 1452bf215546Sopenharmony_ci * \param from source array. 1453bf215546Sopenharmony_ci */ 1454bf215546Sopenharmony_civoid 1455bf215546Sopenharmony_ci_math_transposed( GLdouble to[16], const GLdouble from[16] ) 1456bf215546Sopenharmony_ci{ 1457bf215546Sopenharmony_ci to[0] = from[0]; 1458bf215546Sopenharmony_ci to[1] = from[4]; 1459bf215546Sopenharmony_ci to[2] = from[8]; 1460bf215546Sopenharmony_ci to[3] = from[12]; 1461bf215546Sopenharmony_ci to[4] = from[1]; 1462bf215546Sopenharmony_ci to[5] = from[5]; 1463bf215546Sopenharmony_ci to[6] = from[9]; 1464bf215546Sopenharmony_ci to[7] = from[13]; 1465bf215546Sopenharmony_ci to[8] = from[2]; 1466bf215546Sopenharmony_ci to[9] = from[6]; 1467bf215546Sopenharmony_ci to[10] = from[10]; 1468bf215546Sopenharmony_ci to[11] = from[14]; 1469bf215546Sopenharmony_ci to[12] = from[3]; 1470bf215546Sopenharmony_ci to[13] = from[7]; 1471bf215546Sopenharmony_ci to[14] = from[11]; 1472bf215546Sopenharmony_ci to[15] = from[15]; 1473bf215546Sopenharmony_ci} 1474bf215546Sopenharmony_ci 1475bf215546Sopenharmony_ci/** 1476bf215546Sopenharmony_ci * Transpose a GLdouble matrix and convert to GLfloat. 1477bf215546Sopenharmony_ci * 1478bf215546Sopenharmony_ci * \param to destination array. 1479bf215546Sopenharmony_ci * \param from source array. 1480bf215546Sopenharmony_ci */ 1481bf215546Sopenharmony_civoid 1482bf215546Sopenharmony_ci_math_transposefd( GLfloat to[16], const GLdouble from[16] ) 1483bf215546Sopenharmony_ci{ 1484bf215546Sopenharmony_ci to[0] = (GLfloat) from[0]; 1485bf215546Sopenharmony_ci to[1] = (GLfloat) from[4]; 1486bf215546Sopenharmony_ci to[2] = (GLfloat) from[8]; 1487bf215546Sopenharmony_ci to[3] = (GLfloat) from[12]; 1488bf215546Sopenharmony_ci to[4] = (GLfloat) from[1]; 1489bf215546Sopenharmony_ci to[5] = (GLfloat) from[5]; 1490bf215546Sopenharmony_ci to[6] = (GLfloat) from[9]; 1491bf215546Sopenharmony_ci to[7] = (GLfloat) from[13]; 1492bf215546Sopenharmony_ci to[8] = (GLfloat) from[2]; 1493bf215546Sopenharmony_ci to[9] = (GLfloat) from[6]; 1494bf215546Sopenharmony_ci to[10] = (GLfloat) from[10]; 1495bf215546Sopenharmony_ci to[11] = (GLfloat) from[14]; 1496bf215546Sopenharmony_ci to[12] = (GLfloat) from[3]; 1497bf215546Sopenharmony_ci to[13] = (GLfloat) from[7]; 1498bf215546Sopenharmony_ci to[14] = (GLfloat) from[11]; 1499bf215546Sopenharmony_ci to[15] = (GLfloat) from[15]; 1500bf215546Sopenharmony_ci} 1501bf215546Sopenharmony_ci 1502bf215546Sopenharmony_ci/*@}*/ 1503bf215546Sopenharmony_ci 1504bf215546Sopenharmony_ci 1505bf215546Sopenharmony_ci/** 1506bf215546Sopenharmony_ci * Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This 1507bf215546Sopenharmony_ci * function is used for transforming clipping plane equations and spotlight 1508bf215546Sopenharmony_ci * directions. 1509bf215546Sopenharmony_ci * Mathematically, u = v * m. 1510bf215546Sopenharmony_ci * Input: v - input vector 1511bf215546Sopenharmony_ci * m - transformation matrix 1512bf215546Sopenharmony_ci * Output: u - transformed vector 1513bf215546Sopenharmony_ci */ 1514bf215546Sopenharmony_civoid 1515bf215546Sopenharmony_ci_mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] ) 1516bf215546Sopenharmony_ci{ 1517bf215546Sopenharmony_ci const GLfloat v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; 1518bf215546Sopenharmony_ci#define M(row,col) m[row + col*4] 1519bf215546Sopenharmony_ci u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0); 1520bf215546Sopenharmony_ci u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1); 1521bf215546Sopenharmony_ci u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2); 1522bf215546Sopenharmony_ci u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3); 1523bf215546Sopenharmony_ci#undef M 1524bf215546Sopenharmony_ci} 1525