-#include "matrixlib.h"
#include <math.h>
+#include "matrixlib.h"
+
+const matrix4x4_t identitymatrix =
+{
+ {
+ {1, 0, 0, 0},
+ {0, 1, 0, 0},
+ {0, 0, 1, 0},
+ {0, 0, 0, 1}
+ }
+};
void Matrix4x4_Copy (matrix4x4_t *out, const matrix4x4_t *in)
{
{
double angle, sr, sp, sy, cr, cp, cy;
- angle = yaw * (M_PI*2 / 360);
- sy = sin(angle);
- cy = cos(angle);
- angle = pitch * (M_PI*2 / 360);
- sp = sin(angle);
- cp = cos(angle);
- angle = roll * (M_PI*2 / 360);
- sr = sin(angle);
- cr = cos(angle);
- out->m[0][0] = (float)((cp*cy) * scale);
- out->m[0][1] = (float)((sr*sp*cy+cr*-sy) * scale);
- out->m[0][2] = (float)((cr*sp*cy+-sr*-sy) * scale);
- out->m[0][3] = x;
- out->m[1][0] = (float)((cp*sy) * scale);
- out->m[1][1] = (float)((sr*sp*sy+cr*cy) * scale);
- out->m[1][2] = (float)((cr*sp*sy+-sr*cy) * scale);
- out->m[1][3] = y;
- out->m[2][0] = (float)((-sp) * scale);
- out->m[2][1] = (float)((sr*cp) * scale);
- out->m[2][2] = (float)((cr*cp) * scale);
- out->m[2][3] = z;
- out->m[3][0] = 0;
- out->m[3][1] = 0;
- out->m[3][2] = 0;
- out->m[3][3] = 1;
+ if (roll)
+ {
+ angle = yaw * (M_PI*2 / 360);
+ sy = sin(angle);
+ cy = cos(angle);
+ angle = pitch * (M_PI*2 / 360);
+ sp = sin(angle);
+ cp = cos(angle);
+ angle = roll * (M_PI*2 / 360);
+ sr = sin(angle);
+ cr = cos(angle);
+ out->m[0][0] = (float)((cp*cy) * scale);
+ out->m[0][1] = (float)((sr*sp*cy+cr*-sy) * scale);
+ out->m[0][2] = (float)((cr*sp*cy+-sr*-sy) * scale);
+ out->m[0][3] = x;
+ out->m[1][0] = (float)((cp*sy) * scale);
+ out->m[1][1] = (float)((sr*sp*sy+cr*cy) * scale);
+ out->m[1][2] = (float)((cr*sp*sy+-sr*cy) * scale);
+ out->m[1][3] = y;
+ out->m[2][0] = (float)((-sp) * scale);
+ out->m[2][1] = (float)((sr*cp) * scale);
+ out->m[2][2] = (float)((cr*cp) * scale);
+ out->m[2][3] = z;
+ out->m[3][0] = 0;
+ out->m[3][1] = 0;
+ out->m[3][2] = 0;
+ out->m[3][3] = 1;
+ }
+ else if (pitch)
+ {
+ angle = yaw * (M_PI*2 / 360);
+ sy = sin(angle);
+ cy = cos(angle);
+ angle = pitch * (M_PI*2 / 360);
+ sp = sin(angle);
+ cp = cos(angle);
+ out->m[0][0] = (float)((cp*cy) * scale);
+ out->m[0][1] = (float)((-sy) * scale);
+ out->m[0][2] = (float)((sp*cy) * scale);
+ out->m[0][3] = x;
+ out->m[1][0] = (float)((cp*sy) * scale);
+ out->m[1][1] = (float)((cy) * scale);
+ out->m[1][2] = (float)((sp*sy) * scale);
+ out->m[1][3] = y;
+ out->m[2][0] = (float)((-sp) * scale);
+ out->m[2][1] = 0;
+ out->m[2][2] = (float)((cp) * scale);
+ out->m[2][3] = z;
+ out->m[3][0] = 0;
+ out->m[3][1] = 0;
+ out->m[3][2] = 0;
+ out->m[3][3] = 1;
+ }
+ else if (yaw)
+ {
+ angle = yaw * (M_PI*2 / 360);
+ sy = sin(angle);
+ cy = cos(angle);
+ out->m[0][0] = (float)((cy) * scale);
+ out->m[0][1] = (float)((-sy) * scale);
+ out->m[0][2] = 0;
+ out->m[0][3] = x;
+ out->m[1][0] = (float)((sy) * scale);
+ out->m[1][1] = (float)((cy) * scale);
+ out->m[1][2] = 0;
+ out->m[1][3] = y;
+ out->m[2][0] = 0;
+ out->m[2][1] = 0;
+ out->m[2][2] = scale;
+ out->m[2][3] = z;
+ out->m[3][0] = 0;
+ out->m[3][1] = 0;
+ out->m[3][2] = 0;
+ out->m[3][3] = 1;
+ }
+ else
+ {
+ out->m[0][0] = scale;
+ out->m[0][1] = 0;
+ out->m[0][2] = 0;
+ out->m[0][3] = x;
+ out->m[1][0] = 0;
+ out->m[1][1] = scale;
+ out->m[1][2] = 0;
+ out->m[1][3] = y;
+ out->m[2][0] = 0;
+ out->m[2][1] = 0;
+ out->m[2][2] = scale;
+ out->m[2][3] = z;
+ out->m[3][0] = 0;
+ out->m[3][1] = 0;
+ out->m[3][2] = 0;
+ out->m[3][3] = 1;
+ }
}
void Matrix4x4_ToVectors(const matrix4x4_t *in, float vx[3], float vy[3], float vz[3], float t[3])
out->m[3][3] = 1.0f;
}
+void Matrix4x4_Blend (matrix4x4_t *out, const matrix4x4_t *in1, const matrix4x4_t *in2, float blend)
+{
+ float iblend = 1 - blend;
+ out->m[0][0] = in1->m[0][0] * iblend + in2->m[0][0] * blend;
+ out->m[0][1] = in1->m[0][1] * iblend + in2->m[0][1] * blend;
+ out->m[0][2] = in1->m[0][2] * iblend + in2->m[0][2] * blend;
+ out->m[0][3] = in1->m[0][3] * iblend + in2->m[0][3] * blend;
+ out->m[1][0] = in1->m[1][0] * iblend + in2->m[1][0] * blend;
+ out->m[1][1] = in1->m[1][1] * iblend + in2->m[1][1] * blend;
+ out->m[1][2] = in1->m[1][2] * iblend + in2->m[1][2] * blend;
+ out->m[1][3] = in1->m[1][3] * iblend + in2->m[1][3] * blend;
+ out->m[2][0] = in1->m[2][0] * iblend + in2->m[2][0] * blend;
+ out->m[2][1] = in1->m[2][1] * iblend + in2->m[2][1] * blend;
+ out->m[2][2] = in1->m[2][2] * iblend + in2->m[2][2] * blend;
+ out->m[2][3] = in1->m[2][3] * iblend + in2->m[2][3] * blend;
+ out->m[3][0] = in1->m[3][0] * iblend + in2->m[3][0] * blend;
+ out->m[3][1] = in1->m[3][1] * iblend + in2->m[3][1] * blend;
+ out->m[3][2] = in1->m[3][2] * iblend + in2->m[3][2] * blend;
+ out->m[3][3] = in1->m[3][3] * iblend + in2->m[3][3] * blend;
+}
+
+
void Matrix4x4_Transform (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] + in->m[0][3];
Matrix4x4_Concat(out, &base, &temp);
}
+void Matrix4x4_OriginFromMatrix (const matrix4x4_t *in, float *out)
+{
+ out[0] = in->m[0][3];
+ out[1] = in->m[1][3];
+ out[2] = in->m[2][3];
+}
+
+float Matrix4x4_ScaleFromMatrix (const matrix4x4_t *in)
+{
+ // we only support uniform scaling, so assume the first row is enough
+ return (float)sqrt(in->m[0][0] * in->m[0][0] + in->m[0][1] * in->m[0][1] + in->m[0][2] * in->m[0][2]);
+}
+