2
* Mesa 3-D graphics library
4
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the "Software"),
8
* to deal in the Software without restriction, including without limitation
9
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
* and/or sell copies of the Software, and to permit persons to whom the
11
* Software is furnished to do so, subject to the following conditions:
13
* The above copyright notice and this permission notice shall be included
14
* in all copies or substantial portions of the Software.
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
* OTHER DEALINGS IN THE SOFTWARE.
31
* -# 4x4 transformation matrices are stored in memory in column major order.
32
* -# Points/vertices are to be thought of as column vectors.
33
* -# Transformation of a point p by a matrix M is: p' = M * p
39
#include "main/errors.h"
40
#include "main/glheader.h"
41
#include "main/macros.h"
42
#define MATH_ASM_PTR_SIZE sizeof(void *)
43
#include "math/m_vector_asm.h"
47
#include "util/u_memory.h"
51
* \defgroup MatFlags MAT_FLAG_XXX-flags
53
* Bitmasks to indicate different kinds of 4x4 matrices in GLmatrix::flags
56
#define MAT_FLAG_IDENTITY 0 /**< is an identity matrix flag.
57
* (Not actually used - the identity
58
* matrix is identified by the absence
59
* of all other flags.)
61
#define MAT_FLAG_GENERAL 0x1 /**< is a general matrix flag */
62
#define MAT_FLAG_ROTATION 0x2 /**< is a rotation matrix flag */
63
#define MAT_FLAG_TRANSLATION 0x4 /**< is a translation matrix flag */
64
#define MAT_FLAG_UNIFORM_SCALE 0x8 /**< is an uniform scaling matrix flag */
65
#define MAT_FLAG_GENERAL_SCALE 0x10 /**< is a general scaling matrix flag */
66
#define MAT_FLAG_GENERAL_3D 0x20 /**< general 3D matrix flag */
67
#define MAT_FLAG_PERSPECTIVE 0x40 /**< is a perspective proj matrix flag */
68
#define MAT_FLAG_SINGULAR 0x80 /**< is a singular matrix flag */
69
#define MAT_DIRTY_TYPE 0x100 /**< matrix type is dirty */
70
#define MAT_DIRTY_FLAGS 0x200 /**< matrix flags are dirty */
71
#define MAT_DIRTY_INVERSE 0x400 /**< matrix inverse is dirty */
73
/** angle preserving matrix flags mask */
74
#define MAT_FLAGS_ANGLE_PRESERVING (MAT_FLAG_ROTATION | \
75
MAT_FLAG_TRANSLATION | \
76
MAT_FLAG_UNIFORM_SCALE)
78
/** geometry related matrix flags mask */
79
#define MAT_FLAGS_GEOMETRY (MAT_FLAG_GENERAL | \
81
MAT_FLAG_TRANSLATION | \
82
MAT_FLAG_UNIFORM_SCALE | \
83
MAT_FLAG_GENERAL_SCALE | \
84
MAT_FLAG_GENERAL_3D | \
85
MAT_FLAG_PERSPECTIVE | \
88
/** length preserving matrix flags mask */
89
#define MAT_FLAGS_LENGTH_PRESERVING (MAT_FLAG_ROTATION | \
93
/** 3D (non-perspective) matrix flags mask */
94
#define MAT_FLAGS_3D (MAT_FLAG_ROTATION | \
95
MAT_FLAG_TRANSLATION | \
96
MAT_FLAG_UNIFORM_SCALE | \
97
MAT_FLAG_GENERAL_SCALE | \
100
/** dirty matrix flags mask */
101
#define MAT_DIRTY (MAT_DIRTY_TYPE | \
109
* Test geometry related matrix flags.
111
* \param mat a pointer to a GLmatrix structure.
112
* \param a flags mask.
114
* \returns non-zero if all geometry related matrix flags are contained within
115
* the mask, or zero otherwise.
117
#define TEST_MAT_FLAGS(mat, a) \
118
((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)
123
* Names of the corresponding GLmatrixtype values.
125
static const char *types[] = {
129
"MATRIX_PERSPECTIVE",
139
static const GLfloat Identity[16] = {
148
/**********************************************************************/
149
/** \name Matrix multiplication */
152
#define A(row,col) a[(col<<2)+row]
153
#define B(row,col) b[(col<<2)+row]
154
#define P(row,col) product[(col<<2)+row]
157
* Perform a full 4x4 matrix multiplication.
161
* \param product will receive the product of \p a and \p b.
163
* \warning Is assumed that \p product != \p b. \p product == \p a is allowed.
165
* \note KW: 4*16 = 64 multiplications
167
* \author This \c matmul was contributed by Thomas Malik
169
static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
172
for (i = 0; i < 4; i++) {
173
const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
174
P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0);
175
P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1);
176
P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2);
177
P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3);
182
* Multiply two matrices known to occupy only the top three rows, such
183
* as typical model matrices, and orthogonal matrices.
187
* \param product will receive the product of \p a and \p b.
189
static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
192
for (i = 0; i < 3; i++) {
193
const GLfloat ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3);
194
P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0);
195
P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1);
196
P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2);
197
P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3;
210
* Multiply a matrix by an array of floats with known properties.
212
* \param mat pointer to a GLmatrix structure containing the left multiplication
213
* matrix, and that will receive the product result.
214
* \param m right multiplication matrix array.
215
* \param flags flags of the matrix \p m.
217
* Joins both flags and marks the type and inverse as dirty. Calls matmul34()
218
* if both matrices are 3D, or matmul4() otherwise.
220
static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
222
mat->flags |= (flags | MAT_DIRTY_TYPE | MAT_DIRTY_INVERSE);
224
if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
225
matmul34( mat->m, mat->m, m );
227
matmul4( mat->m, mat->m, m );
231
* Matrix multiplication.
233
* \param dest destination matrix.
234
* \param a left matrix.
235
* \param b right matrix.
237
* Joins both flags and marks the type and inverse as dirty. Calls matmul34()
238
* if both matrices are 3D, or matmul4() otherwise.
241
_math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
243
dest->flags = (a->flags |
248
if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
249
matmul34( dest->m, a->m, b->m );
251
matmul4( dest->m, a->m, b->m );
255
* Matrix multiplication.
257
* \param dest left and destination matrix.
258
* \param m right matrix array.
260
* Marks the matrix flags with general flag, and type and inverse dirty flags.
261
* Calls matmul4() for the multiplication.
264
_math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
266
dest->flags |= (MAT_FLAG_GENERAL |
271
matmul4( dest->m, dest->m, m );
277
/**********************************************************************/
278
/** \name Matrix output */
282
* Print a matrix array.
284
* \param m matrix array.
286
* Called by _math_matrix_print() to print a matrix or its inverse.
288
static void print_matrix_floats( const GLfloat m[16] )
292
_mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
297
* Dumps the contents of a GLmatrix structure.
299
* \param m pointer to the GLmatrix structure.
302
_math_matrix_print( const GLmatrix *m )
306
_mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
307
print_matrix_floats(m->m);
308
_mesa_debug(NULL, "Inverse: \n");
309
print_matrix_floats(m->inv);
310
matmul4(prod, m->m, m->inv);
311
_mesa_debug(NULL, "Mat * Inverse:\n");
312
print_matrix_floats(prod);
319
* References an element of 4x4 matrix.
321
* \param m matrix array.
322
* \param c column of the desired element.
323
* \param r row of the desired element.
325
* \return value of the desired element.
327
* Calculate the linear storage index of the element and references it.
329
#define MAT(m,r,c) (m)[(c)*4+(r)]
332
/**********************************************************************/
333
/** \name Matrix inversion */
337
* Compute inverse of 4x4 transformation matrix.
339
* \param mat pointer to a GLmatrix structure. The matrix inverse will be
340
* stored in the GLmatrix::inv attribute.
342
* \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
345
* Code contributed by Jacques Leroy jle@star.be
347
* Calculates the inverse matrix by performing the gaussian matrix reduction
348
* with partial pivoting followed by back/substitution with the loops manually
351
static GLboolean invert_matrix_general( GLmatrix *mat )
353
return util_invert_mat4x4(mat->inv, mat->m);
357
* Compute inverse of a general 3d transformation matrix.
359
* \param mat pointer to a GLmatrix structure. The matrix inverse will be
360
* stored in the GLmatrix::inv attribute.
362
* \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
364
* \author Adapted from graphics gems II.
366
* Calculates the inverse of the upper left by first calculating its
367
* determinant and multiplying it to the symmetric adjust matrix of each
368
* element. Finally deals with the translation part by transforming the
369
* original translation vector using by the calculated submatrix inverse.
371
static GLboolean invert_matrix_3d_general( GLmatrix *mat )
373
const GLfloat *in = mat->m;
374
GLfloat *out = mat->inv;
378
/* Calculate the determinant of upper left 3x3 submatrix and
379
* determine if the matrix is singular.
382
t = MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
383
if (t >= 0.0F) pos += t; else neg += t;
385
t = MAT(in,1,0) * MAT(in,2,1) * MAT(in,0,2);
386
if (t >= 0.0F) pos += t; else neg += t;
388
t = MAT(in,2,0) * MAT(in,0,1) * MAT(in,1,2);
389
if (t >= 0.0F) pos += t; else neg += t;
391
t = -MAT(in,2,0) * MAT(in,1,1) * MAT(in,0,2);
392
if (t >= 0.0F) pos += t; else neg += t;
394
t = -MAT(in,1,0) * MAT(in,0,1) * MAT(in,2,2);
395
if (t >= 0.0F) pos += t; else neg += t;
397
t = -MAT(in,0,0) * MAT(in,2,1) * MAT(in,1,2);
398
if (t >= 0.0F) pos += t; else neg += t;
402
if (fabsf(det) < 1e-25F)
406
MAT(out,0,0) = ( (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
407
MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
408
MAT(out,0,2) = ( (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
409
MAT(out,1,0) = (- (MAT(in,1,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,1,2) )*det);
410
MAT(out,1,1) = ( (MAT(in,0,0)*MAT(in,2,2) - MAT(in,2,0)*MAT(in,0,2) )*det);
411
MAT(out,1,2) = (- (MAT(in,0,0)*MAT(in,1,2) - MAT(in,1,0)*MAT(in,0,2) )*det);
412
MAT(out,2,0) = ( (MAT(in,1,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,1,1) )*det);
413
MAT(out,2,1) = (- (MAT(in,0,0)*MAT(in,2,1) - MAT(in,2,0)*MAT(in,0,1) )*det);
414
MAT(out,2,2) = ( (MAT(in,0,0)*MAT(in,1,1) - MAT(in,1,0)*MAT(in,0,1) )*det);
416
/* Do the translation part */
417
MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
418
MAT(in,1,3) * MAT(out,0,1) +
419
MAT(in,2,3) * MAT(out,0,2) );
420
MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
421
MAT(in,1,3) * MAT(out,1,1) +
422
MAT(in,2,3) * MAT(out,1,2) );
423
MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
424
MAT(in,1,3) * MAT(out,2,1) +
425
MAT(in,2,3) * MAT(out,2,2) );
431
* Compute inverse of a 3d transformation matrix.
433
* \param mat pointer to a GLmatrix structure. The matrix inverse will be
434
* stored in the GLmatrix::inv attribute.
436
* \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
438
* If the matrix is not an angle preserving matrix then calls
439
* invert_matrix_3d_general for the actual calculation. Otherwise calculates
440
* the inverse matrix analyzing and inverting each of the scaling, rotation and
443
static GLboolean invert_matrix_3d( GLmatrix *mat )
445
const GLfloat *in = mat->m;
446
GLfloat *out = mat->inv;
448
if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
449
return invert_matrix_3d_general( mat );
452
if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
453
GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
454
MAT(in,0,1) * MAT(in,0,1) +
455
MAT(in,0,2) * MAT(in,0,2));
460
scale = 1.0F / scale;
462
/* Transpose and scale the 3 by 3 upper-left submatrix. */
463
MAT(out,0,0) = scale * MAT(in,0,0);
464
MAT(out,1,0) = scale * MAT(in,0,1);
465
MAT(out,2,0) = scale * MAT(in,0,2);
466
MAT(out,0,1) = scale * MAT(in,1,0);
467
MAT(out,1,1) = scale * MAT(in,1,1);
468
MAT(out,2,1) = scale * MAT(in,1,2);
469
MAT(out,0,2) = scale * MAT(in,2,0);
470
MAT(out,1,2) = scale * MAT(in,2,1);
471
MAT(out,2,2) = scale * MAT(in,2,2);
473
else if (mat->flags & MAT_FLAG_ROTATION) {
474
/* Transpose the 3 by 3 upper-left submatrix. */
475
MAT(out,0,0) = MAT(in,0,0);
476
MAT(out,1,0) = MAT(in,0,1);
477
MAT(out,2,0) = MAT(in,0,2);
478
MAT(out,0,1) = MAT(in,1,0);
479
MAT(out,1,1) = MAT(in,1,1);
480
MAT(out,2,1) = MAT(in,1,2);
481
MAT(out,0,2) = MAT(in,2,0);
482
MAT(out,1,2) = MAT(in,2,1);
483
MAT(out,2,2) = MAT(in,2,2);
486
/* pure translation */
487
memcpy( out, Identity, sizeof(Identity) );
488
MAT(out,0,3) = - MAT(in,0,3);
489
MAT(out,1,3) = - MAT(in,1,3);
490
MAT(out,2,3) = - MAT(in,2,3);
494
if (mat->flags & MAT_FLAG_TRANSLATION) {
495
/* Do the translation part */
496
MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
497
MAT(in,1,3) * MAT(out,0,1) +
498
MAT(in,2,3) * MAT(out,0,2) );
499
MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
500
MAT(in,1,3) * MAT(out,1,1) +
501
MAT(in,2,3) * MAT(out,1,2) );
502
MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
503
MAT(in,1,3) * MAT(out,2,1) +
504
MAT(in,2,3) * MAT(out,2,2) );
507
MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
514
* Compute inverse of an identity transformation matrix.
516
* \param mat pointer to a GLmatrix structure. The matrix inverse will be
517
* stored in the GLmatrix::inv attribute.
519
* \return always GL_TRUE.
521
* Simply copies Identity into GLmatrix::inv.
523
static GLboolean invert_matrix_identity( GLmatrix *mat )
525
memcpy( mat->inv, Identity, sizeof(Identity) );
530
* Compute inverse of a no-rotation 3d transformation matrix.
532
* \param mat pointer to a GLmatrix structure. The matrix inverse will be
533
* stored in the GLmatrix::inv attribute.
535
* \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
539
static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
541
const GLfloat *in = mat->m;
542
GLfloat *out = mat->inv;
544
if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
547
memcpy( out, Identity, sizeof(Identity) );
548
MAT(out,0,0) = 1.0F / MAT(in,0,0);
549
MAT(out,1,1) = 1.0F / MAT(in,1,1);
550
MAT(out,2,2) = 1.0F / MAT(in,2,2);
552
if (mat->flags & MAT_FLAG_TRANSLATION) {
553
MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
554
MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
555
MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
562
* Compute inverse of a no-rotation 2d transformation matrix.
564
* \param mat pointer to a GLmatrix structure. The matrix inverse will be
565
* stored in the GLmatrix::inv attribute.
567
* \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
569
* Calculates the inverse matrix by applying the inverse scaling and
570
* translation to the identity matrix.
572
static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
574
const GLfloat *in = mat->m;
575
GLfloat *out = mat->inv;
577
if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
580
memcpy( out, Identity, sizeof(Identity) );
581
MAT(out,0,0) = 1.0F / MAT(in,0,0);
582
MAT(out,1,1) = 1.0F / MAT(in,1,1);
584
if (mat->flags & MAT_FLAG_TRANSLATION) {
585
MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
586
MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
594
static GLboolean invert_matrix_perspective( GLmatrix *mat )
596
const GLfloat *in = mat->m;
597
GLfloat *out = mat->inv;
599
if (MAT(in,2,3) == 0)
602
memcpy( out, Identity, sizeof(Identity) );
604
MAT(out,0,0) = 1.0F / MAT(in,0,0);
605
MAT(out,1,1) = 1.0F / MAT(in,1,1);
607
MAT(out,0,3) = MAT(in,0,2);
608
MAT(out,1,3) = MAT(in,1,2);
613
MAT(out,3,2) = 1.0F / MAT(in,2,3);
614
MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
621
* Matrix inversion function pointer type.
623
typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
626
* Table of the matrix inversion functions according to the matrix type.
628
static inv_mat_func inv_mat_tab[7] = {
629
invert_matrix_general,
630
invert_matrix_identity,
631
invert_matrix_3d_no_rot,
633
/* Don't use this function for now - it fails when the projection matrix
634
* is premultiplied by a translation (ala Chromium's tilesort SPU).
636
invert_matrix_perspective,
638
invert_matrix_general,
640
invert_matrix_3d, /* lazy! */
641
invert_matrix_2d_no_rot,
646
* Compute inverse of a transformation matrix.
648
* \param mat pointer to a GLmatrix structure. The matrix inverse will be
649
* stored in the GLmatrix::inv attribute.
651
* \return GL_TRUE for success, GL_FALSE for failure (\p singular matrix).
653
* Calls the matrix inversion function in inv_mat_tab corresponding to the
654
* given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag,
655
* and copies the identity matrix into GLmatrix::inv.
657
static GLboolean matrix_invert( GLmatrix *mat )
659
if (inv_mat_tab[mat->type](mat)) {
660
mat->flags &= ~MAT_FLAG_SINGULAR;
663
mat->flags |= MAT_FLAG_SINGULAR;
664
memcpy( mat->inv, Identity, sizeof(Identity) );
672
/**********************************************************************/
673
/** \name Matrix generation */
677
* Generate a 4x4 transformation matrix from glRotate parameters, and
678
* post-multiply the input matrix by it.
681
* This function was contributed by Erich Boleyn (erich@uruk.org).
682
* Optimizations contributed by Rudolf Opalla (rudi@khm.de).
685
_math_matrix_rotate( GLmatrix *mat,
686
GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
688
GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
692
s = sinf( angle * M_PI / 180.0 );
693
c = cosf( angle * M_PI / 180.0 );
695
memcpy(m, Identity, sizeof(Identity));
696
optimized = GL_FALSE;
698
#define M(row,col) m[col*4+row]
704
/* rotate only around z-axis */
717
else if (z == 0.0F) {
719
/* rotate only around y-axis */
732
else if (y == 0.0F) {
735
/* rotate only around x-axis */
750
const GLfloat mag = sqrtf(x * x + y * y + z * z);
752
if (mag <= 1.0e-4F) {
753
/* no rotation, leave mat as-is */
763
* Arbitrary axis rotation matrix.
765
* This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
766
* like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
767
* (which is about the X-axis), and the two composite transforms
768
* Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
769
* from the arbitrary axis to the X-axis then back. They are
770
* all elementary rotations.
772
* Rz' is a rotation about the Z-axis, to bring the axis vector
773
* into the x-z plane. Then Ry' is applied, rotating about the
774
* Y-axis to bring the axis vector parallel with the X-axis. The
775
* rotation about the X-axis is then performed. Ry and Rz are
776
* simply the respective inverse transforms to bring the arbitrary
777
* axis back to its original orientation. The first transforms
778
* Rz' and Ry' are considered inverses, since the data from the
779
* arbitrary axis gives you info on how to get to it, not how
780
* to get away from it, and an inverse must be applied.
782
* The basic calculation used is to recognize that the arbitrary
783
* axis vector (x, y, z), since it is of unit length, actually
784
* represents the sines and cosines of the angles to rotate the
785
* X-axis to the same orientation, with theta being the angle about
786
* Z and phi the angle about Y (in the order described above)
789
* cos ( theta ) = x / sqrt ( 1 - z^2 )
790
* sin ( theta ) = y / sqrt ( 1 - z^2 )
792
* cos ( phi ) = sqrt ( 1 - z^2 )
795
* Note that cos ( phi ) can further be inserted to the above
798
* cos ( theta ) = x / cos ( phi )
799
* sin ( theta ) = y / sin ( phi )
801
* ...etc. Because of those relations and the standard trigonometric
802
* relations, it is pssible to reduce the transforms down to what
803
* is used below. It may be that any primary axis chosen will give the
804
* same results (modulo a sign convention) using thie method.
806
* Particularly nice is to notice that all divisions that might
807
* have caused trouble when parallel to certain planes or
808
* axis go away with care paid to reducing the expressions.
809
* After checking, it does perform correctly under all cases, since
810
* in all the cases of division where the denominator would have
811
* been zero, the numerator would have been zero as well, giving
812
* the expected result.
826
/* We already hold the identity-matrix so we can skip some statements */
827
M(0,0) = (one_c * xx) + c;
828
M(0,1) = (one_c * xy) - zs;
829
M(0,2) = (one_c * zx) + ys;
832
M(1,0) = (one_c * xy) + zs;
833
M(1,1) = (one_c * yy) + c;
834
M(1,2) = (one_c * yz) - xs;
837
M(2,0) = (one_c * zx) - ys;
838
M(2,1) = (one_c * yz) + xs;
839
M(2,2) = (one_c * zz) + c;
851
matrix_multf( mat, m, MAT_FLAG_ROTATION );
855
* Apply a perspective projection matrix.
857
* \param mat matrix to apply the projection.
858
* \param left left clipping plane coordinate.
859
* \param right right clipping plane coordinate.
860
* \param bottom bottom clipping plane coordinate.
861
* \param top top clipping plane coordinate.
862
* \param nearval distance to the near clipping plane.
863
* \param farval distance to the far clipping plane.
865
* Creates the projection matrix and multiplies it with \p mat, marking the
866
* MAT_FLAG_PERSPECTIVE flag.
869
_math_matrix_frustum( GLmatrix *mat,
870
GLfloat left, GLfloat right,
871
GLfloat bottom, GLfloat top,
872
GLfloat nearval, GLfloat farval )
874
GLfloat x, y, a, b, c, d;
877
x = (2.0F*nearval) / (right-left);
878
y = (2.0F*nearval) / (top-bottom);
879
a = (right+left) / (right-left);
880
b = (top+bottom) / (top-bottom);
881
c = -(farval+nearval) / ( farval-nearval);
882
d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
884
#define M(row,col) m[col*4+row]
885
M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
886
M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
887
M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
888
M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
891
matrix_multf( mat, m, MAT_FLAG_PERSPECTIVE );
895
* Create an orthographic projection matrix.
897
* \param m float array in which to store the project matrix
898
* \param left left clipping plane coordinate.
899
* \param right right clipping plane coordinate.
900
* \param bottom bottom clipping plane coordinate.
901
* \param top top clipping plane coordinate.
902
* \param nearval distance to the near clipping plane.
903
* \param farval distance to the far clipping plane.
905
* Creates the projection matrix and stored the values in \p m. As with other
906
* OpenGL matrices, the data is stored in column-major ordering.
909
_math_float_ortho(float *m,
910
float left, float right,
911
float bottom, float top,
912
float nearval, float farval)
914
#define M(row,col) m[col*4+row]
915
M(0,0) = 2.0F / (right-left);
918
M(0,3) = -(right+left) / (right-left);
921
M(1,1) = 2.0F / (top-bottom);
923
M(1,3) = -(top+bottom) / (top-bottom);
927
M(2,2) = -2.0F / (farval-nearval);
928
M(2,3) = -(farval+nearval) / (farval-nearval);
938
* Apply an orthographic projection matrix.
940
* \param mat matrix to apply the projection.
941
* \param left left clipping plane coordinate.
942
* \param right right clipping plane coordinate.
943
* \param bottom bottom clipping plane coordinate.
944
* \param top top clipping plane coordinate.
945
* \param nearval distance to the near clipping plane.
946
* \param farval distance to the far clipping plane.
948
* Creates the projection matrix and multiplies it with \p mat, marking the
949
* MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.
952
_math_matrix_ortho( GLmatrix *mat,
953
GLfloat left, GLfloat right,
954
GLfloat bottom, GLfloat top,
955
GLfloat nearval, GLfloat farval )
959
_math_float_ortho(m, left, right, bottom, top, nearval, farval);
960
matrix_multf( mat, m, (MAT_FLAG_GENERAL_SCALE|MAT_FLAG_TRANSLATION));
964
* Multiply a matrix with a general scaling matrix.
967
* \param x x axis scale factor.
968
* \param y y axis scale factor.
969
* \param z z axis scale factor.
971
* Multiplies in-place the elements of \p mat by the scale factors. Checks if
972
* the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE
973
* flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and
974
* MAT_DIRTY_INVERSE dirty flags.
977
_math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
980
m[0] *= x; m[4] *= y; m[8] *= z;
981
m[1] *= x; m[5] *= y; m[9] *= z;
982
m[2] *= x; m[6] *= y; m[10] *= z;
983
m[3] *= x; m[7] *= y; m[11] *= z;
985
if (fabsf(x - y) < 1e-8F && fabsf(x - z) < 1e-8F)
986
mat->flags |= MAT_FLAG_UNIFORM_SCALE;
988
mat->flags |= MAT_FLAG_GENERAL_SCALE;
990
mat->flags |= (MAT_DIRTY_TYPE |
995
* Multiply a matrix with a translation matrix.
998
* \param x translation vector x coordinate.
999
* \param y translation vector y coordinate.
1000
* \param z translation vector z coordinate.
1002
* Adds the translation coordinates to the elements of \p mat in-place. Marks
1003
* the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE
1007
_math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
1009
GLfloat *m = mat->m;
1010
m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1011
m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1012
m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1013
m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1015
mat->flags |= (MAT_FLAG_TRANSLATION |
1022
* Set matrix to do viewport and depthrange mapping.
1023
* Transforms Normalized Device Coords to window/Z values.
1026
_math_matrix_viewport(GLmatrix *m, const float scale[3],
1027
const float translate[3], double depthMax)
1029
m->m[MAT_SX] = scale[0];
1030
m->m[MAT_TX] = translate[0];
1031
m->m[MAT_SY] = scale[1];
1032
m->m[MAT_TY] = translate[1];
1033
m->m[MAT_SZ] = depthMax*scale[2];
1034
m->m[MAT_TZ] = depthMax*translate[2];
1035
m->flags = MAT_FLAG_GENERAL_SCALE | MAT_FLAG_TRANSLATION;
1036
m->type = MATRIX_3D_NO_ROT;
1041
* Set a matrix to the identity matrix.
1043
* \param mat matrix.
1045
* Copies ::Identity into \p GLmatrix::m, and into GLmatrix::inv if not NULL.
1046
* Sets the matrix type to identity, and clear the dirty flags.
1049
_math_matrix_set_identity( GLmatrix *mat )
1051
STATIC_ASSERT(MATRIX_M == offsetof(GLmatrix, m));
1052
STATIC_ASSERT(MATRIX_INV == offsetof(GLmatrix, inv));
1054
memcpy( mat->m, Identity, sizeof(Identity) );
1055
memcpy( mat->inv, Identity, sizeof(Identity) );
1057
mat->type = MATRIX_IDENTITY;
1058
mat->flags &= ~(MAT_DIRTY_FLAGS|
1066
/**********************************************************************/
1067
/** \name Matrix analysis */
1070
#define ZERO(x) (1<<x)
1071
#define ONE(x) (1<<(x+16))
1073
#define MASK_NO_TRX (ZERO(12) | ZERO(13) | ZERO(14))
1074
#define MASK_NO_2D_SCALE ( ONE(0) | ONE(5))
1076
#define MASK_IDENTITY ( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
1077
ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
1078
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1079
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1081
#define MASK_2D_NO_ROT ( ZERO(4) | ZERO(8) | \
1082
ZERO(1) | ZERO(9) | \
1083
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1084
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1086
#define MASK_2D ( ZERO(8) | \
1088
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
1089
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1092
#define MASK_3D_NO_ROT ( ZERO(4) | ZERO(8) | \
1093
ZERO(1) | ZERO(9) | \
1094
ZERO(2) | ZERO(6) | \
1095
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1100
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
1103
#define MASK_PERSPECTIVE ( ZERO(4) | ZERO(12) |\
1104
ZERO(1) | ZERO(13) |\
1105
ZERO(2) | ZERO(6) | \
1106
ZERO(3) | ZERO(7) | ZERO(15) )
1108
#define SQ(x) ((x)*(x))
1111
* Determine type and flags from scratch.
1113
* \param mat matrix.
1115
* This is expensive enough to only want to do it once.
1117
static void analyse_from_scratch( GLmatrix *mat )
1119
const GLfloat *m = mat->m;
1123
for (i = 0 ; i < 16 ; i++) {
1124
if (m[i] == 0.0F) mask |= (1<<i);
1127
if (m[0] == 1.0F) mask |= (1<<16);
1128
if (m[5] == 1.0F) mask |= (1<<21);
1129
if (m[10] == 1.0F) mask |= (1<<26);
1130
if (m[15] == 1.0F) mask |= (1<<31);
1132
mat->flags &= ~MAT_FLAGS_GEOMETRY;
1134
/* Check for translation - no-one really cares
1136
if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
1137
mat->flags |= MAT_FLAG_TRANSLATION;
1141
if (mask == (GLuint) MASK_IDENTITY) {
1142
mat->type = MATRIX_IDENTITY;
1144
else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
1145
mat->type = MATRIX_2D_NO_ROT;
1147
if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
1148
mat->flags |= MAT_FLAG_GENERAL_SCALE;
1150
else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
1151
GLfloat mm = DOT2(m, m);
1152
GLfloat m4m4 = DOT2(m+4,m+4);
1153
GLfloat mm4 = DOT2(m,m+4);
1155
mat->type = MATRIX_2D;
1157
/* Check for scale */
1158
if (SQ(mm-1) > SQ(1e-6F) ||
1159
SQ(m4m4-1) > SQ(1e-6F))
1160
mat->flags |= MAT_FLAG_GENERAL_SCALE;
1162
/* Check for rotation */
1163
if (SQ(mm4) > SQ(1e-6F))
1164
mat->flags |= MAT_FLAG_GENERAL_3D;
1166
mat->flags |= MAT_FLAG_ROTATION;
1169
else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
1170
mat->type = MATRIX_3D_NO_ROT;
1172
/* Check for scale */
1173
if (SQ(m[0]-m[5]) < SQ(1e-6F) &&
1174
SQ(m[0]-m[10]) < SQ(1e-6F)) {
1175
if (SQ(m[0]-1.0F) > SQ(1e-6F)) {
1176
mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1180
mat->flags |= MAT_FLAG_GENERAL_SCALE;
1183
else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
1184
GLfloat c1 = DOT3(m,m);
1185
GLfloat c2 = DOT3(m+4,m+4);
1186
GLfloat c3 = DOT3(m+8,m+8);
1187
GLfloat d1 = DOT3(m, m+4);
1190
mat->type = MATRIX_3D;
1192
/* Check for scale */
1193
if (SQ(c1-c2) < SQ(1e-6F) && SQ(c1-c3) < SQ(1e-6F)) {
1194
if (SQ(c1-1.0F) > SQ(1e-6F))
1195
mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1196
/* else no scale at all */
1199
mat->flags |= MAT_FLAG_GENERAL_SCALE;
1202
/* Check for rotation */
1203
if (SQ(d1) < SQ(1e-6F)) {
1204
CROSS3( cp, m, m+4 );
1205
SUB_3V( cp, cp, (m+8) );
1206
if (LEN_SQUARED_3FV(cp) < SQ(1e-6F))
1207
mat->flags |= MAT_FLAG_ROTATION;
1209
mat->flags |= MAT_FLAG_GENERAL_3D;
1212
mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
1215
else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
1216
mat->type = MATRIX_PERSPECTIVE;
1217
mat->flags |= MAT_FLAG_GENERAL;
1220
mat->type = MATRIX_GENERAL;
1221
mat->flags |= MAT_FLAG_GENERAL;
1226
* Analyze a matrix given that its flags are accurate.
1228
* This is the more common operation, hopefully.
1230
static void analyse_from_flags( GLmatrix *mat )
1232
const GLfloat *m = mat->m;
1234
if (TEST_MAT_FLAGS(mat, 0)) {
1235
mat->type = MATRIX_IDENTITY;
1237
else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
1238
MAT_FLAG_UNIFORM_SCALE |
1239
MAT_FLAG_GENERAL_SCALE))) {
1240
if ( m[10]==1.0F && m[14]==0.0F ) {
1241
mat->type = MATRIX_2D_NO_ROT;
1244
mat->type = MATRIX_3D_NO_ROT;
1247
else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
1250
&& m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
1251
mat->type = MATRIX_2D;
1254
mat->type = MATRIX_3D;
1257
else if ( m[4]==0.0F && m[12]==0.0F
1258
&& m[1]==0.0F && m[13]==0.0F
1259
&& m[2]==0.0F && m[6]==0.0F
1260
&& m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
1261
mat->type = MATRIX_PERSPECTIVE;
1264
mat->type = MATRIX_GENERAL;
1269
* Analyze and update a matrix.
1271
* \param mat matrix.
1273
* If the matrix type is dirty then calls either analyse_from_scratch() or
1274
* analyse_from_flags() to determine its type, according to whether the flags
1275
* are dirty or not, respectively. If the matrix has an inverse and it's dirty
1276
* then calls matrix_invert(). Finally clears the dirty flags.
1279
_math_matrix_analyse( GLmatrix *mat )
1281
if (mat->flags & MAT_DIRTY_TYPE) {
1282
if (mat->flags & MAT_DIRTY_FLAGS)
1283
analyse_from_scratch( mat );
1285
analyse_from_flags( mat );
1288
if (mat->flags & MAT_DIRTY_INVERSE) {
1289
matrix_invert( mat );
1290
mat->flags &= ~MAT_DIRTY_INVERSE;
1293
mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
1300
* Test if the given matrix preserves vector lengths.
1303
_math_matrix_is_length_preserving( const GLmatrix *m )
1305
return TEST_MAT_FLAGS( m, MAT_FLAGS_LENGTH_PRESERVING);
1310
* Test if the given matrix does any rotation.
1311
* (or perhaps if the upper-left 3x3 is non-identity)
1314
_math_matrix_has_rotation( const GLmatrix *m )
1316
if (m->flags & (MAT_FLAG_GENERAL |
1318
MAT_FLAG_GENERAL_3D |
1319
MAT_FLAG_PERSPECTIVE))
1327
_math_matrix_is_general_scale( const GLmatrix *m )
1329
return (m->flags & MAT_FLAG_GENERAL_SCALE) ? GL_TRUE : GL_FALSE;
1334
_math_matrix_is_dirty( const GLmatrix *m )
1336
return (m->flags & MAT_DIRTY) ? GL_TRUE : GL_FALSE;
1340
/**********************************************************************/
1341
/** \name Matrix setup */
1347
* \param to destination matrix.
1348
* \param from source matrix.
1350
* Copies all fields in GLmatrix, creating an inverse array if necessary.
1353
_math_matrix_copy( GLmatrix *to, const GLmatrix *from )
1355
memcpy(to->m, from->m, 16 * sizeof(GLfloat));
1356
memcpy(to->inv, from->inv, 16 * sizeof(GLfloat));
1357
to->flags = from->flags;
1358
to->type = from->type;
1362
* Copy a matrix as part of glPushMatrix.
1364
* The makes the source matrix canonical (inverse and flags are up-to-date),
1365
* so that later glPopMatrix is evaluated as a no-op if there is no state
1368
* It this wasn't done, a draw call would canonicalize the matrix, which
1369
* would make it different from the pushed one and so glPopMatrix wouldn't be
1370
* recognized as a no-op.
1373
_math_matrix_push_copy(GLmatrix *to, GLmatrix *from)
1375
if (from->flags & MAT_DIRTY)
1376
_math_matrix_analyse(from);
1378
_math_matrix_copy(to, from);
1382
* Loads a matrix array into GLmatrix.
1384
* \param m matrix array.
1385
* \param mat matrix.
1387
* Copies \p m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY
1391
_math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
1393
memcpy( mat->m, m, 16*sizeof(GLfloat) );
1394
mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
1398
* Matrix constructor.
1402
* Initialize the GLmatrix fields.
1405
_math_matrix_ctr( GLmatrix *m )
1407
memset(m, 0, sizeof(*m));
1408
memcpy( m->m, Identity, sizeof(Identity) );
1409
memcpy( m->inv, Identity, sizeof(Identity) );
1410
m->type = MATRIX_IDENTITY;
1417
/**********************************************************************/
1418
/** \name Matrix transpose */
1422
* Transpose a GLfloat matrix.
1424
* \param to destination array.
1425
* \param from source array.
1428
_math_transposef( GLfloat to[16], const GLfloat from[16] )
1449
* Transpose a GLdouble matrix.
1451
* \param to destination array.
1452
* \param from source array.
1455
_math_transposed( GLdouble to[16], const GLdouble from[16] )
1476
* Transpose a GLdouble matrix and convert to GLfloat.
1478
* \param to destination array.
1479
* \param from source array.
1482
_math_transposefd( GLfloat to[16], const GLdouble from[16] )
1484
to[0] = (GLfloat) from[0];
1485
to[1] = (GLfloat) from[4];
1486
to[2] = (GLfloat) from[8];
1487
to[3] = (GLfloat) from[12];
1488
to[4] = (GLfloat) from[1];
1489
to[5] = (GLfloat) from[5];
1490
to[6] = (GLfloat) from[9];
1491
to[7] = (GLfloat) from[13];
1492
to[8] = (GLfloat) from[2];
1493
to[9] = (GLfloat) from[6];
1494
to[10] = (GLfloat) from[10];
1495
to[11] = (GLfloat) from[14];
1496
to[12] = (GLfloat) from[3];
1497
to[13] = (GLfloat) from[7];
1498
to[14] = (GLfloat) from[11];
1499
to[15] = (GLfloat) from[15];
1506
* Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This
1507
* function is used for transforming clipping plane equations and spotlight
1509
* Mathematically, u = v * m.
1510
* Input: v - input vector
1511
* m - transformation matrix
1512
* Output: u - transformed vector
1515
_mesa_transform_vector( GLfloat u[4], const GLfloat v[4], const GLfloat m[16] )
1517
const GLfloat v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
1518
#define M(row,col) m[row + col*4]
1519
u[0] = v0 * M(0,0) + v1 * M(1,0) + v2 * M(2,0) + v3 * M(3,0);
1520
u[1] = v0 * M(0,1) + v1 * M(1,1) + v2 * M(2,1) + v3 * M(3,1);
1521
u[2] = v0 * M(0,2) + v1 * M(1,2) + v2 * M(2,2) + v3 * M(3,2);
1522
u[3] = v0 * M(0,3) + v1 * M(1,3) + v2 * M(2,3) + v3 * M(3,3);