Merge remote-tracking branch 'origin/RenderTechnique-Update'

Former-commit-id: db5a6503e43615a47e3be4aaaa033899639392b7
This commit is contained in:
Lynix 2014-05-29 19:59:09 +02:00
commit bc9c8c6bdb
11 changed files with 467 additions and 803 deletions

View File

@ -9,14 +9,15 @@
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/NonCopyable.hpp> #include <Nazara/Core/NonCopyable.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Matrix4.hpp> #include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Utility/Enums.hpp>
class NzDrawable; class NzDrawable;
class NzLight; class NzLight;
class NzMaterial; class NzMaterial;
class NzModel;
class NzSprite; class NzSprite;
class NzSubMesh; struct NzMeshData;
class NAZARA_API NzAbstractRenderQueue : NzNonCopyable class NAZARA_API NzAbstractRenderQueue : NzNonCopyable
{ {
@ -26,8 +27,8 @@ class NAZARA_API NzAbstractRenderQueue : NzNonCopyable
virtual void AddDrawable(const NzDrawable* drawable) = 0; virtual void AddDrawable(const NzDrawable* drawable) = 0;
virtual void AddLight(const NzLight* light) = 0; virtual void AddLight(const NzLight* light) = 0;
virtual void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) = 0;
virtual void AddSprite(const NzSprite* sprite) = 0; virtual void AddSprite(const NzSprite* sprite) = 0;
virtual void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) = 0;
virtual void Clear(bool fully) = 0; virtual void Clear(bool fully) = 0;
}; };

View File

@ -13,6 +13,7 @@
#include <Nazara/Graphics/AbstractRenderQueue.hpp> #include <Nazara/Graphics/AbstractRenderQueue.hpp>
#include <Nazara/Math/Box.hpp> #include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Matrix4.hpp> #include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Utility/MeshData.hpp>
#include <map> #include <map>
#include <tuple> #include <tuple>
@ -29,22 +30,11 @@ class NAZARA_API NzDeferredRenderQueue : public NzAbstractRenderQueue, NzResourc
void AddDrawable(const NzDrawable* drawable) override; void AddDrawable(const NzDrawable* drawable) override;
void AddLight(const NzLight* light) override; void AddLight(const NzLight* light) override;
void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) override;
void AddSprite(const NzSprite* sprite) override; void AddSprite(const NzSprite* sprite) override;
void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) override;
void Clear(bool fully); void Clear(bool fully);
struct SkeletalData
{
///TODO
NzMatrix4f transformMatrix;
};
struct StaticData
{
NzMatrix4f transformMatrix;
};
struct BatchedModelMaterialComparator struct BatchedModelMaterialComparator
{ {
bool operator()(const NzMaterial* mat1, const NzMaterial* mat2); bool operator()(const NzMaterial* mat1, const NzMaterial* mat2);
@ -55,23 +45,17 @@ class NAZARA_API NzDeferredRenderQueue : public NzAbstractRenderQueue, NzResourc
bool operator()(const NzMaterial* mat1, const NzMaterial* mat2); bool operator()(const NzMaterial* mat1, const NzMaterial* mat2);
}; };
struct BatchedSkeletalMeshComparator struct MeshDataComparator
{ {
bool operator()(const NzSkeletalMesh* subMesh1, const NzSkeletalMesh* subMesh2); bool operator()(const NzMeshData& data1, const NzMeshData& data2);
}; };
struct BatchedStaticMeshComparator typedef std::map<NzMeshData, std::vector<NzMatrix4f>, MeshDataComparator> MeshInstanceContainer;
{ typedef std::map<const NzMaterial*, std::tuple<bool, bool, MeshInstanceContainer>, BatchedModelMaterialComparator> ModelBatches;
bool operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2);
};
typedef std::map<const NzSkeletalMesh*, std::vector<SkeletalData>, BatchedSkeletalMeshComparator> BatchedSkeletalMeshContainer;
typedef std::map<const NzStaticMesh*, std::vector<StaticData>, BatchedStaticMeshComparator> BatchedStaticMeshContainer;
typedef std::map<const NzMaterial*, std::tuple<bool, bool, BatchedSkeletalMeshContainer, BatchedStaticMeshContainer>, BatchedModelMaterialComparator> BatchedModelContainer;
typedef std::map<const NzMaterial*, std::vector<const NzSprite*>> BatchedSpriteContainer; typedef std::map<const NzMaterial*, std::vector<const NzSprite*>> BatchedSpriteContainer;
typedef std::vector<const NzLight*> LightContainer; typedef std::vector<const NzLight*> LightContainer;
BatchedModelContainer opaqueModels; ModelBatches opaqueModels;
BatchedSpriteContainer sprites; BatchedSpriteContainer sprites;
LightContainer directionalLights; LightContainer directionalLights;
LightContainer pointLights; LightContainer pointLights;

View File

@ -13,6 +13,7 @@
#include <Nazara/Graphics/AbstractRenderQueue.hpp> #include <Nazara/Graphics/AbstractRenderQueue.hpp>
#include <Nazara/Math/Box.hpp> #include <Nazara/Math/Box.hpp>
#include <Nazara/Math/Matrix4.hpp> #include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Utility/MeshData.hpp>
#include <map> #include <map>
#include <tuple> #include <tuple>
@ -31,8 +32,8 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource
void AddDrawable(const NzDrawable* drawable) override; void AddDrawable(const NzDrawable* drawable) override;
void AddLight(const NzLight* light) override; void AddLight(const NzLight* light) override;
void AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix) override;
void AddSprite(const NzSprite* sprite) override; void AddSprite(const NzSprite* sprite) override;
void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix) override;
void Clear(bool fully); void Clear(bool fully);
@ -42,35 +43,14 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource
bool OnResourceDestroy(const NzResource* resource, int index) override; bool OnResourceDestroy(const NzResource* resource, int index) override;
void OnResourceReleased(const NzResource* resource, int index) override; void OnResourceReleased(const NzResource* resource, int index) override;
struct SkeletalData struct TransparentModelData
{
///TODO
NzMatrix4f transformMatrix;
};
struct StaticData
{
NzMatrix4f transformMatrix;
};
struct TransparentModel
{ {
NzMatrix4f transformMatrix; NzMatrix4f transformMatrix;
NzMeshData meshData;
NzSpheref boundingSphere; NzSpheref boundingSphere;
const NzMaterial* material; const NzMaterial* material;
}; };
struct TransparentSkeletalModel : public TransparentModel
{
///TODO
};
struct TransparentStaticModel : public TransparentModel
{
const NzStaticMesh* mesh;
};
struct BatchedModelMaterialComparator struct BatchedModelMaterialComparator
{ {
bool operator()(const NzMaterial* mat1, const NzMaterial* mat2); bool operator()(const NzMaterial* mat1, const NzMaterial* mat2);
@ -81,28 +61,21 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource
bool operator()(const NzMaterial* mat1, const NzMaterial* mat2); bool operator()(const NzMaterial* mat1, const NzMaterial* mat2);
}; };
struct BatchedSkeletalMeshComparator struct MeshDataComparator
{ {
bool operator()(const NzSkeletalMesh* subMesh1, const NzSkeletalMesh* subMesh2); bool operator()(const NzMeshData& data1, const NzMeshData& data2);
}; };
struct BatchedStaticMeshComparator typedef std::map<NzMeshData, std::pair<NzSpheref, std::vector<NzMatrix4f>>, MeshDataComparator> MeshInstanceContainer;
{ typedef std::map<const NzMaterial*, std::tuple<bool, bool, MeshInstanceContainer>, BatchedModelMaterialComparator> ModelBatches;
bool operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2);
};
typedef std::map<const NzSkeletalMesh*, std::vector<SkeletalData>, BatchedSkeletalMeshComparator> BatchedSkeletalMeshContainer;
typedef std::map<const NzStaticMesh*, std::pair<NzSpheref, std::vector<StaticData>>, BatchedStaticMeshComparator> BatchedStaticMeshContainer;
typedef std::map<const NzMaterial*, std::tuple<bool, bool, BatchedSkeletalMeshContainer, BatchedStaticMeshContainer>, BatchedModelMaterialComparator> BatchedModelContainer;
typedef std::map<const NzMaterial*, std::vector<const NzSprite*>> BatchedSpriteContainer; typedef std::map<const NzMaterial*, std::vector<const NzSprite*>> BatchedSpriteContainer;
typedef std::vector<const NzLight*> LightContainer; typedef std::vector<const NzLight*> LightContainer;
typedef std::vector<std::pair<unsigned int, bool>> TransparentModelContainer; typedef std::vector<unsigned int> TransparentModelContainer;
BatchedModelContainer opaqueModels; ModelBatches opaqueModels;
BatchedSpriteContainer sprites; BatchedSpriteContainer sprites;
TransparentModelContainer transparentsModels; TransparentModelContainer transparentModels;
std::vector<TransparentSkeletalModel> transparentSkeletalModels; std::vector<TransparentModelData> transparentModelData;
std::vector<TransparentStaticModel> transparentStaticModels;
std::vector<const NzDrawable*> otherDrawables; std::vector<const NzDrawable*> otherDrawables;
LightContainer directionalLights; LightContainer directionalLights;
LightContainer lights; LightContainer lights;

View File

@ -9,19 +9,15 @@
#include <Nazara/Prerequesites.hpp> #include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/ResourceLoader.hpp> #include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/Updatable.hpp>
#include <Nazara/Graphics/Material.hpp> #include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/SceneNode.hpp> #include <Nazara/Graphics/SceneNode.hpp>
#include <Nazara/Utility/Animation.hpp>
#include <Nazara/Utility/Mesh.hpp> #include <Nazara/Utility/Mesh.hpp>
struct NAZARA_API NzModelParameters struct NAZARA_API NzModelParameters
{ {
NzModelParameters(); NzModelParameters();
bool loadAnimation = true;
bool loadMaterials = true; bool loadMaterials = true;
NzAnimationParams animation;
NzMaterialParams material; NzMaterialParams material;
NzMeshParams mesh; NzMeshParams mesh;
@ -32,7 +28,7 @@ class NzModel;
using NzModelLoader = NzResourceLoader<NzModel, NzModelParameters>; using NzModelLoader = NzResourceLoader<NzModel, NzModelParameters>;
class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable class NAZARA_API NzModel : public NzSceneNode
{ {
friend NzModelLoader; friend NzModelLoader;
friend class NzScene; friend class NzScene;
@ -41,7 +37,7 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable
NzModel(); NzModel();
NzModel(const NzModel& model); NzModel(const NzModel& model);
NzModel(NzModel&& model); NzModel(NzModel&& model);
~NzModel(); virtual ~NzModel();
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const override; void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const override;
void AdvanceAnimation(float elapsedTime); void AdvanceAnimation(float elapsedTime);
@ -65,6 +61,7 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable
bool HasAnimation() const; bool HasAnimation() const;
bool IsAnimationEnabled() const; bool IsAnimationEnabled() const;
virtual bool IsAnimated() const;
bool IsDrawable() const; bool IsDrawable() const;
void InvalidateBoundingVolume(); void InvalidateBoundingVolume();
@ -75,12 +72,11 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable
void Reset(); void Reset();
bool SetAnimation(NzAnimation* animation);
bool SetMaterial(const NzString& subMeshName, NzMaterial* material); bool SetMaterial(const NzString& subMeshName, NzMaterial* material);
void SetMaterial(unsigned int matIndex, NzMaterial* material); void SetMaterial(unsigned int matIndex, NzMaterial* material);
bool SetMaterial(unsigned int skinIndex, const NzString& subMeshName, NzMaterial* material); bool SetMaterial(unsigned int skinIndex, const NzString& subMeshName, NzMaterial* material);
void SetMaterial(unsigned int skinIndex, unsigned int matIndex, NzMaterial* material); void SetMaterial(unsigned int skinIndex, unsigned int matIndex, NzMaterial* material);
void SetMesh(NzMesh* mesh); virtual void SetMesh(NzMesh* mesh);
bool SetSequence(const NzString& sequenceName); bool SetSequence(const NzString& sequenceName);
void SetSequence(unsigned int sequenceIndex); void SetSequence(unsigned int sequenceIndex);
void SetSkin(unsigned int skin); void SetSkin(unsigned int skin);
@ -89,26 +85,16 @@ class NAZARA_API NzModel : public NzSceneNode, public NzUpdatable
NzModel& operator=(const NzModel& node); NzModel& operator=(const NzModel& node);
NzModel& operator=(NzModel&& node); NzModel& operator=(NzModel&& node);
private: protected:
bool FrustumCull(const NzFrustumf& frustum) override; bool FrustumCull(const NzFrustumf& frustum) override;
void InvalidateNode() override; void InvalidateNode() override;
void Register() override; virtual void UpdateBoundingVolume() const;
void Unregister() override;
void Update() override;
void UpdateBoundingVolume() const;
std::vector<NzMaterialRef> m_materials; std::vector<NzMaterialRef> m_materials;
NzAnimationRef m_animation;
mutable NzBoundingVolumef m_boundingVolume; mutable NzBoundingVolumef m_boundingVolume;
NzMeshRef m_mesh; NzMeshRef m_mesh;
NzSkeleton m_skeleton; // Uniquement pour les animations squelettiques
const NzSequence* m_currentSequence;
bool m_animationEnabled;
mutable bool m_boundingVolumeUpdated; mutable bool m_boundingVolumeUpdated;
float m_interpolation;
unsigned int m_currentFrame;
unsigned int m_matCount; unsigned int m_matCount;
unsigned int m_nextFrame;
unsigned int m_skin; unsigned int m_skin;
unsigned int m_skinCount; unsigned int m_skinCount;

View File

@ -0,0 +1,22 @@
// Copyright (C) 2014 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_MESHDATA_HPP
#define NAZARA_MESHDATA_HPP
#include <Nazara/Utility/Enums.hpp>
class NzIndexBuffer;
class NzVertexBuffer;
struct NzMeshData
{
nzPrimitiveMode primitiveMode;
const NzIndexBuffer* indexBuffer;
const NzVertexBuffer* vertexBuffer;
};
#endif // NAZARA_MESHDATA_HPP

View File

@ -58,10 +58,9 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor
if (used) if (used)
{ {
bool& renderQueueInstancing = std::get<1>(matIt.second); bool& renderQueueInstancing = std::get<1>(matIt.second);
NzDeferredRenderQueue::BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second); NzDeferredRenderQueue::MeshInstanceContainer& meshInstances = std::get<2>(matIt.second);
NzDeferredRenderQueue::BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second);
if (!skeletalContainer.empty() || !staticContainer.empty()) if (!meshInstances.empty())
{ {
const NzMaterial* material = matIt.first; const NzMaterial* material = matIt.first;
@ -88,26 +87,16 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor
lastShader = shader; lastShader = shader;
} }
// Meshs squelettiques // Meshes
/*if (!skeletalContainer.empty()) for (auto& meshIt : meshInstances)
{ {
NzRenderer::SetVertexBuffer(m_skinningBuffer); // Vertex buffer commun const NzMeshData& meshData = meshIt.first;
for (auto& subMeshIt : container) std::vector<NzMatrix4f>& instances = meshIt.second;
{
///TODO
}
}*/
// Meshs statiques if (!instances.empty())
for (auto& subMeshIt : staticContainer)
{
const NzStaticMesh* mesh = subMeshIt.first;
std::vector<NzDeferredRenderQueue::StaticData>& staticData = subMeshIt.second;
if (!staticData.empty())
{ {
const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer(); const NzIndexBuffer* indexBuffer = meshData.indexBuffer;
const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer(); const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer;
// Gestion du draw call avant la boucle de rendu // Gestion du draw call avant la boucle de rendu
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc; std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
@ -130,49 +119,43 @@ bool NzDeferredGeometryPass::Process(const NzScene* scene, unsigned int firstWor
NzRenderer::SetIndexBuffer(indexBuffer); NzRenderer::SetIndexBuffer(indexBuffer);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(vertexBuffer);
nzPrimitiveMode primitiveMode = mesh->GetPrimitiveMode();
if (useInstancing) if (useInstancing)
{ {
// On récupère le buffer d'instancing du Renderer et on le configure pour fonctionner avec des matrices
NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer(); NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer();
instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4)); instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4));
unsigned int stride = instanceBuffer->GetStride(); const NzMatrix4f* instanceMatrices = &instances[0];
unsigned int instanceCount = instances.size();
const NzDeferredRenderQueue::StaticData* data = &staticData[0]; unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre de matrices que peut contenir le buffer
unsigned int instanceCount = staticData.size();
unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // Le nombre de sommets maximum avec la déclaration donnée plus hautg
while (instanceCount > 0) while (instanceCount > 0)
{ {
// On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing)
unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount); unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount);
instanceCount -= renderedInstanceCount; instanceCount -= renderedInstanceCount;
NzBufferMapper<NzVertexBuffer> mapper(instanceBuffer, nzBufferAccess_DiscardAndWrite, 0, renderedInstanceCount); // On remplit l'instancing buffer avec nos matrices world
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(mapper.GetPointer()); instanceBuffer->Fill(instanceMatrices, 0, renderedInstanceCount, true);
instanceMatrices += renderedInstanceCount;
for (unsigned int i = 0; i < renderedInstanceCount; ++i) // Et on affiche
{ InstancedDrawFunc(renderedInstanceCount, meshData.primitiveMode, 0, indexCount);
std::memcpy(ptr, data->transformMatrix, sizeof(float)*16);
data++;
ptr += stride;
}
mapper.Unmap();
InstancedDrawFunc(renderedInstanceCount, primitiveMode, 0, indexCount);
} }
} }
else else
{ {
for (const NzDeferredRenderQueue::StaticData& data : staticData) // Sans instancing, on doit effectuer un drawcall pour chaque instance
// Cela reste néanmoins plus rapide que l'instancing en dessous d'un certain nombre d'instances
// À cause du temps de modification du buffer d'instancing
for (const NzMatrix4f& matrix : instances)
{ {
NzRenderer::SetMatrix(nzMatrixType_World, data.transformMatrix); NzRenderer::SetMatrix(nzMatrixType_World, matrix);
DrawFunc(primitiveMode, 0, indexCount); DrawFunc(meshData.primitiveMode, 0, indexCount);
} }
} }
staticData.clear();
instances.clear();
} }
} }
} }

View File

@ -9,17 +9,15 @@
#include <Nazara/Graphics/Material.hpp> #include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/Model.hpp> #include <Nazara/Graphics/Model.hpp>
#include <Nazara/Graphics/Sprite.hpp> #include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Utility/SkeletalMesh.hpp>
#include <Nazara/Utility/StaticMesh.hpp>
#include <Nazara/Graphics/Debug.hpp> #include <Nazara/Graphics/Debug.hpp>
namespace namespace
{ {
enum ResourceType enum ResourceType
{ {
ResourceType_IndexBuffer,
ResourceType_Material, ResourceType_Material,
ResourceType_SkeletalMesh, ResourceType_VertexBuffer
ResourceType_StaticMesh
}; };
} }
@ -66,6 +64,45 @@ void NzDeferredRenderQueue::AddLight(const NzLight* light)
m_forwardQueue->AddLight(light); m_forwardQueue->AddLight(light);
} }
void NzDeferredRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix)
{
if (material->IsEnabled(nzRendererParameter_Blend))
m_forwardQueue->AddMesh(material, meshData, meshAABB, transformMatrix);
else
{
ModelBatches::iterator it = opaqueModels.find(material);
if (it == opaqueModels.end())
{
it = opaqueModels.insert(std::make_pair(material, ModelBatches::mapped_type())).first;
material->AddResourceListener(this, ResourceType_Material);
}
bool& used = std::get<0>(it->second);
bool& enableInstancing = std::get<1>(it->second);
MeshInstanceContainer& meshMap = std::get<2>(it->second);
used = true;
MeshInstanceContainer::iterator it2 = meshMap.find(meshData);
if (it2 == meshMap.end())
{
it2 = meshMap.insert(std::make_pair(meshData, MeshInstanceContainer::mapped_type())).first;
if (meshData.indexBuffer)
meshData.indexBuffer->AddResourceListener(this, ResourceType_IndexBuffer);
meshData.vertexBuffer->AddResourceListener(this, ResourceType_VertexBuffer);
}
std::vector<NzMatrix4f>& instances = it2->second;
instances.push_back(transformMatrix);
// Avons-nous suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ?
if (instances.size() >= NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT)
enableInstancing = true; // Apparemment oui, activons l'instancing avec ce matériau
}
}
void NzDeferredRenderQueue::AddSprite(const NzSprite* sprite) void NzDeferredRenderQueue::AddSprite(const NzSprite* sprite)
{ {
#if NAZARA_GRAPHICS_SAFE #if NAZARA_GRAPHICS_SAFE
@ -91,75 +128,6 @@ void NzDeferredRenderQueue::AddSprite(const NzSprite* sprite)
m_forwardQueue->AddSprite(sprite); m_forwardQueue->AddSprite(sprite);
} }
void NzDeferredRenderQueue::AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix)
{
switch (subMesh->GetAnimationType())
{
case nzAnimationType_Skeletal:
{
///TODO
/*
** Il y a ici deux choses importantes à gérer:
** -Pour commencer, la mise en cache de std::vector suffisamment grands pour contenir le résultat du skinning
** l'objectif ici est d'éviter une allocation à chaque frame, donc de réutiliser un tableau existant
** Note: Il faudrait évaluer aussi la possibilité de conserver le buffer d'une frame à l'autre.
** Ceci permettant de ne pas skinner inutilement ce qui ne bouge pas, ou de skinner partiellement un mesh.
** Il faut cependant voir stocker ce set de buffers, qui doit être communs à toutes les RQ d'une même scène.
**
** -Ensuite, la possibilité de regrouper les modèles skinnés identiques, une centaine de soldats marchant au pas
** ne devrait requérir qu'un skinning.
*/
NazaraError("Skeletal mesh not supported yet, sorry");
break;
}
case nzAnimationType_Static:
{
if (material->IsEnabled(nzRendererParameter_Blend))
m_forwardQueue->AddSubMesh(material, subMesh, transformMatrix);
else
{
const NzStaticMesh* staticMesh = static_cast<const NzStaticMesh*>(subMesh);
auto it = opaqueModels.find(material);
if (it == opaqueModels.end())
{
it = opaqueModels.insert(std::make_pair(material, BatchedModelContainer::mapped_type())).first;
material->AddResourceListener(this, ResourceType_Material);
}
bool& used = std::get<0>(it->second);
bool& enableInstancing = std::get<1>(it->second);
used = true;
auto& meshMap = std::get<3>(it->second);
auto it2 = meshMap.find(staticMesh);
if (it2 == meshMap.end())
{
it2 = meshMap.insert(std::make_pair(staticMesh, BatchedStaticMeshContainer::mapped_type())).first;
staticMesh->AddResourceListener(this, ResourceType_StaticMesh);
}
std::vector<StaticData>& staticDataContainer = it2->second;
unsigned int instanceCount = staticDataContainer.size() + 1;
// Avons-nous suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ?
if (instanceCount >= NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT)
enableInstancing = true; // Apparemment oui, activons l'instancing avec ce matériau
staticDataContainer.resize(instanceCount);
StaticData& data = staticDataContainer.back();
data.transformMatrix = transformMatrix;
}
break;
}
}
}
void NzDeferredRenderQueue::Clear(bool fully) void NzDeferredRenderQueue::Clear(bool fully)
{ {
directionalLights.clear(); directionalLights.clear();
@ -173,18 +141,15 @@ void NzDeferredRenderQueue::Clear(bool fully)
const NzMaterial* material = matIt.first; const NzMaterial* material = matIt.first;
material->RemoveResourceListener(this); material->RemoveResourceListener(this);
BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second); MeshInstanceContainer& instances = std::get<2>(matIt.second);
for (auto& meshIt : skeletalContainer) for (auto& instanceIt : instances)
{ {
const NzSkeletalMesh* skeletalMesh = meshIt.first; const NzMeshData& renderData = instanceIt.first;
skeletalMesh->RemoveResourceListener(this);
}
BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second); if (renderData.indexBuffer)
for (auto& meshIt : staticContainer) renderData.indexBuffer->RemoveResourceListener(this);
{
const NzStaticMesh* staticMesh = meshIt.first; renderData.vertexBuffer->RemoveResourceListener(this);
staticMesh->RemoveResourceListener(this);
} }
} }
@ -199,23 +164,41 @@ bool NzDeferredRenderQueue::OnResourceDestroy(const NzResource* resource, int in
{ {
switch (index) switch (index)
{ {
case ResourceType_IndexBuffer:
{
for (auto& modelPair : opaqueModels)
{
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
for (auto it = meshes.begin(); it != meshes.end();)
{
const NzMeshData& renderData = it->first;
if (renderData.indexBuffer == resource)
it = meshes.erase(it);
else
++it;
}
}
break;
}
case ResourceType_Material: case ResourceType_Material:
opaqueModels.erase(static_cast<const NzMaterial*>(resource)); opaqueModels.erase(static_cast<const NzMaterial*>(resource));
break; break;
case ResourceType_SkeletalMesh: case ResourceType_VertexBuffer:
{ {
for (auto& pair : opaqueModels) for (auto& modelPair : opaqueModels)
std::get<2>(pair.second).erase(static_cast<const NzSkeletalMesh*>(resource)); {
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
break; for (auto it = meshes.begin(); it != meshes.end();)
} {
const NzMeshData& renderData = it->first;
case ResourceType_StaticMesh: if (renderData.vertexBuffer == resource)
{ it = meshes.erase(it);
for (auto& pair : opaqueModels) else
std::get<3>(pair.second).erase(static_cast<const NzStaticMesh*>(resource)); ++it;
}
}
break; break;
} }
} }
@ -225,7 +208,58 @@ bool NzDeferredRenderQueue::OnResourceDestroy(const NzResource* resource, int in
void NzDeferredRenderQueue::OnResourceReleased(const NzResource* resource, int index) void NzDeferredRenderQueue::OnResourceReleased(const NzResource* resource, int index)
{ {
OnResourceDestroy(resource, index); // La ressource vient d'être libérée, nous ne pouvons donc plus utiliser la méthode traditionnelle de recherche
// des pointeurs stockés (À cause de la fonction de triage utilisant des spécificités des ressources)
switch (index)
{
case ResourceType_IndexBuffer:
{
for (auto& modelPair : opaqueModels)
{
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
for (auto it = meshes.begin(); it != meshes.end();)
{
const NzMeshData& renderData = it->first;
if (renderData.indexBuffer == resource)
it = meshes.erase(it);
else
++it;
}
}
break;
}
case ResourceType_Material:
{
for (auto it = opaqueModels.begin(); it != opaqueModels.end(); ++it)
{
if (it->first == resource)
{
opaqueModels.erase(it);
break;
}
}
break;
}
case ResourceType_VertexBuffer:
{
for (auto& modelPair : opaqueModels)
{
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
for (auto it = meshes.begin(); it != meshes.end();)
{
const NzMeshData& renderData = it->first;
if (renderData.vertexBuffer == resource)
it = meshes.erase(it);
else
++it;
}
}
break;
}
}
} }
bool NzDeferredRenderQueue::BatchedModelMaterialComparator::operator()(const NzMaterial* mat1, const NzMaterial* mat2) bool NzDeferredRenderQueue::BatchedModelMaterialComparator::operator()(const NzMaterial* mat1, const NzMaterial* mat2)
@ -237,7 +271,6 @@ bool NzDeferredRenderQueue::BatchedModelMaterialComparator::operator()(const NzM
const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
if (shader1 != shader2) if (shader1 != shader2)
return shader1 < shader2; return shader1 < shader2;
@ -258,7 +291,6 @@ bool NzDeferredRenderQueue::BatchedSpriteMaterialComparator::operator()(const Nz
const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader();
if (shader1 != shader2) if (shader1 != shader2)
return shader1 < shader2; return shader1 < shader2;
@ -270,38 +302,20 @@ bool NzDeferredRenderQueue::BatchedSpriteMaterialComparator::operator()(const Nz
return mat1 < mat2; return mat1 < mat2;
} }
bool NzDeferredRenderQueue::BatchedSkeletalMeshComparator::operator()(const NzSkeletalMesh* subMesh1, const NzSkeletalMesh* subMesh2) bool NzDeferredRenderQueue::MeshDataComparator::operator()(const NzMeshData& data1, const NzMeshData& data2)
{ {
const NzIndexBuffer* iBuffer1 = subMesh1->GetIndexBuffer(); const NzBuffer* buffer1;
const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr; const NzBuffer* buffer2;
const NzIndexBuffer* iBuffer2 = subMesh1->GetIndexBuffer(); buffer1 = (data1.indexBuffer) ? data1.indexBuffer->GetBuffer() : nullptr;
const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr; buffer2 = (data2.indexBuffer) ? data2.indexBuffer->GetBuffer() : nullptr;
if (buffer1 != buffer2)
if (buffer1 == buffer2)
return subMesh1 < subMesh2;
else
return buffer2 < buffer2;
}
bool NzDeferredRenderQueue::BatchedStaticMeshComparator::operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2)
{
const NzIndexBuffer* iBuffer1 = subMesh1->GetIndexBuffer();
const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr;
const NzIndexBuffer* iBuffer2 = subMesh2->GetIndexBuffer();
const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr;
if (buffer1 == buffer2)
{
buffer1 = subMesh1->GetVertexBuffer()->GetBuffer();
buffer2 = subMesh2->GetVertexBuffer()->GetBuffer();
if (buffer1 == buffer2)
return subMesh1 < subMesh2;
else
return buffer1 < buffer2;
}
else
return buffer1 < buffer2; return buffer1 < buffer2;
buffer1 = data1.vertexBuffer->GetBuffer();
buffer2 = data2.vertexBuffer->GetBuffer();
if (buffer1 != buffer2)
return buffer1 < buffer2;
return data1.primitiveMode < data2.primitiveMode;
} }

View File

@ -16,9 +16,9 @@ namespace
{ {
enum ResourceType enum ResourceType
{ {
ResourceType_IndexBuffer,
ResourceType_Material, ResourceType_Material,
ResourceType_SkeletalMesh, ResourceType_VertexBuffer
ResourceType_StaticMesh
}; };
} }
@ -68,6 +68,59 @@ void NzForwardRenderQueue::AddLight(const NzLight* light)
} }
} }
void NzForwardRenderQueue::AddMesh(const NzMaterial* material, const NzMeshData& meshData, const NzBoxf& meshAABB, const NzMatrix4f& transformMatrix)
{
if (material->IsEnabled(nzRendererParameter_Blend))
{
unsigned int index = transparentModelData.size();
transparentModelData.resize(index+1);
TransparentModelData& data = transparentModelData.back();
data.boundingSphere = NzSpheref(transformMatrix.GetTranslation() + meshAABB.GetCenter(), meshAABB.GetSquaredRadius());
data.material = material;
data.meshData = meshData;
data.transformMatrix = transformMatrix;
transparentModels.push_back(index);
}
else
{
ModelBatches::iterator it = opaqueModels.find(material);
if (it == opaqueModels.end())
{
it = opaqueModels.insert(std::make_pair(material, ModelBatches::mapped_type())).first;
material->AddResourceListener(this, ResourceType_Material);
}
bool& used = std::get<0>(it->second);
bool& enableInstancing = std::get<1>(it->second);
MeshInstanceContainer& meshMap = std::get<2>(it->second);
used = true;
MeshInstanceContainer::iterator it2 = meshMap.find(meshData);
if (it2 == meshMap.end())
{
it2 = meshMap.insert(std::make_pair(meshData, MeshInstanceContainer::mapped_type())).first;
NzSpheref& squaredBoundingSphere = it2->second.first;
squaredBoundingSphere.Set(meshAABB.GetSquaredBoundingSphere());
if (meshData.indexBuffer)
meshData.indexBuffer->AddResourceListener(this, ResourceType_IndexBuffer);
meshData.vertexBuffer->AddResourceListener(this, ResourceType_VertexBuffer);
}
std::vector<NzMatrix4f>& instances = it2->second.second;
instances.push_back(transformMatrix);
// Avons-nous suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ?
if (instances.size() >= NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT)
enableInstancing = true; // Apparemment oui, activons l'instancing avec ce matériau
}
}
void NzForwardRenderQueue::AddSprite(const NzSprite* sprite) void NzForwardRenderQueue::AddSprite(const NzSprite* sprite)
{ {
#if NAZARA_GRAPHICS_SAFE #if NAZARA_GRAPHICS_SAFE
@ -87,100 +140,13 @@ void NzForwardRenderQueue::AddSprite(const NzSprite* sprite)
sprites[sprite->GetMaterial()].push_back(sprite); sprites[sprite->GetMaterial()].push_back(sprite);
} }
void NzForwardRenderQueue::AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix)
{
switch (subMesh->GetAnimationType())
{
case nzAnimationType_Skeletal:
{
///TODO
/*
** Il y a ici deux choses importantes à gérer:
** -Pour commencer, la mise en cache de std::vector suffisamment grands pour contenir le résultat du skinning
** l'objectif ici est d'éviter une allocation à chaque frame, donc de réutiliser un tableau existant
** Note: Il faudrait évaluer aussi la possibilité de conserver le buffer d'une frame à l'autre.
** Ceci permettant de ne pas skinner inutilement ce qui ne bouge pas, ou de skinner partiellement un mesh.
** Il faut cependant voir stocker ce set de buffers, qui doit être communs à toutes les RQ d'une même scène.
**
** -Ensuite, la possibilité de regrouper les modèles skinnés identiques, une centaine de soldats marchant au pas
** ne devrait requérir qu'un skinning.
*/
NazaraError("Skeletal mesh not supported yet, sorry");
break;
}
case nzAnimationType_Static:
{
const NzStaticMesh* staticMesh = static_cast<const NzStaticMesh*>(subMesh);
if (material->IsEnabled(nzRendererParameter_Blend))
{
unsigned int index = transparentStaticModels.size();
transparentStaticModels.resize(index+1);
const NzBoxf& aabb = staticMesh->GetAABB();
TransparentStaticModel& data = transparentStaticModels.back();
data.boundingSphere = NzSpheref(transformMatrix.GetTranslation() + aabb.GetCenter(), aabb.GetSquaredRadius());
data.material = material;
data.mesh = staticMesh;
data.transformMatrix = transformMatrix;
transparentsModels.push_back(std::make_pair(index, true));
}
else
{
auto it = opaqueModels.find(material);
if (it == opaqueModels.end())
{
it = opaqueModels.insert(std::make_pair(material, BatchedModelContainer::mapped_type())).first;
material->AddResourceListener(this, ResourceType_Material);
}
bool& used = std::get<0>(it->second);
bool& enableInstancing = std::get<1>(it->second);
used = true;
auto& meshMap = std::get<3>(it->second);
auto it2 = meshMap.find(staticMesh);
if (it2 == meshMap.end())
{
it2 = meshMap.insert(std::make_pair(staticMesh, BatchedStaticMeshContainer::mapped_type())).first;
staticMesh->AddResourceListener(this, ResourceType_StaticMesh);
NzSpheref& squaredBoundingSphere = it2->second.first;
squaredBoundingSphere.Set(staticMesh->GetAABB().GetSquaredBoundingSphere());
///TODO: Écouter le StaticMesh pour repérer tout changement de géométrie
}
std::vector<StaticData>& staticDataContainer = it2->second.second;
unsigned int instanceCount = staticDataContainer.size() + 1;
// Avons-nous suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ?
if (instanceCount >= NAZARA_GRAPHICS_INSTANCING_MIN_INSTANCES_COUNT)
enableInstancing = true; // Apparemment oui, activons l'instancing avec ce matériau
staticDataContainer.resize(instanceCount);
StaticData& data = staticDataContainer.back();
data.transformMatrix = transformMatrix;
}
break;
}
}
}
void NzForwardRenderQueue::Clear(bool fully) void NzForwardRenderQueue::Clear(bool fully)
{ {
directionalLights.clear(); directionalLights.clear();
lights.clear(); lights.clear();
otherDrawables.clear(); otherDrawables.clear();
transparentsModels.clear(); transparentModels.clear();
transparentSkeletalModels.clear(); transparentModelData.clear();
transparentStaticModels.clear();
if (fully) if (fully)
{ {
@ -189,18 +155,15 @@ void NzForwardRenderQueue::Clear(bool fully)
const NzMaterial* material = matIt.first; const NzMaterial* material = matIt.first;
material->RemoveResourceListener(this); material->RemoveResourceListener(this);
BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second); MeshInstanceContainer& instances = std::get<2>(matIt.second);
for (auto& meshIt : skeletalContainer) for (auto& instanceIt : instances)
{ {
const NzSkeletalMesh* skeletalMesh = meshIt.first; const NzMeshData& renderData = instanceIt.first;
skeletalMesh->RemoveResourceListener(this);
}
BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second); if (renderData.indexBuffer)
for (auto& meshIt : staticContainer) renderData.indexBuffer->RemoveResourceListener(this);
{
const NzStaticMesh* staticMesh = meshIt.first; renderData.vertexBuffer->RemoveResourceListener(this);
staticMesh->RemoveResourceListener(this);
} }
} }
opaqueModels.clear(); opaqueModels.clear();
@ -212,15 +175,10 @@ void NzForwardRenderQueue::Sort(const NzAbstractViewer* viewer)
{ {
struct TransparentModelComparator struct TransparentModelComparator
{ {
bool operator()(const std::pair<unsigned int, bool>& index1, const std::pair<unsigned int, bool>& index2) bool operator()(unsigned int index1, unsigned int index2)
{ {
const NzSpheref& sphere1 = (index1.second) ? const NzSpheref& sphere1 = queue->transparentModelData[index1].boundingSphere;
queue->transparentStaticModels[index1.first].boundingSphere : const NzSpheref& sphere2 = queue->transparentModelData[index2].boundingSphere;
queue->transparentSkeletalModels[index1.first].boundingSphere;
const NzSpheref& sphere2 = (index2.second) ?
queue->transparentStaticModels[index2.first].boundingSphere :
queue->transparentSkeletalModels[index2.first].boundingSphere;
NzVector3f position1 = sphere1.GetNegativeVertex(viewerNormal); NzVector3f position1 = sphere1.GetNegativeVertex(viewerNormal);
NzVector3f position2 = sphere2.GetNegativeVertex(viewerNormal); NzVector3f position2 = sphere2.GetNegativeVertex(viewerNormal);
@ -234,30 +192,48 @@ void NzForwardRenderQueue::Sort(const NzAbstractViewer* viewer)
}; };
TransparentModelComparator comparator {this, viewer->GetFrustum().GetPlane(nzFrustumPlane_Near), viewer->GetForward()}; TransparentModelComparator comparator {this, viewer->GetFrustum().GetPlane(nzFrustumPlane_Near), viewer->GetForward()};
std::sort(transparentsModels.begin(), transparentsModels.end(), comparator); std::sort(transparentModels.begin(), transparentModels.end(), comparator);
} }
bool NzForwardRenderQueue::OnResourceDestroy(const NzResource* resource, int index) bool NzForwardRenderQueue::OnResourceDestroy(const NzResource* resource, int index)
{ {
switch (index) switch (index)
{ {
case ResourceType_IndexBuffer:
{
for (auto& modelPair : opaqueModels)
{
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
for (auto it = meshes.begin(); it != meshes.end();)
{
const NzMeshData& renderData = it->first;
if (renderData.indexBuffer == resource)
it = meshes.erase(it);
else
++it;
}
}
break;
}
case ResourceType_Material: case ResourceType_Material:
opaqueModels.erase(static_cast<const NzMaterial*>(resource)); opaqueModels.erase(static_cast<const NzMaterial*>(resource));
break; break;
case ResourceType_SkeletalMesh: case ResourceType_VertexBuffer:
{ {
for (auto& pair : opaqueModels) for (auto& modelPair : opaqueModels)
std::get<2>(pair.second).erase(static_cast<const NzSkeletalMesh*>(resource)); {
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
break; for (auto it = meshes.begin(); it != meshes.end();)
} {
const NzMeshData& renderData = it->first;
case ResourceType_StaticMesh: if (renderData.vertexBuffer == resource)
{ it = meshes.erase(it);
for (auto& pair : opaqueModels) else
std::get<3>(pair.second).erase(static_cast<const NzStaticMesh*>(resource)); ++it;
}
}
break; break;
} }
} }
@ -272,7 +248,25 @@ void NzForwardRenderQueue::OnResourceReleased(const NzResource* resource, int in
switch (index) switch (index)
{ {
case ResourceType_IndexBuffer:
{
for (auto& modelPair : opaqueModels)
{
MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
for (auto it = meshes.begin(); it != meshes.end();)
{
const NzMeshData& renderData = it->first;
if (renderData.indexBuffer == resource)
it = meshes.erase(it);
else
++it;
}
}
break;
}
case ResourceType_Material: case ResourceType_Material:
{
for (auto it = opaqueModels.begin(); it != opaqueModels.end(); ++it) for (auto it = opaqueModels.begin(); it != opaqueModels.end(); ++it)
{ {
if (it->first == resource) if (it->first == resource)
@ -282,38 +276,20 @@ void NzForwardRenderQueue::OnResourceReleased(const NzResource* resource, int in
} }
} }
break; break;
case ResourceType_SkeletalMesh:
{
for (auto& pair : opaqueModels)
{
BatchedSkeletalMeshContainer& container = std::get<2>(pair.second);
for (auto it = container.begin(); it != container.end(); ++it)
{
if (it->first == resource)
{
container.erase(it);
break;
}
}
}
break;
} }
case ResourceType_StaticMesh: case ResourceType_VertexBuffer:
{ {
for (auto& pair : opaqueModels) for (auto& modelPair : opaqueModels)
{ {
BatchedStaticMeshContainer& container = std::get<3 >(pair.second); MeshInstanceContainer& meshes = std::get<2>(modelPair.second);
for (auto it = meshes.begin(); it != meshes.end();)
for (auto it = container.begin(); it != container.end(); ++it)
{ {
if (it->first == resource) const NzMeshData& renderData = it->first;
{ if (renderData.vertexBuffer == resource)
container.erase(it); it = meshes.erase(it);
break; else
} ++it;
} }
} }
break; break;
@ -349,8 +325,8 @@ bool NzForwardRenderQueue::BatchedSpriteMaterialComparator::operator()(const NzM
if (uberShader1 != uberShader2) if (uberShader1 != uberShader2)
return uberShader1 < uberShader2; return uberShader1 < uberShader2;
const NzShader* shader1 = mat1->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); const NzShader* shader1 = mat1->GetShaderInstance()->GetShader();
const NzShader* shader2 = mat2->GetShaderInstance(nzShaderFlags_Deferred)->GetShader(); const NzShader* shader2 = mat2->GetShaderInstance()->GetShader();
if (shader1 != shader2) if (shader1 != shader2)
return shader1 < shader2; return shader1 < shader2;
@ -363,38 +339,20 @@ bool NzForwardRenderQueue::BatchedSpriteMaterialComparator::operator()(const NzM
return mat1 < mat2; return mat1 < mat2;
} }
bool NzForwardRenderQueue::BatchedSkeletalMeshComparator::operator()(const NzSkeletalMesh* subMesh1, const NzSkeletalMesh* subMesh2) bool NzForwardRenderQueue::MeshDataComparator::operator()(const NzMeshData& data1, const NzMeshData& data2)
{ {
const NzIndexBuffer* iBuffer1 = subMesh1->GetIndexBuffer(); const NzBuffer* buffer1;
const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr; const NzBuffer* buffer2;
const NzIndexBuffer* iBuffer2 = subMesh1->GetIndexBuffer(); buffer1 = (data1.indexBuffer) ? data1.indexBuffer->GetBuffer() : nullptr;
const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr; buffer2 = (data2.indexBuffer) ? data2.indexBuffer->GetBuffer() : nullptr;
if (buffer1 != buffer2)
if (buffer1 == buffer2)
return subMesh1 < subMesh2;
else
return buffer2 < buffer2;
}
bool NzForwardRenderQueue::BatchedStaticMeshComparator::operator()(const NzStaticMesh* subMesh1, const NzStaticMesh* subMesh2)
{
const NzIndexBuffer* iBuffer1 = subMesh1->GetIndexBuffer();
const NzBuffer* buffer1 = (iBuffer1) ? iBuffer1->GetBuffer() : nullptr;
const NzIndexBuffer* iBuffer2 = subMesh2->GetIndexBuffer();
const NzBuffer* buffer2 = (iBuffer2) ? iBuffer2->GetBuffer() : nullptr;
if (buffer1 == buffer2)
{
buffer1 = subMesh1->GetVertexBuffer()->GetBuffer();
buffer2 = subMesh2->GetVertexBuffer()->GetBuffer();
if (buffer1 == buffer2)
return subMesh1 < subMesh2;
else
return buffer1 < buffer2;
}
else
return buffer1 < buffer2; return buffer1 < buffer2;
buffer1 = data1.vertexBuffer->GetBuffer();
buffer2 = data2.vertexBuffer->GetBuffer();
if (buffer1 != buffer2)
return buffer1 < buffer2;
return data1.primitiveMode < data2.primitiveMode;
} }

View File

@ -84,12 +84,12 @@ bool NzForwardRenderTechnique::Draw(const NzScene* scene) const
if (!m_renderQueue.opaqueModels.empty()) if (!m_renderQueue.opaqueModels.empty())
DrawOpaqueModels(scene); DrawOpaqueModels(scene);
if (!m_renderQueue.transparentModels.empty())
DrawTransparentModels(scene);
if (!m_renderQueue.sprites.empty()) if (!m_renderQueue.sprites.empty())
DrawSprites(scene); DrawSprites(scene);
if (!m_renderQueue.transparentsModels.empty())
DrawTransparentModels(scene);
// Les autres drawables (Exemple: Terrain) // Les autres drawables (Exemple: Terrain)
for (const NzDrawable* drawable : m_renderQueue.otherDrawables) for (const NzDrawable* drawable : m_renderQueue.otherDrawables)
drawable->Draw(); drawable->Draw();
@ -174,10 +174,9 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
if (used) if (used)
{ {
bool& renderQueueInstancing = std::get<1>(matIt.second); bool& renderQueueInstancing = std::get<1>(matIt.second);
NzForwardRenderQueue::BatchedSkeletalMeshContainer& skeletalContainer = std::get<2>(matIt.second); NzForwardRenderQueue::MeshInstanceContainer& meshInstances = std::get<2>(matIt.second);
NzForwardRenderQueue::BatchedStaticMeshContainer& staticContainer = std::get<3>(matIt.second);
if (!skeletalContainer.empty() || !staticContainer.empty()) if (!meshInstances.empty())
{ {
const NzMaterial* material = matIt.first; const NzMaterial* material = matIt.first;
@ -203,28 +202,17 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
lastShader = shader; lastShader = shader;
} }
// Meshes
// Meshs squelettiques for (auto& subMeshIt : meshInstances)
/*if (!skeletalContainer.empty())
{
NzRenderer::SetVertexBuffer(m_skinningBuffer); // Vertex buffer commun
for (auto& subMeshIt : container)
{
///TODO
}
}*/
// Meshs statiques
for (auto& subMeshIt : staticContainer)
{ {
const NzMeshData& meshData = subMeshIt.first;
const NzSpheref& boundingSphere = subMeshIt.second.first; const NzSpheref& boundingSphere = subMeshIt.second.first;
const NzStaticMesh* mesh = subMeshIt.first; std::vector<NzMatrix4f>& instances = subMeshIt.second.second;
std::vector<NzForwardRenderQueue::StaticData>& staticData = subMeshIt.second.second;
if (!staticData.empty()) if (!instances.empty())
{ {
const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer(); const NzIndexBuffer* indexBuffer = meshData.indexBuffer;
const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer(); const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer;
// Gestion du draw call avant la boucle de rendu // Gestion du draw call avant la boucle de rendu
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc; std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
@ -247,15 +235,12 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
NzRenderer::SetIndexBuffer(indexBuffer); NzRenderer::SetIndexBuffer(indexBuffer);
NzRenderer::SetVertexBuffer(vertexBuffer); NzRenderer::SetVertexBuffer(vertexBuffer);
nzPrimitiveMode primitiveMode = mesh->GetPrimitiveMode();
if (instancing) if (instancing)
{ {
// On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing)
NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer(); NzVertexBuffer* instanceBuffer = NzRenderer::GetInstanceBuffer();
instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4)); instanceBuffer->SetVertexDeclaration(NzVertexDeclaration::Get(nzVertexLayout_Matrix4));
unsigned int stride = instanceBuffer->GetStride();
// Avec l'instancing, impossible de sélectionner les lumières pour chaque objet // Avec l'instancing, impossible de sélectionner les lumières pour chaque objet
// Du coup, il n'est activé que pour les lumières directionnelles // Du coup, il n'est activé que pour les lumières directionnelles
unsigned int lightCount = m_directionalLights.GetLightCount(); unsigned int lightCount = m_directionalLights.GetLightCount();
@ -288,32 +273,26 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i); NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i);
} }
const NzForwardRenderQueue::StaticData* data = &staticData[0]; const NzMatrix4f* instanceMatrices = &instances[0];
unsigned int instanceCount = staticData.size(); unsigned int instanceCount = instances.size();
unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); unsigned int maxInstanceCount = instanceBuffer->GetVertexCount(); // On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing)
while (instanceCount > 0) while (instanceCount > 0)
{ {
// On calcule le nombre d'instances que l'on pourra afficher cette fois-ci (Selon la taille du buffer d'instancing)
unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount); unsigned int renderedInstanceCount = std::min(instanceCount, maxInstanceCount);
instanceCount -= renderedInstanceCount; instanceCount -= renderedInstanceCount;
NzBufferMapper<NzVertexBuffer> mapper(instanceBuffer, nzBufferAccess_DiscardAndWrite, 0, renderedInstanceCount); // On remplit l'instancing buffer avec nos matrices world
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(mapper.GetPointer()); instanceBuffer->Fill(instanceMatrices, 0, renderedInstanceCount, true);
instanceMatrices += renderedInstanceCount;
for (unsigned int i = 0; i < renderedInstanceCount; ++i) // Et on affiche
{ InstancedDrawFunc(renderedInstanceCount, meshData.primitiveMode, 0, indexCount);
std::memcpy(ptr, data->transformMatrix, sizeof(float)*16);
data++;
ptr += stride;
}
mapper.Unmap();
InstancedDrawFunc(renderedInstanceCount, primitiveMode, 0, indexCount);
} }
} }
// On n'oublie pas de désactiver le blending pour ne pas interférer sur le reste du rendu
NzRenderer::Enable(nzRendererParameter_Blend, false); NzRenderer::Enable(nzRendererParameter_Blend, false);
NzRenderer::SetDepthFunc(oldDepthFunc); NzRenderer::SetDepthFunc(oldDepthFunc);
} }
@ -321,16 +300,16 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
{ {
if (lightUniforms->exists) if (lightUniforms->exists)
{ {
for (const NzForwardRenderQueue::StaticData& data : staticData) for (const NzMatrix4f& matrix : instances)
{ {
unsigned int directionalLightCount = m_directionalLights.GetLightCount(); unsigned int directionalLightCount = m_directionalLights.GetLightCount();
unsigned int otherLightCount = m_lights.ComputeClosestLights(data.transformMatrix.GetTranslation() + boundingSphere.GetPosition(), boundingSphere.radius, m_maxLightPassPerObject*NAZARA_GRAPHICS_MAX_LIGHTPERPASS - directionalLightCount); unsigned int otherLightCount = m_lights.ComputeClosestLights(matrix.GetTranslation() + boundingSphere.GetPosition(), boundingSphere.radius, m_maxLightPassPerObject*NAZARA_GRAPHICS_MAX_LIGHTPERPASS - directionalLightCount);
unsigned int lightCount = directionalLightCount + otherLightCount; unsigned int lightCount = directionalLightCount + otherLightCount;
NzRenderer::SetMatrix(nzMatrixType_World, data.transformMatrix); NzRenderer::SetMatrix(nzMatrixType_World, matrix);
unsigned int directionalLightIndex = 0; unsigned int directionalLightIndex = 0;
unsigned int otherLightIndex = 0; unsigned int otherLightIndex = 0;
nzRendererComparison oldDepthFunc = NzRenderer::GetDepthFunc(); nzRendererComparison oldDepthFunc = NzRenderer::GetDepthFunc(); // Dans le cas où nous aurions à le changer
unsigned int passCount = (lightCount == 0) ? 1 : (lightCount-1)/NAZARA_GRAPHICS_MAX_LIGHTPERPASS + 1; unsigned int passCount = (lightCount == 0) ? 1 : (lightCount-1)/NAZARA_GRAPHICS_MAX_LIGHTPERPASS + 1;
for (unsigned int pass = 0; pass < passCount; ++pass) for (unsigned int pass = 0; pass < passCount; ++pass)
@ -349,6 +328,7 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
NzRenderer::SetDepthFunc(nzRendererComparison_Equal); NzRenderer::SetDepthFunc(nzRendererComparison_Equal);
} }
// On active les lumières de cette passe-ci
for (unsigned int i = 0; i < renderedLightCount; ++i) for (unsigned int i = 0; i < renderedLightCount; ++i)
{ {
if (directionalLightIndex >= directionalLightCount) if (directionalLightIndex >= directionalLightCount)
@ -357,10 +337,12 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
m_directionalLights.GetLight(directionalLightIndex++)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*i); m_directionalLights.GetLight(directionalLightIndex++)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*i);
} }
// On désactive l'éventuel surplus
for (unsigned int i = renderedLightCount; i < NAZARA_GRAPHICS_MAX_LIGHTPERPASS; ++i) for (unsigned int i = renderedLightCount; i < NAZARA_GRAPHICS_MAX_LIGHTPERPASS; ++i)
NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i); NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i);
DrawFunc(primitiveMode, 0, indexCount); // Et on passe à l'affichage
DrawFunc(meshData.primitiveMode, 0, indexCount);
} }
NzRenderer::Enable(nzRendererParameter_Blend, false); NzRenderer::Enable(nzRendererParameter_Blend, false);
@ -369,14 +351,17 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene) const
} }
else else
{ {
for (const NzForwardRenderQueue::StaticData& data : staticData) // Sans instancing, on doit effectuer un drawcall pour chaque instance
// Cela reste néanmoins plus rapide que l'instancing en dessous d'un certain nombre d'instances
// À cause du temps de modification du buffer d'instancing
for (const NzMatrix4f& matrix : instances)
{ {
NzRenderer::SetMatrix(nzMatrixType_World, data.transformMatrix); NzRenderer::SetMatrix(nzMatrixType_World, matrix);
DrawFunc(primitiveMode, 0, indexCount); DrawFunc(meshData.primitiveMode, 0, indexCount);
} }
} }
} }
staticData.clear(); instances.clear();
} }
} }
} }
@ -471,12 +456,12 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene) const
const NzShader* lastShader = nullptr; const NzShader* lastShader = nullptr;
unsigned int lightCount = 0; unsigned int lightCount = 0;
for (const std::pair<unsigned int, bool>& pair : m_renderQueue.transparentsModels) for (unsigned int index : m_renderQueue.transparentModels)
{ {
const NzForwardRenderQueue::TransparentModelData& modelData = m_renderQueue.transparentModelData[index];
// Matériau // Matériau
const NzMaterial* material = (pair.second) ? const NzMaterial* material = modelData.material;
m_renderQueue.transparentStaticModels[pair.first].material :
m_renderQueue.transparentSkeletalModels[pair.first].material;
// On commence par appliquer du matériau (et récupérer le shader ainsi activé) // On commence par appliquer du matériau (et récupérer le shader ainsi activé)
const NzShader* shader = material->Apply(); const NzShader* shader = material->Apply();
@ -501,52 +486,43 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene) const
} }
// Mesh // Mesh
if (pair.second) const NzMatrix4f& matrix = modelData.transformMatrix;
const NzMeshData& meshData = modelData.meshData;
const NzIndexBuffer* indexBuffer = meshData.indexBuffer;
const NzVertexBuffer* vertexBuffer = meshData.vertexBuffer;
// Gestion du draw call avant la boucle de rendu
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
unsigned int indexCount;
if (indexBuffer)
{ {
NzForwardRenderQueue::TransparentStaticModel& staticModel = m_renderQueue.transparentStaticModels[pair.first]; DrawFunc = NzRenderer::DrawIndexedPrimitives;
indexCount = indexBuffer->GetIndexCount();
const NzMatrix4f& matrix = staticModel.transformMatrix;
const NzStaticMesh* mesh = staticModel.mesh;
const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer();
const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer();
// Gestion du draw call avant la boucle de rendu
std::function<void(nzPrimitiveMode, unsigned int, unsigned int)> DrawFunc;
unsigned int indexCount;
if (indexBuffer)
{
DrawFunc = NzRenderer::DrawIndexedPrimitives;
indexCount = indexBuffer->GetIndexCount();
}
else
{
DrawFunc = NzRenderer::DrawPrimitives;
indexCount = vertexBuffer->GetVertexCount();
}
NzRenderer::SetIndexBuffer(indexBuffer);
NzRenderer::SetVertexBuffer(vertexBuffer);
// Calcul des lumières les plus proches
if (lightCount < NAZARA_GRAPHICS_MAX_LIGHTPERPASS && !m_lights.IsEmpty())
{
unsigned int count = std::min(NAZARA_GRAPHICS_MAX_LIGHTPERPASS - lightCount, m_lights.ComputeClosestLights(matrix.GetTranslation() + staticModel.boundingSphere.GetPosition(), staticModel.boundingSphere.radius, NAZARA_GRAPHICS_MAX_LIGHTPERPASS));
for (unsigned int i = 0; i < count; ++i)
m_lights.GetResult(i)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*(lightCount++));
}
for (unsigned int i = lightCount; i < NAZARA_GRAPHICS_MAX_LIGHTPERPASS; ++i)
NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i);
NzRenderer::SetMatrix(nzMatrixType_World, matrix);
DrawFunc(mesh->GetPrimitiveMode(), 0, indexCount);
} }
else else
{ {
///TODO DrawFunc = NzRenderer::DrawPrimitives;
indexCount = vertexBuffer->GetVertexCount();
} }
NzRenderer::SetIndexBuffer(indexBuffer);
NzRenderer::SetVertexBuffer(vertexBuffer);
// Calcul des lumières les plus proches
if (lightCount < NAZARA_GRAPHICS_MAX_LIGHTPERPASS && !m_lights.IsEmpty())
{
unsigned int count = std::min(NAZARA_GRAPHICS_MAX_LIGHTPERPASS - lightCount, m_lights.ComputeClosestLights(matrix.GetTranslation() + modelData.boundingSphere.GetPosition(), modelData.boundingSphere.radius, NAZARA_GRAPHICS_MAX_LIGHTPERPASS));
for (unsigned int i = 0; i < count; ++i)
m_lights.GetResult(i)->Enable(shader, lightUniforms->uniforms, lightUniforms->offset*(lightCount++));
}
for (unsigned int i = lightCount; i < NAZARA_GRAPHICS_MAX_LIGHTPERPASS; ++i)
NzLight::Disable(shader, lightUniforms->uniforms, lightUniforms->offset*i);
NzRenderer::SetMatrix(nzMatrixType_World, matrix);
DrawFunc(meshData.primitiveMode, 0, indexCount);
} }
} }

View File

@ -11,7 +11,7 @@
namespace namespace
{ {
nzTernary Check(NzInputStream& stream, const NzModelParameters& parameters) nzTernary CheckStatic(NzInputStream& stream, const NzModelParameters& parameters)
{ {
NazaraUnused(stream); NazaraUnused(stream);
NazaraUnused(parameters); NazaraUnused(parameters);
@ -19,7 +19,7 @@ namespace
return nzTernary_Unknown; return nzTernary_Unknown;
} }
bool Load(NzModel* model, NzInputStream& stream, const NzModelParameters& parameters) bool LoadStatic(NzModel* model, NzInputStream& stream, const NzModelParameters& parameters)
{ {
NazaraUnused(parameters); NazaraUnused(parameters);
@ -31,6 +31,12 @@ namespace
return false; return false;
} }
if (mesh->IsAnimable())
{
NazaraError("Can't load static mesh into animated model");
return false;
}
// Nous ne pouvons plus avoir recours au smart pointeur à partir d'ici si nous voulons être exception-safe // Nous ne pouvons plus avoir recours au smart pointeur à partir d'ici si nous voulons être exception-safe
NzMesh* meshPtr = mesh.get(); NzMesh* meshPtr = mesh.get();
@ -38,20 +44,6 @@ namespace
model->SetMesh(meshPtr); model->SetMesh(meshPtr);
mesh.release(); mesh.release();
if (parameters.loadAnimation && meshPtr->IsAnimable())
{
NzString animationPath = meshPtr->GetAnimation();
if (!animationPath.IsEmpty())
{
std::unique_ptr<NzAnimation> animation(new NzAnimation);
animation->SetPersistent(false);
if (animation->LoadFromFile(animationPath, parameters.animation) && model->SetAnimation(animation.get()))
animation.release();
else
NazaraWarning("Failed to load animation");
}
}
if (parameters.loadMaterials) if (parameters.loadMaterials)
{ {
unsigned int matCount = model->GetMaterialCount(); unsigned int matCount = model->GetMaterialCount();
@ -81,10 +73,10 @@ namespace
void NzLoaders_Mesh_Register() void NzLoaders_Mesh_Register()
{ {
NzModelLoader::RegisterLoader(NzMeshLoader::IsExtensionSupported, Check, Load); NzModelLoader::RegisterLoader(NzMeshLoader::IsExtensionSupported, CheckStatic, LoadStatic);
} }
void NzLoaders_Mesh_Unregister() void NzLoaders_Mesh_Unregister()
{ {
NzModelLoader::UnregisterLoader(NzMeshLoader::IsExtensionSupported, Check, Load); NzModelLoader::UnregisterLoader(NzMeshLoader::IsExtensionSupported, CheckStatic, LoadStatic);
} }

View File

@ -6,7 +6,7 @@
#include <Nazara/Graphics/AbstractRenderQueue.hpp> #include <Nazara/Graphics/AbstractRenderQueue.hpp>
#include <Nazara/Graphics/Camera.hpp> #include <Nazara/Graphics/Camera.hpp>
#include <Nazara/Graphics/Config.hpp> #include <Nazara/Graphics/Config.hpp>
#include <Nazara/Utility/SkeletalMesh.hpp> #include <Nazara/Utility/MeshData.hpp>
#include <Nazara/Utility/StaticMesh.hpp> #include <Nazara/Utility/StaticMesh.hpp>
#include <memory> #include <memory>
#include <Nazara/Graphics/Debug.hpp> #include <Nazara/Graphics/Debug.hpp>
@ -18,9 +18,6 @@ NzModelParameters::NzModelParameters()
bool NzModelParameters::IsValid() const bool NzModelParameters::IsValid() const
{ {
if (loadAnimation && !animation.IsValid())
return false;
if (loadMaterials && !material.IsValid()) if (loadMaterials && !material.IsValid())
return false; return false;
@ -28,8 +25,6 @@ bool NzModelParameters::IsValid() const
} }
NzModel::NzModel() : NzModel::NzModel() :
m_currentSequence(nullptr),
m_animationEnabled(true),
m_boundingVolumeUpdated(true), m_boundingVolumeUpdated(true),
m_matCount(0), m_matCount(0),
m_skin(0), m_skin(0),
@ -41,25 +36,16 @@ NzModel::NzModel(const NzModel& model) :
NzSceneNode(model), NzSceneNode(model),
m_materials(model.m_materials), m_materials(model.m_materials),
m_boundingVolume(model.m_boundingVolume), m_boundingVolume(model.m_boundingVolume),
m_currentSequence(model.m_currentSequence),
m_animationEnabled(model.m_animationEnabled),
m_boundingVolumeUpdated(model.m_boundingVolumeUpdated), m_boundingVolumeUpdated(model.m_boundingVolumeUpdated),
m_interpolation(model.m_interpolation),
m_currentFrame(model.m_currentFrame),
m_matCount(model.m_matCount), m_matCount(model.m_matCount),
m_nextFrame(model.m_nextFrame),
m_skin(model.m_skin), m_skin(model.m_skin),
m_skinCount(model.m_skinCount) m_skinCount(model.m_skinCount)
{ {
if (model.m_mesh) if (model.m_mesh)
{ {
// Nous n'avons une animation et des matériaux que si nous avons un mesh // Nous n'avons des matériaux que si nous avons un mesh
m_animation = model.m_animation;
m_mesh = model.m_mesh; m_mesh = model.m_mesh;
m_materials = model.m_materials; m_materials = model.m_materials;
if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal)
m_skeleton = model.m_skeleton;
} }
SetParent(model); SetParent(model);
@ -77,64 +63,18 @@ void NzModel::AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const
unsigned int submeshCount = m_mesh->GetSubMeshCount(); unsigned int submeshCount = m_mesh->GetSubMeshCount();
for (unsigned int i = 0; i < submeshCount; ++i) for (unsigned int i = 0; i < submeshCount; ++i)
{ {
NzSubMesh* subMesh = m_mesh->GetSubMesh(i); const NzStaticMesh* mesh = static_cast<const NzStaticMesh*>(m_mesh->GetSubMesh(i));
NzMaterial* material = m_materials[subMesh->GetMaterialIndex()]; NzMaterial* material = m_materials[mesh->GetMaterialIndex()];
renderQueue->AddSubMesh(material, subMesh, transformMatrix); NzMeshData meshData;
meshData.indexBuffer = mesh->GetIndexBuffer();
meshData.primitiveMode = mesh->GetPrimitiveMode();
meshData.vertexBuffer = mesh->GetVertexBuffer();
renderQueue->AddMesh(material, meshData, mesh->GetAABB(), transformMatrix);
} }
} }
void NzModel::AdvanceAnimation(float elapsedTime)
{
#if NAZARA_GRAPHICS_SAFE
if (!m_animation)
{
NazaraError("Model has no animation");
return;
}
#endif
m_interpolation += m_currentSequence->frameRate * elapsedTime;
while (m_interpolation > 1.f)
{
m_interpolation -= 1.f;
unsigned lastFrame = m_currentSequence->firstFrame + m_currentSequence->frameCount - 1;
if (m_nextFrame+1 > lastFrame)
{
if (m_animation->IsLoopPointInterpolationEnabled())
{
m_currentFrame = m_nextFrame;
m_nextFrame = m_currentSequence->firstFrame;
}
else
{
m_currentFrame = m_currentSequence->firstFrame;
m_nextFrame = m_currentFrame+1;
}
}
else
{
m_currentFrame = m_nextFrame;
m_nextFrame++;
}
}
m_animation->AnimateSkeleton(&m_skeleton, m_currentFrame, m_nextFrame, m_interpolation);
m_boundingVolume.MakeNull();
m_boundingVolumeUpdated = false;
}
void NzModel::EnableAnimation(bool animation)
{
m_animationEnabled = animation;
}
NzAnimation* NzModel::GetAnimation() const
{
return m_animation;
}
const NzBoundingVolumef& NzModel::GetBoundingVolume() const const NzBoundingVolumef& NzModel::GetBoundingVolume() const
{ {
#if NAZARA_GRAPHICS_SAFE #if NAZARA_GRAPHICS_SAFE
@ -254,16 +194,6 @@ nzSceneNodeType NzModel::GetSceneNodeType() const
return nzSceneNodeType_Model; return nzSceneNodeType_Model;
} }
NzSkeleton* NzModel::GetSkeleton()
{
return &m_skeleton;
}
const NzSkeleton* NzModel::GetSkeleton() const
{
return &m_skeleton;
}
unsigned int NzModel::GetSkin() const unsigned int NzModel::GetSkin() const
{ {
return m_skin; return m_skin;
@ -274,14 +204,9 @@ unsigned int NzModel::GetSkinCount() const
return m_skinCount; return m_skinCount;
} }
bool NzModel::HasAnimation() const bool NzModel::IsAnimated() const
{ {
return m_animation != nullptr; return false;
}
bool NzModel::IsAnimationEnabled() const
{
return m_animationEnabled;
} }
bool NzModel::IsDrawable() const bool NzModel::IsDrawable() const
@ -312,70 +237,16 @@ bool NzModel::LoadFromStream(NzInputStream& stream, const NzModelParameters& par
void NzModel::Reset() void NzModel::Reset()
{ {
if (m_scene)
m_scene->UnregisterForUpdate(this);
m_matCount = 0; m_matCount = 0;
m_skinCount = 0; m_skinCount = 0;
if (m_mesh) if (m_mesh)
{ {
m_animation.Reset();
m_mesh.Reset(); m_mesh.Reset();
m_materials.clear(); m_materials.clear();
m_skeleton.Destroy();
} }
} }
bool NzModel::SetAnimation(NzAnimation* animation)
{
#if NAZARA_GRAPHICS_SAFE
if (!m_mesh)
{
NazaraError("Model has no animation");
return false;
}
if (animation)
{
if (!animation->IsValid())
{
NazaraError("Invalid animation");
return false;
}
if (animation->GetType() != m_mesh->GetAnimationType())
{
NazaraError("Animation type must match mesh animation type");
return false;
}
if (animation->GetType() == nzAnimationType_Skeletal && animation->GetJointCount() != m_mesh->GetJointCount())
{
NazaraError("Animation joint count must match mesh joint count");
return false;
}
}
#endif
m_animation = animation;
if (m_animation)
{
m_currentFrame = 0;
m_interpolation = 0.f;
SetSequence(0);
if (m_scene)
m_scene->RegisterForUpdate(this);
}
else if (m_scene)
m_scene->UnregisterForUpdate(this);
return true;
}
bool NzModel::SetMaterial(const NzString& subMeshName, NzMaterial* material) bool NzModel::SetMaterial(const NzString& subMeshName, NzMaterial* material)
{ {
NzSubMesh* subMesh = m_mesh->GetSubMesh(subMeshName); NzSubMesh* subMesh = m_mesh->GetSubMesh(subMeshName);
@ -480,25 +351,18 @@ void NzModel::SetMaterial(unsigned int skinIndex, unsigned int matIndex, NzMater
void NzModel::SetMesh(NzMesh* mesh) void NzModel::SetMesh(NzMesh* mesh)
{ {
#if NAZARA_GRAPHICS_SAFE
if (mesh && !mesh->IsValid())
{
NazaraError("Invalid mesh");
return;
}
#endif
m_mesh = mesh; m_mesh = mesh;
if (m_mesh) if (m_mesh)
{ {
m_boundingVolume.MakeNull();
m_boundingVolumeUpdated = false;
if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal)
m_skeleton = *mesh->GetSkeleton(); // Copie du squelette template
if (m_animation)
{
if (m_animation->GetJointCount() != m_mesh->GetJointCount())
{
NazaraWarning("Animation joint count is not matching new mesh joint count");
SetAnimation(nullptr);
}
}
m_matCount = mesh->GetMaterialCount(); m_matCount = mesh->GetMaterialCount();
m_materials.clear(); m_materials.clear();
m_materials.resize(m_matCount, NzMaterial::GetDefault()); m_materials.resize(m_matCount, NzMaterial::GetDefault());
@ -506,61 +370,12 @@ void NzModel::SetMesh(NzMesh* mesh)
} }
else else
{ {
m_boundingVolume.MakeNull();
m_boundingVolumeUpdated = true;
m_matCount = 0; m_matCount = 0;
m_materials.clear(); m_materials.clear();
m_skinCount = 0; m_skinCount = 0;
SetAnimation(nullptr);
}
}
bool NzModel::SetSequence(const NzString& sequenceName)
{
///TODO: Rendre cette erreur "safe" avec le nouveau système de gestions d'erreur (No-log)
#if NAZARA_GRAPHICS_SAFE
if (!m_animation)
{
NazaraError("Model has no animation");
return false;
}
#endif
const NzSequence* currentSequence = m_animation->GetSequence(sequenceName);
if (!currentSequence)
{
NazaraError("Sequence not found");
return false;
} }
m_currentSequence = currentSequence; InvalidateBoundingVolume();
m_nextFrame = m_currentSequence->firstFrame;
return true;
}
void NzModel::SetSequence(unsigned int sequenceIndex)
{
#if NAZARA_GRAPHICS_SAFE
if (!m_animation)
{
NazaraError("Model has no animation");
return;
}
#endif
const NzSequence* currentSequence = m_animation->GetSequence(sequenceIndex);
#if NAZARA_GRAPHICS_SAFE
if (!currentSequence)
{
NazaraError("Sequence not found");
return;
}
#endif
m_currentSequence = currentSequence;
m_nextFrame = m_currentSequence->firstFrame;
} }
void NzModel::SetSkin(unsigned int skin) void NzModel::SetSkin(unsigned int skin)
@ -594,23 +409,14 @@ NzModel& NzModel::operator=(const NzModel& node)
{ {
NzSceneNode::operator=(node); NzSceneNode::operator=(node);
m_animation = node.m_animation;
m_animationEnabled = node.m_animationEnabled;
m_boundingVolume = node.m_boundingVolume; m_boundingVolume = node.m_boundingVolume;
m_boundingVolumeUpdated = node.m_boundingVolumeUpdated; m_boundingVolumeUpdated = node.m_boundingVolumeUpdated;
m_currentFrame = node.m_currentFrame;
m_currentSequence = node.m_currentSequence;
m_interpolation = node.m_interpolation;
m_matCount = node.m_matCount; m_matCount = node.m_matCount;
m_materials = node.m_materials; m_materials = node.m_materials;
m_mesh = node.m_mesh; m_mesh = node.m_mesh;
m_nextFrame = node.m_nextFrame;
m_skin = node.m_skin; m_skin = node.m_skin;
m_skinCount = node.m_skinCount; m_skinCount = node.m_skinCount;
if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal)
m_skeleton = node.m_skeleton;
return *this; return *this;
} }
@ -619,22 +425,13 @@ NzModel& NzModel::operator=(NzModel&& node)
NzSceneNode::operator=(node); NzSceneNode::operator=(node);
// Ressources // Ressources
m_animation = std::move(node.m_animation);
m_mesh = std::move(node.m_mesh); m_mesh = std::move(node.m_mesh);
m_materials = std::move(node.m_materials); m_materials = std::move(node.m_materials);
if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal)
m_skeleton = std::move(node.m_skeleton);
// Paramètres // Paramètres
m_animationEnabled = node.m_animationEnabled;
m_boundingVolume = node.m_boundingVolume; m_boundingVolume = node.m_boundingVolume;
m_boundingVolumeUpdated = node.m_boundingVolumeUpdated; m_boundingVolumeUpdated = node.m_boundingVolumeUpdated;
m_currentFrame = node.m_currentFrame;
m_currentSequence = node.m_currentSequence;
m_interpolation = node.m_interpolation;
m_matCount = node.m_matCount; m_matCount = node.m_matCount;
m_nextFrame = node.m_nextFrame;
m_skin = node.m_skin; m_skin = node.m_skin;
m_skinCount = node.m_skinCount; m_skinCount = node.m_skinCount;
@ -656,32 +453,10 @@ void NzModel::InvalidateNode()
m_boundingVolumeUpdated = false; m_boundingVolumeUpdated = false;
} }
void NzModel::Register()
{
if (m_animation)
m_scene->RegisterForUpdate(this);
}
void NzModel::Unregister()
{
m_scene->UnregisterForUpdate(this);
}
void NzModel::Update()
{
if (m_animationEnabled && m_animation)
AdvanceAnimation(m_scene->GetUpdateTime());
}
void NzModel::UpdateBoundingVolume() const void NzModel::UpdateBoundingVolume() const
{ {
if (m_boundingVolume.IsNull()) if (m_boundingVolume.IsNull())
{ m_boundingVolume.Set(m_mesh->GetAABB());
if (m_mesh->GetAnimationType() == nzAnimationType_Skeletal)
m_boundingVolume.Set(m_skeleton.GetAABB());
else
m_boundingVolume.Set(m_mesh->GetAABB());
}
if (!m_transformMatrixUpdated) if (!m_transformMatrixUpdated)
UpdateTransformMatrix(); UpdateTransformMatrix();