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 =
315 os_realloc_aligned(stack->Stack, stack->StackSize * sizeof(GLmatrix),
316 new_stack_size * sizeof(GLmatrix), 16);
317
318 if (!new_stack) {
319 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
320 return;
321 }
322
323 for (i = stack->StackSize; i < new_stack_size; i++)
324 _math_matrix_ctr(&new_stack[i]);
325
326 stack->Stack = new_stack;
327 stack->StackSize = new_stack_size;
328 }
329
330 _math_matrix_push_copy(&stack->Stack[stack->Depth + 1],
331 &stack->Stack[stack->Depth]);
332 stack->Depth++;
333 stack->Top = &(stack->Stack[stack->Depth]);
334 stack->ChangedSincePush = false;
335 }
336
337
338 /**
339 * Push the current matrix stack.
340 *
341 * \sa glPushMatrix().
342 *
343 * Verifies the current matrix stack is not full, and duplicates the top-most
344 * matrix in the stack.
345 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
346 */
347 void GLAPIENTRY
_mesa_PushMatrix(void)348 _mesa_PushMatrix( void )
349 {
350 GET_CURRENT_CONTEXT(ctx);
351 struct gl_matrix_stack *stack = ctx->CurrentStack;
352
353 if (MESA_VERBOSE&VERBOSE_API)
354 _mesa_debug(ctx, "glPushMatrix %s\n",
355 _mesa_enum_to_string(ctx->Transform.MatrixMode));
356
357 push_matrix(ctx, stack, ctx->Transform.MatrixMode, "glPushMatrix");
358 }
359
360
361 void GLAPIENTRY
_mesa_MatrixPushEXT(GLenum matrixMode)362 _mesa_MatrixPushEXT( GLenum matrixMode )
363 {
364 GET_CURRENT_CONTEXT(ctx);
365 struct gl_matrix_stack *stack = get_named_matrix_stack(ctx, matrixMode,
366 "glMatrixPushEXT");
367 ASSERT_OUTSIDE_BEGIN_END(ctx);
368 if (stack)
369 push_matrix(ctx, stack, matrixMode, "glMatrixPushEXT");
370 }
371
372
373 static GLboolean
pop_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack)374 pop_matrix( struct gl_context *ctx, struct gl_matrix_stack *stack )
375 {
376 if (stack->Depth == 0)
377 return GL_FALSE;
378
379 stack->Depth--;
380
381 /* If the popped matrix is the same as the current one, treat it as
382 * a no-op change.
383 */
384 if (stack->ChangedSincePush &&
385 memcmp(stack->Top, &stack->Stack[stack->Depth],
386 sizeof(GLmatrix))) {
387 FLUSH_VERTICES(ctx, 0, 0);
388 ctx->NewState |= stack->DirtyFlag;
389 }
390
391 stack->Top = &(stack->Stack[stack->Depth]);
392 stack->ChangedSincePush = true;
393 return GL_TRUE;
394 }
395
396
397 /**
398 * Pop the current matrix stack.
399 *
400 * \sa glPopMatrix().
401 *
402 * Flushes the vertices, verifies the current matrix stack is not empty, and
403 * moves the stack head down.
404 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
405 */
406 void GLAPIENTRY
_mesa_PopMatrix(void)407 _mesa_PopMatrix( void )
408 {
409 GET_CURRENT_CONTEXT(ctx);
410 struct gl_matrix_stack *stack = ctx->CurrentStack;
411
412 if (MESA_VERBOSE&VERBOSE_API)
413 _mesa_debug(ctx, "glPopMatrix %s\n",
414 _mesa_enum_to_string(ctx->Transform.MatrixMode));
415
416 if (!pop_matrix(ctx, stack)) {
417 if (ctx->Transform.MatrixMode == GL_TEXTURE) {
418 _mesa_error(ctx, GL_STACK_UNDERFLOW,
419 "glPopMatrix(mode=GL_TEXTURE, unit=%d)",
420 ctx->Texture.CurrentUnit);
421 }
422 else {
423 _mesa_error(ctx, GL_STACK_UNDERFLOW, "glPopMatrix(mode=%s)",
424 _mesa_enum_to_string(ctx->Transform.MatrixMode));
425 }
426 }
427 }
428
429
430 void GLAPIENTRY
_mesa_MatrixPopEXT(GLenum matrixMode)431 _mesa_MatrixPopEXT( GLenum matrixMode )
432 {
433 GET_CURRENT_CONTEXT(ctx);
434 struct gl_matrix_stack *stack = get_named_matrix_stack(ctx, matrixMode,
435 "glMatrixPopEXT");
436 if (!stack)
437 return;
438
439 if (!pop_matrix(ctx, stack)) {
440 if (matrixMode == GL_TEXTURE) {
441 _mesa_error(ctx, GL_STACK_UNDERFLOW,
442 "glMatrixPopEXT(mode=GL_TEXTURE, unit=%d)",
443 ctx->Texture.CurrentUnit);
444 }
445 else {
446 _mesa_error(ctx, GL_STACK_UNDERFLOW, "glMatrixPopEXT(mode=%s)",
447 _mesa_enum_to_string(matrixMode));
448 }
449 }
450 }
451
452
453 void
_mesa_load_identity_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack)454 _mesa_load_identity_matrix(struct gl_context *ctx, struct gl_matrix_stack *stack)
455 {
456 FLUSH_VERTICES(ctx, 0, 0);
457
458 _math_matrix_set_identity(stack->Top);
459 stack->ChangedSincePush = true;
460 ctx->NewState |= stack->DirtyFlag;
461 }
462
463
464 /**
465 * Replace the current matrix with the identity matrix.
466 *
467 * \sa glLoadIdentity().
468 *
469 * Flushes the vertices and calls _math_matrix_set_identity() with the
470 * top-most matrix in the current stack.
471 * Marks __struct gl_contextRec::NewState with the stack dirty flag.
472 */
473 void GLAPIENTRY
_mesa_LoadIdentity(void)474 _mesa_LoadIdentity( void )
475 {
476 GET_CURRENT_CONTEXT(ctx);
477
478 if (MESA_VERBOSE & VERBOSE_API)
479 _mesa_debug(ctx, "glLoadIdentity()\n");
480
481 _mesa_load_identity_matrix(ctx, ctx->CurrentStack);
482 }
483
484
485 void GLAPIENTRY
_mesa_MatrixLoadIdentityEXT(GLenum matrixMode)486 _mesa_MatrixLoadIdentityEXT( GLenum matrixMode )
487 {
488 struct gl_matrix_stack *stack;
489 GET_CURRENT_CONTEXT(ctx);
490 stack = get_named_matrix_stack(ctx, matrixMode, "glMatrixLoadIdentityEXT");
491 if (!stack)
492 return;
493
494 _mesa_load_identity_matrix(ctx, stack);
495 }
496
497
498 void
_mesa_load_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack,const GLfloat * m)499 _mesa_load_matrix(struct gl_context *ctx, struct gl_matrix_stack *stack,
500 const GLfloat *m)
501 {
502 if (memcmp(m, stack->Top->m, 16 * sizeof(GLfloat)) != 0) {
503 FLUSH_VERTICES(ctx, 0, 0);
504 _math_matrix_loadf(stack->Top, m);
505 stack->ChangedSincePush = true;
506 ctx->NewState |= stack->DirtyFlag;
507 }
508 }
509
510
511 static void
matrix_load(struct gl_context * ctx,struct gl_matrix_stack * stack,const GLfloat * m,const char * caller)512 matrix_load(struct gl_context *ctx, struct gl_matrix_stack *stack,
513 const GLfloat *m, const char* caller)
514 {
515 if (!m) return;
516 if (MESA_VERBOSE & VERBOSE_API)
517 _mesa_debug(ctx,
518 "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
519 caller,
520 m[0], m[4], m[8], m[12],
521 m[1], m[5], m[9], m[13],
522 m[2], m[6], m[10], m[14],
523 m[3], m[7], m[11], m[15]);
524
525 _mesa_load_matrix(ctx, stack, m);
526 }
527
528
529 /**
530 * Replace the current matrix with a given matrix.
531 *
532 * \param m matrix.
533 *
534 * \sa glLoadMatrixf().
535 *
536 * Flushes the vertices and calls _math_matrix_loadf() with the top-most
537 * matrix in the current stack and the given matrix.
538 * Marks __struct gl_contextRec::NewState with the dirty stack flag.
539 */
540 void GLAPIENTRY
_mesa_LoadMatrixf(const GLfloat * m)541 _mesa_LoadMatrixf( const GLfloat *m )
542 {
543 GET_CURRENT_CONTEXT(ctx);
544 matrix_load(ctx, ctx->CurrentStack, m, "glLoadMatrix");
545 }
546
547
548 /**
549 * Replace the named matrix with a given matrix.
550 *
551 * \param matrixMode matrix to replace
552 * \param m matrix
553 *
554 * \sa glLoadMatrixf().
555 */
556 void GLAPIENTRY
_mesa_MatrixLoadfEXT(GLenum matrixMode,const GLfloat * m)557 _mesa_MatrixLoadfEXT( GLenum matrixMode, const GLfloat *m )
558 {
559 GET_CURRENT_CONTEXT(ctx);
560 struct gl_matrix_stack * stack =
561 get_named_matrix_stack(ctx, matrixMode, "glMatrixLoadfEXT");
562 if (!stack)
563 return;
564
565 matrix_load(ctx, stack, m, "glMatrixLoadfEXT");
566 }
567
568
569 static void
matrix_mult(struct gl_matrix_stack * stack,const GLfloat * m,const char * caller)570 matrix_mult(struct gl_matrix_stack *stack, const GLfloat *m, const char* caller)
571 {
572 GET_CURRENT_CONTEXT(ctx);
573
574 /* glthread filters out identity matrices, so don't do it again. */
575 if (!m || (!ctx->GLThread.enabled && _mesa_matrix_is_identity(m)))
576 return;
577
578 if (MESA_VERBOSE & VERBOSE_API)
579 _mesa_debug(ctx,
580 "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
581 caller,
582 m[0], m[4], m[8], m[12],
583 m[1], m[5], m[9], m[13],
584 m[2], m[6], m[10], m[14],
585 m[3], m[7], m[11], m[15]);
586
587 FLUSH_VERTICES(ctx, 0, 0);
588 _math_matrix_mul_floats(stack->Top, m);
589 stack->ChangedSincePush = true;
590 ctx->NewState |= stack->DirtyFlag;
591 }
592
593
594 /**
595 * Multiply the current matrix with a given matrix.
596 *
597 * \param m matrix.
598 *
599 * \sa glMultMatrixf().
600 *
601 * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
602 * matrix in the current stack and the given matrix. Marks
603 * __struct gl_contextRec::NewState with the dirty stack flag.
604 */
605 void GLAPIENTRY
_mesa_MultMatrixf(const GLfloat * m)606 _mesa_MultMatrixf( const GLfloat *m )
607 {
608 GET_CURRENT_CONTEXT(ctx);
609 matrix_mult(ctx->CurrentStack, m, "glMultMatrix");
610 }
611
612
613 void GLAPIENTRY
_mesa_MatrixMultfEXT(GLenum matrixMode,const GLfloat * m)614 _mesa_MatrixMultfEXT( GLenum matrixMode, const GLfloat *m )
615 {
616 GET_CURRENT_CONTEXT(ctx);
617 struct gl_matrix_stack * stack =
618 get_named_matrix_stack(ctx, matrixMode, "glMatrixMultfEXT");
619 if (!stack)
620 return;
621
622 matrix_mult(stack, m, "glMultMatrix");
623 }
624
625
626 static void
matrix_rotate(struct gl_matrix_stack * stack,GLfloat angle,GLfloat x,GLfloat y,GLfloat z,const char * caller)627 matrix_rotate(struct gl_matrix_stack *stack, GLfloat angle,
628 GLfloat x, GLfloat y, GLfloat z, const char* caller)
629 {
630 GET_CURRENT_CONTEXT(ctx);
631
632 FLUSH_VERTICES(ctx, 0, 0);
633 if (angle != 0.0F) {
634 _math_matrix_rotate(stack->Top, angle, x, y, z);
635 stack->ChangedSincePush = true;
636 ctx->NewState |=stack->DirtyFlag;
637 }
638 }
639
640
641 /**
642 * Multiply the current matrix with a rotation matrix.
643 *
644 * \param angle angle of rotation, in degrees.
645 * \param x rotation vector x coordinate.
646 * \param y rotation vector y coordinate.
647 * \param z rotation vector z coordinate.
648 *
649 * \sa glRotatef().
650 *
651 * Flushes the vertices and calls _math_matrix_rotate() with the top-most
652 * matrix in the current stack and the given parameters. Marks
653 * __struct gl_contextRec::NewState with the dirty stack flag.
654 */
655 void GLAPIENTRY
_mesa_Rotatef(GLfloat angle,GLfloat x,GLfloat y,GLfloat z)656 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
657 {
658 GET_CURRENT_CONTEXT(ctx);
659 matrix_rotate(ctx->CurrentStack, angle, x, y, z, "glRotatef");
660 }
661
662
663 void GLAPIENTRY
_mesa_MatrixRotatefEXT(GLenum matrixMode,GLfloat angle,GLfloat x,GLfloat y,GLfloat z)664 _mesa_MatrixRotatefEXT( GLenum matrixMode, GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
665 {
666 GET_CURRENT_CONTEXT(ctx);
667 struct gl_matrix_stack *stack =
668 get_named_matrix_stack(ctx, matrixMode, "glMatrixRotatefEXT");
669 if (!stack)
670 return;
671
672 matrix_rotate(stack, angle, x, y, z, "glMatrixRotatefEXT");
673 }
674
675
676 /**
677 * Multiply the current matrix with a general scaling matrix.
678 *
679 * \param x x axis scale factor.
680 * \param y y axis scale factor.
681 * \param z z axis scale factor.
682 *
683 * \sa glScalef().
684 *
685 * Flushes the vertices and calls _math_matrix_scale() with the top-most
686 * matrix in the current stack and the given parameters. Marks
687 * __struct gl_contextRec::NewState with the dirty stack flag.
688 */
689 void GLAPIENTRY
_mesa_Scalef(GLfloat x,GLfloat y,GLfloat z)690 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
691 {
692 GET_CURRENT_CONTEXT(ctx);
693
694 FLUSH_VERTICES(ctx, 0, 0);
695 _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
696 ctx->CurrentStack->ChangedSincePush = true;
697 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
698 }
699
700
701 void GLAPIENTRY
_mesa_MatrixScalefEXT(GLenum matrixMode,GLfloat x,GLfloat y,GLfloat z)702 _mesa_MatrixScalefEXT( GLenum matrixMode, GLfloat x, GLfloat y, GLfloat z )
703 {
704 struct gl_matrix_stack *stack;
705 GET_CURRENT_CONTEXT(ctx);
706
707 stack = get_named_matrix_stack(ctx, matrixMode, "glMatrixScalefEXT");
708 if (!stack)
709 return;
710
711 FLUSH_VERTICES(ctx, 0, 0);
712 _math_matrix_scale(stack->Top, x, y, z);
713 stack->ChangedSincePush = true;
714 ctx->NewState |= stack->DirtyFlag;
715 }
716
717
718 /**
719 * Multiply the current matrix with a translation matrix.
720 *
721 * \param x translation vector x coordinate.
722 * \param y translation vector y coordinate.
723 * \param z translation vector z coordinate.
724 *
725 * \sa glTranslatef().
726 *
727 * Flushes the vertices and calls _math_matrix_translate() with the top-most
728 * matrix in the current stack and the given parameters. Marks
729 * __struct gl_contextRec::NewState with the dirty stack flag.
730 */
731 void GLAPIENTRY
_mesa_Translatef(GLfloat x,GLfloat y,GLfloat z)732 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
733 {
734 GET_CURRENT_CONTEXT(ctx);
735
736 FLUSH_VERTICES(ctx, 0, 0);
737 _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
738 ctx->CurrentStack->ChangedSincePush = true;
739 ctx->NewState |= ctx->CurrentStack->DirtyFlag;
740 }
741
742
743 void GLAPIENTRY
_mesa_MatrixTranslatefEXT(GLenum matrixMode,GLfloat x,GLfloat y,GLfloat z)744 _mesa_MatrixTranslatefEXT( GLenum matrixMode, GLfloat x, GLfloat y, GLfloat z )
745 {
746 GET_CURRENT_CONTEXT(ctx);
747 struct gl_matrix_stack *stack =
748 get_named_matrix_stack(ctx, matrixMode, "glMatrixTranslatefEXT");
749 if (!stack)
750 return;
751
752 FLUSH_VERTICES(ctx, 0, 0);
753 _math_matrix_translate(stack->Top, x, y, z);
754 stack->ChangedSincePush = true;
755 ctx->NewState |= stack->DirtyFlag;
756 }
757
758
759 void GLAPIENTRY
_mesa_LoadMatrixd(const GLdouble * m)760 _mesa_LoadMatrixd( const GLdouble *m )
761 {
762 GLint i;
763 GLfloat f[16];
764 if (!m) return;
765 for (i = 0; i < 16; i++)
766 f[i] = (GLfloat) m[i];
767 _mesa_LoadMatrixf(f);
768 }
769
770
771 void GLAPIENTRY
_mesa_MatrixLoaddEXT(GLenum matrixMode,const GLdouble * m)772 _mesa_MatrixLoaddEXT( GLenum matrixMode, const GLdouble *m )
773 {
774 GLfloat f[16];
775 if (!m) return;
776 for (unsigned i = 0; i < 16; i++)
777 f[i] = (GLfloat) m[i];
778 _mesa_MatrixLoadfEXT(matrixMode, f);
779 }
780
781
782 void GLAPIENTRY
_mesa_MultMatrixd(const GLdouble * m)783 _mesa_MultMatrixd( const GLdouble *m )
784 {
785 GLint i;
786 GLfloat f[16];
787 if (!m) return;
788 for (i = 0; i < 16; i++)
789 f[i] = (GLfloat) m[i];
790 _mesa_MultMatrixf( f );
791 }
792
793
794 void GLAPIENTRY
_mesa_MatrixMultdEXT(GLenum matrixMode,const GLdouble * m)795 _mesa_MatrixMultdEXT( GLenum matrixMode, const GLdouble *m )
796 {
797 GLfloat f[16];
798 if (!m) return;
799 for (unsigned i = 0; i < 16; i++)
800 f[i] = (GLfloat) m[i];
801 _mesa_MatrixMultfEXT(matrixMode, f);
802 }
803
804
805 void GLAPIENTRY
_mesa_Rotated(GLdouble angle,GLdouble x,GLdouble y,GLdouble z)806 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
807 {
808 _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
809 }
810
811
812 void GLAPIENTRY
_mesa_MatrixRotatedEXT(GLenum matrixMode,GLdouble angle,GLdouble x,GLdouble y,GLdouble z)813 _mesa_MatrixRotatedEXT( GLenum matrixMode, GLdouble angle,
814 GLdouble x, GLdouble y, GLdouble z )
815 {
816 _mesa_MatrixRotatefEXT(matrixMode, (GLfloat) angle,
817 (GLfloat) x, (GLfloat) y, (GLfloat) z);
818 }
819
820
821 void GLAPIENTRY
_mesa_Scaled(GLdouble x,GLdouble y,GLdouble z)822 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
823 {
824 _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
825 }
826
827
828 void GLAPIENTRY
_mesa_MatrixScaledEXT(GLenum matrixMode,GLdouble x,GLdouble y,GLdouble z)829 _mesa_MatrixScaledEXT( GLenum matrixMode, GLdouble x, GLdouble y, GLdouble z )
830 {
831 _mesa_MatrixScalefEXT(matrixMode, (GLfloat) x, (GLfloat) y, (GLfloat) z);
832 }
833
834
835 void GLAPIENTRY
_mesa_Translated(GLdouble x,GLdouble y,GLdouble z)836 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
837 {
838 _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
839 }
840
841
842 void GLAPIENTRY
_mesa_MatrixTranslatedEXT(GLenum matrixMode,GLdouble x,GLdouble y,GLdouble z)843 _mesa_MatrixTranslatedEXT( GLenum matrixMode, GLdouble x, GLdouble y, GLdouble z )
844 {
845 _mesa_MatrixTranslatefEXT(matrixMode, (GLfloat) x, (GLfloat) y, (GLfloat) z);
846 }
847
848
849 void GLAPIENTRY
_mesa_LoadTransposeMatrixf(const GLfloat * m)850 _mesa_LoadTransposeMatrixf( const GLfloat *m )
851 {
852 GLfloat tm[16];
853 if (!m) return;
854 _math_transposef(tm, m);
855 _mesa_LoadMatrixf(tm);
856 }
857
858 void GLAPIENTRY
_mesa_MatrixLoadTransposefEXT(GLenum matrixMode,const GLfloat * m)859 _mesa_MatrixLoadTransposefEXT( GLenum matrixMode, const GLfloat *m )
860 {
861 GLfloat tm[16];
862 if (!m) return;
863 _math_transposef(tm, m);
864 _mesa_MatrixLoadfEXT(matrixMode, tm);
865 }
866
867 void GLAPIENTRY
_mesa_LoadTransposeMatrixd(const GLdouble * m)868 _mesa_LoadTransposeMatrixd( const GLdouble *m )
869 {
870 GLfloat tm[16];
871 if (!m) return;
872 _math_transposefd(tm, m);
873 _mesa_LoadMatrixf(tm);
874 }
875
876 void GLAPIENTRY
_mesa_MatrixLoadTransposedEXT(GLenum matrixMode,const GLdouble * m)877 _mesa_MatrixLoadTransposedEXT( GLenum matrixMode, const GLdouble *m )
878 {
879 GLfloat tm[16];
880 if (!m) return;
881 _math_transposefd(tm, m);
882 _mesa_MatrixLoadfEXT(matrixMode, tm);
883 }
884
885 void GLAPIENTRY
_mesa_MultTransposeMatrixf(const GLfloat * m)886 _mesa_MultTransposeMatrixf( const GLfloat *m )
887 {
888 GLfloat tm[16];
889 if (!m) return;
890 _math_transposef(tm, m);
891 _mesa_MultMatrixf(tm);
892 }
893
894 void GLAPIENTRY
_mesa_MatrixMultTransposefEXT(GLenum matrixMode,const GLfloat * m)895 _mesa_MatrixMultTransposefEXT( GLenum matrixMode, const GLfloat *m )
896 {
897 GLfloat tm[16];
898 if (!m) return;
899 _math_transposef(tm, m);
900 _mesa_MatrixMultfEXT(matrixMode, tm);
901 }
902
903 void GLAPIENTRY
_mesa_MultTransposeMatrixd(const GLdouble * m)904 _mesa_MultTransposeMatrixd( const GLdouble *m )
905 {
906 GLfloat tm[16];
907 if (!m) return;
908 _math_transposefd(tm, m);
909 _mesa_MultMatrixf(tm);
910 }
911
912 void GLAPIENTRY
_mesa_MatrixMultTransposedEXT(GLenum matrixMode,const GLdouble * m)913 _mesa_MatrixMultTransposedEXT( GLenum matrixMode, const GLdouble *m )
914 {
915 GLfloat tm[16];
916 if (!m) return;
917 _math_transposefd(tm, m);
918 _mesa_MatrixMultfEXT(matrixMode, tm);
919 }
920
921 /**********************************************************************/
922 /** \name State management */
923 /*@{*/
924
925
926 /**
927 * Update the projection matrix stack.
928 *
929 * \param ctx GL context.
930 *
931 * Recomputes user clip positions if necessary.
932 *
933 * \note This routine references __struct gl_contextRec::Tranform attribute
934 * values to compute userclip positions in clip space, but is only called on
935 * _NEW_PROJECTION. The _mesa_ClipPlane() function keeps these values up to
936 * date across changes to the __struct gl_contextRec::Transform attributes.
937 */
938 static void
update_projection(struct gl_context * ctx)939 update_projection( struct gl_context *ctx )
940 {
941 /* Recompute clip plane positions in clipspace. This is also done
942 * in _mesa_ClipPlane().
943 */
944 GLbitfield mask = ctx->Transform.ClipPlanesEnabled;
945
946 if (mask) {
947 /* make sure the inverse is up to date */
948 _math_matrix_analyse(ctx->ProjectionMatrixStack.Top);
949
950 do {
951 const int p = u_bit_scan(&mask);
952
953 _mesa_transform_vector(ctx->Transform._ClipUserPlane[p],
954 ctx->Transform.EyeUserPlane[p],
955 ctx->ProjectionMatrixStack.Top->inv);
956 } while (mask);
957 }
958 }
959
960
961 /**
962 * Updates the combined modelview-projection matrix.
963 *
964 * \param ctx GL context.
965 * \param new_state new state bit mask.
966 *
967 * If there is a new model view matrix then analyzes it. If there is a new
968 * projection matrix, updates it. Finally calls
969 * calculate_model_project_matrix() to recalculate the modelview-projection
970 * matrix.
971 */
_mesa_update_modelview_project(struct gl_context * ctx,GLuint new_state)972 void _mesa_update_modelview_project( struct gl_context *ctx, GLuint new_state )
973 {
974 if (new_state & _NEW_MODELVIEW)
975 _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
976
977 if (new_state & _NEW_PROJECTION)
978 update_projection( ctx );
979
980 /* Calculate ModelViewMatrix * ProjectionMatrix. */
981 _math_matrix_mul_matrix(&ctx->_ModelProjectMatrix,
982 ctx->ProjectionMatrixStack.Top,
983 ctx->ModelviewMatrixStack.Top);
984 }
985
986 /*@}*/
987
988
989 /**********************************************************************/
990 /** Matrix stack initialization */
991 /*@{*/
992
993
994 /**
995 * Initialize a matrix stack.
996 *
997 * \param stack matrix stack.
998 * \param maxDepth maximum stack depth.
999 * \param dirtyFlag dirty flag.
1000 *
1001 * Allocates an array of \p maxDepth elements for the matrix stack and calls
1002 * _math_matrix_ctr() for each element to initialize it.
1003 */
1004 static void
init_matrix_stack(struct gl_matrix_stack * stack,GLuint maxDepth,GLuint dirtyFlag)1005 init_matrix_stack(struct gl_matrix_stack *stack,
1006 GLuint maxDepth, GLuint dirtyFlag)
1007 {
1008 stack->Depth = 0;
1009 stack->MaxDepth = maxDepth;
1010 stack->DirtyFlag = dirtyFlag;
1011 /* The stack will be dynamically resized at glPushMatrix() time */
1012 stack->Stack = os_malloc_aligned(sizeof(GLmatrix), 16);
1013 stack->StackSize = 1;
1014 _math_matrix_ctr(&stack->Stack[0]);
1015 stack->Top = stack->Stack;
1016 stack->ChangedSincePush = false;
1017 }
1018
1019 /**
1020 * Free matrix stack.
1021 *
1022 * \param stack matrix stack.
1023 */
1024 static void
free_matrix_stack(struct gl_matrix_stack * stack)1025 free_matrix_stack( struct gl_matrix_stack *stack )
1026 {
1027 os_free_aligned(stack->Stack);
1028 stack->Stack = stack->Top = NULL;
1029 stack->StackSize = 0;
1030 }
1031
1032 /*@}*/
1033
1034
1035 /**********************************************************************/
1036 /** \name Initialization */
1037 /*@{*/
1038
1039
1040 /**
1041 * Initialize the context matrix data.
1042 *
1043 * \param ctx GL context.
1044 *
1045 * Initializes each of the matrix stacks and the combined modelview-projection
1046 * matrix.
1047 */
_mesa_init_matrix(struct gl_context * ctx)1048 void _mesa_init_matrix( struct gl_context * ctx )
1049 {
1050 GLuint i;
1051
1052 /* Initialize matrix stacks */
1053 init_matrix_stack(&ctx->ModelviewMatrixStack, MAX_MODELVIEW_STACK_DEPTH,
1054 _NEW_MODELVIEW);
1055 init_matrix_stack(&ctx->ProjectionMatrixStack, MAX_PROJECTION_STACK_DEPTH,
1056 _NEW_PROJECTION);
1057 for (i = 0; i < ARRAY_SIZE(ctx->TextureMatrixStack); i++)
1058 init_matrix_stack(&ctx->TextureMatrixStack[i], MAX_TEXTURE_STACK_DEPTH,
1059 _NEW_TEXTURE_MATRIX);
1060 for (i = 0; i < ARRAY_SIZE(ctx->ProgramMatrixStack); i++)
1061 init_matrix_stack(&ctx->ProgramMatrixStack[i],
1062 MAX_PROGRAM_MATRIX_STACK_DEPTH, _NEW_TRACK_MATRIX);
1063 ctx->CurrentStack = &ctx->ModelviewMatrixStack;
1064
1065 /* Init combined Modelview*Projection matrix */
1066 _math_matrix_ctr( &ctx->_ModelProjectMatrix );
1067 }
1068
1069
1070 /**
1071 * Free the context matrix data.
1072 *
1073 * \param ctx GL context.
1074 *
1075 * Frees each of the matrix stacks.
1076 */
_mesa_free_matrix_data(struct gl_context * ctx)1077 void _mesa_free_matrix_data( struct gl_context *ctx )
1078 {
1079 GLuint i;
1080
1081 free_matrix_stack(&ctx->ModelviewMatrixStack);
1082 free_matrix_stack(&ctx->ProjectionMatrixStack);
1083 for (i = 0; i < ARRAY_SIZE(ctx->TextureMatrixStack); i++)
1084 free_matrix_stack(&ctx->TextureMatrixStack[i]);
1085 for (i = 0; i < ARRAY_SIZE(ctx->ProgramMatrixStack); i++)
1086 free_matrix_stack(&ctx->ProgramMatrixStack[i]);
1087
1088 }
1089
1090
1091 /**
1092 * Initialize the context transform attribute group.
1093 *
1094 * \param ctx GL context.
1095 *
1096 * \todo Move this to a new file with other 'transform' routines.
1097 */
_mesa_init_transform(struct gl_context * ctx)1098 void _mesa_init_transform( struct gl_context *ctx )
1099 {
1100 GLuint i;
1101
1102 /* Transformation group */
1103 ctx->Transform.MatrixMode = GL_MODELVIEW;
1104 ctx->Transform.Normalize = GL_FALSE;
1105 ctx->Transform.RescaleNormals = GL_FALSE;
1106 ctx->Transform.RasterPositionUnclipped = GL_FALSE;
1107 for (i=0;i<ctx->Const.MaxClipPlanes;i++) {
1108 ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
1109 }
1110 ctx->Transform.ClipPlanesEnabled = 0;
1111 }
1112
1113
1114 /*@}*/
1115