| 1 |
/* $Id: matrix.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.2 |
| 6 |
* Copyright (C) 1995-1997 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* Matrix operations |
| 25 |
* |
| 26 |
* |
| 27 |
* NOTES: |
| 28 |
* 1. 4x4 transformation matrices are stored in memory in column major order. |
| 29 |
* 2. Points/vertices are to be thought of as column vectors. |
| 30 |
* 3. Transformation of a point p by a matrix M is: p' = M * p |
| 31 |
* |
| 32 |
*/ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include <math.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include "context.h" |
| 41 |
#include "dlist.h" |
| 42 |
#include "macros.h" |
| 43 |
#include "matrix.h" |
| 44 |
#include "types.h" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static GLfloat Identity[16] = { |
| 49 |
1.0, 0.0, 0.0, 0.0, |
| 50 |
0.0, 1.0, 0.0, 0.0, |
| 51 |
0.0, 0.0, 1.0, 0.0, |
| 52 |
0.0, 0.0, 0.0, 1.0 |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#ifdef DEBUG |
| 59 |
static void print_matrix( const GLfloat m[16] ) |
| 60 |
{ |
| 61 |
int i; |
| 62 |
|
| 63 |
for (i=0;i<4;i++) { |
| 64 |
printf("%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] ); |
| 65 |
} |
| 66 |
} |
| 67 |
#endif |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
/* |
| 72 |
* Perform a 4x4 matrix multiplication (product = a x b). |
| 73 |
* Input: a, b - matrices to multiply |
| 74 |
* Output: product - product of a and b |
| 75 |
* WARNING: (product != b) assumed |
| 76 |
* NOTE: (product == a) allowed |
| 77 |
*/ |
| 78 |
static void matmul( GLfloat *product, const GLfloat *a, const GLfloat *b ) |
| 79 |
{ |
| 80 |
/* This matmul was contributed by Thomas Malik */ |
| 81 |
GLint i; |
| 82 |
|
| 83 |
#define A(row,col) a[(col<<2)+row] |
| 84 |
#define B(row,col) b[(col<<2)+row] |
| 85 |
#define P(row,col) product[(col<<2)+row] |
| 86 |
|
| 87 |
/* i-te Zeile */ |
| 88 |
for (i = 0; i < 4; i++) { |
| 89 |
GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); |
| 90 |
P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); |
| 91 |
P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); |
| 92 |
P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); |
| 93 |
P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); |
| 94 |
} |
| 95 |
|
| 96 |
#undef A |
| 97 |
#undef B |
| 98 |
#undef P |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
/* |
| 104 |
* Compute the inverse of a 4x4 matrix. Contributed by [email protected] |
| 105 |
*/ |
| 106 |
static void invert_matrix_general( const GLfloat *m, GLfloat *out ) |
| 107 |
{ |
| 108 |
|
| 109 |
/* NB. OpenGL Matrices are COLUMN major. */ |
| 110 |
#define MAT(m,r,c) (m)[(c)*4+(r)] |
| 111 |
|
| 112 |
/* Here's some shorthand converting standard (row,column) to index. */ |
| 113 |
#define m11 MAT(m,0,0) |
| 114 |
#define m12 MAT(m,0,1) |
| 115 |
#define m13 MAT(m,0,2) |
| 116 |
#define m14 MAT(m,0,3) |
| 117 |
#define m21 MAT(m,1,0) |
| 118 |
#define m22 MAT(m,1,1) |
| 119 |
#define m23 MAT(m,1,2) |
| 120 |
#define m24 MAT(m,1,3) |
| 121 |
#define m31 MAT(m,2,0) |
| 122 |
#define m32 MAT(m,2,1) |
| 123 |
#define m33 MAT(m,2,2) |
| 124 |
#define m34 MAT(m,2,3) |
| 125 |
#define m41 MAT(m,3,0) |
| 126 |
#define m42 MAT(m,3,1) |
| 127 |
#define m43 MAT(m,3,2) |
| 128 |
#define m44 MAT(m,3,3) |
| 129 |
|
| 130 |
GLfloat det; |
| 131 |
GLfloat d12, d13, d23, d24, d34, d41; |
| 132 |
GLfloat tmp[16]; /* Allow out == in. */ |
| 133 |
|
| 134 |
/* Inverse = adjoint / det. (See linear algebra texts.)*/ |
| 135 |
|
| 136 |
/* pre-compute 2x2 dets for last two rows when computing */ |
| 137 |
/* cofactors of first two rows. */ |
| 138 |
d12 = (m31*m42-m41*m32); |
| 139 |
d13 = (m31*m43-m41*m33); |
| 140 |
d23 = (m32*m43-m42*m33); |
| 141 |
d24 = (m32*m44-m42*m34); |
| 142 |
d34 = (m33*m44-m43*m34); |
| 143 |
d41 = (m34*m41-m44*m31); |
| 144 |
|
| 145 |
tmp[0] = (m22 * d34 - m23 * d24 + m24 * d23); |
| 146 |
tmp[1] = -(m21 * d34 + m23 * d41 + m24 * d13); |
| 147 |
tmp[2] = (m21 * d24 + m22 * d41 + m24 * d12); |
| 148 |
tmp[3] = -(m21 * d23 - m22 * d13 + m23 * d12); |
| 149 |
|
| 150 |
/* Compute determinant as early as possible using these cofactors. */ |
| 151 |
det = m11 * tmp[0] + m12 * tmp[1] + m13 * tmp[2] + m14 * tmp[3]; |
| 152 |
|
| 153 |
/* Run singularity test. */ |
| 154 |
if (det == 0.0F) { |
| 155 |
/* printf("invert_matrix: Warning: Singular matrix.\n"); */ |
| 156 |
MEMCPY( out, Identity, 16*sizeof(GLfloat) ); |
| 157 |
} |
| 158 |
else { |
| 159 |
GLfloat invDet = 1.0F / det; |
| 160 |
/* Compute rest of inverse. */ |
| 161 |
tmp[0] *= invDet; |
| 162 |
tmp[1] *= invDet; |
| 163 |
tmp[2] *= invDet; |
| 164 |
tmp[3] *= invDet; |
| 165 |
|
| 166 |
tmp[4] = -(m12 * d34 - m13 * d24 + m14 * d23) * invDet; |
| 167 |
tmp[5] = (m11 * d34 + m13 * d41 + m14 * d13) * invDet; |
| 168 |
tmp[6] = -(m11 * d24 + m12 * d41 + m14 * d12) * invDet; |
| 169 |
tmp[7] = (m11 * d23 - m12 * d13 + m13 * d12) * invDet; |
| 170 |
|
| 171 |
/* Pre-compute 2x2 dets for first two rows when computing */ |
| 172 |
/* cofactors of last two rows. */ |
| 173 |
d12 = m11*m22-m21*m12; |
| 174 |
d13 = m11*m23-m21*m13; |
| 175 |
d23 = m12*m23-m22*m13; |
| 176 |
d24 = m12*m24-m22*m14; |
| 177 |
d34 = m13*m24-m23*m14; |
| 178 |
d41 = m14*m21-m24*m11; |
| 179 |
|
| 180 |
tmp[8] = (m42 * d34 - m43 * d24 + m44 * d23) * invDet; |
| 181 |
tmp[9] = -(m41 * d34 + m43 * d41 + m44 * d13) * invDet; |
| 182 |
tmp[10] = (m41 * d24 + m42 * d41 + m44 * d12) * invDet; |
| 183 |
tmp[11] = -(m41 * d23 - m42 * d13 + m43 * d12) * invDet; |
| 184 |
tmp[12] = -(m32 * d34 - m33 * d24 + m34 * d23) * invDet; |
| 185 |
tmp[13] = (m31 * d34 + m33 * d41 + m34 * d13) * invDet; |
| 186 |
tmp[14] = -(m31 * d24 + m32 * d41 + m34 * d12) * invDet; |
| 187 |
tmp[15] = (m31 * d23 - m32 * d13 + m33 * d12) * invDet; |
| 188 |
|
| 189 |
MEMCPY(out, tmp, 16*sizeof(GLfloat)); |
| 190 |
} |
| 191 |
|
| 192 |
#undef m11 |
| 193 |
#undef m12 |
| 194 |
#undef m13 |
| 195 |
#undef m14 |
| 196 |
#undef m21 |
| 197 |
#undef m22 |
| 198 |
#undef m23 |
| 199 |
#undef m24 |
| 200 |
#undef m31 |
| 201 |
#undef m32 |
| 202 |
#undef m33 |
| 203 |
#undef m34 |
| 204 |
#undef m41 |
| 205 |
#undef m42 |
| 206 |
#undef m43 |
| 207 |
#undef m44 |
| 208 |
#undef MAT |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/* |
| 213 |
* Invert matrix m. This algorithm contributed by Stephane Rehel |
| 214 |
* <[email protected]> |
| 215 |
*/ |
| 216 |
static void invert_matrix( const GLfloat *m, GLfloat *out ) |
| 217 |
{ |
| 218 |
/* NB. OpenGL Matrices are COLUMN major. */ |
| 219 |
#define MAT(m,r,c) (m)[(c)*4+(r)] |
| 220 |
|
| 221 |
/* Here's some shorthand converting standard (row,column) to index. */ |
| 222 |
#define m11 MAT(m,0,0) |
| 223 |
#define m12 MAT(m,0,1) |
| 224 |
#define m13 MAT(m,0,2) |
| 225 |
#define m14 MAT(m,0,3) |
| 226 |
#define m21 MAT(m,1,0) |
| 227 |
#define m22 MAT(m,1,1) |
| 228 |
#define m23 MAT(m,1,2) |
| 229 |
#define m24 MAT(m,1,3) |
| 230 |
#define m31 MAT(m,2,0) |
| 231 |
#define m32 MAT(m,2,1) |
| 232 |
#define m33 MAT(m,2,2) |
| 233 |
#define m34 MAT(m,2,3) |
| 234 |
#define m41 MAT(m,3,0) |
| 235 |
#define m42 MAT(m,3,1) |
| 236 |
#define m43 MAT(m,3,2) |
| 237 |
#define m44 MAT(m,3,3) |
| 238 |
|
| 239 |
register GLfloat det; |
| 240 |
GLfloat tmp[16]; /* Allow out == in. */ |
| 241 |
|
| 242 |
if( m41 != 0. || m42 != 0. || m43 != 0. || m44 != 1. ) { |
| 243 |
invert_matrix_general(m, out); |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
/* Inverse = adjoint / det. (See linear algebra texts.)*/ |
| 248 |
|
| 249 |
tmp[0]= m22 * m33 - m23 * m32; |
| 250 |
tmp[1]= m23 * m31 - m21 * m33; |
| 251 |
tmp[2]= m21 * m32 - m22 * m31; |
| 252 |
|
| 253 |
/* Compute determinant as early as possible using these cofactors. */ |
| 254 |
det= m11 * tmp[0] + m12 * tmp[1] + m13 * tmp[2]; |
| 255 |
|
| 256 |
/* Run singularity test. */ |
| 257 |
if (det == 0.0F) { |
| 258 |
/* printf("invert_matrix: Warning: Singular matrix.\n"); */ |
| 259 |
MEMCPY( out, Identity, 16*sizeof(GLfloat) ); |
| 260 |
} |
| 261 |
else { |
| 262 |
GLfloat d12, d13, d23, d24, d34, d41; |
| 263 |
register GLfloat im11, im12, im13, im14; |
| 264 |
|
| 265 |
det= 1. / det; |
| 266 |
|
| 267 |
/* Compute rest of inverse. */ |
| 268 |
tmp[0] *= det; |
| 269 |
tmp[1] *= det; |
| 270 |
tmp[2] *= det; |
| 271 |
tmp[3] = 0.; |
| 272 |
|
| 273 |
im11= m11 * det; |
| 274 |
im12= m12 * det; |
| 275 |
im13= m13 * det; |
| 276 |
im14= m14 * det; |
| 277 |
tmp[4] = im13 * m32 - im12 * m33; |
| 278 |
tmp[5] = im11 * m33 - im13 * m31; |
| 279 |
tmp[6] = im12 * m31 - im11 * m32; |
| 280 |
tmp[7] = 0.; |
| 281 |
|
| 282 |
/* Pre-compute 2x2 dets for first two rows when computing */ |
| 283 |
/* cofactors of last two rows. */ |
| 284 |
d12 = im11*m22 - m21*im12; |
| 285 |
d13 = im11*m23 - m21*im13; |
| 286 |
d23 = im12*m23 - m22*im13; |
| 287 |
d24 = im12*m24 - m22*im14; |
| 288 |
d34 = im13*m24 - m23*im14; |
| 289 |
d41 = im14*m21 - m24*im11; |
| 290 |
|
| 291 |
tmp[8] = d23; |
| 292 |
tmp[9] = -d13; |
| 293 |
tmp[10] = d12; |
| 294 |
tmp[11] = 0.; |
| 295 |
|
| 296 |
tmp[12] = -(m32 * d34 - m33 * d24 + m34 * d23); |
| 297 |
tmp[13] = (m31 * d34 + m33 * d41 + m34 * d13); |
| 298 |
tmp[14] = -(m31 * d24 + m32 * d41 + m34 * d12); |
| 299 |
tmp[15] = 1.; |
| 300 |
|
| 301 |
MEMCPY(out, tmp, 16*sizeof(GLfloat)); |
| 302 |
} |
| 303 |
|
| 304 |
#undef m11 |
| 305 |
#undef m12 |
| 306 |
#undef m13 |
| 307 |
#undef m14 |
| 308 |
#undef m21 |
| 309 |
#undef m22 |
| 310 |
#undef m23 |
| 311 |
#undef m24 |
| 312 |
#undef m31 |
| 313 |
#undef m32 |
| 314 |
#undef m33 |
| 315 |
#undef m34 |
| 316 |
#undef m41 |
| 317 |
#undef m42 |
| 318 |
#undef m43 |
| 319 |
#undef m44 |
| 320 |
#undef MAT |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
/* |
| 325 |
* Compute the inverse of the current ModelViewMatrix. |
| 326 |
*/ |
| 327 |
void gl_compute_modelview_inverse( GLcontext *ctx ) |
| 328 |
{ |
| 329 |
invert_matrix( ctx->ModelViewMatrix, ctx->ModelViewInv ); |
| 330 |
ctx->ModelViewInvValid = GL_TRUE; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
/* |
| 337 |
* Determine if the given matrix is the identity matrix. |
| 338 |
*/ |
| 339 |
static GLboolean is_identity( const GLfloat m[16] ) |
| 340 |
{ |
| 341 |
if ( m[0]==1.0F && m[4]==0.0F && m[ 8]==0.0F && m[12]==0.0F |
| 342 |
&& m[1]==0.0F && m[5]==1.0F && m[ 9]==0.0F && m[13]==0.0F |
| 343 |
&& m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F |
| 344 |
&& m[3]==0.0F && m[7]==0.0F && m[11]==0.0F && m[15]==1.0F) { |
| 345 |
return GL_TRUE; |
| 346 |
} |
| 347 |
else { |
| 348 |
return GL_FALSE; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
void gl_Frustum( GLcontext *ctx, |
| 356 |
GLdouble left, GLdouble right, |
| 357 |
GLdouble bottom, GLdouble top, |
| 358 |
GLdouble nearval, GLdouble farval ) |
| 359 |
{ |
| 360 |
GLfloat x, y, a, b, c, d; |
| 361 |
GLfloat m[16]; |
| 362 |
|
| 363 |
if (nearval<=0.0 || farval<=0.0) { |
| 364 |
gl_error( ctx, GL_INVALID_VALUE, "glFrustum(near or far)" ); |
| 365 |
} |
| 366 |
|
| 367 |
x = (2.0*nearval) / (right-left); |
| 368 |
y = (2.0*nearval) / (top-bottom); |
| 369 |
a = (right+left) / (right-left); |
| 370 |
b = (top+bottom) / (top-bottom); |
| 371 |
c = -(farval+nearval) / ( farval-nearval); |
| 372 |
d = -(2.0*farval*nearval) / (farval-nearval); /* error? */ |
| 373 |
|
| 374 |
#define M(row,col) m[col*4+row] |
| 375 |
M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F; |
| 376 |
M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F; |
| 377 |
M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d; |
| 378 |
M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F; |
| 379 |
#undef M |
| 380 |
|
| 381 |
gl_MultMatrixf( ctx, m ); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
void gl_MatrixMode( GLcontext *ctx, GLenum mode ) |
| 387 |
{ |
| 388 |
if (INSIDE_BEGIN_END(ctx)) { |
| 389 |
gl_error( ctx, GL_INVALID_OPERATION, "glMatrixMode" ); |
| 390 |
return; |
| 391 |
} |
| 392 |
switch (mode) { |
| 393 |
case GL_MODELVIEW: |
| 394 |
case GL_PROJECTION: |
| 395 |
case GL_TEXTURE: |
| 396 |
ctx->Transform.MatrixMode = mode; |
| 397 |
break; |
| 398 |
default: |
| 399 |
gl_error( ctx, GL_INVALID_ENUM, "glMatrixMode" ); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
void gl_PushMatrix( GLcontext *ctx ) |
| 406 |
{ |
| 407 |
if (INSIDE_BEGIN_END(ctx)) { |
| 408 |
gl_error( ctx, GL_INVALID_OPERATION, "glPushMatrix" ); |
| 409 |
return; |
| 410 |
} |
| 411 |
switch (ctx->Transform.MatrixMode) { |
| 412 |
case GL_MODELVIEW: |
| 413 |
if (ctx->ModelViewStackDepth>=MAX_MODELVIEW_STACK_DEPTH-1) { |
| 414 |
gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix"); |
| 415 |
return; |
| 416 |
} |
| 417 |
MEMCPY( ctx->ModelViewStack[ctx->ModelViewStackDepth], |
| 418 |
ctx->ModelViewMatrix, |
| 419 |
16*sizeof(GLfloat) ); |
| 420 |
ctx->ModelViewStackDepth++; |
| 421 |
break; |
| 422 |
case GL_PROJECTION: |
| 423 |
if (ctx->ProjectionStackDepth>=MAX_PROJECTION_STACK_DEPTH) { |
| 424 |
gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix"); |
| 425 |
return; |
| 426 |
} |
| 427 |
MEMCPY( ctx->ProjectionStack[ctx->ProjectionStackDepth], |
| 428 |
ctx->ProjectionMatrix, |
| 429 |
16*sizeof(GLfloat) ); |
| 430 |
ctx->ProjectionStackDepth++; |
| 431 |
break; |
| 432 |
case GL_TEXTURE: |
| 433 |
if (ctx->TextureStackDepth>=MAX_TEXTURE_STACK_DEPTH) { |
| 434 |
gl_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix"); |
| 435 |
return; |
| 436 |
} |
| 437 |
MEMCPY( ctx->TextureStack[ctx->TextureStackDepth], |
| 438 |
ctx->TextureMatrix, |
| 439 |
16*sizeof(GLfloat) ); |
| 440 |
ctx->TextureStackDepth++; |
| 441 |
break; |
| 442 |
default: |
| 443 |
abort(); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
void gl_PopMatrix( GLcontext *ctx ) |
| 450 |
{ |
| 451 |
if (INSIDE_BEGIN_END(ctx)) { |
| 452 |
gl_error( ctx, GL_INVALID_OPERATION, "glPopMatrix" ); |
| 453 |
return; |
| 454 |
} |
| 455 |
switch (ctx->Transform.MatrixMode) { |
| 456 |
case GL_MODELVIEW: |
| 457 |
if (ctx->ModelViewStackDepth==0) { |
| 458 |
gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix"); |
| 459 |
return; |
| 460 |
} |
| 461 |
ctx->ModelViewStackDepth--; |
| 462 |
MEMCPY( ctx->ModelViewMatrix, |
| 463 |
ctx->ModelViewStack[ctx->ModelViewStackDepth], |
| 464 |
16*sizeof(GLfloat) ); |
| 465 |
ctx->ModelViewInvValid = GL_FALSE; |
| 466 |
break; |
| 467 |
case GL_PROJECTION: |
| 468 |
if (ctx->ProjectionStackDepth==0) { |
| 469 |
gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix"); |
| 470 |
return; |
| 471 |
} |
| 472 |
ctx->ProjectionStackDepth--; |
| 473 |
MEMCPY( ctx->ProjectionMatrix, |
| 474 |
ctx->ProjectionStack[ctx->ProjectionStackDepth], |
| 475 |
16*sizeof(GLfloat) ); |
| 476 |
break; |
| 477 |
case GL_TEXTURE: |
| 478 |
if (ctx->TextureStackDepth==0) { |
| 479 |
gl_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix"); |
| 480 |
return; |
| 481 |
} |
| 482 |
ctx->TextureStackDepth--; |
| 483 |
MEMCPY( ctx->TextureMatrix, |
| 484 |
ctx->TextureStack[ctx->TextureStackDepth], |
| 485 |
16*sizeof(GLfloat) ); |
| 486 |
ctx->IdentityTexMat = is_identity( ctx->TextureMatrix ); |
| 487 |
break; |
| 488 |
default: |
| 489 |
abort(); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
void gl_LoadMatrixf( GLcontext *ctx, const GLfloat *m ) |
| 496 |
{ |
| 497 |
if (INSIDE_BEGIN_END(ctx)) { |
| 498 |
gl_error( ctx, GL_INVALID_OPERATION, "glLoadMatrix" ); |
| 499 |
return; |
| 500 |
} |
| 501 |
switch (ctx->Transform.MatrixMode) { |
| 502 |
case GL_MODELVIEW: |
| 503 |
MEMCPY( ctx->ModelViewMatrix, m, 16*sizeof(GLfloat) ); |
| 504 |
ctx->ModelViewInvValid = GL_FALSE; |
| 505 |
break; |
| 506 |
case GL_PROJECTION: |
| 507 |
MEMCPY( ctx->ProjectionMatrix, m, 16*sizeof(GLfloat) ); |
| 508 |
break; |
| 509 |
case GL_TEXTURE: |
| 510 |
MEMCPY( ctx->TextureMatrix, m, 16*sizeof(GLfloat) ); |
| 511 |
ctx->IdentityTexMat = is_identity( ctx->TextureMatrix ); |
| 512 |
break; |
| 513 |
default: |
| 514 |
abort(); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
void gl_MultMatrixf( GLcontext *ctx, const GLfloat *m ) |
| 521 |
{ |
| 522 |
if (INSIDE_BEGIN_END(ctx)) { |
| 523 |
gl_error( ctx, GL_INVALID_OPERATION, "glMultMatrix" ); |
| 524 |
return; |
| 525 |
} |
| 526 |
switch (ctx->Transform.MatrixMode) { |
| 527 |
case GL_MODELVIEW: |
| 528 |
matmul( ctx->ModelViewMatrix, ctx->ModelViewMatrix, m ); |
| 529 |
ctx->ModelViewInvValid = GL_FALSE; |
| 530 |
break; |
| 531 |
case GL_PROJECTION: |
| 532 |
matmul( ctx->ProjectionMatrix, ctx->ProjectionMatrix, m ); |
| 533 |
break; |
| 534 |
case GL_TEXTURE: |
| 535 |
matmul( ctx->TextureMatrix, ctx->TextureMatrix, m ); |
| 536 |
ctx->IdentityTexMat = is_identity( ctx->TextureMatrix ); |
| 537 |
break; |
| 538 |
default: |
| 539 |
abort(); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
/* |
| 546 |
* Generate a 4x4 transformation matrix from glRotate parameters. |
| 547 |
*/ |
| 548 |
void gl_rotation_matrix( GLfloat angle, GLfloat x, GLfloat y, GLfloat z, |
| 549 |
GLfloat m[] ) |
| 550 |
{ |
| 551 |
/* This function contributed by Erich Boleyn ([email protected]) */ |
| 552 |
GLfloat mag, s, c; |
| 553 |
GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c; |
| 554 |
|
| 555 |
s = sin( angle * (M_PI / 180.0) ); |
| 556 |
c = cos( angle * (M_PI / 180.0) ); |
| 557 |
|
| 558 |
mag = sqrt( x*x + y*y + z*z ); |
| 559 |
|
| 560 |
if (mag == 0.0) { |
| 561 |
/* generate an identity matrix and return */ |
| 562 |
MEMCPY(m, Identity, sizeof(GLfloat)*16); |
| 563 |
return; |
| 564 |
} |
| 565 |
|
| 566 |
x /= mag; |
| 567 |
y /= mag; |
| 568 |
z /= mag; |
| 569 |
|
| 570 |
#define M(row,col) m[col*4+row] |
| 571 |
|
| 572 |
/* |
| 573 |
* Arbitrary axis rotation matrix. |
| 574 |
* |
| 575 |
* This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied |
| 576 |
* like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation |
| 577 |
* (which is about the X-axis), and the two composite transforms |
| 578 |
* Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary |
| 579 |
* from the arbitrary axis to the X-axis then back. They are |
| 580 |
* all elementary rotations. |
| 581 |
* |
| 582 |
* Rz' is a rotation about the Z-axis, to bring the axis vector |
| 583 |
* into the x-z plane. Then Ry' is applied, rotating about the |
| 584 |
* Y-axis to bring the axis vector parallel with the X-axis. The |
| 585 |
* rotation about the X-axis is then performed. Ry and Rz are |
| 586 |
* simply the respective inverse transforms to bring the arbitrary |
| 587 |
* axis back to it's original orientation. The first transforms |
| 588 |
* Rz' and Ry' are considered inverses, since the data from the |
| 589 |
* arbitrary axis gives you info on how to get to it, not how |
| 590 |
* to get away from it, and an inverse must be applied. |
| 591 |
* |
| 592 |
* The basic calculation used is to recognize that the arbitrary |
| 593 |
* axis vector (x, y, z), since it is of unit length, actually |
| 594 |
* represents the sines and cosines of the angles to rotate the |
| 595 |
* X-axis to the same orientation, with theta being the angle about |
| 596 |
* Z and phi the angle about Y (in the order described above) |
| 597 |
* as follows: |
| 598 |
* |
| 599 |
* cos ( theta ) = x / sqrt ( 1 - z^2 ) |
| 600 |
* sin ( theta ) = y / sqrt ( 1 - z^2 ) |
| 601 |
* |
| 602 |
* cos ( phi ) = sqrt ( 1 - z^2 ) |
| 603 |
* sin ( phi ) = z |
| 604 |
* |
| 605 |
* Note that cos ( phi ) can further be inserted to the above |
| 606 |
* formulas: |
| 607 |
* |
| 608 |
* cos ( theta ) = x / cos ( phi ) |
| 609 |
* sin ( theta ) = y / sin ( phi ) |
| 610 |
* |
| 611 |
* ...etc. Because of those relations and the standard trigonometric |
| 612 |
* relations, it is pssible to reduce the transforms down to what |
| 613 |
* is used below. It may be that any primary axis chosen will give the |
| 614 |
* same results (modulo a sign convention) using thie method. |
| 615 |
* |
| 616 |
* Particularly nice is to notice that all divisions that might |
| 617 |
* have caused trouble when parallel to certain planes or |
| 618 |
* axis go away with care paid to reducing the expressions. |
| 619 |
* After checking, it does perform correctly under all cases, since |
| 620 |
* in all the cases of division where the denominator would have |
| 621 |
* been zero, the numerator would have been zero as well, giving |
| 622 |
* the expected result. |
| 623 |
*/ |
| 624 |
|
| 625 |
xx = x * x; |
| 626 |
yy = y * y; |
| 627 |
zz = z * z; |
| 628 |
xy = x * y; |
| 629 |
yz = y * z; |
| 630 |
zx = z * x; |
| 631 |
xs = x * s; |
| 632 |
ys = y * s; |
| 633 |
zs = z * s; |
| 634 |
one_c = 1.0F - c; |
| 635 |
|
| 636 |
M(0,0) = (one_c * xx) + c; |
| 637 |
M(0,1) = (one_c * xy) - zs; |
| 638 |
M(0,2) = (one_c * zx) + ys; |
| 639 |
M(0,3) = 0.0F; |
| 640 |
|
| 641 |
M(1,0) = (one_c * xy) + zs; |
| 642 |
M(1,1) = (one_c * yy) + c; |
| 643 |
M(1,2) = (one_c * yz) - xs; |
| 644 |
M(1,3) = 0.0F; |
| 645 |
|
| 646 |
M(2,0) = (one_c * zx) - ys; |
| 647 |
M(2,1) = (one_c * yz) + xs; |
| 648 |
M(2,2) = (one_c * zz) + c; |
| 649 |
M(2,3) = 0.0F; |
| 650 |
|
| 651 |
M(3,0) = 0.0F; |
| 652 |
M(3,1) = 0.0F; |
| 653 |
M(3,2) = 0.0F; |
| 654 |
M(3,3) = 1.0F; |
| 655 |
|
| 656 |
#undef M |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
void gl_Rotatef( GLcontext *ctx, |
| 662 |
GLfloat angle, GLfloat x, GLfloat y, GLfloat z ) |
| 663 |
{ |
| 664 |
GLfloat m[16]; |
| 665 |
gl_rotation_matrix( angle, x, y, z, m ); |
| 666 |
gl_MultMatrixf( ctx, m ); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
/* |
| 672 |
* Execute a glScale call |
| 673 |
*/ |
| 674 |
void gl_Scalef( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z ) |
| 675 |
{ |
| 676 |
GLfloat *m; |
| 677 |
|
| 678 |
if (INSIDE_BEGIN_END(ctx)) { |
| 679 |
gl_error( ctx, GL_INVALID_OPERATION, "glScale" ); |
| 680 |
return; |
| 681 |
} |
| 682 |
switch (ctx->Transform.MatrixMode) { |
| 683 |
case GL_MODELVIEW: |
| 684 |
m = ctx->ModelViewMatrix; |
| 685 |
ctx->ModelViewInvValid = GL_FALSE; |
| 686 |
break; |
| 687 |
case GL_PROJECTION: |
| 688 |
m = ctx->ProjectionMatrix; |
| 689 |
break; |
| 690 |
case GL_TEXTURE: |
| 691 |
m = ctx->TextureMatrix; |
| 692 |
break; |
| 693 |
default: |
| 694 |
abort(); |
| 695 |
} |
| 696 |
m[0] *= x; m[4] *= y; m[8] *= z; |
| 697 |
m[1] *= x; m[5] *= y; m[9] *= z; |
| 698 |
m[2] *= x; m[6] *= y; m[10] *= z; |
| 699 |
m[3] *= x; m[7] *= y; m[11] *= z; |
| 700 |
|
| 701 |
if (ctx->Transform.MatrixMode==GL_TEXTURE) { |
| 702 |
ctx->IdentityTexMat = is_identity( ctx->TextureMatrix ); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
/* |
| 709 |
* Execute a glTranslate call |
| 710 |
*/ |
| 711 |
void gl_Translatef( GLcontext *ctx, GLfloat x, GLfloat y, GLfloat z ) |
| 712 |
{ |
| 713 |
GLfloat *m; |
| 714 |
if (INSIDE_BEGIN_END(ctx)) { |
| 715 |
gl_error( ctx, GL_INVALID_OPERATION, "glTranslate" ); |
| 716 |
return; |
| 717 |
} |
| 718 |
switch (ctx->Transform.MatrixMode) { |
| 719 |
case GL_MODELVIEW: |
| 720 |
m = ctx->ModelViewMatrix; |
| 721 |
ctx->ModelViewInvValid = GL_FALSE; |
| 722 |
break; |
| 723 |
case GL_PROJECTION: |
| 724 |
m = ctx->ProjectionMatrix; |
| 725 |
break; |
| 726 |
case GL_TEXTURE: |
| 727 |
m = ctx->TextureMatrix; |
| 728 |
break; |
| 729 |
default: |
| 730 |
abort(); |
| 731 |
} |
| 732 |
|
| 733 |
m[12] = m[0] * x + m[4] * y + m[8] * z + m[12]; |
| 734 |
m[13] = m[1] * x + m[5] * y + m[9] * z + m[13]; |
| 735 |
m[14] = m[2] * x + m[6] * y + m[10] * z + m[14]; |
| 736 |
m[15] = m[3] * x + m[7] * y + m[11] * z + m[15]; |
| 737 |
|
| 738 |
if (ctx->Transform.MatrixMode==GL_TEXTURE) { |
| 739 |
ctx->IdentityTexMat = is_identity( ctx->TextureMatrix ); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
/* |
| 747 |
* Define a new viewport and reallocate auxillary buffers if the size of |
| 748 |
* the window (color buffer) has changed. |
| 749 |
*/ |
| 750 |
void gl_Viewport( GLcontext *ctx, |
| 751 |
GLint x, GLint y, GLsizei width, GLsizei height ) |
| 752 |
{ |
| 753 |
if (width<0 || height<0) { |
| 754 |
gl_error( ctx, GL_INVALID_VALUE, "glViewport" ); |
| 755 |
return; |
| 756 |
} |
| 757 |
if (INSIDE_BEGIN_END(ctx)) { |
| 758 |
gl_error( ctx, GL_INVALID_OPERATION, "glViewport" ); |
| 759 |
return; |
| 760 |
} |
| 761 |
|
| 762 |
/* clamp width, and height to implementation dependent range */ |
| 763 |
width = CLAMP( width, 1, MAX_WIDTH ); |
| 764 |
height = CLAMP( height, 1, MAX_HEIGHT ); |
| 765 |
|
| 766 |
/* Save viewport */ |
| 767 |
ctx->Viewport.X = x; |
| 768 |
ctx->Viewport.Width = width; |
| 769 |
ctx->Viewport.Y = y; |
| 770 |
ctx->Viewport.Height = height; |
| 771 |
|
| 772 |
/* compute scale and bias values */ |
| 773 |
ctx->Viewport.Sx = (GLfloat) width / 2.0F; |
| 774 |
ctx->Viewport.Tx = ctx->Viewport.Sx + x; |
| 775 |
ctx->Viewport.Sy = (GLfloat) height / 2.0F; |
| 776 |
ctx->Viewport.Ty = ctx->Viewport.Sy + y; |
| 777 |
|
| 778 |
ctx->NewState |= NEW_ALL; /* just to be safe */ |
| 779 |
|
| 780 |
/* Check if window/buffer has been resized and if so, reallocate the |
| 781 |
* ancillary buffers. |
| 782 |
*/ |
| 783 |
gl_ResizeBuffersMESA(ctx); |
| 784 |
} |