|
| 1 | +#include <Maths/Math.h> |
| 2 | + |
| 3 | +#include <glm/gtx/matrix_decompose.hpp> |
| 4 | + |
| 5 | +namespace glm |
| 6 | +{ |
| 7 | + |
| 8 | + bool DecomposeTransformComponent(const Matrix4& transform, Vector3& translation, Vector3& rotation, Vector3& scale) |
| 9 | + { |
| 10 | + // From glm::decompose in matrix_decompose.inl |
| 11 | + |
| 12 | + using T = float; |
| 13 | + |
| 14 | + mat4 LocalMatrix(transform); |
| 15 | + |
| 16 | + // Normalize the matrix. |
| 17 | + if (epsilonEqual(LocalMatrix[3][3], static_cast<float>(0), epsilon<T>())) |
| 18 | + return false; |
| 19 | + |
| 20 | + // First, isolate perspective. This is the messiest. |
| 21 | + if (epsilonNotEqual(LocalMatrix[0][3], static_cast<T>(0), epsilon<T>()) || epsilonNotEqual(LocalMatrix[1][3], static_cast<T>(0), epsilon<T>()) || epsilonNotEqual(LocalMatrix[2][3], static_cast<T>(0), epsilon<T>())) |
| 22 | + { |
| 23 | + // Clear the perspective partition |
| 24 | + LocalMatrix[0][3] = LocalMatrix[1][3] = LocalMatrix[2][3] = static_cast<T>(0); |
| 25 | + LocalMatrix[3][3] = static_cast<T>(1); |
| 26 | + } |
| 27 | + |
| 28 | + // Next take care of translation (easy). |
| 29 | + translation = vec3(LocalMatrix[3]); |
| 30 | + LocalMatrix[3] = vec4(0, 0, 0, LocalMatrix[3].w); |
| 31 | + |
| 32 | + vec3 Row[3], Pdum3; |
| 33 | + |
| 34 | + // Now get scale and shear. |
| 35 | + for (length_t i = 0; i < 3; ++i) |
| 36 | + for (length_t j = 0; j < 3; ++j) |
| 37 | + Row[i][j] = LocalMatrix[i][j]; |
| 38 | + |
| 39 | + // Compute X scale factor and normalize first row. |
| 40 | + scale.x = length(Row[0]); |
| 41 | + Row[0] = detail::scale(Row[0], static_cast<T>(1)); |
| 42 | + scale.y = length(Row[1]); |
| 43 | + Row[1] = detail::scale(Row[1], static_cast<T>(1)); |
| 44 | + scale.z = length(Row[2]); |
| 45 | + Row[2] = detail::scale(Row[2], static_cast<T>(1)); |
| 46 | + |
| 47 | + // At this point, the matrix (in rows[]) is orthonormal. |
| 48 | + // Check for a coordinate system flip. If the determinant |
| 49 | + // is -1, then negate the matrix and the scaling factors. |
| 50 | + |
| 51 | + rotation.y = asin(-Row[0][2]); |
| 52 | + if (cos(rotation.y) != 0) |
| 53 | + { |
| 54 | + rotation.x = atan2(Row[1][2], Row[2][2]); |
| 55 | + rotation.z = atan2(Row[0][1], Row[0][0]); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + rotation.x = atan2(-Row[2][0], Row[1][1]); |
| 60 | + rotation.z = 0; |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | +} // namespace glm |
0 commit comments