Merge remote-tracking branch 'origin/RenderTechnique-Update'
Former-commit-id: 85e5af81ed3381af19e87c6ca0ee14e79955e563
This commit is contained in:
commit
b70f693b8c
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright (C) 2014 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SKINNINGMANAGER_HPP
|
||||
#define NAZARA_SKINNINGMANAGER_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
|
||||
class NzSkeleton;
|
||||
class NzSkeletalMesh;
|
||||
class NzVertexBuffer;
|
||||
|
||||
class NAZARA_API NzSkinningManager
|
||||
{
|
||||
friend class NzGraphics;
|
||||
|
||||
public:
|
||||
using SkinFunction = void (*)(const NzSkeletalMesh* mesh, const NzSkeleton* skeleton, NzVertexBuffer* buffer);
|
||||
|
||||
NzSkinningManager() = delete;
|
||||
~NzSkinningManager() = delete;
|
||||
|
||||
static NzVertexBuffer* GetBuffer(const NzSkeletalMesh* mesh, const NzSkeleton* skeleton);
|
||||
static void Skin();
|
||||
|
||||
private:
|
||||
static bool Initialize();
|
||||
static void Uninitialize();
|
||||
|
||||
static SkinFunction s_skinFunc;
|
||||
};
|
||||
|
||||
#endif // NAZARA_SKINNINGMANAGER_HPP
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -21,18 +21,26 @@ class NAZARA_API NzJoint : public NzNode
|
|||
NzJoint(const NzJoint& joint);
|
||||
~NzJoint() = default;
|
||||
|
||||
NzMatrix4f GetInverseBindMatrix() const;
|
||||
void EnsureSkinningMatrixUpdate() const;
|
||||
|
||||
const NzMatrix4f& GetInverseBindMatrix() const;
|
||||
NzString GetName() const;
|
||||
NzSkeleton* GetSkeleton();
|
||||
const NzSkeleton* GetSkeleton() const;
|
||||
const NzMatrix4f& GetSkinningMatrix() const;
|
||||
|
||||
void SetInverseBindMatrix(const NzMatrix4f& matrix);
|
||||
void SetName(const NzString& name);
|
||||
|
||||
private:
|
||||
void InvalidateNode();
|
||||
void UpdateSkinningMatrix() const;
|
||||
|
||||
NzMatrix4f m_inverseBindMatrix;
|
||||
mutable NzMatrix4f m_skinningMatrix;
|
||||
NzString m_name;
|
||||
NzSkeleton* m_skeleton;
|
||||
mutable bool m_skinningMatrixUpdated;
|
||||
};
|
||||
|
||||
#endif // NAZARA_JOINT_HPP
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
#include <Nazara/Graphics/ForwardRenderTechnique.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/RenderTechniques.hpp>
|
||||
#include <Nazara/Graphics/SkinningManager.hpp>
|
||||
#include <Nazara/Graphics/Loaders/Mesh.hpp>
|
||||
#include <Nazara/Graphics/Loaders/OBJ.hpp>
|
||||
#include <Nazara/Graphics/Loaders/Texture.hpp>
|
||||
|
|
@ -39,7 +40,13 @@ bool NzGraphics::Initialize()
|
|||
|
||||
if (!NzMaterial::Initialize())
|
||||
{
|
||||
NazaraError("Failed to create material");
|
||||
NazaraError("Failed to initialize materials");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!NzSkinningManager::Initialize())
|
||||
{
|
||||
NazaraError("Failed to initialize skinning cache");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -89,9 +96,9 @@ void NzGraphics::Uninitialize()
|
|||
NzLoaders_OBJ_Unregister();
|
||||
NzLoaders_Texture_Unregister();
|
||||
|
||||
NzMaterial::Uninitialize();
|
||||
|
||||
NzDeferredRenderTechnique::Uninitialize();
|
||||
NzMaterial::Uninitialize();
|
||||
NzSkinningManager::Uninitialize();
|
||||
|
||||
NazaraNotice("Uninitialized: Graphics module");
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
#include <Nazara/Graphics/ColorBackground.hpp>
|
||||
#include <Nazara/Graphics/RenderTechniques.hpp>
|
||||
#include <Nazara/Graphics/SceneRoot.hpp>
|
||||
#include <Nazara/Graphics/SkinningManager.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
|
@ -240,6 +241,8 @@ void NzScene::Update()
|
|||
|
||||
void NzScene::UpdateVisible()
|
||||
{
|
||||
NzSkinningManager::Skin();
|
||||
|
||||
if (m_impl->update)
|
||||
{
|
||||
for (NzUpdatable* node : m_impl->visibleUpdateList)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,237 @@
|
|||
// Copyright (C) 2014 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Graphics/SkinningManager.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Core/ResourceListener.hpp>
|
||||
#include <Nazara/Core/TaskScheduler.hpp>
|
||||
#include <Nazara/Utility/Algorithm.hpp>
|
||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||
#include <Nazara/Utility/VertexBuffer.hpp>
|
||||
#include <Nazara/Utility/VertexMapper.hpp>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
enum ResourceType
|
||||
{
|
||||
ResourceType_SkeletalMesh,
|
||||
ResourceType_Skeleton,
|
||||
};
|
||||
|
||||
struct BufferData
|
||||
{
|
||||
NzVertexBufferRef buffer;
|
||||
bool updated;
|
||||
};
|
||||
|
||||
struct SkinningData
|
||||
{
|
||||
const NzSkeletalMesh* mesh;
|
||||
const NzSkeleton* skeleton;
|
||||
NzVertexBuffer* buffer;
|
||||
};
|
||||
|
||||
using MeshMap = std::unordered_map<const NzSkeletalMesh*, BufferData>;
|
||||
using SkeletonMap = std::unordered_map<const NzSkeleton*, MeshMap>;
|
||||
SkeletonMap s_cache;
|
||||
std::vector<SkinningData> s_skinningQueue;
|
||||
|
||||
class ResourceListener : public NzResourceListener
|
||||
{
|
||||
public:
|
||||
bool OnResourceDestroy(const NzResource* resource, int index)
|
||||
{
|
||||
switch (index)
|
||||
{
|
||||
case ResourceType_SkeletalMesh:
|
||||
{
|
||||
for (auto& pair : s_cache)
|
||||
{
|
||||
MeshMap& meshMap = pair.second;
|
||||
meshMap.erase(static_cast<const NzSkeletalMesh*>(resource));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ResourceType_Skeleton:
|
||||
s_cache.erase(static_cast<const NzSkeleton*>(resource));
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool OnResourceModified(const NzResource* resource, int index, unsigned int code)
|
||||
{
|
||||
NazaraUnused(code);
|
||||
|
||||
switch (index)
|
||||
{
|
||||
case ResourceType_SkeletalMesh:
|
||||
{
|
||||
for (auto& pair : s_cache)
|
||||
{
|
||||
MeshMap& meshMap = pair.second;
|
||||
for (auto& pair2 : meshMap)
|
||||
pair2.second.updated = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ResourceType_Skeleton:
|
||||
{
|
||||
for (auto& pair : s_cache.at(static_cast<const NzSkeleton*>(resource)))
|
||||
pair.second.updated = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void OnResourceReleased(const NzResource* resource, int index)
|
||||
{
|
||||
OnResourceDestroy(resource, index);
|
||||
}
|
||||
};
|
||||
|
||||
ResourceListener listener;
|
||||
|
||||
void Skin_MonoCPU(const NzSkeletalMesh* mesh, const NzSkeleton* skeleton, NzVertexBuffer* buffer)
|
||||
{
|
||||
NzBufferMapper<NzVertexBuffer> mapper(buffer, nzBufferAccess_DiscardAndWrite);
|
||||
|
||||
NzSkinningData skinningData;
|
||||
skinningData.inputVertex = mesh->GetBindPoseBuffer();
|
||||
skinningData.outputVertex = static_cast<NzMeshVertex*>(mapper.GetPointer());
|
||||
skinningData.joints = skeleton->GetJoints();
|
||||
skinningData.vertexWeights = mesh->GetVertexWeight(0);
|
||||
skinningData.weights = mesh->GetWeight(0);
|
||||
|
||||
NzSkinPositionNormalTangent(skinningData, 0, mesh->GetVertexCount());
|
||||
}
|
||||
|
||||
void Skin_MultiCPU(const NzSkeletalMesh* mesh, const NzSkeleton* skeleton, NzVertexBuffer* buffer)
|
||||
{
|
||||
NzBufferMapper<NzVertexBuffer> mapper(buffer, nzBufferAccess_DiscardAndWrite);
|
||||
|
||||
NzSkinningData skinningData;
|
||||
skinningData.inputVertex = mesh->GetBindPoseBuffer();
|
||||
skinningData.outputVertex = static_cast<NzMeshVertex*>(mapper.GetPointer());
|
||||
skinningData.joints = skeleton->GetJoints();
|
||||
skinningData.vertexWeights = mesh->GetVertexWeight(0);
|
||||
skinningData.weights = mesh->GetWeight(0);
|
||||
|
||||
// Afin d'empêcher les différents threads de vouloir mettre à jour la même matrice en même temps,
|
||||
// on se charge de la mettre à jour avant de les lancer
|
||||
unsigned int jointCount = skeleton->GetJointCount();
|
||||
for (unsigned int i = 0; i < jointCount; ++i)
|
||||
skinningData.joints[i].EnsureSkinningMatrixUpdate();
|
||||
|
||||
unsigned int workerCount = NzTaskScheduler::GetWorkerCount();
|
||||
|
||||
std::ldiv_t div = std::ldiv(mesh->GetVertexCount(), workerCount);
|
||||
for (unsigned int i = 0; i < workerCount; ++i)
|
||||
NzTaskScheduler::AddTask(NzSkinPositionNormalTangent, skinningData, i*div.quot, (i == workerCount-1) ? div.quot + div.rem : div.quot);
|
||||
|
||||
NzTaskScheduler::Run();
|
||||
NzTaskScheduler::WaitForTasks();
|
||||
}
|
||||
}
|
||||
|
||||
NzVertexBuffer* NzSkinningManager::GetBuffer(const NzSkeletalMesh* mesh, const NzSkeleton* skeleton)
|
||||
{
|
||||
#if NAZARA_GRAPHICS_SAFE
|
||||
if (!mesh)
|
||||
{
|
||||
NazaraError("Invalid mesh");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!skeleton)
|
||||
{
|
||||
NazaraError("Invalid skeleton");
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
NzErrorFlags flags(nzErrorFlag_ThrowException);
|
||||
|
||||
SkeletonMap::iterator it = s_cache.find(skeleton);
|
||||
if (it == s_cache.end())
|
||||
{
|
||||
it = s_cache.insert(std::make_pair(skeleton, SkeletonMap::mapped_type())).first;
|
||||
skeleton->AddResourceListener(&listener, ResourceType_Skeleton);
|
||||
}
|
||||
|
||||
NzVertexBuffer* buffer;
|
||||
|
||||
MeshMap& meshMap = it->second;
|
||||
MeshMap::iterator it2 = meshMap.find(mesh);
|
||||
if (it2 == meshMap.end())
|
||||
{
|
||||
std::unique_ptr<NzVertexBuffer> vertexBuffer(new NzVertexBuffer);
|
||||
vertexBuffer->SetPersistent(false);
|
||||
vertexBuffer->Reset(NzVertexDeclaration::Get(nzVertexLayout_XYZ_Normal_UV_Tangent), mesh->GetVertexCount(), nzBufferStorage_Hardware, nzBufferUsage_Dynamic);
|
||||
|
||||
BufferData data({vertexBuffer.get(), true});
|
||||
meshMap.insert(std::make_pair(mesh, data));
|
||||
|
||||
mesh->AddResourceListener(&listener, ResourceType_SkeletalMesh);
|
||||
|
||||
s_skinningQueue.push_back(SkinningData{mesh, skeleton, vertexBuffer.get()});
|
||||
|
||||
buffer = vertexBuffer.release();
|
||||
}
|
||||
else
|
||||
{
|
||||
BufferData& data = it2->second;
|
||||
if (!data.updated)
|
||||
{
|
||||
s_skinningQueue.push_back(SkinningData{mesh, skeleton, data.buffer});
|
||||
data.updated = true;
|
||||
}
|
||||
|
||||
buffer = data.buffer;
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void NzSkinningManager::Skin()
|
||||
{
|
||||
for (SkinningData& data : s_skinningQueue)
|
||||
s_skinFunc(data.mesh, data.skeleton, data.buffer);
|
||||
|
||||
s_skinningQueue.clear();
|
||||
}
|
||||
|
||||
bool NzSkinningManager::Initialize()
|
||||
{
|
||||
///TODO: GPU Skinning
|
||||
if (NzTaskScheduler::Initialize())
|
||||
s_skinFunc = Skin_MultiCPU;
|
||||
else
|
||||
s_skinFunc = Skin_MonoCPU;
|
||||
|
||||
return true; // Rien de particulier à faire
|
||||
}
|
||||
|
||||
void NzSkinningManager::Uninitialize()
|
||||
{
|
||||
for (auto& pair : s_cache)
|
||||
{
|
||||
pair.first->RemoveResourceListener(&listener);
|
||||
MeshMap& meshMap = pair.second;
|
||||
for (auto& pair2 : meshMap)
|
||||
pair2.first->RemoveResourceListener(&listener);
|
||||
}
|
||||
s_cache.clear();
|
||||
s_skinningQueue.clear();
|
||||
}
|
||||
|
||||
NzSkinningManager::SkinFunction NzSkinningManager::s_skinFunc = nullptr;
|
||||
|
|
@ -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++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@
|
|||
#include <Nazara/Utility/Debug.hpp>
|
||||
|
||||
NzJoint::NzJoint(NzSkeleton* skeleton) :
|
||||
m_skeleton(skeleton)
|
||||
m_skeleton(skeleton),
|
||||
m_skinningMatrixUpdated(false)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -15,11 +16,18 @@ NzJoint::NzJoint(const NzJoint& joint) :
|
|||
NzNode(joint),
|
||||
m_inverseBindMatrix(joint.m_inverseBindMatrix),
|
||||
m_name(joint.m_name),
|
||||
m_skeleton(joint.m_skeleton)
|
||||
m_skeleton(joint.m_skeleton),
|
||||
m_skinningMatrixUpdated(false)
|
||||
{
|
||||
}
|
||||
|
||||
NzMatrix4f NzJoint::GetInverseBindMatrix() const
|
||||
void NzJoint::EnsureSkinningMatrixUpdate() const
|
||||
{
|
||||
if (!m_skinningMatrixUpdated)
|
||||
UpdateSkinningMatrix();
|
||||
}
|
||||
|
||||
const NzMatrix4f& NzJoint::GetInverseBindMatrix() const
|
||||
{
|
||||
return m_inverseBindMatrix;
|
||||
}
|
||||
|
|
@ -39,9 +47,18 @@ const NzSkeleton* NzJoint::GetSkeleton() const
|
|||
return m_skeleton;
|
||||
}
|
||||
|
||||
const NzMatrix4f& NzJoint::GetSkinningMatrix() const
|
||||
{
|
||||
if (!m_skinningMatrixUpdated)
|
||||
UpdateSkinningMatrix();
|
||||
|
||||
return m_skinningMatrix;
|
||||
}
|
||||
|
||||
void NzJoint::SetInverseBindMatrix(const NzMatrix4f& matrix)
|
||||
{
|
||||
m_inverseBindMatrix = matrix;
|
||||
m_skinningMatrixUpdated = false;
|
||||
}
|
||||
|
||||
void NzJoint::SetName(const NzString& name)
|
||||
|
|
@ -50,3 +67,20 @@ void NzJoint::SetName(const NzString& name)
|
|||
|
||||
m_skeleton->InvalidateJointMap();
|
||||
}
|
||||
|
||||
void NzJoint::InvalidateNode()
|
||||
{
|
||||
NzNode::InvalidateNode();
|
||||
|
||||
m_skinningMatrixUpdated = false;
|
||||
}
|
||||
|
||||
void NzJoint::UpdateSkinningMatrix() const
|
||||
{
|
||||
if (!m_transformMatrixUpdated)
|
||||
UpdateTransformMatrix();
|
||||
|
||||
m_skinningMatrix.Set(m_inverseBindMatrix);
|
||||
m_skinningMatrix.ConcatenateAffine(m_transformMatrix);
|
||||
m_skinningMatrixUpdated = true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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