From: havoc Date: Mon, 11 Aug 2008 01:54:26 +0000 (+0000) Subject: ported some features over from darkwar matrixlib.[ch] X-Git-Tag: xonotic-v0.1.0preview~2139 X-Git-Url: http://de.git.xonotic.org/?a=commitdiff_plain;h=49527da111ad96c720d540effb651e9c32a4899b;p=xonotic%2Fdarkplaces.git ported some features over from darkwar matrixlib.[ch] git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8442 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/matrixlib.c b/matrixlib.c index a0499861..7b7bfad6 100644 --- a/matrixlib.c +++ b/matrixlib.c @@ -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; diff --git a/matrixlib.h b/matrixlib.h index 480dec15..8e1e6537 100644 --- a/matrixlib.h +++ b/matrixlib.h @@ -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);