1 /* $Id: matrix.c,v 1.43 2002/06/23 02:52:18 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
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
);
89 if (MESA_VERBOSE
& VERBOSE_API
)
90 _mesa_debug(ctx
, "glFrustum(%f, %f, %f, %f, %f, %f)\n",
91 left
, right
, bottom
, top
, nearval
, farval
);
97 _mesa_error( ctx
, GL_INVALID_VALUE
, "glOrtho" );
101 _math_matrix_ortho( ctx
->CurrentStack
->Top
,
102 (GLfloat
) left
, (GLfloat
) right
,
103 (GLfloat
) bottom
, (GLfloat
) top
,
104 (GLfloat
) nearval
, (GLfloat
) farval
);
105 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
110 _mesa_MatrixMode( GLenum mode
)
112 GET_CURRENT_CONTEXT(ctx
);
113 ASSERT_OUTSIDE_BEGIN_END(ctx
);
115 if (ctx
->Transform
.MatrixMode
== mode
&& mode
!= GL_TEXTURE
)
117 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
);
121 ctx
->CurrentStack
= &ctx
->ModelviewMatrixStack
;
124 ctx
->CurrentStack
= &ctx
->ProjectionMatrixStack
;
127 ctx
->CurrentStack
= &ctx
->TextureMatrixStack
[ctx
->Texture
.CurrentUnit
];
130 ctx
->CurrentStack
= &ctx
->ColorMatrixStack
;
140 if (!ctx
->Extensions
.NV_vertex_program
) {
141 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
144 ctx
->CurrentStack
= &ctx
->ProgramMatrixStack
[mode
- GL_MATRIX0_NV
];
147 _mesa_error( ctx
, GL_INVALID_ENUM
, "glMatrixMode" );
151 ctx
->Transform
.MatrixMode
= mode
;
157 _mesa_PushMatrix( void )
159 GET_CURRENT_CONTEXT(ctx
);
160 struct matrix_stack
*stack
= ctx
->CurrentStack
;
161 ASSERT_OUTSIDE_BEGIN_END(ctx
);
163 if (MESA_VERBOSE
&VERBOSE_API
)
164 _mesa_debug(ctx
, "glPushMatrix %s\n",
165 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
167 if (stack
->Depth
+ 1 >= stack
->MaxDepth
) {
168 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushMatrix" );
171 _math_matrix_copy( &stack
->Stack
[stack
->Depth
+ 1],
172 &stack
->Stack
[stack
->Depth
] );
174 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
175 ctx
->NewState
|= stack
->DirtyFlag
;
181 _mesa_PopMatrix( void )
183 GET_CURRENT_CONTEXT(ctx
);
184 struct matrix_stack
*stack
= ctx
->CurrentStack
;
185 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
187 if (MESA_VERBOSE
&VERBOSE_API
)
188 _mesa_debug(ctx
, "glPopMatrix %s\n",
189 _mesa_lookup_enum_by_nr(ctx
->Transform
.MatrixMode
));
191 if (stack
->Depth
== 0) {
192 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopMatrix" );
196 stack
->Top
= &(stack
->Stack
[stack
->Depth
]);
197 ctx
->NewState
|= stack
->DirtyFlag
;
203 _mesa_LoadIdentity( void )
205 GET_CURRENT_CONTEXT(ctx
);
206 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
208 if (MESA_VERBOSE
& VERBOSE_API
)
209 _mesa_debug(ctx
, "glLoadIdentity()");
211 _math_matrix_set_identity( ctx
->CurrentStack
->Top
);
212 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
217 _mesa_LoadMatrixf( const GLfloat
*m
)
219 GET_CURRENT_CONTEXT(ctx
);
221 if (MESA_VERBOSE
& VERBOSE_API
)
223 "glLoadMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
224 m
[0], m
[4], m
[8], m
[12],
225 m
[1], m
[5], m
[9], m
[13],
226 m
[2], m
[6], m
[10], m
[14],
227 m
[3], m
[7], m
[11], m
[15]);
229 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
230 _math_matrix_loadf( ctx
->CurrentStack
->Top
, m
);
231 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
236 _mesa_LoadMatrixd( const GLdouble
*m
)
241 for (i
= 0; i
< 16; i
++)
242 f
[i
] = (GLfloat
) m
[i
];
243 _mesa_LoadMatrixf(f
);
249 * Multiply the active matrix by an arbitary matrix.
252 _mesa_MultMatrixf( const GLfloat
*m
)
254 GET_CURRENT_CONTEXT(ctx
);
256 if (MESA_VERBOSE
& VERBOSE_API
)
258 "glMultMatrix(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
259 m
[0], m
[4], m
[8], m
[12],
260 m
[1], m
[5], m
[9], m
[13],
261 m
[2], m
[6], m
[10], m
[14],
262 m
[3], m
[7], m
[11], m
[15]);
263 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
264 _math_matrix_mul_floats( ctx
->CurrentStack
->Top
, m
);
265 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
270 * Multiply the active matrix by an arbitary matrix.
273 _mesa_MultMatrixd( const GLdouble
*m
)
278 for (i
= 0; i
< 16; i
++)
279 f
[i
] = (GLfloat
) m
[i
];
280 _mesa_MultMatrixf( f
);
287 * Execute a glRotate call
290 _mesa_Rotatef( GLfloat angle
, GLfloat x
, GLfloat y
, GLfloat z
)
292 GET_CURRENT_CONTEXT(ctx
);
293 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
295 _math_matrix_rotate( ctx
->CurrentStack
->Top
, angle
, x
, y
, z
);
296 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
301 _mesa_Rotated( GLdouble angle
, GLdouble x
, GLdouble y
, GLdouble z
)
303 _mesa_Rotatef((GLfloat
) angle
, (GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
308 * Execute a glScale call
311 _mesa_Scalef( GLfloat x
, GLfloat y
, GLfloat z
)
313 GET_CURRENT_CONTEXT(ctx
);
314 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
315 _math_matrix_scale( ctx
->CurrentStack
->Top
, x
, y
, z
);
316 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
321 _mesa_Scaled( GLdouble x
, GLdouble y
, GLdouble z
)
323 _mesa_Scalef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
328 * Execute a glTranslate call
331 _mesa_Translatef( GLfloat x
, GLfloat y
, GLfloat z
)
333 GET_CURRENT_CONTEXT(ctx
);
334 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
335 _math_matrix_translate( ctx
->CurrentStack
->Top
, x
, y
, z
);
336 ctx
->NewState
|= ctx
->CurrentStack
->DirtyFlag
;
341 _mesa_Translated( GLdouble x
, GLdouble y
, GLdouble z
)
343 _mesa_Translatef((GLfloat
) x
, (GLfloat
) y
, (GLfloat
) z
);
348 _mesa_LoadTransposeMatrixfARB( const GLfloat
*m
)
352 _math_transposef(tm
, m
);
353 _mesa_LoadMatrixf(tm
);
358 _mesa_LoadTransposeMatrixdARB( const GLdouble
*m
)
362 _math_transposefd(tm
, m
);
363 _mesa_LoadMatrixf(tm
);
368 _mesa_MultTransposeMatrixfARB( const GLfloat
*m
)
372 _math_transposef(tm
, m
);
373 _mesa_MultMatrixf(tm
);
378 _mesa_MultTransposeMatrixdARB( const GLdouble
*m
)
382 _math_transposefd(tm
, m
);
383 _mesa_MultMatrixf(tm
);
388 * Called via glViewport or display list execution.
391 _mesa_Viewport( GLint x
, GLint y
, GLsizei width
, GLsizei height
)
393 GET_CURRENT_CONTEXT(ctx
);
394 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
395 _mesa_set_viewport(ctx
, x
, y
, width
, height
);
400 * Define a new viewport and reallocate auxillary buffers if the size of
401 * the window (color buffer) has changed.
404 _mesa_set_viewport( GLcontext
*ctx
, GLint x
, GLint y
,
405 GLsizei width
, GLsizei height
)
407 const GLfloat n
= ctx
->Viewport
.Near
;
408 const GLfloat f
= ctx
->Viewport
.Far
;
410 if (width
< 0 || height
< 0) {
411 _mesa_error( ctx
, GL_INVALID_VALUE
, "glViewport" );
415 if (MESA_VERBOSE
& VERBOSE_API
)
416 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
418 /* clamp width, and height to implementation dependent range */
419 width
= CLAMP( width
, 1, MAX_WIDTH
);
420 height
= CLAMP( height
, 1, MAX_HEIGHT
);
424 ctx
->Viewport
.Width
= width
;
426 ctx
->Viewport
.Height
= height
;
428 /* compute scale and bias values :: This is really driver-specific
429 * and should be maintained elsewhere if at all.
431 ctx
->Viewport
._WindowMap
.m
[MAT_SX
] = (GLfloat
) width
/ 2.0F
;
432 ctx
->Viewport
._WindowMap
.m
[MAT_TX
] = ctx
->Viewport
._WindowMap
.m
[MAT_SX
] + x
;
433 ctx
->Viewport
._WindowMap
.m
[MAT_SY
] = (GLfloat
) height
/ 2.0F
;
434 ctx
->Viewport
._WindowMap
.m
[MAT_TY
] = ctx
->Viewport
._WindowMap
.m
[MAT_SY
] + y
;
435 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
);
436 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
+ n
);
437 ctx
->Viewport
._WindowMap
.flags
= MAT_FLAG_GENERAL_SCALE
|MAT_FLAG_TRANSLATION
;
438 ctx
->Viewport
._WindowMap
.type
= MATRIX_3D_NO_ROT
;
439 ctx
->NewState
|= _NEW_VIEWPORT
;
441 /* Check if window/buffer has been resized and if so, reallocate the
444 _mesa_ResizeBuffersMESA();
446 if (ctx
->Driver
.Viewport
) {
447 (*ctx
->Driver
.Viewport
)( ctx
, x
, y
, width
, height
);
454 _mesa_DepthRange( GLclampd nearval
, GLclampd farval
)
457 * nearval - specifies mapping of the near clipping plane to window
458 * coordinates, default is 0
459 * farval - specifies mapping of the far clipping plane to window
460 * coordinates, default is 1
462 * After clipping and div by w, z coords are in -1.0 to 1.0,
463 * corresponding to near and far clipping planes. glDepthRange
464 * specifies a linear mapping of the normalized z coords in
465 * this range to window z coords.
468 GET_CURRENT_CONTEXT(ctx
);
469 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
471 if (MESA_VERBOSE
&VERBOSE_API
)
472 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
474 n
= (GLfloat
) CLAMP( nearval
, 0.0, 1.0 );
475 f
= (GLfloat
) CLAMP( farval
, 0.0, 1.0 );
477 ctx
->Viewport
.Near
= n
;
478 ctx
->Viewport
.Far
= f
;
479 ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
);
480 ctx
->Viewport
._WindowMap
.m
[MAT_TZ
] = ctx
->DepthMaxF
* ((f
- n
) / 2.0F
+ n
);
481 ctx
->NewState
|= _NEW_VIEWPORT
;
483 if (ctx
->Driver
.DepthRange
) {
484 (*ctx
->Driver
.DepthRange
)( ctx
, nearval
, farval
);