From 6d953d9e934a0b0a7924d47a87084df538f28244 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 16 Jun 2015 00:31:04 +0200 Subject: [PATCH] Graphics: Separate Renderable and make Light a Renderable (LightComponent) Former-commit-id: 6177d473f27ef493ba77417fc14461cb08b6f9e1 --- .../NDK/Components/GraphicsComponent.hpp | 12 +-- .../NDK/Components/GraphicsComponent.inl | 4 +- SDK/include/NDK/Components/LightComponent.hpp | 30 +++++++ SDK/include/NDK/Components/LightComponent.inl | 11 +++ SDK/include/NDK/Systems/RenderSystem.hpp | 1 + SDK/src/NDK/Components/GraphicsComponent.cpp | 2 +- SDK/src/NDK/Components/LightComponent.cpp | 10 +++ SDK/src/NDK/Sdk.cpp | 2 + SDK/src/NDK/Systems/RenderSystem.cpp | 20 ++++- .../Nazara/Graphics/InstancedRenderable.hpp | 82 +++++++++++++++++++ .../Nazara/Graphics/InstancedRenderable.inl | 39 +++++++++ include/Nazara/Graphics/Light.hpp | 17 +--- include/Nazara/Graphics/Light.inl | 9 -- include/Nazara/Graphics/Model.hpp | 4 +- include/Nazara/Graphics/Renderable.hpp | 49 ++--------- include/Nazara/Graphics/Renderable.inl | 19 ----- include/Nazara/Graphics/TextSprite.hpp | 4 +- src/Nazara/Graphics/InstancedRenderable.cpp | 43 ++++++++++ src/Nazara/Graphics/Light.cpp | 26 +++--- src/Nazara/Graphics/Renderable.cpp | 30 ++----- src/Nazara/Graphics/TextSprite.cpp | 6 +- 21 files changed, 280 insertions(+), 140 deletions(-) create mode 100644 SDK/include/NDK/Components/LightComponent.hpp create mode 100644 SDK/include/NDK/Components/LightComponent.inl create mode 100644 SDK/src/NDK/Components/LightComponent.cpp create mode 100644 include/Nazara/Graphics/InstancedRenderable.hpp create mode 100644 include/Nazara/Graphics/InstancedRenderable.inl create mode 100644 src/Nazara/Graphics/InstancedRenderable.cpp diff --git a/SDK/include/NDK/Components/GraphicsComponent.hpp b/SDK/include/NDK/Components/GraphicsComponent.hpp index 28911778d..41f03ca3c 100644 --- a/SDK/include/NDK/Components/GraphicsComponent.hpp +++ b/SDK/include/NDK/Components/GraphicsComponent.hpp @@ -7,7 +7,7 @@ #ifndef NDK_COMPONENTS_GRAPHICSCOMPONENT_HPP #define NDK_COMPONENTS_GRAPHICSCOMPONENT_HPP -#include +#include #include #include @@ -22,14 +22,14 @@ namespace Ndk inline void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const; - inline void Attach(NzRenderableRef renderable); + inline void Attach(NzInstancedRenderableRef renderable); inline void EnsureTransformMatrixUpdate() const; static ComponentIndex componentIndex; private: - void InvalidateRenderableData(const NzRenderable* renderable, nzUInt32 flags, unsigned int index); + void InvalidateRenderableData(const NzInstancedRenderable* renderable, nzUInt32 flags, unsigned int index); inline void InvalidateTransformMatrix(); void OnAttached() override; @@ -50,10 +50,10 @@ namespace Ndk { } - NazaraSlot(NzRenderable, OnRenderableInvalidateInstanceData, renderableInvalidationSlot); + NazaraSlot(NzInstancedRenderable, OnInstancedRenderableInvalidateData, renderableInvalidationSlot); - mutable NzRenderable::InstanceData data; - NzRenderableRef renderable; + mutable NzInstancedRenderable::InstanceData data; + NzInstancedRenderableRef renderable; mutable bool dataUpdated; }; diff --git a/SDK/include/NDK/Components/GraphicsComponent.inl b/SDK/include/NDK/Components/GraphicsComponent.inl index 88b554855..af435aa2b 100644 --- a/SDK/include/NDK/Components/GraphicsComponent.inl +++ b/SDK/include/NDK/Components/GraphicsComponent.inl @@ -32,12 +32,12 @@ namespace Ndk } } - inline void GraphicsComponent::Attach(NzRenderableRef renderable) + inline void GraphicsComponent::Attach(NzInstancedRenderableRef renderable) { m_renderables.emplace_back(m_transformMatrix); Renderable& r = m_renderables.back(); r.renderable = std::move(renderable); - r.renderableInvalidationSlot.Connect(r.renderable->OnRenderableInvalidateInstanceData, std::bind(&GraphicsComponent::InvalidateRenderableData, this, std::placeholders::_1, std::placeholders::_2, m_renderables.size()-1)); + r.renderableInvalidationSlot.Connect(r.renderable->OnInstancedRenderableInvalidateData, std::bind(&GraphicsComponent::InvalidateRenderableData, this, std::placeholders::_1, std::placeholders::_2, m_renderables.size()-1)); } inline void GraphicsComponent::EnsureTransformMatrixUpdate() const diff --git a/SDK/include/NDK/Components/LightComponent.hpp b/SDK/include/NDK/Components/LightComponent.hpp new file mode 100644 index 000000000..0839ac98e --- /dev/null +++ b/SDK/include/NDK/Components/LightComponent.hpp @@ -0,0 +1,30 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#pragma once + +#ifndef NDK_COMPONENTS_LIGHTCOMPONENT_HPP +#define NDK_COMPONENTS_LIGHTCOMPONENT_HPP + +#include +#include + +namespace Ndk +{ + class NDK_API LightComponent : public Component, public NzLight + { + public: + inline LightComponent(nzLightType lightType = nzLightType_Point); + LightComponent(const LightComponent& light) = default; + ~LightComponent() = default; + + LightComponent& operator=(const LightComponent& light) = default; + + static ComponentIndex componentIndex; + }; +} + +#include + +#endif // NDK_COMPONENTS_LIGHTCOMPONENT_HPP diff --git a/SDK/include/NDK/Components/LightComponent.inl b/SDK/include/NDK/Components/LightComponent.inl new file mode 100644 index 000000000..3a3d0a794 --- /dev/null +++ b/SDK/include/NDK/Components/LightComponent.inl @@ -0,0 +1,11 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +namespace Ndk +{ + inline LightComponent::LightComponent(nzLightType lightType) : + NzLight(lightType) + { + } +} \ No newline at end of file diff --git a/SDK/include/NDK/Systems/RenderSystem.hpp b/SDK/include/NDK/Systems/RenderSystem.hpp index 47af05039..522f1c775 100644 --- a/SDK/include/NDK/Systems/RenderSystem.hpp +++ b/SDK/include/NDK/Systems/RenderSystem.hpp @@ -34,6 +34,7 @@ namespace Ndk EntityList m_cameras; EntityList m_drawables; + EntityList m_lights; NzForwardRenderTechnique m_renderTechnique; }; } diff --git a/SDK/src/NDK/Components/GraphicsComponent.cpp b/SDK/src/NDK/Components/GraphicsComponent.cpp index 83132b1dc..13d44577b 100644 --- a/SDK/src/NDK/Components/GraphicsComponent.cpp +++ b/SDK/src/NDK/Components/GraphicsComponent.cpp @@ -7,7 +7,7 @@ namespace Ndk { - void GraphicsComponent::InvalidateRenderableData(const NzRenderable* renderable, nzUInt32 flags, unsigned int index) + void GraphicsComponent::InvalidateRenderableData(const NzInstancedRenderable* renderable, nzUInt32 flags, unsigned int index) { NazaraUnused(renderable); diff --git a/SDK/src/NDK/Components/LightComponent.cpp b/SDK/src/NDK/Components/LightComponent.cpp new file mode 100644 index 000000000..d49d5f8f9 --- /dev/null +++ b/SDK/src/NDK/Components/LightComponent.cpp @@ -0,0 +1,10 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include + +namespace Ndk +{ + ComponentIndex LightComponent::componentIndex; +} diff --git a/SDK/src/NDK/Sdk.cpp b/SDK/src/NDK/Sdk.cpp index 71ee8cca5..b2ed7bad9 100644 --- a/SDK/src/NDK/Sdk.cpp +++ b/SDK/src/NDK/Sdk.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -57,6 +58,7 @@ namespace Ndk // Composants InitializeComponent("NdkCam"); InitializeComponent("NdkColli"); + InitializeComponent("NdkLight"); InitializeComponent("NdkList"); InitializeComponent("NdkGfx"); InitializeComponent("NdkNode"); diff --git a/SDK/src/NDK/Systems/RenderSystem.cpp b/SDK/src/NDK/Systems/RenderSystem.cpp index 3390edf0f..1f781e951 100644 --- a/SDK/src/NDK/Systems/RenderSystem.cpp +++ b/SDK/src/NDK/Systems/RenderSystem.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace Ndk @@ -24,14 +25,22 @@ namespace Ndk NzAbstractRenderQueue* renderQueue = m_renderTechnique.GetRenderQueue(); renderQueue->Clear(); - for (const Ndk::EntityHandle& drawable : m_drawables) + for (const Ndk::EntityHandle& light : m_drawables) { - GraphicsComponent& graphicsComponent = drawable->GetComponent(); - NodeComponent& drawableNode = drawable->GetComponent(); + GraphicsComponent& graphicsComponent = light->GetComponent(); + NodeComponent& drawableNode = light->GetComponent(); graphicsComponent.AddToRenderQueue(renderQueue); } + for (const Ndk::EntityHandle& light : m_lights) + { + LightComponent& lightComponent = light->GetComponent(); + NodeComponent& drawableNode = light->GetComponent(); + + lightComponent.AddToRenderQueue(renderQueue, drawableNode.GetTransformMatrix()); + } + NzColorBackground background; NzSceneData sceneData; @@ -47,6 +56,7 @@ namespace Ndk { m_cameras.Remove(entity); m_drawables.Remove(entity); + m_lights.Remove(entity); } void RenderSystem::OnEntityValidation(Entity* entity, bool justAdded) @@ -67,6 +77,10 @@ namespace Ndk else m_drawables.Remove(entity); + if (entity->HasComponent() && entity->HasComponent()) + m_lights.Insert(entity); + else + m_lights.Remove(entity); } SystemIndex RenderSystem::systemIndex; diff --git a/include/Nazara/Graphics/InstancedRenderable.hpp b/include/Nazara/Graphics/InstancedRenderable.hpp new file mode 100644 index 000000000..2851fd3bb --- /dev/null +++ b/include/Nazara/Graphics/InstancedRenderable.hpp @@ -0,0 +1,82 @@ +// 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 + +#pragma once + +#ifndef NAZARA_INSTANCEDRENDERABLE_HPP +#define NAZARA_INSTANCEDRENDERABLE_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class NzAbstractRenderQueue; +class NzInstancedRenderable; + +using NzInstancedRenderableConstRef = NzObjectRef; +using NzInstancedRenderableLibrary = NzObjectLibrary; +using NzInstancedRenderableRef = NzObjectRef; + +class NAZARA_GRAPHICS_API NzInstancedRenderable : public NzRefCounted +{ + public: + struct InstanceData; + + NzInstancedRenderable() = default; + inline NzInstancedRenderable(const NzInstancedRenderable& renderable); + virtual ~NzInstancedRenderable(); + + inline void EnsureBoundingVolumeUpdated() const; + + virtual void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const = 0; + virtual bool Cull(const NzFrustumf& frustum, const InstanceData& instanceData) const; + virtual const NzBoundingVolumef& GetBoundingVolume() const; + virtual void InvalidateData(InstanceData* instanceData, nzUInt32 flags) const; + virtual void UpdateBoundingVolume(InstanceData* instanceData) const; + virtual void UpdateData(InstanceData* instanceData) const; + + inline NzInstancedRenderable& operator=(const NzInstancedRenderable& renderable); + + // Signals: + NazaraSignal(OnInstancedRenderableInvalidateData, const NzInstancedRenderable*, nzUInt32); //< Args: me, flags + NazaraSignal(OnInstancedRenderableRelease, const NzInstancedRenderable*); //< Args: me + + struct InstanceData + { + InstanceData(NzMatrix4f& referenceMatrix) : + transformMatrix(referenceMatrix), + flags(0) + { + } + + std::vector data; + NzBoundingVolumef volume; + NzMatrix4f& transformMatrix; + nzUInt32 flags; + }; + + protected: + virtual void MakeBoundingVolume() const = 0; + void InvalidateBoundingVolume(); + inline void InvalidateInstanceData(nzUInt32 flags); + inline void UpdateBoundingVolume() const; + + mutable NzBoundingVolumef m_boundingVolume; + + private: + mutable bool m_boundingVolumeUpdated; + + static NzInstancedRenderableLibrary::LibraryMap s_library; +}; + +#include + +#endif // NAZARA_INSTANCEDRENDERABLE_HPP diff --git a/include/Nazara/Graphics/InstancedRenderable.inl b/include/Nazara/Graphics/InstancedRenderable.inl new file mode 100644 index 000000000..be49fa49c --- /dev/null +++ b/include/Nazara/Graphics/InstancedRenderable.inl @@ -0,0 +1,39 @@ +// 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 + +inline NzInstancedRenderable::NzInstancedRenderable(const NzInstancedRenderable& renderable) : +m_boundingVolume(renderable.m_boundingVolume), +m_boundingVolumeUpdated(renderable.m_boundingVolumeUpdated) +{ +} + +inline void NzInstancedRenderable::EnsureBoundingVolumeUpdated() const +{ + if (!m_boundingVolumeUpdated) + UpdateBoundingVolume(); +} + +inline void NzInstancedRenderable::InvalidateBoundingVolume() +{ + m_boundingVolumeUpdated = false; +} + +inline void NzInstancedRenderable::InvalidateInstanceData(nzUInt32 flags) +{ + OnInstancedRenderableInvalidateData(this, flags); +} + +inline NzInstancedRenderable& NzInstancedRenderable::operator=(const NzInstancedRenderable& renderable) +{ + m_boundingVolume = renderable.m_boundingVolume; + m_boundingVolumeUpdated = renderable.m_boundingVolumeUpdated; + + return *this; +} + +inline void NzInstancedRenderable::UpdateBoundingVolume() const +{ + MakeBoundingVolume(); + m_boundingVolumeUpdated = true; +} diff --git a/include/Nazara/Graphics/Light.hpp b/include/Nazara/Graphics/Light.hpp index 49823f386..9a6a4235b 100644 --- a/include/Nazara/Graphics/Light.hpp +++ b/include/Nazara/Graphics/Light.hpp @@ -9,19 +9,12 @@ #include #include -#include -#include #include #include class NzLight; -class NzShader; struct NzLightUniforms; -using NzLightConstRef = NzObjectRef; -using NzLightLibrary = NzObjectLibrary; -using NzLightRef = NzObjectRef; - class NAZARA_GRAPHICS_API NzLight : public NzRenderable { public: @@ -29,12 +22,12 @@ class NAZARA_GRAPHICS_API NzLight : public NzRenderable NzLight(const NzLight& light) = default; ~NzLight() = default; - void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const override; + void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const NzMatrix4f& transformMatrix) const override; NzLight* Clone() const; NzLight* Create() const; - bool Cull(const NzFrustumf& frustum, const InstanceData& instanceData) const override; + bool Cull(const NzFrustumf& frustum, const NzMatrix4f& transformMatrix) const override; float GetAmbientFactor() const; float GetAttenuation() const; @@ -58,12 +51,10 @@ class NAZARA_GRAPHICS_API NzLight : public NzRenderable void SetOuterAngle(float outerAngle); void SetRadius(float radius); - void UpdateBoundingVolume(InstanceData* instanceData) const; + void UpdateBoundingVolume(const NzMatrix4f& transformMatrix) override; NzLight& operator=(const NzLight& light) = default; - template static NzLightRef New(Args&&... args); - private: void MakeBoundingVolume() const override; @@ -79,8 +70,6 @@ class NAZARA_GRAPHICS_API NzLight : public NzRenderable float m_outerAngleCosine; float m_outerAngleTangent; float m_radius; - - static NzLightLibrary::LibraryMap s_library; }; struct NzLightUniforms diff --git a/include/Nazara/Graphics/Light.inl b/include/Nazara/Graphics/Light.inl index e82a5f2fd..ecda520a4 100644 --- a/include/Nazara/Graphics/Light.inl +++ b/include/Nazara/Graphics/Light.inl @@ -94,13 +94,4 @@ inline void NzLight::SetRadius(float radius) InvalidateBoundingVolume(); } -template -NzLightRef NzLight::New(Args&&... args) -{ - std::unique_ptr object(new NzLight(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); -} - #include diff --git a/include/Nazara/Graphics/Model.hpp b/include/Nazara/Graphics/Model.hpp index 21a3c3e13..b794d098b 100644 --- a/include/Nazara/Graphics/Model.hpp +++ b/include/Nazara/Graphics/Model.hpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include struct NAZARA_GRAPHICS_API NzModelParameters @@ -31,7 +31,7 @@ using NzModelConstRef = NzObjectRef; using NzModelLoader = NzResourceLoader; using NzModelRef = NzObjectRef; -class NAZARA_GRAPHICS_API NzModel : public NzRenderable, public NzResource +class NAZARA_GRAPHICS_API NzModel : public NzInstancedRenderable, public NzResource { friend NzModelLoader; friend class NzScene; diff --git a/include/Nazara/Graphics/Renderable.hpp b/include/Nazara/Graphics/Renderable.hpp index 6c7280d3f..19859c012 100644 --- a/include/Nazara/Graphics/Renderable.hpp +++ b/include/Nazara/Graphics/Renderable.hpp @@ -9,9 +9,6 @@ #include #include -#include -#include -#include #include #include #include @@ -19,62 +16,32 @@ #include class NzAbstractRenderQueue; -class NzRenderable; -using NzRenderableConstRef = NzObjectRef; -using NzRenderableLibrary = NzObjectLibrary; -using NzRenderableRef = NzObjectRef; - -class NAZARA_GRAPHICS_API NzRenderable : public NzRefCounted +class NAZARA_GRAPHICS_API NzRenderable { public: - struct InstanceData; - NzRenderable() = default; - inline NzRenderable(const NzRenderable& renderable); + NzRenderable(const NzRenderable& renderable) = default; virtual ~NzRenderable(); - inline void EnsureBoundingVolumeUpdated() const; + void EnsureBoundingVolumeUpdated() const; - virtual void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const = 0; - virtual bool Cull(const NzFrustumf& frustum, const InstanceData& instanceData) const; + virtual void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const NzMatrix4f& transformMatrix) const = 0; + virtual bool Cull(const NzFrustumf& frustum, const NzMatrix4f& transformMatrix) const = 0; virtual const NzBoundingVolumef& GetBoundingVolume() const; - virtual void InvalidateData(InstanceData* instanceData, nzUInt32 flags) const; - virtual void UpdateBoundingVolume(InstanceData* instanceData) const; - virtual void UpdateData(InstanceData* instanceData) const; + virtual void UpdateBoundingVolume(const NzMatrix4f& transformMatrix); - inline NzRenderable& operator=(const NzRenderable& renderable); - - // Signals: - NazaraSignal(OnRenderableInvalidateInstanceData, const NzRenderable*, nzUInt32); //< Args: me, flags - NazaraSignal(OnRenderableRelease, const NzRenderable*); //< Args: me - - struct InstanceData - { - InstanceData(NzMatrix4f& referenceMatrix) : - transformMatrix(referenceMatrix), - flags(0) - { - } - - std::vector data; - NzBoundingVolumef volume; - NzMatrix4f& transformMatrix; - nzUInt32 flags; - }; + NzRenderable& operator=(const NzRenderable& renderable) = default; protected: virtual void MakeBoundingVolume() const = 0; void InvalidateBoundingVolume(); - inline void InvalidateInstanceData(nzUInt32 flags); - inline void UpdateBoundingVolume() const; + void UpdateBoundingVolume() const; mutable NzBoundingVolumef m_boundingVolume; private: mutable bool m_boundingVolumeUpdated; - - static NzRenderableLibrary::LibraryMap s_library; }; #include diff --git a/include/Nazara/Graphics/Renderable.inl b/include/Nazara/Graphics/Renderable.inl index a3035d23b..2fd3748a9 100644 --- a/include/Nazara/Graphics/Renderable.inl +++ b/include/Nazara/Graphics/Renderable.inl @@ -2,12 +2,6 @@ // This file is part of the "Nazara Engine - Graphics module" // For conditions of distribution and use, see copyright notice in Config.hpp -inline NzRenderable::NzRenderable(const NzRenderable& renderable) : -m_boundingVolume(renderable.m_boundingVolume), -m_boundingVolumeUpdated(renderable.m_boundingVolumeUpdated) -{ -} - inline void NzRenderable::EnsureBoundingVolumeUpdated() const { if (!m_boundingVolumeUpdated) @@ -19,19 +13,6 @@ inline void NzRenderable::InvalidateBoundingVolume() m_boundingVolumeUpdated = false; } -inline void NzRenderable::InvalidateInstanceData(nzUInt32 flags) -{ - OnRenderableInvalidateInstanceData(this, flags); -} - -inline NzRenderable& NzRenderable::operator=(const NzRenderable& renderable) -{ - m_boundingVolume = renderable.m_boundingVolume; - m_boundingVolumeUpdated = renderable.m_boundingVolumeUpdated; - - return *this; -} - inline void NzRenderable::UpdateBoundingVolume() const { MakeBoundingVolume(); diff --git a/include/Nazara/Graphics/TextSprite.hpp b/include/Nazara/Graphics/TextSprite.hpp index fc6a46afb..606ae51c5 100644 --- a/include/Nazara/Graphics/TextSprite.hpp +++ b/include/Nazara/Graphics/TextSprite.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include #include @@ -22,7 +22,7 @@ using NzTextSpriteConstRef = NzObjectRef; using NzTextSpriteLibrary = NzObjectLibrary; using NzTextSpriteRef = NzObjectRef; -class NAZARA_GRAPHICS_API NzTextSprite : public NzRenderable +class NAZARA_GRAPHICS_API NzTextSprite : public NzInstancedRenderable { public: NzTextSprite(); diff --git a/src/Nazara/Graphics/InstancedRenderable.cpp b/src/Nazara/Graphics/InstancedRenderable.cpp new file mode 100644 index 000000000..e408a45f0 --- /dev/null +++ b/src/Nazara/Graphics/InstancedRenderable.cpp @@ -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 +#include + +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; diff --git a/src/Nazara/Graphics/Light.cpp b/src/Nazara/Graphics/Light.cpp index 454b27977..35717498f 100644 --- a/src/Nazara/Graphics/Light.cpp +++ b/src/Nazara/Graphics/Light.cpp @@ -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; diff --git a/src/Nazara/Graphics/Renderable.cpp b/src/Nazara/Graphics/Renderable.cpp index 2d80efb8d..30140949d 100644 --- a/src/Nazara/Graphics/Renderable.cpp +++ b/src/Nazara/Graphics/Renderable.cpp @@ -5,14 +5,13 @@ #include #include -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; diff --git a/src/Nazara/Graphics/TextSprite.cpp b/src/Nazara/Graphics/TextSprite.cpp index 7373d6a8c..609d37dd1 100644 --- a/src/Nazara/Graphics/TextSprite.cpp +++ b/src/Nazara/Graphics/TextSprite.cpp @@ -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();