17117f1b4Smrg/* 27117f1b4Smrg * Mesa 3-D graphics library 37117f1b4Smrg * 47117f1b4Smrg * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. 57117f1b4Smrg * 67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a 77117f1b4Smrg * copy of this software and associated documentation files (the "Software"), 87117f1b4Smrg * to deal in the Software without restriction, including without limitation 97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the 117117f1b4Smrg * Software is furnished to do so, subject to the following conditions: 127117f1b4Smrg * 137117f1b4Smrg * The above copyright notice and this permission notice shall be included 147117f1b4Smrg * in all copies or substantial portions of the Software. 157117f1b4Smrg * 167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE. 237117f1b4Smrg */ 247117f1b4Smrg 257117f1b4Smrg 267117f1b4Smrg/** 277117f1b4Smrg * \file math/m_matrix.h 287117f1b4Smrg * Defines basic structures for matrix-handling. 297117f1b4Smrg */ 307117f1b4Smrg 317117f1b4Smrg#ifndef _M_MATRIX_H 327117f1b4Smrg#define _M_MATRIX_H 337117f1b4Smrg 347117f1b4Smrg 353464ebd5Sriastradh#include "main/glheader.h" 363464ebd5Sriastradh 377117f1b4Smrg 38af69d88dSmrg#ifdef __cplusplus 39af69d88dSmrgextern "C" { 40af69d88dSmrg#endif 41af69d88dSmrg 42af69d88dSmrg 437117f1b4Smrg/** 447117f1b4Smrg * \name Symbolic names to some of the entries in the matrix 457117f1b4Smrg * 467117f1b4Smrg * These are handy for the viewport mapping, which is expressed as a matrix. 477117f1b4Smrg */ 487117f1b4Smrg/*@{*/ 497117f1b4Smrg#define MAT_SX 0 507117f1b4Smrg#define MAT_SY 5 517117f1b4Smrg#define MAT_SZ 10 527117f1b4Smrg#define MAT_TX 12 537117f1b4Smrg#define MAT_TY 13 547117f1b4Smrg#define MAT_TZ 14 557117f1b4Smrg/*@}*/ 567117f1b4Smrg 577117f1b4Smrg 587117f1b4Smrg/** 597117f1b4Smrg * Different kinds of 4x4 transformation matrices. 607117f1b4Smrg * We use these to select specific optimized vertex transformation routines. 617117f1b4Smrg */ 627117f1b4Smrgenum GLmatrixtype { 637117f1b4Smrg MATRIX_GENERAL, /**< general 4x4 matrix */ 647117f1b4Smrg MATRIX_IDENTITY, /**< identity matrix */ 657117f1b4Smrg MATRIX_3D_NO_ROT, /**< orthogonal projection and others... */ 667117f1b4Smrg MATRIX_PERSPECTIVE, /**< perspective projection matrix */ 677117f1b4Smrg MATRIX_2D, /**< 2-D transformation */ 687117f1b4Smrg MATRIX_2D_NO_ROT, /**< 2-D scale & translate only */ 697117f1b4Smrg MATRIX_3D /**< 3-D transformation */ 707117f1b4Smrg} ; 717117f1b4Smrg 727117f1b4Smrg/** 737117f1b4Smrg * Matrix type to represent 4x4 transformation matrices. 747117f1b4Smrg */ 757117f1b4Smrgtypedef struct { 767ec681f3Smrg ALIGN16 GLfloat m[16]; /**< 16 matrix elements (16-byte aligned) */ 777ec681f3Smrg ALIGN16 GLfloat inv[16]; /**< 16-element inverse (16-byte aligned) */ 787117f1b4Smrg GLuint flags; /**< possible values determined by (of \link 797117f1b4Smrg * MatFlags MAT_FLAG_* flags\endlink) 807117f1b4Smrg */ 817117f1b4Smrg enum GLmatrixtype type; 827117f1b4Smrg} GLmatrix; 837117f1b4Smrg 847117f1b4Smrg 857117f1b4Smrg 867117f1b4Smrg 877117f1b4Smrgextern void 887117f1b4Smrg_math_matrix_ctr( GLmatrix *m ); 897117f1b4Smrg 907117f1b4Smrgextern void 917117f1b4Smrg_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b ); 927117f1b4Smrg 937117f1b4Smrgextern void 947117f1b4Smrg_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *b ); 957117f1b4Smrg 967117f1b4Smrgextern void 977117f1b4Smrg_math_matrix_loadf( GLmatrix *mat, const GLfloat *m ); 987117f1b4Smrg 997117f1b4Smrgextern void 1007117f1b4Smrg_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ); 1017117f1b4Smrg 1027117f1b4Smrgextern void 1037117f1b4Smrg_math_matrix_rotate( GLmatrix *m, GLfloat angle, 1047117f1b4Smrg GLfloat x, GLfloat y, GLfloat z ); 1057117f1b4Smrg 1067117f1b4Smrgextern void 1077117f1b4Smrg_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ); 1087117f1b4Smrg 1097ec681f3Smrgextern void 1107ec681f3Smrg_math_float_ortho(float *m, 1117ec681f3Smrg float left, float right, 1127ec681f3Smrg float bottom, float top, 1137ec681f3Smrg float nearval, float farval); 1147ec681f3Smrg 1157117f1b4Smrgextern void 1167117f1b4Smrg_math_matrix_ortho( GLmatrix *mat, 1177117f1b4Smrg GLfloat left, GLfloat right, 1187117f1b4Smrg GLfloat bottom, GLfloat top, 1197117f1b4Smrg GLfloat nearval, GLfloat farval ); 1207117f1b4Smrg 1217117f1b4Smrgextern void 1227117f1b4Smrg_math_matrix_frustum( GLmatrix *mat, 1237117f1b4Smrg GLfloat left, GLfloat right, 1247117f1b4Smrg GLfloat bottom, GLfloat top, 1257117f1b4Smrg GLfloat nearval, GLfloat farval ); 1267117f1b4Smrg 1277117f1b4Smrgextern void 12801e04c3fSmrg_math_matrix_viewport( GLmatrix *m, const float scale[3], 12901e04c3fSmrg const float translate[3], double depthMax ); 1307117f1b4Smrg 1317117f1b4Smrgextern void 1327117f1b4Smrg_math_matrix_set_identity( GLmatrix *dest ); 1337117f1b4Smrg 1347117f1b4Smrgextern void 1357117f1b4Smrg_math_matrix_copy( GLmatrix *to, const GLmatrix *from ); 1367117f1b4Smrg 1377ec681f3Smrgextern void 1387ec681f3Smrg_math_matrix_push_copy(GLmatrix *to, GLmatrix *from); 1397ec681f3Smrg 1407117f1b4Smrgextern void 1417117f1b4Smrg_math_matrix_analyse( GLmatrix *mat ); 1427117f1b4Smrg 1437117f1b4Smrgextern void 1447117f1b4Smrg_math_matrix_print( const GLmatrix *m ); 1457117f1b4Smrg 1467117f1b4Smrgextern GLboolean 1477117f1b4Smrg_math_matrix_is_length_preserving( const GLmatrix *m ); 1487117f1b4Smrg 1497117f1b4Smrgextern GLboolean 1507117f1b4Smrg_math_matrix_has_rotation( const GLmatrix *m ); 1517117f1b4Smrg 1527117f1b4Smrgextern GLboolean 1537117f1b4Smrg_math_matrix_is_general_scale( const GLmatrix *m ); 1547117f1b4Smrg 1557117f1b4Smrgextern GLboolean 1567117f1b4Smrg_math_matrix_is_dirty( const GLmatrix *m ); 1577117f1b4Smrg 1587117f1b4Smrg 1597117f1b4Smrg/** 1607117f1b4Smrg * \name Related functions that don't actually operate on GLmatrix structs 1617117f1b4Smrg */ 1627117f1b4Smrg/*@{*/ 1637117f1b4Smrg 1647117f1b4Smrgextern void 1657117f1b4Smrg_math_transposef( GLfloat to[16], const GLfloat from[16] ); 1667117f1b4Smrg 1677117f1b4Smrgextern void 1687117f1b4Smrg_math_transposed( GLdouble to[16], const GLdouble from[16] ); 1697117f1b4Smrg 1707117f1b4Smrgextern void 1717117f1b4Smrg_math_transposefd( GLfloat to[16], const GLdouble from[16] ); 1727117f1b4Smrg 1737117f1b4Smrg 1747117f1b4Smrg/* 1757117f1b4Smrg * Transform a point (column vector) by a matrix: Q = M * P 1767117f1b4Smrg */ 1777117f1b4Smrg#define TRANSFORM_POINT( Q, M, P ) \ 1787117f1b4Smrg Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] * P[2] + M[12] * P[3]; \ 1797117f1b4Smrg Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] * P[2] + M[13] * P[3]; \ 1807117f1b4Smrg Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14] * P[3]; \ 1817117f1b4Smrg Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15] * P[3]; 1827117f1b4Smrg 1837117f1b4Smrg 1847117f1b4Smrg#define TRANSFORM_POINT3( Q, M, P ) \ 1857117f1b4Smrg Q[0] = M[0] * P[0] + M[4] * P[1] + M[8] * P[2] + M[12]; \ 1867117f1b4Smrg Q[1] = M[1] * P[0] + M[5] * P[1] + M[9] * P[2] + M[13]; \ 1877117f1b4Smrg Q[2] = M[2] * P[0] + M[6] * P[1] + M[10] * P[2] + M[14]; \ 1887117f1b4Smrg Q[3] = M[3] * P[0] + M[7] * P[1] + M[11] * P[2] + M[15]; 1897117f1b4Smrg 1907117f1b4Smrg 1917117f1b4Smrg/* 1927117f1b4Smrg * Transform a normal (row vector) by a matrix: [NX NY NZ] = N * MAT 1937117f1b4Smrg */ 1947117f1b4Smrg#define TRANSFORM_NORMAL( TO, N, MAT ) \ 1957117f1b4Smrgdo { \ 1967117f1b4Smrg TO[0] = N[0] * MAT[0] + N[1] * MAT[1] + N[2] * MAT[2]; \ 1977117f1b4Smrg TO[1] = N[0] * MAT[4] + N[1] * MAT[5] + N[2] * MAT[6]; \ 1987117f1b4Smrg TO[2] = N[0] * MAT[8] + N[1] * MAT[9] + N[2] * MAT[10]; \ 1997117f1b4Smrg} while (0) 2007117f1b4Smrg 2017117f1b4Smrg 202c1f859d4Smrg/** 203c1f859d4Smrg * Transform a direction by a matrix. 204c1f859d4Smrg */ 205c1f859d4Smrg#define TRANSFORM_DIRECTION( TO, DIR, MAT ) \ 206c1f859d4Smrgdo { \ 207c1f859d4Smrg TO[0] = DIR[0] * MAT[0] + DIR[1] * MAT[4] + DIR[2] * MAT[8]; \ 208c1f859d4Smrg TO[1] = DIR[0] * MAT[1] + DIR[1] * MAT[5] + DIR[2] * MAT[9]; \ 209c1f859d4Smrg TO[2] = DIR[0] * MAT[2] + DIR[1] * MAT[6] + DIR[2] * MAT[10];\ 210c1f859d4Smrg} while (0) 211c1f859d4Smrg 212c1f859d4Smrg 2134a49301eSmrgextern void 2144a49301eSmrg_mesa_transform_vector(GLfloat u[4], const GLfloat v[4], const GLfloat m[16]); 2154a49301eSmrg 216c1f859d4Smrg 2177117f1b4Smrg/*@}*/ 2187117f1b4Smrg 2197117f1b4Smrg 220af69d88dSmrg#ifdef __cplusplus 221af69d88dSmrg} 222af69d88dSmrg#endif 223af69d88dSmrg 2247117f1b4Smrg#endif 225