From 775c49c1975e651d1571fe23d273d2a5b649b522 Mon Sep 17 00:00:00 2001 From: ohyzha Date: Wed, 24 Jul 2024 10:46:25 +0300 Subject: [PATCH 1/4] add functions to work with flags --- openVulkanoCpp/Base/Utils.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/openVulkanoCpp/Base/Utils.hpp b/openVulkanoCpp/Base/Utils.hpp index 391a322..97def0c 100644 --- a/openVulkanoCpp/Base/Utils.hpp +++ b/openVulkanoCpp/Base/Utils.hpp @@ -46,6 +46,24 @@ namespace OpenVulkano return result; } + template + static bool GetFlag(T var, U flag) + { + return var & flag; + } + + template + static void SetFlag(T& var, U flag) + { + var |= flag; + } + + template + static void ResetFlag(T& var, U flag) + { + var &= ~flag; + } + template static inline bool Contains(std::vector& vec, const T& element) { From abf1b729904d8f2f6741f382219c4bfe45cf1821 Mon Sep 17 00:00:00 2001 From: ohyzha Date: Wed, 24 Jul 2024 10:47:52 +0300 Subject: [PATCH 2/4] extend geometry API --- openVulkanoCpp/Scene/Geometry.cpp | 112 +++++++++++++++++- openVulkanoCpp/Scene/Geometry.hpp | 44 ++++--- .../Vulkan/Resources/ResourceManager.cpp | 4 + 3 files changed, 137 insertions(+), 23 deletions(-) diff --git a/openVulkanoCpp/Scene/Geometry.cpp b/openVulkanoCpp/Scene/Geometry.cpp index 8cfd270..c2b28db 100644 --- a/openVulkanoCpp/Scene/Geometry.cpp +++ b/openVulkanoCpp/Scene/Geometry.cpp @@ -19,6 +19,102 @@ namespace OpenVulkano::Scene { + Geometry::Geometry(const Geometry& other) + { + this->vertexCount = other.vertexCount; + this->indexCount = other.indexCount; + this->indexType = other.indexType; + this->aabb = other.aabb; + this->ownsMemory = other.ownsMemory; + this->freeAfterUpload = other.freeAfterUpload; + this->renderGeo = nullptr; + this->vertices = new Vertex[vertexCount]; + if (other.vertices) + { + std::copy(other.vertices, other.vertices + other.vertexCount, this->vertices); + } + this->indices = malloc(static_cast(Utils::EnumAsInt(other.indexType)) * other.indexCount); + if (other.indices) + { + if (other.indexType == VertexIndexType::UINT16) + { + std::copy(static_cast(other.indices), + static_cast(other.indices) + other.indexCount, + static_cast(this->indices)); + } + else + { + std::copy(static_cast(other.indices), + static_cast(other.indices) + other.indexCount, + static_cast(this->indices)); + } + } + } + + Geometry& Geometry::operator=(const Geometry& other) + { + Geometry tmp(other); + this->Swap(tmp); + return *this; + } + + Geometry::Geometry(Geometry&& other) noexcept + { + this->vertexCount = other.vertexCount; + this->indexCount = other.indexCount; + this->indexType = other.indexType; + this->ownsMemory = other.ownsMemory; + this->freeAfterUpload = other.freeAfterUpload; + this->aabb = std::move(other.aabb); + this->vertices = other.vertices; + this->indices = other.indices; + this->renderGeo = other.renderGeo; + other.vertexCount = other.indexCount = 0; + other.vertices = nullptr; + other.indices = nullptr; + other.renderGeo = nullptr; + } + + Geometry& Geometry::operator=(Geometry&& other) noexcept + { + if (this != &other) + { + Close(); + this->vertexCount = other.vertexCount; + this->indexCount = other.indexCount; + this->indexType = other.indexType; + this->ownsMemory = other.ownsMemory; + this->freeAfterUpload = other.freeAfterUpload; + this->aabb = std::move(other.aabb); + this->vertices = other.vertices; + this->indices = other.indices; + this->renderGeo = other.renderGeo; + other.vertexCount = other.indexCount = 0; + other.vertices = nullptr; + other.indices = nullptr; + other.renderGeo = nullptr; + } + return *this; + } + + Geometry::~Geometry() + { + Geometry::Close(); + } + + void Geometry::Swap(Geometry& other) noexcept + { + std::swap(this->vertexCount, other.vertexCount); + std::swap(this->indexCount, other.indexCount); + std::swap(this->aabb, other.aabb); + std::swap(this->indexType, other.indexType); + std::swap(this->vertices, other.vertices); + std::swap(this->indices, other.indices); + std::swap(this->renderGeo, other.renderGeo); + std::swap(this->ownsMemory, other.ownsMemory); + std::swap(this->freeAfterUpload, other.freeAfterUpload); + } + void Geometry::Init(uint32_t vertexCount, uint32_t indexCount) { if (this->vertexCount || this->indexCount) throw std::runtime_error("Geometry is already initialized."); @@ -110,11 +206,17 @@ namespace OpenVulkano::Scene void Geometry::Close() { - vertexCount = 0; - indexCount = 0; - Free(); - renderGeo->Close(); - renderGeo = nullptr; + if (ownsMemory) + { + vertexCount = 0; + indexCount = 0; + Free(); + } + if (renderGeo) + { + renderGeo->Close(); + renderGeo = nullptr; + } } void Geometry::Free() diff --git a/openVulkanoCpp/Scene/Geometry.hpp b/openVulkanoCpp/Scene/Geometry.hpp index 40880ae..c38b9cb 100644 --- a/openVulkanoCpp/Scene/Geometry.hpp +++ b/openVulkanoCpp/Scene/Geometry.hpp @@ -8,6 +8,7 @@ #include "Base/ICloseable.hpp" #include "Math/AABB.hpp" +#include "Base/Utils.hpp" #include class aiMesh; @@ -27,18 +28,19 @@ namespace OpenVulkano { public: uint32_t vertexCount = 0, indexCount = 0; - Vertex* vertices; - void* indices; - VertexIndexType indexType; + Vertex* vertices = nullptr; + void* indices = nullptr; + VertexIndexType indexType = VertexIndexType::UINT16; + bool ownsMemory = true, freeAfterUpload = true; Math::AABB aabb; ICloseable* renderGeo = nullptr; - - Vertex* GetVertices() const { return vertices; } - void* GetIndices() const { return indices; } - uint16_t* GetIndices16() const { return static_cast(indices); } - uint32_t* GetIndices32() const { return static_cast(indices); } - uint32_t GetIndexCount() const { return indexCount; } - uint32_t GetVertexCount() const { return vertexCount; } + public: + Geometry() = default; + Geometry(const Geometry& other); + Geometry& operator=(const Geometry& other); + Geometry(Geometry&& other) noexcept; + Geometry& operator=(Geometry&& other) noexcept; + ~Geometry(); static Geometry* LoadFromFile(const std::string& file) { @@ -47,13 +49,6 @@ namespace OpenVulkano return mesh; } - Geometry() : vertexCount(0), indexCount(0), vertices(nullptr), indices(nullptr), indexType(VertexIndexType::UINT16) {} - - ~Geometry() - { - if (vertices) Geometry::Close(); - } - void InitFromFile(const std::string& file); /** @@ -68,8 +63,21 @@ namespace OpenVulkano void SetIndices(const uint32_t* data, uint32_t size, uint32_t offset = 0) const; void Close() override; - + void Free(); + + Vertex* GetVertices() const { return vertices; } + void* GetIndices() const { return indices; } + uint16_t* GetIndices16() const { return static_cast(indices); } + uint32_t* GetIndices32() const { return static_cast(indices); } + uint32_t GetIndexCount() const { return indexCount; } + uint32_t GetVertexCount() const { return vertexCount; } + bool OwnsMemory() const { return ownsMemory; } + void SetOwnsMemory(bool val) { ownsMemory = val; } + bool FreeAfterUpload() const { return freeAfterUpload; } + void SetFreeAfterUpload(bool val) { freeAfterUpload = val; } + private: + void Swap(Geometry& other) noexcept; }; } } diff --git a/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp b/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp index c49030a..f3cf95f 100644 --- a/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp +++ b/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp @@ -157,6 +157,10 @@ namespace OpenVulkano::Vulkan VulkanGeometry* vkGeo = new VulkanGeometry(geometry, vertexBuffer, indexBuffer); geometries.emplace_back(vkGeo); geometry->renderGeo = vkGeo; + if (geometry->ownsMemory && geometry->freeAfterUpload) + { + geometry->Free(); + } return vkGeo; } return dynamic_cast(geometry->renderGeo); From c29f90fc2843ef10410ff15324551b9de4de293e Mon Sep 17 00:00:00 2001 From: ohyzha Date: Wed, 24 Jul 2024 10:49:03 +0300 Subject: [PATCH 3/4] return geometry by value from factory --- openVulkanoCpp/Scene/GeometryFactory.cpp | 161 +++++++++++------------ openVulkanoCpp/Scene/GeometryFactory.hpp | 14 +- 2 files changed, 87 insertions(+), 88 deletions(-) diff --git a/openVulkanoCpp/Scene/GeometryFactory.cpp b/openVulkanoCpp/Scene/GeometryFactory.cpp index e9fe557..c015c51 100644 --- a/openVulkanoCpp/Scene/GeometryFactory.cpp +++ b/openVulkanoCpp/Scene/GeometryFactory.cpp @@ -13,11 +13,11 @@ namespace OpenVulkano::Scene { - Geometry* GeometryFactory::MakeCube(float x, float y, float z, const Math::Vector4f& color) + Geometry GeometryFactory::MakeCube(float x, float y, float z, const Math::Vector4f& color) { - Geometry* result = new Geometry(); + Geometry result; const int indexCount = 36; - result->Init(24, indexCount); + result.Init(24, indexCount); uint32_t indices[indexCount] = { 0, 1, 2, 0, 2, 3, // front face index data @@ -27,78 +27,77 @@ namespace OpenVulkano::Scene 16, 17, 18, 16, 18, 19, // left face index data 20, 21, 22, 20, 22, 23 // right face index data }; - result->SetIndices(indices, result->indexCount); + result.SetIndices(indices, result.indexCount); x *= 0.5f; y *= 0.5f; z *= 0.5f; uint32_t i = 0; // front face vertex data - result->vertices[i++].Set(+x, -y, +z, +0, +0, +1, +0, +1); - result->vertices[i++].Set(+x, +y, +z, +0, +0, +1, +0, +0); - result->vertices[i++].Set(-x, +y, +z, +0, +0, +1, +1, +0); - result->vertices[i++].Set(-x, -y, +z, +0, +0, +1, +1, +1); + result.vertices[i++].Set(+x, -y, +z, +0, +0, +1, +0, +1); + result.vertices[i++].Set(+x, +y, +z, +0, +0, +1, +0, +0); + result.vertices[i++].Set(-x, +y, +z, +0, +0, +1, +1, +0); + result.vertices[i++].Set(-x, -y, +z, +0, +0, +1, +1, +1); // back face vertex data - result->vertices[i++].Set(+x, -y, -z, +0, +0, -1, +1, +1); - result->vertices[i++].Set(-x, -y, -z, +0, +0, -1, +0, +1); - result->vertices[i++].Set(-x, +y, -z, +0, +0, -1, +0, +0); - result->vertices[i++].Set(+x, +y, -z, +0, +0, -1, +1, +0); + result.vertices[i++].Set(+x, -y, -z, +0, +0, -1, +1, +1); + result.vertices[i++].Set(-x, -y, -z, +0, +0, -1, +0, +1); + result.vertices[i++].Set(-x, +y, -z, +0, +0, -1, +0, +0); + result.vertices[i++].Set(+x, +y, -z, +0, +0, -1, +1, +0); // top face vertex data - result->vertices[i++].Set(+x, +y, +z, +0, -1, +0, +0, +1); - result->vertices[i++].Set(+x, +y, -z, +0, -1, +0, +0, +0); - result->vertices[i++].Set(-x, +y, -z, +0, -1, +0, +1, +0); - result->vertices[i++].Set(-x, +y, +z, +0, -1, +0, +1, +1); + result.vertices[i++].Set(+x, +y, +z, +0, -1, +0, +0, +1); + result.vertices[i++].Set(+x, +y, -z, +0, -1, +0, +0, +0); + result.vertices[i++].Set(-x, +y, -z, +0, -1, +0, +1, +0); + result.vertices[i++].Set(-x, +y, +z, +0, -1, +0, +1, +1); // bottom face vertex data - result->vertices[i++].Set(+x, -y, +z, +0, +1, +0, +1, +1); - result->vertices[i++].Set(-x, -y, +z, +0, +1, +0, +0, +1); - result->vertices[i++].Set(-x, -y, -z, +0, +1, +0, +0, +0); - result->vertices[i++].Set(+x, -y, -z, +0, +1, +0, +1, +0); + result.vertices[i++].Set(+x, -y, +z, +0, +1, +0, +1, +1); + result.vertices[i++].Set(-x, -y, +z, +0, +1, +0, +0, +1); + result.vertices[i++].Set(-x, -y, -z, +0, +1, +0, +0, +0); + result.vertices[i++].Set(+x, -y, -z, +0, +1, +0, +1, +0); // Fill in the left face vertex data - result->vertices[i++].Set(-x, -y, -z, -1, +0, +0, +1, +1); - result->vertices[i++].Set(-x, -y, +z, -1, +0, +0, +0, +1); - result->vertices[i++].Set(-x, +y, +z, -1, +0, +0, +0, +0); - result->vertices[i++].Set(-x, +y, -z, -1, +0, +0, +1, +0); + result.vertices[i++].Set(-x, -y, -z, -1, +0, +0, +1, +1); + result.vertices[i++].Set(-x, -y, +z, -1, +0, +0, +0, +1); + result.vertices[i++].Set(-x, +y, +z, -1, +0, +0, +0, +0); + result.vertices[i++].Set(-x, +y, -z, -1, +0, +0, +1, +0); // Fill in the right face vertex data - result->vertices[i++].Set(+x, -y, +z, +1, +0, +0, +1, +1); - result->vertices[i++].Set(+x, -y, -z, +1, +0, +0, +0, +1); - result->vertices[i++].Set(+x, +y, -z, +1, +0, +0, +0, +0); - result->vertices[i].Set(+x, +y, +z, +1, +0, +0, +1, +0); + result.vertices[i++].Set(+x, -y, +z, +1, +0, +0, +1, +1); + result.vertices[i++].Set(+x, -y, -z, +1, +0, +0, +0, +1); + result.vertices[i++].Set(+x, +y, -z, +1, +0, +0, +0, +0); + result.vertices[i].Set(+x, +y, +z, +1, +0, +0, +1, +0); - for(i = 0; i < result->vertexCount; i++) + for(i = 0; i < result.vertexCount; i++) { - result->vertices[i].color = color; + result.vertices[i].color = color; } - return result; } - Geometry* GeometryFactory::MakePlane(float width, float height, const Math::Vector4f& color) + Geometry GeometryFactory::MakePlane(float width, float height, const Math::Vector4f& color) { - Geometry* result = new Geometry(); - result->Init(4, 6); + Geometry result; + result.Init(4, 6); uint32_t indices[] = { 0, 2, 1, 0, 3, 2 }; - result->SetIndices(indices, result->indexCount); + result.SetIndices(indices, result.indexCount); width *= 0.5f; height *= 0.5f; - result->vertices[0].Set(-width, -height, 0, 0, 0, 1, 0, 1); - result->vertices[1].Set(-width, height, 0, 0, 0, 1, 0, 0); - result->vertices[2].Set(width, height, 0, 0, 0, 1, 1, 0); - result->vertices[3].Set(width, -height, 0, 0, 0, 1, 1, 1); + result.vertices[0].Set(-width, -height, 0, 0, 0, 1, 0, 1); + result.vertices[1].Set(-width, height, 0, 0, 0, 1, 0, 0); + result.vertices[2].Set(width, height, 0, 0, 0, 1, 1, 0); + result.vertices[3].Set(width, -height, 0, 0, 0, 1, 1, 1); - for (uint32_t i = 0; i < result->vertexCount; i++) + for (uint32_t i = 0; i < result.vertexCount; i++) { - result->vertices[i].color = color; + result.vertices[i].color = color; } return result; } - Geometry* GeometryFactory::MakeSphere(float radius, uint32_t segments, uint32_t rings, const Math::Vector4f& color) + Geometry GeometryFactory::MakeSphere(float radius, uint32_t segments, uint32_t rings, const Math::Vector4f& color) { - Geometry* result = new Geometry(); + Geometry result; uint32_t vertexCount = (rings + 1) * (segments + 1); uint32_t indexCount = 6 * rings * segments; - result->Init(vertexCount, indexCount); + result.Init(vertexCount, indexCount); uint32_t i = 0; for (uint32_t ring = 0; ring <= rings; ++ring) @@ -119,8 +118,8 @@ namespace OpenVulkano::Scene float u = 1 - (float(segment) / segments); float v = float(ring) / rings; - result->vertices[i].Set(radius * x, radius * y, radius * z, x, y, z, u, v); - result->vertices[i].color = color; + result.vertices[i].Set(radius * x, radius * y, radius * z, x, y, z, u, v); + result.vertices[i].color = color; ++i; } } @@ -143,18 +142,18 @@ namespace OpenVulkano::Scene indices[i++] = second + 1; } } - result->SetIndices(indices, indexCount); + result.SetIndices(indices, indexCount); delete[] indices; return result; } - Geometry* GeometryFactory::MakeHemisphere(float radius, uint32_t segments, uint32_t rings, const Math::Vector4f& color) + Geometry GeometryFactory::MakeHemisphere(float radius, uint32_t segments, uint32_t rings, const Math::Vector4f& color) { - Geometry* result = new Geometry(); + Geometry result; uint32_t vertexCount = (rings + 1) * (segments + 1); uint32_t indexCount = 6 * rings * segments; - result->Init(vertexCount, indexCount); + result.Init(vertexCount, indexCount); uint32_t i = 0; for (uint32_t ring = 0; ring <= rings; ++ring) @@ -175,8 +174,8 @@ namespace OpenVulkano::Scene float u = 1 - (float(segment) / segments); float v = float(ring) / rings; - result->vertices[i].Set(radius * x, radius * y, radius * z, x, y, z, u, v); - result->vertices[i].color = color; + result.vertices[i].Set(radius * x, radius * y, radius * z, x, y, z, u, v); + result.vertices[i].color = color; ++i; } } @@ -199,37 +198,37 @@ namespace OpenVulkano::Scene indices[i++] = second + 1; } } - result->SetIndices(indices, indexCount); + result.SetIndices(indices, indexCount); delete[] indices; return result; } - Geometry* GeometryFactory::MakeTriangle(const Math::Vector3f& p1, const Math::Vector3f& p2, const Math::Vector3f& p3, const Math::Vector4f& color) + Geometry GeometryFactory::MakeTriangle(const Math::Vector3f& p1, const Math::Vector3f& p2, const Math::Vector3f& p3, const Math::Vector4f& color) { - Geometry* result = new Geometry(); - result->Init(3, 3); + Geometry result; + result.Init(3, 3); uint32_t indices[] = { 0, 1, 2 }; - result->SetIndices(indices, result->indexCount); + result.SetIndices(indices, result.indexCount); - result->vertices[0].Set(p1.x, p1.y, p1.z, 0, 0, 1, 0, 0); - result->vertices[1].Set(p2.x, p2.y, p2.z, 0, 0, 1, 1, 0); - result->vertices[2].Set(p3.x, p3.y, p3.z, 0, 0, 1, 0, 1); + result.vertices[0].Set(p1.x, p1.y, p1.z, 0, 0, 1, 0, 0); + result.vertices[1].Set(p2.x, p2.y, p2.z, 0, 0, 1, 1, 0); + result.vertices[2].Set(p3.x, p3.y, p3.z, 0, 0, 1, 0, 1); - for (uint32_t i = 0; i < result->vertexCount; i++) + for (uint32_t i = 0; i < result.vertexCount; i++) { - result->vertices[i].color = color; + result.vertices[i].color = color; } return result; } - Geometry* GeometryFactory::MakeCylinder(float radius, float height, uint32_t segments, const Math::Vector4f& color) + Geometry GeometryFactory::MakeCylinder(float radius, float height, uint32_t segments, const Math::Vector4f& color) { - Geometry* result = new Geometry(); + Geometry result; uint32_t vertexCount = 2 * (segments + 1); uint32_t indexCount = 12 * segments; - result->Init(vertexCount, indexCount); + result.Init(vertexCount, indexCount); float halfHeight = height * 0.5f; uint32_t i = 0; @@ -240,10 +239,10 @@ namespace OpenVulkano::Scene float z = radius * sin(theta); float u = float(segment) / segments; - result->vertices[i].Set(x, -halfHeight, z, x, 0, z, u, 0); - result->vertices[i++].color = color; - result->vertices[i].Set(x, halfHeight, z, x, 0, z, u, 1); - result->vertices[i++].color = color; + result.vertices[i].Set(x, -halfHeight, z, x, 0, z, u, 0); + result.vertices[i++].color = color; + result.vertices[i].Set(x, halfHeight, z, x, 0, z, u, 1); + result.vertices[i++].color = color; } i = 0; @@ -269,16 +268,16 @@ namespace OpenVulkano::Scene indices[i++] = first; indices[i++] = second + 2; } - result->SetIndices(indices, indexCount); + result.SetIndices(indices, indexCount); delete[] indices; return result; } - Geometry* GeometryFactory::MakePyramid(float baseLength, float height, const Math::Vector4f& color) + Geometry GeometryFactory::MakePyramid(float baseLength, float height, const Math::Vector4f& color) { - Geometry* result = new Geometry(); - result->Init(5, 18); + Geometry result; + result.Init(5, 18); uint32_t indices[] = { // Base 1, 2, 3, @@ -289,21 +288,21 @@ namespace OpenVulkano::Scene 0, 3, 4, 0, 4, 1 }; - result->SetIndices(indices, result->indexCount); + result.SetIndices(indices, result.indexCount); float halfBase = baseLength * 0.5f; // Top vertex - result->vertices[0].Set(0, height, 0, 0, 1, 0, 0.5f, 1); + result.vertices[0].Set(0, height, 0, 0, 1, 0, 0.5f, 1); // Base vertices - result->vertices[1].Set(-halfBase, 0, halfBase, 0, -1, 0, 0, 0); - result->vertices[2].Set(halfBase, 0, halfBase, 0, -1, 0, 1, 0); - result->vertices[3].Set(halfBase, 0, -halfBase, 0, -1, 0, 1, 1); - result->vertices[4].Set(-halfBase, 0, -halfBase, 0, -1, 0, 0, 1); + result.vertices[1].Set(-halfBase, 0, halfBase, 0, -1, 0, 0, 0); + result.vertices[2].Set(halfBase, 0, halfBase, 0, -1, 0, 1, 0); + result.vertices[3].Set(halfBase, 0, -halfBase, 0, -1, 0, 1, 1); + result.vertices[4].Set(-halfBase, 0, -halfBase, 0, -1, 0, 0, 1); - for (uint32_t i = 0; i < result->vertexCount; i++) + for (uint32_t i = 0; i < result.vertexCount; i++) { - result->vertices[i].color = color; + result.vertices[i].color = color; } return result; diff --git a/openVulkanoCpp/Scene/GeometryFactory.hpp b/openVulkanoCpp/Scene/GeometryFactory.hpp index 99d642f..cd5e478 100644 --- a/openVulkanoCpp/Scene/GeometryFactory.hpp +++ b/openVulkanoCpp/Scene/GeometryFactory.hpp @@ -13,12 +13,12 @@ namespace OpenVulkano::Scene class GeometryFactory { public: - static Geometry* MakeCube(float x = 1, float y = 1, float z = 1, const Math::Vector4f& color = Math::Vector4f(1)); - static Geometry* MakePlane(float width = 1, float height = 1, const Math::Vector4f& color = Math::Vector4f(1)); - static Geometry* MakeSphere(float radius, uint32_t segments, uint32_t rings, const Math::Vector4f& color = Math::Vector4f(1)); - static Geometry* MakeHemisphere(float radius, uint32_t segments, uint32_t rings, const Math::Vector4f& color = Math::Vector4f(1)); - static Geometry* MakeTriangle(const Math::Vector3f& p1, const Math::Vector3f& p2, const Math::Vector3f& p3, const Math::Vector4f& color = Math::Vector4f(1)); - static Geometry* MakeCylinder(float radius, float height, uint32_t segments, const Math::Vector4f& color = Math::Vector4f(1)); - static Geometry* MakePyramid(float baseLength = 1, float height = 1, const Math::Vector4f& color = Math::Vector4f(1)); + static Geometry MakeCube(float x = 1, float y = 1, float z = 1, const Math::Vector4f& color = Math::Vector4f(1)); + static Geometry MakePlane(float width = 1, float height = 1, const Math::Vector4f& color = Math::Vector4f(1)); + static Geometry MakeSphere(float radius, uint32_t segments, uint32_t rings, const Math::Vector4f& color = Math::Vector4f(1)); + static Geometry MakeHemisphere(float radius, uint32_t segments, uint32_t rings, const Math::Vector4f& color = Math::Vector4f(1)); + static Geometry MakeTriangle(const Math::Vector3f& p1, const Math::Vector3f& p2, const Math::Vector3f& p3, const Math::Vector4f& color = Math::Vector4f(1)); + static Geometry MakeCylinder(float radius, float height, uint32_t segments, const Math::Vector4f& color = Math::Vector4f(1)); + static Geometry MakePyramid(float baseLength = 1, float height = 1, const Math::Vector4f& color = Math::Vector4f(1)); }; } From 10d624055504dc3008208a1b0c0d83749815e951 Mon Sep 17 00:00:00 2001 From: ohyzha Date: Wed, 24 Jul 2024 10:50:33 +0300 Subject: [PATCH 4/4] rework examples according to new changes --- examples/ExampleApps/BillboardExampleApp.cpp | 13 ++++--------- examples/ExampleApps/CubesExampleApp.cpp | 10 +++++++--- examples/ExampleApps/MovingCubeApp.cpp | 4 ++-- examples/ExampleApps/TexturedCubeExampleApp.cpp | 4 ++-- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/examples/ExampleApps/BillboardExampleApp.cpp b/examples/ExampleApps/BillboardExampleApp.cpp index b579a17..aa74776 100644 --- a/examples/ExampleApps/BillboardExampleApp.cpp +++ b/examples/ExampleApps/BillboardExampleApp.cpp @@ -73,12 +73,12 @@ namespace OpenVulkano m_uniBuffer.setId = 3; m_drawablesPool.resize(cntDrawables); m_nodesPool.resize(cntDrawables); - m_geo.reserve(cntDrawables); + m_geo.resize(cntDrawables); m_texturedMat.texture = &Texture::PLACEHOLDER; for (uint32_t i = 0; i < cntDrawables; i++) { - Geometry* geo = nullptr; + Geometry* geo = &m_geo[i]; m_nodesPool[i].Init(); if (i < quadsCnt) { @@ -94,11 +94,10 @@ namespace OpenVulkano } else { - geo = GeometryFactory::MakePyramid(1, 1, glm::vec4(0, 1, 0, 1)); + *geo = GeometryFactory::MakePyramid(1, 1, glm::vec4(0, 1, 0, 1)); m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-5 + std::rand() % 5, -5 + std::rand() % 5, -std::rand() % 10))); m_drawablesPool[i].Init(&m_shader, geo, &m_mat, &m_uniBuffer); } - m_geo.push_back(geo); m_scene.GetRoot()->AddChild(&m_nodesPool[i]); m_nodesPool[i].AddDrawable(&m_drawablesPool[i]); } @@ -123,10 +122,6 @@ namespace OpenVulkano void Close() override { - for (Geometry* g: m_geo) - { - delete g; - } } private: @@ -143,7 +138,7 @@ namespace OpenVulkano std::vector m_nodesPool; Vector3f_SIMD m_position = { 0, 0, -10 }; OpenVulkano::Scene::UI::SimpleUi m_ui; - std::vector m_geo; + std::vector m_geo; std::shared_ptr m_perfInfo; }; diff --git a/examples/ExampleApps/CubesExampleApp.cpp b/examples/ExampleApps/CubesExampleApp.cpp index 8e24702..8b3909b 100644 --- a/examples/ExampleApps/CubesExampleApp.cpp +++ b/examples/ExampleApps/CubesExampleApp.cpp @@ -42,6 +42,7 @@ namespace OpenVulkano std::vector drawablesPool; std::vector nodesPool; Vector3f_SIMD position = {0, 0, -10}; + std::vector m_geos; OpenVulkano::Scene::UI::SimpleUi m_ui; std::shared_ptr m_perfInfo; @@ -61,11 +62,14 @@ namespace OpenVulkano shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/basic"); shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription()); drawablesPool.resize(GEOS); + m_geos.reserve(GEOS); for (uint32_t i = 0; i < GEOS; i++) { - Geometry* geo = GeometryFactory::MakeCube(std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, - Vector4f((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1)); - drawablesPool[i].Init(&shader, geo, &mat); + m_geos.push_back(GeometryFactory::MakeCube( + std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, + std::rand() % 1000 / 1000.0f + 0.01f, + Vector4f((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1))); + drawablesPool[i].Init(&shader, &m_geos[i], &mat); } nodesPool.resize(OBJECTS); for (uint32_t i = 0; i < OBJECTS; i++) diff --git a/examples/ExampleApps/MovingCubeApp.cpp b/examples/ExampleApps/MovingCubeApp.cpp index 281f4e6..1f637b0 100644 --- a/examples/ExampleApps/MovingCubeApp.cpp +++ b/examples/ExampleApps/MovingCubeApp.cpp @@ -33,7 +33,7 @@ namespace OpenVulkano { struct SceneElement { - Scene::Geometry *m_geometry; + Scene::Geometry m_geometry; Scene::SimpleDrawable m_drawable; Scene::Node m_node; }; @@ -70,7 +70,7 @@ namespace OpenVulkano void CompleteSceneElement(SceneElement *dest) { - dest->m_drawable.Init(&m_shader, dest->m_geometry, &m_material); + dest->m_drawable.Init(&m_shader, &dest->m_geometry, &m_material); dest->m_node.Init(); m_scene.GetRoot()->AddChild(&dest->m_node); dest->m_node.SetUpdateFrequency(Scene::UpdateFrequency::Always); diff --git a/examples/ExampleApps/TexturedCubeExampleApp.cpp b/examples/ExampleApps/TexturedCubeExampleApp.cpp index 39c6906..af3ba7d 100644 --- a/examples/ExampleApps/TexturedCubeExampleApp.cpp +++ b/examples/ExampleApps/TexturedCubeExampleApp.cpp @@ -49,9 +49,9 @@ namespace OpenVulkano shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/basicTexture"); shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription()); shader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING); - Geometry* geo = GeometryFactory::MakeCube(); + static Geometry geo = GeometryFactory::MakeCube(); mat.texture = &Texture::PLACEHOLDER; - drawable.Init(&shader, geo, &mat); + drawable.Init(&shader, &geo, &mat); node.Init(); scene.GetRoot()->AddChild(&node); node.SetUpdateFrequency(UpdateFrequency::Always);