From 2a8568f2ac85bfeab17ef01269f7839a53a31e72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sun, 21 Nov 2021 17:14:39 +0100 Subject: [PATCH] Graphics/InstancedRenderable: Replace constructor AABB with UpdateAABB method --- include/Nazara/Graphics/InstancedRenderable.hpp | 6 +++++- include/Nazara/Graphics/InstancedRenderable.inl | 10 ++++++++-- include/Nazara/Graphics/Sprite.inl | 17 ++++++++++++++--- src/Nazara/Graphics/Model.cpp | 3 ++- src/Nazara/Graphics/Sprite.cpp | 1 - src/Nazara/Graphics/TextSprite.cpp | 7 ++----- 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/include/Nazara/Graphics/InstancedRenderable.hpp b/include/Nazara/Graphics/InstancedRenderable.hpp index b3164c3c4..72c7a4022 100644 --- a/include/Nazara/Graphics/InstancedRenderable.hpp +++ b/include/Nazara/Graphics/InstancedRenderable.hpp @@ -23,7 +23,7 @@ namespace Nz class NAZARA_GRAPHICS_API InstancedRenderable { public: - inline InstancedRenderable(const Boxf& aabb); + inline InstancedRenderable(); InstancedRenderable(const InstancedRenderable&) = delete; InstancedRenderable(InstancedRenderable&&) noexcept = default; ~InstancedRenderable(); @@ -37,8 +37,12 @@ namespace Nz InstancedRenderable& operator=(const InstancedRenderable&) = delete; InstancedRenderable& operator=(InstancedRenderable&&) noexcept = default; + NazaraSignal(OnAABBUpdate, InstancedRenderable* /*instancedRenderable*/, const Boxf& /*aabb*/); NazaraSignal(OnMaterialInvalidated, InstancedRenderable* /*instancedRenderable*/, std::size_t /*materialIndex*/, const std::shared_ptr& /*newMaterial*/); + protected: + inline void UpdateAABB(Boxf aabb); + private: Boxf m_aabb; }; diff --git a/include/Nazara/Graphics/InstancedRenderable.inl b/include/Nazara/Graphics/InstancedRenderable.inl index 8a9f5d5f3..c430d8364 100644 --- a/include/Nazara/Graphics/InstancedRenderable.inl +++ b/include/Nazara/Graphics/InstancedRenderable.inl @@ -7,8 +7,8 @@ namespace Nz { - inline InstancedRenderable::InstancedRenderable(const Boxf& aabb) : - m_aabb(aabb) + inline InstancedRenderable::InstancedRenderable() : + m_aabb(Boxf::Zero()) { } @@ -16,6 +16,12 @@ namespace Nz { return m_aabb; } + + inline void InstancedRenderable::UpdateAABB(Boxf aabb) + { + OnAABBUpdate(this, aabb); + m_aabb = aabb; + } } #include diff --git a/include/Nazara/Graphics/Sprite.inl b/include/Nazara/Graphics/Sprite.inl index 136f14166..bfda4f66b 100644 --- a/include/Nazara/Graphics/Sprite.inl +++ b/include/Nazara/Graphics/Sprite.inl @@ -39,25 +39,36 @@ namespace Nz VertexStruct_XYZ_Color_UV* vertices = m_vertices.data(); Vector3f origin = Vector3f::Zero(); + Boxf aabb; vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::LeftTop)]; vertices->position = Vector3f(-origin); vertices->uv = m_textureCoords.GetCorner(RectCorner::LeftTop); - vertices++; + aabb.Set(vertices->position); + + vertices++; vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::RightTop)]; vertices->position = m_size.x * Vector3f::Right() - origin; vertices->uv = m_textureCoords.GetCorner(RectCorner::RightTop); - vertices++; + aabb.ExtendTo(vertices->position); + + vertices++; vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::LeftBottom)]; vertices->position = m_size.y * Vector3f::Down() - origin; vertices->uv = m_textureCoords.GetCorner(RectCorner::LeftBottom); - vertices++; + aabb.ExtendTo(vertices->position); + + vertices++; vertices->color = m_color * m_cornerColor[UnderlyingCast(RectCorner::RightBottom)]; vertices->position = m_size.x * Vector3f::Right() + m_size.y * Vector3f::Down() - origin; vertices->uv = m_textureCoords.GetCorner(RectCorner::RightBottom); + + aabb.ExtendTo(vertices->position); + + UpdateAABB(aabb); } } diff --git a/src/Nazara/Graphics/Model.cpp b/src/Nazara/Graphics/Model.cpp index 8ecc350d9..c33e893cf 100644 --- a/src/Nazara/Graphics/Model.cpp +++ b/src/Nazara/Graphics/Model.cpp @@ -14,7 +14,6 @@ namespace Nz { Model::Model(std::shared_ptr graphicalMesh, const Boxf& aabb) : - InstancedRenderable(aabb), m_graphicalMesh(std::move(graphicalMesh)) { m_submeshes.reserve(m_graphicalMesh->GetSubMeshCount()); @@ -29,6 +28,8 @@ namespace Nz } }; } + + UpdateAABB(aabb); } void Model::BuildElement(std::size_t passIndex, const WorldInstance& worldInstance, std::vector>& elements) const diff --git a/src/Nazara/Graphics/Sprite.cpp b/src/Nazara/Graphics/Sprite.cpp index 4cd8eba20..627e92c21 100644 --- a/src/Nazara/Graphics/Sprite.cpp +++ b/src/Nazara/Graphics/Sprite.cpp @@ -11,7 +11,6 @@ namespace Nz { Sprite::Sprite(std::shared_ptr material) : - InstancedRenderable(Nz::Boxf(-1000.f, -1000.f, -1000.f, 2000.f, 2000.f, 2000.f)), m_material(std::move(material)), m_color(Color::White), m_textureCoords(0.f, 0.f, 1.f, 1.f), diff --git a/src/Nazara/Graphics/TextSprite.cpp b/src/Nazara/Graphics/TextSprite.cpp index c6e14725e..b40194437 100644 --- a/src/Nazara/Graphics/TextSprite.cpp +++ b/src/Nazara/Graphics/TextSprite.cpp @@ -13,7 +13,7 @@ namespace Nz { TextSprite::TextSprite(std::shared_ptr material) : - InstancedRenderable(Nz::Boxf(-10000.f, -10000.f, -10000.f, 20000.f, 20000.f, 20000.f)), + InstancedRenderable(), m_material(std::move(material)) { } @@ -208,10 +208,7 @@ namespace Nz indices->count++; } - /*m_localBounds = drawer.GetBounds(); - - InvalidateBoundingVolume(); - InvalidateInstanceData(0);*/ + UpdateAABB(bounds); clearOnFail.Reset(); }