1 /* $Id: matrix.c,v 1.36 2001/09/18 16:16:21 kschultz Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
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
51 #include "math/m_matrix.h"
55 /**********************************************************************/
57 /**********************************************************************/
60 #define GET_ACTIVE_MATRIX(ctx, mat, flags, where) \
62 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
63 switch (ctx->Transform.MatrixMode) { \
65 mat = &ctx->ModelView; \
66 flags |= _NEW_MODELVIEW; \
69 mat = &ctx->ProjectionMatrix; \
70 flags |= _NEW_PROJECTION; \
73 mat = &ctx->TextureMatrix[ctx->Texture.CurrentUnit]; \
74 flags |= _NEW_TEXTURE_MATRIX; \
77 mat = &ctx->ColorMatrix; \
78 flags |= _NEW_COLOR_MATRIX; \
81 _mesa_problem(ctx, where); \
87 _mesa_Frustum( GLdouble left
, GLdouble right
,
88 GLdouble bottom
, GLdouble top
,
89 GLdouble nearval
, GLdouble farval
)
91 GET_CURRENT_CONTEXT(ctx
);
93 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
95 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glFrustrum" );
103 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFrustum" );
107 _math_matrix_frustum( mat
, (GLfloat
) left
, (GLfloat
) right
,
108 (GLfloat
) bottom
, (GLfloat
) top
,
109 (GLfloat
) nearval
, (GLfloat
) farval
);
114 _mesa_Ortho( GLdouble left
, GLdouble right
,
115 GLdouble bottom
, GLdouble top
,
116 GLdouble nearval
, GLdouble farval
)
118 GET_CURRENT_CONTEXT(ctx
);
120 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
122 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
128 _mesa_error( ctx
, GL_INVALID_VALUE
, "glOrtho" );
132 _math_matrix_ortho( mat
, (GLfloat
) left
, (GLfloat
) right
,
133 (GLfloat
) bottom
, (GLfloat
) top
,
134 (GLfloat
) nearval
, (GLfloat
) farval
);
139 _mesa_MatrixMode( GLenum mode
)
141 GET_CURRENT_CONTEXT(ctx
);
142 ASSERT_OUTSIDE_BEGIN_END(ctx
);
149 if (ctx
->Transform
.MatrixMode
== mode
)
151 ctx
->Transform
.MatrixMode
= mode
;
152 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
);
155 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
162 _mesa_PushMatrix( void )
164 GET_CURRENT_CONTEXT(ctx
);
165 ASSERT_OUTSIDE_BEGIN_END(ctx
);
167 if (MESA_VERBOSE
&VERBOSE_API
)
168 fprintf(stderr
, "glPushMatrix %s\n",
169 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
171 switch (ctx
->Transform
.MatrixMode
) {
173 if (ctx
->ModelViewStackDepth
>= MAX_MODELVIEW_STACK_DEPTH
- 1) {
174 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
177 _math_matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
181 if (ctx
->ProjectionStackDepth
>= MAX_PROJECTION_STACK_DEPTH
- 1) {
182 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
185 _math_matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
186 &ctx
->ProjectionMatrix
);
190 GLuint t
= ctx
->Texture
.CurrentUnit
;
191 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
- 1) {
192 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
195 _math_matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
196 &ctx
->TextureMatrix
[t
] );
200 if (ctx
->ColorStackDepth
>= MAX_COLOR_STACK_DEPTH
- 1) {
201 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
204 _math_matrix_copy( &ctx
->ColorStack
[ctx
->ColorStackDepth
++],
208 _mesa_problem(ctx
, "Bad matrix mode in _mesa_PushMatrix");
215 _mesa_PopMatrix( void )
217 GET_CURRENT_CONTEXT(ctx
);
218 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
220 if (MESA_VERBOSE
&VERBOSE_API
)
221 fprintf(stderr
, "glPopMatrix %s\n",
222 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
224 switch (ctx
->Transform
.MatrixMode
) {
226 if (ctx
->ModelViewStackDepth
==0) {
227 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
230 _math_matrix_copy( &ctx
->ModelView
,
231 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
232 ctx
->NewState
|= _NEW_MODELVIEW
;
235 if (ctx
->ProjectionStackDepth
==0) {
236 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
240 _math_matrix_copy( &ctx
->ProjectionMatrix
,
241 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
242 ctx
->NewState
|= _NEW_PROJECTION
;
246 GLuint t
= ctx
->Texture
.CurrentUnit
;
247 if (ctx
->TextureStackDepth
[t
]==0) {
248 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
251 _math_matrix_copy(&ctx
->TextureMatrix
[t
],
252 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
253 ctx
->NewState
|= _NEW_TEXTURE_MATRIX
;
257 if (ctx
->ColorStackDepth
==0) {
258 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
261 _math_matrix_copy(&ctx
->ColorMatrix
,
262 &ctx
->ColorStack
[--ctx
->ColorStackDepth
]);
263 ctx
->NewState
|= _NEW_COLOR_MATRIX
;
266 _mesa_problem(ctx
, "Bad matrix mode in _mesa_PopMatrix");
273 _mesa_LoadIdentity( void )
275 GET_CURRENT_CONTEXT(ctx
);
277 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
278 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
279 _math_matrix_set_identity( mat
);
284 _mesa_LoadMatrixf( const GLfloat
*m
)
286 GET_CURRENT_CONTEXT(ctx
);
288 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
289 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
290 _math_matrix_loadf( mat
, m
);
295 _mesa_LoadMatrixd( const GLdouble
*m
)
299 for (i
= 0; i
< 16; i
++)
300 f
[i
] = (GLfloat
) m
[i
];
301 _mesa_LoadMatrixf(f
);
307 * Multiply the active matrix by an arbitary matrix.
310 _mesa_MultMatrixf( const GLfloat
*m
)
312 GET_CURRENT_CONTEXT(ctx
);
314 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
315 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
316 _math_matrix_mul_floats( mat
, m
);
321 * Multiply the active matrix by an arbitary matrix.
324 _mesa_MultMatrixd( const GLdouble
*m
)
328 for (i
= 0; i
< 16; i
++)
329 f
[i
] = (GLfloat
) m
[i
];
330 _mesa_MultMatrixf( f
);
337 * Execute a glRotate call
340 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
342 GET_CURRENT_CONTEXT(ctx
);
343 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
346 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
347 _math_matrix_rotate( mat
, angle
, x
, y
, z
);
352 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
354 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
359 * Execute a glScale call
362 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
364 GET_CURRENT_CONTEXT(ctx
);
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
);
373 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
375 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
380 * Execute a glTranslate call
383 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
385 GET_CURRENT_CONTEXT(ctx
);
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
);
394 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
396 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
401 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
404 _math_transposef(tm
, m
);
405 _mesa_LoadMatrixf(tm
);
410 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
413 _math_transposefd(tm
, m
);
414 _mesa_LoadMatrixf(tm
);
419 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
422 _math_transposef(tm
, m
);
423 _mesa_MultMatrixf(tm
);
428 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
431 _math_transposefd(tm
, m
);
432 _mesa_MultMatrixf(tm
);
437 * Called via glViewport or display list execution.
440 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
442 GET_CURRENT_CONTEXT(ctx
);
443 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
444 _mesa_set_viewport(ctx
, x
, y
, width
, height
);
449 * Define a new viewport and reallocate auxillary buffers if the size of
450 * the window (color buffer) has changed.
453 _mesa_set_viewport( GLcontext
*ctx
, GLint x
, GLint y
,
454 GLsizei width
, GLsizei height
)
456 const GLfloat n
= ctx
->Viewport
.Near
;
457 const GLfloat f
= ctx
->Viewport
.Far
;
459 if (width
< 0 || height
< 0) {
460 _mesa_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
464 if (MESA_VERBOSE
& VERBOSE_API
)
465 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
467 /* clamp width, and height to implementation dependent range */
468 width
= CLAMP( width
, 1, MAX_WIDTH
);
469 height
= CLAMP( height
, 1, MAX_HEIGHT
);
473 ctx
->Viewport
.Width
= width
;
475 ctx
->Viewport
.Height
= height
;
477 /* compute scale and bias values :: This is really driver-specific
478 * and should be maintained elsewhere if at all.
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
;
490 /* Check if window/buffer has been resized and if so, reallocate the
493 _mesa_ResizeBuffersMESA();
495 if (ctx
->Driver
.Viewport
) {
496 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
503 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
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
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.
517 GET_CURRENT_CONTEXT(ctx
);
518 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
520 if (MESA_VERBOSE
&VERBOSE_API
)
521 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
523 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
524 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
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
;
532 if (ctx
->Driver
.DepthRange
) {
533 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);