• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 *
get_named_matrix_stack(struct gl_context * ctx,GLenum mode,const char * caller)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 
matrix_frustum(struct gl_matrix_stack * stack,GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval,const char * caller)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
_mesa_Frustum(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval)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
_mesa_MatrixFrustumEXT(GLenum matrixMode,GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval)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
matrix_ortho(struct gl_matrix_stack * stack,GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval,const char * caller)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
_mesa_Ortho(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval)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
_mesa_MatrixOrthoEXT(GLenum matrixMode,GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble nearval,GLdouble farval)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
_mesa_MatrixMode(GLenum mode)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
push_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack,GLenum matrixMode,const char * func)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
_mesa_PushMatrix(void)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
_mesa_MatrixPushEXT(GLenum matrixMode)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
pop_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack)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
_mesa_PopMatrix(void)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
_mesa_MatrixPopEXT(GLenum matrixMode)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 void
_mesa_load_identity_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack)440 _mesa_load_identity_matrix(struct gl_context *ctx, struct gl_matrix_stack *stack)
441 {
442    FLUSH_VERTICES(ctx, 0);
443 
444    _math_matrix_set_identity(stack->Top);
445    ctx->NewState |= stack->DirtyFlag;
446 }
447 
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
_mesa_LoadIdentity(void)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    _mesa_load_identity_matrix(ctx, ctx->CurrentStack);
467 }
468 
469 
470 void GLAPIENTRY
_mesa_MatrixLoadIdentityEXT(GLenum matrixMode)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    _mesa_load_identity_matrix(ctx, stack);
480 }
481 
482 
483 void
_mesa_load_matrix(struct gl_context * ctx,struct gl_matrix_stack * stack,const GLfloat * m)484 _mesa_load_matrix(struct gl_context *ctx, struct gl_matrix_stack *stack,
485                   const GLfloat *m)
486 {
487    if (memcmp(m, stack->Top->m, 16 * sizeof(GLfloat)) != 0) {
488       FLUSH_VERTICES(ctx, 0);
489       _math_matrix_loadf(stack->Top, m);
490       ctx->NewState |= stack->DirtyFlag;
491    }
492 }
493 
494 
495 static void
matrix_load(struct gl_context * ctx,struct gl_matrix_stack * stack,const GLfloat * m,const char * caller)496 matrix_load(struct gl_context *ctx, struct gl_matrix_stack *stack,
497             const GLfloat *m, const char* caller)
498 {
499    if (!m) return;
500    if (MESA_VERBOSE & VERBOSE_API)
501       _mesa_debug(ctx,
502           "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
503           caller,
504           m[0], m[4], m[8], m[12],
505           m[1], m[5], m[9], m[13],
506           m[2], m[6], m[10], m[14],
507           m[3], m[7], m[11], m[15]);
508 
509    _mesa_load_matrix(ctx, stack, m);
510 }
511 
512 
513 /**
514  * Replace the current matrix with a given matrix.
515  *
516  * \param m matrix.
517  *
518  * \sa glLoadMatrixf().
519  *
520  * Flushes the vertices and calls _math_matrix_loadf() with the top-most
521  * matrix in the current stack and the given matrix.
522  * Marks __struct gl_contextRec::NewState with the dirty stack flag.
523  */
524 void GLAPIENTRY
_mesa_LoadMatrixf(const GLfloat * m)525 _mesa_LoadMatrixf( const GLfloat *m )
526 {
527    GET_CURRENT_CONTEXT(ctx);
528    matrix_load(ctx, ctx->CurrentStack, m, "glLoadMatrix");
529 }
530 
531 
532 /**
533  * Replace the named matrix with a given matrix.
534  *
535  * \param matrixMode matrix to replace
536  * \param m matrix
537  *
538  * \sa glLoadMatrixf().
539  */
540 void GLAPIENTRY
_mesa_MatrixLoadfEXT(GLenum matrixMode,const GLfloat * m)541 _mesa_MatrixLoadfEXT( GLenum matrixMode, const GLfloat *m )
542 {
543    GET_CURRENT_CONTEXT(ctx);
544    struct gl_matrix_stack * stack =
545       get_named_matrix_stack(ctx, matrixMode, "glMatrixLoadfEXT");
546    if (!stack)
547       return;
548 
549    matrix_load(ctx, stack, m, "glMatrixLoadfEXT");
550 }
551 
552 
553 static void
matrix_mult(struct gl_matrix_stack * stack,const GLfloat * m,const char * caller)554 matrix_mult(struct gl_matrix_stack *stack, const GLfloat *m, const char* caller)
555 {
556    GET_CURRENT_CONTEXT(ctx);
557    if (!m) return;
558    if (MESA_VERBOSE & VERBOSE_API)
559       _mesa_debug(ctx,
560           "%s(%f %f %f %f, %f %f %f %f, %f %f %f %f, %f %f %f %f\n",
561           caller,
562           m[0], m[4], m[8], m[12],
563           m[1], m[5], m[9], m[13],
564           m[2], m[6], m[10], m[14],
565           m[3], m[7], m[11], m[15]);
566 
567    FLUSH_VERTICES(ctx, 0);
568    _math_matrix_mul_floats(stack->Top, m);
569    ctx->NewState |= stack->DirtyFlag;
570 }
571 
572 
573 /**
574  * Multiply the current matrix with a given matrix.
575  *
576  * \param m matrix.
577  *
578  * \sa glMultMatrixf().
579  *
580  * Flushes the vertices and calls _math_matrix_mul_floats() with the top-most
581  * matrix in the current stack and the given matrix. Marks
582  * __struct gl_contextRec::NewState with the dirty stack flag.
583  */
584 void GLAPIENTRY
_mesa_MultMatrixf(const GLfloat * m)585 _mesa_MultMatrixf( const GLfloat *m )
586 {
587    GET_CURRENT_CONTEXT(ctx);
588    matrix_mult(ctx->CurrentStack, m, "glMultMatrix");
589 }
590 
591 
592 void GLAPIENTRY
_mesa_MatrixMultfEXT(GLenum matrixMode,const GLfloat * m)593 _mesa_MatrixMultfEXT( GLenum matrixMode, const GLfloat *m )
594 {
595    GET_CURRENT_CONTEXT(ctx);
596    struct gl_matrix_stack * stack =
597       get_named_matrix_stack(ctx, matrixMode, "glMatrixMultfEXT");
598    if (!stack)
599       return;
600 
601    matrix_mult(stack, m, "glMultMatrix");
602 }
603 
604 
605 static void
matrix_rotate(struct gl_matrix_stack * stack,GLfloat angle,GLfloat x,GLfloat y,GLfloat z,const char * caller)606 matrix_rotate(struct gl_matrix_stack *stack, GLfloat angle,
607               GLfloat x, GLfloat y, GLfloat z, const char* caller)
608 {
609    GET_CURRENT_CONTEXT(ctx);
610 
611    FLUSH_VERTICES(ctx, 0);
612    if (angle != 0.0F) {
613       _math_matrix_rotate(stack->Top, angle, x, y, z);
614       ctx->NewState |=stack->DirtyFlag;
615    }
616 }
617 
618 
619 /**
620  * Multiply the current matrix with a rotation matrix.
621  *
622  * \param angle angle of rotation, in degrees.
623  * \param x rotation vector x coordinate.
624  * \param y rotation vector y coordinate.
625  * \param z rotation vector z coordinate.
626  *
627  * \sa glRotatef().
628  *
629  * Flushes the vertices and calls _math_matrix_rotate() with the top-most
630  * matrix in the current stack and the given parameters. Marks
631  * __struct gl_contextRec::NewState with the dirty stack flag.
632  */
633 void GLAPIENTRY
_mesa_Rotatef(GLfloat angle,GLfloat x,GLfloat y,GLfloat z)634 _mesa_Rotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
635 {
636    GET_CURRENT_CONTEXT(ctx);
637    matrix_rotate(ctx->CurrentStack, angle, x, y, z, "glRotatef");
638 }
639 
640 
641 void GLAPIENTRY
_mesa_MatrixRotatefEXT(GLenum matrixMode,GLfloat angle,GLfloat x,GLfloat y,GLfloat z)642 _mesa_MatrixRotatefEXT( GLenum matrixMode, GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
643 {
644    GET_CURRENT_CONTEXT(ctx);
645    struct gl_matrix_stack *stack =
646       get_named_matrix_stack(ctx, matrixMode, "glMatrixRotatefEXT");
647    if (!stack)
648       return;
649 
650    matrix_rotate(stack, angle, x, y, z, "glMatrixRotatefEXT");
651 }
652 
653 
654 /**
655  * Multiply the current matrix with a general scaling matrix.
656  *
657  * \param x x axis scale factor.
658  * \param y y axis scale factor.
659  * \param z z axis scale factor.
660  *
661  * \sa glScalef().
662  *
663  * Flushes the vertices and calls _math_matrix_scale() with the top-most
664  * matrix in the current stack and the given parameters. Marks
665  * __struct gl_contextRec::NewState with the dirty stack flag.
666  */
667 void GLAPIENTRY
_mesa_Scalef(GLfloat x,GLfloat y,GLfloat z)668 _mesa_Scalef( GLfloat x, GLfloat y, GLfloat z )
669 {
670    GET_CURRENT_CONTEXT(ctx);
671 
672    FLUSH_VERTICES(ctx, 0);
673    _math_matrix_scale( ctx->CurrentStack->Top, x, y, z);
674    ctx->NewState |= ctx->CurrentStack->DirtyFlag;
675 }
676 
677 
678 void GLAPIENTRY
_mesa_MatrixScalefEXT(GLenum matrixMode,GLfloat x,GLfloat y,GLfloat z)679 _mesa_MatrixScalefEXT( GLenum matrixMode, GLfloat x, GLfloat y, GLfloat z )
680 {
681    struct gl_matrix_stack *stack;
682    GET_CURRENT_CONTEXT(ctx);
683 
684    stack = get_named_matrix_stack(ctx, matrixMode, "glMatrixScalefEXT");
685    if (!stack)
686       return;
687 
688    FLUSH_VERTICES(ctx, 0);
689    _math_matrix_scale(stack->Top, x, y, z);
690    ctx->NewState |= stack->DirtyFlag;
691 }
692 
693 
694 /**
695  * Multiply the current matrix with a translation matrix.
696  *
697  * \param x translation vector x coordinate.
698  * \param y translation vector y coordinate.
699  * \param z translation vector z coordinate.
700  *
701  * \sa glTranslatef().
702  *
703  * Flushes the vertices and calls _math_matrix_translate() with the top-most
704  * matrix in the current stack and the given parameters. Marks
705  * __struct gl_contextRec::NewState with the dirty stack flag.
706  */
707 void GLAPIENTRY
_mesa_Translatef(GLfloat x,GLfloat y,GLfloat z)708 _mesa_Translatef( GLfloat x, GLfloat y, GLfloat z )
709 {
710    GET_CURRENT_CONTEXT(ctx);
711 
712    FLUSH_VERTICES(ctx, 0);
713    _math_matrix_translate( ctx->CurrentStack->Top, x, y, z);
714    ctx->NewState |= ctx->CurrentStack->DirtyFlag;
715 }
716 
717 
718 void GLAPIENTRY
_mesa_MatrixTranslatefEXT(GLenum matrixMode,GLfloat x,GLfloat y,GLfloat z)719 _mesa_MatrixTranslatefEXT( GLenum matrixMode, GLfloat x, GLfloat y, GLfloat z )
720 {
721    GET_CURRENT_CONTEXT(ctx);
722    struct gl_matrix_stack *stack =
723       get_named_matrix_stack(ctx, matrixMode, "glMatrixTranslatefEXT");
724    if (!stack)
725       return;
726 
727    FLUSH_VERTICES(ctx, 0);
728    _math_matrix_translate(stack->Top, x, y, z);
729    ctx->NewState |= stack->DirtyFlag;
730 }
731 
732 
733 void GLAPIENTRY
_mesa_LoadMatrixd(const GLdouble * m)734 _mesa_LoadMatrixd( const GLdouble *m )
735 {
736    GLint i;
737    GLfloat f[16];
738    if (!m) return;
739    for (i = 0; i < 16; i++)
740       f[i] = (GLfloat) m[i];
741    _mesa_LoadMatrixf(f);
742 }
743 
744 
745 void GLAPIENTRY
_mesa_MatrixLoaddEXT(GLenum matrixMode,const GLdouble * m)746 _mesa_MatrixLoaddEXT( GLenum matrixMode, const GLdouble *m )
747 {
748    GLfloat f[16];
749    if (!m) return;
750    for (unsigned i = 0; i < 16; i++)
751       f[i] = (GLfloat) m[i];
752    _mesa_MatrixLoadfEXT(matrixMode, f);
753 }
754 
755 
756 void GLAPIENTRY
_mesa_MultMatrixd(const GLdouble * m)757 _mesa_MultMatrixd( const GLdouble *m )
758 {
759    GLint i;
760    GLfloat f[16];
761    if (!m) return;
762    for (i = 0; i < 16; i++)
763       f[i] = (GLfloat) m[i];
764    _mesa_MultMatrixf( f );
765 }
766 
767 
768 void GLAPIENTRY
_mesa_MatrixMultdEXT(GLenum matrixMode,const GLdouble * m)769 _mesa_MatrixMultdEXT( GLenum matrixMode, const GLdouble *m )
770 {
771    GLfloat f[16];
772    if (!m) return;
773    for (unsigned i = 0; i < 16; i++)
774       f[i] = (GLfloat) m[i];
775    _mesa_MatrixMultfEXT(matrixMode, f);
776 }
777 
778 
779 void GLAPIENTRY
_mesa_Rotated(GLdouble angle,GLdouble x,GLdouble y,GLdouble z)780 _mesa_Rotated( GLdouble angle, GLdouble x, GLdouble y, GLdouble z )
781 {
782    _mesa_Rotatef((GLfloat) angle, (GLfloat) x, (GLfloat) y, (GLfloat) z);
783 }
784 
785 
786 void GLAPIENTRY
_mesa_MatrixRotatedEXT(GLenum matrixMode,GLdouble angle,GLdouble x,GLdouble y,GLdouble z)787 _mesa_MatrixRotatedEXT( GLenum matrixMode, GLdouble angle,
788       GLdouble x, GLdouble y, GLdouble z )
789 {
790    _mesa_MatrixRotatefEXT(matrixMode, (GLfloat) angle,
791          (GLfloat) x, (GLfloat) y, (GLfloat) z);
792 }
793 
794 
795 void GLAPIENTRY
_mesa_Scaled(GLdouble x,GLdouble y,GLdouble z)796 _mesa_Scaled( GLdouble x, GLdouble y, GLdouble z )
797 {
798    _mesa_Scalef((GLfloat) x, (GLfloat) y, (GLfloat) z);
799 }
800 
801 
802 void GLAPIENTRY
_mesa_MatrixScaledEXT(GLenum matrixMode,GLdouble x,GLdouble y,GLdouble z)803 _mesa_MatrixScaledEXT( GLenum matrixMode, GLdouble x, GLdouble y, GLdouble z )
804 {
805    _mesa_MatrixScalefEXT(matrixMode, (GLfloat) x, (GLfloat) y, (GLfloat) z);
806 }
807 
808 
809 void GLAPIENTRY
_mesa_Translated(GLdouble x,GLdouble y,GLdouble z)810 _mesa_Translated( GLdouble x, GLdouble y, GLdouble z )
811 {
812    _mesa_Translatef((GLfloat) x, (GLfloat) y, (GLfloat) z);
813 }
814 
815 
816 void GLAPIENTRY
_mesa_MatrixTranslatedEXT(GLenum matrixMode,GLdouble x,GLdouble y,GLdouble z)817 _mesa_MatrixTranslatedEXT( GLenum matrixMode, GLdouble x, GLdouble y, GLdouble z )
818 {
819    _mesa_MatrixTranslatefEXT(matrixMode, (GLfloat) x, (GLfloat) y, (GLfloat) z);
820 }
821 
822 
823 void GLAPIENTRY
_mesa_LoadTransposeMatrixf(const GLfloat * m)824 _mesa_LoadTransposeMatrixf( const GLfloat *m )
825 {
826    GLfloat tm[16];
827    if (!m) return;
828    _math_transposef(tm, m);
829    _mesa_LoadMatrixf(tm);
830 }
831 
832 void GLAPIENTRY
_mesa_MatrixLoadTransposefEXT(GLenum matrixMode,const GLfloat * m)833 _mesa_MatrixLoadTransposefEXT( GLenum matrixMode, const GLfloat *m )
834 {
835    GLfloat tm[16];
836    if (!m) return;
837    _math_transposef(tm, m);
838    _mesa_MatrixLoadfEXT(matrixMode, tm);
839 }
840 
841 void GLAPIENTRY
_mesa_LoadTransposeMatrixd(const GLdouble * m)842 _mesa_LoadTransposeMatrixd( const GLdouble *m )
843 {
844    GLfloat tm[16];
845    if (!m) return;
846    _math_transposefd(tm, m);
847    _mesa_LoadMatrixf(tm);
848 }
849 
850 void GLAPIENTRY
_mesa_MatrixLoadTransposedEXT(GLenum matrixMode,const GLdouble * m)851 _mesa_MatrixLoadTransposedEXT( GLenum matrixMode, const GLdouble *m )
852 {
853    GLfloat tm[16];
854    if (!m) return;
855    _math_transposefd(tm, m);
856    _mesa_MatrixLoadfEXT(matrixMode, tm);
857 }
858 
859 void GLAPIENTRY
_mesa_MultTransposeMatrixf(const GLfloat * m)860 _mesa_MultTransposeMatrixf( const GLfloat *m )
861 {
862    GLfloat tm[16];
863    if (!m) return;
864    _math_transposef(tm, m);
865    _mesa_MultMatrixf(tm);
866 }
867 
868 void GLAPIENTRY
_mesa_MatrixMultTransposefEXT(GLenum matrixMode,const GLfloat * m)869 _mesa_MatrixMultTransposefEXT( GLenum matrixMode, const GLfloat *m )
870 {
871    GLfloat tm[16];
872    if (!m) return;
873    _math_transposef(tm, m);
874    _mesa_MatrixMultfEXT(matrixMode, tm);
875 }
876 
877 void GLAPIENTRY
_mesa_MultTransposeMatrixd(const GLdouble * m)878 _mesa_MultTransposeMatrixd( const GLdouble *m )
879 {
880    GLfloat tm[16];
881    if (!m) return;
882    _math_transposefd(tm, m);
883    _mesa_MultMatrixf(tm);
884 }
885 
886 void GLAPIENTRY
_mesa_MatrixMultTransposedEXT(GLenum matrixMode,const GLdouble * m)887 _mesa_MatrixMultTransposedEXT( GLenum matrixMode, const GLdouble *m )
888 {
889    GLfloat tm[16];
890    if (!m) return;
891    _math_transposefd(tm, m);
892    _mesa_MatrixMultfEXT(matrixMode, tm);
893 }
894 
895 /**********************************************************************/
896 /** \name State management */
897 /*@{*/
898 
899 
900 /**
901  * Update the projection matrix stack.
902  *
903  * \param ctx GL context.
904  *
905  * Calls _math_matrix_analyse() with the top-matrix of the projection matrix
906  * stack, and recomputes user clip positions if necessary.
907  *
908  * \note This routine references __struct gl_contextRec::Tranform attribute
909  * values to compute userclip positions in clip space, but is only called on
910  * _NEW_PROJECTION.  The _mesa_ClipPlane() function keeps these values up to
911  * date across changes to the __struct gl_contextRec::Transform attributes.
912  */
913 static void
update_projection(struct gl_context * ctx)914 update_projection( struct gl_context *ctx )
915 {
916    GLbitfield mask;
917 
918    _math_matrix_analyse( ctx->ProjectionMatrixStack.Top );
919 
920    /* Recompute clip plane positions in clipspace.  This is also done
921     * in _mesa_ClipPlane().
922     */
923    mask = ctx->Transform.ClipPlanesEnabled;
924    while (mask) {
925       const int p = u_bit_scan(&mask);
926 
927       _mesa_transform_vector( ctx->Transform._ClipUserPlane[p],
928                               ctx->Transform.EyeUserPlane[p],
929                               ctx->ProjectionMatrixStack.Top->inv );
930    }
931 }
932 
933 
934 /**
935  * Calculate the combined modelview-projection matrix.
936  *
937  * \param ctx GL context.
938  *
939  * Multiplies the top matrices of the projection and model view stacks into
940  * __struct gl_contextRec::_ModelProjectMatrix via _math_matrix_mul_matrix()
941  * and analyzes the resulting matrix via _math_matrix_analyse().
942  */
943 static void
calculate_model_project_matrix(struct gl_context * ctx)944 calculate_model_project_matrix( struct gl_context *ctx )
945 {
946    _math_matrix_mul_matrix( &ctx->_ModelProjectMatrix,
947                             ctx->ProjectionMatrixStack.Top,
948                             ctx->ModelviewMatrixStack.Top );
949 
950    _math_matrix_analyse( &ctx->_ModelProjectMatrix );
951 }
952 
953 
954 /**
955  * Updates the combined modelview-projection matrix.
956  *
957  * \param ctx GL context.
958  * \param new_state new state bit mask.
959  *
960  * If there is a new model view matrix then analyzes it. If there is a new
961  * projection matrix, updates it. Finally calls
962  * calculate_model_project_matrix() to recalculate the modelview-projection
963  * matrix.
964  */
_mesa_update_modelview_project(struct gl_context * ctx,GLuint new_state)965 void _mesa_update_modelview_project( struct gl_context *ctx, GLuint new_state )
966 {
967    if (new_state & _NEW_MODELVIEW)
968       _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
969 
970    if (new_state & _NEW_PROJECTION)
971       update_projection( ctx );
972 
973    /* Keep ModelviewProject up to date always to allow tnl
974     * implementations that go model->clip even when eye is required.
975     */
976    calculate_model_project_matrix(ctx);
977 }
978 
979 /*@}*/
980 
981 
982 /**********************************************************************/
983 /** Matrix stack initialization */
984 /*@{*/
985 
986 
987 /**
988  * Initialize a matrix stack.
989  *
990  * \param stack matrix stack.
991  * \param maxDepth maximum stack depth.
992  * \param dirtyFlag dirty flag.
993  *
994  * Allocates an array of \p maxDepth elements for the matrix stack and calls
995  * _math_matrix_ctr() for each element to initialize it.
996  */
997 static void
init_matrix_stack(struct gl_matrix_stack * stack,GLuint maxDepth,GLuint dirtyFlag)998 init_matrix_stack(struct gl_matrix_stack *stack,
999                   GLuint maxDepth, GLuint dirtyFlag)
1000 {
1001    stack->Depth = 0;
1002    stack->MaxDepth = maxDepth;
1003    stack->DirtyFlag = dirtyFlag;
1004    /* The stack will be dynamically resized at glPushMatrix() time */
1005    stack->Stack = calloc(1, sizeof(GLmatrix));
1006    stack->StackSize = 1;
1007    _math_matrix_ctr(&stack->Stack[0]);
1008    stack->Top = stack->Stack;
1009 }
1010 
1011 /**
1012  * Free matrix stack.
1013  *
1014  * \param stack matrix stack.
1015  *
1016  * Calls _math_matrix_dtr() for each element of the matrix stack and
1017  * frees the array.
1018  */
1019 static void
free_matrix_stack(struct gl_matrix_stack * stack)1020 free_matrix_stack( struct gl_matrix_stack *stack )
1021 {
1022    GLuint i;
1023    for (i = 0; i < stack->StackSize; i++) {
1024       _math_matrix_dtr(&stack->Stack[i]);
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 and the combined modelview-projection
1075  * matrix.
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    /* combined Modelview*Projection matrix */
1088    _math_matrix_dtr( &ctx->_ModelProjectMatrix );
1089 
1090 }
1091 
1092 
1093 /**
1094  * Initialize the context transform attribute group.
1095  *
1096  * \param ctx GL context.
1097  *
1098  * \todo Move this to a new file with other 'transform' routines.
1099  */
_mesa_init_transform(struct gl_context * ctx)1100 void _mesa_init_transform( struct gl_context *ctx )
1101 {
1102    GLuint i;
1103 
1104    /* Transformation group */
1105    ctx->Transform.MatrixMode = GL_MODELVIEW;
1106    ctx->Transform.Normalize = GL_FALSE;
1107    ctx->Transform.RescaleNormals = GL_FALSE;
1108    ctx->Transform.RasterPositionUnclipped = GL_FALSE;
1109    for (i=0;i<ctx->Const.MaxClipPlanes;i++) {
1110       ASSIGN_4V( ctx->Transform.EyeUserPlane[i], 0.0, 0.0, 0.0, 0.0 );
1111    }
1112    ctx->Transform.ClipPlanesEnabled = 0;
1113 }
1114 
1115 
1116 /*@}*/
1117