Merge remote-tracking branch 'origin/RenderTechnique-Update'
Former-commit-id: 85e5af81ed3381af19e87c6ca0ee14e79955e563
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user