1 /* $Id: matrix.c,v 1.28 2000/11/24 10:25:05 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, where); \
63 if (MESA_VERBOSE&VERBOSE_API) fprintf(stderr, "%s\n", where); \
64 switch (ctx->Transform.MatrixMode) { \
66 mat = &ctx->ModelView; \
67 flags |= _NEW_MODELVIEW; \
70 mat = &ctx->ProjectionMatrix; \
71 flags |= _NEW_PROJECTION; \
74 mat = &ctx->TextureMatrix[ctx->Texture.CurrentTransformUnit]; \
75 flags |= _NEW_TEXTURE_MATRIX; \
78 mat = &ctx->ColorMatrix; \
79 flags |= _NEW_COLOR_MATRIX; \
82 gl_problem(ctx, where); \
88 _mesa_Frustum( GLdouble left
, GLdouble right
,
89 GLdouble bottom
, GLdouble top
,
90 GLdouble nearval
, GLdouble farval
)
92 GET_CURRENT_CONTEXT(ctx
);
95 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glFrustrum" );
103 gl_error( ctx
, GL_INVALID_VALUE
, "glFrustum" );
107 _math_matrix_frustrum( mat
, left
, right
, bottom
, top
, nearval
, farval
);
112 _mesa_Ortho( GLdouble left
, GLdouble right
,
113 GLdouble bottom
, GLdouble top
,
114 GLdouble nearval
, GLdouble farval
)
116 GET_CURRENT_CONTEXT(ctx
);
119 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
125 gl_error( ctx
, GL_INVALID_VALUE
, "gl_Ortho" );
129 _math_matrix_ortho( mat
, left
, right
, bottom
, top
, nearval
, farval
);
134 _mesa_MatrixMode( GLenum mode
)
136 GET_CURRENT_CONTEXT(ctx
);
137 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glMatrixMode");
143 ctx
->Transform
.MatrixMode
= mode
;
146 gl_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
153 _mesa_PushMatrix( void )
155 GET_CURRENT_CONTEXT(ctx
);
156 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushMatrix");
158 if (MESA_VERBOSE
&VERBOSE_API
)
159 fprintf(stderr
, "glPushMatrix %s\n",
160 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
162 switch (ctx
->Transform
.MatrixMode
) {
164 if (ctx
->ModelViewStackDepth
>= MAX_MODELVIEW_STACK_DEPTH
- 1) {
165 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
168 _math_matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
172 if (ctx
->ProjectionStackDepth
>= MAX_PROJECTION_STACK_DEPTH
- 1) {
173 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
176 _math_matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
177 &ctx
->ProjectionMatrix
);
181 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
182 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
- 1) {
183 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
186 _math_matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
187 &ctx
->TextureMatrix
[t
] );
191 if (ctx
->ColorStackDepth
>= MAX_COLOR_STACK_DEPTH
- 1) {
192 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
195 _math_matrix_copy( &ctx
->ColorStack
[ctx
->ColorStackDepth
++],
199 gl_problem(ctx
, "Bad matrix mode in gl_PushMatrix");
206 _mesa_PopMatrix( void )
208 GET_CURRENT_CONTEXT(ctx
);
209 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopMatrix");
211 if (MESA_VERBOSE
&VERBOSE_API
)
212 fprintf(stderr
, "glPopMatrix %s\n",
213 gl_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
215 switch (ctx
->Transform
.MatrixMode
) {
217 if (ctx
->ModelViewStackDepth
==0) {
218 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
221 _math_matrix_copy( &ctx
->ModelView
,
222 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
223 ctx
->NewState
|= _NEW_MODELVIEW
;
226 if (ctx
->ProjectionStackDepth
==0) {
227 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
231 _math_matrix_copy( &ctx
->ProjectionMatrix
,
232 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
233 ctx
->NewState
|= _NEW_PROJECTION
;
237 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
238 if (ctx
->TextureStackDepth
[t
]==0) {
239 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
242 _math_matrix_copy(&ctx
->TextureMatrix
[t
],
243 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
244 ctx
->NewState
|= _NEW_TEXTURE_MATRIX
;
248 if (ctx
->ColorStackDepth
==0) {
249 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
252 _math_matrix_copy(&ctx
->ColorMatrix
,
253 &ctx
->ColorStack
[--ctx
->ColorStackDepth
]);
254 ctx
->NewState
|= _NEW_COLOR_MATRIX
;
257 gl_problem(ctx
, "Bad matrix mode in gl_PopMatrix");
264 _mesa_LoadIdentity( void )
266 GET_CURRENT_CONTEXT(ctx
);
268 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
269 _math_matrix_set_identity( mat
);
274 _mesa_LoadMatrixf( const GLfloat
*m
)
276 GET_CURRENT_CONTEXT(ctx
);
278 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
279 _math_matrix_loadf( mat
, m
);
284 _mesa_LoadMatrixd( const GLdouble
*m
)
288 for (i
= 0; i
< 16; i
++)
290 _mesa_LoadMatrixf(f
);
296 * Multiply the active matrix by an arbitary matrix.
299 _mesa_MultMatrixf( const GLfloat
*m
)
301 GET_CURRENT_CONTEXT(ctx
);
303 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
304 _math_matrix_mul_floats( mat
, m
);
309 * Multiply the active matrix by an arbitary matrix.
312 _mesa_MultMatrixd( const GLdouble
*m
)
316 for (i
= 0; i
< 16; i
++)
318 _mesa_MultMatrixf( f
);
325 * Execute a glRotate call
328 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
330 GET_CURRENT_CONTEXT(ctx
);
333 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
334 _math_matrix_rotate( mat
, angle
, x
, y
, z
);
339 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
341 _mesa_Rotatef(angle
, x
, y
, z
);
346 * Execute a glScale call
349 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
351 GET_CURRENT_CONTEXT(ctx
);
353 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glScale");
354 _math_matrix_scale( mat
, x
, y
, z
);
359 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
361 _mesa_Scalef(x
, y
, z
);
366 * Execute a glTranslate call
369 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
371 GET_CURRENT_CONTEXT(ctx
);
373 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glTranslate");
374 _math_matrix_translate( mat
, x
, y
, z
);
379 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
381 _mesa_Translatef(x
, y
, z
);
386 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
389 _math_transposef(tm
, m
);
390 _mesa_LoadMatrixf(tm
);
395 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
398 _math_transposefd(tm
, m
);
399 _mesa_LoadMatrixf(tm
);
404 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
407 _math_transposef(tm
, m
);
408 _mesa_MultMatrixf(tm
);
413 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
416 _math_transposefd(tm
, m
);
417 _mesa_MultMatrixf(tm
);
422 * Called via glViewport or display list execution.
425 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
427 GET_CURRENT_CONTEXT(ctx
);
428 gl_Viewport(ctx
, x
, y
, width
, height
);
434 * Define a new viewport and reallocate auxillary buffers if the size of
435 * the window (color buffer) has changed.
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.
442 gl_Viewport( GLcontext
*ctx
, GLint x
, GLint y
, GLsizei width
, GLsizei height
)
444 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glViewport");
446 if (width
<0 || height
<0) {
447 gl_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
451 if (MESA_VERBOSE
& VERBOSE_API
)
452 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
454 /* clamp width, and height to implementation dependent range */
455 width
= CLAMP( width
, 1, MAX_WIDTH
);
456 height
= CLAMP( height
, 1, MAX_HEIGHT
);
460 ctx
->Viewport
.Width
= width
;
462 ctx
->Viewport
.Height
= height
;
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
;
475 /* Check if window/buffer has been resized and if so, reallocate the
478 _mesa_ResizeBuffersMESA();
480 if (ctx
->Driver
.Viewport
) {
481 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
488 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
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
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.
502 GET_CURRENT_CONTEXT(ctx
);
503 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthRange");
505 if (MESA_VERBOSE
&VERBOSE_API
)
506 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
508 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
509 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
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
;
517 if (ctx
->Driver
.DepthRange
) {
518 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);