Improve mat4:unpack;

- Orientation is correct when the matrix is scaled
- Orientation calculation is more efficient
This commit is contained in:
bjorn 2019-10-04 19:26:09 -07:00
parent eb83962fc6
commit 0c642a6790
2 changed files with 18 additions and 4 deletions

View File

@ -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]);

View File

@ -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));
}