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