From 8d370c98605689b9d62601b170c2262d442d9f8f Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Sat, 31 Jul 2021 11:22:33 +0200 Subject: [PATCH] Refactor Drawable --- .../ExampleApps/CubesExampleApp.cpp | 2 + openVulkanoCpp/Scene/Drawable.cpp | 62 +++++++--- openVulkanoCpp/Scene/Drawable.hpp | 109 ++++++++---------- openVulkanoCpp/Scene/Material.hpp | 29 +++-- openVulkanoCpp/Scene/Scene.hpp | 3 + openVulkanoCpp/Vulkan/Renderer.cpp | 2 + .../Vulkan/Scene/VulkanGeometry.hpp | 1 + 7 files changed, 119 insertions(+), 89 deletions(-) diff --git a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp index 4b12874..2327fb2 100644 --- a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp +++ b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp @@ -7,6 +7,8 @@ #include "CubesExampleApp.hpp" #include "Scene/Scene.hpp" #include "Scene/Shader.hpp" +#include "Scene/Geometry.hpp" +#include "Scene/Material.hpp" #include "Scene/Vertex.hpp" #include "Input/InputManager.hpp" #include "Host/GraphicsAppManager.hpp" diff --git a/openVulkanoCpp/Scene/Drawable.cpp b/openVulkanoCpp/Scene/Drawable.cpp index d612942..178f2c2 100644 --- a/openVulkanoCpp/Scene/Drawable.cpp +++ b/openVulkanoCpp/Scene/Drawable.cpp @@ -6,29 +6,55 @@ #include "Drawable.hpp" #include "Scene.hpp" +#include "Base/Utils.hpp" -namespace openVulkanoCpp +namespace openVulkanoCpp::Scene { - namespace Scene + void Drawable::SetScene(Scene* scene) { - void Drawable::SetScene(Scene* scene) - { - if (this->scene == scene) return; - if (scene && this->scene) throw std::runtime_error("Drawable has been associated with a scene already!"); - const auto oldScene = this->scene; - this->scene = scene; - if(scene) scene->RegisterDrawable(this); - else if (oldScene) oldScene->RemoveDrawable(this); - } + if (m_scene == scene) return; + if (scene && m_scene) throw std::runtime_error("Drawable has been associated with a scene already!"); + const auto oldScene = m_scene; + m_scene = scene; + if(scene) scene->RegisterDrawable(this); + else if (oldScene) oldScene->RemoveDrawable(this); + } - void Drawable::RemoveNode(Node* node) + void Drawable::AddNode(Node* node) + { + if (!m_mesh) throw std::runtime_error("Drawable is not initialized."); + if (Utils::Contains(m_nodes, node)) throw std::runtime_error("A drawable must not use the same node more than once."); + m_nodes.push_back(node); + } + + void Drawable::RemoveNode(Node* node) + { + Utils::Remove(m_nodes, node); + if (m_nodes.empty()) { - Utils::Remove(nodes, node); - if (nodes.empty()) - { - scene->RemoveDrawable(this); - scene = nullptr; - } + m_scene->RemoveDrawable(this); + m_scene = nullptr; } } + + void Drawable::Init(Geometry* mesh, Material* material) + { + if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized."); + m_mesh = mesh; + m_material = material; + } + + void Drawable::Init(Drawable* drawable) + { + if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized."); + m_mesh = drawable->m_mesh; + m_material = drawable->m_material; + } + + void Drawable::Close() + { + if (!m_nodes.empty()) throw std::runtime_error("Drawable is still being used!!!"); + m_mesh = nullptr; + m_material = nullptr; + } } \ No newline at end of file diff --git a/openVulkanoCpp/Scene/Drawable.hpp b/openVulkanoCpp/Scene/Drawable.hpp index f9d40c5..ed944aa 100644 --- a/openVulkanoCpp/Scene/Drawable.hpp +++ b/openVulkanoCpp/Scene/Drawable.hpp @@ -1,82 +1,67 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + #pragma once -#include + #include "Base/ICloseable.hpp" -#include "Geometry.hpp" -#include "Material.hpp" +#include -namespace openVulkanoCpp +namespace openVulkanoCpp::Scene { - namespace Scene + class Node; + class Scene; + class Geometry; + class Material; + + class Drawable : virtual public ICloseable { - class Node; - class Scene; + std::vector m_nodes; + Scene* m_scene = nullptr; + Geometry* m_mesh = nullptr; + Material* m_material = nullptr; - struct Drawable : virtual public ICloseable + public: + Drawable() = default; + + explicit Drawable(const Drawable* toCopy) { - std::vector nodes; - Scene* scene = nullptr; - Geometry* mesh = nullptr; - Material* material = nullptr; + m_mesh = toCopy->m_mesh; + m_material = toCopy->m_material; + } - public: - Drawable() = default; + ~Drawable() override + { + if (m_mesh) Drawable::Close(); + } - explicit Drawable(const Drawable* toCopy) - { - mesh = toCopy->mesh; - material = toCopy->material; - } + [[nodiscard]] Drawable* Copy() const + { + return new Drawable(this); + } - virtual ~Drawable() - { - if(mesh) Drawable::Close(); - } + void Init(Geometry* mesh, Material* material); - Drawable* Copy() const - { - return new Drawable(this); - } + void Init(Drawable* drawable); - void Init(Geometry* mesh, Material* material) - { - if (this->mesh || this->material) throw std::runtime_error("Drawable is already initialized."); - this->mesh = mesh; - this->material = material; - } + void Close() override; - void Init(Drawable* drawable) - { - if (mesh || material) throw std::runtime_error("Drawable is already initialized."); - this->mesh = drawable->mesh; - this->material = drawable->material; - } + [[nodiscard]] Scene* GetScene() const { return m_scene; } - void Close() override - { - if (!nodes.empty()) throw std::runtime_error("Drawable is still being used!!!"); - mesh = nullptr; - material = nullptr; - } + [[nodiscard]] Geometry* GetMesh() const { return m_mesh; } - Scene* GetScene() const - { - return scene; - } + [[nodiscard]] const auto& GetNodes() const { return m_nodes; } - private: - friend class Node; - friend class Scene; + private: + friend class Node; + friend class Scene; - void AddNode(Node* node) - { - if (!mesh) throw std::runtime_error("Drawable is not initialized."); - if (Utils::Contains(nodes, node)) throw std::runtime_error("A drawable must not use the same node more than once."); - nodes.push_back(node); - } + void SetScene(Scene* scene); - void SetScene(Scene* scene); + void AddNode(Node* node); - void RemoveNode(Node* node); - }; - } + void RemoveNode(Node* node); + }; } diff --git a/openVulkanoCpp/Scene/Material.hpp b/openVulkanoCpp/Scene/Material.hpp index eef38f6..f24cd9e 100644 --- a/openVulkanoCpp/Scene/Material.hpp +++ b/openVulkanoCpp/Scene/Material.hpp @@ -1,13 +1,24 @@ -#pragma once -#include "Shader.hpp" +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ -namespace openVulkanoCpp +#pragma once + +#include "Shader.hpp" +#include "Texture.hpp" + +namespace openVulkanoCpp::Scene { - namespace Scene + struct MaterialProperties { - struct Material - { - Shader* shader; - }; - } + }; + + struct Material + { + MaterialProperties properties; + Texture* texture; + Shader* shader; + }; } diff --git a/openVulkanoCpp/Scene/Scene.hpp b/openVulkanoCpp/Scene/Scene.hpp index 5863871..7372c38 100644 --- a/openVulkanoCpp/Scene/Scene.hpp +++ b/openVulkanoCpp/Scene/Scene.hpp @@ -9,11 +9,14 @@ #include "Node.hpp" #include "Camera.hpp" #include "Base/Logger.hpp" +#include "Base/Utils.hpp" namespace openVulkanoCpp { namespace Scene { + class Shader; + class Scene : virtual public IInitable, virtual public ICloseable { public: diff --git a/openVulkanoCpp/Vulkan/Renderer.cpp b/openVulkanoCpp/Vulkan/Renderer.cpp index 90697ca..458d0fb 100644 --- a/openVulkanoCpp/Vulkan/Renderer.cpp +++ b/openVulkanoCpp/Vulkan/Renderer.cpp @@ -5,6 +5,8 @@ */ #include "Renderer.hpp" +#include "Scene/Shader.hpp" +#include "Scene/Geometry.hpp" #include "Scene/VulkanGeometry.hpp" #include "Scene/VulkanNode.hpp" #include "Scene/VulkanShader.hpp" diff --git a/openVulkanoCpp/Vulkan/Scene/VulkanGeometry.hpp b/openVulkanoCpp/Vulkan/Scene/VulkanGeometry.hpp index 51920cf..1f430c9 100644 --- a/openVulkanoCpp/Vulkan/Scene/VulkanGeometry.hpp +++ b/openVulkanoCpp/Vulkan/Scene/VulkanGeometry.hpp @@ -8,6 +8,7 @@ #include "IRecordable.hpp" #include "Scene/Scene.hpp" +#include "Scene/Geometry.hpp" namespace openVulkanoCpp::Vulkan {