Add light support (WIP)

This commit is contained in:
Jérôme Leclercq
2022-02-02 12:55:39 +01:00
parent e6951d54a5
commit 8a3a8547dc
44 changed files with 1700 additions and 253 deletions

View File

@@ -36,6 +36,7 @@
#include <Nazara/Graphics/Camera.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/DepthMaterial.hpp>
#include <Nazara/Graphics/DirectionalLight.hpp>
#include <Nazara/Graphics/ElementRenderer.hpp>
#include <Nazara/Graphics/Enums.hpp>
#include <Nazara/Graphics/ForwardFramePipeline.hpp>
@@ -47,6 +48,7 @@
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/GuillotineTextureAtlas.hpp>
#include <Nazara/Graphics/InstancedRenderable.hpp>
#include <Nazara/Graphics/Light.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/MaterialPass.hpp>
#include <Nazara/Graphics/MaterialPassRegistry.hpp>
@@ -54,6 +56,7 @@
#include <Nazara/Graphics/MaterialSettings.hpp>
#include <Nazara/Graphics/Model.hpp>
#include <Nazara/Graphics/PhongLightingMaterial.hpp>
#include <Nazara/Graphics/PointLight.hpp>
#include <Nazara/Graphics/PredefinedShaderStructs.hpp>
#include <Nazara/Graphics/RenderElement.hpp>
#include <Nazara/Graphics/RenderQueue.hpp>
@@ -61,6 +64,7 @@
#include <Nazara/Graphics/RenderSpriteChain.hpp>
#include <Nazara/Graphics/RenderSubmesh.hpp>
#include <Nazara/Graphics/SlicedSprite.hpp>
#include <Nazara/Graphics/SpotLight.hpp>
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Graphics/SpriteChainRenderer.hpp>
#include <Nazara/Graphics/SubmeshRenderer.hpp>

View File

@@ -31,5 +31,6 @@
#include <Nazara/Graphics/Components/CameraComponent.hpp>
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
#include <Nazara/Graphics/Components/LightComponent.hpp>
#endif // NAZARA_GRAPHICS_COMPONENTS_HPP

View File

@@ -0,0 +1,64 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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_GRAPHICS_COMPONENTS_LIGHTCOMPONENT_HPP
#define NAZARA_GRAPHICS_COMPONENTS_LIGHTCOMPONENT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ECS.hpp>
#include <Nazara/Graphics/Light.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <memory>
#include <vector>
namespace Nz
{
class NAZARA_GRAPHICS_API LightComponent
{
public:
struct LightEntry;
inline LightComponent(bool initialyVisible = true);
LightComponent(const LightComponent&) = default;
LightComponent(LightComponent&&) = default;
~LightComponent() = default;
inline void AttachLight(std::shared_ptr<Light> renderable, UInt32 renderMask);
inline void Clear();
inline void DetachLight(const std::shared_ptr<Light>& renderable);
inline const std::vector<LightEntry>& GetLights() const;
inline void Hide();
inline bool IsVisible() const;
inline void Show(bool show = true);
LightComponent& operator=(const LightComponent&) = default;
LightComponent& operator=(LightComponent&&) = default;
NazaraSignal(OnLightAttached, LightComponent* /*graphicsComponent*/, const LightEntry& /*lightEntry*/);
NazaraSignal(OnLightDetach, LightComponent* /*graphicsComponent*/, const LightEntry& /*lightEntry*/);
NazaraSignal(OnVisibilityUpdate, LightComponent* /*graphicsComponent*/, bool /*newVisibilityState*/);
struct LightEntry
{
std::shared_ptr<Light> light;
UInt32 renderMask;
};
private:
std::vector<LightEntry> m_lightEntries;
bool m_isVisible;
};
}
#include <Nazara/Graphics/Components/LightComponent.inl>
#endif // NAZARA_GRAPHICS_COMPONENTS_LIGHTCOMPONENT_HPP

View File

@@ -0,0 +1,68 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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/Components/LightComponent.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline LightComponent::LightComponent(bool initialyVisible) :
m_isVisible(initialyVisible)
{
}
inline void LightComponent::AttachLight(std::shared_ptr<Light> light, UInt32 renderMask)
{
auto& entry = m_lightEntries.emplace_back();
entry.light = std::move(light);
entry.renderMask = renderMask;
OnLightAttached(this, m_lightEntries.back());
}
inline void LightComponent::Clear()
{
for (const auto& lightEntry : m_lightEntries)
OnLightDetach(this, lightEntry);
m_lightEntries.clear();
}
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, *it);
m_lightEntries.erase(it);
}
}
inline auto LightComponent::GetLights() const -> const std::vector<LightEntry>&
{
return m_lightEntries;
}
inline void LightComponent::Hide()
{
return Show(false);
}
inline bool LightComponent::IsVisible() const
{
return m_isVisible;
}
inline void LightComponent::Show(bool show)
{
if (m_isVisible != show)
{
OnVisibilityUpdate(this, show);
m_isVisible = show;
}
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -0,0 +1,61 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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_GRAPHICS_DIRECTIONALLIGHT_HPP
#define NAZARA_GRAPHICS_DIRECTIONALLIGHT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/Light.hpp>
#include <Nazara/Math/Angle.hpp>
#include <memory>
namespace Nz
{
class NAZARA_GRAPHICS_API DirectionalLight : public Light
{
public:
DirectionalLight();
DirectionalLight(const DirectionalLight&) = delete;
DirectionalLight(DirectionalLight&&) noexcept = default;
~DirectionalLight() = default;
float ComputeContributionScore(const BoundingVolumef& boundingVolume) const override;
void FillLightData(void* data) override;
inline float GetAmbientFactor() const;
inline float GetDiffuseFactor() const;
inline Color GetColor() const;
inline const Vector3f& GetDirection() const;
inline const Quaternionf& GetRotation() const;
inline void UpdateAmbientFactor(float factor);
inline void UpdateColor(Color color);
inline void UpdateDiffuseFactor(float factor);
inline void UpdateDirection(const Vector3f& direction);
inline void UpdateRotation(const Quaternionf& rotation);
void UpdateTransform(const Vector3f& position, const Quaternionf& rotation, const Vector3f& scale) override;
DirectionalLight& operator=(const DirectionalLight&) = delete;
DirectionalLight& operator=(DirectionalLight&&) noexcept = default;
private:
inline void UpdateBoundingVolume();
Color m_color;
Quaternionf m_rotation;
Vector3f m_direction;
float m_ambientFactor;
float m_diffuseFactor;
};
}
#include <Nazara/Graphics/DirectionalLight.inl>
#endif // NAZARA_GRAPHICS_DIRECTIONALLIGHT_HPP

View File

@@ -0,0 +1,85 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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/DirectionalLight.hpp>
#include <Nazara/Graphics/DirectionalLight.hpp>
#include <cassert>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline DirectionalLight::DirectionalLight() :
m_color(Color::White),
m_ambientFactor(0.2f),
m_diffuseFactor(1.f)
{
UpdateRotation(Quaternionf::Identity());
}
inline float DirectionalLight::GetAmbientFactor() const
{
return m_ambientFactor;
}
inline Color DirectionalLight::GetColor() const
{
return m_color;
}
inline const Vector3f& DirectionalLight::GetDirection() const
{
return m_direction;
}
inline const Quaternionf& DirectionalLight::GetRotation() const
{
return m_rotation;
}
inline float DirectionalLight::GetDiffuseFactor() const
{
return m_diffuseFactor;
}
inline void DirectionalLight::UpdateAmbientFactor(float factor)
{
m_ambientFactor = factor;
OnLightDataInvalided(this);
}
inline void DirectionalLight::UpdateColor(Color color)
{
m_color = color;
OnLightDataInvalided(this);
}
inline void DirectionalLight::UpdateDiffuseFactor(float factor)
{
m_diffuseFactor = factor;
OnLightDataInvalided(this);
}
inline void DirectionalLight::UpdateDirection(const Vector3f& direction)
{
UpdateRotation(Quaternionf::RotationBetween(Vector3f::Forward(), direction));
}
inline void DirectionalLight::UpdateRotation(const Quaternionf& rotation)
{
m_rotation = rotation;
m_direction = rotation * Vector3f::Forward();
UpdateBoundingVolume();
}
inline void DirectionalLight::UpdateBoundingVolume()
{
Light::UpdateBoundingVolume(BoundingVolumef::Infinite()); //< will trigger OnLightDataInvalided
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -11,6 +11,7 @@
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/Enums.hpp>
#include <Nazara/Renderer/RenderBufferView.hpp>
#include <memory>
#include <optional>
#include <vector>
@@ -18,7 +19,6 @@
namespace Nz
{
class CommandBufferBuilder;
class RenderBuffer;
class RenderElement;
class RenderFrame;
class ViewerInstance;
@@ -33,13 +33,14 @@ namespace Nz
virtual ~ElementRenderer();
virtual std::unique_ptr<ElementRendererData> InstanciateData() = 0;
virtual void Prepare(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, RenderFrame& currentFrame, const RenderStates& renderStates, const Pointer<const RenderElement>* elements, std::size_t elementCount);
virtual void Render(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, CommandBufferBuilder& commandBuffer, const Pointer<const RenderElement>* elements, std::size_t elementCount) = 0;
virtual void Prepare(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, RenderFrame& currentFrame, std::size_t elementCount, const Pointer<const RenderElement>* elements, const RenderStates* renderStates);
virtual void PrepareEnd(RenderFrame& currentFrame, ElementRendererData& rendererData);
virtual void Render(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, CommandBufferBuilder& commandBuffer, std::size_t elementCount, const Pointer<const RenderElement>* elements) = 0;
virtual void Reset(ElementRendererData& rendererData, RenderFrame& currentFrame);
struct RenderStates
{
std::optional<RenderBufferView> lightData;
RenderBufferView lightData;
};
};

View File

@@ -13,11 +13,13 @@
#include <Nazara/Graphics/ElementRenderer.hpp>
#include <Nazara/Graphics/FramePipeline.hpp>
#include <Nazara/Graphics/InstancedRenderable.hpp>
#include <Nazara/Graphics/Light.hpp>
#include <Nazara/Graphics/MaterialPass.hpp>
#include <Nazara/Graphics/RenderElement.hpp>
#include <Nazara/Graphics/RenderQueue.hpp>
#include <Nazara/Graphics/RenderQueueRegistry.hpp>
#include <Nazara/Renderer/ShaderBinding.hpp>
#include <memory>
#include <optional>
#include <unordered_map>
#include <unordered_set>
@@ -25,6 +27,7 @@
namespace Nz
{
class PointLight;
class RenderFrame;
class RenderTarget;
@@ -40,16 +43,20 @@ namespace Nz
void InvalidateWorldInstance(WorldInstance* worldInstance) override;
void RegisterInstancedDrawable(WorldInstancePtr worldInstance, const InstancedRenderable* instancedRenderable, UInt32 renderMask) override;
void RegisterLight(std::shared_ptr<Light> light, UInt32 renderMask) override;
void RegisterViewer(AbstractViewer* viewerInstance, Int32 renderOrder) override;
void Render(RenderFrame& renderFrame) override;
void UnregisterInstancedDrawable(const WorldInstancePtr& worldInstance, const InstancedRenderable* instancedRenderable) override;
void UnregisterLight(Light* light) override;
void UnregisterViewer(AbstractViewer* viewerInstance) override;
ForwardFramePipeline& operator=(const ForwardFramePipeline&) = delete;
ForwardFramePipeline& operator=(ForwardFramePipeline&&) = delete;
static constexpr std::size_t MaxLightCountPerDraw = 3;
private:
BakedFrameGraph BuildFrameGraph();
@@ -59,6 +66,28 @@ namespace Nz
struct ViewerData;
struct LightData
{
std::shared_ptr<Light> light;
UInt32 renderMask;
NazaraSlot(Light, OnLightDataInvalided, onLightInvalidated);
};
using LightKey = std::array<const Light*, MaxLightCountPerDraw>;
struct LightKeyHasher
{
inline std::size_t operator()(const LightKey& lightKey) const;
};
struct LightDataUbo
{
std::shared_ptr<RenderBuffer> renderBuffer;
std::size_t offset = 0;
UploadPool::Allocation* allocation = nullptr;
};
struct MaterialData
{
std::size_t usedCount = 0;
@@ -92,6 +121,8 @@ namespace Nz
std::size_t colorAttachment;
std::size_t depthStencilAttachment;
std::size_t visibilityHash = 0;
std::unordered_map<const RenderElement*, RenderBufferView> lightPerRenderElement;
std::unordered_map<LightKey, RenderBufferView, LightKeyHasher> lightBufferPerLights;
std::vector<std::unique_ptr<RenderElement>> depthPrepassRenderElements;
std::vector<std::unique_ptr<RenderElement>> forwardRenderElements;
std::vector<std::unique_ptr<ElementRendererData>> elementRendererData;
@@ -108,8 +139,8 @@ namespace Nz
std::size_t m_depthPassIndex;
std::size_t m_forwardPassIndex;
std::shared_ptr<RenderBuffer> m_lightDataBuffer;
std::unordered_map<AbstractViewer*, ViewerData> m_viewers;
std::unordered_map<Light*, LightData> m_lights;
std::unordered_map<MaterialPass*, MaterialData> m_materials;
std::unordered_map<WorldInstancePtr, std::unordered_map<const InstancedRenderable*, RenderableData>> m_renderables;
std::unordered_map<const RenderTarget*, RenderTargetData> m_renderTargets;
@@ -118,6 +149,9 @@ namespace Nz
std::unordered_set<WorldInstance*> m_invalidatedWorldInstances;
std::unordered_set<WorldInstancePtr> m_removedWorldInstances;
std::vector<std::unique_ptr<ElementRenderer>> m_elementRenderers;
std::vector<ElementRenderer::RenderStates> m_renderStates;
std::vector<Light*> m_visibleLights;
std::vector<LightDataUbo> m_lightDataBuffers;
std::vector<VisibleRenderable> m_visibleRenderables;
BakedFrameGraph m_bakedFrameGraph;
RenderFrame* m_currentRenderFrame;

View File

@@ -3,10 +3,25 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/ForwardFramePipeline.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline std::size_t ForwardFramePipeline::LightKeyHasher::operator()(const LightKey& lightKey) const
{
std::size_t lightHash = 5;
auto CombineHash = [](std::size_t currentHash, std::size_t newHash)
{
return currentHash * 23 + newHash;
};
std::hash<const Light*> lightPtrHasher;
for (std::size_t i = 0; i < lightKey.size(); ++i)
lightHash = CombineHash(lightHash, lightPtrHasher(lightKey[i]));
return lightHash;
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -15,6 +15,7 @@ namespace Nz
{
class AbstractViewer;
class InstancedRenderable;
class Light;
class RenderFrame;
class NAZARA_GRAPHICS_API FramePipeline
@@ -29,11 +30,13 @@ namespace Nz
virtual void InvalidateWorldInstance(WorldInstance* worldInstance) = 0;
virtual void RegisterInstancedDrawable(WorldInstancePtr worldInstance, const InstancedRenderable* instancedRenderable, UInt32 renderMask) = 0;
virtual void RegisterLight(std::shared_ptr<Light> light, UInt32 renderMask) = 0;
virtual void RegisterViewer(AbstractViewer* viewerInstance, Int32 renderOrder) = 0;
virtual void Render(RenderFrame& renderFrame) = 0;
virtual void UnregisterInstancedDrawable(const WorldInstancePtr& worldInstance, const InstancedRenderable* instancedRenderable) = 0;
virtual void UnregisterLight(Light* light) = 0;
virtual void UnregisterViewer(AbstractViewer* viewerInstance) = 0;
FramePipeline& operator=(const FramePipeline&) = delete;

View File

@@ -0,0 +1,55 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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_GRAPHICS_LIGHT_HPP
#define NAZARA_GRAPHICS_LIGHT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Math/BoundingVolume.hpp>
#include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <memory>
namespace Nz
{
class CommandBufferBuilder;
class RenderBuffer;
class RenderFrame;
class NAZARA_GRAPHICS_API Light
{
public:
inline Light();
Light(const Light&) = delete;
Light(Light&&) noexcept = default;
virtual ~Light();
virtual float ComputeContributionScore(const BoundingVolumef& boundingVolume) const = 0;
virtual void FillLightData(void* data) = 0;
inline const BoundingVolumef& GetBoundingVolume() const;
virtual void UpdateTransform(const Vector3f& position, const Quaternionf& rotation, const Vector3f& scale) = 0;
Light& operator=(const Light&) = delete;
Light& operator=(Light&&) noexcept = default;
NazaraSignal(OnLightDataInvalided, Light* /*emitter*/);
protected:
inline void UpdateBoundingVolume(const BoundingVolumef& boundingVolume);
private:
BoundingVolumef m_boundingVolume;
};
}
#include <Nazara/Graphics/Light.inl>
#endif // NAZARA_GRAPHICS_LIGHT_HPP

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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/Light.hpp>
#include <cassert>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline Light::Light() :
m_boundingVolume(BoundingVolumef::Null())
{
}
inline const BoundingVolumef& Light::GetBoundingVolume() const
{
return m_boundingVolume;
}
inline void Light::UpdateBoundingVolume(const BoundingVolumef& boundingVolume)
{
m_boundingVolume = boundingVolume;
OnLightDataInvalided(this);
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -0,0 +1,61 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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_GRAPHICS_POINTLIGHT_HPP
#define NAZARA_GRAPHICS_POINTLIGHT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/Light.hpp>
#include <memory>
namespace Nz
{
class NAZARA_GRAPHICS_API PointLight : public Light
{
public:
PointLight();
PointLight(const PointLight&) = delete;
PointLight(PointLight&&) noexcept = default;
~PointLight() = default;
float ComputeContributionScore(const BoundingVolumef& boundingVolume) const override;
void FillLightData(void* data) override;
inline float GetAmbientFactor() const;
inline float GetDiffuseFactor() const;
inline Color GetColor() const;
inline const Vector3f& GetPosition() const;
inline float GetRadius() const;
inline void UpdateAmbientFactor(float factor);
inline void UpdateColor(Color color);
inline void UpdateDiffuseFactor(float factor);
inline void UpdatePosition(const Vector3f& position);
inline void UpdateRadius(float radius);
void UpdateTransform(const Vector3f& position, const Quaternionf& rotation, const Vector3f& scale) override;
PointLight& operator=(const PointLight&) = delete;
PointLight& operator=(PointLight&&) noexcept = default;
private:
inline void UpdateBoundingVolume();
Color m_color;
Vector3f m_position;
float m_ambientFactor;
float m_diffuseFactor;
float m_invRadius;
float m_radius;
};
}
#include <Nazara/Graphics/PointLight.inl>
#endif // NAZARA_GRAPHICS_POINTLIGHT_HPP

View File

@@ -0,0 +1,91 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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/PointLight.hpp>
#include <cassert>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline PointLight::PointLight() :
m_color(Color::White),
m_position(Vector3f::Zero()),
m_ambientFactor(0.2f),
m_diffuseFactor(1.f)
{
UpdateRadius(5.f);
}
inline float PointLight::GetAmbientFactor() const
{
return m_ambientFactor;
}
inline Color PointLight::GetColor() const
{
return m_color;
}
inline const Vector3f& PointLight::GetPosition() const
{
return m_position;
}
inline float PointLight::GetDiffuseFactor() const
{
return m_diffuseFactor;
}
inline float PointLight::GetRadius() const
{
return m_radius;
}
inline void PointLight::UpdateAmbientFactor(float factor)
{
m_ambientFactor = factor;
OnLightDataInvalided(this);
}
inline void PointLight::UpdateColor(Color color)
{
m_color = color;
OnLightDataInvalided(this);
}
inline void PointLight::UpdateDiffuseFactor(float factor)
{
m_diffuseFactor = factor;
OnLightDataInvalided(this);
}
inline void PointLight::UpdatePosition(const Vector3f& position)
{
m_position = position;
UpdateBoundingVolume();
}
inline void PointLight::UpdateRadius(float radius)
{
m_radius = radius;
m_invRadius = 1.f / m_radius;
UpdateBoundingVolume();
}
inline void PointLight::UpdateBoundingVolume()
{
Vector3f extent = Vector3f(m_radius, m_radius, m_radius) * Sqrt3<float>;
BoundingVolumef boundingVolume(Boxf(-extent, extent));
boundingVolume.Update(m_position);
Light::UpdateBoundingVolume(boundingVolume); //< will trigger OnLightDataInvalided
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -25,7 +25,7 @@ namespace Nz
inline RenderElement(UInt8 elementType);
virtual ~RenderElement();
virtual UInt64 ComputeSortingScore(const Nz::Frustumf& frustum, const RenderQueueRegistry& registry) const = 0;
virtual UInt64 ComputeSortingScore(const Frustumf& frustum, const RenderQueueRegistry& registry) const = 0;
inline UInt8 GetElementType() const;

View File

@@ -0,0 +1,77 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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_GRAPHICS_SPOTLIGHT_HPP
#define NAZARA_GRAPHICS_SPOTLIGHT_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Color.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/Light.hpp>
#include <Nazara/Math/Angle.hpp>
#include <memory>
namespace Nz
{
class NAZARA_GRAPHICS_API SpotLight : public Light
{
public:
SpotLight();
SpotLight(const SpotLight&) = delete;
SpotLight(SpotLight&&) noexcept = default;
~SpotLight() = default;
float ComputeContributionScore(const BoundingVolumef& boundingVolume) const override;
void FillLightData(void* data) override;
inline float GetAmbientFactor() const;
inline float GetDiffuseFactor() const;
inline Color GetColor() const;
inline const Vector3f& GetDirection() const;
inline RadianAnglef GetInnerAngle() const;
inline RadianAnglef GetOuterAngle() const;
inline const Vector3f& GetPosition() const;
inline const Quaternionf& GetRotation() const;
inline float GetRadius() const;
inline void UpdateAmbientFactor(float factor);
inline void UpdateColor(Color color);
inline void UpdateDiffuseFactor(float factor);
inline void UpdateDirection(const Vector3f& direction);
inline void UpdateInnerAngle(RadianAnglef innerAngle);
inline void UpdateOuterAngle(RadianAnglef outerAngle);
inline void UpdatePosition(const Vector3f& position);
inline void UpdateRadius(float radius);
inline void UpdateRotation(const Quaternionf& rotation);
void UpdateTransform(const Vector3f& position, const Quaternionf& rotation, const Vector3f& scale) override;
SpotLight& operator=(const SpotLight&) = delete;
SpotLight& operator=(SpotLight&&) noexcept = default;
private:
inline void UpdateBoundingVolume();
Color m_color;
Quaternionf m_rotation;
RadianAnglef m_innerAngle;
RadianAnglef m_outerAngle;
Vector3f m_direction;
Vector3f m_position;
float m_ambientFactor;
float m_diffuseFactor;
float m_invRadius;
float m_radius;
float m_innerAngleCos;
float m_outerAngleCos;
float m_outerAngleTan;
};
}
#include <Nazara/Graphics/SpotLight.inl>
#endif // NAZARA_GRAPHICS_SPOTLIGHT_HPP

View File

@@ -0,0 +1,161 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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/SpotLight.hpp>
#include <cassert>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline SpotLight::SpotLight() :
m_color(Color::White),
m_position(Vector3f::Zero()),
m_ambientFactor(0.2f),
m_diffuseFactor(1.f)
{
UpdateInnerAngle(DegreeAnglef(30.f));
UpdateOuterAngle(DegreeAnglef(45.f));
UpdateRadius(5.f);
UpdateRotation(Quaternionf::Identity());
}
inline float SpotLight::GetAmbientFactor() const
{
return m_ambientFactor;
}
inline Color SpotLight::GetColor() const
{
return m_color;
}
inline const Vector3f& SpotLight::GetDirection() const
{
return m_direction;
}
inline RadianAnglef SpotLight::GetInnerAngle() const
{
return m_innerAngle;
}
inline RadianAnglef SpotLight::GetOuterAngle() const
{
return m_outerAngle;
}
inline const Vector3f& SpotLight::GetPosition() const
{
return m_position;
}
inline const Quaternionf& SpotLight::GetRotation() const
{
return m_rotation;
}
inline float SpotLight::GetDiffuseFactor() const
{
return m_diffuseFactor;
}
inline float SpotLight::GetRadius() const
{
return m_radius;
}
inline void SpotLight::UpdateAmbientFactor(float factor)
{
m_ambientFactor = factor;
OnLightDataInvalided(this);
}
inline void SpotLight::UpdateColor(Color color)
{
m_color = color;
OnLightDataInvalided(this);
}
inline void SpotLight::UpdateDiffuseFactor(float factor)
{
m_diffuseFactor = factor;
OnLightDataInvalided(this);
}
inline void SpotLight::UpdateDirection(const Vector3f& direction)
{
UpdateRotation(Quaternionf::RotationBetween(Vector3f::Forward(), direction));
}
inline void SpotLight::UpdateInnerAngle(RadianAnglef innerAngle)
{
m_innerAngle = innerAngle;
m_innerAngleCos = m_innerAngle.GetCos();
UpdateBoundingVolume();
}
inline void SpotLight::UpdateOuterAngle(RadianAnglef outerAngle)
{
m_outerAngle = outerAngle;
m_outerAngleCos = m_outerAngle.GetCos();
m_outerAngleTan = m_outerAngle.GetTan();
UpdateBoundingVolume();
}
inline void SpotLight::UpdatePosition(const Vector3f& position)
{
m_position = position;
UpdateBoundingVolume();
}
inline void SpotLight::UpdateRadius(float radius)
{
m_radius = radius;
m_invRadius = 1.f / m_radius;
UpdateBoundingVolume();
}
inline void SpotLight::UpdateRotation(const Quaternionf& rotation)
{
m_rotation = rotation;
m_direction = rotation * Vector3f::Forward();
UpdateBoundingVolume();
}
inline void SpotLight::UpdateBoundingVolume()
{
// We make a box center in the origin
Boxf box(Vector3f::Zero());
// We compute the other points
Vector3f base(Vector3f::Forward() * m_radius);
// Now we need the radius of the projected circle depending on the distance
// Tangent = Opposite/Adjacent <=> Opposite = Adjacent * Tangent
float radius = m_radius * m_outerAngleTan;
Vector3f lExtend = Vector3f::Left() * radius;
Vector3f uExtend = Vector3f::Up() * radius;
// And we add the four extremities of our pyramid
box.ExtendTo(base + lExtend + uExtend);
box.ExtendTo(base + lExtend - uExtend);
box.ExtendTo(base - lExtend + uExtend);
box.ExtendTo(base - lExtend - uExtend);
BoundingVolumef boundingVolume(box);
boundingVolume.Update(Matrix4f::Transform(m_position, m_rotation));
Light::UpdateBoundingVolume(boundingVolume); //< will trigger OnLightDataInvalided
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@@ -18,44 +18,15 @@
namespace Nz
{
class MaterialPass;
class RenderDevice;
class RenderPipeline;
class RenderSpriteChain;
class ShaderBinding;
class NAZARA_GRAPHICS_API SpriteChainRenderer : public ElementRenderer
{
public:
SpriteChainRenderer(RenderDevice& device, std::size_t maxVertexBufferSize = 32 * 1024);
~SpriteChainRenderer() = default;
std::unique_ptr<ElementRendererData> InstanciateData() override;
void Prepare(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, RenderFrame& currentFrame, const RenderStates& renderStates, const Pointer<const RenderElement>* elements, std::size_t elementCount) override;
void Render(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, CommandBufferBuilder& commandBuffer, const Pointer<const RenderElement>* elements, std::size_t elementCount) override;
void Reset(ElementRendererData& rendererData, RenderFrame& currentFrame) override;
private:
struct BufferCopy
{
RenderBuffer* targetBuffer;
UploadPool::Allocation* allocation;
std::size_t size;
};
struct VertexBufferPool
{
std::vector<std::shared_ptr<RenderBuffer>> vertexBuffers;
};
std::shared_ptr<RenderBuffer> m_indexBuffer;
std::shared_ptr<VertexBufferPool> m_vertexBufferPool;
std::size_t m_maxVertexBufferSize;
std::size_t m_maxVertexCount;
std::vector<BufferCopy> m_pendingCopies;
std::vector<ShaderBinding::Binding> m_bindingCache;
RenderDevice& m_device;
};
class Texture;
class VertexDeclaration;
class WorldInstance;
struct SpriteChainRendererData : public ElementRendererData
{
struct DrawCall
@@ -79,6 +50,62 @@ namespace Nz
std::vector<std::shared_ptr<RenderBuffer>> vertexBuffers;
std::vector<ShaderBindingPtr> shaderBindings;
};
class NAZARA_GRAPHICS_API SpriteChainRenderer final : public ElementRenderer
{
public:
SpriteChainRenderer(RenderDevice& device, std::size_t maxVertexBufferSize = 32 * 1024);
~SpriteChainRenderer() = default;
std::unique_ptr<ElementRendererData> InstanciateData() override;
void Prepare(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, RenderFrame& currentFrame, std::size_t elementCount, const Pointer<const RenderElement>* elements, const RenderStates* renderStates) override;
void PrepareEnd(RenderFrame& currentFrame, ElementRendererData& rendererData) override;
void Render(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, CommandBufferBuilder& commandBuffer, std::size_t elementCount, const Pointer<const RenderElement>* elements) override;
void Reset(ElementRendererData& rendererData, RenderFrame& currentFrame) override;
private:
void Flush();
void FlushDrawCall();
void FlushDrawData();
struct BufferCopy
{
RenderBuffer* targetBuffer;
UploadPool::Allocation* allocation;
std::size_t size;
};
struct PendingData
{
std::size_t firstQuadIndex = 0;
SpriteChainRendererData::DrawCall* currentDrawCall = nullptr;
UploadPool::Allocation* currentAllocation = nullptr;
UInt8* currentAllocationMemPtr = nullptr;
const VertexDeclaration* currentVertexDeclaration = nullptr;
RenderBuffer* currentVertexBuffer = nullptr;
const MaterialPass* currentMaterialPass = nullptr;
const RenderPipeline* currentPipeline = nullptr;
const ShaderBinding* currentShaderBinding = nullptr;
const Texture* currentTextureOverlay = nullptr;
const WorldInstance* currentWorldInstance = nullptr;
RenderBufferView currentLightData;
Recti currentScissorBox = Recti(-1, -1, -1, -1);
};
struct VertexBufferPool
{
std::vector<std::shared_ptr<RenderBuffer>> vertexBuffers;
};
std::shared_ptr<RenderBuffer> m_indexBuffer;
std::shared_ptr<VertexBufferPool> m_vertexBufferPool;
std::size_t m_maxVertexBufferSize;
std::size_t m_maxVertexCount;
std::vector<BufferCopy> m_pendingCopies;
std::vector<ShaderBinding::Binding> m_bindingCache;
PendingData m_pendingData;
RenderDevice& m_device;
};
}
#include <Nazara/Graphics/SpriteChainRenderer.inl>

View File

@@ -15,6 +15,7 @@
namespace Nz
{
class RenderPipeline;
class RenderSubmesh;
class ShaderBinding;
class NAZARA_GRAPHICS_API SubmeshRenderer : public ElementRenderer
@@ -24,8 +25,8 @@ namespace Nz
~SubmeshRenderer() = default;
std::unique_ptr<ElementRendererData> InstanciateData() override;
void Prepare(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, RenderFrame& currentFrame, const RenderStates& renderStates, const Pointer<const RenderElement>* elements, std::size_t elementCount) override;
void Render(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, CommandBufferBuilder& commandBuffer, const Pointer<const RenderElement>* elements, std::size_t elementCount) override;
void Prepare(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, RenderFrame& currentFrame, std::size_t elementCount, const Pointer<const RenderElement>* elements, const RenderStates* renderStates) override;
void Render(const ViewerInstance& viewerInstance, ElementRendererData& rendererData, CommandBufferBuilder& commandBuffer, std::size_t elementCount, const Pointer<const RenderElement>* elements) override;
void Reset(ElementRendererData& rendererData, RenderFrame& currentFrame) override;
private:
@@ -40,10 +41,18 @@ namespace Nz
const RenderBuffer* vertexBuffer;
const RenderPipeline* renderPipeline;
const ShaderBinding* shaderBinding;
std::size_t firstIndex;
std::size_t indexCount;
Recti scissorBox;
};
struct DrawCallIndices
{
std::size_t start;
std::size_t count;
};
std::unordered_map<const RenderSubmesh*, DrawCallIndices> drawCallPerElement;
std::vector<DrawCall> drawCalls;
std::vector<ShaderBindingPtr> shaderBindings;
};

View File

@@ -11,6 +11,7 @@
#include <Nazara/Core/ECS.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
#include <Nazara/Graphics/Components/LightComponent.hpp>
#include <Nazara/Utility/Node.hpp>
#include <memory>
#include <set>
@@ -39,6 +40,7 @@ namespace Nz
private:
void OnCameraDestroy(entt::registry& registry, entt::entity entity);
void OnGraphicsDestroy(entt::registry& registry, entt::entity entity);
void OnLightDestroy(entt::registry& registry, entt::entity entity);
void OnNodeDestroy(entt::registry& registry, entt::entity entity);
void UpdateInstances(entt::registry& registry);
void UpdateVisibility(entt::registry& registry);
@@ -56,18 +58,32 @@ namespace Nz
NazaraSlot(Node, OnNodeInvalidation, onNodeInvalidation);
};
struct LightEntity
{
NazaraSlot(LightComponent, OnLightAttached, onLightAttached);
NazaraSlot(LightComponent, OnLightDetach, onLightDetach);
NazaraSlot(LightComponent, OnVisibilityUpdate, onVisibilityUpdate);
NazaraSlot(Node, OnNodeInvalidation, onNodeInvalidation);
};
entt::connection m_cameraDestroyConnection;
entt::connection m_graphicsDestroyConnection;
entt::connection m_lightDestroyConnection;
entt::connection m_nodeDestroyConnection;
entt::observer m_cameraConstructObserver;
entt::observer m_graphicsConstructObserver;
entt::observer m_lightConstructObserver;
std::set<entt::entity> m_invalidatedCameraNode;
std::set<entt::entity> m_invalidatedWorldNode;
std::set<entt::entity> m_invalidatedGfxWorldNode;
std::set<entt::entity> m_invalidatedLightWorldNode;
std::unique_ptr<FramePipeline> m_pipeline;
std::unordered_map<entt::entity, CameraEntity> m_cameraEntities;
std::unordered_map<entt::entity, GraphicsEntity> m_graphicsEntities;
std::unordered_set<entt::entity> m_newlyHiddenEntities;
std::unordered_set<entt::entity> m_newlyVisibleEntities;
std::unordered_map<entt::entity, LightEntity> m_lightEntities;
std::unordered_set<entt::entity> m_newlyHiddenGfxEntities;
std::unordered_set<entt::entity> m_newlyVisibleGfxEntities;
std::unordered_set<entt::entity> m_newlyHiddenLightEntities;
std::unordered_set<entt::entity> m_newlyVisibleLightEntities;
};
}

View File

@@ -34,6 +34,8 @@ namespace Nz
BoundingVolume& ExtendTo(const BoundingVolume& volume);
bool Intersect(const Box<T>& box) const;
bool IsFinite() const;
bool IsInfinite() const;
bool IsNull() const;

View File

@@ -169,11 +169,28 @@ namespace Nz
return *this;
}
template<typename T>
bool BoundingVolume<T>::Intersect(const Box<T>& box) const
{
switch (extend)
{
case Extend::Infinite:
return true;
case Extend::Finite:
return aabb.Intersect(box);
case Extend::Null:
return false;
}
return false;
}
/*!
* \brief Checks whether the volume is finite
* \return true if extend is Extend::Finite
*/
template<typename T>
bool BoundingVolume<T>::IsFinite() const
{

View File

@@ -459,17 +459,12 @@ namespace Nz
{
T left = std::max(x, box.x);
T right = std::min(x + width, box.x + box.width);
if (left >= right)
return false;
T top = std::max(y, box.y);
T bottom = std::min(y + height, box.y + box.height);
if (top >= bottom)
return false;
T up = std::max(z, box.z);
T down = std::min(z + depth, box.z + box.depth);
if (up >= down)
if (left >= right || top >= bottom || up >= down)
return false;
if (intersection)

View File

@@ -16,6 +16,7 @@ namespace Nz
class RenderBufferView
{
public:
inline RenderBufferView();
inline RenderBufferView(RenderBuffer* buffer);
inline RenderBufferView(RenderBuffer* buffer, UInt64 offset, UInt64 size);
RenderBufferView(const RenderBufferView&) = default;
@@ -26,6 +27,11 @@ namespace Nz
inline UInt64 GetOffset() const;
inline UInt64 GetSize() const;
inline explicit operator bool() const;
inline bool operator==(const RenderBufferView& rhs) const;
inline bool operator!=(const RenderBufferView& rhs) const;
RenderBufferView& operator=(const RenderBufferView&) = default;
RenderBufferView& operator=(RenderBufferView&&) = default;

View File

@@ -8,6 +8,13 @@
namespace Nz
{
inline RenderBufferView::RenderBufferView() :
m_offset(0),
m_size(0),
m_buffer(nullptr)
{
}
inline RenderBufferView::RenderBufferView(RenderBuffer* buffer) :
RenderBufferView(buffer, 0, buffer->GetSize())
{
@@ -34,6 +41,21 @@ namespace Nz
{
return m_size;
}
inline RenderBufferView::operator bool() const
{
return m_buffer != nullptr;
}
inline bool RenderBufferView::operator==(const RenderBufferView& rhs) const
{
return m_buffer == rhs.m_buffer && m_offset == rhs.m_offset && m_size == rhs.m_size;
}
inline bool RenderBufferView::operator!=(const RenderBufferView& rhs) const
{
return !operator==(rhs);
}
}
#include <Nazara/Renderer/DebugOff.hpp>

View File

@@ -44,7 +44,7 @@ namespace Nz
NAZARA_UTILITY_API Boxf ComputeAABB(SparsePtr<const Vector3f> positionPtr, std::size_t vertexCount);
NAZARA_UTILITY_API void ComputeBoxIndexVertexCount(const Vector3ui& subdivision, std::size_t* indexCount, std::size_t* vertexCount);
NAZARA_UTILITY_API UInt64 ComputeCacheMissCount(IndexIterator indices, std::size_t indexCount);
NAZARA_UTILITY_API UInt64 ComputeCacheMissCount(IndexIterator indices, UInt32 indexCount);
NAZARA_UTILITY_API void ComputeConeIndexVertexCount(unsigned int subdivision, std::size_t* indexCount, std::size_t* vertexCount);
NAZARA_UTILITY_API void ComputeCubicSphereIndexVertexCount(unsigned int subdivision, std::size_t* indexCount, std::size_t* vertexCount);
NAZARA_UTILITY_API void ComputeIcoSphereIndexVertexCount(unsigned int recursionLevel, std::size_t* indexCount, std::size_t* vertexCount);