Test for NULL pointer for LoadMatrix(), MultMatrix() and
[mesa.git] / src / mesa / main / matrix.c
1 /* $Id: matrix.c,v 1.40 2002/04/22 20:00:16 alanh Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28 /*
29 * Matrix operations
30 *
31 * NOTES:
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
35 */
36
37
38 #ifdef PC_HEADER
39 #include "all.h"
40 #else
41 #include "glheader.h"
42 #include "buffers.h"
43 #include "context.h"
44 #include "enums.h"
45 #include "macros.h"
46 #include "matrix.h"
47 #include "mem.h"
48 #include "mmath.h"
49 #include "mtypes.h"
50
51 #include "math/m_matrix.h"
52 #endif
53
54
55 void
56 _mesa_Frustum( GLdouble left, GLdouble right,
57 GLdouble bottom, GLdouble top,
58 GLdouble nearval, GLdouble farval )
59 {
60 GET_CURRENT_CONTEXT(ctx);
61 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
62
63 if (nearval <= 0.0 ||
64 farval <= 0.0 ||
65 nearval == farval ||
66 left == right ||
67 top == bottom)
68 {
69 _mesa_error( ctx, GL_INVALID_VALUE, "glFrustum" );
70 return;
71 }
72
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;
78 }
79
80
81 void
82 _mesa_Ortho( GLdouble left, GLdouble right,
83 GLdouble bottom, GLdouble top,
84 GLdouble nearval, GLdouble farval )
85 {
86 GET_CURRENT_CONTEXT(ctx);
87 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
88
89 if (left == right ||
90 bottom == top ||
91 nearval == farval)
92 {
93 _mesa_error( ctx, GL_INVALID_VALUE, "glOrtho" );
94 return;
95 }
96
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;
102 }
103
104
105 void
106 _mesa_MatrixMode( GLenum mode )
107 {
108 GET_CURRENT_CONTEXT(ctx);
109 ASSERT_OUTSIDE_BEGIN_END(ctx);
110
111 if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
112 return;
113 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
114
115 switch (mode) {
116 case GL_MODELVIEW:
117 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
118 break;
119 case GL_PROJECTION:
120 ctx->CurrentStack = &ctx->ProjectionMatrixStack;
121 break;
122 case GL_TEXTURE:
123 ctx->CurrentStack = &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
124 break;
125 case GL_COLOR:
126 ctx->CurrentStack = &ctx->ColorMatrixStack;
127 break;
128 case GL_MATRIX0_NV:
129 case GL_MATRIX1_NV:
130 case GL_MATRIX2_NV:
131 case GL_MATRIX3_NV:
132 case GL_MATRIX4_NV:
133 case GL_MATRIX5_NV:
134 case GL_MATRIX6_NV:
135 case GL_MATRIX7_NV:
136 if (!ctx->Extensions.NV_vertex_program) {
137 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode" );
138 return;
139 }
140 ctx->CurrentStack = &ctx->ProgramMatrixStack[mode - GL_MATRIX0_NV];
141 break;
142 default:
143 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode" );
144 return;
145 }
146
147 ctx->Transform.MatrixMode = mode;
148 }
149
150
151
152 void
153 _mesa_PushMatrix( void )
154 {
155 GET_CURRENT_CONTEXT(ctx);
156 struct matrix_stack *stack = ctx->CurrentStack;
157 ASSERT_OUTSIDE_BEGIN_END(ctx);
158
159 if (MESA_VERBOSE&VERBOSE_API)
160 fprintf(stderr, "glPushMatrix %s\n",
161 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
162
163 if (stack->Depth + 1 >= stack->MaxDepth) {
164 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix" );
165 return;
166 }
167 _math_matrix_copy( &stack->Stack[stack->Depth + 1],
168 &stack->Stack[stack->Depth] );
169 stack->Depth++;
170 stack->Top = &(stack->Stack[stack->Depth]);
171 ctx->NewState |= stack->DirtyFlag;
172 }
173
174
175
176 void
177 _mesa_PopMatrix( void )
178 {
179 GET_CURRENT_CONTEXT(ctx);
180 struct matrix_stack *stack = ctx->CurrentStack;
181 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
182
183 if (MESA_VERBOSE&VERBOSE_API)
184 fprintf(stderr, "glPopMatrix %s\n",
185 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
186
187 if (stack->Depth == 0) {
188 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix" );
189 return;
190 }
191 stack->Depth--;
192 stack->Top = &(stack->Stack[stack->Depth]);
193 ctx->NewState |= stack->DirtyFlag;
194 }
195
196
197
198 void
199 _mesa_LoadIdentity( void )
200 {
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;
205 }
206
207
208 void
209 _mesa_LoadMatrixf( const GLfloat *m )
210 {
211 GET_CURRENT_CONTEXT(ctx);
212 if (!m) return;
213 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
214 _math_matrix_loadf( ctx->CurrentStack->Top, m );
215 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
216 }
217
218
219 void
220 _mesa_LoadMatrixd( const GLdouble *m )
221 {
222 GLint i;
223 GLfloat f[16];
224 if (!m) return;
225 for (i = 0; i < 16; i++)
226 f[i] = (GLfloat) m[i];
227 _mesa_LoadMatrixf(f);
228 }
229
230
231
232 /*
233 * Multiply the active matrix by an arbitary matrix.
234 */
235 void
236 _mesa_MultMatrixf( const GLfloat *m )
237 {
238 GET_CURRENT_CONTEXT(ctx);
239 if (!m) return;
240 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
241 _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
242 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
243 }
244
245
246 /*
247 * Multiply the active matrix by an arbitary matrix.
248 */
249 void
250 _mesa_MultMatrixd( const GLdouble *m )
251 {
252 GLint i;
253 GLfloat f[16];
254 if (!m) return;
255 for (i = 0; i < 16; i++)
256 f[i] = (GLfloat) m[i];
257 _mesa_MultMatrixf( f );
258 }
259
260
261
262
263 /*
264 * Execute a glRotate call
265 */
266 void
267 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
268 {
269 GET_CURRENT_CONTEXT(ctx);
270 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
271 if (angle != 0.0F) {
272 _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
273 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
274 }
275 }
276
277 void
278 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
279 {
280 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
281 }
282
283
284 /*
285 * Execute a glScale call
286 */
287 void
288 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
289 {
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;
294 }
295
296
297 void
298 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
299 {
300 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
301 }
302
303
304 /*
305 * Execute a glTranslate call
306 */
307 void
308 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
309 {
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;
314 }
315
316
317 void
318 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
319 {
320 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
321 }
322
323
324 void
325 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
326 {
327 GLfloat tm[16];
328 if (!m) return;
329 _math_transposef(tm, m);
330 _mesa_LoadMatrixf(tm);
331 }
332
333
334 void
335 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
336 {
337 GLfloat tm[16];
338 if (!m) return;
339 _math_transposefd(tm, m);
340 _mesa_LoadMatrixf(tm);
341 }
342
343
344 void
345 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
346 {
347 GLfloat tm[16];
348 if (!m) return;
349 _math_transposef(tm, m);
350 _mesa_MultMatrixf(tm);
351 }
352
353
354 void
355 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
356 {
357 GLfloat tm[16];
358 if (!m) return;
359 _math_transposefd(tm, m);
360 _mesa_MultMatrixf(tm);
361 }
362
363
364 /*
365 * Called via glViewport or display list execution.
366 */
367 void
368 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
369 {
370 GET_CURRENT_CONTEXT(ctx);
371 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
372 _mesa_set_viewport(ctx, x, y, width, height);
373 }
374
375
376 /*
377 * Define a new viewport and reallocate auxillary buffers if the size of
378 * the window (color buffer) has changed.
379 */
380 void
381 _mesa_set_viewport( GLcontext *ctx, GLint x, GLint y,
382 GLsizei width, GLsizei height )
383 {
384 const GLfloat n = ctx->Viewport.Near;
385 const GLfloat f = ctx->Viewport.Far;
386
387 if (width < 0 || height < 0) {
388 _mesa_error( ctx, GL_INVALID_VALUE, "glViewport" );
389 return;
390 }
391
392 if (MESA_VERBOSE & VERBOSE_API)
393 fprintf(stderr, "glViewport %d %d %d %d\n", x, y, width, height);
394
395 /* clamp width, and height to implementation dependent range */
396 width = CLAMP( width, 1, MAX_WIDTH );
397 height = CLAMP( height, 1, MAX_HEIGHT );
398
399 /* Save viewport */
400 ctx->Viewport.X = x;
401 ctx->Viewport.Width = width;
402 ctx->Viewport.Y = y;
403 ctx->Viewport.Height = height;
404
405 /* compute scale and bias values :: This is really driver-specific
406 * and should be maintained elsewhere if at all.
407 */
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;
417
418 /* Check if window/buffer has been resized and if so, reallocate the
419 * ancillary buffers.
420 */
421 _mesa_ResizeBuffersMESA();
422
423 if (ctx->Driver.Viewport) {
424 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
425 }
426 }
427
428
429
430 void
431 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
432 {
433 /*
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
438 *
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.
443 */
444 GLfloat n, f;
445 GET_CURRENT_CONTEXT(ctx);
446 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
447
448 if (MESA_VERBOSE&VERBOSE_API)
449 fprintf(stderr, "glDepthRange %f %f\n", nearval, farval);
450
451 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
452 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
453
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;
459
460 if (ctx->Driver.DepthRange) {
461 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
462 }
463 }