⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m_matrix.c

📁 mesa-6.5-minigui源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Mesa 3-D graphics library * Version:  6.3 * * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *//** * \file m_matrix.c * Matrix operations. * * \note * -# 4x4 transformation matrices are stored in memory in column major order. * -# Points/vertices are to be thought of as column vectors. * -# Transformation of a point p by a matrix M is: p' = M * p */#include "glheader.h"#include "imports.h"#include "macros.h"#include "imports.h"#include "m_matrix.h"/** * \defgroup MatFlags MAT_FLAG_XXX-flags * * Bitmasks to indicate different kinds of 4x4 matrices in GLmatrix::flags * It would be nice to make all these flags private to m_matrix.c *//*@{*/#define MAT_FLAG_IDENTITY       0     /**< is an identity matrix flag.                                       *   (Not actually used - the identity                                       *   matrix is identified by the absense                                       *   of all other flags.)                                       */#define MAT_FLAG_GENERAL        0x1   /**< is a general matrix flag */#define MAT_FLAG_ROTATION       0x2   /**< is a rotation matrix flag */#define MAT_FLAG_TRANSLATION    0x4   /**< is a translation matrix flag */#define MAT_FLAG_UNIFORM_SCALE  0x8   /**< is an uniform scaling matrix flag */#define MAT_FLAG_GENERAL_SCALE  0x10  /**< is a general scaling matrix flag */#define MAT_FLAG_GENERAL_3D     0x20  /**< general 3D matrix flag */#define MAT_FLAG_PERSPECTIVE    0x40  /**< is a perspective proj matrix flag */#define MAT_FLAG_SINGULAR       0x80  /**< is a singular matrix flag */#define MAT_DIRTY_TYPE          0x100  /**< matrix type is dirty */#define MAT_DIRTY_FLAGS         0x200  /**< matrix flags are dirty */#define MAT_DIRTY_INVERSE       0x400  /**< matrix inverse is dirty *//** angle preserving matrix flags mask */#define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \				    MAT_FLAG_TRANSLATION | \				    MAT_FLAG_UNIFORM_SCALE)/** geometry related matrix flags mask */#define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \			    MAT_FLAG_ROTATION | \			    MAT_FLAG_TRANSLATION | \			    MAT_FLAG_UNIFORM_SCALE | \			    MAT_FLAG_GENERAL_SCALE | \			    MAT_FLAG_GENERAL_3D | \			    MAT_FLAG_PERSPECTIVE | \	                    MAT_FLAG_SINGULAR)/** length preserving matrix flags mask */#define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \				     MAT_FLAG_TRANSLATION)/** 3D (non-perspective) matrix flags mask */#define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \		      MAT_FLAG_TRANSLATION | \		      MAT_FLAG_UNIFORM_SCALE | \		      MAT_FLAG_GENERAL_SCALE | \		      MAT_FLAG_GENERAL_3D)/** dirty matrix flags mask */#define MAT_DIRTY          (MAT_DIRTY_TYPE | \			    MAT_DIRTY_FLAGS | \			    MAT_DIRTY_INVERSE)/*@}*//**  * Test geometry related matrix flags. *  * \param mat a pointer to a GLmatrix structure. * \param a flags mask. * * \returns non-zero if all geometry related matrix flags are contained within * the mask, or zero otherwise. */ #define TEST_MAT_FLAGS(mat, a)  \    ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)/** * Names of the corresponding GLmatrixtype values. */static const char *types[] = {   "MATRIX_GENERAL",   "MATRIX_IDENTITY",   "MATRIX_3D_NO_ROT",   "MATRIX_PERSPECTIVE",   "MATRIX_2D",   "MATRIX_2D_NO_ROT",   "MATRIX_3D"};/** * Identity matrix. */static GLfloat Identity[16] = {   1.0, 0.0, 0.0, 0.0,   0.0, 1.0, 0.0, 0.0,   0.0, 0.0, 1.0, 0.0,   0.0, 0.0, 0.0, 1.0};/**********************************************************************//** \name Matrix multiplication *//*@{*/#define A(row,col)  a[(col<<2)+row]#define B(row,col)  b[(col<<2)+row]#define P(row,col)  product[(col<<2)+row]/** * Perform a full 4x4 matrix multiplication. * * \param a matrix. * \param b matrix. * \param product will receive the product of \p a and \p b. * * \warning Is assumed that \p product != \p b. \p product == \p a is allowed. * * \note KW: 4*16 = 64 multiplications *  * \author This \c matmul was contributed by Thomas Malik */static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b ){   GLint i;   for (i = 0; i < 4; i++) {      const GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);      P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);      P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);      P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);      P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);   }}/** * Multiply two matrices known to occupy only the top three rows, such * as typical model matrices, and orthogonal matrices. * * \param a matrix. * \param b matrix. * \param product will receive the product of \p a and \p b. */static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b ){   GLint i;   for (i = 0; i < 3; i++) {      const GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3);      P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);      P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);      P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);      P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;   }   P(3,0) = 0;   P(3,1) = 0;   P(3,2) = 0;   P(3,3) = 1;}#undef A#undef B#undef P/** * Multiply a matrix by an array of floats with known properties. * * \param mat pointer to a GLmatrix structure containing the left multiplication * matrix, and that will receive the product result. * \param m right multiplication matrix array. * \param flags flags of the matrix \p m. *  * Joins both flags and marks the type and inverse as dirty.  Calls matmul34() * if both matrices are 3D, or matmul4() otherwise. */static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags ){   mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);   if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))      matmul34( mat->m, mat->m, m );   else      matmul4( mat->m, mat->m, m );}/** * Matrix multiplication. * * \param dest destination matrix. * \param a left matrix. * \param b right matrix. *  * Joins both flags and marks the type and inverse as dirty.  Calls matmul34() * if both matrices are 3D, or matmul4() otherwise. */void_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b ){   dest->flags = (a->flags |		  b->flags |		  MAT_DIRTY_TYPE |		  MAT_DIRTY_INVERSE);   if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))      matmul34( dest->m, a->m, b->m );   else      matmul4( dest->m, a->m, b->m );}/** * Matrix multiplication. * * \param dest left and destination matrix. * \param m right matrix array. *  * Marks the matrix flags with general flag, and type and inverse dirty flags. * Calls matmul4() for the multiplication. */void_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m ){   dest->flags |= (MAT_FLAG_GENERAL |		   MAT_DIRTY_TYPE |		   MAT_DIRTY_INVERSE |                   MAT_DIRTY_FLAGS);   matmul4( dest->m, dest->m, m );}/*@}*//**********************************************************************//** \name Matrix output *//*@{*//** * Print a matrix array. * * \param m matrix array. * * Called by _math_matrix_print() to print a matrix or its inverse. */static void print_matrix_floats( const GLfloat m[16] ){   int i;   for (i=0;i<4;i++) {      _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );   }}/** * Dumps the contents of a GLmatrix structure. *  * \param m pointer to the GLmatrix structure. */void_math_matrix_print( const GLmatrix *m ){   _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);   print_matrix_floats(m->m);   _mesa_debug(NULL, "Inverse: \n");   if (m->inv) {      GLfloat prod[16];      print_matrix_floats(m->inv);      matmul4(prod, m->m, m->inv);      _mesa_debug(NULL, "Mat * Inverse:\n");      print_matrix_floats(prod);   }   else {      _mesa_debug(NULL, "  - not available\n");   }}/*@}*//** * References an element of 4x4 matrix. * * \param m matrix array. * \param c column of the desired element. * \param r row of the desired element. *  * \return value of the desired element. * * Calculate the linear storage index of the element and references it.  */#define MAT(m,r,c) (m)[(c)*4+(r)]/**********************************************************************//** \name Matrix inversion *//*@{*//** * Swaps the values of two floating pointer variables. * * Used by invert_matrix_general() to swap the row pointers. */#define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }/** * Compute inverse of 4x4 transformation matrix. *  * \param mat pointer to a GLmatrix structure. The matrix inverse will be * stored in the GLmatrix::inv attribute. *  * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). *  * \author * Code contributed by Jacques Leroy jle@star.be * * Calculates the inverse matrix by performing the gaussian matrix reduction * with partial pivoting followed by back/substitution with the loops manually * unrolled. */static GLboolean invert_matrix_general( GLmatrix *mat ){   const GLfloat *m = mat->m;   GLfloat *out = mat->inv;   GLfloat wtmp[4][8];   GLfloat m0, m1, m2, m3, s;   GLfloat *r0, *r1, *r2, *r3;   r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];   r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),   r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),   r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,   r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),   r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),   r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,   r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),   r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),   r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,   r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),   r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),   r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;   /* choose pivot - or die */   if (FABSF(r3[0])>FABSF(r2[0])) SWAP_ROWS(r3, r2);   if (FABSF(r2[0])>FABSF(r1[0])) SWAP_ROWS(r2, r1);   if (FABSF(r1[0])>FABSF(r0[0])) SWAP_ROWS(r1, r0);   if (0.0 == r0[0])  return GL_FALSE;   /* eliminate first variable     */   m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];   s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;   s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;   s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;   s = r0[4];   if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }   s = r0[5];   if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }   s = r0[6];   if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }   s = r0[7];   if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }   /* choose pivot - or die */   if (FABSF(r3[1])>FABSF(r2[1])) SWAP_ROWS(r3, r2);   if (FABSF(r2[1])>FABSF(r1[1])) SWAP_ROWS(r2, r1);   if (0.0 == r1[1])  return GL_FALSE;   /* eliminate second variable */   m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];   r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];   r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];   s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }   s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }   s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }   s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }   /* choose pivot - or die */   if (FABSF(r3[2])>FABSF(r2[2])) SWAP_ROWS(r3, r2);   if (0.0 == r2[2])  return GL_FALSE;   /* eliminate third variable */   m3 = r3[2]/r2[2];   r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],   r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],   r3[7] -= m3 * r2[7];   /* last check */   if (0.0 == r3[3]) return GL_FALSE;   s = 1.0F/r3[3];             /* now back substitute row 3 */   r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;   m2 = r2[3];                 /* now back substitute row 2 */   s  = 1.0F/r2[2];   r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),   r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);   m1 = r1[3];   r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,   r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;   m0 = r0[3];   r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,   r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;   m1 = r1[2];                 /* now back substitute row 1 */   s  = 1.0F/r1[1];   r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),   r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);   m0 = r0[2];   r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,   r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;   m0 = r0[1];                 /* now back substitute row 0 */   s  = 1.0F/r0[0];   r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),   r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);   MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],   MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],   MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],   MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],   MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],   MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],   MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],   MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];   return GL_TRUE;}#undef SWAP_ROWS/** * Compute inverse of a general 3d transformation matrix. *  * \param mat pointer to a GLmatrix structure. The matrix inverse will be * stored in the GLmatrix::inv attribute. *  * \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix). * * \author Adapted from graphics gems II. * * Calculates the inverse of the upper left by first calculating its * determinant and multiplying it to the symmetric adjust matrix of each * element. Finally deals with the translation part by transforming the * original translation vector using by the calculated submatrix inverse. */static GLboolean invert_matrix_3d_general( GLmatrix *mat ){   const GLfloat *in = mat->m;   GLfloat *out = mat->inv;   GLfloat pos, neg, t;   GLfloat det;   /* Calculate the determinant of upper left 3x3 submatrix and    * determine if the matrix is singular.    */   pos = neg = 0.0;   t =  MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);   if (t >= 0.0) pos += t; else neg += t;   t =  MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);   if (t >= 0.0) pos += t; else neg += t;   t =  MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);   if (t >= 0.0) pos += t; else neg += t;   t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);   if (t >= 0.0) pos += t; else neg += t;   t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);   if (t >= 0.0) pos += t; else neg += t;   t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);   if (t >= 0.0) pos += t; else neg += t;   det = pos + neg;   if (det*det < 1e-25)      return GL_FALSE;   det = 1.0F / det;   MAT(out,0,0) = (  (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);   MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);   MAT(out,0,2) = (  (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);   MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);   MAT(out,1,1) = (  (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);   MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);   MAT(out,2,0) = (  (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);   MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);   MAT(out,2,2) = (  (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);   /* Do the translation part */   MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +		     MAT(in,1,3) * MAT(out,0,1) +		     MAT(in,2,3) * MAT(out,0,2) );   MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +		     MAT(in,1,3) * MAT(out,1,1) +		     MAT(in,2,3) * MAT(out,1,2) );   MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +		     MAT(in,1,3) * MAT(out,2,1) +		     MAT(in,2,3) * MAT(out,2,2) );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -