Utility: Refactor some algorithms
This commit is contained in:
parent
7c2b8e0576
commit
6469ab5fde
|
|
@ -86,6 +86,7 @@ namespace Nz
|
|||
static Quaternion LookAt(const Vector3<T>& forward, const Vector3<T>& up);
|
||||
static Quaternion Normalize(const Quaternion& quat, T* length = nullptr);
|
||||
static Quaternion RotationBetween(const Vector3<T>& from, const Vector3<T>& to);
|
||||
static Quaternion Mirror(Quaternion quat, const Vector3<T>& axis);
|
||||
static Quaternion Slerp(const Quaternion& from, const Quaternion& to, T interpolation);
|
||||
static Quaternion Zero();
|
||||
|
||||
|
|
|
|||
|
|
@ -787,6 +787,20 @@ namespace Nz
|
|||
return quaternion;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Quaternion<T> Quaternion<T>::Mirror(Quaternion quat, const Vector3<T>& axis)
|
||||
{
|
||||
float x = std::copysign(T(1.0), axis.x);
|
||||
float y = std::copysign(T(1.0), axis.y);
|
||||
float z = std::copysign(T(1.0), axis.z);
|
||||
|
||||
quat.x = y * z * quat.x;
|
||||
quat.y = x * z * quat.y;
|
||||
quat.z = x * y * quat.z;
|
||||
|
||||
return quat;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Interpolates spherically the quaternion to other one with a factor of interpolation
|
||||
* \return A new quaternion which is the interpolation of two quaternions
|
||||
|
|
|
|||
|
|
@ -64,11 +64,17 @@ namespace Nz
|
|||
NAZARA_UTILITY_API void SkinPositionNormal(const SkinningData& data, UInt64 startVertex, UInt64 vertexCount);
|
||||
NAZARA_UTILITY_API void SkinPositionNormalTangent(const SkinningData& data, UInt64 startVertex, UInt64 vertexCount);
|
||||
|
||||
NAZARA_UTILITY_API void TransformVertices(VertexPointers vertexPointers, UInt64 vertexCount, const Matrix4f& matrix);
|
||||
inline Vector3f TransformPositionTRS(const Vector3f& transformTranslation, const Quaternionf& transformRotation, const Vector3f& transformScale, const Vector3f& position);
|
||||
inline Vector3f TransformNormalTRS(const Quaternionf& transformRotation, const Vector3f& transformScale, const Vector3f& normal);
|
||||
inline Quaternionf TransformRotationTRS(const Quaternionf& transformRotation, const Vector3f& transformScale, const Quaternionf& rotation);
|
||||
inline Vector3f TransformScaleTRS(const Vector3f& transformScale, const Vector3f& scale);
|
||||
inline void TransformTRS(const Vector3f& transformTranslation, const Quaternionf& transformRotation, const Vector3f& transformScale, Vector3f& position, Quaternionf& rotation, Vector3f& scale);
|
||||
inline void TransformVertices(VertexPointers vertexPointers, UInt64 vertexCount, const Matrix4f& matrix);
|
||||
|
||||
template<typename T> constexpr ComponentType ComponentTypeId();
|
||||
template<typename T> constexpr ComponentType GetComponentTypeOf();
|
||||
}
|
||||
|
||||
#include <Nazara/Utility/Algorithm.inl>
|
||||
|
||||
#endif // NAZARA_UTILITY_ALGORITHM_HPP
|
||||
|
|
|
|||
|
|
@ -8,6 +8,59 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
inline Vector3f TransformPositionTRS(const Vector3f& transformTranslation, const Quaternionf& transformRotation, const Vector3f& transformScale, const Vector3f& position)
|
||||
{
|
||||
return transformRotation * (transformScale * position) + transformTranslation;
|
||||
}
|
||||
|
||||
Vector3f TransformNormalTRS(const Quaternionf& transformRotation, const Vector3f& transformScale, const Vector3f& normal)
|
||||
{
|
||||
return Quaternionf::Mirror(transformRotation, transformScale) * normal;
|
||||
}
|
||||
|
||||
inline Quaternionf TransformRotationTRS(const Quaternionf& transformRotation, const Vector3f& transformScale, const Quaternionf& rotation)
|
||||
{
|
||||
return Quaternionf::Mirror(transformRotation, transformScale) * rotation;
|
||||
}
|
||||
|
||||
inline Vector3f TransformScaleTRS(const Vector3f& transformScale, const Vector3f& scale)
|
||||
{
|
||||
return transformScale * scale;
|
||||
}
|
||||
|
||||
inline void TransformTRS(const Vector3f& transformTranslation, const Quaternionf& transformRotation, const Vector3f& transformScale, Vector3f& position, Quaternionf& rotation, Vector3f& scale)
|
||||
{
|
||||
position = TransformPositionTRS(transformTranslation, transformRotation, transformScale, position);
|
||||
rotation = TransformRotationTRS(transformRotation, transformScale, rotation);
|
||||
scale = TransformScaleTRS(transformScale, scale);
|
||||
}
|
||||
|
||||
inline void TransformVertices(VertexPointers vertexPointers, UInt64 vertexCount, const Matrix4f& matrix)
|
||||
{
|
||||
if (vertexPointers.positionPtr)
|
||||
{
|
||||
for (UInt64 i = 0; i < vertexCount; ++i)
|
||||
*vertexPointers.positionPtr++ = matrix.Transform(*vertexPointers.positionPtr);
|
||||
}
|
||||
|
||||
if (vertexPointers.normalPtr || vertexPointers.tangentPtr)
|
||||
{
|
||||
Vector3f scale = matrix.GetScale();
|
||||
|
||||
if (vertexPointers.normalPtr)
|
||||
{
|
||||
for (UInt64 i = 0; i < vertexCount; ++i)
|
||||
*vertexPointers.normalPtr++ = matrix.Transform(*vertexPointers.normalPtr, 0.f) / scale;
|
||||
}
|
||||
|
||||
if (vertexPointers.tangentPtr)
|
||||
{
|
||||
for (UInt64 i = 0; i < vertexCount; ++i)
|
||||
*vertexPointers.tangentPtr++ = matrix.Transform(*vertexPointers.tangentPtr, 0.f) / scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> constexpr ComponentType ComponentTypeId()
|
||||
{
|
||||
static_assert(AlwaysFalse<T>::value, "This type cannot be used as a component.");
|
||||
|
|
|
|||
|
|
@ -108,8 +108,6 @@ namespace Nz
|
|||
virtual void UpdateDerived() const;
|
||||
virtual void UpdateTransformMatrix() const;
|
||||
|
||||
static Quaternionf ScaleQuaternion(const Vector3f& scale, Quaternionf quaternion);
|
||||
|
||||
mutable std::vector<Node*> m_childs;
|
||||
mutable Matrix4f m_transformMatrix;
|
||||
mutable Quaternionf m_derivedRotation;
|
||||
|
|
|
|||
|
|
@ -1156,32 +1156,4 @@ namespace Nz
|
|||
outputVertex++;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************Transform*********************************/
|
||||
|
||||
void TransformVertices(VertexPointers vertexPointers, UInt64 vertexCount, const Matrix4f& matrix)
|
||||
{
|
||||
if (vertexPointers.positionPtr)
|
||||
{
|
||||
for (UInt64 i = 0; i < vertexCount; ++i)
|
||||
*vertexPointers.positionPtr++ = matrix.Transform(*vertexPointers.positionPtr);
|
||||
}
|
||||
|
||||
if (vertexPointers.normalPtr || vertexPointers.tangentPtr)
|
||||
{
|
||||
Vector3f scale = matrix.GetScale();
|
||||
|
||||
if (vertexPointers.normalPtr)
|
||||
{
|
||||
for (UInt64 i = 0; i < vertexCount; ++i)
|
||||
*vertexPointers.normalPtr++ = matrix.Transform(*vertexPointers.normalPtr, 0.f) / scale;
|
||||
}
|
||||
|
||||
if (vertexPointers.tangentPtr)
|
||||
{
|
||||
for (UInt64 i = 0; i < vertexCount; ++i)
|
||||
*vertexPointers.tangentPtr++ = matrix.Transform(*vertexPointers.tangentPtr, 0.f) / scale;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <Nazara/Utility/Node.hpp>
|
||||
#include <Nazara/Core/Algorithm.hpp>
|
||||
#include <Nazara/Utility/Algorithm.hpp>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
|
@ -610,7 +611,7 @@ namespace Nz
|
|||
if (!m_derivedUpdated)
|
||||
UpdateDerived();
|
||||
|
||||
return m_derivedPosition + (m_derivedScale * (ScaleQuaternion(m_derivedScale, m_derivedRotation) * localPosition));
|
||||
return TransformPositionTRS(m_derivedPosition, m_derivedRotation, m_derivedScale, localPosition);
|
||||
}
|
||||
|
||||
Quaternionf Node::ToGlobalRotation(const Quaternionf& localRotation) const
|
||||
|
|
@ -618,7 +619,7 @@ namespace Nz
|
|||
if (!m_derivedUpdated)
|
||||
UpdateDerived();
|
||||
|
||||
return ScaleQuaternion(m_derivedScale, m_derivedRotation) * localRotation;
|
||||
return TransformRotationTRS(m_derivedRotation, m_derivedScale, localRotation);
|
||||
}
|
||||
|
||||
Vector3f Node::ToGlobalScale(const Vector3f& localScale) const
|
||||
|
|
@ -626,7 +627,7 @@ namespace Nz
|
|||
if (!m_derivedUpdated)
|
||||
UpdateDerived();
|
||||
|
||||
return m_derivedScale * localScale;
|
||||
return TransformScaleTRS(m_derivedScale, localScale);
|
||||
}
|
||||
|
||||
Vector3f Node::ToLocalPosition(const Vector3f& globalPosition) const
|
||||
|
|
@ -762,7 +763,7 @@ namespace Nz
|
|||
{
|
||||
Quaternionf rotation = m_initialRotation * m_rotation;
|
||||
if (m_inheritScale)
|
||||
rotation = ScaleQuaternion(m_parent->m_derivedScale, rotation);
|
||||
rotation = Quaternionf::Mirror(rotation, m_parent->m_derivedScale);
|
||||
|
||||
m_derivedRotation = m_parent->m_derivedRotation * rotation;
|
||||
m_derivedRotation.Normalize();
|
||||
|
|
@ -792,27 +793,4 @@ namespace Nz
|
|||
m_transformMatrix.MakeTransform(m_derivedPosition, m_derivedRotation, m_derivedScale);
|
||||
m_transformMatrixUpdated = true;
|
||||
}
|
||||
|
||||
Quaternionf Node::ScaleQuaternion(const Vector3f& scale, Quaternionf quaternion)
|
||||
{
|
||||
if (std::signbit(scale.x))
|
||||
{
|
||||
quaternion.z = -quaternion.z;
|
||||
quaternion.y = -quaternion.y;
|
||||
}
|
||||
|
||||
if (std::signbit(scale.y))
|
||||
{
|
||||
quaternion.x = -quaternion.x;
|
||||
quaternion.z = -quaternion.z;
|
||||
}
|
||||
|
||||
if (std::signbit(scale.z))
|
||||
{
|
||||
quaternion.x = -quaternion.x;
|
||||
quaternion.y = -quaternion.y;
|
||||
}
|
||||
|
||||
return quaternion;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue