Graphics: Separate Renderable and make Light a Renderable (LightComponent)

Former-commit-id: 6177d473f27ef493ba77417fc14461cb08b6f9e1
This commit is contained in:
Lynix
2015-06-16 00:31:04 +02:00
parent b3597d5330
commit 6d953d9e93
21 changed files with 280 additions and 140 deletions

View File

@@ -0,0 +1,43 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/InstancedRenderable.hpp>
#include <Nazara/Graphics/Debug.hpp>
NzInstancedRenderable::~NzInstancedRenderable()
{
OnInstancedRenderableRelease(this);
}
bool NzInstancedRenderable::Cull(const NzFrustumf& frustum, const InstanceData& instanceData) const
{
return frustum.Contains(instanceData.volume);
}
const NzBoundingVolumef& NzInstancedRenderable::GetBoundingVolume() const
{
EnsureBoundingVolumeUpdated();
return m_boundingVolume;
}
void NzInstancedRenderable::InvalidateData(InstanceData* instanceData, nzUInt32 flags) const
{
instanceData->flags |= flags;
}
void NzInstancedRenderable::UpdateBoundingVolume(InstanceData* instanceData) const
{
NazaraAssert(instanceData, "Invalid instance data");
NazaraUnused(instanceData);
instanceData->volume.Update(instanceData->transformMatrix);
}
void NzInstancedRenderable::UpdateData(InstanceData* instanceData) const
{
NazaraAssert(instanceData, "Invalid instance data");
}
NzInstancedRenderableLibrary::LibraryMap NzInstancedRenderable::s_library;

View File

@@ -28,7 +28,7 @@ m_type(type)
SetRadius(5.f);
}
void NzLight::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
void NzLight::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const NzMatrix4f& transformMatrix) const
{
switch (m_type)
{
@@ -38,7 +38,7 @@ void NzLight::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const Instanc
light.ambientFactor = m_ambientFactor;
light.color = m_color;
light.diffuseFactor = m_diffuseFactor;
light.direction = instanceData.transformMatrix.Transform(NzVector3f::Forward(), 0.f);
light.direction = transformMatrix.Transform(NzVector3f::Forward(), 0.f);
renderQueue->AddDirectionalLight(light);
break;
@@ -52,7 +52,7 @@ void NzLight::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const Instanc
light.color = m_color;
light.diffuseFactor = m_diffuseFactor;
light.invRadius = m_invRadius;
light.position = instanceData.transformMatrix.GetTranslation();
light.position = transformMatrix.GetTranslation();
light.radius = m_radius;
renderQueue->AddPointLight(light);
@@ -66,12 +66,12 @@ void NzLight::AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const Instanc
light.attenuation = m_attenuation;
light.color = m_color;
light.diffuseFactor = m_diffuseFactor;
light.direction = instanceData.transformMatrix.Transform(NzVector3f::Forward(), 0.f);
light.direction = transformMatrix.Transform(NzVector3f::Forward(), 0.f);
light.innerAngleCosine = m_innerAngleCosine;
light.invRadius = m_invRadius;
light.outerAngleCosine = m_outerAngleCosine;
light.outerAngleTangent = m_outerAngleTangent;
light.position = instanceData.transformMatrix.GetTranslation();
light.position = transformMatrix.GetTranslation();
light.radius = m_radius;
renderQueue->AddSpotLight(light);
@@ -94,7 +94,7 @@ NzLight* NzLight::Create() const
return new NzLight;
}
bool NzLight::Cull(const NzFrustumf& frustum, const InstanceData& instanceData) const
bool NzLight::Cull(const NzFrustumf& frustum, const NzMatrix4f& transformMatrix) const
{
switch (m_type)
{
@@ -102,31 +102,29 @@ bool NzLight::Cull(const NzFrustumf& frustum, const InstanceData& instanceData)
return true; // Always visible
case nzLightType_Point:
return frustum.Contains(NzSpheref(instanceData.transformMatrix.GetTranslation(), m_radius)); // A sphere test is much faster (and precise)
return frustum.Contains(NzSpheref(transformMatrix.GetTranslation(), m_radius)); // A sphere test is much faster (and precise)
case nzLightType_Spot:
return frustum.Contains(instanceData.volume);
return frustum.Contains(m_boundingVolume);
}
NazaraError("Invalid light type (0x" + NzString::Number(m_type, 16) + ')');
return false;
}
void NzLight::UpdateBoundingVolume(InstanceData* instanceData) const
void NzLight::UpdateBoundingVolume(const NzMatrix4f& transformMatrix)
{
NazaraAssert(instanceData, "Invalid data");
switch (m_type)
{
case nzLightType_Directional:
break; // Nothing to do (bounding volume should be infinite)
case nzLightType_Point:
instanceData->volume.Update(instanceData->transformMatrix.GetTranslation()); // The bounding volume only needs to be shifted
m_boundingVolume.Update(transformMatrix.GetTranslation()); // The bounding volume only needs to be shifted
break;
case nzLightType_Spot:
instanceData->volume.Update(instanceData->transformMatrix);
m_boundingVolume.Update(transformMatrix);
break;
default:
@@ -179,5 +177,3 @@ void NzLight::MakeBoundingVolume() const
break;
}
}
NzLightLibrary::LibraryMap NzLight::s_library;

View File

@@ -5,14 +5,13 @@
#include <Nazara/Graphics/Renderable.hpp>
#include <Nazara/Graphics/Debug.hpp>
NzRenderable::~NzRenderable()
{
OnRenderableRelease(this);
}
NzRenderable::~NzRenderable() = default;
bool NzRenderable::Cull(const NzFrustumf& frustum, const InstanceData& instanceData) const
bool NzRenderable::Cull(const NzFrustumf& frustum, const NzMatrix4f& transformMatrix) const
{
return frustum.Contains(instanceData.volume);
NazaraUnused(transformMatrix);
return frustum.Contains(m_boundingVolume);
}
const NzBoundingVolumef& NzRenderable::GetBoundingVolume() const
@@ -22,22 +21,7 @@ const NzBoundingVolumef& NzRenderable::GetBoundingVolume() const
return m_boundingVolume;
}
void NzRenderable::InvalidateData(InstanceData* instanceData, nzUInt32 flags) const
void NzRenderable::UpdateBoundingVolume(const NzMatrix4f& transformMatrix)
{
instanceData->flags |= flags;
m_boundingVolume.Update(transformMatrix);
}
void NzRenderable::UpdateBoundingVolume(InstanceData* instanceData) const
{
NazaraAssert(instanceData, "Invalid instance data");
NazaraUnused(instanceData);
instanceData->volume.Update(instanceData->transformMatrix);
}
void NzRenderable::UpdateData(InstanceData* instanceData) const
{
NazaraAssert(instanceData, "Invalid instance data");
}
NzRenderableLibrary::LibraryMap NzRenderable::s_library;

View File

@@ -19,7 +19,7 @@ m_scale(1.f)
}
NzTextSprite::NzTextSprite(const NzTextSprite& sprite) :
NzRenderable(sprite),
NzInstancedRenderable(sprite),
m_renderInfos(sprite.m_renderInfos),
m_localVertices(sprite.m_localVertices),
m_color(sprite.m_color),
@@ -88,7 +88,7 @@ float NzTextSprite::GetScale() const
void NzTextSprite::InvalidateVertices()
{
OnRenderableInvalidateInstanceData(this, 0);
InvalidateInstanceData(0);
}
bool NzTextSprite::IsDrawable() const
@@ -245,7 +245,7 @@ void NzTextSprite::Update(const NzAbstractTextDrawer& drawer)
NzTextSprite& NzTextSprite::operator=(const NzTextSprite& text)
{
NzRenderable::operator=(text);
NzInstancedRenderable::operator=(text);
m_atlases.clear();