1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * \file matrix.c
29 * Matrix operations.
30 *
31 * \note
32 * -# 4x4 transformation matrices are stored in memory in column major order.
33 * -# Points/vertices are to be thought of as column vectors.
34 * -# Transformation of a point p by a matrix M is: p' = M * p
35 */
36
37
38 #include "util/glheader.h"
39
40 #include "context.h"
41 #include "enums.h"
42 #include "macros.h"
43 #include "matrix.h"
44 #include "mtypes.h"
45 #include "math/m_matrix.h"
46 #include "util/bitscan.h"
47 #include "api_exec_decl.h"
48
49
50 static struct gl_matrix_stack *
get_named_matrix_stack(struct gl_context * ctx,GLenum mode,const char * caller)51 get_named_matrix_stack(struct gl_context *ctx, GLenum mode, const char* caller)
52 {
53 switch (mode) {
54 case GL_MODELVIEW:
55 return &ctx->ModelviewMatrixStack;
56 case GL_PROJECTION:
57 return &ctx->ProjectionMatrixStack;
58 case GL_TEXTURE:
59 /* This error check is disabled because if we're called from
60 * glPopAttrib() when the active texture unit is >= MaxTextureCoordUnits
61 * we'll generate an unexpected error.
62 * From the GL_ARB_vertex_shader spec it sounds like we should instead
63 * do error checking in other places when we actually try to access
64 * texture matrices beyond MaxTextureCoordUnits.
65 */
66 #if 0
67 if (ctx->Texture.CurrentUnit >= ctx->Const.MaxTextureCoordUnits) {
68 _mesa_error(ctx, GL_INVALID_OPERATION,
69 "glMatrixMode(invalid tex unit %d)",
70 ctx->Texture.CurrentUnit);
71 return;
72 }
73 #endif
74 assert(ctx->Texture.CurrentUnit < ARRAY_SIZE(ctx->TextureMatrixStack));
75 return &ctx->TextureMatrixStack[ctx->Texture.CurrentUnit];
76 case GL_MATRIX0_ARB:
77 case GL_MATRIX1_ARB:
78 case GL_MATRIX2_ARB:
79 case GL_MATRIX3_ARB:
80 case GL_MATRIX4_ARB:
81 case GL_MATRIX5_ARB:
82 case GL_MATRIX6_ARB:
83 case GL_MATRIX7_ARB:
84 if (_mesa_is_desktop_gl_compat(ctx)
85 && (ctx->Extensions.ARB_vertex_program ||
86 ctx->Extensions.ARB_fragment_program)) {
87 const GLuint m = mode - GL_MATRIX0_ARB;
88 if (m <= ctx->Const.MaxProgramMatrices)
89 return &ctx->ProgramMatrixStack[m];
90 }
91 FALLTHROUGH;
92 default:
93 break;
94 }
95 if (mode >= GL_TEXTURE0 && mode < (GL_TEXTURE0 + ctx->Const.MaxTextureCoordUnits)) {
96 return &ctx->TextureMatrixStack[mode - GL_TEXTURE0];
97 }
98 _mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
99 return NULL;
100 }
101
102
matrix_frustum(struct gl_matrix_stack * stack,GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval,const char * caller)103 static void matrix_frustum(struct gl_matrix_stack* stack,
104 GLdouble left, GLdouble right,
105 GLdouble bottom, GLdouble top,
106 GLdouble nearval, GLdouble farval,
107 const char* caller)
108 {
109 GET_CURRENT_CONTEXT(ctx);
110 if (nearval <= 0.0 ||
111 farval <= 0.0 ||
112 nearval == farval ||
113 left == right ||
114 top == bottom) {
115 _mesa_error(ctx, GL_INVALID_VALUE, "%s", caller);
116 return;
117 }
118
119 FLUSH_VERTICES(ctx, 0, 0);
120
121 _math_matrix_frustum(stack->Top,
122 (GLfloat) left, (GLfloat) right,
123 (GLfloat) bottom, (GLfloat) top,
124 (GLfloat) nearval, (GLfloat) farval);
125 stack->ChangedSincePush = true;
126 ctx->NewState |= stack->DirtyFlag;
127 }
128
129
130 /**
131 * Apply a perspective projection matrix.
132 *
133 * \param left left clipping plane coordinate.
134 * \param right right clipping plane coordinate.
135 * \param bottom bottom clipping plane coordinate.
136 * \param top top clipping plane coordinate.
137 * \param nearval distance to the near clipping plane.
138 * \param farval distance to the far clipping plane.
139 *
140 * \sa glFrustum().
141 *
142 * Flushes vertices and validates parameters. Calls _math_matrix_frustum() with
143 * the top matrix of the current matrix stack and sets
144 * __struct gl_contextRec::NewState.
145 */
146 void GLAPIENTRY
_mesa_Frustum(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval)147 _mesa_Frustum( GLdouble left, GLdouble right,
148 GLdouble bottom, GLdouble top,
149 GLdouble nearval, GLdouble farval )
150 {
151 GET_CURRENT_CONTEXT(ctx);
152 matrix_frustum(ctx->CurrentStack,
153 (GLfloat) left, (GLfloat) right,
154 (GLfloat) bottom, (GLfloat) top,
155 (GLfloat) nearval, (GLfloat) farval,
156 "glFrustum");
157 }
158
159
160 void GLAPIENTRY
_mesa_MatrixFrustumEXT(GLenum matrixMode,GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval)161 _mesa_MatrixFrustumEXT( GLenum matrixMode,
162 GLdouble left, GLdouble right,
163 GLdouble bottom, GLdouble top,
164 GLdouble nearval, GLdouble farval )
165 {
166 GET_CURRENT_CONTEXT(ctx);
167 struct gl_matrix_stack *stack = get_named_matrix_stack(ctx, matrixMode,
168 "glMatrixFrustumEXT");
169 if (!stack)
170 return;
171
172 matrix_frustum(stack,
173 (GLfloat) left, (GLfloat) right,
174 (GLfloat) bottom, (GLfloat) top,
175 (GLfloat) nearval, (GLfloat) farval,
176 "glMatrixFrustumEXT");
177 }
178
179
180 static void
matrix_ortho(struct gl_matrix_stack * stack,GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval,const char * caller)181 matrix_ortho(struct gl_matrix_stack* stack,
182 GLdouble left, GLdouble right,
183 GLdouble bottom, GLdouble top,
184 GLdouble nearval, GLdouble farval,
185 const char* caller)
186 {
187 GET_CURRENT_CONTEXT(ctx);
188
189 if (MESA_VERBOSE & VERBOSE_API)
190 _mesa_debug(ctx, "%s(%f, %f, %f, %f, %f, %f)\n", caller,
191 left, right, bottom, top, nearval, farval);
192
193 if (left == right ||
194 bottom == top ||
195 nearval == farval)
196 {
197 _mesa_error( ctx, GL_INVALID_VALUE, "%s", caller );
198 return;
199 }
200
201 FLUSH_VERTICES(ctx, 0, 0);
202
203 _math_matrix_ortho( stack->Top,
204 (GLfloat) left, (GLfloat) right,
205 (GLfloat) bottom, (GLfloat) top,
206 (GLfloat) nearval, (GLfloat) farval );
207 stack->ChangedSincePush = true;
208 ctx->NewState |= stack->DirtyFlag;
209 }
210
211
212 /**
213 * Apply an orthographic projection matrix.
214 *
215 * \param left left clipping plane coordinate.
216 * \param right right clipping plane coordinate.
217 * \param bottom bottom clipping plane coordinate.
218 * \param top top clipping plane coordinate.
219 * \param nearval distance to the near clipping plane.
220 * \param farval distance to the far clipping plane.
221 *
222 * \sa glOrtho().
223 *
224 * Flushes vertices and validates parameters. Calls _math_matrix_ortho() with
225 * the top matrix of the current matrix stack and sets
226 * __struct gl_contextRec::NewState.
227 */
228 void GLAPIENTRY
_mesa_Ortho(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval)229 _mesa_Ortho( GLdouble left, GLdouble right,
230 GLdouble bottom, GLdouble top,
231 GLdouble nearval, GLdouble farval )
232 {
233 GET_CURRENT_CONTEXT(ctx);
234 matrix_ortho(ctx->CurrentStack,
235 (GLfloat) left, (GLfloat) right,
236 (GLfloat) bottom, (GLfloat) top,
237 (GLfloat) nearval, (GLfloat) farval,
238 "glOrtho");
239 }
240
241
242 void GLAPIENTRY
_mesa_MatrixOrthoEXT(GLenum matrixMode,GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval)243 _mesa_MatrixOrthoEXT( GLenum matrixMode,
244 GLdouble left, GLdouble right,
245 GLdouble bottom, GLdouble top,
246 GLdouble nearval, GLdouble farval )
247 {
248 GET_CURRENT_CONTEXT(ctx);
249 struct gl_matrix_stack *stack = get_named_matrix_stack(ctx, matrixMode,
250 "glMatrixOrthoEXT");
251 if (!stack)
252 return;
253
254 matrix_ortho(stack,
255 (GLfloat) left, (GLfloat) right,
256 (GLfloat) bottom, (GLfloat) top,
257 (GLfloat) nearval, (GLfloat) farval,
258 "glMatrixOrthoEXT");
259 }
260
261
262 /**
263 * Set the current matrix stack.
264 *
265 * \param mode matrix stack.
266 *
267 * \sa glMatrixMode().
268 *
269 * Flushes the vertices, validates the parameter and updates
270 * __struct gl_contextRec::CurrentStack and gl_transform_attrib::MatrixMode
271 * with the specified matrix stack.
272 */
273 void GLAPIENTRY
_mesa_MatrixMode(GLenum mode)274 _mesa_MatrixMode( GLenum mode )
275 {
276 struct gl_matrix_stack * stack;
277 GET_CURRENT_CONTEXT(ctx);
278
279 if (ctx->Transform.MatrixMode == mode && mode != GL_TEXTURE)
280 return;
281
282 if (mode >= GL_TEXTURE0 && mode < (GL_TEXTURE0 + ctx->Const.MaxTextureCoordUnits)) {
283 stack = NULL;
284 } else {
285 stack = get_named_matrix_stack(ctx, mode, "glMatrixMode");
286 }
287
288 if (stack) {
289 ctx->CurrentStack = stack;
290 ctx->Transform.MatrixMode = mode;
291 ctx->PopAttribState |= GL_TRANSFORM_BIT;
292 }
293 }
294
295
296 static void
push_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack,GLenum matrixMode,const char * func)297 push_matrix(struct gl_context *ctx, struct gl_matrix_stack *stack,
298 GLenum matrixMode, const char *func)
299 {
300 if (stack->Depth + 1 >= stack->MaxDepth) {
301 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
302 _mesa_error(ctx, GL_STACK_OVERFLOW, "%s(mode=GL_TEXTURE, unit=%d)",
303 func, ctx->Texture.CurrentUnit);
304 } else {
305 _mesa_error(ctx, GL_STACK_OVERFLOW, "%s(mode=%s)",
306 func, _mesa_enum_to_string(matrixMode));
307 }
308 return;
309 }
310
311 if (stack->Depth + 1 >= stack->StackSize) {
312 unsigned new_stack_size = stack->StackSize * 2;
313 unsigned i;
314 GLmatrix *new_stack = realloc(stack->Stack,
315 sizeof(*new_stack) * new_stack_size);
316
317 if (!new_stack) {
318 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
319 return;
320 }
321
322 for (i = stack->StackSize; i < new_stack_size; i++)
323 _math_matrix_ctr(&new_stack[i]);
324
325 stack->Stack = new_stack;
326 stack->StackSize = new_stack_size;
327 }
328
329 _math_matrix_push_copy(&stack->Stack[stack->Depth + 1],
330 &stack->Stack[stack->Depth]);
331 stack->Depth++;
332 stack->Top = &(stack->Stack[stack->Depth]);
333 stack->ChangedSincePush = false;
334 }
335
336
337 /**
338 * Push the current matrix stack.
339 *
340 * \sa glPushMatrix().
341 *
342 * Verifies the current matrix stack is not full, and duplicates the top-most
343 * matrix in the stack.
344 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
345 */
346 void GLAPIENTRY
_mesa_PushMatrix(void)347 _mesa_PushMatrix( void )
348 {
349 GET_CURRENT_CONTEXT(ctx);
350 struct gl_matrix_stack *stack = ctx->CurrentStack;
351
352 if (MESA_VERBOSE&VERBOSE_API)
353 _mesa_debug(ctx, "glPushMatrix %s\n",
354 _mesa_enum_to_string(ctx->Transform.MatrixMode));
355
356 push_matrix(ctx, stack, ctx->Transform.MatrixMode, "glPushMatrix");
357 }
358
359
360 void GLAPIENTRY
_mesa_MatrixPushEXT(GLenum matrixMode)361 _mesa_MatrixPushEXT( GLenum matrixMode )
362 {
363 GET_CURRENT_CONTEXT(ctx);
364 struct gl_matrix_stack *stack = get_named_matrix_stack(ctx, matrixMode,
365 "glMatrixPushEXT");
366 ASSERT_OUTSIDE_BEGIN_END(ctx);
367 if (stack)
368 push_matrix(ctx, stack, matrixMode, "glMatrixPushEXT");
369 }
370
371
372 static GLboolean
pop_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack)373 pop_matrix( struct gl_context *ctx, struct gl_matrix_stack *stack )
374 {
375 if (stack->Depth == 0)
376 return GL_FALSE;
377
378 stack->Depth--;
379
380 /* If the popped matrix is the same as the current one, treat it as
381 * a no-op change.
382 */
383 if (stack->ChangedSincePush &&
384 memcmp(stack->Top, &stack->Stack[stack->Depth],
385 sizeof(GLmatrix))) {
386 FLUSH_VERTICES(ctx, 0, 0);
387 ctx->NewState |= stack->DirtyFlag;
388 }
389
390 stack->Top = &(stack->Stack[stack->Depth]);
391 stack->ChangedSincePush = true;
392 return GL_TRUE;
393 }
394
395
396 /**
397 * Pop the current matrix stack.
398 *
399 * \sa glPopMatrix().
400 *
401 * Flushes the vertices, verifies the current matrix stack is not empty, and
402 * moves the stack head down.
403 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
404 */
405 void GLAPIENTRY
_mesa_PopMatrix(void)406 _mesa_PopMatrix( void )
407 {
408 GET_CURRENT_CONTEXT(ctx);
409 struct gl_matrix_stack *stack = ctx->CurrentStack;
410
411 if (MESA_VERBOSE&VERBOSE_API)
412 _mesa_debug(ctx, "glPopMatrix %s\n",
413 _mesa_enum_to_string(ctx->Transform.MatrixMode));
414
415 if (!pop_matrix(ctx, stack)) {
416 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
417 _mesa_error(ctx, GL_STACK_UNDERFLOW,
418 "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
419 ctx->Texture.CurrentUnit);
420 }
421 else {
422 _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)",
423 _mesa_enum_to_string(ctx->Transform.MatrixMode));
424 }
425 }
426 }
427
428
429 void GLAPIENTRY
_mesa_MatrixPopEXT(GLenum matrixMode)430 _mesa_MatrixPopEXT( GLenum matrixMode )
431 {
432 GET_CURRENT_CONTEXT(ctx);
433 struct gl_matrix_stack *stack = get_named_matrix_stack(ctx, matrixMode,
434 "glMatrixPopEXT");
435 if (!stack)
436 return;
437
438 if (!pop_matrix(ctx, stack)) {
439 if (matrixMode == GL_TEXTURE) {
440 _mesa_error(ctx, GL_STACK_UNDERFLOW,
441 "glMatrixPopEXT(mode=GL_TEXTURE, unit=%d)",
442 ctx->Texture.CurrentUnit);
443 }
444 else {
445 _mesa_error(ctx, GL_STACK_UNDERFLOW, "glMatrixPopEXT(mode=%s)",
446 _mesa_enum_to_string(matrixMode));
447 }
448 }
449 }
450
451
452 void
_mesa_load_identity_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack)453 _mesa_load_identity_matrix(struct gl_context *ctx, struct gl_matrix_stack *stack)
454 {
455 FLUSH_VERTICES(ctx, 0, 0);
456
457 _math_matrix_set_identity(stack->Top);
458 stack->ChangedSincePush = true;
459 ctx->NewState |= stack->DirtyFlag;
460 }
461
462
463 /**
464 * Replace the current matrix with the identity matrix.
465 *
466 * \sa glLoadIdentity().
467 *
468 * Flushes the vertices and calls _math_matrix_set_identity() with the
469 * top-most matrix in the current stack.
470 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
471 */
472 void GLAPIENTRY
_mesa_LoadIdentity(void)473 _mesa_LoadIdentity( void )
474 {
475 GET_CURRENT_CONTEXT(ctx);
476
477 if (MESA_VERBOSE & VERBOSE_API)
478 _mesa_debug(ctx, "glLoadIdentity()\n");
479
480 _mesa_load_identity_matrix(ctx, ctx->CurrentStack);
481 }
482
483
484 void GLAPIENTRY
_mesa_MatrixLoadIdentityEXT(GLenum matrixMode)485 _mesa_MatrixLoadIdentityEXT( GLenum matrixMode )
486 {
487 struct gl_matrix_stack *stack;
488 GET_CURRENT_CONTEXT(ctx);
489 stack = get_named_matrix_stack(ctx, matrixMode, "glMatrixLoadIdentityEXT");
490 if (!stack)
491 return;
492
493 _mesa_load_identity_matrix(ctx, stack);
494 }
495
496
497 void
_mesa_load_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack,const GLfloat * m)498 _mesa_load_matrix(struct gl_context *ctx, struct gl_matrix_stack *stack,
499 const GLfloat *m)
500 {
501 if (memcmp(m, stack->Top->m, 16 * sizeof(GLfloat)) != 0) {
502 FLUSH_VERTICES(ctx, 0, 0);
503 _math_matrix_loadf(stack->Top, m);
504 stack->ChangedSincePush = true;
505 ctx->NewState |= stack->DirtyFlag;
506 }
507 }
508
509
510 static void
matrix_load(struct gl_context * ctx,struct gl_matrix_stack * stack,const GLfloat * m,const char * caller)511 matrix_load(struct gl_context *ctx, struct gl_matrix_stack *stack,
512 const GLfloat *m, const char* caller)
513 {
514 if (!m) return;
515 if (MESA_VERBOSE & VERBOSE_API)
516 _mesa_debug(ctx,
517 "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
518 caller,
519 m[0], m[4], m[8], m[12],
520 m[1], m[5], m[9], m[13],
521 m[2], m[6], m[10], m[14],
522 m[3], m[7], m[11], m[15]);
523
524 _mesa_load_matrix(ctx, stack, m);
525 }
526
527
528 /**
529 * Replace the current matrix with a given matrix.
530 *
531 * \param m matrix.
532 *
533 * \sa glLoadMatrixf().
534 *
535 * Flushes the vertices and calls _math_matrix_loadf() with the top-most
536 * matrix in the current stack and the given matrix.
537 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
538 */
539 void GLAPIENTRY
_mesa_LoadMatrixf(const GLfloat * m)540 _mesa_LoadMatrixf( const GLfloat *m )
541 {
542 GET_CURRENT_CONTEXT(ctx);
543 matrix_load(ctx, ctx->CurrentStack, m, "glLoadMatrix");
544 }
545
546
547 /**
548 * Replace the named matrix with a given matrix.
549 *
550 * \param matrixMode matrix to replace
551 * \param m matrix
552 *
553 * \sa glLoadMatrixf().
554 */
555 void GLAPIENTRY
_mesa_MatrixLoadfEXT(GLenum matrixMode,const GLfloat * m)556 _mesa_MatrixLoadfEXT( GLenum matrixMode, const GLfloat *m )
557 {
558 GET_CURRENT_CONTEXT(ctx);
559 struct gl_matrix_stack * stack =
560 get_named_matrix_stack(ctx, matrixMode, "glMatrixLoadfEXT");
561 if (!stack)
562 return;
563
564 matrix_load(ctx, stack, m, "glMatrixLoadfEXT");
565 }
566
567
568 static void
matrix_mult(struct gl_matrix_stack * stack,const GLfloat * m,const char * caller)569 matrix_mult(struct gl_matrix_stack *stack, const GLfloat *m, const char* caller)
570 {
571 GET_CURRENT_CONTEXT(ctx);
572
573 /* glthread filters out identity matrices, so don't do it again. */
574 if (!m || (!ctx->GLThread.enabled && _mesa_matrix_is_identity(m)))
575 return;
576
577 if (MESA_VERBOSE & VERBOSE_API)
578 _mesa_debug(ctx,
579 "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
580 caller,
581 m[0], m[4], m[8], m[12],
582 m[1], m[5], m[9], m[13],
583 m[2], m[6], m[10], m[14],
584 m[3], m[7], m[11], m[15]);
585
586 FLUSH_VERTICES(ctx, 0, 0);
587 _math_matrix_mul_floats(stack->Top, m);
588 stack->ChangedSincePush = true;
589 ctx->NewState |= stack->DirtyFlag;
590 }
591
592
593 /**
594 * Multiply the current matrix with a given matrix.
595 *
596 * \param m matrix.
597 *
598 * \sa glMultMatrixf().
599 *
600 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
601 * matrix in the current stack and the given matrix. Marks
602 * __struct gl_contextRec::NewState with the dirty stack flag.
603 */
604 void GLAPIENTRY
_mesa_MultMatrixf(const GLfloat * m)605 _mesa_MultMatrixf( const GLfloat *m )
606 {
607 GET_CURRENT_CONTEXT(ctx);
608 matrix_mult(ctx->CurrentStack, m, "glMultMatrix");
609 }
610
611
612 void GLAPIENTRY
_mesa_MatrixMultfEXT(GLenum matrixMode,const GLfloat * m)613 _mesa_MatrixMultfEXT( GLenum matrixMode, const GLfloat *m )
614 {
615 GET_CURRENT_CONTEXT(ctx);
616 struct gl_matrix_stack * stack =
617 get_named_matrix_stack(ctx, matrixMode, "glMatrixMultfEXT");
618 if (!stack)
619 return;
620
621 matrix_mult(stack, m, "glMultMatrix");
622 }
623
624
625 static void
matrix_rotate(struct gl_matrix_stack * stack,GLfloat angle,GLfloat x,GLfloat y,GLfloat z,const char * caller)626 matrix_rotate(struct gl_matrix_stack *stack, GLfloat angle,
627 GLfloat x, GLfloat y, GLfloat z, const char* caller)
628 {
629 GET_CURRENT_CONTEXT(ctx);
630
631 FLUSH_VERTICES(ctx, 0, 0);
632 if (angle != 0.0F) {
633 _math_matrix_rotate(stack->Top, angle, x, y, z);
634 stack->ChangedSincePush = true;
635 ctx->NewState |=stack->DirtyFlag;
636 }
637 }
638
639
640 /**
641 * Multiply the current matrix with a rotation matrix.
642 *
643 * \param angle angle of rotation, in degrees.
644 * \param x rotation vector x coordinate.
645 * \param y rotation vector y coordinate.
646 * \param z rotation vector z coordinate.
647 *
648 * \sa glRotatef().
649 *
650 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
651 * matrix in the current stack and the given parameters. Marks
652 * __struct gl_contextRec::NewState with the dirty stack flag.
653 */
654 void GLAPIENTRY
_mesa_Rotatef(GLfloat angle,GLfloat x,GLfloat y,GLfloat z)655 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
656 {
657 GET_CURRENT_CONTEXT(ctx);
658 matrix_rotate(ctx->CurrentStack, angle, x, y, z, "glRotatef");
659 }
660
661
662 void GLAPIENTRY
_mesa_MatrixRotatefEXT(GLenum matrixMode,GLfloat angle,GLfloat x,GLfloat y,GLfloat z)663 _mesa_MatrixRotatefEXT( GLenum matrixMode, GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
664 {
665 GET_CURRENT_CONTEXT(ctx);
666 struct gl_matrix_stack *stack =
667 get_named_matrix_stack(ctx, matrixMode, "glMatrixRotatefEXT");
668 if (!stack)
669 return;
670
671 matrix_rotate(stack, angle, x, y, z, "glMatrixRotatefEXT");
672 }
673
674
675 /**
676 * Multiply the current matrix with a general scaling matrix.
677 *
678 * \param x x axis scale factor.
679 * \param y y axis scale factor.
680 * \param z z axis scale factor.
681 *
682 * \sa glScalef().
683 *
684 * Flushes the vertices and calls _math_matrix_scale() with the top-most
685 * matrix in the current stack and the given parameters. Marks
686 * __struct gl_contextRec::NewState with the dirty stack flag.
687 */
688 void GLAPIENTRY
_mesa_Scalef(GLfloat x,GLfloat y,GLfloat z)689 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
690 {
691 GET_CURRENT_CONTEXT(ctx);
692
693 FLUSH_VERTICES(ctx, 0, 0);
694 _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
695 ctx->CurrentStack->ChangedSincePush = true;
696 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
697 }
698
699
700 void GLAPIENTRY
_mesa_MatrixScalefEXT(GLenum matrixMode,GLfloat x,GLfloat y,GLfloat z)701 _mesa_MatrixScalefEXT( GLenum matrixMode, GLfloat x, GLfloat y, GLfloat z )
702 {
703 struct gl_matrix_stack *stack;
704 GET_CURRENT_CONTEXT(ctx);
705
706 stack = get_named_matrix_stack(ctx, matrixMode, "glMatrixScalefEXT");
707 if (!stack)
708 return;
709
710 FLUSH_VERTICES(ctx, 0, 0);
711 _math_matrix_scale(stack->Top, x, y, z);
712 stack->ChangedSincePush = true;
713 ctx->NewState |= stack->DirtyFlag;
714 }
715
716
717 /**
718 * Multiply the current matrix with a translation matrix.
719 *
720 * \param x translation vector x coordinate.
721 * \param y translation vector y coordinate.
722 * \param z translation vector z coordinate.
723 *
724 * \sa glTranslatef().
725 *
726 * Flushes the vertices and calls _math_matrix_translate() with the top-most
727 * matrix in the current stack and the given parameters. Marks
728 * __struct gl_contextRec::NewState with the dirty stack flag.
729 */
730 void GLAPIENTRY
_mesa_Translatef(GLfloat x,GLfloat y,GLfloat z)731 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
732 {
733 GET_CURRENT_CONTEXT(ctx);
734
735 FLUSH_VERTICES(ctx, 0, 0);
736 _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
737 ctx->CurrentStack->ChangedSincePush = true;
738 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
739 }
740
741
742 void GLAPIENTRY
_mesa_MatrixTranslatefEXT(GLenum matrixMode,GLfloat x,GLfloat y,GLfloat z)743 _mesa_MatrixTranslatefEXT( GLenum matrixMode, GLfloat x, GLfloat y, GLfloat z )
744 {
745 GET_CURRENT_CONTEXT(ctx);
746 struct gl_matrix_stack *stack =
747 get_named_matrix_stack(ctx, matrixMode, "glMatrixTranslatefEXT");
748 if (!stack)
749 return;
750
751 FLUSH_VERTICES(ctx, 0, 0);
752 _math_matrix_translate(stack->Top, x, y, z);
753 stack->ChangedSincePush = true;
754 ctx->NewState |= stack->DirtyFlag;
755 }
756
757
758 void GLAPIENTRY
_mesa_LoadMatrixd(const GLdouble * m)759 _mesa_LoadMatrixd( const GLdouble *m )
760 {
761 GLint i;
762 GLfloat f[16];
763 if (!m) return;
764 for (i = 0; i < 16; i++)
765 f[i] = (GLfloat) m[i];
766 _mesa_LoadMatrixf(f);
767 }
768
769
770 void GLAPIENTRY
_mesa_MatrixLoaddEXT(GLenum matrixMode,const GLdouble * m)771 _mesa_MatrixLoaddEXT( GLenum matrixMode, const GLdouble *m )
772 {
773 GLfloat f[16];
774 if (!m) return;
775 for (unsigned i = 0; i < 16; i++)
776 f[i] = (GLfloat) m[i];
777 _mesa_MatrixLoadfEXT(matrixMode, f);
778 }
779
780
781 void GLAPIENTRY
_mesa_MultMatrixd(const GLdouble * m)782 _mesa_MultMatrixd( const GLdouble *m )
783 {
784 GLint i;
785 GLfloat f[16];
786 if (!m) return;
787 for (i = 0; i < 16; i++)
788 f[i] = (GLfloat) m[i];
789 _mesa_MultMatrixf( f );
790 }
791
792
793 void GLAPIENTRY
_mesa_MatrixMultdEXT(GLenum matrixMode,const GLdouble * m)794 _mesa_MatrixMultdEXT( GLenum matrixMode, const GLdouble *m )
795 {
796 GLfloat f[16];
797 if (!m) return;
798 for (unsigned i = 0; i < 16; i++)
799 f[i] = (GLfloat) m[i];
800 _mesa_MatrixMultfEXT(matrixMode, f);
801 }
802
803
804 void GLAPIENTRY
_mesa_Rotated(GLdouble angle,GLdouble x,GLdouble y,GLdouble z)805 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
806 {
807 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
808 }
809
810
811 void GLAPIENTRY
_mesa_MatrixRotatedEXT(GLenum matrixMode,GLdouble angle,GLdouble x,GLdouble y,GLdouble z)812 _mesa_MatrixRotatedEXT( GLenum matrixMode, GLdouble angle,
813 GLdouble x, GLdouble y, GLdouble z )
814 {
815 _mesa_MatrixRotatefEXT(matrixMode, (GLfloat) angle,
816 (GLfloat) x, (GLfloat) y, (GLfloat) z);
817 }
818
819
820 void GLAPIENTRY
_mesa_Scaled(GLdouble x,GLdouble y,GLdouble z)821 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
822 {
823 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
824 }
825
826
827 void GLAPIENTRY
_mesa_MatrixScaledEXT(GLenum matrixMode,GLdouble x,GLdouble y,GLdouble z)828 _mesa_MatrixScaledEXT( GLenum matrixMode, GLdouble x, GLdouble y, GLdouble z )
829 {
830 _mesa_MatrixScalefEXT(matrixMode, (GLfloat) x, (GLfloat) y, (GLfloat) z);
831 }
832
833
834 void GLAPIENTRY
_mesa_Translated(GLdouble x,GLdouble y,GLdouble z)835 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
836 {
837 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
838 }
839
840
841 void GLAPIENTRY
_mesa_MatrixTranslatedEXT(GLenum matrixMode,GLdouble x,GLdouble y,GLdouble z)842 _mesa_MatrixTranslatedEXT( GLenum matrixMode, GLdouble x, GLdouble y, GLdouble z )
843 {
844 _mesa_MatrixTranslatefEXT(matrixMode, (GLfloat) x, (GLfloat) y, (GLfloat) z);
845 }
846
847
848 void GLAPIENTRY
_mesa_LoadTransposeMatrixf(const GLfloat * m)849 _mesa_LoadTransposeMatrixf( const GLfloat *m )
850 {
851 GLfloat tm[16];
852 if (!m) return;
853 _math_transposef(tm, m);
854 _mesa_LoadMatrixf(tm);
855 }
856
857 void GLAPIENTRY
_mesa_MatrixLoadTransposefEXT(GLenum matrixMode,const GLfloat * m)858 _mesa_MatrixLoadTransposefEXT( GLenum matrixMode, const GLfloat *m )
859 {
860 GLfloat tm[16];
861 if (!m) return;
862 _math_transposef(tm, m);
863 _mesa_MatrixLoadfEXT(matrixMode, tm);
864 }
865
866 void GLAPIENTRY
_mesa_LoadTransposeMatrixd(const GLdouble * m)867 _mesa_LoadTransposeMatrixd( const GLdouble *m )
868 {
869 GLfloat tm[16];
870 if (!m) return;
871 _math_transposefd(tm, m);
872 _mesa_LoadMatrixf(tm);
873 }
874
875 void GLAPIENTRY
_mesa_MatrixLoadTransposedEXT(GLenum matrixMode,const GLdouble * m)876 _mesa_MatrixLoadTransposedEXT( GLenum matrixMode, const GLdouble *m )
877 {
878 GLfloat tm[16];
879 if (!m) return;
880 _math_transposefd(tm, m);
881 _mesa_MatrixLoadfEXT(matrixMode, tm);
882 }
883
884 void GLAPIENTRY
_mesa_MultTransposeMatrixf(const GLfloat * m)885 _mesa_MultTransposeMatrixf( const GLfloat *m )
886 {
887 GLfloat tm[16];
888 if (!m) return;
889 _math_transposef(tm, m);
890 _mesa_MultMatrixf(tm);
891 }
892
893 void GLAPIENTRY
_mesa_MatrixMultTransposefEXT(GLenum matrixMode,const GLfloat * m)894 _mesa_MatrixMultTransposefEXT( GLenum matrixMode, const GLfloat *m )
895 {
896 GLfloat tm[16];
897 if (!m) return;
898 _math_transposef(tm, m);
899 _mesa_MatrixMultfEXT(matrixMode, tm);
900 }
901
902 void GLAPIENTRY
_mesa_MultTransposeMatrixd(const GLdouble * m)903 _mesa_MultTransposeMatrixd( const GLdouble *m )
904 {
905 GLfloat tm[16];
906 if (!m) return;
907 _math_transposefd(tm, m);
908 _mesa_MultMatrixf(tm);
909 }
910
911 void GLAPIENTRY
_mesa_MatrixMultTransposedEXT(GLenum matrixMode,const GLdouble * m)912 _mesa_MatrixMultTransposedEXT( GLenum matrixMode, const GLdouble *m )
913 {
914 GLfloat tm[16];
915 if (!m) return;
916 _math_transposefd(tm, m);
917 _mesa_MatrixMultfEXT(matrixMode, tm);
918 }
919
920 /**********************************************************************/
921 /** \name State management */
922 /*@{*/
923
924
925 /**
926 * Update the projection matrix stack.
927 *
928 * \param ctx GL context.
929 *
930 * Recomputes user clip positions if necessary.
931 *
932 * \note This routine references __struct gl_contextRec::Tranform attribute
933 * values to compute userclip positions in clip space, but is only called on
934 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
935 * date across changes to the __struct gl_contextRec::Transform attributes.
936 */
937 static void
update_projection(struct gl_context * ctx)938 update_projection( struct gl_context *ctx )
939 {
940 /* Recompute clip plane positions in clipspace. This is also done
941 * in _mesa_ClipPlane().
942 */
943 GLbitfield mask = ctx->Transform.ClipPlanesEnabled;
944
945 if (mask) {
946 /* make sure the inverse is up to date */
947 _math_matrix_analyse(ctx->ProjectionMatrixStack.Top);
948
949 do {
950 const int p = u_bit_scan(&mask);
951
952 _mesa_transform_vector(ctx->Transform._ClipUserPlane[p],
953 ctx->Transform.EyeUserPlane[p],
954 ctx->ProjectionMatrixStack.Top->inv);
955 } while (mask);
956 }
957 }
958
959
960 /**
961 * Updates the combined modelview-projection matrix.
962 *
963 * \param ctx GL context.
964 * \param new_state new state bit mask.
965 *
966 * If there is a new model view matrix then analyzes it. If there is a new
967 * projection matrix, updates it. Finally calls
968 * calculate_model_project_matrix() to recalculate the modelview-projection
969 * matrix.
970 */
_mesa_update_modelview_project(struct gl_context * ctx,GLuint new_state)971 void _mesa_update_modelview_project( struct gl_context *ctx, GLuint new_state )
972 {
973 if (new_state & _NEW_MODELVIEW)
974 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
975
976 if (new_state & _NEW_PROJECTION)
977 update_projection( ctx );
978
979 /* Calculate ModelViewMatrix * ProjectionMatrix. */
980 _math_matrix_mul_matrix(&ctx->_ModelProjectMatrix,
981 ctx->ProjectionMatrixStack.Top,
982 ctx->ModelviewMatrixStack.Top);
983 }
984
985 /*@}*/
986
987
988 /**********************************************************************/
989 /** Matrix stack initialization */
990 /*@{*/
991
992
993 /**
994 * Initialize a matrix stack.
995 *
996 * \param stack matrix stack.
997 * \param maxDepth maximum stack depth.
998 * \param dirtyFlag dirty flag.
999 *
1000 * Allocates an array of \p maxDepth elements for the matrix stack and calls
1001 * _math_matrix_ctr() for each element to initialize it.
1002 */
1003 static void
init_matrix_stack(struct gl_matrix_stack * stack,GLuint maxDepth,GLuint dirtyFlag)1004 init_matrix_stack(struct gl_matrix_stack *stack,
1005 GLuint maxDepth, GLuint dirtyFlag)
1006 {
1007 stack->Depth = 0;
1008 stack->MaxDepth = maxDepth;
1009 stack->DirtyFlag = dirtyFlag;
1010 /* The stack will be dynamically resized at glPushMatrix() time */
1011 stack->Stack = calloc(1, sizeof(GLmatrix));
1012 stack->StackSize = 1;
1013 _math_matrix_ctr(&stack->Stack[0]);
1014 stack->Top = stack->Stack;
1015 stack->ChangedSincePush = false;
1016 }
1017
1018 /**
1019 * Free matrix stack.
1020 *
1021 * \param stack matrix stack.
1022 */
1023 static void
free_matrix_stack(struct gl_matrix_stack * stack)1024 free_matrix_stack( struct gl_matrix_stack *stack )
1025 {
1026 free(stack->Stack);
1027 stack->Stack = stack->Top = NULL;
1028 stack->StackSize = 0;
1029 }
1030
1031 /*@}*/
1032
1033
1034 /**********************************************************************/
1035 /** \name Initialization */
1036 /*@{*/
1037
1038
1039 /**
1040 * Initialize the context matrix data.
1041 *
1042 * \param ctx GL context.
1043 *
1044 * Initializes each of the matrix stacks and the combined modelview-projection
1045 * matrix.
1046 */
_mesa_init_matrix(struct gl_context * ctx)1047 void _mesa_init_matrix( struct gl_context * ctx )
1048 {
1049 GLuint i;
1050
1051 /* Initialize matrix stacks */
1052 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
1053 _NEW_MODELVIEW);
1054 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
1055 _NEW_PROJECTION);
1056 for (i = 0; i < ARRAY_SIZE(ctx->TextureMatrixStack); i++)
1057 init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
1058 _NEW_TEXTURE_MATRIX);
1059 for (i = 0; i < ARRAY_SIZE(ctx->ProgramMatrixStack); i++)
1060 init_matrix_stack(&ctx->ProgramMatrixStack[i],
1061 MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
1062 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
1063
1064 /* Init combined Modelview*Projection matrix */
1065 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
1066 }
1067
1068
1069 /**
1070 * Free the context matrix data.
1071 *
1072 * \param ctx GL context.
1073 *
1074 * Frees each of the matrix stacks.
1075 */
_mesa_free_matrix_data(struct gl_context * ctx)1076 void _mesa_free_matrix_data( struct gl_context *ctx )
1077 {
1078 GLuint i;
1079
1080 free_matrix_stack(&ctx->ModelviewMatrixStack);
1081 free_matrix_stack(&ctx->ProjectionMatrixStack);
1082 for (i = 0; i < ARRAY_SIZE(ctx->TextureMatrixStack); i++)
1083 free_matrix_stack(&ctx->TextureMatrixStack[i]);
1084 for (i = 0; i < ARRAY_SIZE(ctx->ProgramMatrixStack); i++)
1085 free_matrix_stack(&ctx->ProgramMatrixStack[i]);
1086
1087 }
1088
1089
1090 /**
1091 * Initialize the context transform attribute group.
1092 *
1093 * \param ctx GL context.
1094 *
1095 * \todo Move this to a new file with other 'transform' routines.
1096 */
_mesa_init_transform(struct gl_context * ctx)1097 void _mesa_init_transform( struct gl_context *ctx )
1098 {
1099 GLuint i;
1100
1101 /* Transformation group */
1102 ctx->Transform.MatrixMode = GL_MODELVIEW;
1103 ctx->Transform.Normalize = GL_FALSE;
1104 ctx->Transform.RescaleNormals = GL_FALSE;
1105 ctx->Transform.RasterPositionUnclipped = GL_FALSE;
1106 for (i=0;i<ctx->Const.MaxClipPlanes;i++) {
1107 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
1108 }
1109 ctx->Transform.ClipPlanesEnabled = 0;
1110 }
1111
1112
1113 /*@}*/
1114