Fixed transparent model rendering

Former-commit-id: da363be3b19951ae49996fe9d05998420e80911f
This commit is contained in:
Lynix 2013-08-24 16:22:55 +02:00
parent 8611f629cc
commit 24e56790cc
8 changed files with 121 additions and 96 deletions

View File

@ -26,6 +26,7 @@ class NAZARA_API NzAbstractViewer
virtual float GetAspectRatio() const = 0; virtual float GetAspectRatio() const = 0;
virtual NzVector3f GetEyePosition() const = 0; virtual NzVector3f GetEyePosition() const = 0;
virtual NzVector3f GetForward() const = 0;
virtual const NzFrustumf& GetFrustum() const = 0; virtual const NzFrustumf& GetFrustum() const = 0;
virtual const NzMatrix4f& GetProjectionMatrix() const = 0; virtual const NzMatrix4f& GetProjectionMatrix() const = 0;
virtual const NzRenderTarget* GetTarget() const = 0; virtual const NzRenderTarget* GetTarget() const = 0;

View File

@ -29,6 +29,7 @@ class NAZARA_API NzCamera : public NzAbstractViewer, public NzNode, NzRenderTarg
float GetAspectRatio() const; float GetAspectRatio() const;
NzVector3f GetEyePosition() const; NzVector3f GetEyePosition() const;
NzVector3f GetForward() const;
float GetFOV() const; float GetFOV() const;
const NzFrustumf& GetFrustum() const; const NzFrustumf& GetFrustum() const;
const NzMatrix4f& GetProjectionMatrix() const; const NzMatrix4f& GetProjectionMatrix() const;

View File

@ -16,7 +16,7 @@
#include <map> #include <map>
#include <tuple> #include <tuple>
class NzCamera; class NzAbstractViewer;
class NzMaterial; class NzMaterial;
class NzSkeletalMesh; class NzSkeletalMesh;
class NzStaticMesh; class NzStaticMesh;
@ -33,10 +33,11 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource
void AddLight(const NzLight* light); void AddLight(const NzLight* light);
void AddModel(const NzModel* model); void AddModel(const NzModel* model);
void AddSprite(const NzSprite* sprite); void AddSprite(const NzSprite* sprite);
void AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix);
void Clear(bool fully); void Clear(bool fully);
void Sort(const NzCamera& camera); void Sort(const NzAbstractViewer* viewer);
private: private:
bool OnResourceDestroy(const NzResource* resource, int index) override; bool OnResourceDestroy(const NzResource* resource, int index) override;
@ -56,7 +57,7 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource
{ {
NzMatrix4f transformMatrix; NzMatrix4f transformMatrix;
NzSpheref boundingSphere; NzSpheref boundingSphere;
NzMaterial* material; const NzMaterial* material;
}; };
struct TransparentSkeletalModel : public TransparentModel struct TransparentSkeletalModel : public TransparentModel
@ -66,7 +67,7 @@ class NAZARA_API NzForwardRenderQueue : public NzAbstractRenderQueue, NzResource
struct TransparentStaticModel : public TransparentModel struct TransparentStaticModel : public TransparentModel
{ {
NzStaticMesh* mesh; const NzStaticMesh* mesh;
}; };

View File

@ -29,6 +29,7 @@ class NAZARA_API NzView : public NzAbstractViewer, public NzNode, NzRenderTarget
float GetAspectRatio() const; float GetAspectRatio() const;
NzVector3f GetEyePosition() const; NzVector3f GetEyePosition() const;
NzVector3f GetForward() const;
const NzFrustumf& GetFrustum() const; const NzFrustumf& GetFrustum() const;
const NzMatrix4f& GetProjectionMatrix() const; const NzMatrix4f& GetProjectionMatrix() const;
const NzRenderTarget* GetTarget() const; const NzRenderTarget* GetTarget() const;

View File

@ -62,6 +62,11 @@ NzVector3f NzCamera::GetEyePosition() const
return GetPosition(nzCoordSys_Global); return GetPosition(nzCoordSys_Global);
} }
NzVector3f NzCamera::GetForward() const
{
return NzNode::GetForward();
}
float NzCamera::GetFOV() const float NzCamera::GetFOV() const
{ {
return m_fov; return m_fov;

View File

@ -3,7 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp // For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/ForwardRenderQueue.hpp> #include <Nazara/Graphics/ForwardRenderQueue.hpp>
#include <Nazara/Graphics/Camera.hpp> #include <Nazara/Graphics/AbstractViewer.hpp>
#include <Nazara/Graphics/Light.hpp> #include <Nazara/Graphics/Light.hpp>
#include <Nazara/Graphics/Model.hpp> #include <Nazara/Graphics/Model.hpp>
#include <Nazara/Graphics/Sprite.hpp> #include <Nazara/Graphics/Sprite.hpp>
@ -94,81 +94,7 @@ void NzForwardRenderQueue::AddModel(const NzModel* model)
NzSubMesh* subMesh = mesh->GetSubMesh(i); NzSubMesh* subMesh = mesh->GetSubMesh(i);
NzMaterial* material = model->GetMaterial(subMesh->GetMaterialIndex()); NzMaterial* material = model->GetMaterial(subMesh->GetMaterialIndex());
switch (subMesh->GetAnimationType()) AddSubMesh(material, subMesh, transformMatrix);
{
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:
{
NzStaticMesh* staticMesh = static_cast<NzStaticMesh*>(subMesh);
if (material->IsEnabled(nzRendererParameter_Blend))
{
unsigned int index = transparentStaticModels.size();
transparentStaticModels.resize(index+1);
TransparentStaticModel& data = transparentStaticModels.back();
data.boundingSphere = staticMesh->GetAABB().GetSquaredBoundingSphere();
data.material = material;
data.mesh = staticMesh;
data.transformMatrix = transformMatrix;
transparentsModels.push_back(std::make_pair(index, true));
}
else
{
auto pair = opaqueModels.insert(std::make_pair(material, BatchedModelContainer::mapped_type()));
if (pair.second)
material->AddResourceListener(this, ResourceType_Material);
bool& used = std::get<0>(pair.first->second);
bool& enableInstancing = std::get<1>(pair.first->second);
used = true;
auto& meshMap = std::get<3>(pair.first->second);
auto pair2 = meshMap.insert(std::make_pair(staticMesh, BatchedStaticMeshContainer::mapped_type()));
if (pair2.second)
{
staticMesh->AddResourceListener(this, ResourceType_StaticMesh);
NzSpheref& squaredBoundingSphere = pair2.first->second.first;
squaredBoundingSphere.Set(staticMesh->GetAABB().GetSquaredBoundingSphere());
///TODO: Écouter le StaticMesh pour repérer tout changement de géométrie
}
std::vector<StaticData>& staticDataContainer = pair2.first->second.second;
unsigned int instanceCount = staticDataContainer.size() + 1;
// As-t-on 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;
}
}
} }
} }
@ -180,11 +106,97 @@ void NzForwardRenderQueue::AddSprite(const NzSprite* sprite)
NazaraError("Invalid sprite"); NazaraError("Invalid sprite");
return; return;
} }
if (!sprite->IsDrawable())
{
NazaraError("Sprite is not drawable");
return;
}
#endif #endif
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);
TransparentStaticModel& data = transparentStaticModels.back();
data.boundingSphere = NzSpheref(transformMatrix.GetTranslation(), staticMesh->GetAABB().GetSquaredRadius());
data.material = material;
data.mesh = staticMesh;
data.transformMatrix = transformMatrix;
transparentsModels.push_back(std::make_pair(index, true));
}
else
{
auto pair = opaqueModels.insert(std::make_pair(material, BatchedModelContainer::mapped_type()));
if (pair.second)
material->AddResourceListener(this, ResourceType_Material);
bool& used = std::get<0>(pair.first->second);
bool& enableInstancing = std::get<1>(pair.first->second);
used = true;
auto& meshMap = std::get<3>(pair.first->second);
auto pair2 = meshMap.insert(std::make_pair(staticMesh, BatchedStaticMeshContainer::mapped_type()));
if (pair2.second)
{
staticMesh->AddResourceListener(this, ResourceType_StaticMesh);
NzSpheref& squaredBoundingSphere = pair2.first->second.first;
squaredBoundingSphere.Set(staticMesh->GetAABB().GetSquaredBoundingSphere());
///TODO: Écouter le StaticMesh pour repérer tout changement de géométrie
}
std::vector<StaticData>& staticDataContainer = pair2.first->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)
{ {
directionnalLights.clear(); directionnalLights.clear();
@ -201,7 +213,7 @@ void NzForwardRenderQueue::Clear(bool fully)
} }
} }
void NzForwardRenderQueue::Sort(const NzCamera& camera) void NzForwardRenderQueue::Sort(const NzAbstractViewer* viewer)
{ {
struct TransparentModelComparator struct TransparentModelComparator
{ {
@ -211,22 +223,22 @@ void NzForwardRenderQueue::Sort(const NzCamera& camera)
queue->transparentStaticModels[index1.first].boundingSphere : queue->transparentStaticModels[index1.first].boundingSphere :
queue->transparentSkeletalModels[index1.first].boundingSphere; queue->transparentSkeletalModels[index1.first].boundingSphere;
const NzSpheref& sphere2 = (index1.second) ? const NzSpheref& sphere2 = (index2.second) ?
queue->transparentStaticModels[index2.first].boundingSphere : queue->transparentStaticModels[index2.first].boundingSphere :
queue->transparentSkeletalModels[index2.first].boundingSphere; queue->transparentSkeletalModels[index2.first].boundingSphere;
NzVector3f position1 = sphere1.GetNegativeVertex(cameraNormal); NzVector3f position1 = sphere1.GetNegativeVertex(viewerNormal);
NzVector3f position2 = sphere2.GetNegativeVertex(cameraNormal); NzVector3f position2 = sphere2.GetNegativeVertex(viewerNormal);
return nearPlane.Distance(position1) < nearPlane.Distance(position2); return nearPlane.Distance(position1) > nearPlane.Distance(position2);
} }
NzForwardRenderQueue* queue; NzForwardRenderQueue* queue;
NzPlanef nearPlane; NzPlanef nearPlane;
NzVector3f cameraNormal; NzVector3f viewerNormal;
}; };
TransparentModelComparator comparator {this, camera.GetFrustum().GetPlane(nzFrustumPlane_Near), camera.GetForward()}; TransparentModelComparator comparator {this, viewer->GetFrustum().GetPlane(nzFrustumPlane_Near), viewer->GetForward()};
std::sort(transparentsModels.begin(), transparentsModels.end(), comparator); std::sort(transparentsModels.begin(), transparentsModels.end(), comparator);
} }

View File

@ -76,9 +76,9 @@ void NzForwardRenderTechnique::Clear(const NzScene* scene)
void NzForwardRenderTechnique::Draw(const NzScene* scene) void NzForwardRenderTechnique::Draw(const NzScene* scene)
{ {
// Rendu en projection perspective (3D)
m_directionnalLights.SetLights(&m_renderQueue.directionnalLights[0], m_renderQueue.directionnalLights.size()); m_directionnalLights.SetLights(&m_renderQueue.directionnalLights[0], m_renderQueue.directionnalLights.size());
m_lights.SetLights(&m_renderQueue.lights[0], m_renderQueue.lights.size()); m_lights.SetLights(&m_renderQueue.lights[0], m_renderQueue.lights.size());
m_renderQueue.Sort(scene->GetViewer());
if (!m_renderQueue.opaqueModels.empty()) if (!m_renderQueue.opaqueModels.empty())
DrawOpaqueModels(scene, m_renderQueue.opaqueModels); DrawOpaqueModels(scene, m_renderQueue.opaqueModels);
@ -167,6 +167,8 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene, NzForwardR
NzAbstractViewer* viewer = scene->GetViewer(); NzAbstractViewer* viewer = scene->GetViewer();
const NzShaderProgram* lastProgram = nullptr; const NzShaderProgram* lastProgram = nullptr;
unsigned int lightCount = 0;
for (auto& matIt : opaqueModels) for (auto& matIt : opaqueModels)
{ {
bool& used = std::get<0>(matIt.second); bool& used = std::get<0>(matIt.second);
@ -188,8 +190,6 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene, NzForwardR
// On commence par récupérer le programme du matériau // On commence par récupérer le programme du matériau
const NzShaderProgram* program = material->GetShaderProgram(nzShaderTarget_Model, (instancing) ? nzShaderFlags_Instancing : 0); const NzShaderProgram* program = material->GetShaderProgram(nzShaderTarget_Model, (instancing) ? nzShaderFlags_Instancing : 0);
unsigned int lightCount = 0;
// Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas // Les uniformes sont conservées au sein d'un programme, inutile de les renvoyer tant qu'il ne change pas
if (program != lastProgram) if (program != lastProgram)
{ {
@ -404,19 +404,18 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene, NzFor
{ {
NzAbstractViewer* viewer = scene->GetViewer(); NzAbstractViewer* viewer = scene->GetViewer();
const NzShaderProgram* lastProgram = nullptr; const NzShaderProgram* lastProgram = nullptr;
unsigned int lightCount = 0;
for (const std::pair<unsigned int, bool>& pair : transparentModels) for (const std::pair<unsigned int, bool>& pair : transparentModels)
{ {
// Matériau // Matériau
NzMaterial* material = (pair.second) ? const NzMaterial* material = (pair.second) ?
m_renderQueue.transparentStaticModels[pair.first].material : m_renderQueue.transparentStaticModels[pair.first].material :
m_renderQueue.transparentSkeletalModels[pair.first].material; m_renderQueue.transparentSkeletalModels[pair.first].material;
// On commence par récupérer le shader du matériau // On commence par récupérer le shader du matériau
const NzShaderProgram* program = material->GetShaderProgram(nzShaderTarget_Model, 0); const NzShaderProgram* program = material->GetShaderProgram(nzShaderTarget_Model, 0);
unsigned int lightCount = 0;
// Les uniformes sont conservées au sein du shader, inutile de les renvoyer tant que le shader reste le même // Les uniformes sont conservées au sein du shader, inutile de les renvoyer tant que le shader reste le même
if (program != lastProgram) if (program != lastProgram)
{ {
@ -443,7 +442,7 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene, NzFor
NzForwardRenderQueue::TransparentStaticModel& staticModel = m_renderQueue.transparentStaticModels[pair.first]; NzForwardRenderQueue::TransparentStaticModel& staticModel = m_renderQueue.transparentStaticModels[pair.first];
const NzMatrix4f& matrix = staticModel.transformMatrix; const NzMatrix4f& matrix = staticModel.transformMatrix;
NzStaticMesh* mesh = staticModel.mesh; const NzStaticMesh* mesh = staticModel.mesh;
const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer(); const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer();
const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer(); const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer();

View File

@ -60,6 +60,11 @@ NzVector3f NzView::GetEyePosition() const
return GetPosition(nzCoordSys_Global); return GetPosition(nzCoordSys_Global);
} }
NzVector3f NzView::GetForward() const
{
return NzNode::GetForward();
}
const NzFrustumf& NzView::GetFrustum() const const NzFrustumf& NzView::GetFrustum() const
{ {
if (!m_frustumUpdated) if (!m_frustumUpdated)