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