1 /* $Id: matrix.c,v 1.40 2002/04/22 20:00:16 alanh 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"
56 _mesa_Frustum( GLdouble left
, GLdouble right
,
57 GLdouble bottom
, GLdouble top
,
58 GLdouble nearval
, GLdouble farval
)
60 GET_CURRENT_CONTEXT(ctx
);
61 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
69 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFrustum" );
73 _math_matrix_frustum( ctx
->CurrentStack
->Top
,
74 (GLfloat
) left
, (GLfloat
) right
,
75 (GLfloat
) bottom
, (GLfloat
) top
,
76 (GLfloat
) nearval
, (GLfloat
) farval
);
77 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
82 _mesa_Ortho( GLdouble left
, GLdouble right
,
83 GLdouble bottom
, GLdouble top
,
84 GLdouble nearval
, GLdouble farval
)
86 GET_CURRENT_CONTEXT(ctx
);
87 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
93 _mesa_error( ctx
, GL_INVALID_VALUE
, "glOrtho" );
97 _math_matrix_ortho( ctx
->CurrentStack
->Top
,
98 (GLfloat
) left
, (GLfloat
) right
,
99 (GLfloat
) bottom
, (GLfloat
) top
,
100 (GLfloat
) nearval
, (GLfloat
) farval
);
101 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
106 _mesa_MatrixMode( GLenum mode
)
108 GET_CURRENT_CONTEXT(ctx
);
109 ASSERT_OUTSIDE_BEGIN_END(ctx
);
111 if (ctx
->Transform
.MatrixMode
== mode
&& mode
!= GL_TEXTURE
)
113 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
);
117 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
120 ctx
->CurrentStack
= &ctx
->ProjectionMatrixStack
;
123 ctx
->CurrentStack
= &ctx
->TextureMatrixStack
[ctx
->Texture
.CurrentUnit
];
126 ctx
->CurrentStack
= &ctx
->ColorMatrixStack
;
136 if (!ctx
->Extensions
.NV_vertex_program
) {
137 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
140 ctx
->CurrentStack
= &ctx
->ProgramMatrixStack
[mode
- GL_MATRIX0_NV
];
143 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
147 ctx
->Transform
.MatrixMode
= mode
;
153 _mesa_PushMatrix( void )
155 GET_CURRENT_CONTEXT(ctx
);
156 struct matrix_stack
*stack
= ctx
->CurrentStack
;
157 ASSERT_OUTSIDE_BEGIN_END(ctx
);
159 if (MESA_VERBOSE
&VERBOSE_API
)
160 fprintf(stderr
, "glPushMatrix %s\n",
161 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
163 if (stack
->Depth
+ 1 >= stack
->MaxDepth
) {
164 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix" );
167 _math_matrix_copy( &stack
->Stack
[stack
->Depth
+ 1],
168 &stack
->Stack
[stack
->Depth
] );
170 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
171 ctx
->NewState
|= stack
->DirtyFlag
;
177 _mesa_PopMatrix( void )
179 GET_CURRENT_CONTEXT(ctx
);
180 struct matrix_stack
*stack
= ctx
->CurrentStack
;
181 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
183 if (MESA_VERBOSE
&VERBOSE_API
)
184 fprintf(stderr
, "glPopMatrix %s\n",
185 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
187 if (stack
->Depth
== 0) {
188 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix" );
192 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
193 ctx
->NewState
|= stack
->DirtyFlag
;
199 _mesa_LoadIdentity( void )
201 GET_CURRENT_CONTEXT(ctx
);
202 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
203 _math_matrix_set_identity( ctx
->CurrentStack
->Top
);
204 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
209 _mesa_LoadMatrixf( const GLfloat
*m
)
211 GET_CURRENT_CONTEXT(ctx
);
213 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
214 _math_matrix_loadf( ctx
->CurrentStack
->Top
, m
);
215 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
220 _mesa_LoadMatrixd( const GLdouble
*m
)
225 for (i
= 0; i
< 16; i
++)
226 f
[i
] = (GLfloat
) m
[i
];
227 _mesa_LoadMatrixf(f
);
233 * Multiply the active matrix by an arbitary matrix.
236 _mesa_MultMatrixf( const GLfloat
*m
)
238 GET_CURRENT_CONTEXT(ctx
);
240 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
241 _math_matrix_mul_floats( ctx
->CurrentStack
->Top
, m
);
242 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
247 * Multiply the active matrix by an arbitary matrix.
250 _mesa_MultMatrixd( const GLdouble
*m
)
255 for (i
= 0; i
< 16; i
++)
256 f
[i
] = (GLfloat
) m
[i
];
257 _mesa_MultMatrixf( f
);
264 * Execute a glRotate call
267 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
269 GET_CURRENT_CONTEXT(ctx
);
270 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
272 _math_matrix_rotate( ctx
->CurrentStack
->Top
, angle
, x
, y
, z
);
273 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
278 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
280 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
285 * Execute a glScale call
288 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
290 GET_CURRENT_CONTEXT(ctx
);
291 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
292 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
293 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
298 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
300 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
305 * Execute a glTranslate call
308 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
310 GET_CURRENT_CONTEXT(ctx
);
311 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
312 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
313 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
318 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
320 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
325 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
329 _math_transposef(tm
, m
);
330 _mesa_LoadMatrixf(tm
);
335 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
339 _math_transposefd(tm
, m
);
340 _mesa_LoadMatrixf(tm
);
345 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
349 _math_transposef(tm
, m
);
350 _mesa_MultMatrixf(tm
);
355 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
359 _math_transposefd(tm
, m
);
360 _mesa_MultMatrixf(tm
);
365 * Called via glViewport or display list execution.
368 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
370 GET_CURRENT_CONTEXT(ctx
);
371 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
372 _mesa_set_viewport(ctx
, x
, y
, width
, height
);
377 * Define a new viewport and reallocate auxillary buffers if the size of
378 * the window (color buffer) has changed.
381 _mesa_set_viewport( GLcontext
*ctx
, GLint x
, GLint y
,
382 GLsizei width
, GLsizei height
)
384 const GLfloat n
= ctx
->Viewport
.Near
;
385 const GLfloat f
= ctx
->Viewport
.Far
;
387 if (width
< 0 || height
< 0) {
388 _mesa_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
392 if (MESA_VERBOSE
& VERBOSE_API
)
393 fprintf(stderr
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
395 /* clamp width, and height to implementation dependent range */
396 width
= CLAMP( width
, 1, MAX_WIDTH
);
397 height
= CLAMP( height
, 1, MAX_HEIGHT
);
401 ctx
->Viewport
.Width
= width
;
403 ctx
->Viewport
.Height
= height
;
405 /* compute scale and bias values :: This is really driver-specific
406 * and should be maintained elsewhere if at all.
408 ctx
->Viewport
._WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
409 ctx
->Viewport
._WindowMap
.m
[MAT_TX
] = ctx
->Viewport
._WindowMap
.m
[MAT_SX
] + x
;
410 ctx
->Viewport
._WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
411 ctx
->Viewport
._WindowMap
.m
[MAT_TY
] = ctx
->Viewport
._WindowMap
.m
[MAT_SY
] + y
;
412 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
);
413 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
+ n
);
414 ctx
->Viewport
._WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
415 ctx
->Viewport
._WindowMap
.type
= MATRIX_3D_NO_ROT
;
416 ctx
->NewState
|= _NEW_VIEWPORT
;
418 /* Check if window/buffer has been resized and if so, reallocate the
421 _mesa_ResizeBuffersMESA();
423 if (ctx
->Driver
.Viewport
) {
424 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
431 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
434 * nearval - specifies mapping of the near clipping plane to window
435 * coordinates, default is 0
436 * farval - specifies mapping of the far clipping plane to window
437 * coordinates, default is 1
439 * After clipping and div by w, z coords are in -1.0 to 1.0,
440 * corresponding to near and far clipping planes. glDepthRange
441 * specifies a linear mapping of the normalized z coords in
442 * this range to window z coords.
445 GET_CURRENT_CONTEXT(ctx
);
446 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
448 if (MESA_VERBOSE
&VERBOSE_API
)
449 fprintf(stderr
, "glDepthRange %f %f\n", nearval
, farval
);
451 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
452 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
454 ctx
->Viewport
.Near
= n
;
455 ctx
->Viewport
.Far
= f
;
456 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
);
457 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
+ n
);
458 ctx
->NewState
|= _NEW_VIEWPORT
;
460 if (ctx
->Driver
.DepthRange
) {
461 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);