s/getenv/_mesa_getenv/
[mesa.git] / src / mesa / math / m_matrix.c
index 0a722fe3ea1ff0c9d9a0a87cd42d2266808dd3ef..4b346860d61710d88e81c9fa4ae3f9e3b77fb242 100644 (file)
@@ -1,21 +1,21 @@
-/* $Id: m_matrix.c,v 1.3 2000/11/20 15:16:33 brianp Exp $ */
+/* $Id: m_matrix.c,v 1.14 2002/10/24 23:57:24 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
- * 
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
- * 
+ * Version:  4.1
+ *
+ * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  */
 
 #include "glheader.h"
+#include "imports.h"
 #include "macros.h"
-#include "mem.h"
+#include "imports.h"
 #include "mmath.h"
 
 #include "m_matrix.h"
 
+
 static const char *types[] = {
    "MATRIX_GENERAL",
    "MATRIX_IDENTITY",
@@ -63,13 +65,13 @@ static GLfloat Identity[16] = {
 
 
 /*
- * This matmul was contributed by Thomas Malik 
+ * This matmul was contributed by Thomas Malik
  *
  * Perform a 4x4 matrix multiplication  (product = a x b).
  * Input:  a, b - matrices to multiply
  * Output:  product - product of a and b
  * WARNING: (product != b) assumed
- * NOTE:    (product == a) allowed    
+ * NOTE:    (product == a) allowed
  *
  * KW: 4*16 = 64 muls
  */
@@ -91,7 +93,7 @@ static void matmul4( GLfloat *product, const GLfloat *a, const GLfloat *b )
 
 
 /* Multiply two matrices known to occupy only the top three rows, such
- * as typical model matrices, and ortho matrices.  
+ * as typical model matrices, and ortho matrices.
  */
 static void matmul34( GLfloat *product, const GLfloat *a, const GLfloat *b )
 {
@@ -124,8 +126,8 @@ static void matrix_multf( GLmatrix *mat, const GLfloat *m, GLuint flags )
 
    if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D))
       matmul34( mat->m, mat->m, m );
-   else 
-      matmul4( mat->m, mat->m, m ); 
+   else
+      matmul4( mat->m, mat->m, m );
 }
 
 
@@ -133,25 +135,25 @@ static void print_matrix_floats( const GLfloat m[16] )
 {
    int i;
    for (i=0;i<4;i++) {
-      fprintf(stderr,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
+      _mesa_debug(NULL,"\t%f %f %f %f\n", m[i], m[4+i], m[8+i], m[12+i] );
    }
 }
 
-void 
+void
 _math_matrix_print( const GLmatrix *m )
 {
-   fprintf(stderr, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
+   _mesa_debug(NULL, "Matrix type: %s, flags: %x\n", types[m->type], m->flags);
    print_matrix_floats(m->m);
-   fprintf(stderr, "Inverse: \n");
+   _mesa_debug(NULL, "Inverse: \n");
    if (m->inv) {
       GLfloat prod[16];
       print_matrix_floats(m->inv);
       matmul4(prod, m->m, m->inv);
-      fprintf(stderr, "Mat * Inverse:\n");
+      _mesa_debug(NULL, "Mat * Inverse:\n");
       print_matrix_floats(prod);
    }
    else {
-      fprintf(stderr, "  - not available\n");
+      _mesa_debug(NULL, "  - not available\n");
    }
 }
 
@@ -173,31 +175,31 @@ static GLboolean invert_matrix_general( GLmatrix *mat )
    GLfloat wtmp[4][8];
    GLfloat m0, m1, m2, m3, s;
    GLfloat *r0, *r1, *r2, *r3;
-  
+
    r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
-  
+
    r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
    r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
    r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
-  
+
    r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
    r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
    r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
-  
+
    r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
    r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
    r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
-  
+
    r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
    r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
    r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
-  
+
    /* choose pivot - or die */
    if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
    if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
    if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
    if (0.0 == r0[0])  return GL_FALSE;
-  
+
    /* eliminate first variable     */
    m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
    s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
@@ -211,12 +213,12 @@ static GLboolean invert_matrix_general( GLmatrix *mat )
    if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
    s = r0[7];
    if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
-  
+
    /* choose pivot - or die */
    if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
    if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
    if (0.0 == r1[1])  return GL_FALSE;
-  
+
    /* eliminate second variable */
    m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
    r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
@@ -225,25 +227,25 @@ static GLboolean invert_matrix_general( GLmatrix *mat )
    s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
    s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
    s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
-  
+
    /* choose pivot - or die */
    if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
    if (0.0 == r2[2])  return GL_FALSE;
-  
+
    /* eliminate third variable */
    m3 = r3[2]/r2[2];
    r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
    r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
    r3[7] -= m3 * r2[7];
-  
+
    /* last check */
    if (0.0 == r3[3]) return GL_FALSE;
-  
-   s = 1.0/r3[3];              /* now back substitute row 3 */
+
+   s = 1.0F/r3[3];             /* now back substitute row 3 */
    r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
-  
+
    m2 = r2[3];                 /* now back substitute row 2 */
-   s  = 1.0/r2[2];
+   s  = 1.0F/r2[2];
    r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
    r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
    m1 = r1[3];
@@ -252,20 +254,20 @@ static GLboolean invert_matrix_general( GLmatrix *mat )
    m0 = r0[3];
    r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
    r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
-  
+
    m1 = r1[2];                 /* now back substitute row 1 */
-   s  = 1.0/r1[1];
+   s  = 1.0F/r1[1];
    r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
    r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
    m0 = r0[2];
    r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
    r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
-  
+
    m0 = r0[1];                 /* now back substitute row 0 */
-   s  = 1.0/r0[0];
+   s  = 1.0F/r0[0];
    r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
    r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
-  
+
    MAT(out,0,0) = r0[4]; MAT(out,0,1) = r0[5],
    MAT(out,0,2) = r0[6]; MAT(out,0,3) = r0[7],
    MAT(out,1,0) = r1[4]; MAT(out,1,1) = r1[5],
@@ -273,15 +275,15 @@ static GLboolean invert_matrix_general( GLmatrix *mat )
    MAT(out,2,0) = r2[4]; MAT(out,2,1) = r2[5],
    MAT(out,2,2) = r2[6]; MAT(out,2,3) = r2[7],
    MAT(out,3,0) = r3[4]; MAT(out,3,1) = r3[5],
-   MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7]; 
-  
+   MAT(out,3,2) = r3[6]; MAT(out,3,3) = r3[7];
+
    return GL_TRUE;
 }
 #undef SWAP_ROWS
 
 
 /* Adapted from graphics gems II.
- */  
+ */
 static GLboolean invert_matrix_3d_general( GLmatrix *mat )
 {
    const GLfloat *in = mat->m;
@@ -290,7 +292,7 @@ static GLboolean invert_matrix_3d_general( GLmatrix *mat )
    GLfloat det;
 
    /* Calculate the determinant of upper left 3x3 submatrix and
-    * determine if the matrix is singular. 
+    * determine if the matrix is singular.
     */
    pos = neg = 0.0;
    t =  MAT(in,0,0) * MAT(in,1,1) * MAT(in,2,2);
@@ -313,10 +315,10 @@ static GLboolean invert_matrix_3d_general( GLmatrix *mat )
 
    det = pos + neg;
 
-   if (det*det < 1e-25) 
+   if (det*det < 1e-25)
       return GL_FALSE;
-   
-   det = 1.0 / det;
+
+   det = 1.0F / det;
    MAT(out,0,0) = (  (MAT(in,1,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,1,2) )*det);
    MAT(out,0,1) = (- (MAT(in,0,1)*MAT(in,2,2) - MAT(in,2,1)*MAT(in,0,2) )*det);
    MAT(out,0,2) = (  (MAT(in,0,1)*MAT(in,1,2) - MAT(in,1,1)*MAT(in,0,2) )*det);
@@ -337,7 +339,7 @@ static GLboolean invert_matrix_3d_general( GLmatrix *mat )
    MAT(out,2,3) = - (MAT(in,0,3) * MAT(out,2,0) +
                     MAT(in,1,3) * MAT(out,2,1) +
                     MAT(in,2,3) * MAT(out,2,2) );
-    
+
    return GL_TRUE;
 }
 
@@ -350,16 +352,16 @@ static GLboolean invert_matrix_3d( GLmatrix *mat )
    if (!TEST_MAT_FLAGS(mat, MAT_FLAGS_ANGLE_PRESERVING)) {
       return invert_matrix_3d_general( mat );
    }
-   
+
    if (mat->flags & MAT_FLAG_UNIFORM_SCALE) {
       GLfloat scale = (MAT(in,0,0) * MAT(in,0,0) +
                        MAT(in,0,1) * MAT(in,0,1) +
                        MAT(in,0,2) * MAT(in,0,2));
 
-      if (scale == 0.0) 
+      if (scale == 0.0)
          return GL_FALSE;
 
-      scale = 1.0 / scale;
+      scale = 1.0F / scale;
 
       /* Transpose and scale the 3 by 3 upper-left submatrix. */
       MAT(out,0,0) = scale * MAT(in,0,0);
@@ -392,7 +394,7 @@ static GLboolean invert_matrix_3d( GLmatrix *mat )
       MAT(out,2,3) = - MAT(in,2,3);
       return GL_TRUE;
    }
-    
+
    if (mat->flags & MAT_FLAG_TRANSLATION) {
       /* Do the translation part */
       MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0) +
@@ -408,11 +410,11 @@ static GLboolean invert_matrix_3d( GLmatrix *mat )
    else {
       MAT(out,0,3) = MAT(out,1,3) = MAT(out,2,3) = 0.0;
    }
-    
+
    return GL_TRUE;
 }
 
-  
+
 
 static GLboolean invert_matrix_identity( GLmatrix *mat )
 {
@@ -426,13 +428,13 @@ static GLboolean invert_matrix_3d_no_rot( GLmatrix *mat )
    const GLfloat *in = mat->m;
    GLfloat *out = mat->inv;
 
-   if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )       
+   if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0 || MAT(in,2,2) == 0 )
       return GL_FALSE;
-  
+
    MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
-   MAT(out,0,0) = 1.0 / MAT(in,0,0);
-   MAT(out,1,1) = 1.0 / MAT(in,1,1);
-   MAT(out,2,2) = 1.0 / MAT(in,2,2);
+   MAT(out,0,0) = 1.0F / MAT(in,0,0);
+   MAT(out,1,1) = 1.0F / MAT(in,1,1);
+   MAT(out,2,2) = 1.0F / MAT(in,2,2);
 
    if (mat->flags & MAT_FLAG_TRANSLATION) {
       MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
@@ -449,12 +451,12 @@ static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
    const GLfloat *in = mat->m;
    GLfloat *out = mat->inv;
 
-   if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)       
+   if (MAT(in,0,0) == 0 || MAT(in,1,1) == 0)
       return GL_FALSE;
-  
+
    MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
-   MAT(out,0,0) = 1.0 / MAT(in,0,0);
-   MAT(out,1,1) = 1.0 / MAT(in,1,1);
+   MAT(out,0,0) = 1.0F / MAT(in,0,0);
+   MAT(out,1,1) = 1.0F / MAT(in,1,1);
 
    if (mat->flags & MAT_FLAG_TRANSLATION) {
       MAT(out,0,3) = - (MAT(in,0,3) * MAT(out,0,0));
@@ -465,6 +467,8 @@ static GLboolean invert_matrix_2d_no_rot( GLmatrix *mat )
 }
 
 
+#if 0
+/* broken */
 static GLboolean invert_matrix_perspective( GLmatrix *mat )
 {
    const GLfloat *in = mat->m;
@@ -475,8 +479,8 @@ static GLboolean invert_matrix_perspective( GLmatrix *mat )
 
    MEMCPY( out, Identity, 16 * sizeof(GLfloat) );
 
-   MAT(out,0,0) = 1.0 / MAT(in,0,0);
-   MAT(out,1,1) = 1.0 / MAT(in,1,1);
+   MAT(out,0,0) = 1.0F / MAT(in,0,0);
+   MAT(out,1,1) = 1.0F / MAT(in,1,1);
 
    MAT(out,0,3) = MAT(in,0,2);
    MAT(out,1,3) = MAT(in,1,2);
@@ -484,11 +488,12 @@ static GLboolean invert_matrix_perspective( GLmatrix *mat )
    MAT(out,2,2) = 0;
    MAT(out,2,3) = -1;
 
-   MAT(out,3,2) = 1.0 / MAT(in,2,3);
+   MAT(out,3,2) = 1.0F / MAT(in,2,3);
    MAT(out,3,3) = MAT(in,2,2) * MAT(out,3,2);
 
    return GL_TRUE;
 }
+#endif
 
 
 typedef GLboolean (*inv_mat_func)( GLmatrix *mat );
@@ -498,7 +503,14 @@ static inv_mat_func inv_mat_tab[7] = {
    invert_matrix_general,
    invert_matrix_identity,
    invert_matrix_3d_no_rot,
+#if 0
+   /* Don't use this function for now - it fails when the projection matrix
+    * is premultiplied by a translation (ala Chromium's tilesort SPU).
+    */
    invert_matrix_perspective,
+#else
+   invert_matrix_general,
+#endif
    invert_matrix_3d,           /* lazy! */
    invert_matrix_2d_no_rot,
    invert_matrix_3d
@@ -514,7 +526,7 @@ static GLboolean matrix_invert( GLmatrix *mat )
       mat->flags |= MAT_FLAG_SINGULAR;
       MEMCPY( mat->inv, Identity, sizeof(Identity) );
       return GL_FALSE;
-   }  
+   }
 }
 
 
@@ -525,138 +537,196 @@ static GLboolean matrix_invert( GLmatrix *mat )
 /*
  * Generate a 4x4 transformation matrix from glRotate parameters, and
  * postmultiply the input matrix by it.
+ * This function contributed by Erich Boleyn (erich@uruk.org).
+ * Optimizatios contributed by Rudolf Opalla (rudi@khm.de).
  */
-void 
-_math_matrix_rotate( GLmatrix *mat, 
+void
+_math_matrix_rotate( GLmatrix *mat,
                     GLfloat angle, GLfloat x, GLfloat y, GLfloat z )
 {
-   /* This function contributed by Erich Boleyn (erich@uruk.org) */
-   GLfloat mag, s, c;
-   GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c;
+   GLfloat xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c, s, c;
    GLfloat m[16];
-   
-   s = sin( angle * DEG2RAD );
-   c = cos( angle * DEG2RAD );
-
-   mag = GL_SQRT( x*x + y*y + z*z );
+   GLboolean optimized;
 
-   if (mag <= 1.0e-4) {
-      /* generate an identity matrix and return */
-      MEMCPY(m, Identity, sizeof(GLfloat)*16);
-      return;
-   }
+   s = (GLfloat) sin( angle * DEG2RAD );
+   c = (GLfloat) cos( angle * DEG2RAD );
 
-   x /= mag;
-   y /= mag;
-   z /= mag;
+   MEMCPY(m, Identity, sizeof(GLfloat)*16);
+   optimized = GL_FALSE;
 
 #define M(row,col)  m[col*4+row]
 
-   /*
-    *     Arbitrary axis rotation matrix.
-    *
-    *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
-    *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
-    *  (which is about the X-axis), and the two composite transforms
-    *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
-    *  from the arbitrary axis to the X-axis then back.  They are
-    *  all elementary rotations.
-    *
-    *  Rz' is a rotation about the Z-axis, to bring the axis vector
-    *  into the x-z plane.  Then Ry' is applied, rotating about the
-    *  Y-axis to bring the axis vector parallel with the X-axis.  The
-    *  rotation about the X-axis is then performed.  Ry and Rz are
-    *  simply the respective inverse transforms to bring the arbitrary
-    *  axis back to it's original orientation.  The first transforms
-    *  Rz' and Ry' are considered inverses, since the data from the
-    *  arbitrary axis gives you info on how to get to it, not how
-    *  to get away from it, and an inverse must be applied.
-    *
-    *  The basic calculation used is to recognize that the arbitrary
-    *  axis vector (x, y, z), since it is of unit length, actually
-    *  represents the sines and cosines of the angles to rotate the
-    *  X-axis to the same orientation, with theta being the angle about
-    *  Z and phi the angle about Y (in the order described above)
-    *  as follows:
-    *
-    *  cos ( theta ) = x / sqrt ( 1 - z^2 )
-    *  sin ( theta ) = y / sqrt ( 1 - z^2 )
-    *
-    *  cos ( phi ) = sqrt ( 1 - z^2 )
-    *  sin ( phi ) = z
-    *
-    *  Note that cos ( phi ) can further be inserted to the above
-    *  formulas:
-    *
-    *  cos ( theta ) = x / cos ( phi )
-    *  sin ( theta ) = y / sin ( phi )
-    *
-    *  ...etc.  Because of those relations and the standard trigonometric
-    *  relations, it is pssible to reduce the transforms down to what
-    *  is used below.  It may be that any primary axis chosen will give the
-    *  same results (modulo a sign convention) using thie method.
-    *
-    *  Particularly nice is to notice that all divisions that might
-    *  have caused trouble when parallel to certain planes or
-    *  axis go away with care paid to reducing the expressions.
-    *  After checking, it does perform correctly under all cases, since
-    *  in all the cases of division where the denominator would have
-    *  been zero, the numerator would have been zero as well, giving
-    *  the expected result.
-    */
+   if (x == 0.0F) {
+      if (y == 0.0F) {
+         if (z != 0.0F) {
+            optimized = GL_TRUE;
+            /* rotate only around z-axis */
+            M(0,0) = c;
+            M(1,1) = c;
+            if (z < 0.0F) {
+               M(0,1) = s;
+               M(1,0) = -s;
+            }
+            else {
+               M(0,1) = -s;
+               M(1,0) = s;
+            }
+         }
+      }
+      else if (z == 0.0F) {
+         optimized = GL_TRUE;
+         /* rotate only around y-axis */
+         M(0,0) = c;
+         M(2,2) = c;
+         if (y < 0.0F) {
+            M(0,2) = -s;
+            M(2,0) = s;
+         }
+         else {
+            M(0,2) = s;
+            M(2,0) = -s;
+         }
+      }
+   }
+   else if (y == 0.0F) {
+      if (z == 0.0F) {
+         optimized = GL_TRUE;
+         /* rotate only around x-axis */
+         M(1,1) = c;
+         M(2,2) = c;
+         if (y < 0.0F) {
+            M(1,2) = s;
+            M(2,1) = -s;
+         }
+         else {
+            M(1,2) = -s;
+            M(2,1) = s;
+         }
+      }
+   }
+
+   if (!optimized) {
+      const GLfloat mag = (GLfloat) GL_SQRT(x * x + y * y + z * z);
+
+      if (mag <= 1.0e-4) {
+         /* no rotation, leave mat as-is */
+         return;
+      }
 
-   xx = x * x;
-   yy = y * y;
-   zz = z * z;
-   xy = x * y;
-   yz = y * z;
-   zx = z * x;
-   xs = x * s;
-   ys = y * s;
-   zs = z * s;
-   one_c = 1.0F - c;
-
-   M(0,0) = (one_c * xx) + c;
-   M(0,1) = (one_c * xy) - zs;
-   M(0,2) = (one_c * zx) + ys;
-   M(0,3) = 0.0F;
-
-   M(1,0) = (one_c * xy) + zs;
-   M(1,1) = (one_c * yy) + c;
-   M(1,2) = (one_c * yz) - xs;
-   M(1,3) = 0.0F;
-
-   M(2,0) = (one_c * zx) - ys;
-   M(2,1) = (one_c * yz) + xs;
-   M(2,2) = (one_c * zz) + c;
-   M(2,3) = 0.0F;
-
-   M(3,0) = 0.0F;
-   M(3,1) = 0.0F;
-   M(3,2) = 0.0F;
-   M(3,3) = 1.0F;
+      x /= mag;
+      y /= mag;
+      z /= mag;
+
+
+      /*
+       *     Arbitrary axis rotation matrix.
+       *
+       *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
+       *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
+       *  (which is about the X-axis), and the two composite transforms
+       *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
+       *  from the arbitrary axis to the X-axis then back.  They are
+       *  all elementary rotations.
+       *
+       *  Rz' is a rotation about the Z-axis, to bring the axis vector
+       *  into the x-z plane.  Then Ry' is applied, rotating about the
+       *  Y-axis to bring the axis vector parallel with the X-axis.  The
+       *  rotation about the X-axis is then performed.  Ry and Rz are
+       *  simply the respective inverse transforms to bring the arbitrary
+       *  axis back to it's original orientation.  The first transforms
+       *  Rz' and Ry' are considered inverses, since the data from the
+       *  arbitrary axis gives you info on how to get to it, not how
+       *  to get away from it, and an inverse must be applied.
+       *
+       *  The basic calculation used is to recognize that the arbitrary
+       *  axis vector (x, y, z), since it is of unit length, actually
+       *  represents the sines and cosines of the angles to rotate the
+       *  X-axis to the same orientation, with theta being the angle about
+       *  Z and phi the angle about Y (in the order described above)
+       *  as follows:
+       *
+       *  cos ( theta ) = x / sqrt ( 1 - z^2 )
+       *  sin ( theta ) = y / sqrt ( 1 - z^2 )
+       *
+       *  cos ( phi ) = sqrt ( 1 - z^2 )
+       *  sin ( phi ) = z
+       *
+       *  Note that cos ( phi ) can further be inserted to the above
+       *  formulas:
+       *
+       *  cos ( theta ) = x / cos ( phi )
+       *  sin ( theta ) = y / sin ( phi )
+       *
+       *  ...etc.  Because of those relations and the standard trigonometric
+       *  relations, it is pssible to reduce the transforms down to what
+       *  is used below.  It may be that any primary axis chosen will give the
+       *  same results (modulo a sign convention) using thie method.
+       *
+       *  Particularly nice is to notice that all divisions that might
+       *  have caused trouble when parallel to certain planes or
+       *  axis go away with care paid to reducing the expressions.
+       *  After checking, it does perform correctly under all cases, since
+       *  in all the cases of division where the denominator would have
+       *  been zero, the numerator would have been zero as well, giving
+       *  the expected result.
+       */
+
+      xx = x * x;
+      yy = y * y;
+      zz = z * z;
+      xy = x * y;
+      yz = y * z;
+      zx = z * x;
+      xs = x * s;
+      ys = y * s;
+      zs = z * s;
+      one_c = 1.0F - c;
+
+      /* We already hold the identity-matrix so we can skip some statements */
+      M(0,0) = (one_c * xx) + c;
+      M(0,1) = (one_c * xy) - zs;
+      M(0,2) = (one_c * zx) + ys;
+/*    M(0,3) = 0.0F; */
+
+      M(1,0) = (one_c * xy) + zs;
+      M(1,1) = (one_c * yy) + c;
+      M(1,2) = (one_c * yz) - xs;
+/*    M(1,3) = 0.0F; */
+
+      M(2,0) = (one_c * zx) - ys;
+      M(2,1) = (one_c * yz) + xs;
+      M(2,2) = (one_c * zz) + c;
+/*    M(2,3) = 0.0F; */
 
+/*
+      M(3,0) = 0.0F;
+      M(3,1) = 0.0F;
+      M(3,2) = 0.0F;
+      M(3,3) = 1.0F;
+*/
+   }
 #undef M
 
    matrix_multf( mat, m, MAT_FLAG_ROTATION );
 }
 
 
+
 void
-_math_matrix_frustrum( GLmatrix *mat, 
-                      GLfloat left, GLfloat right,
-                      GLfloat bottom, GLfloat top, 
-                      GLfloat nearval, GLfloat farval )
+_math_matrix_frustum( GLmatrix *mat,
+                     GLfloat left, GLfloat right,
+                     GLfloat bottom, GLfloat top,
+                     GLfloat nearval, GLfloat farval )
 {
    GLfloat x, y, a, b, c, d;
    GLfloat m[16];
 
-   x = (2.0*nearval) / (right-left);
-   y = (2.0*nearval) / (top-bottom);
+   x = (2.0F*nearval) / (right-left);
+   y = (2.0F*nearval) / (top-bottom);
    a = (right+left) / (right-left);
    b = (top+bottom) / (top-bottom);
    c = -(farval+nearval) / ( farval-nearval);
-   d = -(2.0*farval*nearval) / (farval-nearval);  /* error? */
+   d = -(2.0F*farval*nearval) / (farval-nearval);  /* error? */
 
 #define M(row,col)  m[col*4+row]
    M(0,0) = x;     M(0,1) = 0.0F;  M(0,2) = a;      M(0,3) = 0.0F;
@@ -669,18 +739,18 @@ _math_matrix_frustrum( GLmatrix *mat,
 }
 
 void
-_math_matrix_ortho( GLmatrix *mat,     
+_math_matrix_ortho( GLmatrix *mat,
                    GLfloat left, GLfloat right,
-                   GLfloat bottom, GLfloat top, 
+                   GLfloat bottom, GLfloat top,
                    GLfloat nearval, GLfloat farval )
 {
    GLfloat x, y, z;
    GLfloat tx, ty, tz;
    GLfloat m[16];
 
-   x = 2.0 / (right-left);
-   y = 2.0 / (top-bottom);
-   z = -2.0 / (farval-nearval);
+   x = 2.0F / (right-left);
+   y = 2.0F / (top-bottom);
+   z = -2.0F / (farval-nearval);
    tx = -(right+left) / (right-left);
    ty = -(top+bottom) / (top-bottom);
    tz = -(farval+nearval) / (farval-nearval);
@@ -735,11 +805,11 @@ _math_matrix_ortho( GLmatrix *mat,
                          ZERO(3)  | ZERO(7)  |            ZERO(15) )
 
 #define SQ(x) ((x)*(x))
-  
+
 /* Determine type and flags from scratch.  This is expensive enough to
  * only want to do it once.
  */
-static void analyze_from_scratch( GLmatrix *mat )
+static void analyse_from_scratch( GLmatrix *mat )
 {
    const GLfloat *m = mat->m;
    GLuint mask = 0;
@@ -748,7 +818,7 @@ static void analyze_from_scratch( GLmatrix *mat )
    for (i = 0 ; i < 16 ; i++) {
       if (m[i] == 0.0) mask |= (1<<i);
    }
-   
+
    if (m[0] == 1.0F) mask |= (1<<16);
    if (m[5] == 1.0F) mask |= (1<<21);
    if (m[10] == 1.0F) mask |= (1<<26);
@@ -756,23 +826,23 @@ static void analyze_from_scratch( GLmatrix *mat )
 
    mat->flags &= ~MAT_FLAGS_GEOMETRY;
 
-   /* Check for translation - no-one really cares 
+   /* Check for translation - no-one really cares
     */
-   if ((mask & MASK_NO_TRX) != MASK_NO_TRX) 
-      mat->flags |= MAT_FLAG_TRANSLATION;      
+   if ((mask & MASK_NO_TRX) != MASK_NO_TRX)
+      mat->flags |= MAT_FLAG_TRANSLATION;
 
    /* Do the real work
     */
-   if (mask == MASK_IDENTITY) {
+   if (mask == (GLuint) MASK_IDENTITY) {
       mat->type = MATRIX_IDENTITY;
    }
-   else if ((mask & MASK_2D_NO_ROT) == MASK_2D_NO_ROT) {
+   else if ((mask & MASK_2D_NO_ROT) == (GLuint) MASK_2D_NO_ROT) {
       mat->type = MATRIX_2D_NO_ROT;
-      
+
       if ((mask & MASK_NO_2D_SCALE) != MASK_NO_2D_SCALE)
         mat->flags = MAT_FLAG_GENERAL_SCALE;
    }
-   else if ((mask & MASK_2D) == MASK_2D) {
+   else if ((mask & MASK_2D) == (GLuint) MASK_2D) {
       GLfloat mm = DOT2(m, m);
       GLfloat m4m4 = DOT2(m+4,m+4);
       GLfloat mm4 = DOT2(m,m+4);
@@ -781,7 +851,7 @@ static void analyze_from_scratch( GLmatrix *mat )
 
       /* Check for scale */
       if (SQ(mm-1) > SQ(1e-6) ||
-         SQ(m4m4-1) > SQ(1e-6)) 
+         SQ(m4m4-1) > SQ(1e-6))
         mat->flags |= MAT_FLAG_GENERAL_SCALE;
 
       /* Check for rotation */
@@ -791,11 +861,11 @@ static void analyze_from_scratch( GLmatrix *mat )
         mat->flags |= MAT_FLAG_ROTATION;
 
    }
-   else if ((mask & MASK_3D_NO_ROT) == MASK_3D_NO_ROT) {
+   else if ((mask & MASK_3D_NO_ROT) == (GLuint) MASK_3D_NO_ROT) {
       mat->type = MATRIX_3D_NO_ROT;
 
       /* Check for scale */
-      if (SQ(m[0]-m[5]) < SQ(1e-6) && 
+      if (SQ(m[0]-m[5]) < SQ(1e-6) &&
          SQ(m[0]-m[10]) < SQ(1e-6)) {
         if (SQ(m[0]-1.0) > SQ(1e-6)) {
            mat->flags |= MAT_FLAG_UNIFORM_SCALE;
@@ -805,7 +875,7 @@ static void analyze_from_scratch( GLmatrix *mat )
         mat->flags |= MAT_FLAG_GENERAL_SCALE;
       }
    }
-   else if ((mask & MASK_3D) == MASK_3D) {
+   else if ((mask & MASK_3D) == (GLuint) MASK_3D) {
       GLfloat c1 = DOT3(m,m);
       GLfloat c2 = DOT3(m+4,m+4);
       GLfloat c3 = DOT3(m+8,m+8);
@@ -828,7 +898,7 @@ static void analyze_from_scratch( GLmatrix *mat )
       if (SQ(d1) < SQ(1e-6)) {
         CROSS3( cp, m, m+4 );
         SUB_3V( cp, cp, (m+8) );
-        if (LEN_SQUARED_3FV(cp) < SQ(1e-6)) 
+        if (LEN_SQUARED_3FV(cp) < SQ(1e-6))
            mat->flags |= MAT_FLAG_ROTATION;
         else
            mat->flags |= MAT_FLAG_GENERAL_3D;
@@ -849,9 +919,9 @@ static void analyze_from_scratch( GLmatrix *mat )
 
 
 /* Analyse a matrix given that its flags are accurate - this is the
- * more common operation, hopefully. 
+ * more common operation, hopefully.
  */
-static void analyze_from_flags( GLmatrix *mat )
+static void analyse_from_flags( GLmatrix *mat )
 {
    const GLfloat *m = mat->m;
 
@@ -869,7 +939,7 @@ static void analyze_from_flags( GLmatrix *mat )
       }
    }
    else if (TEST_MAT_FLAGS(mat, MAT_FLAGS_3D)) {
-      if (                                 m[ 8]==0.0F               
+      if (                                 m[ 8]==0.0F
             &&                             m[ 9]==0.0F
             && m[2]==0.0F && m[6]==0.0F && m[10]==1.0F && m[14]==0.0F) {
         mat->type = MATRIX_2D;
@@ -890,14 +960,14 @@ static void analyze_from_flags( GLmatrix *mat )
 }
 
 
-void 
-_math_matrix_analyze( GLmatrix *mat ) 
+void
+_math_matrix_analyse( GLmatrix *mat )
 {
    if (mat->flags & MAT_DIRTY_TYPE) {
-      if (mat->flags & MAT_DIRTY_FLAGS) 
-        analyze_from_scratch( mat );
+      if (mat->flags & MAT_DIRTY_FLAGS)
+        analyse_from_scratch( mat );
       else
-        analyze_from_flags( mat );
+        analyse_from_flags( mat );
    }
 
    if (mat->inv && (mat->flags & MAT_DIRTY_INVERSE)) {
@@ -910,7 +980,7 @@ _math_matrix_analyze( GLmatrix *mat )
 }
 
 
-void 
+void
 _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
 {
    MEMCPY( to->m, from->m, sizeof(Identity) );
@@ -928,7 +998,7 @@ _math_matrix_copy( GLmatrix *to, const GLmatrix *from )
 }
 
 
-void 
+void
 _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
 {
    GLfloat *m = mat->m;
@@ -942,12 +1012,12 @@ _math_matrix_scale( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
    else
       mat->flags |= MAT_FLAG_GENERAL_SCALE;
 
-   mat->flags |= (MAT_DIRTY_TYPE | 
+   mat->flags |= (MAT_DIRTY_TYPE |
                  MAT_DIRTY_INVERSE);
 }
 
 
-void 
+void
 _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
 {
    GLfloat *m = mat->m;
@@ -956,81 +1026,81 @@ _math_matrix_translate( GLmatrix *mat, GLfloat x, GLfloat y, GLfloat z )
    m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
    m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
 
-   mat->flags |= (MAT_FLAG_TRANSLATION | 
-                 MAT_DIRTY_TYPE | 
+   mat->flags |= (MAT_FLAG_TRANSLATION |
+                 MAT_DIRTY_TYPE |
                  MAT_DIRTY_INVERSE);
 }
 
 
-void 
+void
 _math_matrix_loadf( GLmatrix *mat, const GLfloat *m )
 {
    MEMCPY( mat->m, m, 16*sizeof(GLfloat) );
    mat->flags = (MAT_FLAG_GENERAL | MAT_DIRTY);
 }
 
-void 
+void
 _math_matrix_ctr( GLmatrix *m )
 {
-   if ( m->m == 0 ) {
-      m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
-   }
-   MEMCPY( m->m, Identity, sizeof(Identity) );
-   m->inv = 0;
+   m->m = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
+   if (m->m)
+      MEMCPY( m->m, Identity, sizeof(Identity) );
+   m->inv = NULL;
    m->type = MATRIX_IDENTITY;
    m->flags = 0;
 }
 
-void 
+void
 _math_matrix_dtr( GLmatrix *m )
 {
-   if ( m->m != 0 ) {
+   if (m->m) {
       ALIGN_FREE( m->m );
-      m->m = 0;
+      m->m = NULL;
    }
-   if ( m->inv != 0 ) {
+   if (m->inv) {
       ALIGN_FREE( m->inv );
-      m->inv = 0;
+      m->inv = NULL;
    }
 }
 
 
-void 
+void
 _math_matrix_alloc_inv( GLmatrix *m )
 {
-   if ( m->inv == 0 ) {
+   if (!m->inv) {
       m->inv = (GLfloat *) ALIGN_MALLOC( 16 * sizeof(GLfloat), 16 );
-      MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
+      if (m->inv)
+         MEMCPY( m->inv, Identity, 16 * sizeof(GLfloat) );
    }
 }
 
 
-void 
+void
 _math_matrix_mul_matrix( GLmatrix *dest, const GLmatrix *a, const GLmatrix *b )
 {
    dest->flags = (a->flags |
                  b->flags |
-                 MAT_DIRTY_TYPE | 
+                 MAT_DIRTY_TYPE |
                  MAT_DIRTY_INVERSE);
 
    if (TEST_MAT_FLAGS(dest, MAT_FLAGS_3D))
       matmul34( dest->m, a->m, b->m );
-   else 
+   else
       matmul4( dest->m, a->m, b->m );
 }
 
 
-void 
+void
 _math_matrix_mul_floats( GLmatrix *dest, const GLfloat *m )
 {
    dest->flags |= (MAT_FLAG_GENERAL |
-                  MAT_DIRTY_TYPE | 
+                  MAT_DIRTY_TYPE |
                   MAT_DIRTY_INVERSE);
 
-   matmul4( dest->m, dest->m, m );   
+   matmul4( dest->m, dest->m, m );
 }
 
-void 
+void
 _math_matrix_set_identity( GLmatrix *mat )
 {
    MEMCPY( mat->m, Identity, 16*sizeof(GLfloat) );
@@ -1046,7 +1116,7 @@ _math_matrix_set_identity( GLmatrix *mat )
 
 
 
-void 
+void
 _math_transposef( GLfloat to[16], const GLfloat from[16] )
 {
    to[0] = from[0];
@@ -1068,7 +1138,7 @@ _math_transposef( GLfloat to[16], const GLfloat from[16] )
 }
 
 
-void 
+void
 _math_transposed( GLdouble to[16], const GLdouble from[16] )
 {
    to[0] = from[0];
@@ -1089,23 +1159,23 @@ _math_transposed( GLdouble to[16], const GLdouble from[16] )
    to[15] = from[15];
 }
 
-void 
+void
 _math_transposefd( GLfloat to[16], const GLdouble from[16] )
 {
-   to[0] = from[0];
-   to[1] = from[4];
-   to[2] = from[8];
-   to[3] = from[12];
-   to[4] = from[1];
-   to[5] = from[5];
-   to[6] = from[9];
-   to[7] = from[13];
-   to[8] = from[2];
-   to[9] = from[6];
-   to[10] = from[10];
-   to[11] = from[14];
-   to[12] = from[3];
-   to[13] = from[7];
-   to[14] = from[11];
-   to[15] = from[15];
+   to[0] = (GLfloat) from[0];
+   to[1] = (GLfloat) from[4];
+   to[2] = (GLfloat) from[8];
+   to[3] = (GLfloat) from[12];
+   to[4] = (GLfloat) from[1];
+   to[5] = (GLfloat) from[5];
+   to[6] = (GLfloat) from[9];
+   to[7] = (GLfloat) from[13];
+   to[8] = (GLfloat) from[2];
+   to[9] = (GLfloat) from[6];
+   to[10] = (GLfloat) from[10];
+   to[11] = (GLfloat) from[14];
+   to[12] = (GLfloat) from[3];
+   to[13] = (GLfloat) from[7];
+   to[14] = (GLfloat) from[11];
+   to[15] = (GLfloat) from[15];
 }