diff --git a/src/api/l_vectors.c b/src/api/l_vectors.c index cca25f3a..1e8ae2cd 100644 --- a/src/api/l_vectors.c +++ b/src/api/l_vectors.c @@ -1436,12 +1436,10 @@ static int l_lovrMat4Unpack(lua_State* L) { } return 16; } else { - float angle, ax, ay, az; - float position[4], orientation[4], scale[4]; + float position[4], scale[4], angle, ax, ay, az; mat4_getPosition(m, position); mat4_getScale(m, scale); - mat4_getOrientation(m, orientation); - quat_getAngleAxis(orientation, &angle, &ax, &ay, &az); // TODO mat4_getAngleAxis (see math.lua) + mat4_getAngleAxis(m, &angle, &ax, &ay, &az); lua_pushnumber(L, position[0]); lua_pushnumber(L, position[1]); lua_pushnumber(L, position[2]); diff --git a/src/core/maf.h b/src/core/maf.h index 5d89b67b..223d0ff0 100644 --- a/src/core/maf.h +++ b/src/core/maf.h @@ -558,6 +558,22 @@ MAF void mat4_getOrientation(mat4 m, quat orientation) { quat_fromMat4(orientation, m); } +MAF void mat4_getAngleAxis(mat4 m, float* angle, float* ax, float* ay, float* az) { + float sx = vec3_length(m + 0); + float sy = vec3_length(m + 4); + float sz = vec3_length(m + 8); + float diagonal[4] = { m[0], m[5], m[10] }; + float axis[4] = { m[6] - m[9], m[8] - m[2], m[1] - m[4] }; + diagonal[0] /= sx; + diagonal[1] /= sy; + diagonal[2] /= sz; + vec3_normalize(axis); + *angle = acosf((diagonal[0] + diagonal[1] + diagonal[2] - 1.f) / 2.f); + *ax = axis[0]; + *ay = axis[1]; + *az = axis[2]; +} + MAF void mat4_getScale(mat4 m, vec3 scale) { vec3_set(scale, vec3_length(m + 0), vec3_length(m + 4), vec3_length(m + 8)); }