ReactOS  0.4.11-dev-721-g95bc44e
m_matrix.c File Reference
#include <precomp.h>
Include dependency graph for m_matrix.c:

Go to the source code of this file.

Macros

#define MAT_FLAG_IDENTITY   0
 
#define MAT_FLAG_GENERAL   0x1
 
#define MAT_FLAG_ROTATION   0x2
 
#define MAT_FLAG_TRANSLATION   0x4
 
#define MAT_FLAG_UNIFORM_SCALE   0x8
 
#define MAT_FLAG_GENERAL_SCALE   0x10
 
#define MAT_FLAG_GENERAL_3D   0x20
 
#define MAT_FLAG_PERSPECTIVE   0x40
 
#define MAT_FLAG_SINGULAR   0x80
 
#define MAT_DIRTY_TYPE   0x100
 
#define MAT_DIRTY_FLAGS   0x200
 
#define MAT_DIRTY_INVERSE   0x400
 
#define MAT_FLAGS_ANGLE_PRESERVING
 
#define MAT_FLAGS_GEOMETRY
 
#define MAT_FLAGS_LENGTH_PRESERVING
 
#define MAT_FLAGS_3D
 
#define MAT_DIRTY
 
#define TEST_MAT_FLAGS(mat, a)   ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)
 
#define MAT(m, r, c)   (m)[(c)*4+(r)]
 
#define M(row, col)   m[col*4+row]
 
#define M(row, col)   m[col*4+row]
 
#define M(row, col)   m[col*4+row]
 
#define M(row, col)   m[row + col*4]
 

Functions

GLboolean _math_matrix_is_length_preserving (const GLmatrix *m)
 
GLboolean _math_matrix_has_rotation (const GLmatrix *m)
 
GLboolean _math_matrix_is_general_scale (const GLmatrix *m)
 
GLboolean _math_matrix_is_dirty (const GLmatrix *m)
 
void _mesa_transform_vector (GLfloat u[4], const GLfloat v[4], const GLfloat m[16])
 
Matrix output
static void print_matrix_floats (const GLfloat m[16])
 
void _math_matrix_print (const GLmatrix *m)
 
Matrix generation
void _math_matrix_rotate (GLmatrix *mat, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
 
void _math_matrix_frustum (GLmatrix *mat, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearval, GLfloat farval)
 
void _math_matrix_ortho (GLmatrix *mat, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat nearval, GLfloat farval)
 
void _math_matrix_scale (GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z)
 
void _math_matrix_translate (GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z)
 
void _math_matrix_viewport (GLmatrix *m, GLint x, GLint y, GLint width, GLint height, GLfloat zNear, GLfloat zFar, GLfloat depthMax)
 
void _math_matrix_set_identity (GLmatrix *mat)
 
Matrix setup
void _math_matrix_copy (GLmatrix *to, const GLmatrix *from)
 
void _math_matrix_loadf (GLmatrix *mat, const GLfloat *m)
 
void _math_matrix_ctr (GLmatrix *m)
 
void _math_matrix_dtr (GLmatrix *m)
 
void _math_matrix_alloc_inv (GLmatrix *m)
 
Matrix transpose
void _math_transposef (GLfloat to[16], const GLfloat from[16])
 
void _math_transposed (GLdouble to[16], const GLdouble from[16])
 
void _math_transposefd (GLfloat to[16], const GLdouble from[16])
 

Variables

static const chartypes []
 
static GLfloat Identity [16]
 

Matrix multiplication

#define A(row, col)   a[(col<<2)+row]
 
#define B(row, col)   b[(col<<2)+row]
 
#define P(row, col)   product[(col<<2)+row]
 
static void matmul4 (GLfloat *product, const GLfloat *a, const GLfloat *b)
 
static void matmul34 (GLfloat *product, const GLfloat *a, const GLfloat *b)
 
static void matrix_multf (GLmatrix *mat, const GLfloat *m, GLuint flags)
 
void _math_matrix_mul_matrix (GLmatrix *dest, const GLmatrix *a, const GLmatrix *b)
 
void _math_matrix_mul_floats (GLmatrix *dest, const GLfloat *m)
 

Matrix inversion

#define SWAP_ROWS(a, b)   { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
 
typedef GLboolean(* inv_mat_func )(GLmatrix *mat)
 
static inv_mat_func inv_mat_tab [7]
 
static GLboolean invert_matrix_general (GLmatrix *mat)
 
static GLboolean invert_matrix_3d_general (GLmatrix *mat)
 
static GLboolean invert_matrix_3d (GLmatrix *mat)
 
static GLboolean invert_matrix_identity (GLmatrix *mat)
 
static GLboolean invert_matrix_3d_no_rot (GLmatrix *mat)
 
static GLboolean invert_matrix_2d_no_rot (GLmatrix *mat)
 
static GLboolean matrix_invert (GLmatrix *mat)
 

Matrix analysis

#define ZERO(x)   (1<<x)
 
#define ONE(x)   (1<<(x+16))
 
#define MASK_NO_TRX   (ZERO(12) | ZERO(13) | ZERO(14))
 
#define MASK_NO_2D_SCALE   ( ONE(0) | ONE(5))
 
#define MASK_IDENTITY
 
#define MASK_2D_NO_ROT
 
#define MASK_2D
 
#define MASK_3D_NO_ROT
 
#define MASK_3D
 
#define MASK_PERSPECTIVE
 
#define SQ(x)   ((x)*(x))
 
static void analyse_from_scratch (GLmatrix *mat)
 
static void analyse_from_flags (GLmatrix *mat)
 
void _math_matrix_analyse (GLmatrix *mat)
 

Detailed Description

Matrix operations.

Note
  1. 4x4 transformation matrices are stored in memory in column major order.
  2. Points/vertices are to be thought of as column vectors.
  3. Transformation of a point p by a matrix M is: p' = M * p

Definition in file m_matrix.c.

Macro Definition Documentation

#define M (   row,
  col 
)    m[col*4+row]
#define M (   row,
  col 
)    m[col*4+row]
#define M (   row,
  col 
)    m[row + col*4]
#define MASK_2D
Value:
( ZERO(8) | \
ZERO(9) | \
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define ONE(x)
Definition: m_matrix.c:1160
#define ZERO(x)
Definition: m_matrix.c:1159

Definition at line 1175 of file m_matrix.c.

#define MASK_2D_NO_ROT
Value:
( ZERO(4) | ZERO(8) | \
ZERO(1) | ZERO(9) | \
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define ONE(x)
Definition: m_matrix.c:1160
#define ZERO(x)
Definition: m_matrix.c:1159

Definition at line 1170 of file m_matrix.c.

#define MASK_3D
Value:
( \
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define ONE(x)
Definition: m_matrix.c:1160
#define ZERO(x)
Definition: m_matrix.c:1159

Definition at line 1186 of file m_matrix.c.

#define MASK_3D_NO_ROT
Value:
( ZERO(4) | ZERO(8) | \
ZERO(1) | ZERO(9) | \
ZERO(2) | ZERO(6) | \
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define ONE(x)
Definition: m_matrix.c:1160
#define ZERO(x)
Definition: m_matrix.c:1159

Definition at line 1181 of file m_matrix.c.

#define MASK_IDENTITY
Value:
( ONE(0) | ZERO(4) | ZERO(8) | ZERO(12) |\
ZERO(1) | ONE(5) | ZERO(9) | ZERO(13) |\
ZERO(2) | ZERO(6) | ONE(10) | ZERO(14) |\
ZERO(3) | ZERO(7) | ZERO(11) | ONE(15) )
#define ONE(x)
Definition: m_matrix.c:1160
#define ZERO(x)
Definition: m_matrix.c:1159

Definition at line 1165 of file m_matrix.c.

#define MASK_NO_2D_SCALE   ( ONE(0) | ONE(5))

Definition at line 1163 of file m_matrix.c.

#define MASK_NO_TRX   (ZERO(12) | ZERO(13) | ZERO(14))

Definition at line 1162 of file m_matrix.c.

#define MASK_PERSPECTIVE
Value:
( ZERO(4) | ZERO(12) |\
ZERO(1) | ZERO(13) |\
ZERO(2) | ZERO(6) | \
ZERO(3) | ZERO(7) | ZERO(15) )
#define ZERO(x)
Definition: m_matrix.c:1159

Definition at line 1192 of file m_matrix.c.

#define MAT (   m,
  r,
  c 
)    (m)[(c)*4+(r)]

References an element of 4x4 matrix.

Parameters
mmatrix array.
ccolumn of the desired element.
rrow of the desired element.
Returns
value of the desired element.

Calculate the linear storage index of the element and references it.

Definition at line 326 of file m_matrix.c.

#define ONE (   x)    (1<<(x+16))

Definition at line 1160 of file m_matrix.c.

#define SQ (   x)    ((x)*(x))

Definition at line 1197 of file m_matrix.c.

#define SWAP_ROWS (   a,
  b 
)    { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }

Swaps the values of two floating pointer variables.

Used by invert_matrix_general() to swap the row pointers.

Definition at line 338 of file m_matrix.c.

#define TEST_MAT_FLAGS (   mat,
  a 
)    ((MAT_FLAGS_GEOMETRY & (~(a)) & ((mat)->flags) ) == 0)

Test geometry related matrix flags.

Parameters
mata pointer to a GLmatrix structure.
aflags mask.
Returns
non-zero if all geometry related matrix flags are contained within the mask, or zero otherwise.

Definition at line 110 of file m_matrix.c.

#define ZERO (   x)    (1<<x)

Definition at line 1159 of file m_matrix.c.

Typedef Documentation

typedef GLboolean(* inv_mat_func)(GLmatrix *mat)

Matrix inversion function pointer type.

Definition at line 735 of file m_matrix.c.

Function Documentation

void _math_matrix_alloc_inv ( GLmatrix m)

Allocate a matrix inverse.

Parameters
mmatrix.

Allocates the matrix inverse, GLmatrix::inv, and sets it to Identity.

Definition at line 1520 of file m_matrix.c.

Referenced by init_matrix_stack().

1540 {
void _math_matrix_analyse ( GLmatrix mat)

Analyze and update a matrix.

Parameters
matmatrix.

If the matrix type is dirty then calls either analyse_from_scratch() or analyse_from_flags() to determine its type, according to whether the flags are dirty or not, respectively. If the matrix has an inverse and it's dirty then calls matrix_invert(). Finally clears the dirty flags.

Definition at line 1368 of file m_matrix.c.

Referenced by _mesa_ClipPlane(), _mesa_Lightfv(), _mesa_TexGenfv(), _mesa_update_clip_plane(), _mesa_update_modelview_project(), calculate_model_project_matrix(), update_projection(), and update_texture_matrices().

1373  {
1374  matrix_invert( mat );
1375  mat->flags &= ~MAT_DIRTY_INVERSE;
1376  }
1377 
1378  mat->flags &= ~(MAT_DIRTY_FLAGS | MAT_DIRTY_TYPE);
1379 }
1380 
static GLboolean matrix_invert(GLmatrix *mat)
Definition: m_matrix.c:769
GLuint flags
Definition: m_matrix.h:78
#define MAT_DIRTY_INVERSE
Definition: m_matrix.c:64
#define MAT_DIRTY_FLAGS
Definition: m_matrix.c:63
#define MAT_DIRTY_TYPE
Definition: m_matrix.c:62
void _math_matrix_copy ( GLmatrix to,
const GLmatrix from 
)

Copy a matrix.

Parameters
todestination matrix.
fromsource matrix.

Copies all fields in GLmatrix, creating an inverse array if necessary.

Definition at line 1442 of file m_matrix.c.

Referenced by _mesa_PushMatrix().

1444  {
1445  if (from->inv == 0) {
1446  matrix_invert( to );
1447  }
1448  else {
1449  memcpy(to->inv, from->inv, sizeof(GLfloat)*16);
1450  }
1451  }
1452 }
1453 
static GLboolean matrix_invert(GLmatrix *mat)
Definition: m_matrix.c:769
GLfloat * inv
Definition: m_matrix.h:77
float GLfloat
Definition: gl.h:161
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
void _math_matrix_ctr ( GLmatrix m)

Matrix constructor.

Parameters
mmatrix.

Initialize the GLmatrix fields.

Definition at line 1482 of file m_matrix.c.

Referenced by _mesa_init_matrix(), _mesa_init_viewport(), and init_matrix_stack().

1497 {
void _math_matrix_dtr ( GLmatrix m)

Matrix destructor.

Parameters
mmatrix.

Frees the data in a GLmatrix.

Definition at line 1500 of file m_matrix.c.

Referenced by _mesa_free_matrix_data(), _mesa_free_viewport_data(), and free_matrix_stack().

1502  {
1503  _mesa_align_free( m->inv );
1504  m->inv = NULL;
1505  }
1506 }
1507 
GLfloat * inv
Definition: m_matrix.h:77
smooth NULL
Definition: ftsmooth.c:416
void _mesa_align_free(void *ptr)
Definition: imports.c:173
void _math_matrix_frustum ( GLmatrix mat,
GLfloat  left,
GLfloat  right,
GLfloat  bottom,
GLfloat  top,
GLfloat  nearval,
GLfloat  farval 
)

Apply a perspective projection matrix.

Parameters
matmatrix to apply the projection.
leftleft clipping plane coordinate.
rightright clipping plane coordinate.
bottombottom clipping plane coordinate.
toptop clipping plane coordinate.
nearvaldistance to the near clipping plane.
farvaldistance to the far clipping plane.

Creates the projection matrix and multiplies it with mat, marking the MAT_FLAG_PERSPECTIVE flag.

Definition at line 981 of file m_matrix.c.

Referenced by _mesa_Frustum().

981 {
982  GLfloat x, y, a, b, c, d;
983  GLfloat m[16];
984 
985  x = (2.0F*nearval) / (right-left);
986  y = (2.0F*nearval) / (top-bottom);
987  a = (right+left) / (right-left);
988  b = (top+bottom) / (top-bottom);
989  c = -(farval+nearval) / ( farval-nearval);
990  d = -(2.0F*farval*nearval) / (farval-nearval); /* error? */
991 
992 #define M(row,col) m[col*4+row]
993  M(0,0) = x; M(0,1) = 0.0F; M(0,2) = a; M(0,3) = 0.0F;
994  M(1,0) = 0.0F; M(1,1) = y; M(1,2) = b; M(1,3) = 0.0F;
995  M(2,0) = 0.0F; M(2,1) = 0.0F; M(2,2) = c; M(2,3) = d;
996  M(3,0) = 0.0F; M(3,1) = 0.0F; M(3,2) = -1.0F; M(3,3) = 0.0F;
997 #undef M
998 
1000 }
1001 
static void matrix_multf(GLmatrix *mat, const GLfloat *m, GLuint flags)
Definition: m_matrix.c:213
#define a
Definition: ke_i.h:78
#define M(row, col)
GLint GLint bottom
Definition: glext.h:7726
#define b
Definition: ke_i.h:79
float GLfloat
Definition: gl.h:161
#define d
Definition: ke_i.h:81
GLint left
Definition: glext.h:7726
GLdouble GLdouble right
Definition: glext.h:10859
#define MAT_FLAG_PERSPECTIVE
Definition: m_matrix.c:60
const GLfloat * m
Definition: glext.h:10848
#define c
Definition: ke_i.h:80
GLdouble GLdouble GLdouble GLdouble top
Definition: glext.h:10859
INT INT y
Definition: msvc.h:62
INT x
Definition: msvc.h:62
GLboolean _math_matrix_has_rotation ( const GLmatrix m)

Test if the given matrix does any rotation. (or perhaps if the upper-left 3x3 is non-identity)

Definition at line 1403 of file m_matrix.c.

Referenced by validate_normal_stage().

1413 {
GLboolean _math_matrix_is_dirty ( const GLmatrix m)
GLboolean _math_matrix_is_general_scale ( const GLmatrix m)

Definition at line 1416 of file m_matrix.c.

Referenced by run_normal_stage().

1420 {
GLboolean _math_matrix_is_length_preserving ( const GLmatrix m)

Test if the given matrix preserves vector lengths.

Definition at line 1392 of file m_matrix.c.

Referenced by _mesa_update_tnl_spaces(), and update_modelview_scale().

1400 {
void _math_matrix_loadf ( GLmatrix mat,
const GLfloat m 
)

Loads a matrix array into GLmatrix.

Parameters
mmatrix array.
matmatrix.

Copies m into GLmatrix::m and marks the MAT_FLAG_GENERAL and MAT_DIRTY flags.

Definition at line 1468 of file m_matrix.c.

Referenced by _mesa_LoadMatrixf().

1479 {
void _math_matrix_mul_floats ( GLmatrix dest,
const GLfloat m 
)

Matrix multiplication.

Parameters
destleft and destination matrix.
mright matrix array.

Marks the matrix flags with general flag, and type and inverse dirty flags. Calls matmul4() for the multiplication.

Definition at line 257 of file m_matrix.c.

Referenced by _mesa_MultMatrixf().

278 {
void _math_matrix_mul_matrix ( GLmatrix dest,
const GLmatrix a,
const GLmatrix b 
)

Matrix multiplication.

Parameters
destdestination matrix.
aleft matrix.
bright matrix.

Joins both flags and marks the type and inverse as dirty. Calls matmul34() if both matrices are 3D, or matmul4() otherwise.

Definition at line 234 of file m_matrix.c.

Referenced by calculate_model_project_matrix().

254 {
void _math_matrix_ortho ( GLmatrix mat,
GLfloat  left,
GLfloat  right,
GLfloat  bottom,
GLfloat  top,
GLfloat  nearval,
GLfloat  farval 
)

Apply an orthographic projection matrix.

Parameters
matmatrix to apply the projection.
leftleft clipping plane coordinate.
rightright clipping plane coordinate.
bottombottom clipping plane coordinate.
toptop clipping plane coordinate.
nearvaldistance to the near clipping plane.
farvaldistance to the far clipping plane.

Creates the projection matrix and multiplies it with mat, marking the MAT_FLAG_GENERAL_SCALE and MAT_FLAG_TRANSLATION flags.

Definition at line 1021 of file m_matrix.c.

Referenced by _mesa_Ortho().

1021 {
1022  GLfloat m[16];
1023 
1024 #define M(row,col) m[col*4+row]
1025  M(0,0) = 2.0F / (right-left);
1026  M(0,1) = 0.0F;
1027  M(0,2) = 0.0F;
1028  M(0,3) = -(right+left) / (right-left);
1029 
1030  M(1,0) = 0.0F;
1031  M(1,1) = 2.0F / (top-bottom);
1032  M(1,2) = 0.0F;
1033  M(1,3) = -(top+bottom) / (top-bottom);
1034 
1035  M(2,0) = 0.0F;
1036  M(2,1) = 0.0F;
1037  M(2,2) = -2.0F / (farval-nearval);
1038  M(2,3) = -(farval+nearval) / (farval-nearval);
1039 
1040  M(3,0) = 0.0F;
1041  M(3,1) = 0.0F;
1042  M(3,2) = 0.0F;
1043  M(3,3) = 1.0F;
1044 #undef M
1045 
1047 }
1048 
#define MAT_FLAG_GENERAL_SCALE
Definition: m_matrix.c:58
#define MAT_FLAG_TRANSLATION
Definition: m_matrix.c:56
static void matrix_multf(GLmatrix *mat, const GLfloat *m, GLuint flags)
Definition: m_matrix.c:213
#define M(row, col)
GLint GLint bottom
Definition: glext.h:7726
float GLfloat
Definition: gl.h:161
GLint left
Definition: glext.h:7726
GLdouble GLdouble right
Definition: glext.h:10859
const GLfloat * m
Definition: glext.h:10848
GLdouble GLdouble GLdouble GLdouble top
Definition: glext.h:10859
void _math_matrix_print ( const GLmatrix m)

Dumps the contents of a GLmatrix structure.

Parameters
mpointer to the GLmatrix structure.

Definition at line 295 of file m_matrix.c.

295  : \n");
296  if (m->inv) {
297  GLfloat prod[16];
298  print_matrix_floats(m->inv);
299  matmul4(prod, m->m, m->inv);
300  _mesa_debug(NULL, "Mat * Inverse:\n");
301  print_matrix_floats(prod);
302  }
303  else {
304  _mesa_debug(NULL, " - not available\n");
305  }
306 }
307 
GLuint n
Definition: s_context.h:57
void _math_matrix_rotate ( GLmatrix mat,
GLfloat  angle,
GLfloat  x,
GLfloat  y,
GLfloat  z 
)

Generate a 4x4 transformation matrix from glRotate parameters, and post-multiply the input matrix by it.

Author
This function was contributed by Erich Boleyn (erich.nosp@m.@uru.nosp@m.k.org). Optimizations contributed by Rudolf Opalla (rudi@.nosp@m.khm..nosp@m.de).

Definition at line 797 of file m_matrix.c.

Referenced by _mesa_Rotatef().

808  {
809  if (y == 0.0F) {
810  if (z != 0.0F) {
811  optimized = GL_TRUE;
812  /* rotate only around z-axis */
813  M(0,0) = c;
814  M(1,1) = c;
815  if (z < 0.0F) {
816  M(0,1) = s;
817  M(1,0) = -s;
818  }
819  else {
820  M(0,1) = -s;
821  M(1,0) = s;
822  }
823  }
824  }
825  else if (z == 0.0F) {
826  optimized = GL_TRUE;
827  /* rotate only around y-axis */
828  M(0,0) = c;
829  M(2,2) = c;
830  if (y < 0.0F) {
831  M(0,2) = -s;
832  M(2,0) = s;
833  }
834  else {
835  M(0,2) = s;
836  M(2,0) = -s;
837  }
838  }
839  }
840  else if (y == 0.0F) {
841  if (z == 0.0F) {
842  optimized = GL_TRUE;
843  /* rotate only around x-axis */
844  M(1,1) = c;
845  M(2,2) = c;
846  if (x < 0.0F) {
847  M(1,2) = s;
848  M(2,1) = -s;
849  }
850  else {
851  M(1,2) = -s;
852  M(2,1) = s;
853  }
854  }
855  }
856 
857  if (!optimized) {
858  const GLfloat mag = SQRTF(x * x + y * y + z * z);
859 
860  if (mag <= 1.0e-4) {
861  /* no rotation, leave mat as-is */
862  return;
863  }
864 
865  x /= mag;
866  y /= mag;
867  z /= mag;
868 
869 
870  /*
871  * Arbitrary axis rotation matrix.
872  *
873  * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
874  * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
875  * (which is about the X-axis), and the two composite transforms
876  * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
877  * from the arbitrary axis to the X-axis then back. They are
878  * all elementary rotations.
879  *
880  * Rz' is a rotation about the Z-axis, to bring the axis vector
881  * into the x-z plane. Then Ry' is applied, rotating about the
882  * Y-axis to bring the axis vector parallel with the X-axis. The
883  * rotation about the X-axis is then performed. Ry and Rz are
884  * simply the respective inverse transforms to bring the arbitrary
885  * axis back to its original orientation. The first transforms
886  * Rz' and Ry' are considered inverses, since the data from the
887  * arbitrary axis gives you info on how to get to it, not how
888  * to get away from it, and an inverse must be applied.
889  *
890  * The basic calculation used is to recognize that the arbitrary
891  * axis vector (x, y, z), since it is of unit length, actually
892  * represents the sines and cosines of the angles to rotate the
893  * X-axis to the same orientation, with theta being the angle about
894  * Z and phi the angle about Y (in the order described above)
895  * as follows:
896  *
897  * cos ( theta ) = x / sqrt ( 1 - z^2 )
898  * sin ( theta ) = y / sqrt ( 1 - z^2 )
899  *
900  * cos ( phi ) = sqrt ( 1 - z^2 )
901  * sin ( phi ) = z
902  *
903  * Note that cos ( phi ) can further be inserted to the above
904  * formulas:
905  *
906  * cos ( theta ) = x / cos ( phi )
907  * sin ( theta ) = y / sin ( phi )
908  *
909  * ...etc. Because of those relations and the standard trigonometric
910  * relations, it is pssible to reduce the transforms down to what
911  * is used below. It may be that any primary axis chosen will give the
912  * same results (modulo a sign convention) using thie method.
913  *
914  * Particularly nice is to notice that all divisions that might
915  * have caused trouble when parallel to certain planes or
916  * axis go away with care paid to reducing the expressions.
917  * After checking, it does perform correctly under all cases, since
918  * in all the cases of division where the denominator would have
919  * been zero, the numerator would have been zero as well, giving
920  * the expected result.
921  */
922 
923  xx = x * x;
924  yy = y * y;
925  zz = z * z;
926  xy = x * y;
927  yz = y * z;
928  zx = z * x;
929  xs = x * s;
930  ys = y * s;
931  zs = z * s;
932  one_c = 1.0F - c;
933 
934  /* We already hold the identity-matrix so we can skip some statements */
935  M(0,0) = (one_c * xx) + c;
936  M(0,1) = (one_c * xy) - zs;
937  M(0,2) = (one_c * zx) + ys;
938 /* M(0,3) = 0.0F; */
939 
940  M(1,0) = (one_c * xy) + zs;
941  M(1,1) = (one_c * yy) + c;
942  M(1,2) = (one_c * yz) - xs;
943 /* M(1,3) = 0.0F; */
944 
945  M(2,0) = (one_c * zx) - ys;
946  M(2,1) = (one_c * yz) + xs;
947  M(2,2) = (one_c * zz) + c;
948 /* M(2,3) = 0.0F; */
949 
950 /*
951  M(3,0) = 0.0F;
952  M(3,1) = 0.0F;
953  M(3,2) = 0.0F;
954  M(3,3) = 1.0F;
955 */
956  }
957 #undef M
958 
960 }
961 
static void matrix_multf(GLmatrix *mat, const GLfloat *m, GLuint flags)
Definition: m_matrix.c:213
#define e
Definition: ke_i.h:82
GLdouble GLdouble z
Definition: glext.h:5874
#define M(row, col)
float GLfloat
Definition: gl.h:161
const GLubyte * c
Definition: glext.h:8905
int xx
Definition: npserver.c:29
#define MAT_FLAG_ROTATION
Definition: m_matrix.c:55
GLdouble s
Definition: gl.h:2039
const GLfloat * m
Definition: glext.h:10848
#define GL_TRUE
Definition: gl.h:174
#define c
Definition: ke_i.h:80
#define SQRTF(X)
Definition: imports.h:105
INT INT y
Definition: msvc.h:62
#define F(x, y, z)
Definition: md5.c:51
INT x
Definition: msvc.h:62
void _math_matrix_scale ( GLmatrix mat,
GLfloat  x,
GLfloat  y,
GLfloat  z 
)

Multiply a matrix with a general scaling matrix.

Parameters
matmatrix.
xx axis scale factor.
yy axis scale factor.
zz axis scale factor.

Multiplies in-place the elements of mat by the scale factors. Checks if the scales factors are roughly the same, marking the MAT_FLAG_UNIFORM_SCALE flag, or MAT_FLAG_GENERAL_SCALE. Marks the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE dirty flags.

Definition at line 1067 of file m_matrix.c.

Referenced by _mesa_Scalef().

1094 {
void _math_matrix_set_identity ( GLmatrix mat)

Set a matrix to the identity matrix.

Parameters
matmatrix.

Copies Identity into GLmatrix::m, and into GLmatrix::inv if not NULL. Sets the matrix type to identity, and clear the dirty flags.

Definition at line 1139 of file m_matrix.c.

Referenced by _mesa_LoadIdentity().

1203 {
void _math_matrix_translate ( GLmatrix mat,
GLfloat  x,
GLfloat  y,
GLfloat  z 
)

Multiply a matrix with a translation matrix.

Parameters
matmatrix.
xtranslation vector x coordinate.
ytranslation vector y coordinate.
ztranslation vector z coordinate.

Adds the translation coordinates to the elements of mat in-place. Marks the MAT_FLAG_TRANSLATION flag, and the MAT_DIRTY_TYPE and MAT_DIRTY_INVERSE dirty flags.

Definition at line 1097 of file m_matrix.c.

Referenced by _mesa_Translatef().

1114 {
void _math_matrix_viewport ( GLmatrix m,
GLint  x,
GLint  y,
GLint  width,
GLint  height,
GLfloat  zNear,
GLfloat  zFar,
GLfloat  depthMax 
)

Set matrix to do viewport and depthrange mapping. Transforms Normalized Device Coords to window/Z values.

Definition at line 1116 of file m_matrix.c.

Referenced by _mesa_DepthRange(), _mesa_init_viewport(), _mesa_set_viewport(), and update_viewport_matrix().

1136 {
void _math_transposed ( GLdouble  to[16],
const GLdouble  from[16] 
)

Transpose a GLdouble matrix.

Parameters
todestination array.
fromsource array.

Definition at line 1570 of file m_matrix.c.

1594 {
void _math_transposef ( GLfloat  to[16],
const GLfloat  from[16] 
)

Transpose a GLfloat matrix.

Parameters
todestination array.
fromsource array.

Definition at line 1543 of file m_matrix.c.

1567 {
void _math_transposefd ( GLfloat  to[16],
const GLdouble  from[16] 
)

Transpose a GLdouble matrix and convert to GLfloat.

Parameters
todestination array.
fromsource array.

Definition at line 1597 of file m_matrix.c.

1627 {
void _mesa_transform_vector ( GLfloat  u[4],
const GLfloat  v[4],
const GLfloat  m[16] 
)

Transform a 4-element row vector (1x4 matrix) by a 4x4 matrix. This function is used for transforming clipping plane equations and spotlight directions. Mathematically, u = v * m. Input: v - input vector m - transformation matrix Output: u - transformed vector

Definition at line 1630 of file m_matrix.c.

Referenced by _mesa_ClipPlane(), _mesa_TexGenfv(), _mesa_update_clip_plane(), and update_projection().

static void analyse_from_flags ( GLmatrix mat)
static

Analyze a matrix given that its flags are accurate.

This is the more common operation, hopefully.

Definition at line 1319 of file m_matrix.c.

1319  {
1320  mat->type = MATRIX_IDENTITY;
1321  }
1322  else if (TEST_MAT_FLAGS(mat, (MAT_FLAG_TRANSLATION |
1325  if ( m[10]==1.0F && m[14]==0.0F ) {
1326  mat->type = MATRIX_2D_NO_ROT;
1327  }
1328  else {
1329  mat->type = MATRIX_3D_NO_ROT;
1330  }
1331  }
1332  else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
1333  if ( m[ 8]==0.0F
1334  && m[ 9]==0.0F
1335  && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
1336  mat->type = MATRIX_2D;
1337  }
1338  else {
1339  mat->type = MATRIX_3D;
1340  }
1341  }
1342  else if ( m[4]==0.0F && m[12]==0.0F
1343  && m[1]==0.0F && m[13]==0.0F
1344  && m[2]==0.0F && m[6]==0.0F
1345  && m[3]==0.0F && m[7]==0.0F && m[11]==-1.0F && m[15]==0.0F) {
1346  mat->type = MATRIX_PERSPECTIVE;
1347  }
1348  else {
1349  mat->type = MATRIX_GENERAL;
1350  }
1351 }
1352 
#define MAT_FLAG_GENERAL_SCALE
Definition: m_matrix.c:58
#define MAT_FLAG_TRANSLATION
Definition: m_matrix.c:56
enum GLmatrixtype type
Definition: m_matrix.h:81
#define MAT_FLAGS_3D
Definition: m_matrix.c:87
#define TEST_MAT_FLAGS(mat, a)
Definition: m_matrix.c:110
const GLfloat * m
Definition: glext.h:10848
#define MAT_FLAG_UNIFORM_SCALE
Definition: m_matrix.c:57
#define F(x, y, z)
Definition: md5.c:51
static void analyse_from_scratch ( GLmatrix mat)
static

Determine type and flags from scratch.

Parameters
matmatrix.

This is expensive enough to only want to do it once.

Definition at line 1206 of file m_matrix.c.

1208  {
1209  if (m[i] == 0.0) mask |= (1<<i);
1210  }
1211 
1212  if (m[0] == 1.0F) mask |= (1<<16);
1213  if (m[5] == 1.0F) mask |= (1<<21);
1214  if (m[10] == 1.0F) mask |= (1<<26);
1215  if (m[15] == 1.0F) mask |= (1<<31);
1216 
1217  mat->flags &= ~MAT_FLAGS_GEOMETRY;
1218 
1219  /* Check for translation - no-one really cares
1220  */
1221  if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
1222  mat->flags |= MAT_FLAG_TRANSLATION;
1223 
1224  /* Do the real work
1225  */
1226  if (mask == (GLuint) MASK_IDENTITY) {
1227  mat->type = MATRIX_IDENTITY;
1228  }
1229  else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
1230  mat->type = MATRIX_2D_NO_ROT;
1231 
1233  mat->flags |= MAT_FLAG_GENERAL_SCALE;
1234  }
1235  else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
1236  GLfloat mm = DOT2(m, m);
1237  GLfloat m4m4 = DOT2(m+4,m+4);
1238  GLfloat mm4 = DOT2(m,m+4);
1239 
1240  mat->type = MATRIX_2D;
1241 
1242  /* Check for scale */
1243  if (SQ(mm-1) > SQ(1e-6) ||
1244  SQ(m4m4-1) > SQ(1e-6))
1245  mat->flags |= MAT_FLAG_GENERAL_SCALE;
1246 
1247  /* Check for rotation */
1248  if (SQ(mm4) > SQ(1e-6))
1249  mat->flags |= MAT_FLAG_GENERAL_3D;
1250  else
1251  mat->flags |= MAT_FLAG_ROTATION;
1252 
1253  }
1254  else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
1255  mat->type = MATRIX_3D_NO_ROT;
1256 
1257  /* Check for scale */
1258  if (SQ(m[0]-m[5]) < SQ(1e-6) &&
1259  SQ(m[0]-m[10]) < SQ(1e-6)) {
1260  if (SQ(m[0]-1.0) > SQ(1e-6)) {
1261  mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1262  }
1263  }
1264  else {
1265  mat->flags |= MAT_FLAG_GENERAL_SCALE;
1266  }
1267  }
1268  else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
1269  GLfloat c1 = DOT3(m,m);
1270  GLfloat c2 = DOT3(m+4,m+4);
1271  GLfloat c3 = DOT3(m+8,m+8);
1272  GLfloat d1 = DOT3(m, m+4);
1273  GLfloat cp[3];
1274 
1275  mat->type = MATRIX_3D;
1276 
1277  /* Check for scale */
1278  if (SQ(c1-c2) < SQ(1e-6) && SQ(c1-c3) < SQ(1e-6)) {
1279  if (SQ(c1-1.0) > SQ(1e-6))
1280  mat->flags |= MAT_FLAG_UNIFORM_SCALE;
1281  /* else no scale at all */
1282  }
1283  else {
1284  mat->flags |= MAT_FLAG_GENERAL_SCALE;
1285  }
1286 
1287  /* Check for rotation */
1288  if (SQ(d1) < SQ(1e-6)) {
1289  CROSS3( cp, m, m+4 );
1290  SUB_3V( cp, cp, (m+8) );
1291  if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
1292  mat->flags |= MAT_FLAG_ROTATION;
1293  else
1294  mat->flags |= MAT_FLAG_GENERAL_3D;
1295  }
1296  else {
1297  mat->flags |= MAT_FLAG_GENERAL_3D; /* shear, etc */
1298  }
1299  }
1300  else if ((mask & MASK_PERSPECTIVE) == MASK_PERSPECTIVE && m[11]==-1.0F) {
1301  mat->type = MATRIX_PERSPECTIVE;
1302  mat->flags |= MAT_FLAG_GENERAL;
1303  }
1304  else {
1305  mat->type = MATRIX_GENERAL;
1306  mat->flags |= MAT_FLAG_GENERAL;
1307  }
1308 }
1309 
#define MASK_PERSPECTIVE
Definition: m_matrix.c:1192
#define MAT_FLAG_GENERAL_SCALE
Definition: m_matrix.c:58
#define MAT_FLAG_TRANSLATION
Definition: m_matrix.c:56
#define MASK_3D
Definition: m_matrix.c:1186
enum GLmatrixtype type
Definition: m_matrix.h:81
#define MASK_NO_2D_SCALE
Definition: m_matrix.c:1163
#define SQ(x)
Definition: m_matrix.c:1197
#define MASK_NO_TRX
Definition: m_matrix.c:1162
GLenum GLclampf GLint i
Definition: glfuncs.h:14
GLenum GLint GLuint mask
Definition: glext.h:6028
#define e
Definition: ke_i.h:82
#define MAT_FLAG_GENERAL_3D
Definition: m_matrix.c:59
#define MASK_2D_NO_ROT
Definition: m_matrix.c:1170
float GLfloat
Definition: gl.h:161
ecx edi ebx edx edi decl ecx esi eax jecxz decl eax andl eax esi movl edx movl TEMP incl eax andl eax ecx incl ebx eax jnz xchgl ecx incl TEMP esp ecx subl ebx pushl ecx ecx edx ecx ecx mm0 mm4 mm0 mm4 mm1 mm5 mm1 mm5 mm2 mm6 mm2 mm6 mm3 mm7 mm3 mm7 paddd mm0 paddd mm4 paddd mm0 paddd mm4 paddd mm0 paddd mm4 movq mm1 movq mm4
Definition: synth_sse3d.h:133
#define CROSS3(n, u, v)
Definition: macros.h:645
#define MASK_3D_NO_ROT
Definition: m_matrix.c:1181
#define MASK_2D
Definition: m_matrix.c:1175
if(!(yy_init))
Definition: macro.lex.yy.c:717
#define MAT_FLAG_ROTATION
Definition: m_matrix.c:55
#define DOT3(a, b)
Definition: macros.h:637
#define MAT_FLAG_GENERAL
Definition: m_matrix.c:54
const GLfloat * m
Definition: glext.h:10848
GLuint flags
Definition: m_matrix.h:78
#define MASK_IDENTITY
Definition: m_matrix.c:1165
unsigned int GLuint
Definition: gl.h:159
#define DOT2(a, b)
Definition: macros.h:634
#define LEN_SQUARED_3FV(V)
Definition: macros.h:668
#define MAT_FLAG_UNIFORM_SCALE
Definition: m_matrix.c:57
#define SUB_3V(DST, SRCA, SRCB)
Definition: macros.h:395
POINT cp
Definition: magnifier.c:60
#define MAT_FLAGS_GEOMETRY
Definition: m_matrix.c:72
#define F(x, y, z)
Definition: md5.c:51
static GLboolean invert_matrix_2d_no_rot ( GLmatrix mat)
static

Compute inverse of a no-rotation 2d transformation matrix.

Parameters
matpointer to a GLmatrix structure. The matrix inverse will be stored in the GLmatrix::inv attribute.
Returns
GL_TRUE for success, GL_FALSE for failure (singular matrix).

Calculates the inverse matrix by applying the inverse scaling and translation to the identity matrix.

Definition at line 684 of file m_matrix.c.

692  {
693  MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
694  MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
695  }
696 
697  return GL_TRUE;
698 }
699 
700 #if 0
701 /* broken */
702 static GLboolean invert_matrix_perspective( GLmatrix *mat )
unsigned char GLboolean
Definition: gl.h:151
static FILE * out
Definition: regtests2xml.c:44
#define MAT(m, r, c)
Definition: m_matrix.c:326
GLfloat CONST GLvector4f * in
Definition: m_xform.h:122
#define GL_TRUE
Definition: gl.h:174
static GLboolean invert_matrix_3d ( GLmatrix mat)
static

Compute inverse of a 3d transformation matrix.

Parameters
matpointer to a GLmatrix structure. The matrix inverse will be stored in the GLmatrix::inv attribute.
Returns
GL_TRUE for success, GL_FALSE for failure (singular matrix).

If the matrix is not an angle preserving matrix then calls invert_matrix_3d_general for the actual calculation. Otherwise calculates the inverse matrix analyzing and inverting each of the scaling, rotation and translation parts.

Definition at line 555 of file m_matrix.c.

556  {
557  return invert_matrix_3d_general( mat );
558  }
559 
560  if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
561  GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
562  MAT(in,0,1) * MAT(in,0,1) +
563  MAT(in,0,2) * MAT(in,0,2));
564 
565  if (scale == 0.0)
566  return GL_FALSE;
567 
568  scale = 1.0F / scale;
569 
570  /* Transpose and scale the 3 by 3 upper-left submatrix. */
571  MAT(out,0,0) = scale * MAT(in,0,0);
572  MAT(out,1,0) = scale * MAT(in,0,1);
573  MAT(out,2,0) = scale * MAT(in,0,2);
574  MAT(out,0,1) = scale * MAT(in,1,0);
575  MAT(out,1,1) = scale * MAT(in,1,1);
576  MAT(out,2,1) = scale * MAT(in,1,2);
577  MAT(out,0,2) = scale * MAT(in,2,0);
578  MAT(out,1,2) = scale * MAT(in,2,1);
579  MAT(out,2,2) = scale * MAT(in,2,2);
580  }
581  else if (mat->flags & MAT_FLAG_ROTATION) {
582  /* Transpose the 3 by 3 upper-left submatrix. */
583  MAT(out,0,0) = MAT(in,0,0);
584  MAT(out,1,0) = MAT(in,0,1);
585  MAT(out,2,0) = MAT(in,0,2);
586  MAT(out,0,1) = MAT(in,1,0);
587  MAT(out,1,1) = MAT(in,1,1);
588  MAT(out,2,1) = MAT(in,1,2);
589  MAT(out,0,2) = MAT(in,2,0);
590  MAT(out,1,2) = MAT(in,2,1);
591  MAT(out,2,2) = MAT(in,2,2);
592  }
593  else {
594  /* pure translation */
595  memcpy( out, Identity, sizeof(Identity) );
596  MAT(out,0,3) = - MAT(in,0,3);
597  MAT(out,1,3) = - MAT(in,1,3);
598  MAT(out,2,3) = - MAT(in,2,3);
599  return GL_TRUE;
600  }
601 
602  if (mat->flags & MAT_FLAG_TRANSLATION) {
603  /* Do the translation part */
604  MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
605  MAT(in,1,3) * MAT(out,0,1) +
606  MAT(in,2,3) * MAT(out,0,2) );
607  MAT(out,1,3) = - (MAT(in,0,3) * MAT(out,1,0) +
608  MAT(in,1,3) * MAT(out,1,1) +
609  MAT(in,2,3) * MAT(out,1,2) );
610  MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
611  MAT(in,1,3) * MAT(out,2,1) +
612  MAT(in,2,3) * MAT(out,2,2) );
613  }
614  else {
615  MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
616  }
617 
618  return GL_TRUE;
619 }
620 
#define GL_FALSE
Definition: gl.h:173
#define MAT_FLAG_TRANSLATION
Definition: m_matrix.c:56
static GLboolean invert_matrix_3d_general(GLmatrix *mat)
Definition: m_matrix.c:483
GLfloat scale
Definition: m_xform.h:122
float GLfloat
Definition: gl.h:161
static FILE * out
Definition: regtests2xml.c:44
static GLfloat Identity[16]
Definition: m_matrix.c:132
#define MAT_FLAG_ROTATION
Definition: m_matrix.c:55
#define MAT(m, r, c)
Definition: m_matrix.c:326
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLfloat CONST GLvector4f * in
Definition: m_xform.h:122
GLuint flags
Definition: m_matrix.h:78
#define GL_TRUE
Definition: gl.h:174
#define MAT_FLAG_UNIFORM_SCALE
Definition: m_matrix.c:57
static GLboolean invert_matrix_3d_general ( GLmatrix mat)
static

Compute inverse of a general 3d transformation matrix.

Parameters
matpointer to a GLmatrix structure. The matrix inverse will be stored in the GLmatrix::inv attribute.
Returns
GL_TRUE for success, GL_FALSE for failure (singular matrix).
Author
Adapted from graphics gems II.

Calculates the inverse of the upper left by first calculating its determinant and multiplying it to the symmetric adjust matrix of each element. Finally deals with the translation part by transforming the original translation vector using by the calculated submatrix inverse.

Definition at line 483 of file m_matrix.c.

Referenced by invert_matrix_3d().

552 {
static GLboolean invert_matrix_3d_no_rot ( GLmatrix mat)
static

Compute inverse of a no-rotation 3d transformation matrix.

Parameters
matpointer to a GLmatrix structure. The matrix inverse will be stored in the GLmatrix::inv attribute.
Returns
GL_TRUE for success, GL_FALSE for failure (singular matrix).

Calculates the

Definition at line 651 of file m_matrix.c.

660  {
661  MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
662  MAT(out,1,3) = - (MAT(in,1,3) * MAT(out,1,1));
663  MAT(out,2,3) = - (MAT(in,2,3) * MAT(out,2,2));
664  }
665 
666  return GL_TRUE;
667 }
668 
static FILE * out
Definition: regtests2xml.c:44
#define MAT(m, r, c)
Definition: m_matrix.c:326
GLfloat CONST GLvector4f * in
Definition: m_xform.h:122
#define GL_TRUE
Definition: gl.h:174
static GLboolean invert_matrix_general ( GLmatrix mat)
static

Compute inverse of 4x4 transformation matrix.

Parameters
matpointer to a GLmatrix structure. The matrix inverse will be stored in the GLmatrix::inv attribute.
Returns
GL_TRUE for success, GL_FALSE for failure (singular matrix).
Author
Code contributed by Jacques Leroy jle@s.nosp@m.tar..nosp@m.be

Calculates the inverse matrix by performing the gaussian matrix reduction with partial pivoting followed by back/substitution with the loops manually unrolled.

Definition at line 355 of file m_matrix.c.

389  { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
390  s = r0[5];
391  if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
392  s = r0[6];
393  if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
394  s = r0[7];
395  if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
396 
397  /* choose pivot - or die */
398  if (FABSF(r3[1])>FABSF(r2[1])) SWAP_ROWS(r3, r2);
399  if (FABSF(r2[1])>FABSF(r1[1])) SWAP_ROWS(r2, r1);
400  if (0.0 == r1[1]) return GL_FALSE;
401 
402  /* eliminate second variable */
403  m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
404  r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
405  r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
406  s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
407  s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
408  s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
409  s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
410 
411  /* choose pivot - or die */
412  if (FABSF(r3[2])>FABSF(r2[2])) SWAP_ROWS(r3, r2);
413  if (0.0 == r2[2]) return GL_FALSE;
414 
415  /* eliminate third variable */
416  m3 = r3[2]/r2[2];
417  r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
418  r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
419  r3[7] -= m3 * r2[7];
420 
421  /* last check */
422  if (0.0 == r3[3]) return GL_FALSE;
423 
424  s = 1.0F/r3[3]; /* now back substitute row 3 */
425  r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
426 
427  m2 = r2[3]; /* now back substitute row 2 */
428  s = 1.0F/r2[2];
429  r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
430  r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
431  m1 = r1[3];
432  r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
433  r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
434  m0 = r0[3];
435  r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
436  r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
437 
438  m1 = r1[2]; /* now back substitute row 1 */
439  s = 1.0F/r1[1];
440  r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
441  r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
442  m0 = r0[2];
443  r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
444  r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
445 
446  m0 = r0[1]; /* now back substitute row 0 */
447  s = 1.0F/r0[0];
448  r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
449  r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
450 
451  MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
452  MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
453  MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
454  MAT(out,1,2) = r1[6]; MAT(out,1,3) = r1[7],
455  MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
456  MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
457  MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
458  MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
459 
460  return GL_TRUE;
461 }
462 #undef SWAP_ROWS
463 
static DNS_RECORDW r3
Definition: record.c:39
#define GL_FALSE
Definition: gl.h:173
#define FABSF(x)
Definition: imports.h:280
static DNS_RECORDW r1
Definition: record.c:37
static DNS_RECORDW r2
Definition: record.c:38
static FILE * out
Definition: regtests2xml.c:44
#define MAT(m, r, c)
Definition: m_matrix.c:326
GLdouble s
Definition: gl.h:2039
#define GL_TRUE
Definition: gl.h:174
#define SWAP_ROWS(a, b)
Definition: m_matrix.c:338
static GLboolean invert_matrix_identity ( GLmatrix mat)
static

Compute inverse of an identity transformation matrix.

Parameters
matpointer to a GLmatrix structure. The matrix inverse will be stored in the GLmatrix::inv attribute.
Returns
always GL_TRUE.

Simply copies Identity into GLmatrix::inv.

Definition at line 635 of file m_matrix.c.

648 {
static void matmul34 ( GLfloat product,
const GLfloat a,
const GLfloat b 
)
static

Multiply two matrices known to occupy only the top three rows, such as typical model matrices, and orthogonal matrices.

Parameters
amatrix.
bmatrix.
productwill receive the product of a and b.

Definition at line 182 of file m_matrix.c.

210 {
static void matmul4 ( GLfloat product,
const GLfloat a,
const GLfloat b 
)
static

Perform a full 4x4 matrix multiplication.

Parameters
amatrix.
bmatrix.
productwill receive the product of a and b.
Warning
Is assumed that product != b. product == a is allowed.
Note
KW: 4*16 = 64 multiplications
Author
This matmul was contributed by Thomas Malik

Definition at line 162 of file m_matrix.c.

179 {
static GLboolean matrix_invert ( GLmatrix mat)
static

Compute inverse of a transformation matrix.

Parameters
matpointer to a GLmatrix structure. The matrix inverse will be stored in the GLmatrix::inv attribute.
Returns
GL_TRUE for success, GL_FALSE for failure (singular matrix).

Calls the matrix inversion function in inv_mat_tab corresponding to the given matrix type. In case of failure, updates the MAT_FLAG_SINGULAR flag, and copies the identity matrix into GLmatrix::inv.

Definition at line 769 of file m_matrix.c.

Referenced by _math_matrix_copy().

770  {
771  mat->flags |= MAT_FLAG_SINGULAR;
772  memcpy( mat->inv, Identity, sizeof(Identity) );
773  return GL_FALSE;
774  }
775 }
776 
#define GL_FALSE
Definition: gl.h:173
GLfloat * inv
Definition: m_matrix.h:77
#define MAT_FLAG_SINGULAR
Definition: m_matrix.c:61
static GLfloat Identity[16]
Definition: m_matrix.c:132
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLuint flags
Definition: m_matrix.h:78
static void matrix_multf ( GLmatrix mat,
const GLfloat m,
GLuint  flags 
)
static

Multiply a matrix by an array of floats with known properties.

Parameters
matpointer to a GLmatrix structure containing the left multiplication matrix, and that will receive the product result.
mright multiplication matrix array.
flagsflags of the matrix m.

Joins both flags and marks the type and inverse as dirty. Calls matmul34() if both matrices are 3D, or matmul4() otherwise.

Definition at line 213 of file m_matrix.c.

Referenced by _math_matrix_frustum(), and _math_matrix_ortho().

231 {
static void print_matrix_floats ( const GLfloat  m[16])
static

Print a matrix array.

Parameters
mmatrix array.

Called by _math_matrix_print() to print a matrix or its inverse.

Definition at line 281 of file m_matrix.c.

292 {

Variable Documentation

GLfloat Identity[16]
static
Initial value:
= {
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
}

Identity matrix.

Definition at line 132 of file m_matrix.c.

Referenced by _math_matrix_is_dirty().

inv_mat_func inv_mat_tab[7]
static
Initial value:
= {
invert_matrix_3d
}
static GLboolean invert_matrix_3d_no_rot(GLmatrix *mat)
Definition: m_matrix.c:651
static GLboolean invert_matrix_2d_no_rot(GLmatrix *mat)
Definition: m_matrix.c:684
static GLboolean invert_matrix_3d(GLmatrix *mat)
Definition: m_matrix.c:555
static GLboolean invert_matrix_general(GLmatrix *mat)
Definition: m_matrix.c:355
static GLboolean invert_matrix_identity(GLmatrix *mat)
Definition: m_matrix.c:635

Table of the matrix inversion functions according to the matrix type.

Definition at line 740 of file m_matrix.c.

const char* types[]
static
Initial value:
= {
"MATRIX_GENERAL",
"MATRIX_IDENTITY",
"MATRIX_3D_NO_ROT",
"MATRIX_PERSPECTIVE",
"MATRIX_2D",
"MATRIX_2D_NO_ROT",
"MATRIX_3D"
}

Names of the corresponding GLmatrixtype values.

Definition at line 118 of file m_matrix.c.