| 1 | /* $Id: matrix.c,v 1.3 2000-05-23 20:40:40 jeroen Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Mesa 3-D graphics library | 
|---|
| 5 | * Version:  3.3 | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (C) 1999  Brian Paul   All Rights Reserved. | 
|---|
| 8 | * | 
|---|
| 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: | 
|---|
| 15 | * | 
|---|
| 16 | * The above copyright notice and this permission notice shall be included | 
|---|
| 17 | * in all copies or substantial portions of the Software. | 
|---|
| 18 | * | 
|---|
| 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 |  | 
|---|
| 30 |  | 
|---|
| 31 | /* | 
|---|
| 32 | * Matrix operations | 
|---|
| 33 | * | 
|---|
| 34 | * | 
|---|
| 35 | * NOTES: | 
|---|
| 36 | * 1. 4x4 transformation matrices are stored in memory in column major order. | 
|---|
| 37 | * 2. Points/vertices are to be thought of as column vectors. | 
|---|
| 38 | * 3. Transformation of a point p by a matrix M is: p' = M * p | 
|---|
| 39 | * | 
|---|
| 40 | */ | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 | #ifdef PC_HEADER | 
|---|
| 44 | #include "all.h" | 
|---|
| 45 | #else | 
|---|
| 46 | #include "glheader.h" | 
|---|
| 47 | #include "types.h" | 
|---|
| 48 | #include "context.h" | 
|---|
| 49 | #include "enums.h" | 
|---|
| 50 | #include "macros.h" | 
|---|
| 51 | #include "matrix.h" | 
|---|
| 52 | #include "mmath.h" | 
|---|
| 53 | #include "buffers.h" | 
|---|
| 54 | #include "mem.h" | 
|---|
| 55 | #endif | 
|---|
| 56 |  | 
|---|
| 57 | static const char *types[] = { | 
|---|
| 58 | "MATRIX_GENERAL", | 
|---|
| 59 | "MATRIX_IDENTITY", | 
|---|
| 60 | "MATRIX_3D_NO_ROT", | 
|---|
| 61 | "MATRIX_PERSPECTIVE", | 
|---|
| 62 | "MATRIX_2D", | 
|---|
| 63 | "MATRIX_2D_NO_ROT", | 
|---|
| 64 | "MATRIX_3D" | 
|---|
| 65 | }; | 
|---|
| 66 | static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b ); | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | static GLfloat Identity[16] = { | 
|---|
| 70 | 1.0, 0.0, 0.0, 0.0, | 
|---|
| 71 | 0.0, 1.0, 0.0, 0.0, | 
|---|
| 72 | 0.0, 0.0, 1.0, 0.0, | 
|---|
| 73 | 0.0, 0.0, 0.0, 1.0 | 
|---|
| 74 | }; | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | static void print_matrix_floats( const GLfloat m[16] ) | 
|---|
| 78 | { | 
|---|
| 79 | int i; | 
|---|
| 80 | for (i=0;i<4;i++) { | 
|---|
| 81 | fprintf(stderr,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] ); | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void gl_print_matrix( const GLmatrix *m ) | 
|---|
| 86 | { | 
|---|
| 87 | fprintf(stderr, "Matrix type: %s, flags: %x\n", types[m->type], m->flags); | 
|---|
| 88 | print_matrix_floats(m->m); | 
|---|
| 89 | #if 1 | 
|---|
| 90 | fprintf(stderr, "Inverse: \n"); | 
|---|
| 91 | if (m->inv) { | 
|---|
| 92 | GLfloat prod[16]; | 
|---|
| 93 | print_matrix_floats(m->inv); | 
|---|
| 94 | matmul4(prod, m->m, m->inv); | 
|---|
| 95 | fprintf(stderr, "Mat * Inverse:\n"); | 
|---|
| 96 | print_matrix_floats(prod); | 
|---|
| 97 | } else | 
|---|
| 98 | fprintf(stderr, "  - not available\n"); | 
|---|
| 99 | #endif | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 | /* | 
|---|
| 105 | * This matmul was contributed by Thomas Malik | 
|---|
| 106 | * | 
|---|
| 107 | * Perform a 4x4 matrix multiplication  (product = a x b). | 
|---|
| 108 | * Input:  a, b - matrices to multiply | 
|---|
| 109 | * Output:  product - product of a and b | 
|---|
| 110 | * WARNING: (product != b) assumed | 
|---|
| 111 | * NOTE:    (product == a) allowed | 
|---|
| 112 | * | 
|---|
| 113 | * KW: 4*16 = 64 muls | 
|---|
| 114 | */ | 
|---|
| 115 | #define A(row,col)  a[(col<<2)+row] | 
|---|
| 116 | #define B(row,col)  b[(col<<2)+row] | 
|---|
| 117 | #define P(row,col)  product[(col<<2)+row] | 
|---|
| 118 |  | 
|---|
| 119 | static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b ) | 
|---|
| 120 | { | 
|---|
| 121 | GLint i; | 
|---|
| 122 | for (i = 0; i < 4; i++) { | 
|---|
| 123 | GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3); | 
|---|
| 124 | P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); | 
|---|
| 125 | P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); | 
|---|
| 126 | P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); | 
|---|
| 127 | P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | /* Multiply two matrices known to occupy only the top three rows, | 
|---|
| 135 | * such as typical modelling matrices, and ortho matrices. | 
|---|
| 136 | * | 
|---|
| 137 | * KW: 3*9 = 27 muls | 
|---|
| 138 | */ | 
|---|
| 139 | static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b ) | 
|---|
| 140 | { | 
|---|
| 141 | GLint i; | 
|---|
| 142 | for (i = 0; i < 3; i++) { | 
|---|
| 143 | GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3); | 
|---|
| 144 | P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0); | 
|---|
| 145 | P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1); | 
|---|
| 146 | P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2); | 
|---|
| 147 | P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3; | 
|---|
| 148 | } | 
|---|
| 149 | P(3,0) = 0; | 
|---|
| 150 | P(3,1) = 0; | 
|---|
| 151 | P(3,2) = 0; | 
|---|
| 152 | P(3,3) = 1; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | static void matmul4fd( GLfloat *product, const GLfloat *a, const GLdouble *b ) | 
|---|
| 156 | { | 
|---|
| 157 | GLint i; | 
|---|
| 158 | for (i = 0; i < 4; i++) { | 
|---|
| 159 | GLfloat ai0=A(i,0),  ai1=A(i,1),  ai2=A(i,2),  ai3=A(i,3); | 
|---|
| 160 | P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); | 
|---|
| 161 | P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); | 
|---|
| 162 | P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); | 
|---|
| 163 | P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); | 
|---|
| 164 | } | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | #undef A | 
|---|
| 168 | #undef B | 
|---|
| 169 | #undef P | 
|---|
| 170 |  | 
|---|
| 171 |  | 
|---|
| 172 |  | 
|---|
| 173 | #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; } | 
|---|
| 174 | #define MAT(m,r,c) (m)[(c)*4+(r)] | 
|---|
| 175 |  | 
|---|
| 176 | /* | 
|---|
| 177 | * Compute inverse of 4x4 transformation matrix. | 
|---|
| 178 | * Code contributed by Jacques Leroy jle@star.be | 
|---|
| 179 | * Return GL_TRUE for success, GL_FALSE for failure (singular matrix) | 
|---|
| 180 | */ | 
|---|
| 181 | static GLboolean invert_matrix_general( GLmatrix *mat ) | 
|---|
| 182 | { | 
|---|
| 183 | const GLfloat *m = mat->m; | 
|---|
| 184 | GLfloat *out = mat->inv; | 
|---|
| 185 | GLfloat wtmp[4][8]; | 
|---|
| 186 | GLfloat m0, m1, m2, m3, s; | 
|---|
| 187 | GLfloat *r0, *r1, *r2, *r3; | 
|---|
| 188 |  | 
|---|
| 189 | r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3]; | 
|---|
| 190 |  | 
|---|
| 191 | r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1), | 
|---|
| 192 | r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3), | 
|---|
| 193 | r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0, | 
|---|
| 194 |  | 
|---|
| 195 | r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1), | 
|---|
| 196 | r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3), | 
|---|
| 197 | r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0, | 
|---|
| 198 |  | 
|---|
| 199 | r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1), | 
|---|
| 200 | r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3), | 
|---|
| 201 | r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0, | 
|---|
| 202 |  | 
|---|
| 203 | r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1), | 
|---|
| 204 | r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3), | 
|---|
| 205 | r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0; | 
|---|
| 206 |  | 
|---|
| 207 | /* choose pivot - or die */ | 
|---|
| 208 | if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2); | 
|---|
| 209 | if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1); | 
|---|
| 210 | if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0); | 
|---|
| 211 | if (0.0 == r0[0])  return GL_FALSE; | 
|---|
| 212 |  | 
|---|
| 213 | /* eliminate first variable     */ | 
|---|
| 214 | m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0]; | 
|---|
| 215 | s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s; | 
|---|
| 216 | s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s; | 
|---|
| 217 | s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s; | 
|---|
| 218 | s = r0[4]; | 
|---|
| 219 | if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; } | 
|---|
| 220 | s = r0[5]; | 
|---|
| 221 | if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; } | 
|---|
| 222 | s = r0[6]; | 
|---|
| 223 | if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; } | 
|---|
| 224 | s = r0[7]; | 
|---|
| 225 | if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; } | 
|---|
| 226 |  | 
|---|
| 227 | /* choose pivot - or die */ | 
|---|
| 228 | if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2); | 
|---|
| 229 | if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1); | 
|---|
| 230 | if (0.0 == r1[1])  return GL_FALSE; | 
|---|
| 231 |  | 
|---|
| 232 | /* eliminate second variable */ | 
|---|
| 233 | m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1]; | 
|---|
| 234 | r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2]; | 
|---|
| 235 | r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3]; | 
|---|
| 236 | s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; } | 
|---|
| 237 | s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; } | 
|---|
| 238 | s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; } | 
|---|
| 239 | s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; } | 
|---|
| 240 |  | 
|---|
| 241 | /* choose pivot - or die */ | 
|---|
| 242 | if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2); | 
|---|
| 243 | if (0.0 == r2[2])  return GL_FALSE; | 
|---|
| 244 |  | 
|---|
| 245 | /* eliminate third variable */ | 
|---|
| 246 | m3 = r3[2]/r2[2]; | 
|---|
| 247 | r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4], | 
|---|
| 248 | r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], | 
|---|
| 249 | r3[7] -= m3 * r2[7]; | 
|---|
| 250 |  | 
|---|
| 251 | /* last check */ | 
|---|
| 252 | if (0.0 == r3[3]) return GL_FALSE; | 
|---|
| 253 |  | 
|---|
| 254 | s = 1.0/r3[3];              /* now back substitute row 3 */ | 
|---|
| 255 | r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s; | 
|---|
| 256 |  | 
|---|
| 257 | m2 = r2[3];                 /* now back substitute row 2 */ | 
|---|
| 258 | s  = 1.0/r2[2]; | 
|---|
| 259 | r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2), | 
|---|
| 260 | r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2); | 
|---|
| 261 | m1 = r1[3]; | 
|---|
| 262 | r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1, | 
|---|
| 263 | r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1; | 
|---|
| 264 | m0 = r0[3]; | 
|---|
| 265 | r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0, | 
|---|
| 266 | r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0; | 
|---|
| 267 |  | 
|---|
| 268 | m1 = r1[2];                 /* now back substitute row 1 */ | 
|---|
| 269 | s  = 1.0/r1[1]; | 
|---|
| 270 | r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1), | 
|---|
| 271 | r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1); | 
|---|
| 272 | m0 = r0[2]; | 
|---|
| 273 | r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0, | 
|---|
| 274 | r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0; | 
|---|
| 275 |  | 
|---|
| 276 | m0 = r0[1];                 /* now back substitute row 0 */ | 
|---|
| 277 | s  = 1.0/r0[0]; | 
|---|
| 278 | r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0), | 
|---|
| 279 | r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0); | 
|---|
| 280 |  | 
|---|
| 281 | MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5], | 
|---|
| 282 | MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7], | 
|---|
| 283 | MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5], | 
|---|
| 284 | MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7], | 
|---|
| 285 | MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5], | 
|---|
| 286 | MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7], | 
|---|
| 287 | MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5], | 
|---|
| 288 | MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; | 
|---|
| 289 |  | 
|---|
| 290 | return GL_TRUE; | 
|---|
| 291 | } | 
|---|
| 292 | #undef SWAP_ROWS | 
|---|
| 293 |  | 
|---|
| 294 | /* Adapted from graphics gems II. | 
|---|
| 295 | */ | 
|---|
| 296 | static GLboolean invert_matrix_3d_general( GLmatrix *mat ) | 
|---|
| 297 | { | 
|---|
| 298 | const GLfloat *in = mat->m; | 
|---|
| 299 | GLfloat *out = mat->inv; | 
|---|
| 300 | GLfloat pos, neg, t; | 
|---|
| 301 | GLfloat det; | 
|---|
| 302 |  | 
|---|
| 303 | /* Calculate the determinant of upper left 3x3 submatrix and | 
|---|
| 304 | * determine if the matrix is singular. | 
|---|
| 305 | */ | 
|---|
| 306 | pos = neg = 0.0; | 
|---|
| 307 | t =  MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2); | 
|---|
| 308 | if (t >= 0.0) pos += t; else neg += t; | 
|---|
| 309 |  | 
|---|
| 310 | t =  MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2); | 
|---|
| 311 | if (t >= 0.0) pos += t; else neg += t; | 
|---|
| 312 |  | 
|---|
| 313 | t =  MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2); | 
|---|
| 314 | if (t >= 0.0) pos += t; else neg += t; | 
|---|
| 315 |  | 
|---|
| 316 | t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2); | 
|---|
| 317 | if (t >= 0.0) pos += t; else neg += t; | 
|---|
| 318 |  | 
|---|
| 319 | t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2); | 
|---|
| 320 | if (t >= 0.0) pos += t; else neg += t; | 
|---|
| 321 |  | 
|---|
| 322 | t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2); | 
|---|
| 323 | if (t >= 0.0) pos += t; else neg += t; | 
|---|
| 324 |  | 
|---|
| 325 | det = pos + neg; | 
|---|
| 326 |  | 
|---|
| 327 | if (det*det < 1e-25) | 
|---|
| 328 | return GL_FALSE; | 
|---|
| 329 |  | 
|---|
| 330 | det = 1.0 / det; | 
|---|
| 331 | MAT(out,0,0) = (  (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det); | 
|---|
| 332 | MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det); | 
|---|
| 333 | MAT(out,0,2) = (  (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det); | 
|---|
| 334 | MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det); | 
|---|
| 335 | MAT(out,1,1) = (  (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det); | 
|---|
| 336 | MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det); | 
|---|
| 337 | MAT(out,2,0) = (  (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det); | 
|---|
| 338 | MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det); | 
|---|
| 339 | MAT(out,2,2) = (  (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det); | 
|---|
| 340 |  | 
|---|
| 341 | /* Do the translation part */ | 
|---|
| 342 | MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + | 
|---|
| 343 | MAT(in,1,3) * MAT(out,0,1) + | 
|---|
| 344 | MAT(in,2,3) * MAT(out,0,2) ); | 
|---|
| 345 | MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + | 
|---|
| 346 | MAT(in,1,3) * MAT(out,1,1) + | 
|---|
| 347 | MAT(in,2,3) * MAT(out,1,2) ); | 
|---|
| 348 | MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + | 
|---|
| 349 | MAT(in,1,3) * MAT(out,2,1) + | 
|---|
| 350 | MAT(in,2,3) * MAT(out,2,2) ); | 
|---|
| 351 |  | 
|---|
| 352 | return GL_TRUE; | 
|---|
| 353 | } | 
|---|
| 354 |  | 
|---|
| 355 |  | 
|---|
| 356 | static GLboolean invert_matrix_3d( GLmatrix *mat ) | 
|---|
| 357 | { | 
|---|
| 358 | const GLfloat *in = mat->m; | 
|---|
| 359 | GLfloat *out = mat->inv; | 
|---|
| 360 |  | 
|---|
| 361 | if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) | 
|---|
| 362 | { | 
|---|
| 363 | return invert_matrix_3d_general( mat ); | 
|---|
| 364 | } | 
|---|
| 365 |  | 
|---|
| 366 | if (mat->flags & MAT_FLAG_UNIFORM_SCALE) | 
|---|
| 367 | { | 
|---|
| 368 | GLfloat  scale = (MAT(in,0,0) * MAT(in,0,0) + | 
|---|
| 369 | MAT(in,0,1) * MAT(in,0,1) + | 
|---|
| 370 | MAT(in,0,2) * MAT(in,0,2)); | 
|---|
| 371 |  | 
|---|
| 372 | if (scale == 0.0) | 
|---|
| 373 | return GL_FALSE; | 
|---|
| 374 |  | 
|---|
| 375 | scale = 1.0 / scale; | 
|---|
| 376 |  | 
|---|
| 377 | /* Transpose and scale the 3 by 3 upper-left submatrix. */ | 
|---|
| 378 | MAT(out,0,0) = scale * MAT(in,0,0); | 
|---|
| 379 | MAT(out,1,0) = scale * MAT(in,0,1); | 
|---|
| 380 | MAT(out,2,0) = scale * MAT(in,0,2); | 
|---|
| 381 | MAT(out,0,1) = scale * MAT(in,1,0); | 
|---|
| 382 | MAT(out,1,1) = scale * MAT(in,1,1); | 
|---|
| 383 | MAT(out,2,1) = scale * MAT(in,1,2); | 
|---|
| 384 | MAT(out,0,2) = scale * MAT(in,2,0); | 
|---|
| 385 | MAT(out,1,2) = scale * MAT(in,2,1); | 
|---|
| 386 | MAT(out,2,2) = scale * MAT(in,2,2); | 
|---|
| 387 | } | 
|---|
| 388 | else if (mat->flags & MAT_FLAG_ROTATION) | 
|---|
| 389 | { | 
|---|
| 390 | /* Transpose the 3 by 3 upper-left submatrix. */ | 
|---|
| 391 | MAT(out,0,0) = MAT(in,0,0); | 
|---|
| 392 | MAT(out,1,0) = MAT(in,0,1); | 
|---|
| 393 | MAT(out,2,0) = MAT(in,0,2); | 
|---|
| 394 | MAT(out,0,1) = MAT(in,1,0); | 
|---|
| 395 | MAT(out,1,1) = MAT(in,1,1); | 
|---|
| 396 | MAT(out,2,1) = MAT(in,1,2); | 
|---|
| 397 | MAT(out,0,2) = MAT(in,2,0); | 
|---|
| 398 | MAT(out,1,2) = MAT(in,2,1); | 
|---|
| 399 | MAT(out,2,2) = MAT(in,2,2); | 
|---|
| 400 | } | 
|---|
| 401 | else /* pure translation */ | 
|---|
| 402 | { | 
|---|
| 403 | MEMCPY( out, Identity, sizeof(Identity) ); | 
|---|
| 404 | MAT(out,0,3) = - MAT(in,0,3); | 
|---|
| 405 | MAT(out,1,3) = - MAT(in,1,3); | 
|---|
| 406 | MAT(out,2,3) = - MAT(in,2,3); | 
|---|
| 407 | return GL_TRUE; | 
|---|
| 408 | } | 
|---|
| 409 |  | 
|---|
| 410 | if (mat->flags & MAT_FLAG_TRANSLATION) | 
|---|
| 411 | { | 
|---|
| 412 | /* Do the translation part */ | 
|---|
| 413 | MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) + | 
|---|
| 414 | MAT(in,1,3) * MAT(out,0,1) + | 
|---|
| 415 | MAT(in,2,3) * MAT(out,0,2) ); | 
|---|
| 416 | MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) + | 
|---|
| 417 | MAT(in,1,3) * MAT(out,1,1) + | 
|---|
| 418 | MAT(in,2,3) * MAT(out,1,2) ); | 
|---|
| 419 | MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) + | 
|---|
| 420 | MAT(in,1,3) * MAT(out,2,1) + | 
|---|
| 421 | MAT(in,2,3) * MAT(out,2,2) ); | 
|---|
| 422 | } | 
|---|
| 423 | else | 
|---|
| 424 | { | 
|---|
| 425 | MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0; | 
|---|
| 426 | } | 
|---|
| 427 |  | 
|---|
| 428 | return GL_TRUE; | 
|---|
| 429 | } | 
|---|
| 430 |  | 
|---|
| 431 |  | 
|---|
| 432 |  | 
|---|
| 433 | static GLboolean invert_matrix_identity( GLmatrix *mat ) | 
|---|
| 434 | { | 
|---|
| 435 | MEMCPY( mat->inv, Identity, sizeof(Identity) ); | 
|---|
| 436 | return GL_TRUE; | 
|---|
| 437 | } | 
|---|
| 438 |  | 
|---|
| 439 |  | 
|---|
| 440 | static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat ) | 
|---|
| 441 | { | 
|---|
| 442 | const GLfloat *in = mat->m; | 
|---|
| 443 | GLfloat *out = mat->inv; | 
|---|
| 444 |  | 
|---|
| 445 | if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 ) | 
|---|
| 446 | return GL_FALSE; | 
|---|
| 447 |  | 
|---|
| 448 | MEMCPY( out, Identity, 16 * sizeof(GLfloat) ); | 
|---|
| 449 | MAT(out,0,0) = 1.0 / MAT(in,0,0); | 
|---|
| 450 | MAT(out,1,1) = 1.0 / MAT(in,1,1); | 
|---|
| 451 | MAT(out,2,2) = 1.0 / MAT(in,2,2); | 
|---|
| 452 |  | 
|---|
| 453 | if (mat->flags & MAT_FLAG_TRANSLATION) | 
|---|
| 454 | { | 
|---|
| 455 | MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); | 
|---|
| 456 | MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); | 
|---|
| 457 | MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2)); | 
|---|
| 458 | } | 
|---|
| 459 |  | 
|---|
| 460 | return GL_TRUE; | 
|---|
| 461 | } | 
|---|
| 462 |  | 
|---|
| 463 |  | 
|---|
| 464 | static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat ) | 
|---|
| 465 | { | 
|---|
| 466 | const GLfloat *in = mat->m; | 
|---|
| 467 | GLfloat *out = mat->inv; | 
|---|
| 468 |  | 
|---|
| 469 | if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0) | 
|---|
| 470 | return GL_FALSE; | 
|---|
| 471 |  | 
|---|
| 472 | MEMCPY( out, Identity, 16 * sizeof(GLfloat) ); | 
|---|
| 473 | MAT(out,0,0) = 1.0 / MAT(in,0,0); | 
|---|
| 474 | MAT(out,1,1) = 1.0 / MAT(in,1,1); | 
|---|
| 475 |  | 
|---|
| 476 | if (mat->flags & MAT_FLAG_TRANSLATION) | 
|---|
| 477 | { | 
|---|
| 478 | MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0)); | 
|---|
| 479 | MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1)); | 
|---|
| 480 | } | 
|---|
| 481 |  | 
|---|
| 482 | return GL_TRUE; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 |  | 
|---|
| 486 | static GLboolean invert_matrix_perspective( GLmatrix *mat ) | 
|---|
| 487 | { | 
|---|
| 488 | const GLfloat *in = mat->m; | 
|---|
| 489 | GLfloat *out = mat->inv; | 
|---|
| 490 |  | 
|---|
| 491 | if (MAT(in,2,3) == 0) | 
|---|
| 492 | return GL_FALSE; | 
|---|
| 493 |  | 
|---|
| 494 | MEMCPY( out, Identity, 16 * sizeof(GLfloat) ); | 
|---|
| 495 |  | 
|---|
| 496 | MAT(out,0,0) = 1.0 / MAT(in,0,0); | 
|---|
| 497 | MAT(out,1,1) = 1.0 / MAT(in,1,1); | 
|---|
| 498 |  | 
|---|
| 499 | MAT(out,0,3) = MAT(in,0,2); | 
|---|
| 500 | MAT(out,1,3) = MAT(in,1,2); | 
|---|
| 501 |  | 
|---|
| 502 | MAT(out,2,2) = 0; | 
|---|
| 503 | MAT(out,2,3) = -1; | 
|---|
| 504 |  | 
|---|
| 505 | MAT(out,3,2) = 1.0 / MAT(in,2,3); | 
|---|
| 506 | MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2); | 
|---|
| 507 |  | 
|---|
| 508 | return GL_TRUE; | 
|---|
| 509 | } | 
|---|
| 510 |  | 
|---|
| 511 |  | 
|---|
| 512 | typedef GLboolean (*inv_mat_func)( GLmatrix *mat ); | 
|---|
| 513 |  | 
|---|
| 514 | static inv_mat_func inv_mat_tab[7] = { | 
|---|
| 515 | invert_matrix_general, | 
|---|
| 516 | invert_matrix_identity, | 
|---|
| 517 | invert_matrix_3d_no_rot, | 
|---|
| 518 | invert_matrix_perspective, | 
|---|
| 519 | invert_matrix_3d,            /* lazy! */ | 
|---|
| 520 | invert_matrix_2d_no_rot, | 
|---|
| 521 | invert_matrix_3d | 
|---|
| 522 | }; | 
|---|
| 523 |  | 
|---|
| 524 |  | 
|---|
| 525 | GLboolean gl_matrix_invert( GLmatrix *mat ) | 
|---|
| 526 | { | 
|---|
| 527 | if (inv_mat_tab[mat->type](mat)) { | 
|---|
| 528 | #if 0 | 
|---|
| 529 | GLmatrix m; m.inv = 0; m.type = 0; m.flags = 0; | 
|---|
| 530 | matmul4( m.m, mat->m, mat->inv ); | 
|---|
| 531 | printf("inverted matrix of type %s:\n", types[mat->type]); | 
|---|
| 532 | gl_print_matrix( mat ); | 
|---|
| 533 | gl_print_matrix( &m ); | 
|---|
| 534 | #endif | 
|---|
| 535 | return GL_TRUE; | 
|---|
| 536 | } else { | 
|---|
| 537 | MEMCPY( mat->inv, Identity, sizeof(Identity) ); | 
|---|
| 538 | return GL_FALSE; | 
|---|
| 539 | } | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 |  | 
|---|
| 543 |  | 
|---|
| 544 | void gl_matrix_transposef( GLfloat to[16], const GLfloat from[16] ) | 
|---|
| 545 | { | 
|---|
| 546 | to[0] = from[0]; | 
|---|
| 547 | to[1] = from[4]; | 
|---|
| 548 | to[2] = from[8]; | 
|---|
| 549 | to[3] = from[12]; | 
|---|
| 550 | to[4] = from[1]; | 
|---|
| 551 | to[5] = from[5]; | 
|---|
| 552 | to[6] = from[9]; | 
|---|
| 553 | to[7] = from[13]; | 
|---|
| 554 | to[8] = from[2]; | 
|---|
| 555 | to[9] = from[6]; | 
|---|
| 556 | to[10] = from[10]; | 
|---|
| 557 | to[11] = from[14]; | 
|---|
| 558 | to[12] = from[3]; | 
|---|
| 559 | to[13] = from[7]; | 
|---|
| 560 | to[14] = from[11]; | 
|---|
| 561 | to[15] = from[15]; | 
|---|
| 562 | } | 
|---|
| 563 |  | 
|---|
| 564 |  | 
|---|
| 565 |  | 
|---|
| 566 | void gl_matrix_transposed( GLdouble to[16], const GLdouble from[16] ) | 
|---|
| 567 | { | 
|---|
| 568 | to[0] = from[0]; | 
|---|
| 569 | to[1] = from[4]; | 
|---|
| 570 | to[2] = from[8]; | 
|---|
| 571 | to[3] = from[12]; | 
|---|
| 572 | to[4] = from[1]; | 
|---|
| 573 | to[5] = from[5]; | 
|---|
| 574 | to[6] = from[9]; | 
|---|
| 575 | to[7] = from[13]; | 
|---|
| 576 | to[8] = from[2]; | 
|---|
| 577 | to[9] = from[6]; | 
|---|
| 578 | to[10] = from[10]; | 
|---|
| 579 | to[11] = from[14]; | 
|---|
| 580 | to[12] = from[3]; | 
|---|
| 581 | to[13] = from[7]; | 
|---|
| 582 | to[14] = from[11]; | 
|---|
| 583 | to[15] = from[15]; | 
|---|
| 584 | } | 
|---|
| 585 |  | 
|---|
| 586 |  | 
|---|
| 587 |  | 
|---|
| 588 | /* | 
|---|
| 589 | * Generate a 4x4 transformation matrix from glRotate parameters. | 
|---|
| 590 | */ | 
|---|
| 591 | void gl_rotation_matrix( GLfloat angle, GLfloat x, GLfloat y, GLfloat z, | 
|---|
| 592 | GLfloat m[] ) | 
|---|
| 593 | { | 
|---|
| 594 | /* This function contributed by Erich Boleyn (erich@uruk.org) */ | 
|---|
| 595 | GLfloat mag, s, c; | 
|---|
| 596 | GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c; | 
|---|
| 597 |  | 
|---|
| 598 | s = sin( angle * DEG2RAD ); | 
|---|
| 599 | c = cos( angle * DEG2RAD ); | 
|---|
| 600 |  | 
|---|
| 601 | mag = GL_SQRT( x*x + y*y + z*z ); | 
|---|
| 602 |  | 
|---|
| 603 | if (mag == 0.0) { | 
|---|
| 604 | /* generate an identity matrix and return */ | 
|---|
| 605 | MEMCPY(m, Identity, sizeof(GLfloat)*16); | 
|---|
| 606 | return; | 
|---|
| 607 | } | 
|---|
| 608 |  | 
|---|
| 609 | x /= mag; | 
|---|
| 610 | y /= mag; | 
|---|
| 611 | z /= mag; | 
|---|
| 612 |  | 
|---|
| 613 | #define M(row,col)  m[col*4+row] | 
|---|
| 614 |  | 
|---|
| 615 | /* | 
|---|
| 616 | *     Arbitrary axis rotation matrix. | 
|---|
| 617 | * | 
|---|
| 618 | *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied | 
|---|
| 619 | *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation | 
|---|
| 620 | *  (which is about the X-axis), and the two composite transforms | 
|---|
| 621 | *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary | 
|---|
| 622 | *  from the arbitrary axis to the X-axis then back.  They are | 
|---|
| 623 | *  all elementary rotations. | 
|---|
| 624 | * | 
|---|
| 625 | *  Rz' is a rotation about the Z-axis, to bring the axis vector | 
|---|
| 626 | *  into the x-z plane.  Then Ry' is applied, rotating about the | 
|---|
| 627 | *  Y-axis to bring the axis vector parallel with the X-axis.  The | 
|---|
| 628 | *  rotation about the X-axis is then performed.  Ry and Rz are | 
|---|
| 629 | *  simply the respective inverse transforms to bring the arbitrary | 
|---|
| 630 | *  axis back to it's original orientation.  The first transforms | 
|---|
| 631 | *  Rz' and Ry' are considered inverses, since the data from the | 
|---|
| 632 | *  arbitrary axis gives you info on how to get to it, not how | 
|---|
| 633 | *  to get away from it, and an inverse must be applied. | 
|---|
| 634 | * | 
|---|
| 635 | *  The basic calculation used is to recognize that the arbitrary | 
|---|
| 636 | *  axis vector (x, y, z), since it is of unit length, actually | 
|---|
| 637 | *  represents the sines and cosines of the angles to rotate the | 
|---|
| 638 | *  X-axis to the same orientation, with theta being the angle about | 
|---|
| 639 | *  Z and phi the angle about Y (in the order described above) | 
|---|
| 640 | *  as follows: | 
|---|
| 641 | * | 
|---|
| 642 | *  cos ( theta ) = x / sqrt ( 1 - z^2 ) | 
|---|
| 643 | *  sin ( theta ) = y / sqrt ( 1 - z^2 ) | 
|---|
| 644 | * | 
|---|
| 645 | *  cos ( phi ) = sqrt ( 1 - z^2 ) | 
|---|
| 646 | *  sin ( phi ) = z | 
|---|
| 647 | * | 
|---|
| 648 | *  Note that cos ( phi ) can further be inserted to the above | 
|---|
| 649 | *  formulas: | 
|---|
| 650 | * | 
|---|
| 651 | *  cos ( theta ) = x / cos ( phi ) | 
|---|
| 652 | *  sin ( theta ) = y / sin ( phi ) | 
|---|
| 653 | * | 
|---|
| 654 | *  ...etc.  Because of those relations and the standard trigonometric | 
|---|
| 655 | *  relations, it is pssible to reduce the transforms down to what | 
|---|
| 656 | *  is used below.  It may be that any primary axis chosen will give the | 
|---|
| 657 | *  same results (modulo a sign convention) using thie method. | 
|---|
| 658 | * | 
|---|
| 659 | *  Particularly nice is to notice that all divisions that might | 
|---|
| 660 | *  have caused trouble when parallel to certain planes or | 
|---|
| 661 | *  axis go away with care paid to reducing the expressions. | 
|---|
| 662 | *  After checking, it does perform correctly under all cases, since | 
|---|
| 663 | *  in all the cases of division where the denominator would have | 
|---|
| 664 | *  been zero, the numerator would have been zero as well, giving | 
|---|
| 665 | *  the expected result. | 
|---|
| 666 | */ | 
|---|
| 667 |  | 
|---|
| 668 | xx = x * x; | 
|---|
| 669 | yy = y * y; | 
|---|
| 670 | zz = z * z; | 
|---|
| 671 | xy = x * y; | 
|---|
| 672 | yz = y * z; | 
|---|
| 673 | zx = z * x; | 
|---|
| 674 | xs = x * s; | 
|---|
| 675 | ys = y * s; | 
|---|
| 676 | zs = z * s; | 
|---|
| 677 | one_c = 1.0F - c; | 
|---|
| 678 |  | 
|---|
| 679 | M(0,0) = (one_c * xx) + c; | 
|---|
| 680 | M(0,1) = (one_c * xy) - zs; | 
|---|
| 681 | M(0,2) = (one_c * zx) + ys; | 
|---|
| 682 | M(0,3) = 0.0F; | 
|---|
| 683 |  | 
|---|
| 684 | M(1,0) = (one_c * xy) + zs; | 
|---|
| 685 | M(1,1) = (one_c * yy) + c; | 
|---|
| 686 | M(1,2) = (one_c * yz) - xs; | 
|---|
| 687 | M(1,3) = 0.0F; | 
|---|
| 688 |  | 
|---|
| 689 | M(2,0) = (one_c * zx) - ys; | 
|---|
| 690 | M(2,1) = (one_c * yz) + xs; | 
|---|
| 691 | M(2,2) = (one_c * zz) + c; | 
|---|
| 692 | M(2,3) = 0.0F; | 
|---|
| 693 |  | 
|---|
| 694 | M(3,0) = 0.0F; | 
|---|
| 695 | M(3,1) = 0.0F; | 
|---|
| 696 | M(3,2) = 0.0F; | 
|---|
| 697 | M(3,3) = 1.0F; | 
|---|
| 698 |  | 
|---|
| 699 | #undef M | 
|---|
| 700 | } | 
|---|
| 701 |  | 
|---|
| 702 | #define ZERO(x) (1<<x) | 
|---|
| 703 | #define ONE(x)  (1<<(x+16)) | 
|---|
| 704 |  | 
|---|
| 705 | #define MASK_NO_TRX      (ZERO(12) | ZERO(13) | ZERO(14)) | 
|---|
| 706 | #define MASK_NO_2D_SCALE ( ONE(0)  | ONE(5)) | 
|---|
| 707 |  | 
|---|
| 708 | #define MASK_IDENTITY    ( ONE(0)  | ZERO(4)  | ZERO(8)  | ZERO(12) |\ | 
|---|
| 709 | ZERO(1)  |  ONE(5)  | ZERO(9)  | ZERO(13) |\ | 
|---|
| 710 | ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\ | 
|---|
| 711 | ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) ) | 
|---|
| 712 |  | 
|---|
| 713 | #define MASK_2D_NO_ROT   (           ZERO(4)  | ZERO(8)  |           \ | 
|---|
| 714 | ZERO(1)  |            ZERO(9)  |           \ | 
|---|
| 715 | ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\ | 
|---|
| 716 | ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) ) | 
|---|
| 717 |  | 
|---|
| 718 | #define MASK_2D          (                      ZERO(8)  |           \ | 
|---|
| 719 | ZERO(9)  |           \ | 
|---|
| 720 | ZERO(2)  | ZERO(6)  |  ONE(10) | ZERO(14) |\ | 
|---|
| 721 | ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) ) | 
|---|
| 722 |  | 
|---|
| 723 |  | 
|---|
| 724 | #define MASK_3D_NO_ROT   (           ZERO(4)  | ZERO(8)  |           \ | 
|---|
| 725 | ZERO(1)  |            ZERO(9)  |           \ | 
|---|
| 726 | ZERO(2)  | ZERO(6)  |                      \ | 
|---|
| 727 | ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) ) | 
|---|
| 728 |  | 
|---|
| 729 | #define MASK_3D          (                                           \ | 
|---|
| 730 | \ | 
|---|
| 731 | \ | 
|---|
| 732 | ZERO(3)  | ZERO(7)  | ZERO(11) |  ONE(15) ) | 
|---|
| 733 |  | 
|---|
| 734 |  | 
|---|
| 735 | #define MASK_PERSPECTIVE (           ZERO(4)  |            ZERO(12) |\ | 
|---|
| 736 | ZERO(1)  |                       ZERO(13) |\ | 
|---|
| 737 | ZERO(2)  | ZERO(6)  |                      \ | 
|---|
| 738 | ZERO(3)  | ZERO(7)  |            ZERO(15) ) | 
|---|
| 739 |  | 
|---|
| 740 | #define SQ(x) ((x)*(x)) | 
|---|
| 741 |  | 
|---|
| 742 | /* Determine type and flags from scratch.  This is expensive enough to | 
|---|
| 743 | * only want to do it once. | 
|---|
| 744 | */ | 
|---|
| 745 | static void analyze_from_scratch( GLmatrix *mat ) | 
|---|
| 746 | { | 
|---|
| 747 | const GLfloat *m = mat->m; | 
|---|
| 748 | GLuint mask = 0; | 
|---|
| 749 | GLuint i; | 
|---|
| 750 |  | 
|---|
| 751 | for (i = 0 ; i < 16 ; i++) | 
|---|
| 752 | { | 
|---|
| 753 | if (m[i] == 0.0) mask |= (1<<i); | 
|---|
| 754 | } | 
|---|
| 755 |  | 
|---|
| 756 | if (m[0] == 1.0F) mask |= (1<<16); | 
|---|
| 757 | if (m[5] == 1.0F) mask |= (1<<21); | 
|---|
| 758 | if (m[10] == 1.0F) mask |= (1<<26); | 
|---|
| 759 | if (m[15] == 1.0F) mask |= (1<<31); | 
|---|
| 760 |  | 
|---|
| 761 | mat->flags &= ~MAT_FLAGS_GEOMETRY; | 
|---|
| 762 |  | 
|---|
| 763 | /* Check for translation - no-one really cares | 
|---|
| 764 | */ | 
|---|
| 765 | if ((mask & MASK_NO_TRX) != MASK_NO_TRX) | 
|---|
| 766 | mat->flags |= MAT_FLAG_TRANSLATION; | 
|---|
| 767 |  | 
|---|
| 768 | /* Do the real work | 
|---|
| 769 | */ | 
|---|
| 770 | if (mask == MASK_IDENTITY) { | 
|---|
| 771 | mat->type = MATRIX_IDENTITY; | 
|---|
| 772 | } | 
|---|
| 773 | else if ((mask & MASK_2D_NO_ROT) == MASK_2D_NO_ROT) | 
|---|
| 774 | { | 
|---|
| 775 | mat->type = MATRIX_2D_NO_ROT; | 
|---|
| 776 |  | 
|---|
| 777 | if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE) | 
|---|
| 778 | mat->flags = MAT_FLAG_GENERAL_SCALE; | 
|---|
| 779 | } | 
|---|
| 780 | else if ((mask & MASK_2D) == MASK_2D) | 
|---|
| 781 | { | 
|---|
| 782 | GLfloat mm = DOT2(m, m); | 
|---|
| 783 | GLfloat m4m4 = DOT2(m+4,m+4); | 
|---|
| 784 | GLfloat mm4 = DOT2(m,m+4); | 
|---|
| 785 |  | 
|---|
| 786 | mat->type = MATRIX_2D; | 
|---|
| 787 |  | 
|---|
| 788 | /* Check for scale */ | 
|---|
| 789 | if (SQ(mm-1) > SQ(1e-6) || | 
|---|
| 790 | SQ(m4m4-1) > SQ(1e-6)) | 
|---|
| 791 | mat->flags |= MAT_FLAG_GENERAL_SCALE; | 
|---|
| 792 |  | 
|---|
| 793 | /* Check for rotation */ | 
|---|
| 794 | if (SQ(mm4) > SQ(1e-6)) | 
|---|
| 795 | mat->flags |= MAT_FLAG_GENERAL_3D; | 
|---|
| 796 | else | 
|---|
| 797 | mat->flags |= MAT_FLAG_ROTATION; | 
|---|
| 798 |  | 
|---|
| 799 | } | 
|---|
| 800 | else if ((mask & MASK_3D_NO_ROT) == MASK_3D_NO_ROT) | 
|---|
| 801 | { | 
|---|
| 802 | mat->type = MATRIX_3D_NO_ROT; | 
|---|
| 803 |  | 
|---|
| 804 | /* Check for scale */ | 
|---|
| 805 | if (SQ(m[0]-m[5]) < SQ(1e-6) && | 
|---|
| 806 | SQ(m[0]-m[10]) < SQ(1e-6)) { | 
|---|
| 807 | if (SQ(m[0]-1.0) > SQ(1e-6)) | 
|---|
| 808 | mat->flags |= MAT_FLAG_UNIFORM_SCALE; | 
|---|
| 809 | } else | 
|---|
| 810 | mat->flags |= MAT_FLAG_GENERAL_SCALE; | 
|---|
| 811 | } | 
|---|
| 812 | else if ((mask & MASK_3D) == MASK_3D) | 
|---|
| 813 | { | 
|---|
| 814 | GLfloat c1 = DOT3(m,m); | 
|---|
| 815 | GLfloat c2 = DOT3(m+4,m+4); | 
|---|
| 816 | GLfloat c3 = DOT3(m+8,m+8); | 
|---|
| 817 | GLfloat d1 = DOT3(m, m+4); | 
|---|
| 818 | GLfloat cp[3]; | 
|---|
| 819 |  | 
|---|
| 820 | mat->type = MATRIX_3D; | 
|---|
| 821 |  | 
|---|
| 822 | /* Check for scale */ | 
|---|
| 823 | if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) { | 
|---|
| 824 | if (SQ(c1-1.0) > SQ(1e-6)) | 
|---|
| 825 | mat->flags |= MAT_FLAG_UNIFORM_SCALE; | 
|---|
| 826 | /* else no scale at all */ | 
|---|
| 827 | } else | 
|---|
| 828 | mat->flags |= MAT_FLAG_GENERAL_SCALE; | 
|---|
| 829 |  | 
|---|
| 830 | /* Check for rotation */ | 
|---|
| 831 | if (SQ(d1) < SQ(1e-6)) { | 
|---|
| 832 | CROSS3( cp, m, m+4 ); | 
|---|
| 833 | SUB_3V( cp, cp, (m+8) ); | 
|---|
| 834 | if (LEN_SQUARED_3FV(cp) < SQ(1e-6)) | 
|---|
| 835 | mat->flags |= MAT_FLAG_ROTATION; | 
|---|
| 836 | else | 
|---|
| 837 | mat->flags |= MAT_FLAG_GENERAL_3D; | 
|---|
| 838 | } | 
|---|
| 839 | else | 
|---|
| 840 | mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */ | 
|---|
| 841 | } | 
|---|
| 842 | else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) | 
|---|
| 843 | { | 
|---|
| 844 | mat->type = MATRIX_PERSPECTIVE; | 
|---|
| 845 | mat->flags |= MAT_FLAG_GENERAL; | 
|---|
| 846 | } | 
|---|
| 847 | else { | 
|---|
| 848 | mat->type = MATRIX_GENERAL; | 
|---|
| 849 | mat->flags |= MAT_FLAG_GENERAL; | 
|---|
| 850 | } | 
|---|
| 851 | } | 
|---|
| 852 |  | 
|---|
| 853 |  | 
|---|
| 854 | /* Analyse a matrix given that its flags are accurate - this is the | 
|---|
| 855 | * more common operation, hopefully. | 
|---|
| 856 | */ | 
|---|
| 857 | static void analyze_from_flags( GLmatrix *mat ) | 
|---|
| 858 | { | 
|---|
| 859 | const GLfloat *m = mat->m; | 
|---|
| 860 |  | 
|---|
| 861 | if (TEST_MAT_FLAGS(mat, 0)) { | 
|---|
| 862 | mat->type = MATRIX_IDENTITY; | 
|---|
| 863 | } | 
|---|
| 864 | else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION | | 
|---|
| 865 | MAT_FLAG_UNIFORM_SCALE | | 
|---|
| 866 | MAT_FLAG_GENERAL_SCALE))) | 
|---|
| 867 | { | 
|---|
| 868 | if ( m[10]==1.0F && m[14]==0.0F ) { | 
|---|
| 869 | mat->type = MATRIX_2D_NO_ROT; | 
|---|
| 870 | } | 
|---|
| 871 | else { | 
|---|
| 872 | mat->type = MATRIX_3D_NO_ROT; | 
|---|
| 873 | } | 
|---|
| 874 | } | 
|---|
| 875 | else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) { | 
|---|
| 876 | if (                                 m[ 8]==0.0F | 
|---|
| 877 | &&                             m[ 9]==0.0F | 
|---|
| 878 | && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) | 
|---|
| 879 | { | 
|---|
| 880 | mat->type = MATRIX_2D; | 
|---|
| 881 | } | 
|---|
| 882 | else | 
|---|
| 883 | { | 
|---|
| 884 | mat->type = MATRIX_3D; | 
|---|
| 885 | } | 
|---|
| 886 | } | 
|---|
| 887 | else if (                 m[4]==0.0F                 && m[12]==0.0F | 
|---|
| 888 | && m[1]==0.0F                               && m[13]==0.0F | 
|---|
| 889 | && m[2]==0.0F && m[6]==0.0F | 
|---|
| 890 | && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) | 
|---|
| 891 | { | 
|---|
| 892 | mat->type = MATRIX_PERSPECTIVE; | 
|---|
| 893 | } | 
|---|
| 894 | else { | 
|---|
| 895 | mat->type = MATRIX_GENERAL; | 
|---|
| 896 | } | 
|---|
| 897 |  | 
|---|
| 898 | } | 
|---|
| 899 |  | 
|---|
| 900 |  | 
|---|
| 901 | void gl_matrix_analyze( GLmatrix *mat ) | 
|---|
| 902 | { | 
|---|
| 903 | if (mat->flags & MAT_DIRTY_TYPE) { | 
|---|
| 904 | if (mat->flags & MAT_DIRTY_FLAGS) | 
|---|
| 905 | analyze_from_scratch( mat ); | 
|---|
| 906 | else | 
|---|
| 907 | analyze_from_flags( mat ); | 
|---|
| 908 | } | 
|---|
| 909 |  | 
|---|
| 910 | if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) { | 
|---|
| 911 | gl_matrix_invert( mat ); | 
|---|
| 912 | } | 
|---|
| 913 |  | 
|---|
| 914 | mat->flags &= ~(MAT_DIRTY_FLAGS| | 
|---|
| 915 | MAT_DIRTY_TYPE| | 
|---|
| 916 | MAT_DIRTY_INVERSE); | 
|---|
| 917 | } | 
|---|
| 918 |  | 
|---|
| 919 |  | 
|---|
| 920 | #define GET_ACTIVE_MATRIX(ctx, mat, flags, where)                       \ | 
|---|
| 921 | do {                                                                    \ | 
|---|
| 922 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where);                      \ | 
|---|
| 923 | if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where);        \ | 
|---|
| 924 | switch (ctx->Transform.MatrixMode) {                                 \ | 
|---|
| 925 | case GL_MODELVIEW:                                                \ | 
|---|
| 926 | mat = &ctx->ModelView;                                         \ | 
|---|
| 927 | flags |= NEW_MODELVIEW;                                        \ | 
|---|
| 928 | break;                                                         \ | 
|---|
| 929 | case GL_PROJECTION:                                               \ | 
|---|
| 930 | mat = &ctx->ProjectionMatrix;                                  \ | 
|---|
| 931 | flags |= NEW_PROJECTION;                                       \ | 
|---|
| 932 | break;                                                         \ | 
|---|
| 933 | case GL_TEXTURE:                                                  \ | 
|---|
| 934 | mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit];  \ | 
|---|
| 935 | flags |= NEW_TEXTURE_MATRIX;                                   \ | 
|---|
| 936 | break;                                                         \ | 
|---|
| 937 | default:                                                          \ | 
|---|
| 938 | gl_problem(ctx, where);                                        \ | 
|---|
| 939 | }                                                                    \ | 
|---|
| 940 | } while (0) | 
|---|
| 941 |  | 
|---|
| 942 |  | 
|---|
| 943 | void | 
|---|
| 944 | _mesa_Frustum( GLdouble left, GLdouble right, | 
|---|
| 945 | GLdouble bottom, GLdouble top, | 
|---|
| 946 | GLdouble nearval, GLdouble farval ) | 
|---|
| 947 | { | 
|---|
| 948 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 949 | GLfloat x, y, a, b, c, d; | 
|---|
| 950 | GLfloat m[16]; | 
|---|
| 951 | GLmatrix *mat = 0; | 
|---|
| 952 |  | 
|---|
| 953 | GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glFrustrum" ); | 
|---|
| 954 |  | 
|---|
| 955 | if ((nearval<=0.0 || farval<=0.0) || (nearval == farval) || (left == right) || (top == bottom)) { | 
|---|
| 956 | gl_error( ctx,  GL_INVALID_VALUE, "glFrustum(near or far)" ); | 
|---|
| 957 | return; | 
|---|
| 958 | } | 
|---|
| 959 |  | 
|---|
| 960 | x = (2.0*nearval) / (right-left); | 
|---|
| 961 | y = (2.0*nearval) / (top-bottom); | 
|---|
| 962 | a = (right+left) / (right-left); | 
|---|
| 963 | b = (top+bottom) / (top-bottom); | 
|---|
| 964 | c = -(farval+nearval) / ( farval-nearval); | 
|---|
| 965 | d = -(2.0*farval*nearval) / (farval-nearval);  /* error? */ | 
|---|
| 966 |  | 
|---|
| 967 | #define M(row,col)  m[col*4+row] | 
|---|
| 968 | M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = a;      M(0,3) = 0.0F; | 
|---|
| 969 | M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = b;      M(1,3) = 0.0F; | 
|---|
| 970 | M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = c;      M(2,3) = d; | 
|---|
| 971 | M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = -1.0F;  M(3,3) = 0.0F; | 
|---|
| 972 | #undef M | 
|---|
| 973 |  | 
|---|
| 974 |  | 
|---|
| 975 | gl_mat_mul_floats( mat, m, MAT_FLAG_PERSPECTIVE ); | 
|---|
| 976 |  | 
|---|
| 977 |  | 
|---|
| 978 | if (ctx->Transform.MatrixMode == GL_PROJECTION) | 
|---|
| 979 | { | 
|---|
| 980 | /* Need to keep a stack of near/far values in case the user push/pops | 
|---|
| 981 | * the projection matrix stack so that we can call Driver.NearFar() | 
|---|
| 982 | * after a pop. | 
|---|
| 983 | */ | 
|---|
| 984 | ctx->NearFarStack[ctx->ProjectionStackDepth][0] = nearval; | 
|---|
| 985 | ctx->NearFarStack[ctx->ProjectionStackDepth][1] = farval; | 
|---|
| 986 |  | 
|---|
| 987 | if (ctx->Driver.NearFar) { | 
|---|
| 988 | (*ctx->Driver.NearFar)( ctx, nearval, farval ); | 
|---|
| 989 | } | 
|---|
| 990 | } | 
|---|
| 991 | } | 
|---|
| 992 |  | 
|---|
| 993 |  | 
|---|
| 994 | void | 
|---|
| 995 | _mesa_Ortho( GLdouble left, GLdouble right, | 
|---|
| 996 | GLdouble bottom, GLdouble top, | 
|---|
| 997 | GLdouble nearval, GLdouble farval ) | 
|---|
| 998 | { | 
|---|
| 999 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1000 | GLfloat x, y, z; | 
|---|
| 1001 | GLfloat tx, ty, tz; | 
|---|
| 1002 | GLfloat m[16]; | 
|---|
| 1003 | GLmatrix *mat = 0; | 
|---|
| 1004 |  | 
|---|
| 1005 | GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glOrtho" ); | 
|---|
| 1006 |  | 
|---|
| 1007 | if ((left == right) || (bottom == top) || (nearval == farval)) { | 
|---|
| 1008 | gl_error( ctx,  GL_INVALID_VALUE, "gl_Ortho((l = r) or (b = top) or (n=f)" ); | 
|---|
| 1009 | return; | 
|---|
| 1010 | } | 
|---|
| 1011 |  | 
|---|
| 1012 | x = 2.0 / (right-left); | 
|---|
| 1013 | y = 2.0 / (top-bottom); | 
|---|
| 1014 | z = -2.0 / (farval-nearval); | 
|---|
| 1015 | tx = -(right+left) / (right-left); | 
|---|
| 1016 | ty = -(top+bottom) / (top-bottom); | 
|---|
| 1017 | tz = -(farval+nearval) / (farval-nearval); | 
|---|
| 1018 |  | 
|---|
| 1019 | #define M(row,col)  m[col*4+row] | 
|---|
| 1020 | M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = 0.0F;  M(0,3) = tx; | 
|---|
| 1021 | M(1,0) = 0.0F;  M(1,1) = y;     M(1,2) = 0.0F;  M(1,3) = ty; | 
|---|
| 1022 | M(2,0) = 0.0F;  M(2,1) = 0.0F;  M(2,2) = z;     M(2,3) = tz; | 
|---|
| 1023 | M(3,0) = 0.0F;  M(3,1) = 0.0F;  M(3,2) = 0.0F;  M(3,3) = 1.0F; | 
|---|
| 1024 | #undef M | 
|---|
| 1025 |  | 
|---|
| 1026 | gl_mat_mul_floats( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION)); | 
|---|
| 1027 |  | 
|---|
| 1028 | if (ctx->Driver.NearFar) { | 
|---|
| 1029 | (*ctx->Driver.NearFar)( ctx, nearval, farval ); | 
|---|
| 1030 | } | 
|---|
| 1031 | } | 
|---|
| 1032 |  | 
|---|
| 1033 |  | 
|---|
| 1034 | void | 
|---|
| 1035 | _mesa_MatrixMode( GLenum mode ) | 
|---|
| 1036 | { | 
|---|
| 1037 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1038 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glMatrixMode"); | 
|---|
| 1039 | switch (mode) { | 
|---|
| 1040 | case GL_MODELVIEW: | 
|---|
| 1041 | case GL_PROJECTION: | 
|---|
| 1042 | case GL_TEXTURE: | 
|---|
| 1043 | ctx->Transform.MatrixMode = mode; | 
|---|
| 1044 | break; | 
|---|
| 1045 | default: | 
|---|
| 1046 | gl_error( ctx,  GL_INVALID_ENUM, "glMatrixMode" ); | 
|---|
| 1047 | } | 
|---|
| 1048 | } | 
|---|
| 1049 |  | 
|---|
| 1050 |  | 
|---|
| 1051 |  | 
|---|
| 1052 | void | 
|---|
| 1053 | _mesa_PushMatrix( void ) | 
|---|
| 1054 | { | 
|---|
| 1055 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1056 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushMatrix"); | 
|---|
| 1057 |  | 
|---|
| 1058 | if (MESA_VERBOSE&VERBOSE_API) | 
|---|
| 1059 | fprintf(stderr, "glPushMatrix %s\n", | 
|---|
| 1060 | gl_lookup_enum_by_nr(ctx->Transform.MatrixMode)); | 
|---|
| 1061 |  | 
|---|
| 1062 | switch (ctx->Transform.MatrixMode) { | 
|---|
| 1063 | case GL_MODELVIEW: | 
|---|
| 1064 | if (ctx->ModelViewStackDepth >= MAX_MODELVIEW_STACK_DEPTH - 1) { | 
|---|
| 1065 | gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix"); | 
|---|
| 1066 | return; | 
|---|
| 1067 | } | 
|---|
| 1068 | gl_matrix_copy( &ctx->ModelViewStack[ctx->ModelViewStackDepth++], | 
|---|
| 1069 | &ctx->ModelView ); | 
|---|
| 1070 | break; | 
|---|
| 1071 | case GL_PROJECTION: | 
|---|
| 1072 | if (ctx->ProjectionStackDepth >= MAX_PROJECTION_STACK_DEPTH - 1) { | 
|---|
| 1073 | gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix"); | 
|---|
| 1074 | return; | 
|---|
| 1075 | } | 
|---|
| 1076 | gl_matrix_copy( &ctx->ProjectionStack[ctx->ProjectionStackDepth++], | 
|---|
| 1077 | &ctx->ProjectionMatrix ); | 
|---|
| 1078 |  | 
|---|
| 1079 | /* Save near and far projection values */ | 
|---|
| 1080 | ctx->NearFarStack[ctx->ProjectionStackDepth][0] | 
|---|
| 1081 | = ctx->NearFarStack[ctx->ProjectionStackDepth-1][0]; | 
|---|
| 1082 | ctx->NearFarStack[ctx->ProjectionStackDepth][1] | 
|---|
| 1083 | = ctx->NearFarStack[ctx->ProjectionStackDepth-1][1]; | 
|---|
| 1084 | break; | 
|---|
| 1085 | case GL_TEXTURE: | 
|---|
| 1086 | { | 
|---|
| 1087 | GLuint t = ctx->Texture.CurrentTransformUnit; | 
|---|
| 1088 | if (ctx->TextureStackDepth[t] >= MAX_TEXTURE_STACK_DEPTH - 1) { | 
|---|
| 1089 | gl_error( ctx,  GL_STACK_OVERFLOW, "glPushMatrix"); | 
|---|
| 1090 | return; | 
|---|
| 1091 | } | 
|---|
| 1092 | gl_matrix_copy( &ctx->TextureStack[t][ctx->TextureStackDepth[t]++], | 
|---|
| 1093 | &ctx->TextureMatrix[t] ); | 
|---|
| 1094 | } | 
|---|
| 1095 | break; | 
|---|
| 1096 | default: | 
|---|
| 1097 | gl_problem(ctx, "Bad matrix mode in gl_PushMatrix"); | 
|---|
| 1098 | } | 
|---|
| 1099 | } | 
|---|
| 1100 |  | 
|---|
| 1101 |  | 
|---|
| 1102 |  | 
|---|
| 1103 | void | 
|---|
| 1104 | _mesa_PopMatrix( void ) | 
|---|
| 1105 | { | 
|---|
| 1106 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1107 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopMatrix"); | 
|---|
| 1108 |  | 
|---|
| 1109 | if (MESA_VERBOSE&VERBOSE_API) | 
|---|
| 1110 | fprintf(stderr, "glPopMatrix %s\n", | 
|---|
| 1111 | gl_lookup_enum_by_nr(ctx->Transform.MatrixMode)); | 
|---|
| 1112 |  | 
|---|
| 1113 | switch (ctx->Transform.MatrixMode) { | 
|---|
| 1114 | case GL_MODELVIEW: | 
|---|
| 1115 | if (ctx->ModelViewStackDepth==0) { | 
|---|
| 1116 | gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix"); | 
|---|
| 1117 | return; | 
|---|
| 1118 | } | 
|---|
| 1119 | gl_matrix_copy( &ctx->ModelView, | 
|---|
| 1120 | &ctx->ModelViewStack[--ctx->ModelViewStackDepth] ); | 
|---|
| 1121 | ctx->NewState |= NEW_MODELVIEW; | 
|---|
| 1122 | break; | 
|---|
| 1123 | case GL_PROJECTION: | 
|---|
| 1124 | if (ctx->ProjectionStackDepth==0) { | 
|---|
| 1125 | gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix"); | 
|---|
| 1126 | return; | 
|---|
| 1127 | } | 
|---|
| 1128 |  | 
|---|
| 1129 | gl_matrix_copy( &ctx->ProjectionMatrix, | 
|---|
| 1130 | &ctx->ProjectionStack[--ctx->ProjectionStackDepth] ); | 
|---|
| 1131 | ctx->NewState |= NEW_PROJECTION; | 
|---|
| 1132 |  | 
|---|
| 1133 | /* Device driver near/far values */ | 
|---|
| 1134 | { | 
|---|
| 1135 | GLfloat nearVal = ctx->NearFarStack[ctx->ProjectionStackDepth][0]; | 
|---|
| 1136 | GLfloat farVal  = ctx->NearFarStack[ctx->ProjectionStackDepth][1]; | 
|---|
| 1137 | if (ctx->Driver.NearFar) { | 
|---|
| 1138 | (*ctx->Driver.NearFar)( ctx, nearVal, farVal ); | 
|---|
| 1139 | } | 
|---|
| 1140 | } | 
|---|
| 1141 | break; | 
|---|
| 1142 | case GL_TEXTURE: | 
|---|
| 1143 | { | 
|---|
| 1144 | GLuint t = ctx->Texture.CurrentTransformUnit; | 
|---|
| 1145 | if (ctx->TextureStackDepth[t]==0) { | 
|---|
| 1146 | gl_error( ctx,  GL_STACK_UNDERFLOW, "glPopMatrix"); | 
|---|
| 1147 | return; | 
|---|
| 1148 | } | 
|---|
| 1149 | gl_matrix_copy(&ctx->TextureMatrix[t], | 
|---|
| 1150 | &ctx->TextureStack[t][--ctx->TextureStackDepth[t]]); | 
|---|
| 1151 | } | 
|---|
| 1152 | break; | 
|---|
| 1153 | default: | 
|---|
| 1154 | gl_problem(ctx, "Bad matrix mode in gl_PopMatrix"); | 
|---|
| 1155 | } | 
|---|
| 1156 | } | 
|---|
| 1157 |  | 
|---|
| 1158 |  | 
|---|
| 1159 |  | 
|---|
| 1160 | void | 
|---|
| 1161 | _mesa_LoadIdentity( void ) | 
|---|
| 1162 | { | 
|---|
| 1163 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1164 | GLmatrix *mat = 0; | 
|---|
| 1165 | GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadIdentity"); | 
|---|
| 1166 |  | 
|---|
| 1167 | MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) ); | 
|---|
| 1168 |  | 
|---|
| 1169 | if (mat->inv) | 
|---|
| 1170 | MEMCPY( mat->inv, Identity, 16*sizeof(GLfloat) ); | 
|---|
| 1171 |  | 
|---|
| 1172 | mat->type = MATRIX_IDENTITY; | 
|---|
| 1173 |  | 
|---|
| 1174 | /* Have to set this to dirty to make sure we recalculate the | 
|---|
| 1175 | * combined matrix later.  The update_matrix in this case is a | 
|---|
| 1176 | * shortcircuit anyway... | 
|---|
| 1177 | */ | 
|---|
| 1178 | mat->flags = MAT_DIRTY_DEPENDENTS; | 
|---|
| 1179 | } | 
|---|
| 1180 |  | 
|---|
| 1181 |  | 
|---|
| 1182 | void | 
|---|
| 1183 | _mesa_LoadMatrixf( const GLfloat *m ) | 
|---|
| 1184 | { | 
|---|
| 1185 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1186 | GLmatrix *mat = 0; | 
|---|
| 1187 | GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadMatrix"); | 
|---|
| 1188 |  | 
|---|
| 1189 | MEMCPY( mat->m, m, 16*sizeof(GLfloat) ); | 
|---|
| 1190 | mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER); | 
|---|
| 1191 |  | 
|---|
| 1192 | if (ctx->Transform.MatrixMode == GL_PROJECTION) { | 
|---|
| 1193 |  | 
|---|
| 1194 | #define M(row,col)  m[col*4+row] | 
|---|
| 1195 | GLfloat c = M(2,2); | 
|---|
| 1196 | GLfloat d = M(2,3); | 
|---|
| 1197 | #undef M | 
|---|
| 1198 | GLfloat n = (c ==  1.0 ? 0.0 : d / (c - 1.0)); | 
|---|
| 1199 | GLfloat f = (c == -1.0 ? 1.0 : d / (c + 1.0)); | 
|---|
| 1200 |  | 
|---|
| 1201 | /* Need to keep a stack of near/far values in case the user | 
|---|
| 1202 | * push/pops the projection matrix stack so that we can call | 
|---|
| 1203 | * Driver.NearFar() after a pop. | 
|---|
| 1204 | */ | 
|---|
| 1205 | ctx->NearFarStack[ctx->ProjectionStackDepth][0] = n; | 
|---|
| 1206 | ctx->NearFarStack[ctx->ProjectionStackDepth][1] = f; | 
|---|
| 1207 |  | 
|---|
| 1208 | if (ctx->Driver.NearFar) { | 
|---|
| 1209 | (*ctx->Driver.NearFar)( ctx, n, f ); | 
|---|
| 1210 | } | 
|---|
| 1211 | } | 
|---|
| 1212 | } | 
|---|
| 1213 |  | 
|---|
| 1214 |  | 
|---|
| 1215 | void | 
|---|
| 1216 | _mesa_LoadMatrixd( const GLdouble *m ) | 
|---|
| 1217 | { | 
|---|
| 1218 | GLfloat f[16]; | 
|---|
| 1219 | GLint i; | 
|---|
| 1220 | for (i = 0; i < 16; i++) | 
|---|
| 1221 | f[i] = m[i]; | 
|---|
| 1222 | _mesa_LoadMatrixf(f); | 
|---|
| 1223 | } | 
|---|
| 1224 |  | 
|---|
| 1225 |  | 
|---|
| 1226 |  | 
|---|
| 1227 | /* | 
|---|
| 1228 | * Multiply the active matrix by an arbitary matrix. | 
|---|
| 1229 | */ | 
|---|
| 1230 | void | 
|---|
| 1231 | _mesa_MultMatrixf( const GLfloat *m ) | 
|---|
| 1232 | { | 
|---|
| 1233 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1234 | GLmatrix *mat = 0; | 
|---|
| 1235 | GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glMultMatrix" ); | 
|---|
| 1236 | matmul4( mat->m, mat->m, m ); | 
|---|
| 1237 | mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER); | 
|---|
| 1238 | } | 
|---|
| 1239 |  | 
|---|
| 1240 |  | 
|---|
| 1241 | /* | 
|---|
| 1242 | * Multiply the active matrix by an arbitary matrix. | 
|---|
| 1243 | */ | 
|---|
| 1244 | void | 
|---|
| 1245 | _mesa_MultMatrixd( const GLdouble *m ) | 
|---|
| 1246 | { | 
|---|
| 1247 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1248 | GLmatrix *mat = 0; | 
|---|
| 1249 | GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glMultMatrix" ); | 
|---|
| 1250 | matmul4fd( mat->m, mat->m, m ); | 
|---|
| 1251 | mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY_ALL_OVER); | 
|---|
| 1252 | } | 
|---|
| 1253 |  | 
|---|
| 1254 |  | 
|---|
| 1255 |  | 
|---|
| 1256 |  | 
|---|
| 1257 | /* | 
|---|
| 1258 | * Multiply a matrix by an array of floats with known properties. | 
|---|
| 1259 | */ | 
|---|
| 1260 | void gl_mat_mul_floats( GLmatrix *mat, const GLfloat *m, GLuint flags ) | 
|---|
| 1261 | { | 
|---|
| 1262 | mat->flags |= (flags | | 
|---|
| 1263 | MAT_DIRTY_TYPE | | 
|---|
| 1264 | MAT_DIRTY_INVERSE | | 
|---|
| 1265 | MAT_DIRTY_DEPENDENTS); | 
|---|
| 1266 |  | 
|---|
| 1267 | if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) | 
|---|
| 1268 | matmul34( mat->m, mat->m, m ); | 
|---|
| 1269 | else | 
|---|
| 1270 | matmul4( mat->m, mat->m, m ); | 
|---|
| 1271 |  | 
|---|
| 1272 | } | 
|---|
| 1273 |  | 
|---|
| 1274 | /* | 
|---|
| 1275 | * Multiply a matrix by an array of floats with known properties. | 
|---|
| 1276 | */ | 
|---|
| 1277 | void gl_mat_mul_mat( GLmatrix *mat, const GLmatrix *m ) | 
|---|
| 1278 | { | 
|---|
| 1279 | mat->flags |= (m->flags | | 
|---|
| 1280 | MAT_DIRTY_TYPE | | 
|---|
| 1281 | MAT_DIRTY_INVERSE | | 
|---|
| 1282 | MAT_DIRTY_DEPENDENTS); | 
|---|
| 1283 |  | 
|---|
| 1284 | if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) | 
|---|
| 1285 | matmul34( mat->m, mat->m, m->m ); | 
|---|
| 1286 | else | 
|---|
| 1287 | matmul4( mat->m, mat->m, m->m ); | 
|---|
| 1288 | } | 
|---|
| 1289 |  | 
|---|
| 1290 |  | 
|---|
| 1291 |  | 
|---|
| 1292 | /* | 
|---|
| 1293 | * Execute a glRotate call | 
|---|
| 1294 | */ | 
|---|
| 1295 | void | 
|---|
| 1296 | _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z ) | 
|---|
| 1297 | { | 
|---|
| 1298 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1299 | GLfloat m[16]; | 
|---|
| 1300 | if (angle != 0.0F) { | 
|---|
| 1301 | GLmatrix *mat = 0; | 
|---|
| 1302 | GET_ACTIVE_MATRIX( ctx,  mat, ctx->NewState, "glRotate" ); | 
|---|
| 1303 |  | 
|---|
| 1304 | gl_rotation_matrix( angle, x, y, z, m ); | 
|---|
| 1305 | gl_mat_mul_floats( mat, m, MAT_FLAG_ROTATION ); | 
|---|
| 1306 | } | 
|---|
| 1307 | } | 
|---|
| 1308 |  | 
|---|
| 1309 | void | 
|---|
| 1310 | _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z ) | 
|---|
| 1311 | { | 
|---|
| 1312 | _mesa_Rotatef(angle, x, y, z); | 
|---|
| 1313 | } | 
|---|
| 1314 |  | 
|---|
| 1315 |  | 
|---|
| 1316 | /* | 
|---|
| 1317 | * Execute a glScale call | 
|---|
| 1318 | */ | 
|---|
| 1319 | void | 
|---|
| 1320 | _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z ) | 
|---|
| 1321 | { | 
|---|
| 1322 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1323 | GLmatrix *mat = 0; | 
|---|
| 1324 | GLfloat *m; | 
|---|
| 1325 | GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glScale"); | 
|---|
| 1326 |  | 
|---|
| 1327 | m = mat->m; | 
|---|
| 1328 | m[0] *= x;   m[4] *= y;   m[8]  *= z; | 
|---|
| 1329 | m[1] *= x;   m[5] *= y;   m[9]  *= z; | 
|---|
| 1330 | m[2] *= x;   m[6] *= y;   m[10] *= z; | 
|---|
| 1331 | m[3] *= x;   m[7] *= y;   m[11] *= z; | 
|---|
| 1332 |  | 
|---|
| 1333 | if (fabs(x - y) < 1e-8 && fabs(x - z) < 1e-8) | 
|---|
| 1334 | mat->flags |= MAT_FLAG_UNIFORM_SCALE; | 
|---|
| 1335 | else | 
|---|
| 1336 | mat->flags |= MAT_FLAG_GENERAL_SCALE; | 
|---|
| 1337 |  | 
|---|
| 1338 | mat->flags |= (MAT_DIRTY_TYPE | | 
|---|
| 1339 | MAT_DIRTY_INVERSE | | 
|---|
| 1340 | MAT_DIRTY_DEPENDENTS); | 
|---|
| 1341 | } | 
|---|
| 1342 |  | 
|---|
| 1343 |  | 
|---|
| 1344 | void | 
|---|
| 1345 | _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z ) | 
|---|
| 1346 | { | 
|---|
| 1347 | _mesa_Scalef(x, y, z); | 
|---|
| 1348 | } | 
|---|
| 1349 |  | 
|---|
| 1350 |  | 
|---|
| 1351 | /* | 
|---|
| 1352 | * Execute a glTranslate call | 
|---|
| 1353 | */ | 
|---|
| 1354 | void | 
|---|
| 1355 | _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z ) | 
|---|
| 1356 | { | 
|---|
| 1357 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1358 | GLmatrix *mat = 0; | 
|---|
| 1359 | GLfloat *m; | 
|---|
| 1360 | GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glTranslate"); | 
|---|
| 1361 | m = mat->m; | 
|---|
| 1362 | m[12] = m[0] * x + m[4] * y + m[8]  * z + m[12]; | 
|---|
| 1363 | m[13] = m[1] * x + m[5] * y + m[9]  * z + m[13]; | 
|---|
| 1364 | m[14] = m[2] * x + m[6] * y + m[10] * z + m[14]; | 
|---|
| 1365 | m[15] = m[3] * x + m[7] * y + m[11] * z + m[15]; | 
|---|
| 1366 |  | 
|---|
| 1367 | mat->flags |= (MAT_FLAG_TRANSLATION | | 
|---|
| 1368 | MAT_DIRTY_TYPE | | 
|---|
| 1369 | MAT_DIRTY_INVERSE | | 
|---|
| 1370 | MAT_DIRTY_DEPENDENTS); | 
|---|
| 1371 | } | 
|---|
| 1372 |  | 
|---|
| 1373 |  | 
|---|
| 1374 | void | 
|---|
| 1375 | _mesa_Translated( GLdouble x, GLdouble y, GLdouble z ) | 
|---|
| 1376 | { | 
|---|
| 1377 | _mesa_Translatef(x, y, z); | 
|---|
| 1378 | } | 
|---|
| 1379 |  | 
|---|
| 1380 |  | 
|---|
| 1381 |  | 
|---|
| 1382 | void | 
|---|
| 1383 | _mesa_LoadTransposeMatrixfARB( const GLfloat *m ) | 
|---|
| 1384 | { | 
|---|
| 1385 | GLfloat tm[16]; | 
|---|
| 1386 | gl_matrix_transposef(tm, m); | 
|---|
| 1387 | _mesa_LoadMatrixf(tm); | 
|---|
| 1388 | } | 
|---|
| 1389 |  | 
|---|
| 1390 |  | 
|---|
| 1391 | void | 
|---|
| 1392 | _mesa_LoadTransposeMatrixdARB( const GLdouble *m ) | 
|---|
| 1393 | { | 
|---|
| 1394 | GLdouble tm[16]; | 
|---|
| 1395 | gl_matrix_transposed(tm, m); | 
|---|
| 1396 | _mesa_LoadMatrixd(tm); | 
|---|
| 1397 | } | 
|---|
| 1398 |  | 
|---|
| 1399 |  | 
|---|
| 1400 | void | 
|---|
| 1401 | _mesa_MultTransposeMatrixfARB( const GLfloat *m ) | 
|---|
| 1402 | { | 
|---|
| 1403 | GLfloat tm[16]; | 
|---|
| 1404 | gl_matrix_transposef(tm, m); | 
|---|
| 1405 | _mesa_MultMatrixf(tm); | 
|---|
| 1406 | } | 
|---|
| 1407 |  | 
|---|
| 1408 |  | 
|---|
| 1409 | void | 
|---|
| 1410 | _mesa_MultTransposeMatrixdARB( const GLdouble *m ) | 
|---|
| 1411 | { | 
|---|
| 1412 | GLdouble tm[16]; | 
|---|
| 1413 | gl_matrix_transposed(tm, m); | 
|---|
| 1414 | _mesa_MultMatrixd(tm); | 
|---|
| 1415 | } | 
|---|
| 1416 |  | 
|---|
| 1417 |  | 
|---|
| 1418 | /* | 
|---|
| 1419 | * Called via glViewport or display list execution. | 
|---|
| 1420 | */ | 
|---|
| 1421 | void | 
|---|
| 1422 | _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height ) | 
|---|
| 1423 | { | 
|---|
| 1424 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1425 | gl_Viewport(ctx, x, y, width, height); | 
|---|
| 1426 | } | 
|---|
| 1427 |  | 
|---|
| 1428 |  | 
|---|
| 1429 |  | 
|---|
| 1430 | /* | 
|---|
| 1431 | * Define a new viewport and reallocate auxillary buffers if the size of | 
|---|
| 1432 | * the window (color buffer) has changed. | 
|---|
| 1433 | * | 
|---|
| 1434 | * XXX This is directly called by device drivers, BUT this function | 
|---|
| 1435 | * may be renamed _mesa_Viewport (without ctx arg) in the future so | 
|---|
| 1436 | * use of _mesa_Viewport is encouraged. | 
|---|
| 1437 | */ | 
|---|
| 1438 | void | 
|---|
| 1439 | gl_Viewport( GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height ) | 
|---|
| 1440 | { | 
|---|
| 1441 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glViewport"); | 
|---|
| 1442 |  | 
|---|
| 1443 | if (width<0 || height<0) { | 
|---|
| 1444 | gl_error( ctx,  GL_INVALID_VALUE, "glViewport" ); | 
|---|
| 1445 | return; | 
|---|
| 1446 | } | 
|---|
| 1447 |  | 
|---|
| 1448 | if (MESA_VERBOSE & VERBOSE_API) | 
|---|
| 1449 | fprintf(stderr, "glViewport %d %d %d %d\n", x, y, width, height); | 
|---|
| 1450 |  | 
|---|
| 1451 | /* clamp width, and height to implementation dependent range */ | 
|---|
| 1452 | width  = CLAMP( width,  1, MAX_WIDTH ); | 
|---|
| 1453 | height = CLAMP( height, 1, MAX_HEIGHT ); | 
|---|
| 1454 |  | 
|---|
| 1455 | /* Save viewport */ | 
|---|
| 1456 | ctx->Viewport.X = x; | 
|---|
| 1457 | ctx->Viewport.Width = width; | 
|---|
| 1458 | ctx->Viewport.Y = y; | 
|---|
| 1459 | ctx->Viewport.Height = height; | 
|---|
| 1460 |  | 
|---|
| 1461 | /* compute scale and bias values */ | 
|---|
| 1462 | ctx->Viewport.WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F; | 
|---|
| 1463 | ctx->Viewport.WindowMap.m[MAT_TX] = ctx->Viewport.WindowMap.m[MAT_SX] + x; | 
|---|
| 1464 | ctx->Viewport.WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F; | 
|---|
| 1465 | ctx->Viewport.WindowMap.m[MAT_TY] = ctx->Viewport.WindowMap.m[MAT_SY] + y; | 
|---|
| 1466 | ctx->Viewport.WindowMap.m[MAT_SZ] = 0.5 * ctx->Visual->DepthMaxF; | 
|---|
| 1467 | ctx->Viewport.WindowMap.m[MAT_TZ] = 0.5 * ctx->Visual->DepthMaxF; | 
|---|
| 1468 |  | 
|---|
| 1469 | ctx->Viewport.WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION; | 
|---|
| 1470 | ctx->Viewport.WindowMap.type = MATRIX_3D_NO_ROT; | 
|---|
| 1471 |  | 
|---|
| 1472 | ctx->ModelProjectWinMatrixUptodate = GL_FALSE; | 
|---|
| 1473 | ctx->NewState |= NEW_VIEWPORT; | 
|---|
| 1474 |  | 
|---|
| 1475 | /* Check if window/buffer has been resized and if so, reallocate the | 
|---|
| 1476 | * ancillary buffers. | 
|---|
| 1477 | */ | 
|---|
| 1478 | _mesa_ResizeBuffersMESA(); | 
|---|
| 1479 |  | 
|---|
| 1480 |  | 
|---|
| 1481 | ctx->RasterMask &= ~WINCLIP_BIT; | 
|---|
| 1482 |  | 
|---|
| 1483 | if (   ctx->Viewport.X<0 | 
|---|
| 1484 | || ctx->Viewport.X + ctx->Viewport.Width > ctx->DrawBuffer->Width | 
|---|
| 1485 | || ctx->Viewport.Y<0 | 
|---|
| 1486 | || ctx->Viewport.Y + ctx->Viewport.Height > ctx->DrawBuffer->Height) { | 
|---|
| 1487 | ctx->RasterMask |= WINCLIP_BIT; | 
|---|
| 1488 | } | 
|---|
| 1489 |  | 
|---|
| 1490 |  | 
|---|
| 1491 | if (ctx->Driver.Viewport) { | 
|---|
| 1492 | (*ctx->Driver.Viewport)( ctx, x, y, width, height ); | 
|---|
| 1493 | } | 
|---|
| 1494 | } | 
|---|
| 1495 |  | 
|---|
| 1496 |  | 
|---|
| 1497 |  | 
|---|
| 1498 | void | 
|---|
| 1499 | _mesa_DepthRange( GLclampd nearval, GLclampd farval ) | 
|---|
| 1500 | { | 
|---|
| 1501 | /* | 
|---|
| 1502 | * nearval - specifies mapping of the near clipping plane to window | 
|---|
| 1503 | *   coordinates, default is 0 | 
|---|
| 1504 | * farval - specifies mapping of the far clipping plane to window | 
|---|
| 1505 | *   coordinates, default is 1 | 
|---|
| 1506 | * | 
|---|
| 1507 | * After clipping and div by w, z coords are in -1.0 to 1.0, | 
|---|
| 1508 | * corresponding to near and far clipping planes.  glDepthRange | 
|---|
| 1509 | * specifies a linear mapping of the normalized z coords in | 
|---|
| 1510 | * this range to window z coords. | 
|---|
| 1511 | */ | 
|---|
| 1512 | GLfloat n, f; | 
|---|
| 1513 | GET_CURRENT_CONTEXT(ctx); | 
|---|
| 1514 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthRange"); | 
|---|
| 1515 |  | 
|---|
| 1516 | if (MESA_VERBOSE&VERBOSE_API) | 
|---|
| 1517 | fprintf(stderr, "glDepthRange %f %f\n", nearval, farval); | 
|---|
| 1518 |  | 
|---|
| 1519 | n = (GLfloat) CLAMP( nearval, 0.0, 1.0 ); | 
|---|
| 1520 | f = (GLfloat) CLAMP( farval, 0.0, 1.0 ); | 
|---|
| 1521 |  | 
|---|
| 1522 | ctx->Viewport.Near = n; | 
|---|
| 1523 | ctx->Viewport.Far = f; | 
|---|
| 1524 | ctx->Viewport.WindowMap.m[MAT_SZ] = ctx->Visual->DepthMaxF * ((f - n) / 2.0); | 
|---|
| 1525 | ctx->Viewport.WindowMap.m[MAT_TZ] = ctx->Visual->DepthMaxF * ((f - n) / 2.0 + n); | 
|---|
| 1526 |  | 
|---|
| 1527 | ctx->ModelProjectWinMatrixUptodate = GL_FALSE; | 
|---|
| 1528 |  | 
|---|
| 1529 | if (ctx->Driver.DepthRange) { | 
|---|
| 1530 | (*ctx->Driver.DepthRange)( ctx, nearval, farval ); | 
|---|
| 1531 | } | 
|---|
| 1532 | } | 
|---|
| 1533 |  | 
|---|
| 1534 |  | 
|---|
| 1535 | void gl_calculate_model_project_matrix( GLcontext *ctx ) | 
|---|
| 1536 | { | 
|---|
| 1537 | gl_matrix_mul( &ctx->ModelProjectMatrix, | 
|---|
| 1538 | &ctx->ProjectionMatrix, | 
|---|
| 1539 | &ctx->ModelView ); | 
|---|
| 1540 |  | 
|---|
| 1541 | gl_matrix_analyze( &ctx->ModelProjectMatrix ); | 
|---|
| 1542 | } | 
|---|
| 1543 |  | 
|---|
| 1544 |  | 
|---|
| 1545 | void gl_matrix_ctr( GLmatrix *m ) | 
|---|
| 1546 | { | 
|---|
| 1547 | m->inv = 0; | 
|---|
| 1548 | MEMCPY( m->m, Identity, sizeof(Identity)); | 
|---|
| 1549 | m->type = MATRIX_IDENTITY; | 
|---|
| 1550 | m->flags = MAT_DIRTY_DEPENDENTS; | 
|---|
| 1551 | } | 
|---|
| 1552 |  | 
|---|
| 1553 | void gl_matrix_dtr( GLmatrix *m ) | 
|---|
| 1554 | { | 
|---|
| 1555 | if (m->inv != 0) { | 
|---|
| 1556 | FREE(m->inv); | 
|---|
| 1557 | m->inv = 0; | 
|---|
| 1558 | } | 
|---|
| 1559 | } | 
|---|
| 1560 |  | 
|---|
| 1561 | #if 0 | 
|---|
| 1562 | void gl_matrix_set_identity( GLmatrix *m ) | 
|---|
| 1563 | { | 
|---|
| 1564 | MEMCPY( m->m, Identity, sizeof(Identity)); | 
|---|
| 1565 | m->type = MATRIX_IDENTITY; | 
|---|
| 1566 | m->flags = MAT_DIRTY_DEPENDENTS; | 
|---|
| 1567 | } | 
|---|
| 1568 | #endif | 
|---|
| 1569 |  | 
|---|
| 1570 | void gl_matrix_alloc_inv( GLmatrix *m ) | 
|---|
| 1571 | { | 
|---|
| 1572 | if (m->inv == 0) { | 
|---|
| 1573 | m->inv = (GLfloat *)MALLOC(16*sizeof(GLfloat)); | 
|---|
| 1574 | MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) ); | 
|---|
| 1575 | } | 
|---|
| 1576 | } | 
|---|
| 1577 |  | 
|---|
| 1578 | void gl_matrix_copy( GLmatrix *to, const GLmatrix *from ) | 
|---|
| 1579 | { | 
|---|
| 1580 | MEMCPY( to->m, from->m, sizeof(Identity)); | 
|---|
| 1581 | to->flags = from->flags | MAT_DIRTY_DEPENDENTS; | 
|---|
| 1582 | to->type = from->type; | 
|---|
| 1583 |  | 
|---|
| 1584 | if (to->inv != 0) { | 
|---|
| 1585 | if (from->inv == 0) { | 
|---|
| 1586 | gl_matrix_invert( to ); | 
|---|
| 1587 | } else { | 
|---|
| 1588 | MEMCPY(to->inv, from->inv, sizeof(GLfloat)*16); | 
|---|
| 1589 | } | 
|---|
| 1590 | } | 
|---|
| 1591 | } | 
|---|
| 1592 |  | 
|---|
| 1593 | void gl_matrix_mul( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b ) | 
|---|
| 1594 | { | 
|---|
| 1595 | dest->flags = (a->flags | | 
|---|
| 1596 | b->flags | | 
|---|
| 1597 | MAT_DIRTY_TYPE | | 
|---|
| 1598 | MAT_DIRTY_INVERSE | | 
|---|
| 1599 | MAT_DIRTY_DEPENDENTS); | 
|---|
| 1600 |  | 
|---|
| 1601 | if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D)) | 
|---|
| 1602 | matmul34( dest->m, a->m, b->m ); | 
|---|
| 1603 | else | 
|---|
| 1604 | matmul4( dest->m, a->m, b->m ); | 
|---|
| 1605 | } | 
|---|