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