Brian Paul | 27558a1 | 2003-03-01 01:50:20 +0000 | [diff] [blame^] | 1 | /* $Id: m_matrix.c,v 1.16 2003/03/01 01:50:24 brianp Exp $ */ |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Mesa 3-D graphics library |
Brian Paul | 1e091f4 | 2003-01-08 16:42:47 +0000 | [diff] [blame] | 5 | * Version: 5.1 |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 6 | * |
Brian Paul | 1e091f4 | 2003-01-08 16:42:47 +0000 | [diff] [blame] | 7 | * Copyright (C) 1999-2003 Brian Paul All Rights Reserved. |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 8 | * |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 10 | * copy of this software and associated documentation files (the "Software"), |
| 11 | * to deal in the Software without restriction, including without limitation |
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | * and/or sell copies of the Software, and to permit persons to whom the |
| 14 | * Software is furnished to do so, subject to the following conditions: |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 15 | * |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 16 | * The above copyright notice and this permission notice shall be included |
| 17 | * in all copies or substantial portions of the Software. |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 18 | * |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 23 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | /* |
| 29 | * Matrix operations |
| 30 | * |
| 31 | * NOTES: |
| 32 | * 1. 4x4 transformation matrices are stored in memory in column major order. |
| 33 | * 2. Points/vertices are to be thought of as column vectors. |
| 34 | * 3. Transformation of a point p by a matrix M is: p' = M * p |
| 35 | */ |
| 36 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 37 | #include "glheader.h" |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 38 | #include "imports.h" |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 39 | #include "macros.h" |
Brian Paul | 3c63452 | 2002-10-24 23:57:19 +0000 | [diff] [blame] | 40 | #include "imports.h" |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 41 | |
| 42 | #include "m_matrix.h" |
| 43 | |
Keith Whitwell | f4b02d1 | 2001-01-05 05:31:42 +0000 | [diff] [blame] | 44 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 45 | static const char *types[] = { |
| 46 | "MATRIX_GENERAL", |
| 47 | "MATRIX_IDENTITY", |
| 48 | "MATRIX_3D_NO_ROT", |
| 49 | "MATRIX_PERSPECTIVE", |
| 50 | "MATRIX_2D", |
| 51 | "MATRIX_2D_NO_ROT", |
| 52 | "MATRIX_3D" |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | static GLfloat Identity[16] = { |
| 57 | 1.0, 0.0, 0.0, 0.0, |
| 58 | 0.0, 1.0, 0.0, 0.0, |
| 59 | 0.0, 0.0, 1.0, 0.0, |
| 60 | 0.0, 0.0, 0.0, 1.0 |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | /* |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 67 | * This matmul was contributed by Thomas Malik |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 68 | * |
| 69 | * Perform a 4x4 matrix multiplication (product = a x b). |
| 70 | * Input: a, b - matrices to multiply |
| 71 | * Output: product - product of a and b |
| 72 | * WARNING: (product != b) assumed |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 73 | * NOTE: (product == a) allowed |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 74 | * |
| 75 | * KW: 4*16 = 64 muls |
| 76 | */ |
| 77 | #define A(row,col) a[(col<<2)+row] |
| 78 | #define B(row,col) b[(col<<2)+row] |
| 79 | #define P(row,col) product[(col<<2)+row] |
| 80 | |
| 81 | static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b ) |
| 82 | { |
| 83 | GLint i; |
| 84 | for (i = 0; i < 4; i++) { |
| 85 | const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); |
| 86 | P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); |
| 87 | P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); |
| 88 | P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); |
| 89 | P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /* Multiply two matrices known to occupy only the top three rows, such |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 95 | * as typical model matrices, and ortho matrices. |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 96 | */ |
| 97 | static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b ) |
| 98 | { |
| 99 | GLint i; |
| 100 | for (i = 0; i < 3; i++) { |
| 101 | const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); |
| 102 | P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0); |
| 103 | P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1); |
| 104 | P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2); |
| 105 | P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3; |
| 106 | } |
| 107 | P(3,0) = 0; |
| 108 | P(3,1) = 0; |
| 109 | P(3,2) = 0; |
| 110 | P(3,3) = 1; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | #undef A |
| 115 | #undef B |
| 116 | #undef P |
| 117 | |
| 118 | |
| 119 | /* |
| 120 | * Multiply a matrix by an array of floats with known properties. |
| 121 | */ |
| 122 | static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags ) |
| 123 | { |
| 124 | mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE); |
| 125 | |
| 126 | if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) |
| 127 | matmul34( mat->m, mat->m, m ); |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 128 | else |
| 129 | matmul4( mat->m, mat->m, m ); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | static void print_matrix_floats( const GLfloat m[16] ) |
| 134 | { |
| 135 | int i; |
| 136 | for (i=0;i<4;i++) { |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 137 | _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] ); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 141 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 142 | _math_matrix_print( const GLmatrix *m ) |
| 143 | { |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 144 | _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 145 | print_matrix_floats(m->m); |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 146 | _mesa_debug(NULL, "Inverse: \n"); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 147 | if (m->inv) { |
| 148 | GLfloat prod[16]; |
| 149 | print_matrix_floats(m->inv); |
| 150 | matmul4(prod, m->m, m->inv); |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 151 | _mesa_debug(NULL, "Mat * Inverse:\n"); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 152 | print_matrix_floats(prod); |
| 153 | } |
| 154 | else { |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 155 | _mesa_debug(NULL, " - not available\n"); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; } |
| 163 | #define MAT(m,r,c) (m)[(c)*4+(r)] |
| 164 | |
| 165 | /* |
| 166 | * Compute inverse of 4x4 transformation matrix. |
| 167 | * Code contributed by Jacques Leroy jle@star.be |
| 168 | * Return GL_TRUE for success, GL_FALSE for failure (singular matrix) |
| 169 | */ |
| 170 | static GLboolean invert_matrix_general( GLmatrix *mat ) |
| 171 | { |
| 172 | const GLfloat *m = mat->m; |
| 173 | GLfloat *out = mat->inv; |
| 174 | GLfloat wtmp[4][8]; |
| 175 | GLfloat m0, m1, m2, m3, s; |
| 176 | GLfloat *r0, *r1, *r2, *r3; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 177 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 178 | r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3]; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 179 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 180 | r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1), |
| 181 | r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3), |
| 182 | r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0, |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 183 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 184 | r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1), |
| 185 | r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3), |
| 186 | r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0, |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 187 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 188 | r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1), |
| 189 | r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3), |
| 190 | r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0, |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 191 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 192 | r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1), |
| 193 | r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3), |
| 194 | r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 195 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 196 | /* choose pivot - or die */ |
| 197 | if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2); |
| 198 | if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1); |
| 199 | if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0); |
| 200 | if (0.0 == r0[0]) return GL_FALSE; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 201 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 202 | /* eliminate first variable */ |
| 203 | m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0]; |
| 204 | s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s; |
| 205 | s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s; |
| 206 | s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s; |
| 207 | s = r0[4]; |
| 208 | if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; } |
| 209 | s = r0[5]; |
| 210 | if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; } |
| 211 | s = r0[6]; |
| 212 | if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; } |
| 213 | s = r0[7]; |
| 214 | if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; } |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 215 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 216 | /* choose pivot - or die */ |
| 217 | if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2); |
| 218 | if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1); |
| 219 | if (0.0 == r1[1]) return GL_FALSE; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 220 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 221 | /* eliminate second variable */ |
| 222 | m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1]; |
| 223 | r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2]; |
| 224 | r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3]; |
| 225 | s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; } |
| 226 | s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; } |
| 227 | s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; } |
| 228 | s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; } |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 229 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 230 | /* choose pivot - or die */ |
| 231 | if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2); |
| 232 | if (0.0 == r2[2]) return GL_FALSE; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 233 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 234 | /* eliminate third variable */ |
| 235 | m3 = r3[2]/r2[2]; |
| 236 | r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4], |
| 237 | r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], |
| 238 | r3[7] -= m3 * r2[7]; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 239 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 240 | /* last check */ |
| 241 | if (0.0 == r3[3]) return GL_FALSE; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 242 | |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 243 | s = 1.0F/r3[3]; /* now back substitute row 3 */ |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 244 | r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 245 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 246 | m2 = r2[3]; /* now back substitute row 2 */ |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 247 | s = 1.0F/r2[2]; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 248 | r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2), |
| 249 | r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2); |
| 250 | m1 = r1[3]; |
| 251 | r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1, |
| 252 | r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1; |
| 253 | m0 = r0[3]; |
| 254 | r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0, |
| 255 | r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 256 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 257 | m1 = r1[2]; /* now back substitute row 1 */ |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 258 | s = 1.0F/r1[1]; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 259 | r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1), |
| 260 | r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1); |
| 261 | m0 = r0[2]; |
| 262 | r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0, |
| 263 | r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 264 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 265 | m0 = r0[1]; /* now back substitute row 0 */ |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 266 | s = 1.0F/r0[0]; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 267 | r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0), |
| 268 | r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0); |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 269 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 270 | MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5], |
| 271 | MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7], |
| 272 | MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5], |
| 273 | MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7], |
| 274 | MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5], |
| 275 | MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7], |
| 276 | MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5], |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 277 | MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; |
| 278 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 279 | return GL_TRUE; |
| 280 | } |
| 281 | #undef SWAP_ROWS |
| 282 | |
| 283 | |
| 284 | /* Adapted from graphics gems II. |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 285 | */ |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 286 | static GLboolean invert_matrix_3d_general( GLmatrix *mat ) |
| 287 | { |
| 288 | const GLfloat *in = mat->m; |
| 289 | GLfloat *out = mat->inv; |
| 290 | GLfloat pos, neg, t; |
| 291 | GLfloat det; |
| 292 | |
| 293 | /* Calculate the determinant of upper left 3x3 submatrix and |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 294 | * determine if the matrix is singular. |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 295 | */ |
| 296 | pos = neg = 0.0; |
| 297 | t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2); |
| 298 | if (t >= 0.0) pos += t; else neg += t; |
| 299 | |
| 300 | t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2); |
| 301 | if (t >= 0.0) pos += t; else neg += t; |
| 302 | |
| 303 | t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2); |
| 304 | if (t >= 0.0) pos += t; else neg += t; |
| 305 | |
| 306 | t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2); |
| 307 | if (t >= 0.0) pos += t; else neg += t; |
| 308 | |
| 309 | t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2); |
| 310 | if (t >= 0.0) pos += t; else neg += t; |
| 311 | |
| 312 | t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2); |
| 313 | if (t >= 0.0) pos += t; else neg += t; |
| 314 | |
| 315 | det = pos + neg; |
| 316 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 317 | if (det*det < 1e-25) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 318 | return GL_FALSE; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 319 | |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 320 | det = 1.0F / det; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 321 | MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det); |
| 322 | MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det); |
| 323 | MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det); |
| 324 | MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det); |
| 325 | MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det); |
| 326 | MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det); |
| 327 | MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det); |
| 328 | MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det); |
| 329 | MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det); |
| 330 | |
| 331 | /* Do the translation part */ |
| 332 | MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + |
| 333 | MAT(in,1,3) * MAT(out,0,1) + |
| 334 | MAT(in,2,3) * MAT(out,0,2) ); |
| 335 | MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + |
| 336 | MAT(in,1,3) * MAT(out,1,1) + |
| 337 | MAT(in,2,3) * MAT(out,1,2) ); |
| 338 | MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + |
| 339 | MAT(in,1,3) * MAT(out,2,1) + |
| 340 | MAT(in,2,3) * MAT(out,2,2) ); |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 341 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 342 | return GL_TRUE; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | static GLboolean invert_matrix_3d( GLmatrix *mat ) |
| 347 | { |
| 348 | const GLfloat *in = mat->m; |
| 349 | GLfloat *out = mat->inv; |
| 350 | |
| 351 | if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) { |
| 352 | return invert_matrix_3d_general( mat ); |
| 353 | } |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 354 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 355 | if (mat->flags & MAT_FLAG_UNIFORM_SCALE) { |
| 356 | GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) + |
| 357 | MAT(in,0,1) * MAT(in,0,1) + |
| 358 | MAT(in,0,2) * MAT(in,0,2)); |
| 359 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 360 | if (scale == 0.0) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 361 | return GL_FALSE; |
| 362 | |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 363 | scale = 1.0F / scale; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 364 | |
| 365 | /* Transpose and scale the 3 by 3 upper-left submatrix. */ |
| 366 | MAT(out,0,0) = scale * MAT(in,0,0); |
| 367 | MAT(out,1,0) = scale * MAT(in,0,1); |
| 368 | MAT(out,2,0) = scale * MAT(in,0,2); |
| 369 | MAT(out,0,1) = scale * MAT(in,1,0); |
| 370 | MAT(out,1,1) = scale * MAT(in,1,1); |
| 371 | MAT(out,2,1) = scale * MAT(in,1,2); |
| 372 | MAT(out,0,2) = scale * MAT(in,2,0); |
| 373 | MAT(out,1,2) = scale * MAT(in,2,1); |
| 374 | MAT(out,2,2) = scale * MAT(in,2,2); |
| 375 | } |
| 376 | else if (mat->flags & MAT_FLAG_ROTATION) { |
| 377 | /* Transpose the 3 by 3 upper-left submatrix. */ |
| 378 | MAT(out,0,0) = MAT(in,0,0); |
| 379 | MAT(out,1,0) = MAT(in,0,1); |
| 380 | MAT(out,2,0) = MAT(in,0,2); |
| 381 | MAT(out,0,1) = MAT(in,1,0); |
| 382 | MAT(out,1,1) = MAT(in,1,1); |
| 383 | MAT(out,2,1) = MAT(in,1,2); |
| 384 | MAT(out,0,2) = MAT(in,2,0); |
| 385 | MAT(out,1,2) = MAT(in,2,1); |
| 386 | MAT(out,2,2) = MAT(in,2,2); |
| 387 | } |
| 388 | else { |
| 389 | /* pure translation */ |
| 390 | MEMCPY( out, Identity, sizeof(Identity) ); |
| 391 | MAT(out,0,3) = - MAT(in,0,3); |
| 392 | MAT(out,1,3) = - MAT(in,1,3); |
| 393 | MAT(out,2,3) = - MAT(in,2,3); |
| 394 | return GL_TRUE; |
| 395 | } |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 396 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 397 | if (mat->flags & MAT_FLAG_TRANSLATION) { |
| 398 | /* Do the translation part */ |
| 399 | MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + |
| 400 | MAT(in,1,3) * MAT(out,0,1) + |
| 401 | MAT(in,2,3) * MAT(out,0,2) ); |
| 402 | MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + |
| 403 | MAT(in,1,3) * MAT(out,1,1) + |
| 404 | MAT(in,2,3) * MAT(out,1,2) ); |
| 405 | MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + |
| 406 | MAT(in,1,3) * MAT(out,2,1) + |
| 407 | MAT(in,2,3) * MAT(out,2,2) ); |
| 408 | } |
| 409 | else { |
| 410 | MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0; |
| 411 | } |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 412 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 413 | return GL_TRUE; |
| 414 | } |
| 415 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 416 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 417 | |
| 418 | static GLboolean invert_matrix_identity( GLmatrix *mat ) |
| 419 | { |
| 420 | MEMCPY( mat->inv, Identity, sizeof(Identity) ); |
| 421 | return GL_TRUE; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat ) |
| 426 | { |
| 427 | const GLfloat *in = mat->m; |
| 428 | GLfloat *out = mat->inv; |
| 429 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 430 | if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 ) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 431 | return GL_FALSE; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 432 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 433 | MEMCPY( out, Identity, 16 * sizeof(GLfloat) ); |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 434 | MAT(out,0,0) = 1.0F / MAT(in,0,0); |
| 435 | MAT(out,1,1) = 1.0F / MAT(in,1,1); |
| 436 | MAT(out,2,2) = 1.0F / MAT(in,2,2); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 437 | |
| 438 | if (mat->flags & MAT_FLAG_TRANSLATION) { |
| 439 | MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); |
| 440 | MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); |
| 441 | MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2)); |
| 442 | } |
| 443 | |
| 444 | return GL_TRUE; |
| 445 | } |
| 446 | |
| 447 | |
| 448 | static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat ) |
| 449 | { |
| 450 | const GLfloat *in = mat->m; |
| 451 | GLfloat *out = mat->inv; |
| 452 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 453 | if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 454 | return GL_FALSE; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 455 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 456 | MEMCPY( out, Identity, 16 * sizeof(GLfloat) ); |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 457 | MAT(out,0,0) = 1.0F / MAT(in,0,0); |
| 458 | MAT(out,1,1) = 1.0F / MAT(in,1,1); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 459 | |
| 460 | if (mat->flags & MAT_FLAG_TRANSLATION) { |
| 461 | MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); |
| 462 | MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); |
| 463 | } |
| 464 | |
| 465 | return GL_TRUE; |
| 466 | } |
| 467 | |
| 468 | |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 469 | #if 0 |
| 470 | /* broken */ |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 471 | static GLboolean invert_matrix_perspective( GLmatrix *mat ) |
| 472 | { |
| 473 | const GLfloat *in = mat->m; |
| 474 | GLfloat *out = mat->inv; |
| 475 | |
| 476 | if (MAT(in,2,3) == 0) |
| 477 | return GL_FALSE; |
| 478 | |
| 479 | MEMCPY( out, Identity, 16 * sizeof(GLfloat) ); |
| 480 | |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 481 | MAT(out,0,0) = 1.0F / MAT(in,0,0); |
| 482 | MAT(out,1,1) = 1.0F / MAT(in,1,1); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 483 | |
| 484 | MAT(out,0,3) = MAT(in,0,2); |
| 485 | MAT(out,1,3) = MAT(in,1,2); |
| 486 | |
| 487 | MAT(out,2,2) = 0; |
| 488 | MAT(out,2,3) = -1; |
| 489 | |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 490 | MAT(out,3,2) = 1.0F / MAT(in,2,3); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 491 | MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2); |
| 492 | |
| 493 | return GL_TRUE; |
| 494 | } |
Brian Paul | 4e9676f | 2002-06-29 19:48:15 +0000 | [diff] [blame] | 495 | #endif |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 496 | |
| 497 | |
| 498 | typedef GLboolean (*inv_mat_func)( GLmatrix *mat ); |
| 499 | |
| 500 | |
| 501 | static inv_mat_func inv_mat_tab[7] = { |
| 502 | invert_matrix_general, |
| 503 | invert_matrix_identity, |
| 504 | invert_matrix_3d_no_rot, |
Brian Paul | a68b8df | 2002-03-29 17:18:08 +0000 | [diff] [blame] | 505 | #if 0 |
| 506 | /* Don't use this function for now - it fails when the projection matrix |
| 507 | * is premultiplied by a translation (ala Chromium's tilesort SPU). |
| 508 | */ |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 509 | invert_matrix_perspective, |
Brian Paul | a68b8df | 2002-03-29 17:18:08 +0000 | [diff] [blame] | 510 | #else |
| 511 | invert_matrix_general, |
| 512 | #endif |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 513 | invert_matrix_3d, /* lazy! */ |
| 514 | invert_matrix_2d_no_rot, |
| 515 | invert_matrix_3d |
| 516 | }; |
| 517 | |
| 518 | |
| 519 | static GLboolean matrix_invert( GLmatrix *mat ) |
| 520 | { |
| 521 | if (inv_mat_tab[mat->type](mat)) { |
| 522 | mat->flags &= ~MAT_FLAG_SINGULAR; |
| 523 | return GL_TRUE; |
| 524 | } else { |
| 525 | mat->flags |= MAT_FLAG_SINGULAR; |
| 526 | MEMCPY( mat->inv, Identity, sizeof(Identity) ); |
| 527 | return GL_FALSE; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 528 | } |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | /* |
| 537 | * Generate a 4x4 transformation matrix from glRotate parameters, and |
| 538 | * postmultiply the input matrix by it. |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 539 | * This function contributed by Erich Boleyn (erich@uruk.org). |
| 540 | * Optimizatios contributed by Rudolf Opalla (rudi@khm.de). |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 541 | */ |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 542 | void |
| 543 | _math_matrix_rotate( GLmatrix *mat, |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 544 | GLfloat angle, GLfloat x, GLfloat y, GLfloat z ) |
| 545 | { |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 546 | GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 547 | GLfloat m[16]; |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 548 | GLboolean optimized; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 549 | |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 550 | s = (GLfloat) sin( angle * DEG2RAD ); |
| 551 | c = (GLfloat) cos( angle * DEG2RAD ); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 552 | |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 553 | MEMCPY(m, Identity, sizeof(GLfloat)*16); |
| 554 | optimized = GL_FALSE; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 555 | |
| 556 | #define M(row,col) m[col*4+row] |
| 557 | |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 558 | if (x == 0.0F) { |
| 559 | if (y == 0.0F) { |
| 560 | if (z != 0.0F) { |
| 561 | optimized = GL_TRUE; |
| 562 | /* rotate only around z-axis */ |
| 563 | M(0,0) = c; |
| 564 | M(1,1) = c; |
| 565 | if (z < 0.0F) { |
| 566 | M(0,1) = s; |
| 567 | M(1,0) = -s; |
| 568 | } |
| 569 | else { |
| 570 | M(0,1) = -s; |
| 571 | M(1,0) = s; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | else if (z == 0.0F) { |
| 576 | optimized = GL_TRUE; |
| 577 | /* rotate only around y-axis */ |
| 578 | M(0,0) = c; |
| 579 | M(2,2) = c; |
| 580 | if (y < 0.0F) { |
| 581 | M(0,2) = -s; |
| 582 | M(2,0) = s; |
| 583 | } |
| 584 | else { |
| 585 | M(0,2) = s; |
| 586 | M(2,0) = -s; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | else if (y == 0.0F) { |
| 591 | if (z == 0.0F) { |
| 592 | optimized = GL_TRUE; |
| 593 | /* rotate only around x-axis */ |
| 594 | M(1,1) = c; |
| 595 | M(2,2) = c; |
Brian Paul | 1e091f4 | 2003-01-08 16:42:47 +0000 | [diff] [blame] | 596 | if (x < 0.0F) { |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 597 | M(1,2) = s; |
| 598 | M(2,1) = -s; |
| 599 | } |
| 600 | else { |
| 601 | M(1,2) = -s; |
| 602 | M(2,1) = s; |
| 603 | } |
| 604 | } |
| 605 | } |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 606 | |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 607 | if (!optimized) { |
Brian Paul | 27558a1 | 2003-03-01 01:50:20 +0000 | [diff] [blame^] | 608 | const GLfloat mag = SQRTF(x * x + y * y + z * z); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 609 | |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 610 | if (mag <= 1.0e-4) { |
| 611 | /* no rotation, leave mat as-is */ |
| 612 | return; |
| 613 | } |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 614 | |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 615 | x /= mag; |
| 616 | y /= mag; |
| 617 | z /= mag; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 618 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 619 | |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 620 | /* |
| 621 | * Arbitrary axis rotation matrix. |
| 622 | * |
| 623 | * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied |
| 624 | * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation |
| 625 | * (which is about the X-axis), and the two composite transforms |
| 626 | * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary |
| 627 | * from the arbitrary axis to the X-axis then back. They are |
| 628 | * all elementary rotations. |
| 629 | * |
| 630 | * Rz' is a rotation about the Z-axis, to bring the axis vector |
| 631 | * into the x-z plane. Then Ry' is applied, rotating about the |
| 632 | * Y-axis to bring the axis vector parallel with the X-axis. The |
| 633 | * rotation about the X-axis is then performed. Ry and Rz are |
| 634 | * simply the respective inverse transforms to bring the arbitrary |
| 635 | * axis back to it's original orientation. The first transforms |
| 636 | * Rz' and Ry' are considered inverses, since the data from the |
| 637 | * arbitrary axis gives you info on how to get to it, not how |
| 638 | * to get away from it, and an inverse must be applied. |
| 639 | * |
| 640 | * The basic calculation used is to recognize that the arbitrary |
| 641 | * axis vector (x, y, z), since it is of unit length, actually |
| 642 | * represents the sines and cosines of the angles to rotate the |
| 643 | * X-axis to the same orientation, with theta being the angle about |
| 644 | * Z and phi the angle about Y (in the order described above) |
| 645 | * as follows: |
| 646 | * |
| 647 | * cos ( theta ) = x / sqrt ( 1 - z^2 ) |
| 648 | * sin ( theta ) = y / sqrt ( 1 - z^2 ) |
| 649 | * |
| 650 | * cos ( phi ) = sqrt ( 1 - z^2 ) |
| 651 | * sin ( phi ) = z |
| 652 | * |
| 653 | * Note that cos ( phi ) can further be inserted to the above |
| 654 | * formulas: |
| 655 | * |
| 656 | * cos ( theta ) = x / cos ( phi ) |
| 657 | * sin ( theta ) = y / sin ( phi ) |
| 658 | * |
| 659 | * ...etc. Because of those relations and the standard trigonometric |
| 660 | * relations, it is pssible to reduce the transforms down to what |
| 661 | * is used below. It may be that any primary axis chosen will give the |
| 662 | * same results (modulo a sign convention) using thie method. |
| 663 | * |
| 664 | * Particularly nice is to notice that all divisions that might |
| 665 | * have caused trouble when parallel to certain planes or |
| 666 | * axis go away with care paid to reducing the expressions. |
| 667 | * After checking, it does perform correctly under all cases, since |
| 668 | * in all the cases of division where the denominator would have |
| 669 | * been zero, the numerator would have been zero as well, giving |
| 670 | * the expected result. |
| 671 | */ |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 672 | |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 673 | xx = x * x; |
| 674 | yy = y * y; |
| 675 | zz = z * z; |
| 676 | xy = x * y; |
| 677 | yz = y * z; |
| 678 | zx = z * x; |
| 679 | xs = x * s; |
| 680 | ys = y * s; |
| 681 | zs = z * s; |
| 682 | one_c = 1.0F - c; |
| 683 | |
| 684 | /* We already hold the identity-matrix so we can skip some statements */ |
| 685 | M(0,0) = (one_c * xx) + c; |
| 686 | M(0,1) = (one_c * xy) - zs; |
| 687 | M(0,2) = (one_c * zx) + ys; |
| 688 | /* M(0,3) = 0.0F; */ |
| 689 | |
| 690 | M(1,0) = (one_c * xy) + zs; |
| 691 | M(1,1) = (one_c * yy) + c; |
| 692 | M(1,2) = (one_c * yz) - xs; |
| 693 | /* M(1,3) = 0.0F; */ |
| 694 | |
| 695 | M(2,0) = (one_c * zx) - ys; |
| 696 | M(2,1) = (one_c * yz) + xs; |
| 697 | M(2,2) = (one_c * zz) + c; |
| 698 | /* M(2,3) = 0.0F; */ |
| 699 | |
| 700 | /* |
| 701 | M(3,0) = 0.0F; |
| 702 | M(3,1) = 0.0F; |
| 703 | M(3,2) = 0.0F; |
| 704 | M(3,3) = 1.0F; |
| 705 | */ |
| 706 | } |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 707 | #undef M |
| 708 | |
| 709 | matrix_multf( mat, m, MAT_FLAG_ROTATION ); |
| 710 | } |
| 711 | |
| 712 | |
Brian Paul | 4991d0f | 2002-09-12 16:26:04 +0000 | [diff] [blame] | 713 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 714 | void |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 715 | _math_matrix_frustum( GLmatrix *mat, |
Brian Paul | d8bc5a9 | 2001-02-05 18:48:52 +0000 | [diff] [blame] | 716 | GLfloat left, GLfloat right, |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 717 | GLfloat bottom, GLfloat top, |
Brian Paul | d8bc5a9 | 2001-02-05 18:48:52 +0000 | [diff] [blame] | 718 | GLfloat nearval, GLfloat farval ) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 719 | { |
| 720 | GLfloat x, y, a, b, c, d; |
| 721 | GLfloat m[16]; |
| 722 | |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 723 | x = (2.0F*nearval) / (right-left); |
| 724 | y = (2.0F*nearval) / (top-bottom); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 725 | a = (right+left) / (right-left); |
| 726 | b = (top+bottom) / (top-bottom); |
| 727 | c = -(farval+nearval) / ( farval-nearval); |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 728 | d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */ |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 729 | |
| 730 | #define M(row,col) m[col*4+row] |
| 731 | M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F; |
| 732 | M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F; |
| 733 | M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d; |
| 734 | M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F; |
| 735 | #undef M |
| 736 | |
| 737 | matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE ); |
| 738 | } |
| 739 | |
| 740 | void |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 741 | _math_matrix_ortho( GLmatrix *mat, |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 742 | GLfloat left, GLfloat right, |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 743 | GLfloat bottom, GLfloat top, |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 744 | GLfloat nearval, GLfloat farval ) |
| 745 | { |
| 746 | GLfloat x, y, z; |
| 747 | GLfloat tx, ty, tz; |
| 748 | GLfloat m[16]; |
| 749 | |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 750 | x = 2.0F / (right-left); |
| 751 | y = 2.0F / (top-bottom); |
| 752 | z = -2.0F / (farval-nearval); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 753 | tx = -(right+left) / (right-left); |
| 754 | ty = -(top+bottom) / (top-bottom); |
| 755 | tz = -(farval+nearval) / (farval-nearval); |
| 756 | |
| 757 | #define M(row,col) m[col*4+row] |
| 758 | M(0,0) = x; M(0,1) = 0.0F; M(0,2) = 0.0F; M(0,3) = tx; |
| 759 | M(1,0) = 0.0F; M(1,1) = y; M(1,2) = 0.0F; M(1,3) = ty; |
| 760 | M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = z; M(2,3) = tz; |
| 761 | M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = 0.0F; M(3,3) = 1.0F; |
| 762 | #undef M |
| 763 | |
| 764 | matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION)); |
| 765 | } |
| 766 | |
| 767 | |
| 768 | #define ZERO(x) (1<<x) |
| 769 | #define ONE(x) (1<<(x+16)) |
| 770 | |
| 771 | #define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14)) |
| 772 | #define MASK_NO_2D_SCALE ( ONE(0) | ONE(5)) |
| 773 | |
| 774 | #define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\ |
| 775 | ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\ |
| 776 | ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ |
| 777 | ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) |
| 778 | |
| 779 | #define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \ |
| 780 | ZERO(1) | ZERO(9) | \ |
| 781 | ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ |
| 782 | ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) |
| 783 | |
| 784 | #define MASK_2D ( ZERO(8) | \ |
| 785 | ZERO(9) | \ |
| 786 | ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\ |
| 787 | ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) |
| 788 | |
| 789 | |
| 790 | #define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \ |
| 791 | ZERO(1) | ZERO(9) | \ |
| 792 | ZERO(2) | ZERO(6) | \ |
| 793 | ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) |
| 794 | |
| 795 | #define MASK_3D ( \ |
| 796 | \ |
| 797 | \ |
| 798 | ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) ) |
| 799 | |
| 800 | |
| 801 | #define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\ |
| 802 | ZERO(1) | ZERO(13) |\ |
| 803 | ZERO(2) | ZERO(6) | \ |
| 804 | ZERO(3) | ZERO(7) | ZERO(15) ) |
| 805 | |
| 806 | #define SQ(x) ((x)*(x)) |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 807 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 808 | /* Determine type and flags from scratch. This is expensive enough to |
| 809 | * only want to do it once. |
| 810 | */ |
Keith Whitwell | ad2ac21 | 2000-11-24 10:25:05 +0000 | [diff] [blame] | 811 | static void analyse_from_scratch( GLmatrix *mat ) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 812 | { |
| 813 | const GLfloat *m = mat->m; |
| 814 | GLuint mask = 0; |
| 815 | GLuint i; |
| 816 | |
| 817 | for (i = 0 ; i < 16 ; i++) { |
| 818 | if (m[i] == 0.0) mask |= (1<<i); |
| 819 | } |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 820 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 821 | if (m[0] == 1.0F) mask |= (1<<16); |
| 822 | if (m[5] == 1.0F) mask |= (1<<21); |
| 823 | if (m[10] == 1.0F) mask |= (1<<26); |
| 824 | if (m[15] == 1.0F) mask |= (1<<31); |
| 825 | |
| 826 | mat->flags &= ~MAT_FLAGS_GEOMETRY; |
| 827 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 828 | /* Check for translation - no-one really cares |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 829 | */ |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 830 | if ((mask & MASK_NO_TRX) != MASK_NO_TRX) |
| 831 | mat->flags |= MAT_FLAG_TRANSLATION; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 832 | |
| 833 | /* Do the real work |
| 834 | */ |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 835 | if (mask == (GLuint) MASK_IDENTITY) { |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 836 | mat->type = MATRIX_IDENTITY; |
| 837 | } |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 838 | else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) { |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 839 | mat->type = MATRIX_2D_NO_ROT; |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 840 | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 841 | if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE) |
| 842 | mat->flags = MAT_FLAG_GENERAL_SCALE; |
| 843 | } |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 844 | else if ((mask & MASK_2D) == (GLuint) MASK_2D) { |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 845 | GLfloat mm = DOT2(m, m); |
| 846 | GLfloat m4m4 = DOT2(m+4,m+4); |
| 847 | GLfloat mm4 = DOT2(m,m+4); |
| 848 | |
| 849 | mat->type = MATRIX_2D; |
| 850 | |
| 851 | /* Check for scale */ |
| 852 | if (SQ(mm-1) > SQ(1e-6) || |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 853 | SQ(m4m4-1) > SQ(1e-6)) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 854 | mat->flags |= MAT_FLAG_GENERAL_SCALE; |
| 855 | |
| 856 | /* Check for rotation */ |
| 857 | if (SQ(mm4) > SQ(1e-6)) |
| 858 | mat->flags |= MAT_FLAG_GENERAL_3D; |
| 859 | else |
| 860 | mat->flags |= MAT_FLAG_ROTATION; |
| 861 | |
| 862 | } |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 863 | else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) { |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 864 | mat->type = MATRIX_3D_NO_ROT; |
| 865 | |
| 866 | /* Check for scale */ |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 867 | if (SQ(m[0]-m[5]) < SQ(1e-6) && |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 868 | SQ(m[0]-m[10]) < SQ(1e-6)) { |
| 869 | if (SQ(m[0]-1.0) > SQ(1e-6)) { |
| 870 | mat->flags |= MAT_FLAG_UNIFORM_SCALE; |
| 871 | } |
| 872 | } |
| 873 | else { |
| 874 | mat->flags |= MAT_FLAG_GENERAL_SCALE; |
| 875 | } |
| 876 | } |
Brian Paul | b51b0a8 | 2001-03-07 05:06:11 +0000 | [diff] [blame] | 877 | else if ((mask & MASK_3D) == (GLuint) MASK_3D) { |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 878 | GLfloat c1 = DOT3(m,m); |
| 879 | GLfloat c2 = DOT3(m+4,m+4); |
| 880 | GLfloat c3 = DOT3(m+8,m+8); |
| 881 | GLfloat d1 = DOT3(m, m+4); |
| 882 | GLfloat cp[3]; |
| 883 | |
| 884 | mat->type = MATRIX_3D; |
| 885 | |
| 886 | /* Check for scale */ |
| 887 | if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) { |
| 888 | if (SQ(c1-1.0) > SQ(1e-6)) |
| 889 | mat->flags |= MAT_FLAG_UNIFORM_SCALE; |
| 890 | /* else no scale at all */ |
| 891 | } |
| 892 | else { |
| 893 | mat->flags |= MAT_FLAG_GENERAL_SCALE; |
| 894 | } |
| 895 | |
| 896 | /* Check for rotation */ |
| 897 | if (SQ(d1) < SQ(1e-6)) { |
| 898 | CROSS3( cp, m, m+4 ); |
| 899 | SUB_3V( cp, cp, (m+8) ); |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 900 | if (LEN_SQUARED_3FV(cp) < SQ(1e-6)) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 901 | mat->flags |= MAT_FLAG_ROTATION; |
| 902 | else |
| 903 | mat->flags |= MAT_FLAG_GENERAL_3D; |
| 904 | } |
| 905 | else { |
| 906 | mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */ |
| 907 | } |
| 908 | } |
| 909 | else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) { |
| 910 | mat->type = MATRIX_PERSPECTIVE; |
| 911 | mat->flags |= MAT_FLAG_GENERAL; |
| 912 | } |
| 913 | else { |
| 914 | mat->type = MATRIX_GENERAL; |
| 915 | mat->flags |= MAT_FLAG_GENERAL; |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | |
| 920 | /* Analyse a matrix given that its flags are accurate - this is the |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 921 | * more common operation, hopefully. |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 922 | */ |
Keith Whitwell | ad2ac21 | 2000-11-24 10:25:05 +0000 | [diff] [blame] | 923 | static void analyse_from_flags( GLmatrix *mat ) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 924 | { |
| 925 | const GLfloat *m = mat->m; |
| 926 | |
| 927 | if (TEST_MAT_FLAGS(mat, 0)) { |
| 928 | mat->type = MATRIX_IDENTITY; |
| 929 | } |
| 930 | else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION | |
| 931 | MAT_FLAG_UNIFORM_SCALE | |
| 932 | MAT_FLAG_GENERAL_SCALE))) { |
| 933 | if ( m[10]==1.0F && m[14]==0.0F ) { |
| 934 | mat->type = MATRIX_2D_NO_ROT; |
| 935 | } |
| 936 | else { |
| 937 | mat->type = MATRIX_3D_NO_ROT; |
| 938 | } |
| 939 | } |
| 940 | else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) { |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 941 | if ( m[ 8]==0.0F |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 942 | && m[ 9]==0.0F |
| 943 | && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) { |
| 944 | mat->type = MATRIX_2D; |
| 945 | } |
| 946 | else { |
| 947 | mat->type = MATRIX_3D; |
| 948 | } |
| 949 | } |
| 950 | else if ( m[4]==0.0F && m[12]==0.0F |
| 951 | && m[1]==0.0F && m[13]==0.0F |
| 952 | && m[2]==0.0F && m[6]==0.0F |
| 953 | && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) { |
| 954 | mat->type = MATRIX_PERSPECTIVE; |
| 955 | } |
| 956 | else { |
| 957 | mat->type = MATRIX_GENERAL; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 962 | void |
| 963 | _math_matrix_analyse( GLmatrix *mat ) |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 964 | { |
| 965 | if (mat->flags & MAT_DIRTY_TYPE) { |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 966 | if (mat->flags & MAT_DIRTY_FLAGS) |
Keith Whitwell | ad2ac21 | 2000-11-24 10:25:05 +0000 | [diff] [blame] | 967 | analyse_from_scratch( mat ); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 968 | else |
Keith Whitwell | ad2ac21 | 2000-11-24 10:25:05 +0000 | [diff] [blame] | 969 | analyse_from_flags( mat ); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) { |
| 973 | matrix_invert( mat ); |
| 974 | } |
| 975 | |
| 976 | mat->flags &= ~(MAT_DIRTY_FLAGS| |
| 977 | MAT_DIRTY_TYPE| |
| 978 | MAT_DIRTY_INVERSE); |
| 979 | } |
| 980 | |
| 981 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 982 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 983 | _math_matrix_copy( GLmatrix *to, const GLmatrix *from ) |
| 984 | { |
| 985 | MEMCPY( to->m, from->m, sizeof(Identity) ); |
| 986 | to->flags = from->flags; |
| 987 | to->type = from->type; |
| 988 | |
| 989 | if (to->inv != 0) { |
| 990 | if (from->inv == 0) { |
| 991 | matrix_invert( to ); |
| 992 | } |
| 993 | else { |
| 994 | MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1000 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1001 | _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ) |
| 1002 | { |
| 1003 | GLfloat *m = mat->m; |
| 1004 | m[0] *= x; m[4] *= y; m[8] *= z; |
| 1005 | m[1] *= x; m[5] *= y; m[9] *= z; |
| 1006 | m[2] *= x; m[6] *= y; m[10] *= z; |
| 1007 | m[3] *= x; m[7] *= y; m[11] *= z; |
| 1008 | |
| 1009 | if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8) |
| 1010 | mat->flags |= MAT_FLAG_UNIFORM_SCALE; |
| 1011 | else |
| 1012 | mat->flags |= MAT_FLAG_GENERAL_SCALE; |
| 1013 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1014 | mat->flags |= (MAT_DIRTY_TYPE | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1015 | MAT_DIRTY_INVERSE); |
| 1016 | } |
| 1017 | |
| 1018 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1019 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1020 | _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z ) |
| 1021 | { |
| 1022 | GLfloat *m = mat->m; |
| 1023 | m[12] = m[0] * x + m[4] * y + m[8] * z + m[12]; |
| 1024 | m[13] = m[1] * x + m[5] * y + m[9] * z + m[13]; |
| 1025 | m[14] = m[2] * x + m[6] * y + m[10] * z + m[14]; |
| 1026 | m[15] = m[3] * x + m[7] * y + m[11] * z + m[15]; |
| 1027 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1028 | mat->flags |= (MAT_FLAG_TRANSLATION | |
| 1029 | MAT_DIRTY_TYPE | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1030 | MAT_DIRTY_INVERSE); |
| 1031 | } |
| 1032 | |
| 1033 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1034 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1035 | _math_matrix_loadf( GLmatrix *mat, const GLfloat *m ) |
| 1036 | { |
| 1037 | MEMCPY( mat->m, m, 16*sizeof(GLfloat) ); |
| 1038 | mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY); |
| 1039 | } |
| 1040 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1041 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1042 | _math_matrix_ctr( GLmatrix *m ) |
| 1043 | { |
Brian Paul | 30f51ae | 2001-12-18 04:06:44 +0000 | [diff] [blame] | 1044 | m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 ); |
| 1045 | if (m->m) |
| 1046 | MEMCPY( m->m, Identity, sizeof(Identity) ); |
| 1047 | m->inv = NULL; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1048 | m->type = MATRIX_IDENTITY; |
| 1049 | m->flags = 0; |
| 1050 | } |
| 1051 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1052 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1053 | _math_matrix_dtr( GLmatrix *m ) |
| 1054 | { |
Brian Paul | 30f51ae | 2001-12-18 04:06:44 +0000 | [diff] [blame] | 1055 | if (m->m) { |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1056 | ALIGN_FREE( m->m ); |
Brian Paul | 30f51ae | 2001-12-18 04:06:44 +0000 | [diff] [blame] | 1057 | m->m = NULL; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1058 | } |
Brian Paul | 30f51ae | 2001-12-18 04:06:44 +0000 | [diff] [blame] | 1059 | if (m->inv) { |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1060 | ALIGN_FREE( m->inv ); |
Brian Paul | 30f51ae | 2001-12-18 04:06:44 +0000 | [diff] [blame] | 1061 | m->inv = NULL; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1066 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1067 | _math_matrix_alloc_inv( GLmatrix *m ) |
| 1068 | { |
Brian Paul | 30f51ae | 2001-12-18 04:06:44 +0000 | [diff] [blame] | 1069 | if (!m->inv) { |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1070 | m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 ); |
Brian Paul | 30f51ae | 2001-12-18 04:06:44 +0000 | [diff] [blame] | 1071 | if (m->inv) |
| 1072 | MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) ); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1077 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1078 | _math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b ) |
| 1079 | { |
| 1080 | dest->flags = (a->flags | |
| 1081 | b->flags | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1082 | MAT_DIRTY_TYPE | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1083 | MAT_DIRTY_INVERSE); |
| 1084 | |
| 1085 | if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D)) |
| 1086 | matmul34( dest->m, a->m, b->m ); |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1087 | else |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1088 | matmul4( dest->m, a->m, b->m ); |
| 1089 | } |
| 1090 | |
| 1091 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1092 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1093 | _math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m ) |
| 1094 | { |
| 1095 | dest->flags |= (MAT_FLAG_GENERAL | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1096 | MAT_DIRTY_TYPE | |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1097 | MAT_DIRTY_INVERSE); |
| 1098 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1099 | matmul4( dest->m, dest->m, m ); |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1102 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1103 | _math_matrix_set_identity( GLmatrix *mat ) |
| 1104 | { |
| 1105 | MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) ); |
| 1106 | |
| 1107 | if (mat->inv) |
| 1108 | MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) ); |
| 1109 | |
| 1110 | mat->type = MATRIX_IDENTITY; |
| 1111 | mat->flags &= ~(MAT_DIRTY_FLAGS| |
| 1112 | MAT_DIRTY_TYPE| |
| 1113 | MAT_DIRTY_INVERSE); |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1118 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1119 | _math_transposef( GLfloat to[16], const GLfloat from[16] ) |
| 1120 | { |
| 1121 | to[0] = from[0]; |
| 1122 | to[1] = from[4]; |
| 1123 | to[2] = from[8]; |
| 1124 | to[3] = from[12]; |
| 1125 | to[4] = from[1]; |
| 1126 | to[5] = from[5]; |
| 1127 | to[6] = from[9]; |
| 1128 | to[7] = from[13]; |
| 1129 | to[8] = from[2]; |
| 1130 | to[9] = from[6]; |
| 1131 | to[10] = from[10]; |
| 1132 | to[11] = from[14]; |
| 1133 | to[12] = from[3]; |
| 1134 | to[13] = from[7]; |
| 1135 | to[14] = from[11]; |
| 1136 | to[15] = from[15]; |
| 1137 | } |
| 1138 | |
| 1139 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1140 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1141 | _math_transposed( GLdouble to[16], const GLdouble from[16] ) |
| 1142 | { |
| 1143 | to[0] = from[0]; |
| 1144 | to[1] = from[4]; |
| 1145 | to[2] = from[8]; |
| 1146 | to[3] = from[12]; |
| 1147 | to[4] = from[1]; |
| 1148 | to[5] = from[5]; |
| 1149 | to[6] = from[9]; |
| 1150 | to[7] = from[13]; |
| 1151 | to[8] = from[2]; |
| 1152 | to[9] = from[6]; |
| 1153 | to[10] = from[10]; |
| 1154 | to[11] = from[14]; |
| 1155 | to[12] = from[3]; |
| 1156 | to[13] = from[7]; |
| 1157 | to[14] = from[11]; |
| 1158 | to[15] = from[15]; |
| 1159 | } |
| 1160 | |
Gareth Hughes | 22144ab | 2001-03-12 00:48:37 +0000 | [diff] [blame] | 1161 | void |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1162 | _math_transposefd( GLfloat to[16], const GLdouble from[16] ) |
| 1163 | { |
Karl Schultz | 7b9fe82 | 2001-09-18 23:06:14 +0000 | [diff] [blame] | 1164 | to[0] = (GLfloat) from[0]; |
| 1165 | to[1] = (GLfloat) from[4]; |
| 1166 | to[2] = (GLfloat) from[8]; |
| 1167 | to[3] = (GLfloat) from[12]; |
| 1168 | to[4] = (GLfloat) from[1]; |
| 1169 | to[5] = (GLfloat) from[5]; |
| 1170 | to[6] = (GLfloat) from[9]; |
| 1171 | to[7] = (GLfloat) from[13]; |
| 1172 | to[8] = (GLfloat) from[2]; |
| 1173 | to[9] = (GLfloat) from[6]; |
| 1174 | to[10] = (GLfloat) from[10]; |
| 1175 | to[11] = (GLfloat) from[14]; |
| 1176 | to[12] = (GLfloat) from[3]; |
| 1177 | to[13] = (GLfloat) from[7]; |
| 1178 | to[14] = (GLfloat) from[11]; |
| 1179 | to[15] = (GLfloat) from[15]; |
Keith Whitwell | 23caf20 | 2000-11-16 21:05:34 +0000 | [diff] [blame] | 1180 | } |