]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
ported some features over from darkwar matrixlib.[ch]
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 11 Aug 2008 01:54:26 +0000 (01:54 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Mon, 11 Aug 2008 01:54:26 +0000 (01:54 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8442 d7cf8633-e32d-0410-b094-e92efae38249

matrixlib.c
matrixlib.h

index a0499861b6161ccdb86ecdc48a8b1a977758b285..7b7bfad6fa7a43aa8e214e1a73fc0b167d8fe40e 100644 (file)
@@ -362,6 +362,30 @@ void Matrix4x4_Invert_Simple (matrix4x4_t *out, const matrix4x4_t *in1)
 #endif
 }
 
+void Matrix4x4_Interpolate (matrix4x4_t *out, matrix4x4_t *in1, matrix4x4_t *in2, double frac)
+{
+       int i, j;
+       for (i = 0;i < 4;i++)
+               for (j = 0;j < 4;j++)
+                       out->m[i][j] = in1->m[i][j] + frac * (in2->m[i][j] - in1->m[i][j]);
+}
+
+void Matrix4x4_Clear (matrix4x4_t *out)
+{
+       int i, j;
+       for (i = 0;i < 4;i++)
+               for (j = 0;j < 4;j++)
+                       out->m[i][j] = 0;
+}
+
+void Matrix4x4_Accumulate (matrix4x4_t *out, matrix4x4_t *in, double weight)
+{
+       int i, j;
+       for (i = 0;i < 4;i++)
+               for (j = 0;j < 4;j++)
+                       out->m[i][j] += in->m[i][j] * weight;
+}
+
 void Matrix4x4_Normalize (matrix4x4_t *out, matrix4x4_t *in1)
 {
        // scale rotation matrix vectors to a length of 1
@@ -371,6 +395,33 @@ void Matrix4x4_Normalize (matrix4x4_t *out, matrix4x4_t *in1)
        Matrix4x4_Scale(out, scale, 1);
 }
 
+void Matrix4x4_Normalize3 (matrix4x4_t *out, matrix4x4_t *in1)
+{
+       int i;
+       double scale;
+       // scale each rotation matrix vector to a length of 1
+       // intended for use after Matrix4x4_Interpolate or Matrix4x4_Accumulate
+       *out = *in1;
+       for (i = 0;i < 3;i++)
+       {
+#ifdef MATRIX4x4_OPENGLORIENTATION
+               scale = sqrt(in1->m[i][0] * in1->m[i][0] + in1->m[i][1] * in1->m[i][1] + in1->m[i][2] * in1->m[i][2]);
+               if (scale)
+                       scale = 1.0 / scale;
+               out->m[i][0] *= scale;
+               out->m[i][1] *= scale;
+               out->m[i][2] *= scale;
+#else
+               scale = sqrt(in1->m[0][i] * in1->m[0][i] + in1->m[1][i] * in1->m[1][i] + in1->m[2][i] * in1->m[2][i]);
+               if (scale)
+                       scale = 1.0 / scale;
+               out->m[0][i] *= scale;
+               out->m[1][i] *= scale;
+               out->m[2][i] *= scale;
+#endif
+       }
+}
+
 void Matrix4x4_Reflect (matrix4x4_t *out, double normalx, double normaly, double normalz, double dist, double axisscale)
 {
        int i;
index 480dec150105651468aa32e27fb51ee11c35757a..8e1e65379c85361c4ef31b1936e276b65398d8ae 100644 (file)
@@ -39,9 +39,23 @@ int Matrix4x4_Invert_Full (matrix4x4_t *out, const matrix4x4_t *in1);
 // creates a matrix that does the opposite of the matrix provided
 // only supports translate, rotate, scale (not scale3) matrices
 void Matrix4x4_Invert_Simple (matrix4x4_t *out, const matrix4x4_t *in1);
+// blends between two matrices, used primarily for animation interpolation
+// (note: it is recommended to follow this with Matrix4x4_Normalize, a method
+//  known as nlerp rotation, often better for animation purposes than slerp)
+void Matrix4x4_Interpolate (matrix4x4_t *out, matrix4x4_t *in1, matrix4x4_t *in2, double frac);
+// zeros all matrix components, used with Matrix4x4_Accumulate
+void Matrix4x4_Clear (matrix4x4_t *out);
+// adds a weighted contribution from the supplied matrix, used to blend 3 or
+// more matrices with weighting, it is recommended that Matrix4x4_Normalize be
+// called afterward (a method known as nlerp rotation, often better for
+// animation purposes than slerp)
+void Matrix4x4_Accumulate (matrix4x4_t *out, matrix4x4_t *in, double weight);
 // creates a matrix that does the same rotation and translation as the matrix
 // provided, but no uniform scaling, does not support scale3 matrices
 void Matrix4x4_Normalize (matrix4x4_t *out, matrix4x4_t *in1);
+// creates a matrix with vectors normalized individually (use after
+// Matrix4x4_Accumulate)
+void Matrix4x4_Normalize3 (matrix4x4_t *out, matrix4x4_t *in1);
 // modifies a matrix to have all vectors and origin reflected across the plane
 // to the opposite side (at least if axisscale is -2)
 void Matrix4x4_Reflect (matrix4x4_t *out, double normalx, double normaly, double normalz, double dist, double axisscale);