Moved skinning to algorithms
Former-commit-id: ab31467438f55e8381daa8f238c201c46ba96f52
This commit is contained in:
parent
c8ae34f20b
commit
5310c8b0d6
|
|
@ -14,6 +14,16 @@
|
|||
#include <Nazara/Math/Vector3.hpp>
|
||||
#include <Nazara/Utility/IndexIterator.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||
|
||||
struct NzSkinningData
|
||||
{
|
||||
const NzJoint* joints;
|
||||
const NzMeshVertex* inputVertex;
|
||||
NzMeshVertex* outputVertex;
|
||||
const NzVertexWeight* vertexWeights;
|
||||
const NzWeight* weights;
|
||||
};
|
||||
|
||||
NAZARA_API void NzComputeBoxIndexVertexCount(const NzVector3ui& subdivision, unsigned int* indexCount, unsigned int* vertexCount);
|
||||
NAZARA_API unsigned int NzComputeCacheMissCount(NzIndexIterator indices, unsigned int indexCount);
|
||||
|
|
@ -33,6 +43,10 @@ NAZARA_API void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned
|
|||
|
||||
NAZARA_API void NzOptimizeIndices(NzIndexIterator indices, unsigned int indexCount);
|
||||
|
||||
NAZARA_API void NzSkinPosition(const NzSkinningData& data, unsigned int startVertex, unsigned int vertexCount);
|
||||
NAZARA_API void NzSkinPositionNormal(const NzSkinningData& data, unsigned int startVertex, unsigned int vertexCount);
|
||||
NAZARA_API void NzSkinPositionNormalTangent(const NzSkinningData& data, unsigned int startVertex, unsigned int vertexCount);
|
||||
|
||||
template<typename T> void NzTransformVertices(T* vertices, unsigned int vertexCount, const NzMatrix4f& matrix);
|
||||
|
||||
#include <Nazara/Utility/Algorithm.inl>
|
||||
|
|
|
|||
|
|
@ -55,9 +55,6 @@ class NAZARA_API NzSkeletalMesh final : public NzSubMesh
|
|||
bool IsAnimated() const final;
|
||||
bool IsValid() const;
|
||||
|
||||
void Skin(NzMeshVertex* outputBuffer) const;
|
||||
void Skin(NzMeshVertex* outputBuffer, const NzSkeleton* skeleton) const;
|
||||
|
||||
void SetIndexBuffer(const NzIndexBuffer* indexBuffer);
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -818,7 +818,7 @@ void NzGenerateCone(float length, float radius, unsigned int subdivision, const
|
|||
NzVector3f lExtend = NzVector3f::Left()*radius;
|
||||
NzVector3f fExtend = NzVector3f::Forward()*radius;
|
||||
|
||||
// Et on ajoute ensuite les quatres extrémités de la pyramide
|
||||
// Et on ajoute ensuite les quatres extrémités de la pyramide
|
||||
aabb->ExtendTo(base + lExtend + fExtend);
|
||||
aabb->ExtendTo(base + lExtend - fExtend);
|
||||
aabb->ExtendTo(base - lExtend + fExtend);
|
||||
|
|
@ -961,7 +961,7 @@ void NzGenerateUvSphere(float size, unsigned int sliceCount, unsigned int stackC
|
|||
}
|
||||
}
|
||||
|
||||
/************************************Autres***********************************/
|
||||
/************************************NzOptimize***********************************/
|
||||
|
||||
void NzOptimizeIndices(NzIndexIterator indices, unsigned int indexCount)
|
||||
{
|
||||
|
|
@ -969,3 +969,106 @@ void NzOptimizeIndices(NzIndexIterator indices, unsigned int indexCount)
|
|||
if (optimizer.Optimize(indices, indexCount) != VertexCacheOptimizer::Success)
|
||||
NazaraWarning("Indices optimizer failed");
|
||||
}
|
||||
|
||||
/************************************NzSkin***********************************/
|
||||
|
||||
void NzSkinPosition(const NzSkinningData& data, unsigned int startVertex, unsigned int vertexCount)
|
||||
{
|
||||
const NzMeshVertex* inputVertex = &data.inputVertex[startVertex];
|
||||
NzMeshVertex* outputVertex = &data.outputVertex[startVertex];
|
||||
|
||||
unsigned int endVertex = startVertex + vertexCount - 1;
|
||||
for (unsigned int i = startVertex; i <= endVertex; ++i)
|
||||
{
|
||||
NzVector3f finalPosition(NzVector3f::Zero());
|
||||
|
||||
unsigned int weightCount = data.vertexWeights[i].weights.size();
|
||||
for (unsigned int j = 0; j < weightCount; ++j)
|
||||
{
|
||||
const NzWeight& weight = data.weights[data.vertexWeights[i].weights[j]];
|
||||
|
||||
NzMatrix4f mat(data.joints[weight.jointIndex].GetSkinningMatrix());
|
||||
mat *= weight.weight;
|
||||
|
||||
finalPosition += mat.Transform(inputVertex->position);
|
||||
}
|
||||
|
||||
outputVertex->position = finalPosition;
|
||||
outputVertex->uv = inputVertex->uv;
|
||||
|
||||
inputVertex++;
|
||||
outputVertex++;
|
||||
}
|
||||
}
|
||||
|
||||
void NzSkinPositionNormal(const NzSkinningData& 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].GetSkinningMatrix());
|
||||
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 NzSkinPositionNormalTangent(const NzSkinningData& 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].GetSkinningMatrix());
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue