Graphics/LightComponent: Replace AttachLight with AddLight

This commit is contained in:
SirLynix 2022-11-26 18:32:47 +01:00 committed by Jérôme Leclercq
parent d7eab778fb
commit ec3bc45544
6 changed files with 58 additions and 49 deletions

View File

@ -103,11 +103,11 @@ int main()
framePipeline.RegisterRenderable(worldInstanceIndex1, Nz::FramePipeline::NoSkeletonInstance, &model, 0xFFFFFFFF, scissorBox); framePipeline.RegisterRenderable(worldInstanceIndex1, Nz::FramePipeline::NoSkeletonInstance, &model, 0xFFFFFFFF, scissorBox);
framePipeline.RegisterRenderable(worldInstanceIndex2, Nz::FramePipeline::NoSkeletonInstance, &model, 0xFFFFFFFF, scissorBox); framePipeline.RegisterRenderable(worldInstanceIndex2, Nz::FramePipeline::NoSkeletonInstance, &model, 0xFFFFFFFF, scissorBox);
std::shared_ptr<Nz::SpotLight> light = std::make_shared<Nz::SpotLight>(); std::unique_ptr<Nz::SpotLight> light = std::make_unique<Nz::SpotLight>();
light->UpdateInnerAngle(Nz::DegreeAnglef(15.f)); light->UpdateInnerAngle(Nz::DegreeAnglef(15.f));
light->UpdateOuterAngle(Nz::DegreeAnglef(20.f)); light->UpdateOuterAngle(Nz::DegreeAnglef(20.f));
framePipeline.RegisterLight(light, 0xFFFFFFFF); framePipeline.RegisterLight(light.get(), 0xFFFFFFFF);
Nz::Vector3f viewerPos = Nz::Vector3f::Zero(); Nz::Vector3f viewerPos = Nz::Vector3f::Zero();

View File

@ -92,10 +92,10 @@ int main()
std::size_t worldInstanceIndex1 = framePipeline.RegisterWorldInstance(modelInstance); std::size_t worldInstanceIndex1 = framePipeline.RegisterWorldInstance(modelInstance);
framePipeline.RegisterRenderable(worldInstanceIndex1, Nz::FramePipeline::NoSkeletonInstance, &model, 0xFFFFFFFF, scissorBox); framePipeline.RegisterRenderable(worldInstanceIndex1, Nz::FramePipeline::NoSkeletonInstance, &model, 0xFFFFFFFF, scissorBox);
std::shared_ptr<Nz::DirectionalLight> light = std::make_shared<Nz::DirectionalLight>(); std::unique_ptr<Nz::DirectionalLight> light = std::make_unique<Nz::DirectionalLight>();
light->UpdateRotation(Nz::EulerAnglesf(-45.f, 0.f, 0.f)); light->UpdateRotation(Nz::EulerAnglesf(-45.f, 0.f, 0.f));
framePipeline.RegisterLight(light, 0xFFFFFFFF); framePipeline.RegisterLight(light.get(), 0xFFFFFFFF);
Nz::Vector3f viewerPos = Nz::Vector3f::Zero(); Nz::Vector3f viewerPos = Nz::Vector3f::Zero();

View File

@ -148,12 +148,10 @@ int main()
entt::entity headingEntity = registry.create(); entt::entity headingEntity = registry.create();
{ {
auto spotLight = std::make_shared<Nz::SpotLight>();
spotLight->EnableShadowCasting(true);
spotLight->UpdateShadowMapSize(1024);
auto& entityLight = registry.emplace<Nz::LightComponent>(playerEntity); auto& entityLight = registry.emplace<Nz::LightComponent>(playerEntity);
entityLight.AttachLight(std::move(spotLight), 1); auto& spotLight = entityLight.AddLight<Nz::SpotLight>(1);
spotLight.EnableShadowCasting(true);
spotLight.UpdateShadowMapSize(1024);
auto& entityGfx = registry.emplace<Nz::GraphicsComponent>(playerEntity); auto& entityGfx = registry.emplace<Nz::GraphicsComponent>(playerEntity);
entityGfx.AttachRenderable(model, 1); entityGfx.AttachRenderable(model, 1);

View File

@ -169,14 +169,13 @@ int main()
lightNode.SetPosition(Nz::Vector3f::Up() * 3.f + Nz::Vector3f::Backward() * 1.f); lightNode.SetPosition(Nz::Vector3f::Up() * 3.f + Nz::Vector3f::Backward() * 1.f);
lightNode.SetRotation(Nz::EulerAnglesf(-70.f, 0.f, 0.f)); lightNode.SetRotation(Nz::EulerAnglesf(-70.f, 0.f, 0.f));
auto spotLight = std::make_shared<Nz::SpotLight>();
spotLight->UpdateColor(Nz::Color::Red);
spotLight->UpdateInnerAngle(Nz::DegreeAnglef(15.f));
spotLight->UpdateOuterAngle(Nz::DegreeAnglef(20.f));
spotLight->EnableShadowCasting(true);
auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity1); auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity1);
cameraLight.AttachLight(spotLight, 0xFFFFFFFF);
auto& spotLight = cameraLight.AddLight<Nz::SpotLight>(0xFFFFFFFF);
spotLight.UpdateColor(Nz::Color::Red);
spotLight.UpdateInnerAngle(Nz::DegreeAnglef(15.f));
spotLight.UpdateOuterAngle(Nz::DegreeAnglef(20.f));
spotLight.EnableShadowCasting(true);
} }
entt::entity lightEntity2 = registry.create(); entt::entity lightEntity2 = registry.create();
@ -185,13 +184,12 @@ int main()
lightNode.SetPosition(Nz::Vector3f::Up() * 3.5f + Nz::Vector3f::Right() * 1.5f); lightNode.SetPosition(Nz::Vector3f::Up() * 3.5f + Nz::Vector3f::Right() * 1.5f);
lightNode.SetRotation(Nz::EulerAnglesf(-70.f, 90.f, 0.f)); lightNode.SetRotation(Nz::EulerAnglesf(-70.f, 90.f, 0.f));
auto spotLight = std::make_shared<Nz::SpotLight>();
spotLight->UpdateColor(Nz::Color::Green);
spotLight->EnableShadowCasting(true);
spotLight->UpdateShadowMapSize(1024);
auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity2); auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity2);
cameraLight.AttachLight(spotLight, 0xFFFFFFFF);
auto& spotLight = cameraLight.AddLight<Nz::SpotLight>(0xFFFFFFFF);
spotLight.UpdateColor(Nz::Color::Green);
spotLight.EnableShadowCasting(true);
spotLight.UpdateShadowMapSize(1024);
} }
entt::entity lightEntity3 = registry.create(); entt::entity lightEntity3 = registry.create();
@ -255,14 +253,13 @@ int main()
lightNode.SetRotation(Nz::EulerAnglesf(-45.f, 180.f, 0.f)); lightNode.SetRotation(Nz::EulerAnglesf(-45.f, 180.f, 0.f));
lightNode.SetParentJoint(bobEntity, "Spine2"); lightNode.SetParentJoint(bobEntity, "Spine2");
auto spotLight = std::make_shared<Nz::SpotLight>();
spotLight->UpdateColor(Nz::Color::Blue);
spotLight->UpdateInnerAngle(Nz::DegreeAnglef(15.f));
spotLight->UpdateOuterAngle(Nz::DegreeAnglef(20.f));
spotLight->EnableShadowCasting(true);
auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity3); auto& cameraLight = registry.emplace<Nz::LightComponent>(lightEntity3);
cameraLight.AttachLight(spotLight, 0xFFFFFFFF);
auto& spotLight = cameraLight.AddLight<Nz::SpotLight>(0xFFFFFFFF);
spotLight.UpdateColor(Nz::Color::Blue);
spotLight.UpdateInnerAngle(Nz::DegreeAnglef(15.f));
spotLight.UpdateOuterAngle(Nz::DegreeAnglef(20.f));
spotLight.EnableShadowCasting(true);
} }
} }
@ -522,7 +519,7 @@ int main()
Nz::DebugDrawer& debugDrawer = renderSystem.GetFramePipeline().GetDebugDrawer(); Nz::DebugDrawer& debugDrawer = renderSystem.GetFramePipeline().GetDebugDrawer();
auto& lightNode = registry.get<Nz::NodeComponent>(lightEntity3); auto& lightNode = registry.get<Nz::NodeComponent>(lightEntity3);
debugDrawer.DrawLine(lightNode.GetPosition(Nz::CoordSys::Global), lightNode.GetForward() * 10.f, Nz::Color::Blue); //debugDrawer.DrawLine(lightNode.GetPosition(Nz::CoordSys::Global), lightNode.GetForward() * 10.f, Nz::Color::Blue);
/*Nz::Boxf test = spotLight->GetBoundingVolume().aabb; /*Nz::Boxf test = spotLight->GetBoundingVolume().aabb;
debugDrawer.DrawBox(test, Nz::Color::Blue); debugDrawer.DrawBox(test, Nz::Color::Blue);
debugDrawer.DrawBox(floorBox, Nz::Color::Red); debugDrawer.DrawBox(floorBox, Nz::Color::Red);

View File

@ -28,12 +28,10 @@ namespace Nz
LightComponent(LightComponent&&) = default; LightComponent(LightComponent&&) = default;
~LightComponent() = default; ~LightComponent() = default;
inline void AttachLight(std::shared_ptr<Light> renderable, UInt32 renderMask); template<typename T, typename... Args> T& AddLight(UInt32 renderMask, Args&&... args);
inline void Clear(); inline void Clear();
inline void DetachLight(const std::shared_ptr<Light>& renderable);
inline const LightEntry& GetLightEntry(std::size_t lightIndex) const; inline const LightEntry& GetLightEntry(std::size_t lightIndex) const;
inline const std::array<LightEntry, MaxLightCount>& GetLights() const; inline const std::array<LightEntry, MaxLightCount>& GetLights() const;
@ -41,6 +39,9 @@ namespace Nz
inline bool IsVisible() const; inline bool IsVisible() const;
inline void RemoveLight(std::size_t lightIndex);
inline void RemoveLight(const Light& renderable);
inline void Show(bool show = true); inline void Show(bool show = true);
LightComponent& operator=(const LightComponent&) = default; LightComponent& operator=(const LightComponent&) = default;
@ -52,7 +53,7 @@ namespace Nz
struct LightEntry struct LightEntry
{ {
std::shared_ptr<Light> light; std::unique_ptr<Light> light;
UInt32 renderMask; UInt32 renderMask;
}; };

View File

@ -3,6 +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/Components/LightComponent.hpp> #include <Nazara/Graphics/Components/LightComponent.hpp>
#include <stdexcept>
#include <Nazara/Graphics/Debug.hpp> #include <Nazara/Graphics/Debug.hpp>
namespace Nz namespace Nz
@ -12,20 +13,25 @@ namespace Nz
{ {
} }
inline void LightComponent::AttachLight(std::shared_ptr<Light> light, UInt32 renderMask) template<typename T, typename... Args>
T& LightComponent::AddLight(UInt32 renderMask, Args&&... args)
{ {
static_assert(std::is_base_of_v<Light, T>, "Type must inherit Light");
for (std::size_t i = 0; i < m_lightEntries.size(); ++i) for (std::size_t i = 0; i < m_lightEntries.size(); ++i)
{ {
auto& entry = m_lightEntries[i]; auto& entry = m_lightEntries[i];
if (entry.light) if (entry.light)
continue; continue;
entry.light = std::move(light); entry.light = std::make_unique<T>(std::forward<Args>(args)...);
entry.renderMask = renderMask; entry.renderMask = renderMask;
OnLightAttached(this, i); OnLightAttached(this, i);
break; return static_cast<T&>(*entry.light);
} }
throw std::runtime_error("too many lights attached to the same entity");
} }
inline void LightComponent::Clear() inline void LightComponent::Clear()
@ -33,7 +39,7 @@ namespace Nz
for (std::size_t i = 0; i < m_lightEntries.size(); ++i) for (std::size_t i = 0; i < m_lightEntries.size(); ++i)
{ {
auto& entry = m_lightEntries[i]; auto& entry = m_lightEntries[i];
if (entry.light) if (!entry.light)
continue; continue;
OnLightDetach(this, i); OnLightDetach(this, i);
@ -41,17 +47,6 @@ namespace Nz
} }
} }
inline void LightComponent::DetachLight(const std::shared_ptr<Light>& light)
{
auto it = std::find_if(m_lightEntries.begin(), m_lightEntries.end(), [&](const auto& lightEntry) { return lightEntry.light == light; });
if (it != m_lightEntries.end())
{
OnLightDetach(this, std::distance(m_lightEntries.begin(), it));
it->light.reset();
}
}
inline auto LightComponent::GetLightEntry(std::size_t lightIndex) const -> const LightEntry& inline auto LightComponent::GetLightEntry(std::size_t lightIndex) const -> const LightEntry&
{ {
assert(lightIndex < m_lightEntries.size()); assert(lightIndex < m_lightEntries.size());
@ -73,6 +68,24 @@ namespace Nz
return m_isVisible; return m_isVisible;
} }
inline void LightComponent::RemoveLight(std::size_t lightIndex)
{
assert(lightIndex < m_lightEntries.size());
auto& entry = m_lightEntries[lightIndex];
if (!entry.light)
return;
OnLightDetach(this, lightIndex);
entry.light.reset();
}
inline void LightComponent::RemoveLight(const Light& light)
{
auto it = std::find_if(m_lightEntries.begin(), m_lightEntries.end(), [&](const auto& lightEntry) { return lightEntry.light.get() == &light; });
if (it != m_lightEntries.end())
RemoveLight(std::distance(m_lightEntries.begin(), it));
}
inline void LightComponent::Show(bool show) inline void LightComponent::Show(bool show)
{ {
if (m_isVisible != show) if (m_isVisible != show)