Moved skinning to algorithms
Former-commit-id: ab31467438f55e8381daa8f238c201c46ba96f52
This commit is contained in:
@@ -2,135 +2,13 @@
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Clock.hpp>
|
||||
|
||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||
#include <Nazara/Core/TaskScheduler.hpp>
|
||||
#include <Nazara/Utility/BufferMapper.hpp>
|
||||
#include <Nazara/Utility/Config.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/Skeleton.hpp>
|
||||
#include <Nazara/Utility/VertexStruct.hpp>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct SkinningInfos
|
||||
{
|
||||
const NzJoint* joints;
|
||||
const NzMeshVertex* inputVertex;
|
||||
NzMeshVertex* outputVertex;
|
||||
const NzVertexWeight* vertexWeights;
|
||||
const NzWeight* weights;
|
||||
};
|
||||
|
||||
void Skin_Position(const SkinningInfos& skinningInfos, unsigned int startVertex, unsigned int vertexCount)
|
||||
{
|
||||
const NzMeshVertex* inputVertex = &skinningInfos.inputVertex[startVertex];
|
||||
NzMeshVertex* outputVertex = &skinningInfos.outputVertex[startVertex];
|
||||
|
||||
unsigned int endVertex = startVertex + vertexCount - 1;
|
||||
for (unsigned int i = startVertex; i <= endVertex; ++i)
|
||||
{
|
||||
NzVector3f finalPosition(NzVector3f::Zero());
|
||||
|
||||
unsigned int weightCount = skinningInfos.vertexWeights[i].weights.size();
|
||||
for (unsigned int j = 0; j < weightCount; ++j)
|
||||
{
|
||||
const NzWeight& weight = skinningInfos.weights[skinningInfos.vertexWeights[i].weights[j]];
|
||||
|
||||
NzMatrix4f mat(skinningInfos.joints[weight.jointIndex].GetInverseBindMatrix());
|
||||
mat.ConcatenateAffine(skinningInfos.joints[weight.jointIndex].GetTransformMatrix());
|
||||
mat *= weight.weight;
|
||||
|
||||
finalPosition += mat.Transform(inputVertex->position);
|
||||
}
|
||||
|
||||
outputVertex->position = finalPosition;
|
||||
outputVertex->uv = inputVertex->uv;
|
||||
|
||||
inputVertex++;
|
||||
outputVertex++;
|
||||
}
|
||||
}
|
||||
|
||||
void Skin_PositionNormal(const SkinningInfos& skinningInfos, unsigned int startVertex, unsigned int vertexCount)
|
||||
{
|
||||
const NzMeshVertex* inputVertex = &skinningInfos.inputVertex[startVertex];
|
||||
NzMeshVertex* outputVertex = &skinningInfos.outputVertex[startVertex];
|
||||
|
||||
unsigned int endVertex = startVertex + vertexCount - 1;
|
||||
for (unsigned int i = startVertex; i <= endVertex; ++i)
|
||||
{
|
||||
NzVector3f finalPosition(NzVector3f::Zero());
|
||||
NzVector3f finalNormal(NzVector3f::Zero());
|
||||
|
||||
unsigned int weightCount = skinningInfos.vertexWeights[i].weights.size();
|
||||
for (unsigned int j = 0; j < weightCount; ++j)
|
||||
{
|
||||
const NzWeight& weight = skinningInfos.weights[skinningInfos.vertexWeights[i].weights[j]];
|
||||
|
||||
NzMatrix4f mat(skinningInfos.joints[weight.jointIndex].GetInverseBindMatrix());
|
||||
mat.ConcatenateAffine(skinningInfos.joints[weight.jointIndex].GetTransformMatrix());
|
||||
mat *= weight.weight;
|
||||
|
||||
finalPosition += mat.Transform(inputVertex->position);
|
||||
finalNormal += mat.Transform(inputVertex->normal, 0.f);
|
||||
}
|
||||
|
||||
finalNormal.Normalize();
|
||||
|
||||
outputVertex->normal = finalNormal;
|
||||
outputVertex->position = finalPosition;
|
||||
outputVertex->uv = inputVertex->uv;
|
||||
|
||||
inputVertex++;
|
||||
outputVertex++;
|
||||
}
|
||||
}
|
||||
|
||||
void Skin_PositionNormalTangent(const SkinningInfos& skinningInfos, unsigned int startVertex, unsigned int vertexCount)
|
||||
{
|
||||
const NzMeshVertex* inputVertex = &skinningInfos.inputVertex[startVertex];
|
||||
NzMeshVertex* outputVertex = &skinningInfos.outputVertex[startVertex];
|
||||
|
||||
unsigned int endVertex = startVertex + vertexCount - 1;
|
||||
for (unsigned int i = startVertex; i <= endVertex; ++i)
|
||||
{
|
||||
NzVector3f finalPosition(NzVector3f::Zero());
|
||||
NzVector3f finalNormal(NzVector3f::Zero());
|
||||
NzVector3f finalTangent(NzVector3f::Zero());
|
||||
|
||||
unsigned int weightCount = skinningInfos.vertexWeights[i].weights.size();
|
||||
for (unsigned int j = 0; j < weightCount; ++j)
|
||||
{
|
||||
const NzWeight& weight = skinningInfos.weights[skinningInfos.vertexWeights[i].weights[j]];
|
||||
|
||||
NzMatrix4f mat(skinningInfos.joints[weight.jointIndex].GetInverseBindMatrix());
|
||||
mat.ConcatenateAffine(skinningInfos.joints[weight.jointIndex].GetTransformMatrix());
|
||||
mat *= weight.weight;
|
||||
|
||||
finalPosition += mat.Transform(inputVertex->position);
|
||||
finalNormal += mat.Transform(inputVertex->normal, 0.f);
|
||||
finalTangent += mat.Transform(inputVertex->tangent, 0.f);
|
||||
}
|
||||
|
||||
finalNormal.Normalize();
|
||||
finalTangent.Normalize();
|
||||
|
||||
outputVertex->normal = finalNormal;
|
||||
outputVertex->position = finalPosition;
|
||||
outputVertex->tangent = finalTangent;
|
||||
outputVertex->uv = inputVertex->uv;
|
||||
|
||||
inputVertex++;
|
||||
outputVertex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NzSkeletalMeshImpl
|
||||
{
|
||||
std::unique_ptr<NzMeshVertex[]> bindPoseBuffer;
|
||||
@@ -336,56 +214,6 @@ bool NzSkeletalMesh::IsValid() const
|
||||
return m_impl != nullptr;
|
||||
}
|
||||
|
||||
void NzSkeletalMesh::Skin(NzMeshVertex* outputBuffer) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
Skin(outputBuffer, m_parent->GetSkeleton());
|
||||
}
|
||||
|
||||
void NzSkeletalMesh::Skin(NzMeshVertex* outputBuffer, const NzSkeleton* skeleton) const
|
||||
{
|
||||
#if NAZARA_UTILITY_SAFE
|
||||
if (!m_impl)
|
||||
{
|
||||
NazaraError("Skeletal mesh not created");
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
SkinningInfos skinningInfos;
|
||||
skinningInfos.inputVertex = m_impl->bindPoseBuffer.get();
|
||||
skinningInfos.outputVertex = outputBuffer;
|
||||
skinningInfos.joints = skeleton->GetJoints();
|
||||
skinningInfos.vertexWeights = &m_impl->vertexWeights[0];
|
||||
skinningInfos.weights = &m_impl->weights[0];
|
||||
|
||||
#if NAZARA_UTILITY_MULTITHREADED_SKINNING
|
||||
unsigned int jointCount = skeleton->GetJointCount();
|
||||
for (unsigned int i = 0; i < jointCount; ++i)
|
||||
skinningInfos.joints[i].EnsureTransformMatrixUpdate();
|
||||
|
||||
unsigned int workerCount = NzTaskScheduler::GetWorkerCount();
|
||||
|
||||
std::ldiv_t div = std::ldiv(m_impl->vertexCount, workerCount); // Qui sait, peut-être que ça permet des optimisations plus efficaces
|
||||
for (unsigned int i = 0; i < workerCount; ++i)
|
||||
NzTaskScheduler::AddTask(Skin_PositionNormalTangent, skinningInfos, i*div.quot, (i == workerCount-1) ? div.quot + div.rem : div.quot);
|
||||
|
||||
NzTaskScheduler::Run();
|
||||
NzTaskScheduler::WaitForTasks();
|
||||
#else
|
||||
Skin_PositionNormalTangent(skinningInfos, 0, m_impl->vertexCount);
|
||||
#endif
|
||||
|
||||
m_impl->aabb = skeleton->GetAABB(); ///FIXME: Qu'est-ce que ça fait encore là ça ?
|
||||
}
|
||||
|
||||
void NzSkeletalMesh::SetIndexBuffer(const NzIndexBuffer* indexBuffer)
|
||||
{
|
||||
m_impl->indexBuffer = indexBuffer;
|
||||
|
||||
Reference in New Issue
Block a user