]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - matrixlib.c
got rid of all the (no longer used) cached_ fields in the msurface_t struct, except...
[xonotic/darkplaces.git] / matrixlib.c
index ff447d7ab5f3a7a5dc17d81a5048c663b851391e..7d6db2ece7ff67903264812d67b0184bdd630fbb 100644 (file)
@@ -37,11 +37,11 @@ void Matrix4x4_CopyTranslateOnly (matrix4x4_t *out, const matrix4x4_t *in)
        out->m[1][0] = 0.0f;
        out->m[1][1] = 0.0f;
        out->m[1][2] = 0.0f;
-       out->m[1][3] = in->m[0][3];
+       out->m[1][3] = in->m[1][3];
        out->m[2][0] = 0.0f;
        out->m[2][1] = 0.0f;
        out->m[2][2] = 0.0f;
-       out->m[2][3] = in->m[0][3];
+       out->m[2][3] = in->m[2][3];
        out->m[3][0] = 0.0f;
        out->m[3][1] = 0.0f;
        out->m[3][2] = 0.0f;
@@ -135,7 +135,15 @@ void Matrix4x4_Invert_Simple (matrix4x4_t *out, const matrix4x4_t *in1)
        // (note the lack of sqrt here, because we're trying to undo the scaling,
        // this means multiplying by the inverse scale twice - squaring it, which
        // makes the sqrt a waste of time)
+#if 1
        double scale = 1.0 / (in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]);
+#else
+       double scale = 3.0 / sqrt
+                (in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]
+               + in1->m[1][0] * in1->m[1][0] + in1->m[1][1] * in1->m[1][1] + in1->m[1][2] * in1->m[1][2]
+               + in1->m[2][0] * in1->m[2][0] + in1->m[2][1] * in1->m[2][1] + in1->m[2][2] * in1->m[2][2]);
+       scale *= scale;
+#endif
 
        // invert the rotation by transposing and multiplying by the squared
        // recipricol of the input matrix scale as described above
@@ -212,7 +220,7 @@ void Matrix4x4_CreateRotate (matrix4x4_t *out, float angle, float x, float y, fl
        y *= len;
        z *= len;
 
-       angle *= M_PI / 180.0;
+       angle *= -M_PI / 180.0;
        c = cos(angle);
        s = sin(angle);
 
@@ -287,17 +295,17 @@ void Matrix4x4_CreateFromQuakeEntity(matrix4x4_t *out, float x, float y, float z
        angle = roll * (M_PI*2 / 360);
        sr = sin(angle);
        cr = cos(angle);
-       out->m[0][0] = cp*cy * scale;
-       out->m[0][1] = sr*sp*cy+cr*-sy * scale;
-       out->m[0][2] = cr*sp*cy+-sr*-sy * scale;
+       out->m[0][0] = (cp*cy) * scale;
+       out->m[0][1] = (sr*sp*cy+cr*-sy) * scale;
+       out->m[0][2] = (cr*sp*cy+-sr*-sy) * scale;
        out->m[0][3] = x;
-       out->m[1][0] = cp*sy * scale;
-       out->m[1][1] = sr*sp*sy+cr*cy * scale;
-       out->m[1][2] = cr*sp*sy+-sr*cy * scale;
+       out->m[1][0] = (cp*sy) * scale;
+       out->m[1][1] = (sr*sp*sy+cr*cy) * scale;
+       out->m[1][2] = (cr*sp*sy+-sr*cy) * scale;
        out->m[1][3] = y;
-       out->m[2][0] = -sp * scale;
-       out->m[2][1] = sr*cp * scale;
-       out->m[2][2] = cr*cp * scale;
+       out->m[2][0] = (-sp) * scale;
+       out->m[2][1] = (sr*cp) * scale;
+       out->m[2][2] = (cr*cp) * scale;
        out->m[2][3] = z;
        out->m[3][0] = 0;
        out->m[3][1] = 0;
@@ -356,6 +364,13 @@ void Matrix4x4_Transform4 (const matrix4x4_t *in, const float v[4], float out[4]
        out[3] = v[0] * in->m[3][0] + v[1] * in->m[3][1] + v[2] * in->m[3][2] + v[3] * in->m[3][3];
 }
 
+void Matrix4x4_Transform3x3 (const matrix4x4_t *in, const float v[3], float out[3])
+{
+       out[0] = v[0] * in->m[0][0] + v[1] * in->m[0][1] + v[2] * in->m[0][2];
+       out[1] = v[0] * in->m[1][0] + v[1] * in->m[1][1] + v[2] * in->m[1][2];
+       out[2] = v[0] * in->m[2][0] + v[1] * in->m[2][1] + v[2] * in->m[2][2];
+}
+
 /*
 void Matrix4x4_SimpleUntransform (const matrix4x4_t *in, const float v[3], float out[3])
 {
@@ -373,7 +388,7 @@ void Matrix4x4_SimpleUntransform (const matrix4x4_t *in, const float v[3], float
 void Matrix4x4_ConcatTranslate (matrix4x4_t *out, float x, float y, float z)
 {
        matrix4x4_t base, temp;
-       Matrix4x4_Copy(out, &base);
+       base = *out;
        Matrix4x4_CreateTranslate(&temp, x, y, z);
        Matrix4x4_Concat(out, &base, &temp);
 }
@@ -382,7 +397,7 @@ void Matrix4x4_ConcatTranslate (matrix4x4_t *out, float x, float y, float z)
 void Matrix4x4_ConcatRotate (matrix4x4_t *out, float angle, float x, float y, float z)
 {
        matrix4x4_t base, temp;
-       Matrix4x4_Copy(out, &base);
+       base = *out;
        Matrix4x4_CreateRotate(&temp, angle, x, y, z);
        Matrix4x4_Concat(out, &base, &temp);
 }
@@ -391,7 +406,7 @@ void Matrix4x4_ConcatRotate (matrix4x4_t *out, float angle, float x, float y, fl
 void Matrix4x4_ConcatScale (matrix4x4_t *out, float x)
 {
        matrix4x4_t base, temp;
-       Matrix4x4_Copy(out, &base);
+       base = *out;
        Matrix4x4_CreateScale(&temp, x);
        Matrix4x4_Concat(out, &base, &temp);
 }
@@ -400,11 +415,19 @@ void Matrix4x4_ConcatScale (matrix4x4_t *out, float x)
 void Matrix4x4_ConcatScale3 (matrix4x4_t *out, float x, float y, float z)
 {
        matrix4x4_t base, temp;
-       Matrix4x4_Copy(out, &base);
+       base = *out;
        Matrix4x4_CreateScale3(&temp, x, y, z);
        Matrix4x4_Concat(out, &base, &temp);
 }
 
+void Matrix4x4_Print (const matrix4x4_t *in)
+{
+       Con_Printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
+       , in->m[0][0], in->m[0][1], in->m[0][2], in->m[0][3]
+       , in->m[1][0], in->m[1][1], in->m[1][2], in->m[1][3]
+       , in->m[2][0], in->m[2][1], in->m[2][2], in->m[2][3]
+       , in->m[3][0], in->m[3][1], in->m[3][2], in->m[3][3]);
+}
 
 
 
@@ -442,11 +465,11 @@ void Matrix3x4_CopyTranslateOnly (matrix3x4_t *out, const matrix3x4_t *in)
        out->m[1][0] = 0.0f;
        out->m[1][1] = 0.0f;
        out->m[1][2] = 0.0f;
-       out->m[1][3] = in->m[0][3];
+       out->m[1][3] = in->m[1][3];
        out->m[2][0] = 0.0f;
        out->m[2][1] = 0.0f;
        out->m[2][2] = 0.0f;
-       out->m[2][3] = in->m[0][3];
+       out->m[2][3] = in->m[2][3];
 }
 
 void Matrix3x4_FromMatrix4x4 (matrix3x4_t *out, const matrix4x4_t *in)
@@ -503,7 +526,15 @@ void Matrix3x4_Invert_Simple (matrix3x4_t *out, const matrix3x4_t *in1)
        // (note the lack of sqrt here, because we're trying to undo the scaling,
        // this means multiplying by the inverse scale twice - squaring it, which
        // makes the sqrt a waste of time)
+#if 1
        double scale = 1.0 / (in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]);
+#else
+       double scale = 3.0 / sqrt
+                (in1->m[0][0] * in1->m[0][0] + in1->m[0][1] * in1->m[0][1] + in1->m[0][2] * in1->m[0][2]
+               + in1->m[1][0] * in1->m[1][0] + in1->m[1][1] * in1->m[1][1] + in1->m[1][2] * in1->m[1][2]
+               + in1->m[2][0] * in1->m[2][0] + in1->m[2][1] * in1->m[2][1] + in1->m[2][2] * in1->m[2][2]);
+       scale *= scale;
+#endif
 
        // invert the rotation by transposing and multiplying by the squared
        // recipricol of the input matrix scale as described above
@@ -630,17 +661,17 @@ void Matrix3x4_CreateFromQuakeEntity(matrix3x4_t *out, float x, float y, float z
        angle = roll * (M_PI*2 / 360);
        sr = sin(angle);
        cr = cos(angle);
-       out->m[0][0] = cp*cy * scale;
-       out->m[0][1] = sr*sp*cy+cr*-sy * scale;
-       out->m[0][2] = cr*sp*cy+-sr*-sy * scale;
+       out->m[0][0] = (cp*cy) * scale;
+       out->m[0][1] = (sr*sp*cy+cr*-sy) * scale;
+       out->m[0][2] = (cr*sp*cy+-sr*-sy) * scale;
        out->m[0][3] = x;
-       out->m[1][0] = cp*sy * scale;
-       out->m[1][1] = sr*sp*sy+cr*cy * scale;
-       out->m[1][2] = cr*sp*sy+-sr*cy * scale;
+       out->m[1][0] = (cp*sy) * scale;
+       out->m[1][1] = (sr*sp*sy+cr*cy) * scale;
+       out->m[1][2] = (cr*sp*sy+-sr*cy) * scale;
        out->m[1][3] = y;
-       out->m[2][0] = -sp * scale;
-       out->m[2][1] = sr*cp * scale;
-       out->m[2][2] = cr*cp * scale;
+       out->m[2][0] = (-sp) * scale;
+       out->m[2][1] = (sr*cp) * scale;
+       out->m[2][2] = (cr*cp) * scale;
        out->m[2][3] = z;
 }
 
@@ -683,6 +714,7 @@ void Matrix3x4_Transform (const matrix3x4_t *in, const float v[3], float out[3])
        out[2] = v[0] * in->m[2][0] + v[1] * in->m[2][1] + v[2] * in->m[2][2] + in->m[2][3];
 }
 
+/*
 void Matrix3x4_SimpleUntransform (const matrix3x4_t *in, const float v[3], float out[3])
 {
        float t[3];
@@ -693,12 +725,21 @@ void Matrix3x4_SimpleUntransform (const matrix3x4_t *in, const float v[3], float
        out[1] = t[0] * in->m[0][1] + t[1] * in->m[1][1] + t[2] * in->m[2][1];
        out[2] = t[0] * in->m[0][2] + t[1] * in->m[1][2] + t[2] * in->m[2][2];
 }
+*/
+
+void Matrix3x4_Transform3x3 (const matrix3x4_t *in, const float v[3], float out[3])
+{
+       out[0] = v[0] * in->m[0][0] + v[1] * in->m[0][1] + v[2] * in->m[0][2] + in->m[0][3];
+       out[1] = v[0] * in->m[1][0] + v[1] * in->m[1][1] + v[2] * in->m[1][2] + in->m[1][3];
+       out[2] = v[0] * in->m[2][0] + v[1] * in->m[2][1] + v[2] * in->m[2][2] + in->m[2][3];
+}
+
 
 // FIXME: optimize
 void Matrix3x4_ConcatTranslate (matrix3x4_t *out, float x, float y, float z)
 {
        matrix3x4_t base, temp;
-       Matrix3x4_Copy(out, &base);
+       base = *out;
        Matrix3x4_CreateTranslate(&temp, x, y, z);
        Matrix3x4_Concat(out, &base, &temp);
 }
@@ -707,7 +748,7 @@ void Matrix3x4_ConcatTranslate (matrix3x4_t *out, float x, float y, float z)
 void Matrix3x4_ConcatRotate (matrix3x4_t *out, float angle, float x, float y, float z)
 {
        matrix3x4_t base, temp;
-       Matrix3x4_Copy(out, &base);
+       base = *out;
        Matrix3x4_CreateRotate(&temp, angle, x, y, z);
        Matrix3x4_Concat(out, &base, &temp);
 }
@@ -716,7 +757,7 @@ void Matrix3x4_ConcatRotate (matrix3x4_t *out, float angle, float x, float y, fl
 void Matrix3x4_ConcatScale (matrix3x4_t *out, float x)
 {
        matrix3x4_t base, temp;
-       Matrix3x4_Copy(out, &base);
+       base = *out;
        Matrix3x4_CreateScale(&temp, x);
        Matrix3x4_Concat(out, &base, &temp);
 }
@@ -725,7 +766,16 @@ void Matrix3x4_ConcatScale (matrix3x4_t *out, float x)
 void Matrix3x4_ConcatScale3 (matrix3x4_t *out, float x, float y, float z)
 {
        matrix3x4_t base, temp;
-       Matrix3x4_Copy(out, &base);
+       base = *out;
        Matrix3x4_CreateScale3(&temp, x, y, z);
        Matrix3x4_Concat(out, &base, &temp);
 }
+
+void Matrix3x4_Print (const matrix3x4_t *in)
+{
+       Con_Printf("%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n"
+       , in->m[0][0], in->m[0][1], in->m[0][2], in->m[0][3]
+       , in->m[1][0], in->m[1][1], in->m[1][2], in->m[1][3]
+       , in->m[2][0], in->m[2][1], in->m[2][2], in->m[2][3]);
+}
+