diff options
Diffstat (limited to 'sdl/math3d.c')
-rw-r--r-- | sdl/math3d.c | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/sdl/math3d.c b/sdl/math3d.c index 3277ede..ca60233 100644 --- a/sdl/math3d.c +++ b/sdl/math3d.c @@ -1,5 +1,3 @@ -/* revision 7 */ - /* @2010 Kamil Kaminski * * math3d.c @@ -12,7 +10,7 @@ #include "math3d.h" void m3dFindNormalf(M3DVector3f result, const M3DVector3f point1, - const M3DVector3f point2, const M3DVector3f point3) + const M3DVector3f point2, const M3DVector3f point3) { M3DVector3f v1, v2; @@ -59,7 +57,7 @@ void m3dLoadIdentity44f(M3DMatrix44f m) /* 4x4 float */ * planeEq contains the a, b, c, and d of the plane equation coefficients */ void m3dGetPlaneEquationf(M3DVector4f planeEq, const M3DVector3f p1, - const M3DVector3f p2, const M3DVector3f p3) + const M3DVector3f p2, const M3DVector3f p3) { /* get two vectors... do the cross product */ M3DVector3f v1, v2; @@ -182,7 +180,7 @@ void m3dMakePlanarShadowMatrixf(M3DMatrix44f proj, const M3DVector4f planeEq, /* hmm not sure if defines could reside outside of a function as they did */ void m3dMatrixMultiply44f(M3DMatrix44f product, const M3DMatrix44f a, - const M3DMatrix44f b) + const M3DMatrix44f b) { #define A(row,col) a[(col<<2)+row] #define B(row,col) b[(col<<2)+row] @@ -223,3 +221,55 @@ void m3dPrintMatrix44f(const M3DMatrix44f m) #undef M } +/* invert 4x4 matrix, contributed by David Moore (See Mesa bug #6748) */ +int m3dInvertMatrix44f(M3DMatrix44f dst, const M3DMatrix44f m) +{ + M3DMatrix44f inv; + double det; + int i; + + inv[0] = m[5]*m[10]*m[15] - m[5]*m[11]*m[14] - m[9]*m[6]*m[15] + + m[9]*m[7]*m[14] + m[13]*m[6]*m[11] - m[13]*m[7]*m[10]; + inv[4] = -m[4]*m[10]*m[15] + m[4]*m[11]*m[14] + m[8]*m[6]*m[15] + - m[8]*m[7]*m[14] - m[12]*m[6]*m[11] + m[12]*m[7]*m[10]; + inv[8] = m[4]*m[9]*m[15] - m[4]*m[11]*m[13] - m[8]*m[5]*m[15] + + m[8]*m[7]*m[13] + m[12]*m[5]*m[11] - m[12]*m[7]*m[9]; + inv[12] = -m[4]*m[9]*m[14] + m[4]*m[10]*m[13] + m[8]*m[5]*m[14] + - m[8]*m[6]*m[13] - m[12]*m[5]*m[10] + m[12]*m[6]*m[9]; + inv[1] = -m[1]*m[10]*m[15] + m[1]*m[11]*m[14] + m[9]*m[2]*m[15] + - m[9]*m[3]*m[14] - m[13]*m[2]*m[11] + m[13]*m[3]*m[10]; + inv[5] = m[0]*m[10]*m[15] - m[0]*m[11]*m[14] - m[8]*m[2]*m[15] + + m[8]*m[3]*m[14] + m[12]*m[2]*m[11] - m[12]*m[3]*m[10]; + inv[9] = -m[0]*m[9]*m[15] + m[0]*m[11]*m[13] + m[8]*m[1]*m[15] + - m[8]*m[3]*m[13] - m[12]*m[1]*m[11] + m[12]*m[3]*m[9]; + inv[13] = m[0]*m[9]*m[14] - m[0]*m[10]*m[13] - m[8]*m[1]*m[14] + + m[8]*m[2]*m[13] + m[12]*m[1]*m[10] - m[12]*m[2]*m[9]; + inv[2] = m[1]*m[6]*m[15] - m[1]*m[7]*m[14] - m[5]*m[2]*m[15] + + m[5]*m[3]*m[14] + m[13]*m[2]*m[7] - m[13]*m[3]*m[6]; + inv[6] = -m[0]*m[6]*m[15] + m[0]*m[7]*m[14] + m[4]*m[2]*m[15] + - m[4]*m[3]*m[14] - m[12]*m[2]*m[7] + m[12]*m[3]*m[6]; + inv[10] = m[0]*m[5]*m[15] - m[0]*m[7]*m[13] - m[4]*m[1]*m[15] + + m[4]*m[3]*m[13] + m[12]*m[1]*m[7] - m[12]*m[3]*m[5]; + inv[14] = -m[0]*m[5]*m[14] + m[0]*m[6]*m[13] + m[4]*m[1]*m[14] + - m[4]*m[2]*m[13] - m[12]*m[1]*m[6] + m[12]*m[2]*m[5]; + inv[3] = -m[1]*m[6]*m[11] + m[1]*m[7]*m[10] + m[5]*m[2]*m[11] + - m[5]*m[3]*m[10] - m[9]*m[2]*m[7] + m[9]*m[3]*m[6]; + inv[7] = m[0]*m[6]*m[11] - m[0]*m[7]*m[10] - m[4]*m[2]*m[11] + + m[4]*m[3]*m[10] + m[8]*m[2]*m[7] - m[8]*m[3]*m[6]; + inv[11] = -m[0]*m[5]*m[11] + m[0]*m[7]*m[9] + m[4]*m[1]*m[11] + - m[4]*m[3]*m[9] - m[8]*m[1]*m[7] + m[8]*m[3]*m[5]; + inv[15] = m[0]*m[5]*m[10] - m[0]*m[6]*m[9] - m[4]*m[1]*m[10] + + m[4]*m[2]*m[9] + m[8]*m[1]*m[6] - m[8]*m[2]*m[5]; + + det = m[0]*inv[0] + m[1]*inv[4] + m[2]*inv[8] + m[3]*inv[12]; + if (det == 0) + return -1; + + det = 1.0 / det; + + for (i = 0; i < 16; i++) + dst[i] = inv[i] * det; + + return 0; +} + |