1 /* $Id: matrix.c,v 1.45 2002/10/24 23:57:21 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 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
47 #include "math/m_matrix.h"
52 _mesa_Frustum( GLdouble left
, GLdouble right
,
53 GLdouble bottom
, GLdouble top
,
54 GLdouble nearval
, GLdouble farval
)
56 GET_CURRENT_CONTEXT(ctx
);
57 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
65 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFrustum" );
69 _math_matrix_frustum( ctx
->CurrentStack
->Top
,
70 (GLfloat
) left
, (GLfloat
) right
,
71 (GLfloat
) bottom
, (GLfloat
) top
,
72 (GLfloat
) nearval
, (GLfloat
) farval
);
73 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
78 _mesa_Ortho( GLdouble left
, GLdouble right
,
79 GLdouble bottom
, GLdouble top
,
80 GLdouble nearval
, GLdouble farval
)
82 GET_CURRENT_CONTEXT(ctx
);
83 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
85 if (MESA_VERBOSE
& VERBOSE_API
)
86 _mesa_debug(ctx
, "glFrustum(%f, %f, %f, %f, %f, %f)\n",
87 left
, right
, bottom
, top
, nearval
, farval
);
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 _mesa_debug(ctx
, "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 _mesa_debug(ctx
, "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
);
204 if (MESA_VERBOSE
& VERBOSE_API
)
205 _mesa_debug(ctx
, "glLoadIdentity()");
207 _math_matrix_set_identity( ctx
->CurrentStack
->Top
);
208 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
213 _mesa_LoadMatrixf( const GLfloat
*m
)
215 GET_CURRENT_CONTEXT(ctx
);
217 if (MESA_VERBOSE
& VERBOSE_API
)
219 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
220 m
[0], m
[4], m
[8], m
[12],
221 m
[1], m
[5], m
[9], m
[13],
222 m
[2], m
[6], m
[10], m
[14],
223 m
[3], m
[7], m
[11], m
[15]);
225 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
226 _math_matrix_loadf( ctx
->CurrentStack
->Top
, m
);
227 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
232 _mesa_LoadMatrixd( const GLdouble
*m
)
237 for (i
= 0; i
< 16; i
++)
238 f
[i
] = (GLfloat
) m
[i
];
239 _mesa_LoadMatrixf(f
);
245 * Multiply the active matrix by an arbitary matrix.
248 _mesa_MultMatrixf( const GLfloat
*m
)
250 GET_CURRENT_CONTEXT(ctx
);
252 if (MESA_VERBOSE
& VERBOSE_API
)
254 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
255 m
[0], m
[4], m
[8], m
[12],
256 m
[1], m
[5], m
[9], m
[13],
257 m
[2], m
[6], m
[10], m
[14],
258 m
[3], m
[7], m
[11], m
[15]);
259 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
260 _math_matrix_mul_floats( ctx
->CurrentStack
->Top
, m
);
261 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
266 * Multiply the active matrix by an arbitary matrix.
269 _mesa_MultMatrixd( const GLdouble
*m
)
274 for (i
= 0; i
< 16; i
++)
275 f
[i
] = (GLfloat
) m
[i
];
276 _mesa_MultMatrixf( f
);
283 * Execute a glRotate call
286 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
288 GET_CURRENT_CONTEXT(ctx
);
289 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
291 _math_matrix_rotate( ctx
->CurrentStack
->Top
, angle
, x
, y
, z
);
292 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
297 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
299 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
304 * Execute a glScale call
307 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
309 GET_CURRENT_CONTEXT(ctx
);
310 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
311 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
312 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
317 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
319 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
324 * Execute a glTranslate call
327 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
329 GET_CURRENT_CONTEXT(ctx
);
330 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
331 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
332 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
337 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
339 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
344 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
348 _math_transposef(tm
, m
);
349 _mesa_LoadMatrixf(tm
);
354 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
358 _math_transposefd(tm
, m
);
359 _mesa_LoadMatrixf(tm
);
364 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
368 _math_transposef(tm
, m
);
369 _mesa_MultMatrixf(tm
);
374 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
378 _math_transposefd(tm
, m
);
379 _mesa_MultMatrixf(tm
);
384 * Called via glViewport or display list execution.
387 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
389 GET_CURRENT_CONTEXT(ctx
);
390 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
391 _mesa_set_viewport(ctx
, x
, y
, width
, height
);
396 * Define a new viewport and reallocate auxillary buffers if the size of
397 * the window (color buffer) has changed.
400 _mesa_set_viewport( GLcontext
*ctx
, GLint x
, GLint y
,
401 GLsizei width
, GLsizei height
)
403 const GLfloat n
= ctx
->Viewport
.Near
;
404 const GLfloat f
= ctx
->Viewport
.Far
;
406 if (MESA_VERBOSE
& VERBOSE_API
)
407 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
409 if (width
< 0 || height
< 0) {
410 _mesa_error( ctx
, GL_INVALID_VALUE
,
411 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
415 /* clamp width, and height to implementation dependent range */
416 width
= CLAMP( width
, 1, MAX_WIDTH
);
417 height
= CLAMP( height
, 1, MAX_HEIGHT
);
421 ctx
->Viewport
.Width
= width
;
423 ctx
->Viewport
.Height
= height
;
425 /* compute scale and bias values :: This is really driver-specific
426 * and should be maintained elsewhere if at all.
428 ctx
->Viewport
._WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
429 ctx
->Viewport
._WindowMap
.m
[MAT_TX
] = ctx
->Viewport
._WindowMap
.m
[MAT_SX
] + x
;
430 ctx
->Viewport
._WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
431 ctx
->Viewport
._WindowMap
.m
[MAT_TY
] = ctx
->Viewport
._WindowMap
.m
[MAT_SY
] + y
;
432 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
);
433 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
+ n
);
434 ctx
->Viewport
._WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
435 ctx
->Viewport
._WindowMap
.type
= MATRIX_3D_NO_ROT
;
436 ctx
->NewState
|= _NEW_VIEWPORT
;
438 /* Check if window/buffer has been resized and if so, reallocate the
441 _mesa_ResizeBuffersMESA();
443 if (ctx
->Driver
.Viewport
) {
444 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
451 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
454 * nearval - specifies mapping of the near clipping plane to window
455 * coordinates, default is 0
456 * farval - specifies mapping of the far clipping plane to window
457 * coordinates, default is 1
459 * After clipping and div by w, z coords are in -1.0 to 1.0,
460 * corresponding to near and far clipping planes. glDepthRange
461 * specifies a linear mapping of the normalized z coords in
462 * this range to window z coords.
465 GET_CURRENT_CONTEXT(ctx
);
466 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
468 if (MESA_VERBOSE
&VERBOSE_API
)
469 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
471 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
472 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
474 ctx
->Viewport
.Near
= n
;
475 ctx
->Viewport
.Far
= f
;
476 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
);
477 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
+ n
);
478 ctx
->NewState
|= _NEW_VIEWPORT
;
480 if (ctx
->Driver
.DepthRange
) {
481 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);