1 /* $Id: matrix.c,v 1.46 2003/03/01 01:50:21 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 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
46 #include "math/m_matrix.h"
51 _mesa_Frustum( GLdouble left
, GLdouble right
,
52 GLdouble bottom
, GLdouble top
,
53 GLdouble nearval
, GLdouble farval
)
55 GET_CURRENT_CONTEXT(ctx
);
56 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
64 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFrustum" );
68 _math_matrix_frustum( ctx
->CurrentStack
->Top
,
69 (GLfloat
) left
, (GLfloat
) right
,
70 (GLfloat
) bottom
, (GLfloat
) top
,
71 (GLfloat
) nearval
, (GLfloat
) farval
);
72 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
77 _mesa_Ortho( GLdouble left
, GLdouble right
,
78 GLdouble bottom
, GLdouble top
,
79 GLdouble nearval
, GLdouble farval
)
81 GET_CURRENT_CONTEXT(ctx
);
82 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
84 if (MESA_VERBOSE
& VERBOSE_API
)
85 _mesa_debug(ctx
, "glFrustum(%f, %f, %f, %f, %f, %f)\n",
86 left
, right
, bottom
, top
, nearval
, farval
);
92 _mesa_error( ctx
, GL_INVALID_VALUE
, "glOrtho" );
96 _math_matrix_ortho( ctx
->CurrentStack
->Top
,
97 (GLfloat
) left
, (GLfloat
) right
,
98 (GLfloat
) bottom
, (GLfloat
) top
,
99 (GLfloat
) nearval
, (GLfloat
) farval
);
100 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
105 _mesa_MatrixMode( GLenum mode
)
107 GET_CURRENT_CONTEXT(ctx
);
108 ASSERT_OUTSIDE_BEGIN_END(ctx
);
110 if (ctx
->Transform
.MatrixMode
== mode
&& mode
!= GL_TEXTURE
)
112 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
);
116 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
119 ctx
->CurrentStack
= &ctx
->ProjectionMatrixStack
;
122 ctx
->CurrentStack
= &ctx
->TextureMatrixStack
[ctx
->Texture
.CurrentUnit
];
125 ctx
->CurrentStack
= &ctx
->ColorMatrixStack
;
135 if (!ctx
->Extensions
.NV_vertex_program
) {
136 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
139 ctx
->CurrentStack
= &ctx
->ProgramMatrixStack
[mode
- GL_MATRIX0_NV
];
142 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
146 ctx
->Transform
.MatrixMode
= mode
;
152 _mesa_PushMatrix( void )
154 GET_CURRENT_CONTEXT(ctx
);
155 struct matrix_stack
*stack
= ctx
->CurrentStack
;
156 ASSERT_OUTSIDE_BEGIN_END(ctx
);
158 if (MESA_VERBOSE
&VERBOSE_API
)
159 _mesa_debug(ctx
, "glPushMatrix %s\n",
160 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
162 if (stack
->Depth
+ 1 >= stack
->MaxDepth
) {
163 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix" );
166 _math_matrix_copy( &stack
->Stack
[stack
->Depth
+ 1],
167 &stack
->Stack
[stack
->Depth
] );
169 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
170 ctx
->NewState
|= stack
->DirtyFlag
;
176 _mesa_PopMatrix( void )
178 GET_CURRENT_CONTEXT(ctx
);
179 struct matrix_stack
*stack
= ctx
->CurrentStack
;
180 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
182 if (MESA_VERBOSE
&VERBOSE_API
)
183 _mesa_debug(ctx
, "glPopMatrix %s\n",
184 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
186 if (stack
->Depth
== 0) {
187 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix" );
191 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
192 ctx
->NewState
|= stack
->DirtyFlag
;
198 _mesa_LoadIdentity( void )
200 GET_CURRENT_CONTEXT(ctx
);
201 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
203 if (MESA_VERBOSE
& VERBOSE_API
)
204 _mesa_debug(ctx
, "glLoadIdentity()");
206 _math_matrix_set_identity( ctx
->CurrentStack
->Top
);
207 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
212 _mesa_LoadMatrixf( const GLfloat
*m
)
214 GET_CURRENT_CONTEXT(ctx
);
216 if (MESA_VERBOSE
& VERBOSE_API
)
218 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
219 m
[0], m
[4], m
[8], m
[12],
220 m
[1], m
[5], m
[9], m
[13],
221 m
[2], m
[6], m
[10], m
[14],
222 m
[3], m
[7], m
[11], m
[15]);
224 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
225 _math_matrix_loadf( ctx
->CurrentStack
->Top
, m
);
226 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
231 _mesa_LoadMatrixd( const GLdouble
*m
)
236 for (i
= 0; i
< 16; i
++)
237 f
[i
] = (GLfloat
) m
[i
];
238 _mesa_LoadMatrixf(f
);
244 * Multiply the active matrix by an arbitary matrix.
247 _mesa_MultMatrixf( const GLfloat
*m
)
249 GET_CURRENT_CONTEXT(ctx
);
251 if (MESA_VERBOSE
& VERBOSE_API
)
253 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
254 m
[0], m
[4], m
[8], m
[12],
255 m
[1], m
[5], m
[9], m
[13],
256 m
[2], m
[6], m
[10], m
[14],
257 m
[3], m
[7], m
[11], m
[15]);
258 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
259 _math_matrix_mul_floats( ctx
->CurrentStack
->Top
, m
);
260 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
265 * Multiply the active matrix by an arbitary matrix.
268 _mesa_MultMatrixd( const GLdouble
*m
)
273 for (i
= 0; i
< 16; i
++)
274 f
[i
] = (GLfloat
) m
[i
];
275 _mesa_MultMatrixf( f
);
282 * Execute a glRotate call
285 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
287 GET_CURRENT_CONTEXT(ctx
);
288 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
290 _math_matrix_rotate( ctx
->CurrentStack
->Top
, angle
, x
, y
, z
);
291 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
296 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
298 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
303 * Execute a glScale call
306 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
308 GET_CURRENT_CONTEXT(ctx
);
309 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
310 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
311 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
316 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
318 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
323 * Execute a glTranslate call
326 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
328 GET_CURRENT_CONTEXT(ctx
);
329 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
330 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
331 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
336 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
338 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
343 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
347 _math_transposef(tm
, m
);
348 _mesa_LoadMatrixf(tm
);
353 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
357 _math_transposefd(tm
, m
);
358 _mesa_LoadMatrixf(tm
);
363 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
367 _math_transposef(tm
, m
);
368 _mesa_MultMatrixf(tm
);
373 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
377 _math_transposefd(tm
, m
);
378 _mesa_MultMatrixf(tm
);
383 * Called via glViewport or display list execution.
386 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
388 GET_CURRENT_CONTEXT(ctx
);
389 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
390 _mesa_set_viewport(ctx
, x
, y
, width
, height
);
395 * Define a new viewport and reallocate auxillary buffers if the size of
396 * the window (color buffer) has changed.
399 _mesa_set_viewport( GLcontext
*ctx
, GLint x
, GLint y
,
400 GLsizei width
, GLsizei height
)
402 const GLfloat n
= ctx
->Viewport
.Near
;
403 const GLfloat f
= ctx
->Viewport
.Far
;
405 if (MESA_VERBOSE
& VERBOSE_API
)
406 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
408 if (width
< 0 || height
< 0) {
409 _mesa_error( ctx
, GL_INVALID_VALUE
,
410 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
414 /* clamp width, and height to implementation dependent range */
415 width
= CLAMP( width
, 1, MAX_WIDTH
);
416 height
= CLAMP( height
, 1, MAX_HEIGHT
);
420 ctx
->Viewport
.Width
= width
;
422 ctx
->Viewport
.Height
= height
;
424 /* compute scale and bias values :: This is really driver-specific
425 * and should be maintained elsewhere if at all.
427 ctx
->Viewport
._WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
428 ctx
->Viewport
._WindowMap
.m
[MAT_TX
] = ctx
->Viewport
._WindowMap
.m
[MAT_SX
] + x
;
429 ctx
->Viewport
._WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
430 ctx
->Viewport
._WindowMap
.m
[MAT_TY
] = ctx
->Viewport
._WindowMap
.m
[MAT_SY
] + y
;
431 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
);
432 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
+ n
);
433 ctx
->Viewport
._WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
434 ctx
->Viewport
._WindowMap
.type
= MATRIX_3D_NO_ROT
;
435 ctx
->NewState
|= _NEW_VIEWPORT
;
437 /* Check if window/buffer has been resized and if so, reallocate the
440 _mesa_ResizeBuffersMESA();
442 if (ctx
->Driver
.Viewport
) {
443 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
450 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
453 * nearval - specifies mapping of the near clipping plane to window
454 * coordinates, default is 0
455 * farval - specifies mapping of the far clipping plane to window
456 * coordinates, default is 1
458 * After clipping and div by w, z coords are in -1.0 to 1.0,
459 * corresponding to near and far clipping planes. glDepthRange
460 * specifies a linear mapping of the normalized z coords in
461 * this range to window z coords.
464 GET_CURRENT_CONTEXT(ctx
);
465 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
467 if (MESA_VERBOSE
&VERBOSE_API
)
468 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
470 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
471 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
473 ctx
->Viewport
.Near
= n
;
474 ctx
->Viewport
.Far
= f
;
475 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
);
476 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
+ n
);
477 ctx
->NewState
|= _NEW_VIEWPORT
;
479 if (ctx
->Driver
.DepthRange
) {
480 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);