Skip to content

Commit

Permalink
Clean up to close model matrix decompose g-truc#227
Browse files Browse the repository at this point in the history
  • Loading branch information
Groovounet committed Mar 6, 2016
1 parent 58f432b commit ecfebe6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
1 change: 1 addition & 0 deletions glm/gtx/matrix_decompose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
36 changes: 14 additions & 22 deletions glm/gtx/matrix_decompose.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit ecfebe6

Please sign in to comment.