1 /* $Id: matrix.c,v 1.34 2001/03/19 22:45:52 brianp 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.CurrentTransformUnit]; \
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
, 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
);
118 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
120 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glOrtho" );
126 _mesa_error( ctx
, GL_INVALID_VALUE
, "glOrtho" );
130 _math_matrix_ortho( mat
, left
, right
, bottom
, top
, nearval
, farval
);
135 _mesa_MatrixMode( GLenum mode
)
137 GET_CURRENT_CONTEXT(ctx
);
138 ASSERT_OUTSIDE_BEGIN_END(ctx
);
145 if (ctx
->Transform
.MatrixMode
== mode
)
147 ctx
->Transform
.MatrixMode
= mode
;
148 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
);
151 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
158 _mesa_PushMatrix( void )
160 GET_CURRENT_CONTEXT(ctx
);
161 ASSERT_OUTSIDE_BEGIN_END(ctx
);
163 if (MESA_VERBOSE
&VERBOSE_API
)
164 fprintf(stderr
, "glPushMatrix %s\n",
165 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
167 switch (ctx
->Transform
.MatrixMode
) {
169 if (ctx
->ModelViewStackDepth
>= MAX_MODELVIEW_STACK_DEPTH
- 1) {
170 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
173 _math_matrix_copy( &ctx
->ModelViewStack
[ctx
->ModelViewStackDepth
++],
177 if (ctx
->ProjectionStackDepth
>= MAX_PROJECTION_STACK_DEPTH
- 1) {
178 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
181 _math_matrix_copy( &ctx
->ProjectionStack
[ctx
->ProjectionStackDepth
++],
182 &ctx
->ProjectionMatrix
);
186 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
187 if (ctx
->TextureStackDepth
[t
] >= MAX_TEXTURE_STACK_DEPTH
- 1) {
188 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
191 _math_matrix_copy( &ctx
->TextureStack
[t
][ctx
->TextureStackDepth
[t
]++],
192 &ctx
->TextureMatrix
[t
] );
196 if (ctx
->ColorStackDepth
>= MAX_COLOR_STACK_DEPTH
- 1) {
197 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix");
200 _math_matrix_copy( &ctx
->ColorStack
[ctx
->ColorStackDepth
++],
204 _mesa_problem(ctx
, "Bad matrix mode in _mesa_PushMatrix");
211 _mesa_PopMatrix( void )
213 GET_CURRENT_CONTEXT(ctx
);
214 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
216 if (MESA_VERBOSE
&VERBOSE_API
)
217 fprintf(stderr
, "glPopMatrix %s\n",
218 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
220 switch (ctx
->Transform
.MatrixMode
) {
222 if (ctx
->ModelViewStackDepth
==0) {
223 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
226 _math_matrix_copy( &ctx
->ModelView
,
227 &ctx
->ModelViewStack
[--ctx
->ModelViewStackDepth
] );
228 ctx
->NewState
|= _NEW_MODELVIEW
;
231 if (ctx
->ProjectionStackDepth
==0) {
232 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
236 _math_matrix_copy( &ctx
->ProjectionMatrix
,
237 &ctx
->ProjectionStack
[--ctx
->ProjectionStackDepth
] );
238 ctx
->NewState
|= _NEW_PROJECTION
;
242 GLuint t
= ctx
->Texture
.CurrentTransformUnit
;
243 if (ctx
->TextureStackDepth
[t
]==0) {
244 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
247 _math_matrix_copy(&ctx
->TextureMatrix
[t
],
248 &ctx
->TextureStack
[t
][--ctx
->TextureStackDepth
[t
]]);
249 ctx
->NewState
|= _NEW_TEXTURE_MATRIX
;
253 if (ctx
->ColorStackDepth
==0) {
254 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix");
257 _math_matrix_copy(&ctx
->ColorMatrix
,
258 &ctx
->ColorStack
[--ctx
->ColorStackDepth
]);
259 ctx
->NewState
|= _NEW_COLOR_MATRIX
;
262 _mesa_problem(ctx
, "Bad matrix mode in _mesa_PopMatrix");
269 _mesa_LoadIdentity( void )
271 GET_CURRENT_CONTEXT(ctx
);
273 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
274 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadIdentity");
275 _math_matrix_set_identity( mat
);
280 _mesa_LoadMatrixf( const GLfloat
*m
)
282 GET_CURRENT_CONTEXT(ctx
);
284 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
285 GET_ACTIVE_MATRIX(ctx
, mat
, ctx
->NewState
, "glLoadMatrix");
286 _math_matrix_loadf( mat
, m
);
291 _mesa_LoadMatrixd( const GLdouble
*m
)
295 for (i
= 0; i
< 16; i
++)
297 _mesa_LoadMatrixf(f
);
303 * Multiply the active matrix by an arbitary matrix.
306 _mesa_MultMatrixf( const GLfloat
*m
)
308 GET_CURRENT_CONTEXT(ctx
);
310 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
311 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glMultMatrix" );
312 _math_matrix_mul_floats( mat
, m
);
317 * Multiply the active matrix by an arbitary matrix.
320 _mesa_MultMatrixd( const GLdouble
*m
)
324 for (i
= 0; i
< 16; i
++)
326 _mesa_MultMatrixf( f
);
333 * Execute a glRotate call
336 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
338 GET_CURRENT_CONTEXT(ctx
);
339 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
342 GET_ACTIVE_MATRIX( ctx
, mat
, ctx
->NewState
, "glRotate" );
343 _math_matrix_rotate( mat
, angle
, x
, y
, z
);
348 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
350 _mesa_Rotatef(angle
, x
, y
, z
);
355 * Execute a glScale call
358 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
360 GET_CURRENT_CONTEXT(ctx
);
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
);
369 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
371 _mesa_Scalef(x
, y
, z
);
376 * Execute a glTranslate call
379 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
381 GET_CURRENT_CONTEXT(ctx
);
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
);
390 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
392 _mesa_Translatef(x
, y
, z
);
397 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
400 _math_transposef(tm
, m
);
401 _mesa_LoadMatrixf(tm
);
406 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
409 _math_transposefd(tm
, m
);
410 _mesa_LoadMatrixf(tm
);
415 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
418 _math_transposef(tm
, m
);
419 _mesa_MultMatrixf(tm
);
424 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
427 _math_transposefd(tm
, m
);
428 _mesa_MultMatrixf(tm
);
433 * Called via glViewport or display list execution.
436 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
438 GET_CURRENT_CONTEXT(ctx
);
439 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
440 _mesa_set_viewport(ctx
, x
, y
, width
, height
);
445 * Define a new viewport and reallocate auxillary buffers if the size of
446 * the window (color buffer) has changed.
449 _mesa_set_viewport( GLcontext
*ctx
, GLint x
, GLint y
,
450 GLsizei width
, GLsizei height
)
452 const GLfloat n
= ctx
->Viewport
.Near
;
453 const GLfloat f
= ctx
->Viewport
.Far
;
455 if (width
< 0 || height
< 0) {
456 _mesa_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
460 if (MESA_VERBOSE
& VERBOSE_API
)
461 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
463 /* clamp width, and height to implementation dependent range */
464 width
= CLAMP( width
, 1, MAX_WIDTH
);
465 height
= CLAMP( height
, 1, MAX_HEIGHT
);
469 ctx
->Viewport
.Width
= width
;
471 ctx
->Viewport
.Height
= height
;
473 /* compute scale and bias values :: This is really driver-specific
474 * and should be maintained elsewhere if at all.
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
;
486 /* Check if window/buffer has been resized and if so, reallocate the
489 _mesa_ResizeBuffersMESA();
491 if (ctx
->Driver
.Viewport
) {
492 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
499 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
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
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.
513 GET_CURRENT_CONTEXT(ctx
);
514 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
516 if (MESA_VERBOSE
&VERBOSE_API
)
517 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
519 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
520 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
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
;
528 if (ctx
->Driver
.DepthRange
) {
529 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);