fixed a multitexture MatrixMode() bug
[mesa.git] / src / mesa / main / matrix.c
1 /* $Id: matrix.c,v 1.39 2002/02/15 16:24:37 brianp 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 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
213 _math_matrix_loadf( ctx->CurrentStack->Top, m );
214 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
215 }
216
217
218 void
219 _mesa_LoadMatrixd( const GLdouble *m )
220 {
221 GLint i;
222 GLfloat f[16];
223 for (i = 0; i < 16; i++)
224 f[i] = (GLfloat) m[i];
225 _mesa_LoadMatrixf(f);
226 }
227
228
229
230 /*
231 * Multiply the active matrix by an arbitary matrix.
232 */
233 void
234 _mesa_MultMatrixf( const GLfloat *m )
235 {
236 GET_CURRENT_CONTEXT(ctx);
237 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
238 _math_matrix_mul_floats( ctx->CurrentStack->Top, m );
239 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
240 }
241
242
243 /*
244 * Multiply the active matrix by an arbitary matrix.
245 */
246 void
247 _mesa_MultMatrixd( const GLdouble *m )
248 {
249 GLint i;
250 GLfloat f[16];
251 for (i = 0; i < 16; i++)
252 f[i] = (GLfloat) m[i];
253 _mesa_MultMatrixf( f );
254 }
255
256
257
258
259 /*
260 * Execute a glRotate call
261 */
262 void
263 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
264 {
265 GET_CURRENT_CONTEXT(ctx);
266 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
267 if (angle != 0.0F) {
268 _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
269 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
270 }
271 }
272
273 void
274 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
275 {
276 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
277 }
278
279
280 /*
281 * Execute a glScale call
282 */
283 void
284 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
285 {
286 GET_CURRENT_CONTEXT(ctx);
287 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
288 _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
289 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
290 }
291
292
293 void
294 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
295 {
296 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
297 }
298
299
300 /*
301 * Execute a glTranslate call
302 */
303 void
304 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
305 {
306 GET_CURRENT_CONTEXT(ctx);
307 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
308 _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
309 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
310 }
311
312
313 void
314 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
315 {
316 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
317 }
318
319
320 void
321 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
322 {
323 GLfloat tm[16];
324 _math_transposef(tm, m);
325 _mesa_LoadMatrixf(tm);
326 }
327
328
329 void
330 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
331 {
332 GLfloat tm[16];
333 _math_transposefd(tm, m);
334 _mesa_LoadMatrixf(tm);
335 }
336
337
338 void
339 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
340 {
341 GLfloat tm[16];
342 _math_transposef(tm, m);
343 _mesa_MultMatrixf(tm);
344 }
345
346
347 void
348 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
349 {
350 GLfloat tm[16];
351 _math_transposefd(tm, m);
352 _mesa_MultMatrixf(tm);
353 }
354
355
356 /*
357 * Called via glViewport or display list execution.
358 */
359 void
360 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
361 {
362 GET_CURRENT_CONTEXT(ctx);
363 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
364 _mesa_set_viewport(ctx, x, y, width, height);
365 }
366
367
368 /*
369 * Define a new viewport and reallocate auxillary buffers if the size of
370 * the window (color buffer) has changed.
371 */
372 void
373 _mesa_set_viewport( GLcontext *ctx, GLint x, GLint y,
374 GLsizei width, GLsizei height )
375 {
376 const GLfloat n = ctx->Viewport.Near;
377 const GLfloat f = ctx->Viewport.Far;
378
379 if (width < 0 || height < 0) {
380 _mesa_error( ctx, GL_INVALID_VALUE, "glViewport" );
381 return;
382 }
383
384 if (MESA_VERBOSE & VERBOSE_API)
385 fprintf(stderr, "glViewport %d %d %d %d\n", x, y, width, height);
386
387 /* clamp width, and height to implementation dependent range */
388 width = CLAMP( width, 1, MAX_WIDTH );
389 height = CLAMP( height, 1, MAX_HEIGHT );
390
391 /* Save viewport */
392 ctx->Viewport.X = x;
393 ctx->Viewport.Width = width;
394 ctx->Viewport.Y = y;
395 ctx->Viewport.Height = height;
396
397 /* compute scale and bias values :: This is really driver-specific
398 * and should be maintained elsewhere if at all.
399 */
400 ctx->Viewport._WindowMap.m[MAT_SX] = (GLfloat) width / 2.0F;
401 ctx->Viewport._WindowMap.m[MAT_TX] = ctx->Viewport._WindowMap.m[MAT_SX] + x;
402 ctx->Viewport._WindowMap.m[MAT_SY] = (GLfloat) height / 2.0F;
403 ctx->Viewport._WindowMap.m[MAT_TY] = ctx->Viewport._WindowMap.m[MAT_SY] + y;
404 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0F);
405 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0F + n);
406 ctx->Viewport._WindowMap.flags = MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION;
407 ctx->Viewport._WindowMap.type = MATRIX_3D_NO_ROT;
408 ctx->NewState |= _NEW_VIEWPORT;
409
410 /* Check if window/buffer has been resized and if so, reallocate the
411 * ancillary buffers.
412 */
413 _mesa_ResizeBuffersMESA();
414
415 if (ctx->Driver.Viewport) {
416 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
417 }
418 }
419
420
421
422 void
423 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
424 {
425 /*
426 * nearval - specifies mapping of the near clipping plane to window
427 * coordinates, default is 0
428 * farval - specifies mapping of the far clipping plane to window
429 * coordinates, default is 1
430 *
431 * After clipping and div by w, z coords are in -1.0 to 1.0,
432 * corresponding to near and far clipping planes. glDepthRange
433 * specifies a linear mapping of the normalized z coords in
434 * this range to window z coords.
435 */
436 GLfloat n, f;
437 GET_CURRENT_CONTEXT(ctx);
438 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
439
440 if (MESA_VERBOSE&VERBOSE_API)
441 fprintf(stderr, "glDepthRange %f %f\n", nearval, farval);
442
443 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
444 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
445
446 ctx->Viewport.Near = n;
447 ctx->Viewport.Far = f;
448 ctx->Viewport._WindowMap.m[MAT_SZ] = ctx->DepthMaxF * ((f - n) / 2.0F);
449 ctx->Viewport._WindowMap.m[MAT_TZ] = ctx->DepthMaxF * ((f - n) / 2.0F + n);
450 ctx->NewState |= _NEW_VIEWPORT;
451
452 if (ctx->Driver.DepthRange) {
453 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
454 }
455 }