From d8499cff0419cce8cf1f58d7fb334e90606a7f4d Mon Sep 17 00:00:00 2001 From: Aaron <56617292+SpinnerX@users.noreply.github.com> Date: Sat, 23 Nov 2024 12:00:48 -0800 Subject: [PATCH] Updated Scene Objects (#66) * Merged mesh factory branch to main * Working Vertex buffer class that replaces vk::VulkanModel to contains the vertices of the mesh * Working index buffers and added mesh class that contains both the vertex and index buffers * Working mesh class can also now load 3D models and work with new vertex and index buffers as well --- CMakeLists.txt | 3 + Editor/CMakeLists.txt | 3 +- Editor/Editor/Editor.cpp | 2 - Editor/Editor/Editor.hpp | 11 -- Editor/Editor/EditorScene.cpp | 120 ++++++++------- Editor/Editor/EditorScene.hpp | 1 + TestApp/CMakeLists.txt | 5 +- .../Graphics/Meshes/MeshContainer.hpp | 8 +- .../Meshes/ChildrenMeshes/CubeMesh.cpp | 9 +- .../Meshes/ChildrenMeshes/SphereMesh.cpp | 10 +- conanfile.py | 2 + engine3d/Core/GraphicDrivers/IndexBuffer.hpp | 21 +++ engine3d/Core/GraphicDrivers/Mesh.hpp | 28 ++++ engine3d/Core/GraphicDrivers/VertexBuffer.hpp | 41 ++++++ engine3d/Core/Renderer/Renderer.hpp | 3 - engine3d/Core/Scene/SceneObject.hpp | 75 ---------- engine3d/Core/Scene/SceneTest.hpp | 120 --------------- .../SceneObjects/SceneObject.hpp | 9 +- .../Vulkan2Showcase/Shaders/VulkanShader.hpp | 2 + .../Vulkan2Showcase/VulkanIndexBuffer.hpp | 21 +++ .../internal/Vulkan2Showcase/VulkanModel.hpp | 45 ------ .../Vulkan2Showcase/VulkanVertexBuffer.hpp | 21 +++ engine3d/Core/utils/types.hpp | 12 ++ .../ObjectLayerPairFilterInterface.hpp | 1 + src/CMakeLists.txt | 22 ++- src/engine3d/Core/ApplicationInstance.cpp | 5 + .../Core/GraphicDrivers/IndexBuffer.cpp | 26 ++++ src/engine3d/Core/GraphicDrivers/Mesh.cpp | 98 +++++++++++++ .../Core/GraphicDrivers/VertexBuffer.cpp | 32 ++++ src/engine3d/Core/Renderer/Renderer.cpp | 67 ++++----- src/engine3d/Core/Scene/SceneObject.cpp | 10 -- .../Vulkan2Showcase/Shaders/VulkanShader.cpp | 38 ++++- .../Vulkan2Showcase/VulkanIndexBuffer.cpp | 90 ++++++++++++ .../internal/Vulkan2Showcase/VulkanModel.cpp | 137 ------------------ .../Vulkan2Showcase/VulkanVertexBuffer.cpp | 57 ++++++++ 35 files changed, 627 insertions(+), 528 deletions(-) create mode 100644 engine3d/Core/GraphicDrivers/IndexBuffer.hpp create mode 100644 engine3d/Core/GraphicDrivers/Mesh.hpp create mode 100644 engine3d/Core/GraphicDrivers/VertexBuffer.hpp delete mode 100644 engine3d/Core/Scene/SceneObject.hpp delete mode 100644 engine3d/Core/Scene/SceneTest.hpp create mode 100644 engine3d/Core/internal/Vulkan2Showcase/VulkanIndexBuffer.hpp delete mode 100644 engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp create mode 100644 engine3d/Core/internal/Vulkan2Showcase/VulkanVertexBuffer.hpp create mode 100644 engine3d/Core/utils/types.hpp create mode 100644 src/engine3d/Core/GraphicDrivers/IndexBuffer.cpp create mode 100644 src/engine3d/Core/GraphicDrivers/Mesh.cpp create mode 100644 src/engine3d/Core/GraphicDrivers/VertexBuffer.cpp delete mode 100644 src/engine3d/Core/Scene/SceneObject.cpp create mode 100644 src/engine3d/Core/internal/Vulkan2Showcase/VulkanIndexBuffer.cpp delete mode 100644 src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp create mode 100644 src/engine3d/Core/internal/Vulkan2Showcase/VulkanVertexBuffer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7024885..661df3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,9 @@ project(engine3d CXX) set(CMAKE_CXX_STANDARD 23) set(ENGINE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/engine3d) +find_package(tinyobjloader REQUIRED) + build_library( DIRECTORIES src Editor TestApp + LINK_LIBRARIES tinyobjloader::tinyobjloader ) \ No newline at end of file diff --git a/Editor/CMakeLists.txt b/Editor/CMakeLists.txt index 40c9ba7..405d9f7 100644 --- a/Editor/CMakeLists.txt +++ b/Editor/CMakeLists.txt @@ -6,6 +6,7 @@ project(Editor CXX) # Editor/Editor.hpp # Editor/Editor.cpp # ) +find_package(tinyobjloader REQUIRED) build_demos( SOURCES @@ -14,5 +15,5 @@ build_demos( Editor/EditorScene.hpp Editor/EditorScene.cpp - LINK_LIBRARIES engine3d + LINK_LIBRARIES tinyobjloader::tinyobjloader engine3d ) diff --git a/Editor/Editor/Editor.cpp b/Editor/Editor/Editor.cpp index a947832..7d8c16d 100644 --- a/Editor/Editor/Editor.cpp +++ b/Editor/Editor/Editor.cpp @@ -1,7 +1,5 @@ #include "Editor.hpp" #include "EditorScene.hpp" -#include -#include #include #include diff --git a/Editor/Editor/Editor.hpp b/Editor/Editor/Editor.hpp index 2c3c9ff..24150d0 100644 --- a/Editor/Editor/Editor.hpp +++ b/Editor/Editor/Editor.hpp @@ -1,8 +1,5 @@ #pragma once -// #include #include -// #include -// #include #include "EditorScene.hpp" namespace engine3d{ @@ -22,15 +19,7 @@ namespace engine3d{ void OnApplicationUpdate(); private: - // Ref m_SquareMesh; - // Ref m_CircleMesh; EditorScene* m_EditorScene; - // std::vector m_SceneGameObjects; - // std::vector m_VectorFields; - - // GravityPhysicsSystem m_GravitySystem; - // Vec2FieldSystem m_VectorFieldSystem; - // Ref m_CubeMesh; diff --git a/Editor/Editor/EditorScene.cpp b/Editor/Editor/EditorScene.cpp index 6201cac..e925225 100644 --- a/Editor/Editor/EditorScene.cpp +++ b/Editor/Editor/EditorScene.cpp @@ -1,6 +1,6 @@ #include "EditorScene.hpp" +#include "Core/GraphicDrivers/VertexBuffer.hpp" #include -#include #include #include #include @@ -9,61 +9,54 @@ #include namespace engine3d{ - Ref CreateCubeMesh(glm::vec3 offset){ - std::vector vertices{ - // left Face (white) - vk::VulkanModel::Vertex{.Position{-.5f, -.5f, -.5f}, .Color{.9f, .9f, .9f}}, - vk::VulkanModel::Vertex{.Position{-.5f, .5f, .5f}, .Color{.9f, .9f, .9f}}, - vk::VulkanModel::Vertex{.Position ={-.5f, -.5f, .5f}, .Color{.9f, .9f, .9f}}, - vk::VulkanModel::Vertex{.Position ={-.5f, -.5f, -.5f},.Color {.9f, .9f, .9f}}, - vk::VulkanModel::Vertex{.Position ={-.5f, .5f, -.5f}, .Color{.9f, .9f, .9f}}, - vk::VulkanModel::Vertex{.Position ={-.5f, .5f, .5f}, .Color{.9f, .9f, .9f}}, - - // right face (yellow) - {{.5f, -.5f, -.5f}, {.8f, .8f, .1f}}, - {{.5f, .5f, .5f}, {.8f, .8f, .1f}}, - {{.5f, -.5f, .5f}, {.8f, .8f, .1f}}, - {{.5f, -.5f, -.5f}, {.8f, .8f, .1f}}, - {{.5f, .5f, -.5f}, {.8f, .8f, .1f}}, - {{.5f, .5f, .5f}, {.8f, .8f, .1f}}, - - // top face (orange, remember y axis points down) - {{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}}, - {{.5f, -.5f, .5f}, {.9f, .6f, .1f}}, - {{-.5f, -.5f, .5f}, {.9f, .6f, .1f}}, - {{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}}, - {{.5f, -.5f, -.5f}, {.9f, .6f, .1f}}, - {{.5f, -.5f, .5f}, {.9f, .6f, .1f}}, - - // bottom face (red) - {{-.5f, .5f, -.5f}, {.8f, .1f, .1f}}, - {{.5f, .5f, .5f}, {.8f, .1f, .1f}}, - {{-.5f, .5f, .5f}, {.8f, .1f, .1f}}, - {{-.5f, .5f, -.5f}, {.8f, .1f, .1f}}, - {{.5f, .5f, -.5f}, {.8f, .1f, .1f}}, - {{.5f, .5f, .5f}, {.8f, .1f, .1f}}, - - // nose face (blue) - {{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}}, - {{.5f, .5f, 0.5f}, {.1f, .1f, .8f}}, - {{-.5f, .5f, 0.5f}, {.1f, .1f, .8f}}, - {{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}}, - {{.5f, -.5f, 0.5f}, {.1f, .1f, .8f}}, - {{.5f, .5f, 0.5f}, {.1f, .1f, .8f}}, + static Mesh CreateCubeMesh(glm::vec3 offset){ + std::vector vertices = { // left face (white) + {{-.5f, -.5f, -.5f}, {.9f, .9f, .9f}}, + {{-.5f, .5f, .5f}, {.9f, .9f, .9f}}, + {{-.5f, -.5f, .5f}, {.9f, .9f, .9f}}, + {{-.5f, .5f, -.5f}, {.9f, .9f, .9f}}, + + // right face (yellow) + {{.5f, -.5f, -.5f}, {.8f, .8f, .1f}}, + {{.5f, .5f, .5f}, {.8f, .8f, .1f}}, + {{.5f, -.5f, .5f}, {.8f, .8f, .1f}}, + {{.5f, .5f, -.5f}, {.8f, .8f, .1f}}, + + // top face (orange, remember y axis points down) + {{-.5f, -.5f, -.5f}, {.9f, .6f, .1f}}, + {{.5f, -.5f, .5f}, {.9f, .6f, .1f}}, + {{-.5f, -.5f, .5f}, {.9f, .6f, .1f}}, + {{.5f, -.5f, -.5f}, {.9f, .6f, .1f}}, + + // bottom face (red) + {{-.5f, .5f, -.5f}, {.8f, .1f, .1f}}, + {{.5f, .5f, .5f}, {.8f, .1f, .1f}}, + {{-.5f, .5f, .5f}, {.8f, .1f, .1f}}, + {{.5f, .5f, -.5f}, {.8f, .1f, .1f}}, + + // nose face (blue) + {{-.5f, -.5f, 0.5f}, {.1f, .1f, .8f}}, + {{.5f, .5f, 0.5f}, {.1f, .1f, .8f}}, + {{-.5f, .5f, 0.5f}, {.1f, .1f, .8f}}, + {{.5f, -.5f, 0.5f}, {.1f, .1f, .8f}}, + + // tail face (green) + {{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}}, + {{.5f, .5f, -0.5f}, {.1f, .8f, .1f}}, + {{-.5f, .5f, -0.5f}, {.1f, .8f, .1f}}, + {{.5f, -.5f, -0.5f}, {.1f, .8f, .1f}}, + }; + for (auto& v : vertices) { + v.Position += offset; + } + + std::vector indices = {0, 1, 2, 0, 3, 1, 4, 5, 6, 4, 7, 5, 8, 9, 10, 8, 11, 9, + 12, 13, 14, 12, 15, 13, 16, 17, 18, 16, 19, 17, 20, 21, 22, 20, 23, 21}; - // tail face (green) - {{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}}, - {{.5f, .5f, -0.5f}, {.1f, .8f, .1f}}, - {{-.5f, .5f, -0.5f}, {.1f, .8f, .1f}}, - {{-.5f, -.5f, -0.5f}, {.1f, .8f, .1f}}, - {{.5f, -.5f, -0.5f}, {.1f, .8f, .1f}}, - {{.5f, .5f, -0.5f}, {.1f, .8f, .1f}}, - }; - - for (auto& v : vertices) { - v.Position += offset; - } - return CreateRef(vertices, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + Ref vb = VertexBuffer::Create(vertices); + Ref ib = IndexBuffer::Create(indices); + // return VertexBuffer::Create(vertices); + return Mesh(vb, ib); } EditorScene::EditorScene(){ @@ -71,7 +64,12 @@ namespace engine3d{ // SyncUpdateManager::GetInstance()->Subscribe(this, &EditorScene::OnUpdate); - auto cube_mesh = CreateCubeMesh({.0f, .0f, .0f}); + // auto cube_mesh = CreateCubeMesh({.0f, .0f, .0f}); + + //! @note Instead of loading in primitive cubes by hand, we load in .obj's instead. + // auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/smooth_vase.obj"); + // auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/colored_cube.obj"); + auto cube_mesh = Mesh::LoadModel("3d_models/tutorial/sphere.obj"); //! @note Make this scene object as part of our current scene. @@ -89,10 +87,9 @@ namespace engine3d{ // ----------------------------- SceneObject* cube1 = new SceneObject(m_Scene); auto& cube1_transform = cube1->SceneGetComponent(); - // auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio(); cube1_transform.m_Position = {.0f, .0f, 2.5}; cube1_transform.m_Scale = {.5f, .5f, 0.5}; - cube1->SetModal(cube_mesh); + cube1->SetMesh(cube_mesh); // ----------------------------- // Cube 2 Scene object Creation @@ -102,8 +99,7 @@ namespace engine3d{ // auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio(); cube2_transform.m_Position = {5.f, .0f, -7.f}; cube2_transform.m_Scale = {.5f, .5f, 0.5}; - cube2->SetModal(cube_mesh); - + cube2->SetMesh(cube_mesh); @@ -133,9 +129,9 @@ namespace engine3d{ auto cube_transform = m_SceneObjects[0]->SceneGetComponent(); // float tempDt_Y; glm::vec2 temp_position = {0.f, 0.f}; - constexpr float sensitivity = 5.0f; + constexpr float sensitivity = 2.0f; constexpr float pos_sensitivity = 2.f; - constexpr glm::vec2 invert_pos = {-1, 1}; + constexpr glm::vec2 invert_pos = {1, -1}; // ConsoleLogInfo("x = {}, y = {}, z = {}", transform.m_Position.x, transform.m_Position.y, transform.m_Position.z); // ConsoleLogInfo("x = {}, y = {}, z = {}\n", cube_transform.m_Position.x, cube_transform.m_Position.y, cube_transform.m_Position.z); diff --git a/Editor/Editor/EditorScene.hpp b/Editor/Editor/EditorScene.hpp index 86a8247..c03f4fa 100644 --- a/Editor/Editor/EditorScene.hpp +++ b/Editor/Editor/EditorScene.hpp @@ -1,5 +1,6 @@ #pragma once #include +// #include #include #include diff --git a/TestApp/CMakeLists.txt b/TestApp/CMakeLists.txt index 2daaf6c..9498906 100644 --- a/TestApp/CMakeLists.txt +++ b/TestApp/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.25) project(TestApp CXX) +find_package(tinyobjloader REQUIRED) + build_demos( SOURCES Application.cpp @@ -40,9 +42,8 @@ build_demos( src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/SphereMesh.cpp Scenes/Assets/Components/Graphics/Meshes/MeshContainer.hpp src/Scenes/Assets/Components/Graphics/Meshes/MeshContainer.cpp - - LINK_LIBRARIES engine3d + LINK_LIBRARIES tinyobjloader::tinyobjloader engine3d ) target_include_directories(${PROJECT_NAME} PUBLIC ./) diff --git a/TestApp/Scenes/Assets/Components/Graphics/Meshes/MeshContainer.hpp b/TestApp/Scenes/Assets/Components/Graphics/Meshes/MeshContainer.hpp index 8491f0c..e8907a7 100644 --- a/TestApp/Scenes/Assets/Components/Graphics/Meshes/MeshContainer.hpp +++ b/TestApp/Scenes/Assets/Components/Graphics/Meshes/MeshContainer.hpp @@ -1,18 +1,20 @@ #pragma once +#include +#include #include #include -#include + //! @brief Factory class MeshContainer { public: MeshContainer() = default; - std::vector* GetVertices(){return vertices;} + engine3d::Ref GetVertices(){return vertices;} std::vector GetNormals(){return normals;} std::vector GettexCoords(){return texCoords;} protected: - std::vector* vertices; + engine3d::Ref vertices; std::vector normals; std::vector texCoords; }; \ No newline at end of file diff --git a/TestApp/src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/CubeMesh.cpp b/TestApp/src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/CubeMesh.cpp index a3cedb6..74049eb 100644 --- a/TestApp/src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/CubeMesh.cpp +++ b/TestApp/src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/CubeMesh.cpp @@ -1,5 +1,6 @@ -#include "Core/internal/Vulkan2Showcase/VulkanModel.hpp" +#include "Core/EngineLogger.hpp" +#include "Core/GraphicDrivers/VertexBuffer.hpp" #include #include #include @@ -75,6 +76,8 @@ CubeMesh::CubeMesh() {.1f, .8f, .1f} }; - vertices = engine3d::vk::VulkanModel::CreateVertexVector(verts, colors); - + vertices = engine3d::VertexBuffer::CreateVertexVector(verts, colors); + if(vertices != nullptr){ + ConsoleLogWarn("SphereMesh vertices are a nullptr. Implement VertexBuffer::CreateVertexVector() in VulkanVertexBuffer to get this working and not nullptr"); + } } diff --git a/TestApp/src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/SphereMesh.cpp b/TestApp/src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/SphereMesh.cpp index 58bbc1d..c17973e 100644 --- a/TestApp/src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/SphereMesh.cpp +++ b/TestApp/src/Scenes/Assets/Components/Graphics/Meshes/ChildrenMeshes/SphereMesh.cpp @@ -1,6 +1,8 @@ -#include "Core/internal/Vulkan2Showcase/VulkanModel.hpp" +#include "Core/EngineLogger.hpp" +#include "Core/GraphicDrivers/VertexBuffer.hpp" #include #include + SphereMesh::SphereMesh(int Radius, int SectorCount, int StackCount) { @@ -47,5 +49,9 @@ SphereMesh::SphereMesh(int Radius, int SectorCount, int StackCount) } } - vertices = engine3d::vk::VulkanModel::CreateVertexVector(verts); + vertices = engine3d::VertexBuffer::CreateVertexVector(verts); + + if(vertices != nullptr){ + ConsoleLogWarn("SphereMesh vertices are a nullptr. Implement VertexBuffer::CreateVertexVector() in VulkanVertexBuffer to get this working and not nullptr"); + } } \ No newline at end of file diff --git a/conanfile.py b/conanfile.py index 3e77402..9903e3b 100644 --- a/conanfile.py +++ b/conanfile.py @@ -42,6 +42,8 @@ def requirements(self): self.requires("imguidocking/1.0") self.requires("entt/3.13.2") + self.requires("tinyobjloader/2.0.0-rc10") + self.requires("joltphysics/1.0") diff --git a/engine3d/Core/GraphicDrivers/IndexBuffer.hpp b/engine3d/Core/GraphicDrivers/IndexBuffer.hpp new file mode 100644 index 0000000..ddbd436 --- /dev/null +++ b/engine3d/Core/GraphicDrivers/IndexBuffer.hpp @@ -0,0 +1,21 @@ +#pragma once +#include +#include +#include + +namespace engine3d{ + class IndexBuffer{ + public: + static Ref Create(std::vector p_Indices); + + void Bind(VkCommandBuffer p_CommandBuffer); + void Draw(VkCommandBuffer p_CommandBuffer); + + bool HasIndicesPresent() const; + + protected: + virtual void BindToIndexBuffer(VkCommandBuffer p_CommandBuffer) = 0; + virtual void RenderIndexBuffer(VkCommandBuffer p_CommandBuffer) = 0; + virtual bool HasIndices() const = 0; + }; +}; \ No newline at end of file diff --git a/engine3d/Core/GraphicDrivers/Mesh.hpp b/engine3d/Core/GraphicDrivers/Mesh.hpp new file mode 100644 index 0000000..23997da --- /dev/null +++ b/engine3d/Core/GraphicDrivers/Mesh.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include + +#define TINYOBJLOADER_IMPLEMENTATION +#include + +namespace engine3d{ + + class Mesh{ + public: + Mesh() = default; + Mesh(const Ref& p_Vb, const Ref& p_Ib); + + static Mesh LoadModel(const std::string& p_Filename); + + Ref& GetVertices() { return m_Vertices; } + + Ref& GetIndices() { return m_Indices; } + + private: + Ref m_Vertices; + Ref m_Indices; + }; + +}; \ No newline at end of file diff --git a/engine3d/Core/GraphicDrivers/VertexBuffer.hpp b/engine3d/Core/GraphicDrivers/VertexBuffer.hpp new file mode 100644 index 0000000..fe0f7c1 --- /dev/null +++ b/engine3d/Core/GraphicDrivers/VertexBuffer.hpp @@ -0,0 +1,41 @@ +#pragma once +#include +#include +#include +#include + +namespace engine3d{ + /* + @name VertexBuffer + @note Contains vertices and does backend API stuff. + */ + struct Vertex{ + glm::vec3 Position; + glm::vec3 Color; + glm::vec3 Normals; + glm::vec2 Uv; + + bool operator==(const Vertex& other) const{ + return (Position == other.Position and Color == other.Color and Normals == other.Normals and Uv == other.Uv); + } + }; + + class VertexBuffer{ + public: + virtual ~VertexBuffer() = default; + + static Ref Create(const std::vector& p_Vertices); + static Ref CreateVertexVector(const std::vector& p_Vertices, std::vector p_Colors); + static Ref CreateVertexVector(const std::vector& p_Vertices); + + void Bind(VkCommandBuffer p_CommandBuffer); + + void Draw(VkCommandBuffer p_Command); + + protected: + virtual void BindVertexBuffer(VkCommandBuffer p_CommandBuffer) = 0; + virtual void RenderVertexBuffer(VkCommandBuffer p_CommandBuffer) = 0; + }; + + // vector> +}; \ No newline at end of file diff --git a/engine3d/Core/Renderer/Renderer.hpp b/engine3d/Core/Renderer/Renderer.hpp index 8c76a54..4e3a83a 100644 --- a/engine3d/Core/Renderer/Renderer.hpp +++ b/engine3d/Core/Renderer/Renderer.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include @@ -16,8 +15,6 @@ namespace engine3d{ //! @note Currently the record command buffers just does what "simple_render_system.hpp/.cpp" does already //! @note In the future I'll add API's for submitting draw calls. - // static void RecordCommandBuffers(VkCommandBuffer p_CommandBuffer); - static void RecordGameObjects(std::vector& p_Objects); static void RecordSceneGameObjects(std::vector& p_SceneObjects, SceneObject* p_CameraObject); static void EndFrame(); diff --git a/engine3d/Core/Scene/SceneObject.hpp b/engine3d/Core/Scene/SceneObject.hpp deleted file mode 100644 index ddc20b9..0000000 --- a/engine3d/Core/Scene/SceneObject.hpp +++ /dev/null @@ -1,75 +0,0 @@ -#pragma once -#include -#include -// #include - -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include -#include -#include - -namespace engine3d{ - //! @note Creating a transform component for our scene object - struct Transform2DComponent{ - glm::vec3 Translation{}; - glm::vec3 scale = {1.f, 1.f, 1.f}; - glm::vec3 rotation; - - glm::mat4 mat4(){ - auto transform = glm::translate(glm::mat4{1.f}, Translation); - transform = glm::rotate(transform, rotation.y, {0.f, 1.f, 0.f}); - transform = glm::rotate(transform, rotation.x, {1.f, 0.f, 0.f}); - transform = glm::rotate(transform, rotation.z, {0.f, 0.f, 1.f}); - - transform = glm::scale(transform, scale); - return transform; - } - }; - - struct RigidBody2dComponent { - glm::vec2 velocity; - float mass{1.0f}; - }; - - //! @note Defines what our scene objects'll be. - class SceneObject{ - using id_t = unsigned int; - public: - static SceneObject Create(); - - id_t GetID() const { return m_Id; } - - void SetModel(Ref& p_Model){ - m_Model = p_Model; - } - - void SetRotation(glm::vec3& p_Rotation){ - m_Transform2D.rotation = p_Rotation; - } - - void SetColor(glm::vec3 p_Color){ - m_Color = p_Color; - } - - void SetTranslation(Transform2DComponent p_Transformation){ - m_Transform2D = p_Transformation; - } - - Ref& GetModel() { return m_Model; } - glm::vec3 GetColor() { return m_Color; } - Transform2DComponent& GetTransform() { return m_Transform2D; } - - Transform2DComponent m_Transform2D; - RigidBody2dComponent RigidBodyComponent; - - private: - SceneObject(id_t p_Id); - - private: - Ref m_Model; - glm::vec3 m_Color; - - id_t m_Id; - }; -}; \ No newline at end of file diff --git a/engine3d/Core/Scene/SceneTest.hpp b/engine3d/Core/Scene/SceneTest.hpp deleted file mode 100644 index 3d4f7d3..0000000 --- a/engine3d/Core/Scene/SceneTest.hpp +++ /dev/null @@ -1,120 +0,0 @@ -#pragma once -#include -#include -#include - -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include -#include -#include - -// #include - -namespace engine3d{ - - //! @note Defines what our scene objects'll be. - class SceneObjectTutorial{ - using id_t = unsigned int; - public: - //! @note Creating a transform component for our scene object - struct Transform2DComponent{ - glm::vec3 Position{}; - glm::vec3 scale = {1.f, 1.f, 1.f}; - glm::vec3 rotation; - - glm::mat4 mat4(){ - auto transform = glm::translate(glm::mat4{1.f}, Position); - transform = glm::rotate(transform, rotation.y, {0.f, 1.f, 0.f}); - transform = glm::rotate(transform, rotation.x, {1.f, 0.f, 0.f}); - transform = glm::rotate(transform, rotation.z, {0.f, 0.f, 1.f}); - - transform = glm::scale(transform, scale); - return transform; - } - }; - - struct RigidBody2dComponent { - glm::vec2 velocity; - float mass{1.0f}; - }; - static SceneObjectTutorial Create(); - - id_t GetID() const { return m_Id; } - - void SetModel(Ref& p_Model){ - m_Model = p_Model; - } - - void SetRotation(glm::vec3& p_Rotation){ - m_Transform2D.rotation = p_Rotation; - } - - void SetColor(glm::vec3 p_Color){ - m_Color = p_Color; - } - - void SetTranslation(Transform2DComponent p_Transformation){ - m_Transform2D = p_Transformation; - } - - Ref& GetModel() { return m_Model; } - glm::vec3 GetColor() { return m_Color; } - Transform2DComponent& GetTransform() { return m_Transform2D; } - - Transform2DComponent m_Transform2D; - RigidBody2dComponent RigidBodyComponent; - - private: - SceneObjectTutorial(id_t p_Id); - - private: - Ref m_Model; - glm::vec3 m_Color; - - id_t m_Id; - }; - - // class SceneObjectTutorial2{ - // using id_t = unsigned int; - // public: - // static SceneObjectTutorial2 Create(Scene* p_Scene); - - // id_t GetID() const { return m_Id; } - - // void SetModel(Ref& p_Model){ - // m_Model = p_Model; - // } - - // void SetRotation(glm::vec3& p_Rotation){ - // m_Transform2D.rotation = p_Rotation; - // } - - // void SetColor(glm::vec3 p_Color){ - // m_Color = p_Color; - // } - - // void SetTranslation(Transform2DComponent p_Transformation){ - // m_Transform2D = p_Transformation; - // } - - // Ref& GetModel() { return m_Model; } - // glm::vec3 GetColor() { return m_Color; } - // Transform2DComponent& GetTransform() { return m_Transform2D; } - - // Transform2DComponent m_Transform2D; - // RigidBody2dComponent RigidBodyComponent; - - - - - // private: - // SceneObjectTutorial2(id_t p_Id, Scene* p_Scene); - - // private: - // Ref m_Model; - // glm::vec3 m_Color; - // Scene* m_Scene; - // id_t m_Id; - // }; -}; \ No newline at end of file diff --git a/engine3d/Core/SceneManagment/SceneObjects/SceneObject.hpp b/engine3d/Core/SceneManagment/SceneObjects/SceneObject.hpp index 91e7e6b..cabe52f 100644 --- a/engine3d/Core/SceneManagment/SceneObjects/SceneObject.hpp +++ b/engine3d/Core/SceneManagment/SceneObjects/SceneObject.hpp @@ -5,7 +5,7 @@ * @param id - the ID of the SceneObject itself * @note actual wrapper for SceneObject for ECS * */ -#include +#include #include #include @@ -69,13 +69,14 @@ namespace engine3d m_ParentScene->m_SceneRegistry.remove(SceneObjectHandler); } - void SetModal(Ref& p_Model){ + void SetMesh(Mesh& p_Model){ m_Model = p_Model; } + glm::mat4 toMat4(); - Ref& GetModel() { return m_Model; } + Mesh& GetMesh() { return m_Model; } operator bool() const { return SceneObjectHandler != entt::null; } @@ -102,6 +103,6 @@ namespace engine3d entt::entity SceneObjectHandler{entt::null}; UUID objectID; Scene *m_ParentScene = nullptr; // 12 bytes - Ref m_Model; + Mesh m_Model; }; }; // namespace Engine3D diff --git a/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.hpp b/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.hpp index 4dbde26..bc77f5e 100644 --- a/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.hpp +++ b/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.hpp @@ -24,6 +24,8 @@ namespace engine3d::vk{ private: VkPipeline GraphicsPipeline() override { return m_GraphicsPipeline; } private: + std::vector GetVertexInputBindDescription(); + std::vector GetVertexAttributeDescriptions(); std::vector read_file(const std::string& p_Filepath); diff --git a/engine3d/Core/internal/Vulkan2Showcase/VulkanIndexBuffer.hpp b/engine3d/Core/internal/Vulkan2Showcase/VulkanIndexBuffer.hpp new file mode 100644 index 0000000..7bf13a9 --- /dev/null +++ b/engine3d/Core/internal/Vulkan2Showcase/VulkanIndexBuffer.hpp @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +namespace engine3d::vk{ + class VulkanIndexBuffer : public IndexBuffer { + public: + VulkanIndexBuffer(std::vector& p_Indices); + + private: + void BindToIndexBuffer(VkCommandBuffer p_CommandBuffer) override; + void RenderIndexBuffer(VkCommandBuffer p_CommandBuffer) override; + bool HasIndices() const override; + + private: + VkBuffer m_IndexBufferHandler; + VkDeviceMemory m_IndexDeviceBufferMemory; + uint32_t m_IndicesCount; + bool m_HasIndices = false; + }; +}; \ No newline at end of file diff --git a/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp b/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp deleted file mode 100644 index daf1ccc..0000000 --- a/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once -#include -#include -#include - -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include - -namespace engine3d::vk{ - /** - * @name VulkanModel - * @note For now I am going to have each model contain vertices, indices, and so forth. - * @note And may separate the vertex/index/storage buffers as their own thing but for the time being, just gonna have them be under this one class - */ - class VulkanModel{ - public: - //! @note Can be the way we structure what data types is accepted in our shader. - struct Vertex{ - glm::vec3 Position; - glm::vec3 Color; - - static std::vector GetVertexInputBindDescription(); - static std::vector GetVertexAttributeDescriptions(); - }; - - VulkanModel() = default; - VulkanModel(const std::vector& p_Vertices, const VkBufferUsageFlags& p_UsageFlags, const VkMemoryPropertyFlags& p_PropertyFlags); - - - void Bind(VkCommandBuffer p_Command); - void Draw(VkCommandBuffer p_Command); - - static std::vector* CreateVertexVector(std::vector p_Vertices, std::vector p_Colors); - static std::vector* CreateVertexVector(std::vector p_Vertices); - - private: - void initialize_vertex_buffers(const std::vector& p_Vertices); - - private: - VkBuffer m_VertexBuffer; - VkDeviceMemory m_VertexBufferDeviceMemory; - uint32_t m_VerticesCount; - }; -}; \ No newline at end of file diff --git a/engine3d/Core/internal/Vulkan2Showcase/VulkanVertexBuffer.hpp b/engine3d/Core/internal/Vulkan2Showcase/VulkanVertexBuffer.hpp new file mode 100644 index 0000000..ee51433 --- /dev/null +++ b/engine3d/Core/internal/Vulkan2Showcase/VulkanVertexBuffer.hpp @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +namespace engine3d::vk{ + class VulkanVertexBuffer : public VertexBuffer{ + public: + VulkanVertexBuffer(const std::vector& p_Vertices); + + protected: + + void BindVertexBuffer(VkCommandBuffer p_CommandBuffer) override; + + void RenderVertexBuffer(VkCommandBuffer p_CommandBuffer) override; + + private: + VkBuffer m_VertexBufferHandler; + VkDeviceMemory m_VertexBufferDeviceMemoryHandler; + uint32_t m_VerticesCount; + }; +}; \ No newline at end of file diff --git a/engine3d/Core/utils/types.hpp b/engine3d/Core/utils/types.hpp new file mode 100644 index 0000000..9b9b055 --- /dev/null +++ b/engine3d/Core/utils/types.hpp @@ -0,0 +1,12 @@ +#pragma once +#include +#include + +namespace engine3d{ + template + void HashCombine(size_t& seed, const T& v, const Rest&... rest){ + seed ^= std::hash()(v) + 0x9e3779b9 + (seed << 6) + (seed << 2); + (HashCombine(seed, rest), ...); + } + +}; \ No newline at end of file diff --git a/engine3d/Physics/Interfaces/ObjectLayerPairFilterInterface.hpp b/engine3d/Physics/Interfaces/ObjectLayerPairFilterInterface.hpp index 7b83b1b..b13e490 100644 --- a/engine3d/Physics/Interfaces/ObjectLayerPairFilterInterface.hpp +++ b/engine3d/Physics/Interfaces/ObjectLayerPairFilterInterface.hpp @@ -5,6 +5,7 @@ namespace engine3d class ObjectLayerPairFilterInterface : public JPH::ObjectLayerPairFilter { public: + //! @note TODO: After showcase we should fix this, because doesnt rely on child sub-classes. virtual bool ShouldCollide(JPH::ObjectLayer inObject1, JPH::ObjectLayer inObject2) const override { switch (inObject1) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2fdd892..8c4cdcf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -33,12 +33,13 @@ add_library( ${ENGINE_INCLUDE_NAME}/Core/Event/KeyEvent.hpp ${ENGINE_INCLUDE_NAME}/Core/Event/MouseEvent.hpp - ${ENGINE_INCLUDE_NAME}/Core/Scene/SceneObject.hpp - - - ${ENGINE_INCLUDE_NAME}/Core/GraphicDrivers/GraphicSwapchain.hpp ${ENGINE_INCLUDE_NAME}/Core/GraphicDrivers/Shader.hpp + ${ENGINE_INCLUDE_NAME}/Core/GraphicDrivers/VertexBuffer.hpp + ${ENGINE_INCLUDE_NAME}/Core/GraphicDrivers/IndexBuffer.hpp + + # Mesh stuff + ${ENGINE_INCLUDE_NAME}/Core/GraphicDrivers/Mesh.hpp ${VULKAN_INCLUDE_DIR}/Vulkan.hpp ${VULKAN_INCLUDE_DIR}/helper_functions.hpp @@ -47,7 +48,8 @@ add_library( ${VULKAN_INCLUDE_DIR}/VulkanPhysicalDriver.hpp ${VULKAN_INCLUDE_DIR}/VulkanDriver.hpp ${VULKAN_INCLUDE_DIR}/VulkanSwapchain.hpp - ${VULKAN_INCLUDE_DIR}/VulkanModel.hpp + ${VULKAN_INCLUDE_DIR}/VulkanVertexBuffer.hpp + ${VULKAN_INCLUDE_DIR}/VulkanIndexBuffer.hpp ${VULKAN_SHADERS_INCLUDE_DIR}/VulkanShader.hpp @@ -85,10 +87,13 @@ add_library( ${ENGINE_SRC_DIR}/Core/ApplicationManager/Scene.cpp ${ENGINE_SRC_DIR}/Core/ApplicationManager/ThreadMngr.cpp - ${ENGINE_SRC_DIR}/Core/Scene/SceneObject.cpp - ${ENGINE_SRC_DIR}/Core/GraphicDrivers/GraphicSwapchain.cpp ${ENGINE_SRC_DIR}/Core/GraphicDrivers/Shader.cpp + ${ENGINE_SRC_DIR}/Core/GraphicDrivers/VertexBuffer.cpp + ${ENGINE_SRC_DIR}/Core/GraphicDrivers/IndexBuffer.cpp + + # Mesh stuff + ${ENGINE_SRC_DIR}/Core/GraphicDrivers/Mesh.cpp ${ENGINE_SRC_DIR}/Core/Event/InputPoll.cpp @@ -98,9 +103,10 @@ add_library( ${VULKAN_SRC_DIR}/VulkanPhysicalDriver.cpp ${VULKAN_SRC_DIR}/VulkanDriver.cpp ${VULKAN_SRC_DIR}/VulkanSwapchain.cpp + ${VULKAN_SRC_DIR}/VulkanVertexBuffer.cpp + ${VULKAN_SRC_DIR}/VulkanIndexBuffer.cpp ${VULKAN_SHADERS_SRC_DIR}/VulkanShader.cpp - ${VULKAN_SRC_DIR}/VulkanModel.cpp ${ENGINE_SRC_DIR}/Core/SceneManagment/SceneObjects/SceneObject.cpp ${ENGINE_SRC_DIR}/Core/SceneManagment/Components/GameComponent.cpp diff --git a/src/engine3d/Core/ApplicationInstance.cpp b/src/engine3d/Core/ApplicationInstance.cpp index 03b990d..2cdbd5f 100644 --- a/src/engine3d/Core/ApplicationInstance.cpp +++ b/src/engine3d/Core/ApplicationInstance.cpp @@ -6,6 +6,9 @@ #include #include +// #include +#include + namespace engine3d{ static float m_LastFrameTime = 0.0f; static std::string g_DebugName = "Engine3D"; @@ -19,6 +22,8 @@ namespace engine3d{ SetCurrentAPI(VULKAN); m_Window = Window::Create(900, 600, p_DebugName); + // ImGui_ImplVulkanH_Window(); + // ImGui_ImplGlfw_InitForVulkan(ApplicationInstance::GetWindow().GetNativeWindow(), true); Renderer::Initialize("Engine3D Renderer"); } diff --git a/src/engine3d/Core/GraphicDrivers/IndexBuffer.cpp b/src/engine3d/Core/GraphicDrivers/IndexBuffer.cpp new file mode 100644 index 0000000..d42e200 --- /dev/null +++ b/src/engine3d/Core/GraphicDrivers/IndexBuffer.cpp @@ -0,0 +1,26 @@ +#include "ApplicationInstance.hpp" +#include +#include + +namespace engine3d{ + Ref IndexBuffer::Create(std::vector p_Indices){ + switch (ApplicationInstance::CurrentAPI()){ + case API::VULKAN: return CreateRef(p_Indices); + default: return nullptr; + } + + return nullptr; + } + + void IndexBuffer::Bind(VkCommandBuffer p_CommandBuffer){ + return BindToIndexBuffer(p_CommandBuffer); + } + + void IndexBuffer::Draw(VkCommandBuffer p_CommandBuffer){ + return RenderIndexBuffer(p_CommandBuffer); + } + + bool IndexBuffer::HasIndicesPresent() const{ + return HasIndices(); + } +}; \ No newline at end of file diff --git a/src/engine3d/Core/GraphicDrivers/Mesh.cpp b/src/engine3d/Core/GraphicDrivers/Mesh.cpp new file mode 100644 index 0000000..10c8cad --- /dev/null +++ b/src/engine3d/Core/GraphicDrivers/Mesh.cpp @@ -0,0 +1,98 @@ +#include "EngineLogger.hpp" +#include "GraphicDrivers/VertexBuffer.hpp" +#include +#include + +#include + +#define GLM_ENABLE_EXPERIMENTAL +#include + +namespace std{ + template<> + struct hash{ + size_t operator()(const engine3d::Vertex& vertex) const { + size_t seed = 0; + engine3d::HashCombine(seed, vertex.Position, vertex.Color, vertex.Normals, vertex.Uv); + return seed; + } + }; + +}; + +namespace engine3d{ + Mesh::Mesh(const Ref& p_Vb, const Ref& p_Ib){ + m_Vertices = p_Vb; + m_Indices = p_Ib; + } + + Mesh Mesh::LoadModel(const std::string& p_Filename){ + tinyobj::attrib_t attrib; + std::vector shapes; + std::vector materials; + std::string warn, err; + + if(!tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, p_Filename.c_str())){ + ConsoleLogWarn("Could not Load Mesh!! In Mesh::LoadModel!"); + } + + std::vector vertices; + // std::vector indices; + std::vector indices; + std::unordered_map unique_vertices{}; + + for(const auto& shape : shapes){ + for(const auto& index : shape.mesh.indices){ + Vertex vertex{}; + + if(index.vertex_index >= 0){ + vertex.Position = { + attrib.vertices[3 * index.vertex_index + 0], + attrib.vertices[3 * index.vertex_index + 1], + attrib.vertices[3 * index.vertex_index + 2] + }; + + //! @note Setting the colors + auto color_idx = 3 * index.vertex_index + 2; + if(color_idx < attrib.colors.size()){ + vertex.Color = { + attrib.colors[color_idx - 2], + attrib.colors[color_idx - 1], + attrib.colors[color_idx - 0] + }; + } + else{ + vertex.Color = {0.f, 0.f, 0.f}; + } + } + + if(index.normal_index >= 0){ + vertex.Normals = { + attrib.normals[3 * index.normal_index + 0], + attrib.normals[3 * index.normal_index + 1], + attrib.normals[3 * index.normal_index + 2] + }; + } + + if(index.texcoord_index >= 0){ + vertex.Uv = { + attrib.texcoords[2 * index.texcoord_index + 0], + attrib.texcoords[2 * index.texcoord_index + 1] + }; + } + + + // vertices.push_back(vertex); + if(unique_vertices.contains(vertex) == 0){ + unique_vertices[vertex] = static_cast(vertices.size()); + vertices.push_back(vertex); + } + + indices.push_back(unique_vertices[vertex]); + } + } + Ref vb = VertexBuffer::Create(vertices); + Ref ib = IndexBuffer::Create(indices); + return Mesh(vb, ib); + } +}; \ No newline at end of file diff --git a/src/engine3d/Core/GraphicDrivers/VertexBuffer.cpp b/src/engine3d/Core/GraphicDrivers/VertexBuffer.cpp new file mode 100644 index 0000000..5fd74a1 --- /dev/null +++ b/src/engine3d/Core/GraphicDrivers/VertexBuffer.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +namespace engine3d{ + Ref VertexBuffer::Create(const std::vector& p_Vertices){ + switch (ApplicationInstance::CurrentAPI()){ + case API::VULKAN: return CreateRef(p_Vertices); + default: return nullptr; + } + + return nullptr; + } + + Ref VertexBuffer::CreateVertexVector(const std::vector& p_Vertices, std::vector p_Colors){ + return nullptr; + } + + Ref VertexBuffer::CreateVertexVector(const std::vector& p_Vertices){ + return nullptr; + } + + void VertexBuffer::Bind(VkCommandBuffer p_CommandBuffer){ + BindVertexBuffer(p_CommandBuffer); + } + + void VertexBuffer::Draw(VkCommandBuffer p_CommandBuffer){ + RenderVertexBuffer(p_CommandBuffer); + } + +}; \ No newline at end of file diff --git a/src/engine3d/Core/Renderer/Renderer.cpp b/src/engine3d/Core/Renderer/Renderer.cpp index 999beb7..dfbfcba 100644 --- a/src/engine3d/Core/Renderer/Renderer.cpp +++ b/src/engine3d/Core/Renderer/Renderer.cpp @@ -1,10 +1,8 @@ -// #include "internal/VulkanCpp/Vulkan.hpp" -#include "EngineLogger.hpp" -#include "GraphicDrivers/Shader.hpp" -#include "TimeManagement/UpdateManagers/SyncUpdateManager.hpp" +#include +#include +#include #include "internal/Vulkan2Showcase/Shaders/VulkanShader.hpp" #include "internal/Vulkan2Showcase/VulkanContext.hpp" -#include "internal/Vulkan2Showcase/VulkanModel.hpp" #include "internal/Vulkan2Showcase/helper_functions.hpp" #include #include @@ -161,36 +159,6 @@ namespace engine3d{ } - void Renderer::RecordGameObjects(std::vector& p_Objects){ - auto current_cmd_buffer = GetCurrentCommandBuffer(); - //! @note Essentially doing m_Pipeline->Bind(m_CommandBuffer[i]) - //! @note Starts when to start rendering!! - vkCmdBindPipeline(current_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Shader->GetGraphicsPipeline()); - - //! @note Only for testing purposes for mesh data. - for(auto& obj : p_Objects){ - obj.m_Transform2D.rotation.y = glm::mod(obj.GetTransform().rotation.y + 0.001f, glm::two_pi()); - obj.m_Transform2D.rotation.x = glm::mod(obj.GetTransform().rotation.x + 0.001f, glm::two_pi()); - - SimplePushConstantData push = { - .Transform = obj.GetTransform().mat4(), - .iResolution = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()}, - .Color = obj.GetColor(), - }; - vkCmdPushConstants( - current_cmd_buffer, - g_PipelineLayout, - VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, - 0, - sizeof(SimplePushConstantData), - &push - ); - - obj.GetModel()->Bind(current_cmd_buffer); - obj.GetModel()->Draw(current_cmd_buffer); - } - } - void Renderer::RecordSceneGameObjects(std::vector& p_Objects, SceneObject* p_CameraObject){ auto current_cmd_buffer = GetCurrentCommandBuffer(); @@ -233,8 +201,33 @@ namespace engine3d{ &push ); - obj->GetModel()->Bind(current_cmd_buffer); - obj->GetModel()->Draw(current_cmd_buffer); + // obj->GetModel()->Bind(current_cmd_buffer); + // obj->GetModel()->Draw(current_cmd_buffer); + auto& vb = obj->GetMesh().GetVertices(); + auto ib = obj->GetMesh().GetIndices(); + vb->Bind(current_cmd_buffer); + // ib->Bind(current_cmd_buffer); + + // if(ib->HasIndicesPresent()){ + // ib->Draw(current_cmd_buffer); + // } + // else{ + // vb->Draw(current_cmd_buffer); + // } + + if(ib != nullptr){ + ib->Bind(current_cmd_buffer); + if(ib->HasIndicesPresent()){ + ib->Draw(GetCurrentCommandBuffer()); + } + else{ + vb->Draw(GetCurrentCommandBuffer()); + } + } + else{ + vb->Draw(GetCurrentCommandBuffer()); + } + } } diff --git a/src/engine3d/Core/Scene/SceneObject.cpp b/src/engine3d/Core/Scene/SceneObject.cpp deleted file mode 100644 index fcdc738..0000000 --- a/src/engine3d/Core/Scene/SceneObject.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include - -namespace engine3d{ - SceneObject SceneObject::Create(){ - static id_t current_id = 0; - return SceneObject(current_id); - } - - SceneObject::SceneObject(id_t p_Id) : m_Id(p_Id) {} -}; \ No newline at end of file diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.cpp index dd56e58..ee91c4e 100644 --- a/src/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.cpp +++ b/src/engine3d/Core/internal/Vulkan2Showcase/Shaders/VulkanShader.cpp @@ -3,7 +3,8 @@ #include #include #include -#include +// #include +#include #include #include @@ -61,8 +62,8 @@ namespace engine3d::vk{ shader_stages[0] = vert_shader_stage; shader_stages[1] = frag_shader_stage; - auto binding_description = VulkanModel::Vertex::GetVertexInputBindDescription(); - auto attachment_description = VulkanModel::Vertex::GetVertexAttributeDescriptions(); + auto binding_description = GetVertexInputBindDescription(); + auto attachment_description = GetVertexAttributeDescriptions(); VkPipelineVertexInputStateCreateInfo vert_input_create_info = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, @@ -108,6 +109,37 @@ namespace engine3d::vk{ ConsoleLogWarn("Fragment Shader Size == {}", frag.size()); } + std::vector VulkanShader::GetVertexInputBindDescription(){ + std::vector binding_descriptions(1); + binding_descriptions[0] = { + .binding = 0, + .stride = sizeof(Vertex), + .inputRate = VK_VERTEX_INPUT_RATE_VERTEX + }; + + return binding_descriptions; + } + + std::vector VulkanShader::GetVertexAttributeDescriptions(){ + std::vector attribute_description(2); + attribute_description[0] = { + .location = 0, // // layout(location = 0) + .binding = 0, + .format = VK_FORMAT_R32G32B32_SFLOAT, + // .offset = 0 + .offset = offsetof(Vertex, Position) + }; + + attribute_description[1] = { + .location = 1, // layout(location = 1) + .binding = 0, + .format = VK_FORMAT_R32G32B32_SFLOAT, + .offset = offsetof(Vertex, Color) + }; + + return attribute_description; + } + void VulkanShader::initialize_shader_module(const std::vector& p_Bin, VkShaderModule& p_ShaderMod){ VkShaderModuleCreateInfo shader_mod_create_info = { .sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanIndexBuffer.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanIndexBuffer.cpp new file mode 100644 index 0000000..b3a50d4 --- /dev/null +++ b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanIndexBuffer.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include + +namespace engine3d::vk{ + VulkanIndexBuffer::VulkanIndexBuffer(std::vector& p_Indices){ + ConsoleLogInfo("Vulkan2Showcase: Begin Vulkan VertexBuffer Initialization!!"); + m_IndicesCount = static_cast(p_Indices.size()); + + m_HasIndices = (m_IndicesCount > 0); + + if(!m_HasIndices){ + ConsoleLogWarn("Cannot provide empty indices to the VulkanIndexBuffer!"); + return; + } + + VkDeviceSize buffer_size = sizeof(p_Indices[0]) * m_IndicesCount; + VkBufferUsageFlags usage_flags = VK_BUFFER_USAGE_INDEX_BUFFER_BIT; + VkMemoryPropertyFlags property_flags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + + VkBufferCreateInfo vk_buffer_create_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .size = buffer_size, + .usage = usage_flags, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE + }; + + vk_check(vkCreateBuffer(VulkanContext::GetDriver(), &vk_buffer_create_info, nullptr, &m_IndexBufferHandler), "vkCreateBuffer", __FILE__, __LINE__, __FUNCTION__); + + VkMemoryRequirements memory_reqs; + vkGetBufferMemoryRequirements(VulkanContext::GetDriver(), m_IndexBufferHandler, &memory_reqs); + + VkMemoryAllocateInfo alloc_info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = memory_reqs.size, + .memoryTypeIndex = VulkanContext::GetDriver().SelectMemoryType(memory_reqs.memoryTypeBits, property_flags) + }; + + vk_check(vkAllocateMemory(VulkanContext::GetDriver(), &alloc_info, nullptr, &m_IndexDeviceBufferMemory), "vkAllocateMemory", __FILE__, __LINE__, __FUNCTION__); + + vkBindBufferMemory(VulkanContext::GetDriver(), m_IndexBufferHandler, m_IndexDeviceBufferMemory, 0); + + //! @note Mapping memory data. + //! @note THIS is how we map our vertices data to our VkBuffer (essentially it is our vertex buffer) + + /** + @note Purpose of these buffer objects are to write these information to memory. + @note This maps memory on our host to the device's memory. + @note Where this creates a data region on the host, that is linked to the device memory + @note This way when we used memcpy to write to the CPU's accessible memory. That information will also be flushed to the GPU's memory, by this link + @note Then once this information has been mapped, then we no longer need the data on the host's side, therefore unmapping that information. + */ + void* data; + vkMapMemory(VulkanContext::GetDriver(), m_IndexDeviceBufferMemory, 0, buffer_size, 0, &data); + memcpy(data, p_Indices.data(), static_cast(buffer_size)); + vkUnmapMemory(VulkanContext::GetDriver(), m_IndexDeviceBufferMemory); + + ConsoleLogWarn("Vulkan2Showcase: Vulkan VertexBuffer Initialized Completed!!!"); + } + + void VulkanIndexBuffer::BindToIndexBuffer(VkCommandBuffer p_CommandBuffer) { + VkBuffer buffers[] = {m_IndexBufferHandler}; + VkDeviceSize offsets[] = {0}; + + if(m_HasIndices){ + vkCmdBindIndexBuffer(p_CommandBuffer, m_IndexBufferHandler, 0, VK_INDEX_TYPE_UINT32); + } + } + + void VulkanIndexBuffer::RenderIndexBuffer(VkCommandBuffer p_CommandBuffer) { + /** + //! @note This will happen within Renderer itself, but not here nor VertexBuffer class. + if(m_HasIndices){ + vkCmdDrawIndexed(p_CommandBuffer, m_IndicesCount, 1, 0, 0, 0); + } + else{ + vkCmdDraw(p_CommandBuffer, m_VerticesCount, 1, 0, 0); + } + */ + + vkCmdDrawIndexed(p_CommandBuffer, m_IndicesCount, 1, 0, 0, 0); + } + + bool VulkanIndexBuffer::HasIndices() const{ + return m_HasIndices; + } +}; \ No newline at end of file diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp deleted file mode 100644 index c99705c..0000000 --- a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp +++ /dev/null @@ -1,137 +0,0 @@ -#include "EngineLogger.hpp" -#include -#include -#include -#include -#include - -namespace engine3d::vk{ - VulkanModel::VulkanModel(const std::vector& p_Vertices, const VkBufferUsageFlags& p_UsageFlags, const VkMemoryPropertyFlags& p_PropertyFlags){ - ConsoleLogInfo("Vulkan2Showcase: Begin Vulkan Model Initialization!!"); - m_VerticesCount = static_cast(p_Vertices.size()); - VkDeviceSize buffer_size = sizeof(p_Vertices[0]) * m_VerticesCount; - - //! @note Host = CPU, Device = GPU - /* - - createBuffer( - VkDeviceSize: VCbufferSize, - VkBufferUsageFlags: VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - VkBuffer: vertexBuffer, - VkDeviceMemory: vertexDeviceMemory - ); - - */ - - //! @note Initialize Vertex Buffer - //! @not eDoes initialize_vertex_buffers(vertices) stuff - VkBufferCreateInfo vk_buf_create_info = { - .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, - .pNext = nullptr, - .size = buffer_size, - .usage = p_UsageFlags, - .sharingMode = VK_SHARING_MODE_EXCLUSIVE - }; - - vk_check(vkCreateBuffer(VulkanContext::GetDriver(), &vk_buf_create_info, nullptr, &m_VertexBuffer), "vkCreateBuffer", __FILE__, __LINE__, __FUNCTION__); - - VkMemoryRequirements memory_reqs; - vkGetBufferMemoryRequirements(VulkanContext::GetDriver(), m_VertexBuffer, &memory_reqs); - - VkMemoryAllocateInfo alloc_info = { - .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, - .allocationSize = memory_reqs.size, - .memoryTypeIndex = VulkanContext::GetDriver().SelectMemoryType(memory_reqs.memoryTypeBits, p_PropertyFlags) - }; - - vk_check(vkAllocateMemory(VulkanContext::GetDriver(), &alloc_info, nullptr, &m_VertexBufferDeviceMemory), "vkAllocateMemory", __FILE__, __LINE__, __FUNCTION__); - - vkBindBufferMemory(VulkanContext::GetDriver(), m_VertexBuffer, m_VertexBufferDeviceMemory, 0); - - //! @note Mapping memory data. - //! @note THIS is how we map our vertices data to our VkBuffer (essentially it is our vertex buffer) - void* data; - vkMapMemory(VulkanContext::GetDriver(), m_VertexBufferDeviceMemory, 0, buffer_size, 0, &data); - memcpy(data, p_Vertices.data(), static_cast(buffer_size)); - vkUnmapMemory(VulkanContext::GetDriver(), m_VertexBufferDeviceMemory); - - - ConsoleLogWarn("Vulkan2Showcase: Vulkan Model Initialized Completed!!!"); - - // initialize_vertex_buffers(p_Vertices); - - } - - void VulkanModel::initialize_vertex_buffers(const std::vector& p_Vertices){ - - } - - void VulkanModel::Draw(VkCommandBuffer p_Command){ - vkCmdDraw(p_Command, m_VerticesCount, 1, 0, 0); - } - - void VulkanModel::Bind(VkCommandBuffer p_Command){ - VkBuffer buffers[] = { m_VertexBuffer}; - VkDeviceSize offsets[] = {0}; - vkCmdBindVertexBuffers(p_Command, 0, 1, buffers, offsets); - } - - std::vector VulkanModel::Vertex::GetVertexInputBindDescription(){ - std::vector binding_descriptions(1); - binding_descriptions[0] = { - .binding = 0, - .stride = sizeof(Vertex), - .inputRate = VK_VERTEX_INPUT_RATE_VERTEX - }; - - return binding_descriptions; - } - - std::vector VulkanModel::Vertex::GetVertexAttributeDescriptions(){ - std::vector attribute_description(2); - attribute_description[0] = { - .location = 0, // // layout(location = 0) - .binding = 0, - .format = VK_FORMAT_R32G32B32_SFLOAT, - // .offset = 0 - .offset = offsetof(Vertex, Position) - }; - - attribute_description[1] = { - .location = 1, // layout(location = 1) - .binding = 0, - .format = VK_FORMAT_R32G32B32_SFLOAT, - .offset = offsetof(Vertex, Color) - }; - - return attribute_description; - } - - std::vector* VulkanModel::CreateVertexVector - (std::vector p_Vertices, std::vector p_Colors) - { - std::vector* temp = new std::vector; - int i = 0; - for(auto verticies : p_Vertices) - { - //! if does not work vertex needs a constructor - temp[i].push_back(VulkanModel::Vertex(p_Vertices[i], p_Colors[i])); - i++; - } - return temp; - } - - std::vector* VulkanModel::CreateVertexVector - (std::vector p_Vertices) - { - std::vector* temp = new std::vector; - int i = 0; - for(auto verticies : p_Vertices) - { - //! if does not work vertex needs a constructor - temp[i].push_back(VulkanModel::Vertex(p_Vertices[i], {1.0f,1.0f,1.0f})); - i++; - } - return temp; - } -}; \ No newline at end of file diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanVertexBuffer.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanVertexBuffer.cpp new file mode 100644 index 0000000..a25e064 --- /dev/null +++ b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanVertexBuffer.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +namespace engine3d::vk{ + VulkanVertexBuffer::VulkanVertexBuffer(const std::vector& p_Vertices){ + ConsoleLogInfo("Vulkan2Showcase: Begin Vulkan VertexBuffer Initialization!!"); + m_VerticesCount = static_cast(p_Vertices.size()); + VkDeviceSize buffer_size = sizeof(p_Vertices[0]) * m_VerticesCount; + VkBufferUsageFlags usage_flags = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; + VkMemoryPropertyFlags property_flags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; + + VkBufferCreateInfo vk_buffer_create_info = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .size = buffer_size, + .usage = usage_flags, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE + }; + + vk_check(vkCreateBuffer(VulkanContext::GetDriver(), &vk_buffer_create_info, nullptr, &m_VertexBufferHandler), "vkCreateBuffer", __FILE__, __LINE__, __FUNCTION__); + + VkMemoryRequirements memory_reqs; + vkGetBufferMemoryRequirements(VulkanContext::GetDriver(), m_VertexBufferHandler, &memory_reqs); + + VkMemoryAllocateInfo alloc_info = { + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .allocationSize = memory_reqs.size, + .memoryTypeIndex = VulkanContext::GetDriver().SelectMemoryType(memory_reqs.memoryTypeBits, property_flags) + }; + + vk_check(vkAllocateMemory(VulkanContext::GetDriver(), &alloc_info, nullptr, &m_VertexBufferDeviceMemoryHandler), "vkAllocateMemory", __FILE__, __LINE__, __FUNCTION__); + + vkBindBufferMemory(VulkanContext::GetDriver(), m_VertexBufferHandler, m_VertexBufferDeviceMemoryHandler, 0); + + //! @note Mapping memory data. + //! @note THIS is how we map our vertices data to our VkBuffer (essentially it is our vertex buffer) + void* data; + vkMapMemory(VulkanContext::GetDriver(), m_VertexBufferDeviceMemoryHandler, 0, buffer_size, 0, &data); + memcpy(data, p_Vertices.data(), static_cast(buffer_size)); + vkUnmapMemory(VulkanContext::GetDriver(), m_VertexBufferDeviceMemoryHandler); + + ConsoleLogWarn("Vulkan2Showcase: Vulkan VertexBuffer Initialized Completed!!!"); + } + + void VulkanVertexBuffer::BindVertexBuffer(VkCommandBuffer p_CommandBuffer) { + VkBuffer buffers[] = {m_VertexBufferHandler}; + VkDeviceSize offsets[] = {0}; + vkCmdBindVertexBuffers(p_CommandBuffer, 0, 1, buffers, offsets); + } + + void VulkanVertexBuffer::RenderVertexBuffer(VkCommandBuffer p_CommandBuffer) { + vkCmdDraw(p_CommandBuffer, m_VerticesCount, 1, 0, 0); + } +}; \ No newline at end of file