Clean up to close model matrix decompose #227

This commit is contained in:
Christophe Riccio 2016-03-06 02:13:45 +01:00
parent 58f432b76d
commit ecfebe640f
2 changed files with 15 additions and 22 deletions

View File

@ -45,6 +45,7 @@
#include "../mat4x4.hpp"
#include "../vec3.hpp"
#include "../vec4.hpp"
#include "../geometric.hpp"
#include "../gtc/quaternion.hpp"
#include "../gtc/matrix_transform.hpp"

View File

@ -30,7 +30,8 @@
/// @author Christophe Riccio
///////////////////////////////////////////////////////////////////////////////////
namespace glm
namespace glm{
namespace detail
{
/// Make a linear combination of two vectors and return the result.
// result = (a * ascl) + (b * bscl)
@ -44,24 +45,15 @@ namespace glm
}
template <typename T, precision P>
GLM_FUNC_QUALIFIER void v3Scale(tvec3<T, P> & v, T desiredLength)
GLM_FUNC_QUALIFIER tvec3<T, P> scale(tvec3<T, P> const& v, T desiredLength)
{
T len = glm::length(v);
if(len != 0)
{
T l = desiredLength / len;
v[0] *= l;
v[1] *= l;
v[2] *= l;
}
return v * desiredLength / length(v);
}
}//namespace detail
/**
* Matrix decompose
* http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp
* Decomposes the mode matrix to translations,rotation scale components
*
*/
// Matrix decompose
// http://www.opensource.apple.com/source/WebCore/WebCore-514/platform/graphics/transforms/TransformationMatrix.cpp
// Decomposes the mode matrix to translations,rotation scale components
template <typename T, precision P>
GLM_FUNC_QUALIFIER bool decompose(tmat4x4<T, P> const & ModelMatrix, tvec3<T, P> & Scale, tquat<T, P> & Orientation, tvec3<T, P> & Translation, tvec3<T, P> & Skew, tvec4<T, P> & Perspective)
@ -131,26 +123,26 @@ namespace glm
// Compute X scale factor and normalize first row.
Scale.x = length(Row[0]);// v3Length(Row[0]);
v3Scale(Row[0], static_cast<T>(1));
Row[0] = detail::scale(Row[0], static_cast<T>(1));
// Compute XY shear factor and make 2nd row orthogonal to 1st.
Skew.z = dot(Row[0], Row[1]);
Row[1] = combine(Row[1], Row[0], static_cast<T>(1), -Skew.z);
Row[1] = detail::combine(Row[1], Row[0], static_cast<T>(1), -Skew.z);
// Now, compute Y scale and normalize 2nd row.
Scale.y = length(Row[1]);
v3Scale(Row[1], static_cast<T>(1));
Row[1] = detail::scale(Row[1], static_cast<T>(1));
Skew.z /= Scale.y;
// Compute XZ and YZ shears, orthogonalize 3rd row.
Skew.y = glm::dot(Row[0], Row[2]);
Row[2] = combine(Row[2], Row[0], static_cast<T>(1), -Skew.y);
Row[2] = detail::combine(Row[2], Row[0], static_cast<T>(1), -Skew.y);
Skew.x = glm::dot(Row[1], Row[2]);
Row[2] = combine(Row[2], Row[1], static_cast<T>(1), -Skew.x);
Row[2] = detail::combine(Row[2], Row[1], static_cast<T>(1), -Skew.x);
// Next, get Z scale and normalize 3rd row.
Scale.z = length(Row[2]);
v3Scale(Row[2], static_cast<T>(1));
Row[2] = detail::scale(Row[2], static_cast<T>(1));
Skew.y /= Scale.z;
Skew.x /= Scale.z;