bug fixes, added missing state query cases
[mesa.git] / src / mesa / main / matrix.c
1 /* $Id: matrix.c,v 1.34 2001/03/19 22:45:52 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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 * Matrix operations
30 *
31 * NOTES:
32 * 1. 4x4 transformation matrices are stored in memory in column major order.
33 * 2. Points/vertices are to be thought of as column vectors.
34 * 3. Transformation of a point p by a matrix M is: p' = M * p
35 */
36
37
38 #ifdef PC_HEADER
39 #include "all.h"
40 #else
41 #include "glheader.h"
42 #include "buffers.h"
43 #include "context.h"
44 #include "enums.h"
45 #include "macros.h"
46 #include "matrix.h"
47 #include "mem.h"
48 #include "mmath.h"
49 #include "mtypes.h"
50
51 #include "math/m_matrix.h"
52 #endif
53
54
55 /**********************************************************************/
56 /* API functions */
57 /**********************************************************************/
58
59
60 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
61 do { \
62 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
63 switch (ctx->Transform.MatrixMode) { \
64 case GL_MODELVIEW: \
65 mat = &ctx->ModelView; \
66 flags |= _NEW_MODELVIEW; \
67 break; \
68 case GL_PROJECTION: \
69 mat = &ctx->ProjectionMatrix; \
70 flags |= _NEW_PROJECTION; \
71 break; \
72 case GL_TEXTURE: \
73 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
74 flags |= _NEW_TEXTURE_MATRIX; \
75 break; \
76 case GL_COLOR: \
77 mat = &ctx->ColorMatrix; \
78 flags |= _NEW_COLOR_MATRIX; \
79 break; \
80 default: \
81 _mesa_problem(ctx, where); \
82 } \
83 } while (0)
84
85
86 void
87 _mesa_Frustum( GLdouble left, GLdouble right,
88 GLdouble bottom, GLdouble top,
89 GLdouble nearval, GLdouble farval )
90 {
91 GET_CURRENT_CONTEXT(ctx);
92 GLmatrix *mat = 0;
93 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
94
95 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glFrustrum" );
96
97 if (nearval <= 0.0 ||
98 farval <= 0.0 ||
99 nearval == farval ||
100 left == right ||
101 top == bottom)
102 {
103 _mesa_error( ctx, GL_INVALID_VALUE, "glFrustum" );
104 return;
105 }
106
107 _math_matrix_frustum( mat, left, right, bottom, top, nearval, farval );
108 }
109
110
111 void
112 _mesa_Ortho( GLdouble left, GLdouble right,
113 GLdouble bottom, GLdouble top,
114 GLdouble nearval, GLdouble farval )
115 {
116 GET_CURRENT_CONTEXT(ctx);
117 GLmatrix *mat = 0;
118 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
119
120 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glOrtho" );
121
122 if (left == right ||
123 bottom == top ||
124 nearval == farval)
125 {
126 _mesa_error( ctx, GL_INVALID_VALUE, "glOrtho" );
127 return;
128 }
129
130 _math_matrix_ortho( mat, left, right, bottom, top, nearval, farval );
131 }
132
133
134 void
135 _mesa_MatrixMode( GLenum mode )
136 {
137 GET_CURRENT_CONTEXT(ctx);
138 ASSERT_OUTSIDE_BEGIN_END(ctx);
139
140 switch (mode) {
141 case GL_MODELVIEW:
142 case GL_PROJECTION:
143 case GL_TEXTURE:
144 case GL_COLOR:
145 if (ctx->Transform.MatrixMode == mode)
146 return;
147 ctx->Transform.MatrixMode = mode;
148 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
149 break;
150 default:
151 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode" );
152 }
153 }
154
155
156
157 void
158 _mesa_PushMatrix( void )
159 {
160 GET_CURRENT_CONTEXT(ctx);
161 ASSERT_OUTSIDE_BEGIN_END(ctx);
162
163 if (MESA_VERBOSE&VERBOSE_API)
164 fprintf(stderr, "glPushMatrix %s\n",
165 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
166
167 switch (ctx->Transform.MatrixMode) {
168 case GL_MODELVIEW:
169 if (ctx->ModelViewStackDepth >= MAX_MODELVIEW_STACK_DEPTH - 1) {
170 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
171 return;
172 }
173 _math_matrix_copy( &ctx->ModelViewStack[ctx->ModelViewStackDepth++],
174 &ctx->ModelView );
175 break;
176 case GL_PROJECTION:
177 if (ctx->ProjectionStackDepth >= MAX_PROJECTION_STACK_DEPTH - 1) {
178 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
179 return;
180 }
181 _math_matrix_copy( &ctx->ProjectionStack[ctx->ProjectionStackDepth++],
182 &ctx->ProjectionMatrix );
183 break;
184 case GL_TEXTURE:
185 {
186 GLuint t = ctx->Texture.CurrentTransformUnit;
187 if (ctx->TextureStackDepth[t] >= MAX_TEXTURE_STACK_DEPTH - 1) {
188 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
189 return;
190 }
191 _math_matrix_copy( &ctx->TextureStack[t][ctx->TextureStackDepth[t]++],
192 &ctx->TextureMatrix[t] );
193 }
194 break;
195 case GL_COLOR:
196 if (ctx->ColorStackDepth >= MAX_COLOR_STACK_DEPTH - 1) {
197 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix");
198 return;
199 }
200 _math_matrix_copy( &ctx->ColorStack[ctx->ColorStackDepth++],
201 &ctx->ColorMatrix );
202 break;
203 default:
204 _mesa_problem(ctx, "Bad matrix mode in _mesa_PushMatrix");
205 }
206 }
207
208
209
210 void
211 _mesa_PopMatrix( void )
212 {
213 GET_CURRENT_CONTEXT(ctx);
214 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
215
216 if (MESA_VERBOSE&VERBOSE_API)
217 fprintf(stderr, "glPopMatrix %s\n",
218 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
219
220 switch (ctx->Transform.MatrixMode) {
221 case GL_MODELVIEW:
222 if (ctx->ModelViewStackDepth==0) {
223 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
224 return;
225 }
226 _math_matrix_copy( &ctx->ModelView,
227 &ctx->ModelViewStack[--ctx->ModelViewStackDepth] );
228 ctx->NewState |= _NEW_MODELVIEW;
229 break;
230 case GL_PROJECTION:
231 if (ctx->ProjectionStackDepth==0) {
232 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
233 return;
234 }
235
236 _math_matrix_copy( &ctx->ProjectionMatrix,
237 &ctx->ProjectionStack[--ctx->ProjectionStackDepth] );
238 ctx->NewState |= _NEW_PROJECTION;
239 break;
240 case GL_TEXTURE:
241 {
242 GLuint t = ctx->Texture.CurrentTransformUnit;
243 if (ctx->TextureStackDepth[t]==0) {
244 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
245 return;
246 }
247 _math_matrix_copy(&ctx->TextureMatrix[t],
248 &ctx->TextureStack[t][--ctx->TextureStackDepth[t]]);
249 ctx->NewState |= _NEW_TEXTURE_MATRIX;
250 }
251 break;
252 case GL_COLOR:
253 if (ctx->ColorStackDepth==0) {
254 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix");
255 return;
256 }
257 _math_matrix_copy(&ctx->ColorMatrix,
258 &ctx->ColorStack[--ctx->ColorStackDepth]);
259 ctx->NewState |= _NEW_COLOR_MATRIX;
260 break;
261 default:
262 _mesa_problem(ctx, "Bad matrix mode in _mesa_PopMatrix");
263 }
264 }
265
266
267
268 void
269 _mesa_LoadIdentity( void )
270 {
271 GET_CURRENT_CONTEXT(ctx);
272 GLmatrix *mat = 0;
273 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
274 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadIdentity");
275 _math_matrix_set_identity( mat );
276 }
277
278
279 void
280 _mesa_LoadMatrixf( const GLfloat *m )
281 {
282 GET_CURRENT_CONTEXT(ctx);
283 GLmatrix *mat = 0;
284 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
285 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glLoadMatrix");
286 _math_matrix_loadf( mat, m );
287 }
288
289
290 void
291 _mesa_LoadMatrixd( const GLdouble *m )
292 {
293 GLint i;
294 GLfloat f[16];
295 for (i = 0; i < 16; i++)
296 f[i] = m[i];
297 _mesa_LoadMatrixf(f);
298 }
299
300
301
302 /*
303 * Multiply the active matrix by an arbitary matrix.
304 */
305 void
306 _mesa_MultMatrixf( const GLfloat *m )
307 {
308 GET_CURRENT_CONTEXT(ctx);
309 GLmatrix *mat = 0;
310 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
311 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glMultMatrix" );
312 _math_matrix_mul_floats( mat, m );
313 }
314
315
316 /*
317 * Multiply the active matrix by an arbitary matrix.
318 */
319 void
320 _mesa_MultMatrixd( const GLdouble *m )
321 {
322 GLint i;
323 GLfloat f[16];
324 for (i = 0; i < 16; i++)
325 f[i] = m[i];
326 _mesa_MultMatrixf( f );
327 }
328
329
330
331
332 /*
333 * Execute a glRotate call
334 */
335 void
336 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
337 {
338 GET_CURRENT_CONTEXT(ctx);
339 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
340 if (angle != 0.0F) {
341 GLmatrix *mat = 0;
342 GET_ACTIVE_MATRIX( ctx, mat, ctx->NewState, "glRotate" );
343 _math_matrix_rotate( mat, angle, x, y, z );
344 }
345 }
346
347 void
348 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
349 {
350 _mesa_Rotatef(angle, x, y, z);
351 }
352
353
354 /*
355 * Execute a glScale call
356 */
357 void
358 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
359 {
360 GET_CURRENT_CONTEXT(ctx);
361 GLmatrix *mat = 0;
362 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
363 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glScale");
364 _math_matrix_scale( mat, x, y, z );
365 }
366
367
368 void
369 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
370 {
371 _mesa_Scalef(x, y, z);
372 }
373
374
375 /*
376 * Execute a glTranslate call
377 */
378 void
379 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
380 {
381 GET_CURRENT_CONTEXT(ctx);
382 GLmatrix *mat = 0;
383 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
384 GET_ACTIVE_MATRIX(ctx, mat, ctx->NewState, "glTranslate");
385 _math_matrix_translate( mat, x, y, z );
386 }
387
388
389 void
390 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
391 {
392 _mesa_Translatef(x, y, z);
393 }
394
395
396 void
397 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
398 {
399 GLfloat tm[16];
400 _math_transposef(tm, m);
401 _mesa_LoadMatrixf(tm);
402 }
403
404
405 void
406 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
407 {
408 GLfloat tm[16];
409 _math_transposefd(tm, m);
410 _mesa_LoadMatrixf(tm);
411 }
412
413
414 void
415 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
416 {
417 GLfloat tm[16];
418 _math_transposef(tm, m);
419 _mesa_MultMatrixf(tm);
420 }
421
422
423 void
424 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
425 {
426 GLfloat tm[16];
427 _math_transposefd(tm, m);
428 _mesa_MultMatrixf(tm);
429 }
430
431
432 /*
433 * Called via glViewport or display list execution.
434 */
435 void
436 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
437 {
438 GET_CURRENT_CONTEXT(ctx);
439 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
440 _mesa_set_viewport(ctx, x, y, width, height);
441 }
442
443
444 /*
445 * Define a new viewport and reallocate auxillary buffers if the size of
446 * the window (color buffer) has changed.
447 */
448 void
449 _mesa_set_viewport( GLcontext *ctx, GLint x, GLint y,
450 GLsizei width, GLsizei height )
451 {
452 const GLfloat n = ctx->Viewport.Near;
453 const GLfloat f = ctx->Viewport.Far;
454
455 if (width < 0 || height < 0) {
456 _mesa_error( ctx, GL_INVALID_VALUE, "glViewport" );
457 return;
458 }
459
460 if (MESA_VERBOSE & VERBOSE_API)
461 fprintf(stderr, "glViewport %d %d %d %d\n", x, y, width, height);
462
463 /* clamp width, and height to implementation dependent range */
464 width = CLAMP( width, 1, MAX_WIDTH );
465 height = CLAMP( height, 1, MAX_HEIGHT );
466
467 /* Save viewport */
468 ctx->Viewport.X = x;
469 ctx->Viewport.Width = width;
470 ctx->Viewport.Y = y;
471 ctx->Viewport.Height = height;
472
473 /* compute scale and bias values :: This is really driver-specific
474 * and should be maintained elsewhere if at all.
475 */
476 ctx->Viewport._WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
477 ctx->Viewport._WindowMap.m[MAT_TX] = ctx->Viewport._WindowMap.m[MAT_SX] + x;
478 ctx->Viewport._WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
479 ctx->Viewport._WindowMap.m[MAT_TY] = ctx->Viewport._WindowMap.m[MAT_SY] + y;
480 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0);
481 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0 + n);
482 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
483 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
484 ctx->NewState |= _NEW_VIEWPORT;
485
486 /* Check if window/buffer has been resized and if so, reallocate the
487 * ancillary buffers.
488 */
489 _mesa_ResizeBuffersMESA();
490
491 if (ctx->Driver.Viewport) {
492 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
493 }
494 }
495
496
497
498 void
499 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
500 {
501 /*
502 * nearval - specifies mapping of the near clipping plane to window
503 * coordinates, default is 0
504 * farval - specifies mapping of the far clipping plane to window
505 * coordinates, default is 1
506 *
507 * After clipping and div by w, z coords are in -1.0 to 1.0,
508 * corresponding to near and far clipping planes. glDepthRange
509 * specifies a linear mapping of the normalized z coords in
510 * this range to window z coords.
511 */
512 GLfloat n, f;
513 GET_CURRENT_CONTEXT(ctx);
514 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
515
516 if (MESA_VERBOSE&VERBOSE_API)
517 fprintf(stderr, "glDepthRange %f %f\n", nearval, farval);
518
519 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
520 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
521
522 ctx->Viewport.Near = n;
523 ctx->Viewport.Far = f;
524 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0);
525 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0 + n);
526 ctx->NewState |= _NEW_VIEWPORT;
527
528 if (ctx->Driver.DepthRange) {
529 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
530 }
531 }