Killed mmath.[ch]. Moved low-level functions/assembly code into imports.[ch]
[mesa.git] / src / mesa / main / matrix.c
1 /* $Id: matrix.c,v 1.46 2003/03/01 01:50:21 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 5.1
6 *
7 * Copyright (C) 1999-2003 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 #include "glheader.h"
39 #include "imports.h"
40 #include "buffers.h"
41 #include "context.h"
42 #include "enums.h"
43 #include "macros.h"
44 #include "matrix.h"
45 #include "mtypes.h"
46 #include "math/m_matrix.h"
47
48
49
50 void
51 _mesa_Frustum( GLdouble left, GLdouble right,
52 GLdouble bottom, GLdouble top,
53 GLdouble nearval, GLdouble farval )
54 {
55 GET_CURRENT_CONTEXT(ctx);
56 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
57
58 if (nearval <= 0.0 ||
59 farval <= 0.0 ||
60 nearval == farval ||
61 left == right ||
62 top == bottom)
63 {
64 _mesa_error( ctx, GL_INVALID_VALUE, "glFrustum" );
65 return;
66 }
67
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;
73 }
74
75
76 void
77 _mesa_Ortho( GLdouble left, GLdouble right,
78 GLdouble bottom, GLdouble top,
79 GLdouble nearval, GLdouble farval )
80 {
81 GET_CURRENT_CONTEXT(ctx);
82 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
83
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);
87
88 if (left == right ||
89 bottom == top ||
90 nearval == farval)
91 {
92 _mesa_error( ctx, GL_INVALID_VALUE, "glOrtho" );
93 return;
94 }
95
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;
101 }
102
103
104 void
105 _mesa_MatrixMode( GLenum mode )
106 {
107 GET_CURRENT_CONTEXT(ctx);
108 ASSERT_OUTSIDE_BEGIN_END(ctx);
109
110 if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
111 return;
112 FLUSH_VERTICES(ctx, _NEW_TRANSFORM);
113
114 switch (mode) {
115 case GL_MODELVIEW:
116 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
117 break;
118 case GL_PROJECTION:
119 ctx->CurrentStack = &ctx->ProjectionMatrixStack;
120 break;
121 case GL_TEXTURE:
122 ctx->CurrentStack = &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
123 break;
124 case GL_COLOR:
125 ctx->CurrentStack = &ctx->ColorMatrixStack;
126 break;
127 case GL_MATRIX0_NV:
128 case GL_MATRIX1_NV:
129 case GL_MATRIX2_NV:
130 case GL_MATRIX3_NV:
131 case GL_MATRIX4_NV:
132 case GL_MATRIX5_NV:
133 case GL_MATRIX6_NV:
134 case GL_MATRIX7_NV:
135 if (!ctx->Extensions.NV_vertex_program) {
136 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode" );
137 return;
138 }
139 ctx->CurrentStack = &ctx->ProgramMatrixStack[mode - GL_MATRIX0_NV];
140 break;
141 default:
142 _mesa_error( ctx, GL_INVALID_ENUM, "glMatrixMode" );
143 return;
144 }
145
146 ctx->Transform.MatrixMode = mode;
147 }
148
149
150
151 void
152 _mesa_PushMatrix( void )
153 {
154 GET_CURRENT_CONTEXT(ctx);
155 struct matrix_stack *stack = ctx->CurrentStack;
156 ASSERT_OUTSIDE_BEGIN_END(ctx);
157
158 if (MESA_VERBOSE&VERBOSE_API)
159 _mesa_debug(ctx, "glPushMatrix %s\n",
160 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
161
162 if (stack->Depth + 1 >= stack->MaxDepth) {
163 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushMatrix" );
164 return;
165 }
166 _math_matrix_copy( &stack->Stack[stack->Depth + 1],
167 &stack->Stack[stack->Depth] );
168 stack->Depth++;
169 stack->Top = &(stack->Stack[stack->Depth]);
170 ctx->NewState |= stack->DirtyFlag;
171 }
172
173
174
175 void
176 _mesa_PopMatrix( void )
177 {
178 GET_CURRENT_CONTEXT(ctx);
179 struct matrix_stack *stack = ctx->CurrentStack;
180 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
181
182 if (MESA_VERBOSE&VERBOSE_API)
183 _mesa_debug(ctx, "glPopMatrix %s\n",
184 _mesa_lookup_enum_by_nr(ctx->Transform.MatrixMode));
185
186 if (stack->Depth == 0) {
187 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopMatrix" );
188 return;
189 }
190 stack->Depth--;
191 stack->Top = &(stack->Stack[stack->Depth]);
192 ctx->NewState |= stack->DirtyFlag;
193 }
194
195
196
197 void
198 _mesa_LoadIdentity( void )
199 {
200 GET_CURRENT_CONTEXT(ctx);
201 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
202
203 if (MESA_VERBOSE & VERBOSE_API)
204 _mesa_debug(ctx, "glLoadIdentity()");
205
206 _math_matrix_set_identity( ctx->CurrentStack->Top );
207 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
208 }
209
210
211 void
212 _mesa_LoadMatrixf( const GLfloat *m )
213 {
214 GET_CURRENT_CONTEXT(ctx);
215 if (!m) return;
216 if (MESA_VERBOSE & VERBOSE_API)
217 _mesa_debug(ctx,
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]);
223
224 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
225 _math_matrix_loadf( ctx->CurrentStack->Top, m );
226 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
227 }
228
229
230 void
231 _mesa_LoadMatrixd( const GLdouble *m )
232 {
233 GLint i;
234 GLfloat f[16];
235 if (!m) return;
236 for (i = 0; i < 16; i++)
237 f[i] = (GLfloat) m[i];
238 _mesa_LoadMatrixf(f);
239 }
240
241
242
243 /*
244 * Multiply the active matrix by an arbitary matrix.
245 */
246 void
247 _mesa_MultMatrixf( const GLfloat *m )
248 {
249 GET_CURRENT_CONTEXT(ctx);
250 if (!m) return;
251 if (MESA_VERBOSE & VERBOSE_API)
252 _mesa_debug(ctx,
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;
261 }
262
263
264 /*
265 * Multiply the active matrix by an arbitary matrix.
266 */
267 void
268 _mesa_MultMatrixd( const GLdouble *m )
269 {
270 GLint i;
271 GLfloat f[16];
272 if (!m) return;
273 for (i = 0; i < 16; i++)
274 f[i] = (GLfloat) m[i];
275 _mesa_MultMatrixf( f );
276 }
277
278
279
280
281 /*
282 * Execute a glRotate call
283 */
284 void
285 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
286 {
287 GET_CURRENT_CONTEXT(ctx);
288 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
289 if (angle != 0.0F) {
290 _math_matrix_rotate( ctx->CurrentStack->Top, angle, x, y, z);
291 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
292 }
293 }
294
295 void
296 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
297 {
298 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
299 }
300
301
302 /*
303 * Execute a glScale call
304 */
305 void
306 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
307 {
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;
312 }
313
314
315 void
316 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
317 {
318 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
319 }
320
321
322 /*
323 * Execute a glTranslate call
324 */
325 void
326 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
327 {
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;
332 }
333
334
335 void
336 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
337 {
338 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
339 }
340
341
342 void
343 _mesa_LoadTransposeMatrixfARB( const GLfloat *m )
344 {
345 GLfloat tm[16];
346 if (!m) return;
347 _math_transposef(tm, m);
348 _mesa_LoadMatrixf(tm);
349 }
350
351
352 void
353 _mesa_LoadTransposeMatrixdARB( const GLdouble *m )
354 {
355 GLfloat tm[16];
356 if (!m) return;
357 _math_transposefd(tm, m);
358 _mesa_LoadMatrixf(tm);
359 }
360
361
362 void
363 _mesa_MultTransposeMatrixfARB( const GLfloat *m )
364 {
365 GLfloat tm[16];
366 if (!m) return;
367 _math_transposef(tm, m);
368 _mesa_MultMatrixf(tm);
369 }
370
371
372 void
373 _mesa_MultTransposeMatrixdARB( const GLdouble *m )
374 {
375 GLfloat tm[16];
376 if (!m) return;
377 _math_transposefd(tm, m);
378 _mesa_MultMatrixf(tm);
379 }
380
381
382 /*
383 * Called via glViewport or display list execution.
384 */
385 void
386 _mesa_Viewport( GLint x, GLint y, GLsizei width, GLsizei height )
387 {
388 GET_CURRENT_CONTEXT(ctx);
389 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
390 _mesa_set_viewport(ctx, x, y, width, height);
391 }
392
393
394 /*
395 * Define a new viewport and reallocate auxillary buffers if the size of
396 * the window (color buffer) has changed.
397 */
398 void
399 _mesa_set_viewport( GLcontext *ctx, GLint x, GLint y,
400 GLsizei width, GLsizei height )
401 {
402 const GLfloat n = ctx->Viewport.Near;
403 const GLfloat f = ctx->Viewport.Far;
404
405 if (MESA_VERBOSE & VERBOSE_API)
406 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
407
408 if (width < 0 || height < 0) {
409 _mesa_error( ctx, GL_INVALID_VALUE,
410 "glViewport(%d, %d, %d, %d)", x, y, width, height );
411 return;
412 }
413
414 /* clamp width, and height to implementation dependent range */
415 width = CLAMP( width, 1, MAX_WIDTH );
416 height = CLAMP( height, 1, MAX_HEIGHT );
417
418 /* Save viewport */
419 ctx->Viewport.X = x;
420 ctx->Viewport.Width = width;
421 ctx->Viewport.Y = y;
422 ctx->Viewport.Height = height;
423
424 /* compute scale and bias values :: This is really driver-specific
425 * and should be maintained elsewhere if at all.
426 */
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;
436
437 /* Check if window/buffer has been resized and if so, reallocate the
438 * ancillary buffers.
439 */
440 _mesa_ResizeBuffersMESA();
441
442 if (ctx->Driver.Viewport) {
443 (*ctx->Driver.Viewport)( ctx, x, y, width, height );
444 }
445 }
446
447
448
449 void
450 _mesa_DepthRange( GLclampd nearval, GLclampd farval )
451 {
452 /*
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
457 *
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.
462 */
463 GLfloat n, f;
464 GET_CURRENT_CONTEXT(ctx);
465 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
466
467 if (MESA_VERBOSE&VERBOSE_API)
468 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
469
470 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
471 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
472
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;
478
479 if (ctx->Driver.DepthRange) {
480 (*ctx->Driver.DepthRange)( ctx, nearval, farval );
481 }
482 }