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 NzVector3f GetEyePosition() const = 0;
virtual NzVector3f GetForward() const = 0;
virtual const NzFrustumf& GetFrustum() const = 0;
virtual const NzMatrix4f& GetProjectionMatrix() 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;
NzVector3f GetEyePosition() const;
NzVector3f GetForward() const;
float GetFOV() const;
const NzFrustumf& GetFrustum() const;
const NzMatrix4f& GetProjectionMatrix() const;

View File

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

View File

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

View File

@ -3,7 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/ForwardRenderQueue.hpp>
#include <Nazara/Graphics/Camera.hpp>
#include <Nazara/Graphics/AbstractViewer.hpp>
#include <Nazara/Graphics/Light.hpp>
#include <Nazara/Graphics/Model.hpp>
#include <Nazara/Graphics/Sprite.hpp>
@ -94,6 +94,31 @@ void NzForwardRenderQueue::AddModel(const NzModel* model)
NzSubMesh* subMesh = mesh->GetSubMesh(i);
NzMaterial* material = model->GetMaterial(subMesh->GetMaterialIndex());
AddSubMesh(material, subMesh, transformMatrix);
}
}
void NzForwardRenderQueue::AddSprite(const NzSprite* sprite)
{
#if NAZARA_GRAPHICS_SAFE
if (!sprite)
{
NazaraError("Invalid sprite");
return;
}
if (!sprite->IsDrawable())
{
NazaraError("Sprite is not drawable");
return;
}
#endif
sprites[sprite->GetMaterial()].push_back(sprite);
}
void NzForwardRenderQueue::AddSubMesh(const NzMaterial* material, const NzSubMesh* subMesh, const NzMatrix4f& transformMatrix)
{
switch (subMesh->GetAnimationType())
{
case nzAnimationType_Skeletal:
@ -116,14 +141,15 @@ void NzForwardRenderQueue::AddModel(const NzModel* model)
case nzAnimationType_Static:
{
NzStaticMesh* staticMesh = static_cast<NzStaticMesh*>(subMesh);
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 = staticMesh->GetAABB().GetSquaredBoundingSphere();
data.boundingSphere = NzSpheref(transformMatrix.GetTranslation(), staticMesh->GetAABB().GetSquaredRadius());
data.material = material;
data.mesh = staticMesh;
data.transformMatrix = transformMatrix;
@ -157,7 +183,7 @@ void NzForwardRenderQueue::AddModel(const NzModel* model)
unsigned int instanceCount = staticDataContainer.size() + 1;
// As-t-on suffisamment d'instances pour que le coût d'utilisation de l'instancing soit payé ?
// 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
@ -170,20 +196,6 @@ void NzForwardRenderQueue::AddModel(const NzModel* model)
}
}
}
}
void NzForwardRenderQueue::AddSprite(const NzSprite* sprite)
{
#if NAZARA_GRAPHICS_SAFE
if (!sprite)
{
NazaraError("Invalid sprite");
return;
}
#endif
sprites[sprite->GetMaterial()].push_back(sprite);
}
void NzForwardRenderQueue::Clear(bool fully)
{
@ -201,7 +213,7 @@ void NzForwardRenderQueue::Clear(bool fully)
}
}
void NzForwardRenderQueue::Sort(const NzCamera& camera)
void NzForwardRenderQueue::Sort(const NzAbstractViewer* viewer)
{
struct TransparentModelComparator
{
@ -211,22 +223,22 @@ void NzForwardRenderQueue::Sort(const NzCamera& camera)
queue->transparentStaticModels[index1.first].boundingSphere :
queue->transparentSkeletalModels[index1.first].boundingSphere;
const NzSpheref& sphere2 = (index1.second) ?
const NzSpheref& sphere2 = (index2.second) ?
queue->transparentStaticModels[index2.first].boundingSphere :
queue->transparentSkeletalModels[index2.first].boundingSphere;
NzVector3f position1 = sphere1.GetNegativeVertex(cameraNormal);
NzVector3f position2 = sphere2.GetNegativeVertex(cameraNormal);
NzVector3f position1 = sphere1.GetNegativeVertex(viewerNormal);
NzVector3f position2 = sphere2.GetNegativeVertex(viewerNormal);
return nearPlane.Distance(position1) < nearPlane.Distance(position2);
return nearPlane.Distance(position1) > nearPlane.Distance(position2);
}
NzForwardRenderQueue* queue;
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);
}

View File

@ -76,9 +76,9 @@ void NzForwardRenderTechnique::Clear(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_lights.SetLights(&m_renderQueue.lights[0], m_renderQueue.lights.size());
m_renderQueue.Sort(scene->GetViewer());
if (!m_renderQueue.opaqueModels.empty())
DrawOpaqueModels(scene, m_renderQueue.opaqueModels);
@ -167,6 +167,8 @@ void NzForwardRenderTechnique::DrawOpaqueModels(const NzScene* scene, NzForwardR
NzAbstractViewer* viewer = scene->GetViewer();
const NzShaderProgram* lastProgram = nullptr;
unsigned int lightCount = 0;
for (auto& matIt : opaqueModels)
{
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
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
if (program != lastProgram)
{
@ -404,19 +404,18 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene, NzFor
{
NzAbstractViewer* viewer = scene->GetViewer();
const NzShaderProgram* lastProgram = nullptr;
unsigned int lightCount = 0;
for (const std::pair<unsigned int, bool>& pair : transparentModels)
{
// Matériau
NzMaterial* material = (pair.second) ?
const NzMaterial* material = (pair.second) ?
m_renderQueue.transparentStaticModels[pair.first].material :
m_renderQueue.transparentSkeletalModels[pair.first].material;
// On commence par récupérer le shader du matériau
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
if (program != lastProgram)
{
@ -443,7 +442,7 @@ void NzForwardRenderTechnique::DrawTransparentModels(const NzScene* scene, NzFor
NzForwardRenderQueue::TransparentStaticModel& staticModel = m_renderQueue.transparentStaticModels[pair.first];
const NzMatrix4f& matrix = staticModel.transformMatrix;
NzStaticMesh* mesh = staticModel.mesh;
const NzStaticMesh* mesh = staticModel.mesh;
const NzIndexBuffer* indexBuffer = mesh->GetIndexBuffer();
const NzVertexBuffer* vertexBuffer = mesh->GetVertexBuffer();

View File

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