From 55f2937678409d438636eb1685c6aa3e9fdb94fa Mon Sep 17 00:00:00 2001 From: SirLynix Date: Thu, 9 Mar 2023 17:50:38 +0100 Subject: [PATCH] Graphics/GraphicalMesh: Add AABB --- examples/DeferredShading/main.cpp | 4 +-- examples/PhysicallyBasedRendering/main.cpp | 2 +- examples/PhysicsDemo/main.cpp | 4 +-- examples/Showcase/main.cpp | 8 +++--- include/Nazara/Graphics/GraphicalMesh.hpp | 5 ++++ include/Nazara/Graphics/GraphicalMesh.inl | 31 ++++++++++++++++++++++ include/Nazara/Graphics/Model.hpp | 2 +- src/Nazara/Graphics/GraphicalMesh.cpp | 2 ++ src/Nazara/Graphics/Model.cpp | 5 ++-- tests/GraphicsTest/main.cpp | 2 +- 10 files changed, 52 insertions(+), 13 deletions(-) diff --git a/examples/DeferredShading/main.cpp b/examples/DeferredShading/main.cpp index bbd617311..522c9186b 100644 --- a/examples/DeferredShading/main.cpp +++ b/examples/DeferredShading/main.cpp @@ -226,11 +226,11 @@ int main() std::shared_ptr planeMat = deferredMaterial->Instantiate(); planeMat->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "dev_grey.png", texParams), planeSampler); - Nz::Model spaceshipModel(std::move(gfxMesh), spaceship->GetAABB()); + Nz::Model spaceshipModel(std::move(gfxMesh)); for (std::size_t i = 0; i < spaceshipModel.GetSubMeshCount(); ++i) spaceshipModel.SetMaterial(i, spaceshipMat); - Nz::Model planeModel(std::move(planeMeshGfx), planeMesh->GetAABB()); + Nz::Model planeModel(std::move(planeMeshGfx)); for (std::size_t i = 0; i < planeModel.GetSubMeshCount(); ++i) planeModel.SetMaterial(i, planeMat); diff --git a/examples/PhysicallyBasedRendering/main.cpp b/examples/PhysicallyBasedRendering/main.cpp index 4f1cc9b69..e041ea206 100644 --- a/examples/PhysicallyBasedRendering/main.cpp +++ b/examples/PhysicallyBasedRendering/main.cpp @@ -57,7 +57,7 @@ int main() std::size_t normalMapProperty = materialInstance->FindTextureProperty("NormalMap"); materialInstance->SetTextureProperty(normalMapProperty, normalMap); - Nz::Model model(std::move(gfxMesh), sphereMesh->GetAABB()); + Nz::Model model(std::move(gfxMesh)); for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) model.SetMaterial(i, materialInstance); diff --git a/examples/PhysicsDemo/main.cpp b/examples/PhysicsDemo/main.cpp index 8bf45bd42..0a7bf7885 100644 --- a/examples/PhysicsDemo/main.cpp +++ b/examples/PhysicsDemo/main.cpp @@ -72,7 +72,7 @@ int main() material->SetTextureProperty("AlphaMap", Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams)); material->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams)); - std::shared_ptr model = std::make_shared(std::move(gfxMesh), spaceshipAABB); + std::shared_ptr model = std::make_shared(std::move(gfxMesh)); for (std::size_t i = 0; i < model->GetSubMeshCount(); ++i) model->SetMaterial(i, material); @@ -118,7 +118,7 @@ int main() std::shared_ptr colliderMesh = Nz::Mesh::Build(shipCollider->GenerateMesh()); std::shared_ptr colliderGraphicalMesh = Nz::GraphicalMesh::BuildFromMesh(*colliderMesh); - colliderModel = std::make_shared(colliderGraphicalMesh, spaceshipAABB); + colliderModel = std::make_shared(colliderGraphicalMesh); for (std::size_t i = 0; i < colliderModel->GetSubMeshCount(); ++i) colliderModel->SetMaterial(i, colliderMat); } diff --git a/examples/Showcase/main.cpp b/examples/Showcase/main.cpp index c8bc2cd69..cc981cdfc 100644 --- a/examples/Showcase/main.cpp +++ b/examples/Showcase/main.cpp @@ -108,7 +108,7 @@ int main() //std::shared_ptr material = Nz::Graphics::Instance()->GetDefaultMaterials().basicTransparent; - std::shared_ptr bobModel = std::make_shared(std::move(bobGfxMesh), bobAABB); + std::shared_ptr bobModel = std::make_shared(std::move(bobGfxMesh)); std::vector> materials(bobMesh->GetMaterialCount()); std::bitset<5> alphaMaterials("01010"); @@ -212,7 +212,7 @@ int main() std::shared_ptr sphereMat = Nz::Graphics::Instance()->GetDefaultMaterials().phongMaterial->Instantiate(); sphereMat->SetTextureProperty("BaseColorMap", Nz::Texture::LoadFromFile(resourceDir / "Rusty/rustediron2_basecolor.png", srgbTexParams)); - std::shared_ptr sphereModel = std::make_shared(std::move(gfxMesh), sphereMesh->GetAABB()); + std::shared_ptr sphereModel = std::make_shared(std::move(gfxMesh)); for (std::size_t i = 0; i < sphereModel->GetSubMeshCount(); ++i) sphereModel->SetMaterial(i, sphereMat); @@ -318,7 +318,7 @@ int main() floorBox = planeMesh.GetAABB(); - std::shared_ptr planeModel = std::make_shared(std::move(planeMeshGfx), planeMesh.GetAABB()); + std::shared_ptr planeModel = std::make_shared(std::move(planeMeshGfx)); planeModel->SetMaterial(0, planeMat); auto& planeGfx = planeEntity.emplace(); @@ -336,7 +336,7 @@ int main() std::shared_ptr boxMeshGfx = Nz::GraphicalMesh::BuildFromMesh(boxMesh); - std::shared_ptr boxModel = std::make_shared(std::move(boxMeshGfx), boxMesh.GetAABB()); + std::shared_ptr boxModel = std::make_shared(std::move(boxMeshGfx)); boxModel->SetMaterial(0, planeMat); entt::handle boxEntity = world.CreateEntity(); diff --git a/include/Nazara/Graphics/GraphicalMesh.hpp b/include/Nazara/Graphics/GraphicalMesh.hpp index 299e36515..0ab5cc35e 100644 --- a/include/Nazara/Graphics/GraphicalMesh.hpp +++ b/include/Nazara/Graphics/GraphicalMesh.hpp @@ -31,6 +31,7 @@ namespace Nz inline void Clear(); + inline const Boxf& GetAABB() const; inline const std::shared_ptr& GetIndexBuffer(std::size_t subMesh) const; inline UInt32 GetIndexCount(std::size_t subMesh) const; inline IndexType GetIndexType(std::size_t subMesh) const; @@ -38,6 +39,7 @@ namespace Nz inline const std::shared_ptr& GetVertexDeclaration(std::size_t subMesh) const; inline std::size_t GetSubMeshCount() const; + inline void UpdateAABB(const Boxf& aabb); inline void UpdateSubMeshIndexCount(std::size_t subMeshIndex, UInt32 indexCount); GraphicalMesh& operator=(const GraphicalMesh&) = delete; @@ -52,12 +54,15 @@ namespace Nz UInt32 indexCount; }; + static inline std::shared_ptr Build(const Primitive& primitive, const MeshParams& params = MeshParams()); + static inline std::shared_ptr Build(const PrimitiveList& primitiveList, const MeshParams& params = MeshParams()); static std::shared_ptr BuildFromMesh(const Mesh& mesh); NazaraSignal(OnInvalidated, GraphicalMesh* /*gfxMesh*/); private: std::vector m_subMeshes; + Boxf m_aabb; }; } diff --git a/include/Nazara/Graphics/GraphicalMesh.inl b/include/Nazara/Graphics/GraphicalMesh.inl index d2dece531..bc34f5ee7 100644 --- a/include/Nazara/Graphics/GraphicalMesh.inl +++ b/include/Nazara/Graphics/GraphicalMesh.inl @@ -25,6 +25,11 @@ namespace Nz OnInvalidated(this); } + inline const Boxf& GraphicalMesh::GetAABB() const + { + return m_aabb; + } + inline const std::shared_ptr& GraphicalMesh::GetIndexBuffer(std::size_t subMesh) const { assert(subMesh < m_subMeshes.size()); @@ -60,6 +65,13 @@ namespace Nz return m_subMeshes.size(); } + inline void GraphicalMesh::UpdateAABB(const Boxf& aabb) + { + m_aabb = aabb; + + OnInvalidated(this); + } + inline void GraphicalMesh::UpdateSubMeshIndexCount(std::size_t subMeshIndex, UInt32 indexCount) { NazaraAssert(subMeshIndex < m_subMeshes.size(), "invalid submesh index"); @@ -67,6 +79,25 @@ namespace Nz OnInvalidated(this); } + + inline std::shared_ptr GraphicalMesh::Build(const Primitive& primitive, const MeshParams& params) + { + Mesh mesh; + mesh.CreateStatic(); + mesh.BuildSubMesh(primitive, params); + + return BuildFromMesh(mesh); + } + + inline std::shared_ptr GraphicalMesh::Build(const PrimitiveList& primitiveList, const MeshParams& params) + { + Mesh mesh; + mesh.CreateStatic(); + mesh.BuildSubMeshes(primitiveList, params); + + return BuildFromMesh(mesh); + } + } #include diff --git a/include/Nazara/Graphics/Model.hpp b/include/Nazara/Graphics/Model.hpp index 029523f16..89d2f32bc 100644 --- a/include/Nazara/Graphics/Model.hpp +++ b/include/Nazara/Graphics/Model.hpp @@ -23,7 +23,7 @@ namespace Nz class NAZARA_GRAPHICS_API Model : public InstancedRenderable { public: - Model(std::shared_ptr graphicalMesh, const Boxf& aabb); + Model(std::shared_ptr graphicalMesh); Model(const Model&) = delete; Model(Model&&) noexcept = default; ~Model() = default; diff --git a/src/Nazara/Graphics/GraphicalMesh.cpp b/src/Nazara/Graphics/GraphicalMesh.cpp index 12ffceefc..8ecc9b3d3 100644 --- a/src/Nazara/Graphics/GraphicalMesh.cpp +++ b/src/Nazara/Graphics/GraphicalMesh.cpp @@ -54,6 +54,8 @@ namespace Nz gfxMesh->AddSubMesh(std::move(submeshData)); } + gfxMesh->UpdateAABB(mesh.GetAABB()); + return gfxMesh; } } diff --git a/src/Nazara/Graphics/Model.cpp b/src/Nazara/Graphics/Model.cpp index 833cd25c6..4b289dad0 100644 --- a/src/Nazara/Graphics/Model.cpp +++ b/src/Nazara/Graphics/Model.cpp @@ -14,7 +14,7 @@ namespace Nz { - Model::Model(std::shared_ptr graphicalMesh, const Boxf& aabb) : + Model::Model(std::shared_ptr graphicalMesh) : m_graphicalMesh(std::move(graphicalMesh)) { Graphics* graphics = Graphics::Instance(); @@ -34,10 +34,11 @@ namespace Nz m_onInvalidated.Connect(m_graphicalMesh->OnInvalidated, [this](GraphicalMesh*) { + UpdateAABB(m_graphicalMesh->GetAABB()); OnElementInvalidated(this); }); - UpdateAABB(aabb); + UpdateAABB(m_graphicalMesh->GetAABB()); } void Model::BuildElement(ElementRendererRegistry& registry, const ElementData& elementData, std::size_t passIndex, std::vector& elements) const diff --git a/tests/GraphicsTest/main.cpp b/tests/GraphicsTest/main.cpp index aa45695d2..5241e5e3e 100644 --- a/tests/GraphicsTest/main.cpp +++ b/tests/GraphicsTest/main.cpp @@ -72,7 +72,7 @@ int main() std::shared_ptr materialInstance2 = std::make_shared(material); materialInstance2->SetValueProperty(0, Nz::Color::Green()); - Nz::Model model(std::move(gfxMesh), spaceshipMesh->GetAABB()); + Nz::Model model(std::move(gfxMesh)); for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) model.SetMaterial(i, materialInstance);