From cd5c380dd15925c36df684e1eda2b6b22283753b Mon Sep 17 00:00:00 2001 From: Aaron <56617292+SpinnerX@users.noreply.github.com> Date: Fri, 22 Nov 2024 20:15:00 -0800 Subject: [PATCH] Working vulkan (#62) * Added clear comments on specific aspects of vulkan abstraction layers, modified header extensions to hpp, including adding in the event-system into this commit * Submitting bug fixes to the event system and made some changes by removing an update function from the window class that was only used for events into InputPoll::UpdateEvents instead * Updated cmake build to using cmake utils * Updated cmake build to using updated cmake utils * Working Vulkan Rendering Triangle with Better Design * Working colored triangle with updated shaders * working of rendering four triangles vertically with the color blue using push constants * Refactored Swapchain API for utilizing graphic swapchain instead of directly calling vulkan swapchain * Updated triangles to prettier colors and now got scaling, rotation, and translation matrices working using push constants stil * Merging big PR to engine3d for work TestApp and Editor application alongside threading and sync update managers * Getting the renderer to render meshes simplified and got an example basic physics system up and running for testing the renderer's capabilities * Fixed resolution offset to be dynamic with the windows dimensions * Working 3D cube with updated renderer * Updating cmake to utilize a much better working cmake-utils/2.0 * Working EditorScene to test the ECS and custom Scene objects intsead of the ones from the tutorials * Removed unecessary commented code and added back the rotation to make sure some of the transforms work properly still after these modifications * Working editor scene that utilizes our scene structure with basic ECS system working. * Working camera component with the cube object, and added viewport to get a perfect cube rendering added into the renderer * Camera component works!! * Working camera rotation using interpolation --------- Co-authored-by: = <=> --- CMakeLists.txt | 27 +- Editor/CMakeLists.txt | 49 +-- Editor/Editor/Editor.cpp | 296 +----------------- Editor/Editor/Editor.hpp | 32 +- Editor/Editor/EditorScene.cpp | 250 +++++++++++++++ Editor/Editor/EditorScene.hpp | 29 ++ TestApp/Application.cpp | 2 +- TestApp/CMakeLists.txt | 80 ++--- .../Components/Bodies/BodyContainer.hpp | 11 + .../Components/Bodies/Shapes/BoxShaper.hpp | 6 + .../Components/Bodies/Shapes/SphereShaper.hpp | 6 + .../Components/Physics/PhysicsBody3D.hpp | 34 ++ TestApp/Scenes/Assets/Components/testComp.hpp | 16 + .../SceneInstances/ShowCaseSceneInstance.hpp | 20 ++ .../Components/Bodies/BodyContainer.cpp | 6 + .../Components/Bodies/Shapes/BoxShaper.cpp | 19 ++ .../Components/Bodies/Shapes/SphereShaper.cpp | 21 ++ .../Components/Physics/PhysicsBody3D.cpp | 112 +++++++ .../src/Scenes/Assets/Components/testComp.cpp | 49 +++ .../SceneInstances/ShowCaseSceneInstance.cpp | 41 +++ conanfile.py | 3 +- engine3d/Core/ApplicationManager/Scene.hpp | 1 + engine3d/Core/Renderer/Renderer.hpp | 15 + engine3d/Core/Renderer/RendererBackend.hpp | 20 ++ engine3d/Core/Scene/SceneObject.hpp | 35 ++- engine3d/Core/Scene/SceneTest.hpp | 120 +++++++ .../Components/GameComponent.hpp | 3 +- .../Components/SPComps/EditorCamera.hpp | 100 ++++++ .../SceneObjects/SceneObject.hpp | 22 ++ engine3d/Core/Window.hpp | 2 + .../Vulkan2Showcase/VulkanImGuiLayer.hpp | 10 + .../internal/Vulkan2Showcase/VulkanModel.hpp | 2 +- .../Vulkan2Showcase/VulkanRenderer.hpp | 41 +++ engine3d/Math/Interpolation.hpp | 9 +- sim_shader_transforms/simple_shader.frag | 8 +- sim_shader_transforms/simple_shader.frag.spv | Bin 856 -> 864 bytes sim_shader_transforms/simple_shader.vert | 43 ++- sim_shader_transforms/simple_shader.vert.spv | Bin 1412 -> 1500 bytes src/CMakeLists.txt | 8 +- src/engine3d/Core/ApplicationInstance.cpp | 16 +- src/engine3d/Core/Renderer/Renderer.cpp | 266 +++++++++++++--- .../Core/Renderer/RendererBackend.cpp | 23 ++ .../Components/SPComps/EditorCamera.cpp | 253 +++++++++++++++ .../SceneObjects/SceneObject.cpp | 28 +- .../TimeManagement/GlobalUpdateManager.cpp | 3 +- src/engine3d/Core/Window.cpp | 4 + .../Vulkan2Showcase/VulkanImGuiLayer.cpp | 71 +++++ .../internal/Vulkan2Showcase/VulkanModel.cpp | 3 +- .../Vulkan2Showcase/VulkanRenderer.cpp | 179 +++++++++++ .../Vulkan2Showcase/VulkanSwapchain.cpp | 1 + 50 files changed, 1873 insertions(+), 522 deletions(-) create mode 100644 Editor/Editor/EditorScene.cpp create mode 100644 Editor/Editor/EditorScene.hpp create mode 100644 TestApp/Scenes/Assets/Components/Bodies/BodyContainer.hpp create mode 100644 TestApp/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp create mode 100644 TestApp/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp create mode 100644 TestApp/Scenes/Assets/Components/Physics/PhysicsBody3D.hpp create mode 100644 TestApp/Scenes/Assets/Components/testComp.hpp create mode 100644 TestApp/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp create mode 100644 TestApp/src/Scenes/Assets/Components/Bodies/BodyContainer.cpp create mode 100644 TestApp/src/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.cpp create mode 100644 TestApp/src/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.cpp create mode 100644 TestApp/src/Scenes/Assets/Components/Physics/PhysicsBody3D.cpp create mode 100644 TestApp/src/Scenes/Assets/Components/testComp.cpp create mode 100644 TestApp/src/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.cpp create mode 100644 engine3d/Core/Renderer/RendererBackend.hpp create mode 100644 engine3d/Core/Scene/SceneTest.hpp create mode 100644 engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp create mode 100644 engine3d/Core/internal/Vulkan2Showcase/VulkanImGuiLayer.hpp create mode 100644 engine3d/Core/internal/Vulkan2Showcase/VulkanRenderer.hpp create mode 100644 src/engine3d/Core/Renderer/RendererBackend.cpp create mode 100644 src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp create mode 100644 src/engine3d/Core/internal/Vulkan2Showcase/VulkanImGuiLayer.cpp create mode 100644 src/engine3d/Core/internal/Vulkan2Showcase/VulkanRenderer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4962928..46550a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,29 +4,6 @@ project(engine3d CXX) set(CMAKE_CXX_STANDARD 23) set(ENGINE_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/engine3d) -cmake_minimum_required(VERSION 3.25) -project(engine3d C CXX) - -set(CMAKE_CXX_STANDARD 23) - -set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") # works (in creating the compile_commands.json file) - -add_subdirectory(Editor) -add_subdirectory(TestApp) -add_subdirectory(src) - -# # target_include_directory is setting some private settings for differentiating what internal includes are privates and what should be includes should be exposed to the application-dev -target_include_directories(${PROJECT_NAME} PUBLIC ${JoltPhysics_SOURCE_DIR} ${EnTT_INCLUDE_DIR}) -target_include_directories(${PROJECT_NAME} PRIVATE engine3d/ engine3d/Core) -target_include_directories(${PROJECT_NAME} PUBLIC ${GLM_INCLUDE_DIR} ./engine3d) - -# #Set Compiler definitions -set(is_msvc_cl $) -set(dev_definitions - $<${is_msvc_cl}:JPH_FLOATING_POINT_EXCEPTIONS_ENABLED> - JPH_PROFILE_ENABLED - JPH_DEBUG_RENDERER - JPH_OBJECT_STREAM +build_library( + DIRECTORIES src Editor TestApp ) - -target_compile_definitions(${PROJECT_NAME} PRIVATE ${dev_definitions}) diff --git a/Editor/CMakeLists.txt b/Editor/CMakeLists.txt index 8053b84..40c9ba7 100644 --- a/Editor/CMakeLists.txt +++ b/Editor/CMakeLists.txt @@ -7,51 +7,12 @@ project(Editor CXX) # Editor/Editor.cpp # ) -set( - all_src +build_demos( + SOURCES Editor/Editor.hpp Editor/Editor.cpp -) - -add_executable(${PROJECT_NAME} ${all_src}) - -find_package(OpenGL REQUIRED) -find_package(glfw3 REQUIRED) - -find_package(Vulkan REQUIRED) -find_package(VulkanHeaders REQUIRED) -if(LINUX) -find_package(VulkanLoader REQUIRED) -endif(LINUX) - -# target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR}) -target_include_directories(${PROJECT_NAME} PRIVATE ../) -find_package(glm REQUIRED) -find_package(fmt REQUIRED) -find_package(spdlog REQUIRED) -find_package(yaml-cpp REQUIRED) -find_package(imguidocking REQUIRED) -find_package(box2d REQUIRED) -find_package(joltphysics REQUIRED) -find_package(EnTT REQUIRED) - - -target_link_libraries( - ${PROJECT_NAME} - PRIVATE - glfw - ${OPENGL_LIBRARIES} - Vulkan::Vulkan - vulkan-headers::vulkan-headers - glm::glm - fmt::fmt - spdlog::spdlog - yaml-cpp::yaml-cpp - imguidocking::imguidocking - box2d::box2d - Jolt::Jolt - EnTT::EnTT - engine3d + Editor/EditorScene.hpp + Editor/EditorScene.cpp + LINK_LIBRARIES engine3d ) - diff --git a/Editor/Editor/Editor.cpp b/Editor/Editor/Editor.cpp index ed64aad..a947832 100644 --- a/Editor/Editor/Editor.cpp +++ b/Editor/Editor/Editor.cpp @@ -1,22 +1,9 @@ #include "Editor.hpp" -#include "Core/TimeManagement/GlobalUpdateManager.hpp" -// #include "internal/VulkanCpp/helper_functions.hpp" -#include -#include -#include -#include -#include -#include -#include -#include -#include -// #include -#include -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include -#include - +#include "EditorScene.hpp" +#include +#include +#include +#include namespace engine3d{ @@ -27,279 +14,18 @@ namespace engine3d{ }; EditorApplication::EditorApplication(const std::string& p_DebugName) : ApplicationInstance(p_DebugName) { - // Renderer::Initialize(); - // Renderer::SetBackgroundColor({1.0f, 0.0f, 0.0f, 0.0f}); - - // VulkanVertexBuffer vb = VulkanVertexBuffer({ - // {{-0.5f, -0.288f, 0.0f, 1.0f}, {0.0f, 0.0f}}, - // {{.5f, -.288f, 0.0f, 1.0f}, {1.0f, 0.0f}}, - // {{0.0f, .577f, 0.0f, 1.0f}, {0.5f, 1.0f}} - // }); - - //! @note Basics of command buffers. - - //! @note Initialize Push constant range - VkPushConstantRange push_const_range = { - .stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, - .offset = 0, - .size = sizeof(SimplePushConstantData) - }; - - //! @note First initializing pipeline layout create info - VkPipelineLayoutCreateInfo pipeline_layout_create_info = { - .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, - .pNext = nullptr, - .flags = 0, - .setLayoutCount = 0, - .pSetLayouts = nullptr, - // .pushConstantRangeCount = 0, - // .pPushConstantRanges = nullptr, - .pushConstantRangeCount = 1, - .pPushConstantRanges = &push_const_range - }; - - vk::vk_check(vkCreatePipelineLayout(vk::VulkanContext::GetDriver(), &pipeline_layout_create_info, nullptr, &m_PipelineLayout), "vkCreatePipelineLayout", __FILE__, __LINE__, __FUNCTION__); - - //! @note Setting up our pipeline. - auto pipeline_config = vk::VulkanShader::shader_configuration(ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()); - //! @note We are setting our shader pipeline to utilize our current window's swapchain - //! @note a TODO is to utilize different render passes utiization for shader pipelines, potentially. - pipeline_config.PipelineRenderPass = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetRenderPass(); - pipeline_config.PipelineLayout = m_PipelineLayout; - - // m_Shader = Shader::Create("simple_shader/simple_shader.vert.spv", "simple_shader/simple_shader.frag.spv", pipeline_config); - m_Shader = Shader::Create("sim_shader_transforms/simple_shader.vert.spv", "sim_shader_transforms/simple_shader.frag.spv", pipeline_config); - - - //! @note Initializing Command buffers. - m_CommandBuffers.resize(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetImagesSize()); - - //! @note Creating our pools of command buffer structs - VkCommandPoolCreateInfo pool_create_info = { - .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, - .pNext = nullptr, - .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, - .queueFamilyIndex = vk::VulkanContext::GetPhysicalDriver().GetQueueIndices().Graphics - }; - - vk::vk_check(vkCreateCommandPool(vk::VulkanContext::GetDriver(), &pool_create_info, nullptr, &m_CommandPool), "vkCreateCommandPool", __FILE__, __LINE__, __FUNCTION__); - - //! @note Allocating our command buffers. - VkCommandBufferAllocateInfo cmd_buffer_alloc_info = { - .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, - .commandPool = m_CommandPool, - .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, - .commandBufferCount = static_cast(m_CommandBuffers.size()), - }; - - vk::vk_check(vkAllocateCommandBuffers(vk::VulkanContext::GetDriver(), &cmd_buffer_alloc_info, m_CommandBuffers.data()), "vkAllocateCommandBuffers", __FILE__, __LINE__, __FUNCTION__); - - ConsoleLogInfo("CommandBuffers Size === {}", m_CommandBuffers.size()); - - //-------------------------------------------- - //! @note Loading Game Objectts Here - //-------------------------------------------- - std::vector triangle_vertices = { - {.Position={0.0f, -0.5f}, .Color = {1.0f, 0.0f, 0.0f}}, - {.Position ={0.5f, 0.5f}, .Color = {0.0f, 1.0f, 0.0f}}, - {.Position ={-0.5f, 0.5f}, .Color = {0.0f, 0.0f, 1.0f}} - }; - // vk::VulkanModel model = vk::VulkanModel(triangle_vertices, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); - auto model = CreateRef(triangle_vertices, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); - auto triangle = SceneObject::Create(); - triangle.SetModel(model); - - //! @note Push Constants is pretty much how we can modify data without needing to record command buffers again. - - // triangle.SetColor({.1f, .8f, .1f}); - // triangle.SetColor({.1f, .8f, .1f}); - // triangle.SetColor({.1f, .8f, .1f}); - // Transform2DComponent transform; - // transform.Translation.x = .2f; - // transform.scale = {2.f, .5f}; - // transform.rotation = .25f * glm::two_pi(); - // triangle.SetTranslation(transform); - - // m_GameObjects.push_back(std::move(triangle)); - std::vector colors{ - {1.f, .7f, .73f}, - {1.f, .87f, .73f}, - {1.f, 1.f, .73f}, - {.73f, 1.f, .8f}, - {.73, .88f, 1.f} // - }; - - //! @note Loading 40 triangles. - for(int i = 0; i < 40; i++){ - auto triangle = SceneObject::Create(); - triangle.SetModel(model); - triangle.m_Transform2D.scale = glm::vec2(.5f) + i * 0.0025f; - triangle.m_Transform2D.rotation = glm::pi() * .00002f; - triangle.SetColor(colors[i % colors.size()]); - m_GameObjects.push_back(triangle); - } - GlobalUpdateManager::GetInstance()->SubscribeApplicationUpdate(this, &EditorApplication::OnApplicationUpdate); - + m_EditorScene = new EditorScene(); } - EditorApplication::~EditorApplication() {} - - void EditorApplication::OnRender(uint32_t image_index){ - - // for(uint32_t i = 0; i < m_CommandBuffers.size(); i++){ - VkCommandBufferBeginInfo cmd_buf_begin_info = { - .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO - }; - - // START OF COMMAND BUFFER RECORD - vk::vk_check(vkBeginCommandBuffer(m_CommandBuffers[image_index], &cmd_buf_begin_info), "vkBeginCommandBuffer", __FILE__, __LINE__, __FUNCTION__); - - // starting render pass - VkRenderPassBeginInfo rp_begin_info = { - .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, - .renderPass = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetRenderPass(), - .framebuffer = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetFramebuffer(image_index), // Specifying which framebuffer to render pass to. - .renderArea = { - .offset = {0, 0}, - .extent = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()} - } - }; - - std::array clearValues; - clearValues[0].color = {0.1f, 0.1f, 0.1f, 1.0f}; - clearValues[1].depthStencil = {1.0f, 0}; - - rp_begin_info.clearValueCount = static_cast(clearValues.size()); - rp_begin_info.pClearValues = clearValues.data(); - - /* - if(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetRenderPass() == VK_NULL_HANDLE){ - ConsoleLogInfo("Renderpass Read at index i = {} is not valid", i); - } - else{ - ConsoleLogInfo("Renderpass Read at index i = {} is valid", i); - } - - if(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetFramebuffer(i) == VK_NULL_HANDLE){ - ConsoleLogInfo("Framebuffer Read at index i = {} is not valid", i); - } - else{ - ConsoleLogInfo("Framebuffer Read at index i = {} is valid", i); - } - */ - - vkCmdBeginRenderPass(m_CommandBuffers[image_index], &rp_begin_info, VK_SUBPASS_CONTENTS_INLINE); - - //! @note We can probably add this into Shader::Bind() - //! @note Usage: m_Shader.Bind(m_CommandBuffers[i]); - - //! @note Give some cool effects - int i = 0; - for(auto& obj : m_GameObjects){ - i += 1; - obj.m_Transform2D.rotation = glm::mod(obj.m_Transform2D.rotation + 0.001f * i, 2.f * glm::pi()); - // obj.m_Transform2D.Translation.x = glm::mod(obj.m_Transform2D.rotation + 0.00001f * i, 2.f * glm::pi()); - // obj.m_Transform2D.Translation.y = glm::mod(obj.m_Transform2D.rotation + 0.00001f * i, 2.f * glm::pi()); - - // obj.SetRotation(glm::mod(obj.m_Transform2D.rotation + 0.001f * i, 2.f * glm::pi())); - } - - - //! @note Essentially doing m_Pipeline->Bind(m_CommandBuffer[i]) - //! @note Starts when to start rendering!! - vkCmdBindPipeline(m_CommandBuffers[image_index], VK_PIPELINE_BIND_POINT_GRAPHICS, m_Shader->GetGraphicsPipeline()); - // vkCmdDraw(m_CommandBuffers[i], 3, 1, 0, 0); - // model->Bind(m_CommandBuffers[i]); - // model.Bind(m_CommandBuffers[i]); - - //! @note Only for testing purposes for mesh data. - /* - for(int j = 0; j < 4; j++){ - SimplePushConstantData push{ - .Offsets = {0.0f, -0.4f + j * 0.25f}, - .Color = {0.0f, 0.0f, 0.2f + 0.2f * j} - }; - - vkCmdPushConstants( - m_CommandBuffers[i], - m_PipelineLayout, - VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, - 0, - sizeof(SimplePushConstantData), - &push - ); - - model->Draw(m_CommandBuffers[i]); - // model.Draw(m_CommandBuffers[i]); - } - */ - - //! @note - for(auto& obj : m_GameObjects){ - obj.m_Transform2D.rotation = glm::mod(obj.GetTransform().rotation + 0.001f, glm::two_pi()); - - SimplePushConstantData push = { - .Transform = obj.GetTransform().mat2(), - // .Transform = glm::mod(obj.GetTransform().rotation * 0.1f, glm::two_pi()), - .Offsets = obj.GetTransform().Translation, - .Color = obj.GetColor(), - }; - vkCmdPushConstants( - m_CommandBuffers[image_index], - m_PipelineLayout, - VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, - 0, - sizeof(SimplePushConstantData), - &push - ); - - obj.GetModel()->Bind(m_CommandBuffers[image_index]); - obj.GetModel()->Draw(m_CommandBuffers[image_index]); - } - - vkCmdEndRenderPass(m_CommandBuffers[image_index]); - - // END OF COMMAND BUFFER RECORDING - vk::vk_check(vkEndCommandBuffer(m_CommandBuffers[image_index]), "vkEndCommandBuffer", __FILE__, __LINE__, __FUNCTION__); + EditorApplication::~EditorApplication() { } - // void EditorApplication::UpdateThisApplicationInstance(){ void EditorApplication::OnApplicationUpdate(){ - //! @note Just testing to see if application still closes cleanly. - // if(InputPoll::IsKeyPressed(ENGINE_KEY_ESCAPE)){ - // exit(0); - // } - - //! @note This function will render our primitives - //! @note TODO -- Flush should only happens when our scene is given everything that lives within this scene (ref to lifetimes) - /* Renderer::FlushScene(); */ - - // Renderer::Presentation(); - // ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. - // ImGui::End(); - - /* - @note This is going to be what we will implement next in the Renderer.hpp/.cpp once we can render the triangle. - @note The idea is in the application this the swapchain should not be called directly by the application developer. - @note Want to migrate to doing this so its easier for testability and submitting commands/drawcalls to vulkan's API. - @note Realistically we'll have VulkanRenderer that would be accepting these structures. - Renderer::Submit([](){ - vkCmdBeginRenderPass(m_CommandBuffers[i], &rp_begin_info, VK_SUBPASS_CONTENTS_INLINE); - - vkCmdBindPipeline(m_CommandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, m_Shader.GetGraphicsPipeline()); - vkCmdDraw(m_CommandBuffers[i], 3, 1, 0, 0); - - vkCmdEndRenderPass(m_CommandBuffers[i]); - - vk::vk_check(vkEndCommandBuffer(m_CommandBuffers[i]), "vkEndCommandBuffer", __FILE__, __LINE__, __FUNCTION__); - }); - */ - uint32_t image_index = ApplicationInstance::GetWindow().GetCurrentSwapchain()->AcquireNextImage(); - - OnRender(image_index); - - ApplicationInstance::GetWindow().GetCurrentSwapchain()->SubmitCommandBuffer(&m_CommandBuffers[image_index]); + //! @note Handle Events. + // m_EditorScene->OnUpdate(); + m_EditorScene->OnMoveCamUpdate(); + Renderer::RecordSceneGameObjects(m_EditorScene->GetSceneObjects(), m_EditorScene->GetCameraObject()); } ApplicationInstance* InitializeApplication(){ diff --git a/Editor/Editor/Editor.hpp b/Editor/Editor/Editor.hpp index 81925ee..2c3c9ff 100644 --- a/Editor/Editor/Editor.hpp +++ b/Editor/Editor/Editor.hpp @@ -1,8 +1,9 @@ #pragma once +// #include #include -#include -#include -#include +// #include +// #include +#include "EditorScene.hpp" namespace engine3d{ @@ -19,20 +20,19 @@ namespace engine3d{ // void ShutdownEditor(); // void UpdateThisApplicationInstance() override; void OnApplicationUpdate(); - - private: - //! @note Basically to differentiate draw calls with initialization tasks - void OnRender(uint32_t image_index); private: - //! @note Editor application, Engine, UI Layer. - float m_LastFrameTime = 0.0f; - //! @note Essentially our VulkanShader is VkPipeline, ShaderModule all in one. - Ref m_Shader; - VkPipelineLayout m_PipelineLayout; - VkCommandPool m_CommandPool; - std::vector m_CommandBuffers; - - std::vector m_GameObjects; + // 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; + + + }; }; \ No newline at end of file diff --git a/Editor/Editor/EditorScene.cpp b/Editor/Editor/EditorScene.cpp new file mode 100644 index 0000000..6201cac --- /dev/null +++ b/Editor/Editor/EditorScene.cpp @@ -0,0 +1,250 @@ +#include "EditorScene.hpp" +#include +#include +#include +#include +#include +#include +#include +#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}}, + + // 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); + } + + EditorScene::EditorScene(){ + m_Scene = new Scene(); + + // SyncUpdateManager::GetInstance()->Subscribe(this, &EditorScene::OnUpdate); + + auto cube_mesh = CreateCubeMesh({.0f, .0f, .0f}); + + //! @note Make this scene object as part of our current scene. + + // ----------------------------- + // Camera Scene Object Creation + // ----------------------------- + m_CameraObject = new SceneObject(m_Scene); + m_CameraObject->AddComponent(); + auto& camera_transform = m_CameraObject->SceneGetComponent(); + camera_transform.m_Position = {-1.f, -2.f, -20.f}; + auto camera = m_CameraObject->SceneGetComponent(); + + // ----------------------------- + // Cube 1 Scene object Creation + // ----------------------------- + 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); + + // ----------------------------- + // Cube 2 Scene object Creation + // ----------------------------- + SceneObject* cube2 = new SceneObject(m_Scene); + auto& cube2_transform = cube2->SceneGetComponent(); + // 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); + + + + + //! @note Then we add them to our vector. + m_SceneObjects.push_back(cube1); + m_SceneObjects.push_back(cube2); + + } + + void EditorScene::OnCreate(){ + } + + void EditorScene::OnUpdate(){ + // glm::vec3 m_MoveDirection{0.f}; + // glm::vec3 m_Rotation{0}; + + // for(const auto& obj : m_SceneObjects){ + // // auto& transform_compoent = obj->SceneGetComponent(); + // auto& camera_component = obj->SceneGetComponent(); + // camera_component.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 50.f); + // } + } + + void EditorScene::OnMoveCamUpdate(){ + auto& transform = m_CameraObject->SceneGetComponent(); + auto& camera = m_CameraObject->SceneGetComponent(); + 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 pos_sensitivity = 2.f; + 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); + + /* + move-right = D + move-left = A + move-forward = W + move-backward = S + + move-up = E + move-down = Q + + look-left = LEFT + look-right = RIGHT + look-up = UP + look-down = DOWN + */ + + /* + for(const auto& obj : m_SceneObjects){ + glm::vec3 rotate{0}; + if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT)) rotate.y += 1.f; + if(InputPoll::IsKeyPressed(ENGINE_KEY_RIGHT)) rotate.y -= 1.f; + if(InputPoll::IsKeyPressed(ENGINE_KEY_UP)) rotate.x += 1.f; + if(InputPoll::IsKeyPressed(ENGINE_KEY_DOWN)) rotate.x -= 1.f; + + auto& transform = obj->SceneGetComponent(); + auto& camera = obj->SceneGetComponent(); + + if(glm::dot(rotate, rotate) > std::numeric_limits::epsilon()){ + transform.m_AxisRotation += m_LookSpeed * SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime * glm::normalize(rotate); + } + + transform.m_AxisRotation.x = glm::clamp(transform.m_AxisRotation.x, -1.5f, 1.5f); + transform.m_AxisRotation.y = glm::mod(transform.m_AxisRotation.y, glm::two_pi()); + + float yaw = transform.m_AxisRotation.y; + const glm::vec3 forward_dir{sin(yaw), 0.f, cos(yaw)}; + const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.y}; + const glm::vec3 up_dir{0.f, -1.f, 0.f}; + + glm::vec3 move_dir{0.f}; + + if(InputPoll::IsKeyPressed(ENGINE_KEY_W)) move_dir += forward_dir; // FORWARD + if(InputPoll::IsKeyPressed(ENGINE_KEY_S)) move_dir -= forward_dir; // BACKWARD + if(InputPoll::IsKeyPressed(ENGINE_KEY_D)) move_dir += right_dir; // RIGHT + if(InputPoll::IsKeyPressed(ENGINE_KEY_A)) move_dir -= right_dir; // LEFT + if(InputPoll::IsKeyPressed(ENGINE_KEY_E)) move_dir += up_dir; // UP + if(InputPoll::IsKeyPressed(ENGINE_KEY_Q)) move_dir -= up_dir; // DOWN + + if(glm::dot(move_dir, move_dir) > std::numeric_limits::epsilon()){ + transform.m_Position += m_MoveSpeed * (SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) * glm::normalize(move_dir); + } + + // camera.SetViewTarget({-1.f, -2.f, -20.f}, transform.m_Position); + // camera.SetViewTarget({0.f, 0.f, 0.f}, transform.m_Position); + camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation); + } + */ + + glm::vec3 rotate{0}; + + //! @note Make sure that our mouse controls how camera rotates. + if(InputPoll::IsMousePressed(Mouse::ButtonRight)){ + // temp_position.x = m_MousePosition.x - InputPoll::GetMouseX(); + rotate.y += (m_MousePosition.x - InputPoll::GetMouseX()) * invert_pos.y; + rotate.x += (m_MousePosition.y - InputPoll::GetMouseY()) * invert_pos.x; + } + + m_MousePosition = InputPoll::GetMousePosition(); + + //! @note Utilize linear interpolation to get smooth camera rotation. + if(glm::dot(rotate, rotate) > std::numeric_limits::epsilon()){ + float dt = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime; + auto temp_rotation = (m_LookSpeed * glm::normalize(rotate) * sensitivity) + transform.m_AxisRotation; + transform.m_AxisRotation = Interpolation::LinearInterpolate(transform.m_AxisRotation, temp_rotation, nullptr, dt); + } + + transform.m_AxisRotation.x = glm::clamp(transform.m_AxisRotation.x, -1.5f, 1.5f); + transform.m_AxisRotation.y = glm::mod(transform.m_AxisRotation.y, glm::two_pi()); + + float yaw = transform.m_AxisRotation.y; + const glm::vec3 forward_dir{sin(yaw), 0.f, cos(yaw)}; + const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.y}; + const glm::vec3 up_dir{0.f, -1.f, 0.f}; + + glm::vec3 move_dir{0.f}; + + if(InputPoll::IsKeyPressed(ENGINE_KEY_W)) move_dir += forward_dir; // FORWARD + if(InputPoll::IsKeyPressed(ENGINE_KEY_S)) move_dir -= forward_dir; // BACKWARD + if(InputPoll::IsKeyPressed(ENGINE_KEY_D)) move_dir += right_dir; // RIGHT + if(InputPoll::IsKeyPressed(ENGINE_KEY_A)) move_dir -= right_dir; // LEFT + if(InputPoll::IsKeyPressed(ENGINE_KEY_E)) move_dir += up_dir; // UP + if(InputPoll::IsKeyPressed(ENGINE_KEY_Q)) move_dir -= up_dir; // DOWN + + if(glm::dot(move_dir, move_dir) > std::numeric_limits::epsilon()){ + transform.m_Position += m_MoveSpeed * (SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) * glm::normalize(move_dir) * pos_sensitivity; + } + + camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation); + + + camera.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 100.f); + + } + + // void EditorScene::OnCameraUpdate(){ + // for(const auto& obj : m_SceneObjects){ + // obj->SceneGetComponent().OnUpdate(); + // } + // } +}; \ No newline at end of file diff --git a/Editor/Editor/EditorScene.hpp b/Editor/Editor/EditorScene.hpp new file mode 100644 index 0000000..86a8247 --- /dev/null +++ b/Editor/Editor/EditorScene.hpp @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include + +namespace engine3d{ + class EditorScene{ + public: + EditorScene(); + + void OnCreate(); + + void OnUpdate(); + + void OnMoveCamUpdate(); + + std::vector& GetSceneObjects() { return m_SceneObjects; } + + SceneObject* GetCameraObject() const { return m_CameraObject; } + + private: + std::vector m_SceneObjects; + Scene* m_Scene; + SceneObject* m_CameraObject; + glm::vec2 m_MousePosition; + float m_MoveSpeed = {3.f}; + float m_LookSpeed = {1.5f}; + }; +}; \ No newline at end of file diff --git a/TestApp/Application.cpp b/TestApp/Application.cpp index d04d8eb..b1cf406 100644 --- a/TestApp/Application.cpp +++ b/TestApp/Application.cpp @@ -1,6 +1,6 @@ #include "Core/EngineLogger.hpp" #include -#include "SceneTest/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp" +#include "Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp" #include namespace engine3d{ diff --git a/TestApp/CMakeLists.txt b/TestApp/CMakeLists.txt index e923169..7cf153b 100644 --- a/TestApp/CMakeLists.txt +++ b/TestApp/CMakeLists.txt @@ -1,79 +1,37 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.25) project(TestApp CXX) -# find_package(Vulkan REQUIRED) +set(SCENE_TEST_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/SceneTest) -# find_package(engine3d CONFIG REQUIRED) -add_executable(${PROJECT_NAME} +build_demos( + SOURCES Application.cpp #To Test - SceneTest/Scenes/Assets/Components/testComp.hpp - SceneTest/src/Scenes/Assets/Components/testComp.cpp + Scenes/Assets/Components/testComp.hpp + src/Scenes/Assets/Components/testComp.cpp #Physics - SceneTest/Scenes/Assets/Components/Physics/PhysicsBody3D.hpp - SceneTest/src/Scenes/Assets/Components/Physics/PhysicsBody3D.cpp + Scenes/Assets/Components/Physics/PhysicsBody3D.hpp + src/Scenes/Assets/Components/Physics/PhysicsBody3D.cpp - SceneTest/Scenes/Assets/Components/Bodies/BodyContainer.hpp - SceneTest/src/Scenes/Assets/Components/Bodies/BodyContainer.cpp + Scenes/Assets/Components/Bodies/BodyContainer.hpp + src/Scenes/Assets/Components/Bodies/BodyContainer.cpp #Body Shapes - SceneTest/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp - SceneTest/src/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.cpp + Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp + src/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.cpp - SceneTest/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp - SceneTest/src/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.cpp + Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp + src/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.cpp #Scenes - SceneTest/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp - SceneTest/src/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.cpp + Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp + src/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.cpp - + LINK_LIBRARIES engine3d ) -target_include_directories(${PROJECT_NAME} PUBLIC SceneTest) -target_include_directories(${PROJECT_NAME} PRIVATE ../) - -target_compile_definitions(${PROJECT_NAME} PRIVATE ${dev_definitions}) - -find_package(OpenGL REQUIRED) -find_package(glfw3 REQUIRED) - -find_package(Vulkan REQUIRED) -find_package(VulkanHeaders REQUIRED) - -if(LINUX) -find_package(VulkanLoader REQUIRED) -endif(LINUX) - -# target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR}) -target_include_directories(${PROJECT_NAME} PRIVATE ../) -find_package(glm REQUIRED) -find_package(fmt REQUIRED) -find_package(spdlog REQUIRED) -find_package(yaml-cpp REQUIRED) -find_package(imguidocking REQUIRED) -find_package(box2d REQUIRED) -find_package(joltphysics REQUIRED) -find_package(EnTT REQUIRED) - - -target_link_libraries( - ${PROJECT_NAME} - PRIVATE - glfw - ${OPENGL_LIBRARIES} - Vulkan::Vulkan - vulkan-headers::vulkan-headers - glm::glm - fmt::fmt - spdlog::spdlog - yaml-cpp::yaml-cpp - imguidocking::imguidocking - box2d::box2d - Jolt::Jolt - EnTT::EnTT - engine3d -) \ No newline at end of file +# target_include_directories(${PROJECT_NAME} PUBLIC ${SCENE_TEST_INCLUDE_DIR}) +target_include_directories(${PROJECT_NAME} PUBLIC ./ ../) diff --git a/TestApp/Scenes/Assets/Components/Bodies/BodyContainer.hpp b/TestApp/Scenes/Assets/Components/Bodies/BodyContainer.hpp new file mode 100644 index 0000000..2fcd601 --- /dev/null +++ b/TestApp/Scenes/Assets/Components/Bodies/BodyContainer.hpp @@ -0,0 +1,11 @@ +#include +// Jolt Includes +class BodyContainer +{ + public: + BodyContainer(); + + operator JPH::BodyID() { return m_BodyID; } + JPH::BodyCreationSettings m_BodySettings; + JPH::BodyID m_BodyID; +}; \ No newline at end of file diff --git a/TestApp/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp b/TestApp/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp new file mode 100644 index 0000000..5ec67a1 --- /dev/null +++ b/TestApp/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.hpp @@ -0,0 +1,6 @@ +#include +class BoxShaper : public BodyContainer +{ + public: + BoxShaper(); +}; \ No newline at end of file diff --git a/TestApp/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp b/TestApp/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp new file mode 100644 index 0000000..472f82d --- /dev/null +++ b/TestApp/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.hpp @@ -0,0 +1,6 @@ +#include +class SphereShaper : public BodyContainer +{ + public: + SphereShaper(); +}; \ No newline at end of file diff --git a/TestApp/Scenes/Assets/Components/Physics/PhysicsBody3D.hpp b/TestApp/Scenes/Assets/Components/Physics/PhysicsBody3D.hpp new file mode 100644 index 0000000..4b4fc6c --- /dev/null +++ b/TestApp/Scenes/Assets/Components/Physics/PhysicsBody3D.hpp @@ -0,0 +1,34 @@ + +#pragma once + +#include "Core/SceneManagment/Components/GameComponent.hpp" +#include +#include +#include +#include + +#include +// This is a test version of this class as there is to much +class PhysicsBody3D: public engine3d::GameComponent +{ + public: + PhysicsBody3D(BodyContainer * p_bodyCon); + ~PhysicsBody3D(); + void OnIntegrate(); + void Update(); + void LateUpdate(); + void PhysicsUpdate(); + void Begin(); + BodyContainer* GetBody(); + + void SetScale(float x, float y, float z); + void SetPosition(float x, float y, float z); + void SetRotation(Quat quaternion); + + private: + engine3d::Transform* m_Transform; + PhysicsBody3D() = default; + BodyContainer * bodyType; + JPH::BodyInterface* m_interface; + bool once = false; +}; diff --git a/TestApp/Scenes/Assets/Components/testComp.hpp b/TestApp/Scenes/Assets/Components/testComp.hpp new file mode 100644 index 0000000..f0cf5d5 --- /dev/null +++ b/TestApp/Scenes/Assets/Components/testComp.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "Scenes/Assets/Components/Physics/PhysicsBody3D.hpp" +#include + + class testComp : public engine3d::GameComponent + { + public: + void OnIntegrate(); + void Update(); + void LateUpdate(); + void PhysicsUpdate(); + private: + bool t_Secret = false; + BodyContainer * m_rb; + }; \ No newline at end of file diff --git a/TestApp/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp b/TestApp/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp new file mode 100644 index 0000000..5572927 --- /dev/null +++ b/TestApp/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.hpp @@ -0,0 +1,20 @@ +#include "Core/ApplicationManager/Scene.hpp" +// #include "Core/SceneManagment/SceneObjects/SceneObject.hpp" + + +namespace engine3d{ + class SceneObject; +}; + +class ShowCaseSceneInstance +{ + public: + ShowCaseSceneInstance(); + ~ShowCaseSceneInstance(); + engine3d::Scene* m_Scene; + std::vector m_SceneObjects; + engine3d::Scene* GetScene(); + + private: + void CreateObjects(); +}; \ No newline at end of file diff --git a/TestApp/src/Scenes/Assets/Components/Bodies/BodyContainer.cpp b/TestApp/src/Scenes/Assets/Components/Bodies/BodyContainer.cpp new file mode 100644 index 0000000..a4284db --- /dev/null +++ b/TestApp/src/Scenes/Assets/Components/Bodies/BodyContainer.cpp @@ -0,0 +1,6 @@ +#include + +BodyContainer::BodyContainer() +{ + // will add more settings and configurations later +} diff --git a/TestApp/src/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.cpp b/TestApp/src/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.cpp new file mode 100644 index 0000000..566bb88 --- /dev/null +++ b/TestApp/src/Scenes/Assets/Components/Bodies/Shapes/BoxShaper.cpp @@ -0,0 +1,19 @@ +#include +using namespace JPH; +using namespace JPH::literals; +using namespace engine3d; +BoxShaper::BoxShaper() +{ + JoltHandler * temp = engine3d::JoltHandler::GetInstance(); + m_BodySettings = BodyCreationSettings( + temp->m_BoxShapeScaled, + RVec3(0.0_r, -1.0_r, 0.0_r), + Quat::sIdentity(), + EMotionType::Static, + Engine3DLayers::Static + ); + + m_BodyID = temp->getInterface()->CreateAndAddBody( + m_BodySettings, + EActivation::DontActivate); +} \ No newline at end of file diff --git a/TestApp/src/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.cpp b/TestApp/src/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.cpp new file mode 100644 index 0000000..eec65a1 --- /dev/null +++ b/TestApp/src/Scenes/Assets/Components/Bodies/Shapes/SphereShaper.cpp @@ -0,0 +1,21 @@ +#include +using namespace JPH; +using namespace JPH::literals; +using namespace engine3d; +SphereShaper::SphereShaper() +{ + JoltHandler * temp = engine3d::JoltHandler::GetInstance(); + + + m_BodySettings = BodyCreationSettings( + temp->m_SphereShapeScaled, + RVec3(0.0_r, 4.0_r, 0.0_r), + Quat::sIdentity(), + EMotionType::Dynamic, + Engine3DLayers::Dynamic + ); + + m_BodyID = temp->getInterface()->CreateAndAddBody( + m_BodySettings, + EActivation::Activate); +} \ No newline at end of file diff --git a/TestApp/src/Scenes/Assets/Components/Physics/PhysicsBody3D.cpp b/TestApp/src/Scenes/Assets/Components/Physics/PhysicsBody3D.cpp new file mode 100644 index 0000000..e5bfc6b --- /dev/null +++ b/TestApp/src/Scenes/Assets/Components/Physics/PhysicsBody3D.cpp @@ -0,0 +1,112 @@ +#include + +#include +#include +#include +#include +#include +#include +using namespace engine3d; +PhysicsBody3D::PhysicsBody3D(BodyContainer * p_bodyCon) +{ + bodyType = p_bodyCon; +} + +void PhysicsBody3D::OnIntegrate() +{ + SyncUpdateManager::GetInstance()->Subscribe + (this, &PhysicsBody3D::Update); + SyncUpdateManager::GetInstance()->Subscribe + (this, &PhysicsBody3D::LateUpdate); + SyncUpdateManager::GetInstance()->Subscribe + (this, &PhysicsBody3D::PhysicsUpdate); + + // Probably should be an event or called when activated + //! @note For now just calling Begin on integrate + + Begin(); +} + +void PhysicsBody3D::Begin() +{ + m_interface = engine3d::JoltHandler::GetInstance()->getInterface(); + // m_Transform = &m_GameObjectRef->SceneGetComponent(); + // m_Transform = m_GameObjectRef +} + +void PhysicsBody3D::Update() +{ + + //Convert Posiitons + m_Transform->m_Position.x = m_interface-> + GetCenterOfMassPosition(bodyType->m_BodyID).GetX(); + m_Transform->m_Position.y = m_interface-> + GetCenterOfMassPosition(bodyType->m_BodyID).GetY(); + m_Transform->m_Position.z = m_interface-> + GetCenterOfMassPosition(bodyType->m_BodyID).GetZ(); + + //Convert Rotations + m_Transform->m_QuaterionRot.x = m_interface-> + GetRotation(bodyType->m_BodyID).GetX(); + m_Transform->m_QuaterionRot.y = m_interface-> + GetRotation(bodyType->m_BodyID).GetY(); + m_Transform->m_QuaterionRot.z = m_interface-> + GetRotation(bodyType->m_BodyID).GetZ(); + m_Transform->m_QuaterionRot.w = m_interface-> + GetRotation(bodyType->m_BodyID).GetW(); + + //Convert Rotations + m_Transform->m_AxisRotation.x = m_interface-> + GetRotation(bodyType->m_BodyID).GetEulerAngles().GetX(); + m_Transform->m_AxisRotation.y = m_interface-> + GetRotation(bodyType->m_BodyID).GetEulerAngles().GetY(); + m_Transform->m_AxisRotation.z = m_interface-> + GetRotation(bodyType->m_BodyID).GetEulerAngles().GetZ(); + + // std::print("Rotation: (X: {0}, Y: {1}, Z: {2})\n", + // m_Transform->m_AxisRotation.x, + // m_Transform->m_AxisRotation.y, + // m_Transform->m_AxisRotation.z); + +} + +void PhysicsBody3D::SetScale(float x, float y, float z) +{ + m_interface->GetShape(bodyType->m_BodyID)->ScaleShape(RVec3(x,y,z)); +} + +void PhysicsBody3D::SetPosition(float x, float y, float z) +{ + m_interface->SetPosition( + bodyType->m_BodyID, + RVec3(x,y,z), + JPH::EActivation::Activate); +} + +void PhysicsBody3D::SetRotation(Quat quaternion) +{ + m_interface->SetRotation( + bodyType->m_BodyID, + quaternion, + JPH::EActivation::Activate); +} + +void PhysicsBody3D::LateUpdate() +{ + +} + +void PhysicsBody3D::PhysicsUpdate() +{ + +} + +BodyContainer* PhysicsBody3D::GetBody() +{ + return bodyType; +} + +PhysicsBody3D::~PhysicsBody3D() +{ + delete bodyType; +} \ No newline at end of file diff --git a/TestApp/src/Scenes/Assets/Components/testComp.cpp b/TestApp/src/Scenes/Assets/Components/testComp.cpp new file mode 100644 index 0000000..1afa5bd --- /dev/null +++ b/TestApp/src/Scenes/Assets/Components/testComp.cpp @@ -0,0 +1,49 @@ +// #include +#include "Core/SceneManagment/Components/GameComponent.hpp" +#include "Physics/JoltHandler.hpp" +#include "Scenes/Assets/Components/Physics/PhysicsBody3D.hpp" +#include +#include +#include +#include +#include +#include +// #include "Scene" + +using namespace engine3d; + void testComp::OnIntegrate() + {; + SyncUpdateManager::GetInstance()->Subscribe + (this, &testComp::Update); + SyncUpdateManager::GetInstance()->Subscribe + (this, &testComp::LateUpdate); + SyncUpdateManager::GetInstance()->Subscribe + (this, &testComp::PhysicsUpdate); + + // Need an activation and start funciton + // m_rb = m_GameObjectRef->SceneGetComponent().GetBody(); + + } + + void testComp::Update() + { + if(InputPoll::IsKeyPressed(KeyCode::F5)) + { + JoltHandler::GetInstance()-> + getInterface()->AddForce( + m_rb->m_BodyID, + RVec3(0.0f,100000.0f,0.0f), + EActivation::Activate + ); + } + } + + void testComp::LateUpdate() + { + + } + + void testComp::PhysicsUpdate() + { + + } \ No newline at end of file diff --git a/TestApp/src/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.cpp b/TestApp/src/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.cpp new file mode 100644 index 0000000..603a92b --- /dev/null +++ b/TestApp/src/Scenes/Assets/SceneInstances/ShowCaseSceneInstance.cpp @@ -0,0 +1,41 @@ +#include "Core/ApplicationManager/Scene.hpp" +#include "Core/SceneManagment/SceneObjects/SceneObject.hpp" +#include "Scenes/Assets/Components/Physics/PhysicsBody3D.hpp" +#include "Scenes/Assets/Components/testComp.hpp" +#include +#include +#include + +ShowCaseSceneInstance::ShowCaseSceneInstance() +{ + m_Scene = new engine3d::Scene(); + printf("getting here\n"); + CreateObjects(); + +} + +void ShowCaseSceneInstance::CreateObjects() +{ + //Platform + m_SceneObjects.push_back(new engine3d::SceneObject(m_Scene)); + BodyContainer * l_Body = new BoxShaper(); + m_SceneObjects[0]->AddComponent(l_Body); + m_SceneObjects[0]->name = "Platform1"; + + //Sphere + m_SceneObjects.push_back(new engine3d::SceneObject(m_Scene)); + l_Body = new SphereShaper(); + m_SceneObjects[1]->AddComponent(l_Body); + m_SceneObjects[1]->AddComponent(); + m_SceneObjects[1]->name = "Ball"; + +} + +ShowCaseSceneInstance::~ShowCaseSceneInstance() +{ + delete m_Scene; + for(auto obj : m_SceneObjects) + { + delete obj; + } +} \ No newline at end of file diff --git a/conanfile.py b/conanfile.py index 8608013..88f7b45 100644 --- a/conanfile.py +++ b/conanfile.py @@ -26,12 +26,11 @@ class engine3dRecipe(ConanFile): def build_requirements(self): self.tool_requires("make/4.4.1") self.tool_requires("cmake/3.27.1") - self.tool_requires("engine3d-cmake-utils/1.0") + self.tool_requires("engine3d-cmake-utils/2.0") def requirements(self): self.requires("glfw/3.4", transitive_headers=True) self.requires("opengl/system", transitive_headers=True) - self.requires("engine3d-cmake-utils/1.0") # These end in 1.0 because they are engine3d-customized conan packages # Slighly modified of the conan packages and it's CMake generators to using "Unix Makefiles" diff --git a/engine3d/Core/ApplicationManager/Scene.hpp b/engine3d/Core/ApplicationManager/Scene.hpp index 5515d32..7034996 100644 --- a/engine3d/Core/ApplicationManager/Scene.hpp +++ b/engine3d/Core/ApplicationManager/Scene.hpp @@ -2,6 +2,7 @@ #include namespace engine3d { + //! @note TODO: Move this into Core/Scene/ directory. Not in Application Manager. class Scene { public: entt::registry m_SceneRegistry; diff --git a/engine3d/Core/Renderer/Renderer.hpp b/engine3d/Core/Renderer/Renderer.hpp index c557768..8c76a54 100644 --- a/engine3d/Core/Renderer/Renderer.hpp +++ b/engine3d/Core/Renderer/Renderer.hpp @@ -1,12 +1,24 @@ #pragma once +#include +#include #include #include +#include +#include +#include namespace engine3d{ class Renderer{ public: + static void Initialize(const std::string& p_DebugName); static void BeginFrame(); + + //! @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(); template @@ -18,5 +30,8 @@ namespace engine3d{ static void SetBackgroundColor(const std::array& rgba); static RendererSettings& GetSettings(); + + private: + static VkCommandBuffer GetCurrentCommandBuffer(); }; }; \ No newline at end of file diff --git a/engine3d/Core/Renderer/RendererBackend.hpp b/engine3d/Core/Renderer/RendererBackend.hpp new file mode 100644 index 0000000..bfb92cb --- /dev/null +++ b/engine3d/Core/Renderer/RendererBackend.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "Core.hpp" +namespace engine3d{ + /** + * @name Renderer Backend + * @note that will represent our actual renderer to draw stuff + * @note Graphics API-agnostic renderer that the main renderer will utilize. + */ + class RendererBackend{ + public: + static Ref InitializeRenderer(const std::string& p_DebugName); + void BeginFrame(); + void EndFrame(); + + private: + virtual void StartFrame() = 0; + virtual void FinishFrame() = 0; + }; +}; \ No newline at end of file diff --git a/engine3d/Core/Scene/SceneObject.hpp b/engine3d/Core/Scene/SceneObject.hpp index 858bf05..ddc20b9 100644 --- a/engine3d/Core/Scene/SceneObject.hpp +++ b/engine3d/Core/Scene/SceneObject.hpp @@ -1,31 +1,37 @@ #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::vec2 Translation{}; - glm::vec2 scale = {1.f, 1.f}; - float rotation; - - glm::mat2 mat2() { - const float s = glm::sin(rotation); - const float c = glm::cos(rotation); - glm::mat2 rotation_mat = {{c, s}, {-s, c}}; - - glm::mat2 scale_mat{{scale.x, .0f}, {.0f, scale.y}}; - // return glm::mat2{1.f}; - // return scale_mat; - return rotation_mat * scale_mat; + 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; @@ -38,7 +44,7 @@ namespace engine3d{ m_Model = p_Model; } - void SetRotation(float& p_Rotation){ + void SetRotation(glm::vec3& p_Rotation){ m_Transform2D.rotation = p_Rotation; } @@ -55,6 +61,7 @@ namespace engine3d{ Transform2DComponent& GetTransform() { return m_Transform2D; } Transform2DComponent m_Transform2D; + RigidBody2dComponent RigidBodyComponent; private: SceneObject(id_t p_Id); diff --git a/engine3d/Core/Scene/SceneTest.hpp b/engine3d/Core/Scene/SceneTest.hpp new file mode 100644 index 0000000..3d4f7d3 --- /dev/null +++ b/engine3d/Core/Scene/SceneTest.hpp @@ -0,0 +1,120 @@ +#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/Components/GameComponent.hpp b/engine3d/Core/SceneManagment/Components/GameComponent.hpp index f846f05..3fe4dcb 100644 --- a/engine3d/Core/SceneManagment/Components/GameComponent.hpp +++ b/engine3d/Core/SceneManagment/Components/GameComponent.hpp @@ -1,10 +1,11 @@ #pragma once -#include +// #include #include namespace engine3d { + class SceneObject; class GameComponent { public: diff --git a/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp b/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp new file mode 100644 index 0000000..8328216 --- /dev/null +++ b/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp @@ -0,0 +1,100 @@ +#pragma once + +#include +#include +#include + +namespace engine3d{ + class EditorCamera : public GameComponent{ + public: + struct CameraData{ + float PerspectiveFOV = glm::radians(45.0f); + float PerspectiveNear = 0.01; + float PerspectiveFar = 1000.0f; + float OrthographicSize = 10.0f; + float OrthographicNear = 1.0f; + float OrthographicFar = 1.0f; + }; + + enum CameraProjectionType{ + UNDEFINED = -1, + ORTHOGRAPHIC = 0, + PERSPECTIVE = 1 + }; + + + EditorCamera() = default; + EditorCamera(const std::string& p_Tag); + + void SetInitialProperties(float p_Fov, float p_NearClip, float p_FarClip); + + // --------------------- + //! @note From tutorial. + // --------------------- + void SetOrthoProjection(float left, float right, float top, float bottom, float near, float far); + void SetPerspectiveProjection(float fovy, float aspect, float near, float far); + + //! @note Used to get camera to view at a specific direction + void SetViewDirection(glm::vec3 Position, glm::vec3 Direction, glm::vec3 Up = glm::vec3(0.f, -1.f, 0.f)); + + //! @note Used to get the camera to view at a specific target. + void SetViewTarget(glm::vec3 Position, glm::vec3 Target, glm::vec3 Up = glm::vec3(0.f, -1.f, 0.f)); + + //! @note Euler angles to specify the rotation of transforms for orienting the camera. + void SetViewXYZ(glm::vec3 Position, glm::vec3 Rotation); + // --------------------- + //! @note From tutorial. + // --------------------- + + + + void OnUpdate(); + + //! @note Eventually we will refer to OnIntegrate as OnCreate or OnPlay or something. + //! @note Come up with a better name for this for accurate API. + void OnIntegrate(); + + glm::quat GetOrientation() const; + glm::vec3 GetUpDirection() const; + glm::vec3 GetRightDirection() const; + glm::vec3 GetForwardDirection() const; + + glm::mat4 GetProjection() const { return m_ProjectionMatrix; } + glm::mat4 GetView() const { return m_ViewMatrix; } + + + //! @note Getting pans. + void CameraPan(const glm::vec2& p_Data); + void CameraZoom(float delta); + void CameraRotate(const glm::vec2& p_Data); + + private: + glm::vec3 CalculateNewPosition() const; + void RecalculateView(); + + void RecalculateProjection(); + + glm::vec3 RecalculatePosition() const; + + glm::vec2 PanSpeed() const; + float ZoomSpeed() const; + + float RotationSpeed() const; + + private: + std::string m_Tag; + glm::vec2 m_InitialMousePosition = {0.0f, 0.0f}; + float m_Fov = 45.0f, m_AspectRatio = 1.778f, m_NearClip=0.1f, m_FarClip = 1000.0f; + + glm::mat4 m_ViewMatrix; + glm::mat4 m_ProjectionMatrix; + glm::vec3 m_Position; + glm::vec3 m_FocalPoint; + //! @note Moving this out of Editor Camera. + // glm::vec2 m_InitialMousePosition + float m_Distance = 10.0f; + float m_Pitch = 0.f, m_Yaw = 0.f; + glm::vec2 m_ViewportSize; + + }; +}; \ No newline at end of file diff --git a/engine3d/Core/SceneManagment/SceneObjects/SceneObject.hpp b/engine3d/Core/SceneManagment/SceneObjects/SceneObject.hpp index 9a179c8..91e7e6b 100644 --- a/engine3d/Core/SceneManagment/SceneObjects/SceneObject.hpp +++ b/engine3d/Core/SceneManagment/SceneObjects/SceneObject.hpp @@ -5,11 +5,19 @@ * @param id - the ID of the SceneObject itself * @note actual wrapper for SceneObject for ECS * */ +#include #include #include + #include #include +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include +#include +#include + namespace engine3d { class SceneObject @@ -61,6 +69,14 @@ namespace engine3d m_ParentScene->m_SceneRegistry.remove(SceneObjectHandler); } + void SetModal(Ref& p_Model){ + m_Model = p_Model; + } + + glm::mat4 toMat4(); + + Ref& GetModel() { return m_Model; } + operator bool() const { return SceneObjectHandler != entt::null; } operator entt::entity() const { return SceneObjectHandler; } @@ -73,6 +89,11 @@ namespace engine3d m_ParentScene == other.m_ParentScene; } + glm::vec3 GetRotation(); + void SetRotation(const glm::vec3& p_Rotation); + + void SetPosition(const glm::vec3& p_Position); + bool operator!=(const SceneObject &other) const { return !(*this == other); } UUID GetUUID() { return objectID; } @@ -81,5 +102,6 @@ namespace engine3d entt::entity SceneObjectHandler{entt::null}; UUID objectID; Scene *m_ParentScene = nullptr; // 12 bytes + Ref m_Model; }; }; // namespace Engine3D diff --git a/engine3d/Core/Window.hpp b/engine3d/Core/Window.hpp index b445030..0eec118 100644 --- a/engine3d/Core/Window.hpp +++ b/engine3d/Core/Window.hpp @@ -48,6 +48,8 @@ namespace engine3d{ uint32_t GetWidth() const; uint32_t GetHeight() const; std::string GetTitle() const; + + float GetAspectRatio() const; void OnUpdateAllFrames(); protected: diff --git a/engine3d/Core/internal/Vulkan2Showcase/VulkanImGuiLayer.hpp b/engine3d/Core/internal/Vulkan2Showcase/VulkanImGuiLayer.hpp new file mode 100644 index 0000000..417a202 --- /dev/null +++ b/engine3d/Core/internal/Vulkan2Showcase/VulkanImGuiLayer.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace engine3d::vk{ + class VulkanImGuiLayer{ + public: + static void Initialize(); + static void BeginFrame(); + static void EndFrame(); + }; +}; \ No newline at end of file diff --git a/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp b/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp index bf3bb4c..e8a8a29 100644 --- a/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp +++ b/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.hpp @@ -16,7 +16,7 @@ namespace engine3d::vk{ public: //! @note Can be the way we structure what data types is accepted in our shader. struct Vertex{ - glm::vec2 Position; + glm::vec3 Position; glm::vec3 Color; static std::vector GetVertexInputBindDescription(); diff --git a/engine3d/Core/internal/Vulkan2Showcase/VulkanRenderer.hpp b/engine3d/Core/internal/Vulkan2Showcase/VulkanRenderer.hpp new file mode 100644 index 0000000..33d8ceb --- /dev/null +++ b/engine3d/Core/internal/Vulkan2Showcase/VulkanRenderer.hpp @@ -0,0 +1,41 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +namespace engine3d::vk{ + class VulkanRenderer : public RendererBackend{ + public: + VulkanRenderer(const std::string& p_DebugName); + + private: + void StartFrame() override; + void FinishFrame() override; + + template + void DrawMesh(const UFunction& p_Function){ + + } + + private: + VkCommandBuffer CurrentCommandBuffer(); + + private: + Ref m_Swapchain; + VkPipeline m_Pipeline; + std::vector m_CommandBuffers; + uint32_t m_CurrentFrameIndex = -1; + bool m_IsFrameStarted = false; + + //! @note Editor application, Engine, UI Layer. + float m_LastFrameTime = 0.0f; + //! @note Essentially our VulkanShader is VkPipeline, ShaderModule all in one. + Ref m_Shader; + VkPipelineLayout m_PipelineLayout; + VkCommandPool m_CommandPool; + std::vector m_GameObjects; + }; +}; \ No newline at end of file diff --git a/engine3d/Math/Interpolation.hpp b/engine3d/Math/Interpolation.hpp index f708019..c455f2f 100644 --- a/engine3d/Math/Interpolation.hpp +++ b/engine3d/Math/Interpolation.hpp @@ -18,17 +18,18 @@ namespace engine3d class Interpolation { public: - template - static T& LinearInterpolate(T start, T end, Func function, float t) + template + static T LinearInterpolate(T start, T end, std::function function, float t) { float l_AdjustedTime = 0.0f; - if(function == NULL) + if(!function) { l_AdjustedTime = t; } else { - l_AdjustedTime = function(t); + const float f = function(t); + l_AdjustedTime = f; } diff --git a/sim_shader_transforms/simple_shader.frag b/sim_shader_transforms/simple_shader.frag index e877f26..2c2e18e 100644 --- a/sim_shader_transforms/simple_shader.frag +++ b/sim_shader_transforms/simple_shader.frag @@ -1,17 +1,17 @@ #version 450 -// layout(location = 0) in vec3 fragColor; +layout(location = 0) in vec3 fragColor; layout(location = 0) out vec4 outColor; layout(push_constant) uniform Push { - mat2 transform; - vec2 offset; + mat4 transform; + vec2 iResolution; vec3 color; } push; void main(){ // outColor = vec4(fragColor, 0.0); - outColor = vec4(push.color, 1.0); + outColor = vec4(fragColor, 1.0); } \ No newline at end of file diff --git a/sim_shader_transforms/simple_shader.frag.spv b/sim_shader_transforms/simple_shader.frag.spv index 96bd40bcd224ff1cde3b85238beb211147efe8f9..c07daa5904668771c6f14aa988b958818417662f 100644 GIT binary patch literal 864 zcmYk3OH0F05QRr;YJJpN-z!mE3gSXV5Fa3tE(%ut0imR+24Yf@RB-2S^H;eMJYSkf zZkRiF&OLYL%%m}Yv}iVGE7rC>o2{c6vlx!3e&*P!;b)7 ztHujsKbnr}1V@Z|UB{fFb~4THlPF15PKZ-z8K=Y73gq3k?grEB4Ng-ZF)L4paW+oU zcbzMoKEvlAOQLBWCb4j*;M0#^79!`a;z^m)Rm>drWh`Q5QpLSe19y(BSLP%>V$-T) z%y7Mip$Xny4MPvuK;~-R^aQr49e-H-g&DS^b5q!XY*$*yPs}_Iw72B>%ivdKOg~`o zm*wFT!(Wj{6F7Wg`UN{y40lat_I10bcvB~GQ;&RG@^H|(U(Ny!I60}YBaa69q6RTD z2fNT4-ja`nt{)-H_F5=8+A=gf38hx2#N_@lW(*JA6|Y-j+AiXbW?VtWuW`T$GlN(<_Wy9T}W+4@wy1fAb{ zt-UgL?wmP0XJ*W;w%5#7Y{Rzf$QGw&t3pgzG4)>erQ3;s=HRoOz<* zFbAoVJWr>>r-m|2@L&ZE=gT}b0aUE(&E&ZE=f&2CB=41IXj42C{fOXg{h z^a-}B8-JW`cY*EeT@==mHPj3BiP=+2H(LC2;8!lDe=zuEdHB>lP`n`zMofNFo;>`f zn`6l#hsL%%HR%%#Vs-{rSIlfJ*^2DM#WlUr;QJkUxYQ$$UR`GPd3zyEc+A70k8dID z`c60B?#j@k=CzpA*^_}ki$lGAhsph64TsU!l6T-RG<(__dpnfzJ$&-?cB4+{ncR;i HyOjL`c4j@C diff --git a/sim_shader_transforms/simple_shader.vert b/sim_shader_transforms/simple_shader.vert index c5c6daa..709464b 100644 --- a/sim_shader_transforms/simple_shader.vert +++ b/sim_shader_transforms/simple_shader.vert @@ -1,18 +1,47 @@ #version 450 -layout(location = 0) in vec2 Position; +//! @note Position and Color used in the Vertex class. +layout(location = 0) in vec3 Position; layout(location = 1) in vec3 Color; -// layout(location = 0) out vec3 fragColor; +layout(location = 0) out vec3 fragColor; layout(push_constant) uniform Push { - mat2 transform; - vec2 offset; + mat4 transform; + mat4 Projection; + vec2 iResolution; vec3 color; } push; void main(){ // gl_Position = vec4(Position, 0.0, 1.0); - gl_Position = vec4(push.transform * Position + push.offset, 0.0, 1.0); - // fragColor = Color; -} \ No newline at end of file + // mat2 p = (push.transform * 1.f); + // p[0] *= (push.iResolution.y / push.iResolution.x); + // p[1] *= (push.iResolution.y / push.iResolution.x); + // gl_Position = vec4(p * Position + push.offset, 0.0, 1.0); + // mat4 p = push.transform; + // p[0] /= (push.iResolution.x / push.iResolution.y); + // p[1] /= (push.iResolution.x / push.iResolution.y); + // p[2] /= (push.iResolution.x / push.iResolution.y); + // gl_Position = p * vec4(Position, 1.0); + gl_Position = push.transform * vec4(Position, 1.0); + fragColor = Color; +} + +// layout(location = 0) in vec2 Position; +// layout(location = 1) in vec3 Color; + +// // layout(location = 0) out vec3 fragColor; + +// layout(push_constant) uniform Push { +// mat2 transform; +// vec2 offset; +// vec2 iResolution; +// vec3 color; +// } push; + +// void main(){ +// // gl_Position = vec4(Position, 0.0, 1.0); +// gl_Position = vec4(push.transform * Position + push.offset, 0.0, 1.0); +// // fragColor = Color; +// } diff --git a/sim_shader_transforms/simple_shader.vert.spv b/sim_shader_transforms/simple_shader.vert.spv index 96e5da90f7d823df928d3f78c8d1958265dd508e..168989782ffa0df4ed3c52efba55376ada40364f 100644 GIT binary patch delta 657 zcmZ8f%Syvg5S=7VL_(<1MH*jijYh?lhz}57xUd_cx_1$zN~ARqb!``dTf<$6U*V!( z;Mza%R|L<=qYG0`@0^))XXb>w*gv*o=)r`DCbl@TmO0zaN#Roi22p&^*a2pN6(Ib) zYHPce%plf)ZkpWnt{>wBN((>cbs)a%JtY07ykLo$=MD9)ZF*+j=O&GAPm_LxDDc30IAbt!G9psC=BljOfxV-NTP DWo|J+ delta 534 zcmXw$Jx;?w5QTSb6F`h{j{HkJjk5#mpfTt7GGh6Y z`d*hr8Le`_x=-I|BXD6*%Xm(#oGv;S~M> D2?8Vu diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 186270a..a29ffa6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,8 +7,8 @@ set(VULKAN_SRC_DIR ${ENGINE_SRC_DIR}/Core/internal/Vulkan2Showcase) set(VULKAN_SHADERS_INCLUDE_DIR ${VULKAN_INCLUDE_DIR}/Shaders) set(VULKAN_SHADERS_SRC_DIR ${VULKAN_SRC_DIR}/Shaders) -build_library( - SOURCES +add_library( + ${PROJECT_NAME} ${ENGINE_INCLUDE_NAME}/Core/ApplicationInstance.hpp ${ENGINE_INCLUDE_NAME}/Core/EngineLogger.hpp @@ -57,13 +57,16 @@ build_library( # Special Component Includes ${ENGINE_INCLUDE_NAME}/Core/SceneManagment/Components/SPComps/Transform.hpp + ${ENGINE_INCLUDE_NAME}/Core/SceneManagment/Components/SPComps/EditorCamera.hpp # Renderer Includes ${ENGINE_INCLUDE_NAME}/Core/Renderer/Renderer.hpp + ############################# # Source .cpp files below ############################# + ${ENGINE_SRC_DIR}/Core/ApplicationInstance.cpp @@ -103,6 +106,7 @@ build_library( ${ENGINE_SRC_DIR}/Core/ApplicationManager/GameObjManager/UUID.cpp ${ENGINE_SRC_DIR}/Core/SceneManagment/Components/SPComps/Transform.cpp + ${ENGINE_SRC_DIR}/Core/SceneManagment/Components/SPComps/EditorCamera.cpp ${ENGINE_SRC_DIR}/Core/Renderer/Renderer.cpp diff --git a/src/engine3d/Core/ApplicationInstance.cpp b/src/engine3d/Core/ApplicationInstance.cpp index 34d9dc4..03b990d 100644 --- a/src/engine3d/Core/ApplicationInstance.cpp +++ b/src/engine3d/Core/ApplicationInstance.cpp @@ -1,4 +1,5 @@ -#include "TimeManagement/GlobalUpdateManager.hpp" +#include "Renderer/Renderer.hpp" +// #include "TimeManagement/GlobalUpdateManager.hpp" #include #include #include @@ -17,6 +18,8 @@ namespace engine3d{ g_DebugName = p_DebugName; SetCurrentAPI(VULKAN); m_Window = Window::Create(900, 600, p_DebugName); + + Renderer::Initialize("Engine3D Renderer"); } void ApplicationInstance::ExecuteApplicationMainloop(){ @@ -26,16 +29,9 @@ namespace engine3d{ InputPoll::UpdateEvents(); // FrameTimer::UpdateFrameTimer(); // give us the frames in flight. - // Renderer::Presentation(); - // m_Window->GetCurrentSwapchain()->BeginFrame(); - // Renderer::BeginFrame(); - - // Renderer::SetBackgroundColor({1.0f, 0.0f, 0.0f, 0.0f}); - // UpdateCurrentApplicationInstance(); - - // Renderer::EndFrame(); - + Renderer::BeginFrame(); m_Window->OnUpdateAllFrames(); + Renderer::EndFrame(); } //! @note Cleaning up imgui diff --git a/src/engine3d/Core/Renderer/Renderer.cpp b/src/engine3d/Core/Renderer/Renderer.cpp index e17294d..999beb7 100644 --- a/src/engine3d/Core/Renderer/Renderer.cpp +++ b/src/engine3d/Core/Renderer/Renderer.cpp @@ -1,70 +1,254 @@ // #include "internal/VulkanCpp/Vulkan.hpp" +#include "EngineLogger.hpp" +#include "GraphicDrivers/Shader.hpp" +#include "TimeManagement/UpdateManagers/SyncUpdateManager.hpp" +#include "internal/Vulkan2Showcase/Shaders/VulkanShader.hpp" +#include "internal/Vulkan2Showcase/VulkanContext.hpp" +#include "internal/Vulkan2Showcase/VulkanModel.hpp" +#include "internal/Vulkan2Showcase/helper_functions.hpp" #include #include +#include #include +#include + +#include +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include +#include + +#include namespace engine3d{ - static RendererSettings g_Settings; + static std::vector g_CommandBuffers; + VkCommandPool g_CmdPool; + uint32_t g_CurrentFrameIndex = -1; + static bool g_IsFrameStarted = false; + + static Ref g_Shader = nullptr; + static VkPipelineLayout g_PipelineLayout; + static VkPipeline g_Pipeline; + + //! @note Push Constants to apply data to the shaders. + //! @note vk::VulkanModal is how shaders are going to be + struct SimplePushConstantData{ + glm::mat4 Transform{1.f}; + glm::vec2 iResolution; + alignas(16) glm::vec3 Color; + }; + + void Renderer::Initialize(const std::string& p_DebugName){ + ConsoleLogInfo("RENDERER BEGIN INTIALIZATION!!!"); + g_CurrentFrameIndex = 0; + g_IsFrameStarted = false; + + //! @note Setting up our pipeline. + auto pipeline_config = vk::VulkanShader::shader_configuration(ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()); + + //! @note Initialize Push constant range + VkPushConstantRange push_const_range = { + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + .offset = 0, + .size = sizeof(SimplePushConstantData) + }; + + //! @note First initializing pipeline layout create info + VkPipelineLayoutCreateInfo pipeline_layout_create_info = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .setLayoutCount = 0, + .pSetLayouts = nullptr, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &push_const_range + }; + + vk::vk_check(vkCreatePipelineLayout(vk::VulkanContext::GetDriver(), &pipeline_layout_create_info, nullptr, &g_PipelineLayout), "vkCreatePipelineLayout", __FILE__, __LINE__, __FUNCTION__); + - RendererSettings& Renderer::GetSettings(){ - return g_Settings; - } + //! @note We are setting our shader pipeline to utilize our current window's swapchain + //! @note a TODO is to utilize different render passes utiization for shader pipelines, potentially. + pipeline_config.PipelineRenderPass = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetRenderPass(); + pipeline_config.PipelineLayout = g_PipelineLayout; - // Ref g_CurrentSwapchain = nullptr; - VkCommandBuffer g_ActiveCommandBuffer = nullptr; - VkRenderPass g_RenderPass; + // m_Shader = Shader::Create("simple_shader/simple_shader.vert.spv", "simple_shader/simple_shader.frag.spv", pipeline_config); + g_Shader = Shader::Create("sim_shader_transforms/simple_shader.vert.spv", "sim_shader_transforms/simple_shader.frag.spv", pipeline_config); - void Renderer::BeginFrame(){ - // g_CurrentSwapchain = ApplicationInstance::GetWindow().GetCurrentSwapchain(); - + ConsoleLogError("NOT AN ERROR: Shader Loaded Successfully!"); + + + //! @note Initializing Command buffers. + g_CommandBuffers.resize(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetImagesSize()); + + VkCommandPoolCreateInfo pool_create_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .pNext = nullptr, + .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, + .queueFamilyIndex = vk::VulkanContext::GetPhysicalDriver().GetQueueIndices().Graphics + }; + + vk::vk_check(vkCreateCommandPool(vk::VulkanContext::GetDriver(), &pool_create_info, nullptr, &g_CmdPool), "vkCreateCommandPool", __FILE__, __LINE__, __FUNCTION__); + + + ConsoleLogInfo("RENDERER COMMAND BUFFERS SIZE === {}", g_CommandBuffers.size()); + //! @note Allocating our command buffers. + VkCommandBufferAllocateInfo cmd_buffer_alloc_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = g_CmdPool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = static_cast(g_CommandBuffers.size()), + }; + + vk::vk_check(vkAllocateCommandBuffers(vk::VulkanContext::GetDriver(), &cmd_buffer_alloc_info, g_CommandBuffers.data()), "vkAllocateCommandBuffers", __FILE__, __LINE__, __FUNCTION__); + + ConsoleLogInfo("CommandBuffers Size === {}", g_CommandBuffers.size()); } void Renderer::SetBackgroundColor(const std::array& p_Rgba){ VkClearValue clearColorValue = {{p_Rgba[0], p_Rgba[1], p_Rgba[2], p_Rgba[3]}}; } + void Renderer::BeginFrame(){ + g_CurrentFrameIndex = ApplicationInstance::GetWindow().GetCurrentSwapchain()->AcquireNextImage(); + VkCommandBufferBeginInfo cmd_buffer_begin_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO + }; + + auto cmd_buffer = GetCurrentCommandBuffer(); + + vk::vk_check(vkBeginCommandBuffer(cmd_buffer, &cmd_buffer_begin_info), "vkBeginCommandBuffer", __FILE__, __LINE__, __FUNCTION__); + VkRenderPassBeginInfo rp_begin_info = { + .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, + .renderPass = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetRenderPass(), + .framebuffer = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetFramebuffer(g_CurrentFrameIndex), // Specifying which framebuffer to render pass to. + .renderArea = { + .offset = {0, 0}, + .extent = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()} + } + }; + + std::array clearValues; + clearValues[0].color = {0.1f, 0.1f, 0.1f, 1.0f}; + clearValues[1].depthStencil = {1.0f, 0}; + + rp_begin_info.clearValueCount = static_cast(clearValues.size()); + rp_begin_info.pClearValues = clearValues.data(); + + + vkCmdBeginRenderPass(cmd_buffer, &rp_begin_info, VK_SUBPASS_CONTENTS_INLINE); + g_IsFrameStarted = true; + + VkViewport viewport = { + .x = 0.0f, + .y = 0.0f, + .width = static_cast(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetSwapchainExtent().width), + .height = static_cast(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetSwapchainExtent().height), + .maxDepth = 1.0f, + }; - /* - //! @note This gets submitted into our renderer queue. - //! @note Few properties of how submission to the queue will work. + VkRect2D scissor = { + .offset = {0, 0}, + .extent = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetSwapchainExtent() + }; - @note These are the things we (as the renderer) should keep track of - 1.) Queues submission idx to know the queue we are submitting to. - 2.) Specify what queue to allocate to our tasks to. - 3.) Example is at the end of frame we execute all of our queue's - Example - static uint32_t s_SubmissionCommandQueueCount = 2; - //! @note Atomic are just thread-safe counters. Which we will use for reading in queue's that we are submitting into. - static std::atomic s_SubmissionQueueIdx = 0; // thread-safe in reading the data from queue's that will be needed to render - Renderer::BeginFrame(){ - // Probably would do some initialization - s_CurrentRendererCommandQueue[0] = new RenderCommandQueue(); - s_CurrentRendererCommandQueue[1] = new RenderCommandQueue(); - } - //! @note In our renderer thread we would utilize this to make sure that in our renderer thread. - //! @note This would be happening in our next frame. (Where we would do something like double-buffering or something like that) - void Renderer::NextSubmittedQueueInThread(){ - s_SubmissionQueueIdx = (s_SubmissionQueueIdx + 1) % s_SubmissionCommandQueueCount; - } + g_CommandBuffers[g_CurrentFrameIndex] = cmd_buffer; - Renderer::EndFrame(){ - s_CurrentRendererCommandQueue[s_SubmissionQueueIdx]->Execute(); - } + vkCmdSetViewport(g_CommandBuffers[g_CurrentFrameIndex], 0, 1, &viewport); + vkCmdSetScissor(g_CommandBuffers[g_CurrentFrameIndex], 0, 1, &scissor); - while(IsActive()){ - Renderer::BeginFrame(); + } - Renderer::EndFrame(); + 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(); - //! @note This is what the API, would look. - Renderer::Submit([](){ - }); - */ + //! @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()); + float delta_time = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime; + // ConsoleLogWarn("Delta Time = {:.7}", delta_time); + // auto projection_view = + auto camera_component = p_CameraObject->SceneGetComponent(); + + //! @note Only for testing purposes for mesh data. + for(auto& obj : p_Objects){ + // auto camera_component = obj->SceneGetComponent(); + auto proj_view = camera_component.GetProjection() * camera_component.GetView(); + + // obj.m_Transform2D.rotation.y = glm::mod(obj.GetTransform().rotation.y + 0.001f, glm::two_pi()); + // float x = obj->GetRotation().x; + // float y = glm::mod(obj->GetRotation().y + (0.1f * delta_time), glm::two_pi()); + // float x = glm::mod(obj->GetRotation().x + (0.05f * delta_time), glm::two_pi()); + // float z = obj->GetRotation().z; + + // glm::vec3 new_position = {x, y, z}; + // new_position = glm::normalize(new_position); + // obj->SetRotation(new_position); + + SimplePushConstantData push = { + .Transform = proj_view * obj->toMat4(), + // .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::EndFrame(){ + auto cmd_buffer = GetCurrentCommandBuffer(); + vkCmdEndRenderPass(cmd_buffer); + vk::vk_check(vkEndCommandBuffer(cmd_buffer), "vkEndCommandBuffer", __FILE__, __LINE__, __FUNCTION__); + + ApplicationInstance::GetWindow().GetCurrentSwapchain()->SubmitCommandBuffer(&cmd_buffer); + g_IsFrameStarted = false; } + + VkCommandBuffer Renderer::GetCurrentCommandBuffer(){ + return g_CommandBuffers[g_CurrentFrameIndex]; + } + }; \ No newline at end of file diff --git a/src/engine3d/Core/Renderer/RendererBackend.cpp b/src/engine3d/Core/Renderer/RendererBackend.cpp new file mode 100644 index 0000000..a831818 --- /dev/null +++ b/src/engine3d/Core/Renderer/RendererBackend.cpp @@ -0,0 +1,23 @@ +#include +#include +#include + +namespace engine3d{ + + Ref RendererBackend::InitializeRenderer(const std::string& p_DebugName){ + switch (ApplicationInstance::CurrentAPI()){ + case API::VULKAN: return CreateRef(p_DebugName); + default: return nullptr; + } + + return nullptr; + } + + void RendererBackend::BeginFrame(){ + return StartFrame(); + } + + void RendererBackend::EndFrame(){ + return FinishFrame(); + } +}; \ No newline at end of file diff --git a/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp b/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp new file mode 100644 index 0000000..1837e4e --- /dev/null +++ b/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp @@ -0,0 +1,253 @@ +// #include "EditorCamera.hpp" +#include +#include +#include +#include +#include +#include + +#define GLM_ENABLE_EXPERIMENTAL +#include + +#include +#include +#include + +namespace engine3d{ + EditorCamera::EditorCamera(const std::string& p_Tag) : m_Tag(p_Tag) {} + + void EditorCamera::OnIntegrate(){ + } + + void EditorCamera::OnUpdate(){ + if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT_ALT)){ + glm::vec2 delta = (InputPoll::GetMousePosition() - m_InitialMousePosition) * 0.003f; + m_InitialMousePosition = InputPoll::GetMousePosition(); + + if(InputPoll::IsMousePressed(Mouse::ButtonMiddle)){ + CameraPan(delta); + } + else if(InputPoll::IsMousePressed(Mouse::ButtonLast)){ + CameraRotate(delta); + } + else if(InputPoll::IsMousePressed(Mouse::ButtonRight)){ + CameraZoom(delta.y); + } + } + + RecalculateView(); + } + + void EditorCamera::SetInitialProperties(float p_Fov, float p_NearClip, float p_FarClip){ + m_Fov = p_Fov; + m_AspectRatio = ((float)ApplicationInstance::GetWindow().GetWidth() / (float)ApplicationInstance::GetWindow().GetHeight()); + m_NearClip = p_NearClip; + m_FarClip = p_FarClip; + + m_ViewportSize.x = (float)ApplicationInstance::GetWindow().GetWidth(); + m_ViewportSize.y = (float)ApplicationInstance::GetWindow().GetHeight(); + RecalculateView(); + } + + + /* + void EditorCamera::SetView(glm::vec3 Position, glm::vec3 Target, glm::vec3 Up){ + } + + void EditorCamera::SetViewXYZ(glm::vec3 Position, glm::vec3 Rotation){ + const float c3 = glm::cos(Rotation.z); + const float s3 = glm::sin(Rotation.z); + const float c2 = glm::cos(Rotation.x); + const float s2 = glm::sin(Rotation.x); + const float c1 = glm::cos(Rotation.y); + const float s1 = glm::sin(Rotation.y); + const glm::vec3 u{(c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)}; + const glm::vec3 v{(c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)}; + const glm::vec3 w{(c2 * s1), (-s2), (c1 * c2)}; + m_ViewMatrix = glm::mat4{1.f}; + m_ViewMatrix[0][0] = u.x; + m_ViewMatrix[1][0] = u.y; + m_ViewMatrix[2][0] = u.z; + m_ViewMatrix[0][1] = v.x; + m_ViewMatrix[1][1] = v.y; + m_ViewMatrix[2][1] = v.z; + m_ViewMatrix[0][2] = w.x; + m_ViewMatrix[1][2] = w.y; + m_ViewMatrix[2][2] = w.z; + m_ViewMatrix[3][0] = -glm::dot(u, Position); + m_ViewMatrix[3][1] = -glm::dot(v, Position); + m_ViewMatrix[3][2] = -glm::dot(w, Position); + } + + void EditorCamera::SetViewTarget(glm::vec3 Position, glm::vec3 Target, glm::vec3 Up){ + SetView(Position, Target - Position, Up); + } + */ + + void EditorCamera::SetViewDirection(glm::vec3 Position, glm::vec3 Direction, glm::vec3 Up){ + const glm::vec3 w{glm::normalize(Direction)}; + const glm::vec3 u{glm::normalize(glm::cross(w, Up))}; + const glm::vec3 v{glm::cross(w, u)}; + + m_ViewMatrix = glm::mat4{1.f}; + m_ViewMatrix[0][0] = u.x; + m_ViewMatrix[1][0] = u.y; + m_ViewMatrix[2][0] = u.z; + m_ViewMatrix[0][1] = v.x; + m_ViewMatrix[1][1] = v.y; + m_ViewMatrix[2][1] = v.z; + m_ViewMatrix[0][2] = w.x; + m_ViewMatrix[1][2] = w.y; + m_ViewMatrix[2][2] = w.z; + m_ViewMatrix[3][0] = -glm::dot(u, Position); + m_ViewMatrix[3][1] = -glm::dot(v, Position); + m_ViewMatrix[3][2] = -glm::dot(w, Position); + } + + //! @note Used to get the camera to view at a specific target. + void EditorCamera::SetViewTarget(glm::vec3 Position, glm::vec3 Target, glm::vec3 Up){ + SetViewDirection(Position, Target - Position, Up); + } + + //! @note Euler angles to specify the rotation of transforms for orienting the camera. + void EditorCamera::SetViewXYZ(glm::vec3 Position, glm::vec3 Rotation){ + const float c3 = glm::cos(Rotation.z); + const float s3 = glm::sin(Rotation.z); + const float c2 = glm::cos(Rotation.x); + const float s2 = glm::sin(Rotation.x); + const float c1 = glm::cos(Rotation.y); + const float s1 = glm::sin(Rotation.y); + const glm::vec3 u{(c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)}; + const glm::vec3 v{(c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)}; + const glm::vec3 w{(c2 * s1), (-s2), (c1 * c2)}; + m_ViewMatrix = glm::mat4{1.f}; + m_ViewMatrix[0][0] = u.x; + m_ViewMatrix[1][0] = u.y; + m_ViewMatrix[2][0] = u.z; + m_ViewMatrix[0][1] = v.x; + m_ViewMatrix[1][1] = v.y; + m_ViewMatrix[2][1] = v.z; + m_ViewMatrix[0][2] = w.x; + m_ViewMatrix[1][2] = w.y; + m_ViewMatrix[2][2] = w.z; + m_ViewMatrix[3][0] = -glm::dot(u, Position); + m_ViewMatrix[3][1] = -glm::dot(v, Position); + m_ViewMatrix[3][2] = -glm::dot(w, Position); + } + // --------------------- + + + void EditorCamera::SetOrthoProjection(float left, float right, float top, float bottom, float near, float far){ + m_ProjectionMatrix = glm::mat4{1.0f}; + m_ProjectionMatrix[0][0] = 2.f / (right - left); + m_ProjectionMatrix[1][1] = 2.f / (bottom - top); + m_ProjectionMatrix[2][2] = 1.f / (far - near); + m_ProjectionMatrix[3][0] = -(right + left) / (right - left); + m_ProjectionMatrix[3][1] = -(bottom + top) / (bottom - top); + m_ProjectionMatrix[3][2] = -near / (far - near); + } + + + void EditorCamera::SetPerspectiveProjection(float fovy, float aspect, float near, float far){ + assert(glm::abs(aspect - std::numeric_limits::epsilon()) > 0.0f); + const float tanHalfFovy = tan(fovy / 2.f); + m_ProjectionMatrix = glm::mat4{0.0f}; + m_ProjectionMatrix[0][0] = 1.f / (aspect * tanHalfFovy); + m_ProjectionMatrix[1][1] = 1.f / (tanHalfFovy); + m_ProjectionMatrix[2][2] = far / (far - near); + m_ProjectionMatrix[2][3] = 1.f; + m_ProjectionMatrix[3][2] = -(far * near) / (far - near); + } + + + + + + + + + + + + + + glm::quat EditorCamera::GetOrientation() const { + return glm::quat(glm::vec3(-m_Pitch, -m_Yaw, 1.0f)); + } + + glm::vec3 EditorCamera::GetUpDirection() const{ + return glm::rotate(GetOrientation(), glm::vec3{0.0f, 1.0f, 0.0f}); + } + + glm::vec3 EditorCamera::GetRightDirection() const { + return glm::rotate(GetOrientation(), glm::vec3(1.0f, 0.0f, 0.0f)); + } + + glm::vec3 EditorCamera::GetForwardDirection() const { + return glm::rotate(GetOrientation(), glm::vec3(0.0f, 0.0f, -1.0f)); + } + + + //! @note Getting pans. + void EditorCamera::CameraPan(const glm::vec2& p_Data){} + + void EditorCamera::CameraZoom(float delta) { + // float dt = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime; + m_Distance -= (delta * ZoomSpeed()); + + if(m_Distance < 1.0f){ + m_FocalPoint += GetForwardDirection(); + m_Distance = 1.0f; + } + } + + void EditorCamera::CameraRotate(const glm::vec2& p_Position) { + float yawSign = GetUpDirection().y < 0 ? -1.0f : 1.0f; + m_Yaw += yawSign * p_Position.x * RotationSpeed(); + m_Pitch += p_Position.y * RotationSpeed(); + } + + float EditorCamera::RotationSpeed() const { + return 0.8f; + } + + glm::vec3 EditorCamera::CalculateNewPosition() const{ + return m_FocalPoint - GetForwardDirection() * m_Distance; + } + + float EditorCamera::ZoomSpeed() const{ + float distance = m_Distance * 0.2f; + distance = std::max(distance, 0.0f); + + float speed = distance * distance; + speed = std::min(speed, 100.0f); + return speed; + } + + void EditorCamera::RecalculateProjection() { + m_ProjectionMatrix = glm::perspective(glm::radians(m_Fov), m_AspectRatio, m_NearClip, m_FarClip); + RecalculateView(); + } + + void EditorCamera::RecalculateView() { + m_Position = CalculateNewPosition(); + glm::quat orientation = GetOrientation(); + m_ViewMatrix = glm::translate(glm::mat4(1.0f), m_Position) * glm::toMat4(orientation); + m_ViewMatrix = glm::inverse(m_ViewMatrix); + } + + glm::vec3 EditorCamera::RecalculatePosition() const { + return m_FocalPoint * GetForwardDirection() * m_Distance; + + } + + glm::vec2 EditorCamera::PanSpeed() const{ + float x = std::min(m_ViewportSize.x / 1000.0f, 2.4f); // max = 2.4.f + float xFactor = 0.0366f * (x * x) - 0.1778f * x + 0.3021f; + + float y = std::min(m_ViewportSize.y / 1000.0f, 2.4f); // max = 2.4f + float yFactor = 0.0366f * (y * y) - 0.1778f * y + 0.3021f; + + return {xFactor, yFactor}; + } +}; \ No newline at end of file diff --git a/src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp b/src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp index 415077a..be84f1d 100644 --- a/src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp +++ b/src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp @@ -1,6 +1,5 @@ #include #include -#include "EngineLogger.hpp" namespace engine3d { SceneObject::SceneObject(entt::entity handle, Scene *scene) @@ -13,7 +12,32 @@ namespace engine3d { { m_ParentScene = scene; SceneObjectHandler = scene->m_SceneRegistry.create(); - ConsoleLogInfo("Entity Registered"); AddComponent(); } + + void SceneObject::SetPosition(const glm::vec3& p_Position){ + SceneGetComponent().m_Position = p_Position; + } + + void SceneObject::SetRotation(const glm::vec3& p_Rotation){ + SceneGetComponent().m_AxisRotation = p_Rotation; + } + + glm::vec3 SceneObject::GetRotation(){ + return SceneGetComponent().m_AxisRotation; + } + + glm::mat4 SceneObject::toMat4(){ + auto transform_component = SceneGetComponent(); + + auto transform = glm::translate(glm::mat4{1.f}, transform_component.m_Position); + transform[0][0] *= transform_component.m_Scale.x; + transform[1][1] *= transform_component.m_Scale.y; + transform[2][2] *= transform_component.m_Scale.z; + + transform = glm::rotate(transform, transform_component.m_AxisRotation.y, {0.f, 1.f, 0.f}); + transform = glm::rotate(transform, transform_component.m_AxisRotation.x, {1.f, 0.f, 0.f}); + transform = glm::rotate(transform, transform_component.m_AxisRotation.z, {0.f, 0.f, 1.f}); + return transform; + } }; // namespace Engine3D diff --git a/src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp b/src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp index 4ecd224..0505f85 100644 --- a/src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp +++ b/src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp @@ -33,7 +33,8 @@ namespace engine3d m_GlobalDeltaTime = 0.0; m_UpdateTime = m_GlobalTimer->GetCurrentTime(); - m_MaxFPS = 90; + // m_MaxFPS = 5000; + m_MaxFPS = 500; m_FPSCounter = 1; //! @note syncupdatemanager will not work until the thread manager works. diff --git a/src/engine3d/Core/Window.cpp b/src/engine3d/Core/Window.cpp index 2ff3d3d..e16f30f 100644 --- a/src/engine3d/Core/Window.cpp +++ b/src/engine3d/Core/Window.cpp @@ -52,6 +52,10 @@ namespace engine3d{ return Title(); } + float Window::GetAspectRatio() const{ + return (float)GetWidth() / (float)GetHeight(); + } + void Window::OnUpdateAllFrames(){ GlobalUpdateManager::GetInstance()->GlobalOnTickUpdate(); diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanImGuiLayer.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanImGuiLayer.cpp new file mode 100644 index 0000000..e3cefea --- /dev/null +++ b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanImGuiLayer.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace engine3d::vk{ + void VulkanImGuiLayer::Initialize() { + ConsoleLogInfo("Vulkan2Showcase: Begin Initializing VulkanImGuiLayer!!!"); + //! @note Setting up imgui stuff. + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking + io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows + //io.ConfigViewportsNoAutoMerge = true; + //io.ConfigViewportsNoTaskBarIcon = true; + + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + //ImGui::StyleColorsClassic(); + + ImGuiStyle& style = ImGui::GetStyle(); + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + style.WindowRounding = 0.0f; + style.Colors[ImGuiCol_WindowBg].w = 1.0f; + } + + // Setup Platform/Renderer backends + // ImGui_ImplGlfw_InitForVulkan(ApplicationInstance::GetWindow().GetNativeWindow(), true); + ImGui_ImplGlfw_InitForVulkan(ApplicationInstance::GetWindow().GetNativeWindow(), true); + ImGui_ImplVulkan_InitInfo init_info = {}; + init_info.Instance = VulkanContext::GetVkInstance(); + init_info.PhysicalDevice = VulkanContext::GetPhysicalDriver(); + init_info.Device = VulkanContext::GetDriver(); + init_info.QueueFamily = VulkanContext::GetPhysicalDriver().GetQueueIndices().Graphics; + init_info.Queue = VK_NULL_HANDLE; + init_info.PipelineCache = VK_NULL_HANDLE; + init_info.DescriptorPool = VK_NULL_HANDLE; + init_info.Subpass = 0; + init_info.MinImageCount = 2; + init_info.ImageCount = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetImagesSize(); + init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT; + init_info.Allocator = nullptr; + // init_info.CheckVkResultFn = check_vk_result; + // ImGui_ImplVulkan_Init(&init_info); + ImGui_ImplGlfw_InitForVulkan(ApplicationInstance::GetWindow().GetNativeWindow(), false); + + // Load default font + ImFontConfig fontConfig; + fontConfig.FontDataOwnedByAtlas = false; + // ImFont* robotoFont = io.Fonts->AddFontFromMemoryTTF((void*)g_RobotoRegular, sizeof(g_RobotoRegular), 20.0f, &fontConfig); + // io.FontDefault = robotoFont; + + ConsoleLogWarn("Vulkan2Showcase: Finished Initializing VulkanImGuiLayer!!!!"); + } + + void VulkanImGuiLayer::BeginFrame() { + + } + + void VulkanImGuiLayer::EndFrame() { + + } +}; \ No newline at end of file diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp index 8524b61..12a2857 100644 --- a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp +++ b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp @@ -48,6 +48,7 @@ namespace engine3d::vk{ 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)); @@ -90,7 +91,7 @@ namespace engine3d::vk{ attribute_description[0] = { .location = 0, // // layout(location = 0) .binding = 0, - .format = VK_FORMAT_R32G32_SFLOAT, + .format = VK_FORMAT_R32G32B32_SFLOAT, // .offset = 0 .offset = offsetof(Vertex, Position) }; diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanRenderer.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanRenderer.cpp new file mode 100644 index 0000000..42a62fc --- /dev/null +++ b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanRenderer.cpp @@ -0,0 +1,179 @@ +#include "EngineLogger.hpp" +#include "internal/Vulkan2Showcase/VulkanContext.hpp" +#include +#include +#include +#include +#include + +namespace engine3d::vk{ + struct SimplePushConstantData{ + glm::mat2 Transform{1.f}; + glm::vec2 Offsets; + alignas(16) glm::vec3 Color; + }; + + VulkanRenderer::VulkanRenderer(const std::string& p_DebugName){ + ConsoleLogError("VulkanRenderer Initializing...."); + m_Swapchain = ApplicationInstance::GetWindow().GetCurrentSwapchain(); + //! @note Setting up our pipeline. + auto pipeline_config = vk::VulkanShader::shader_configuration(ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()); + // //! @note We are setting our shader pipeline to utilize our current window's swapchain + // //! @note a TODO is to utilize different render passes utiization for shader pipelines, potentially. + pipeline_config.PipelineRenderPass = m_Swapchain->GetRenderPass(); + pipeline_config.PipelineLayout = m_PipelineLayout; + + // ConsoleLogWarn("Just before loading shaders!!"); + m_Shader = Shader::Create("sim_shader_transforms/simple_shader.vert.spv", "sim_shader_transforms/simple_shader.frag.spv", pipeline_config); + + // ConsoleLogWarn("Initializing vulkan renderer2!"); + // //! @note Initializing Command buffers. + m_CommandBuffers.resize(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetImagesSize()); + + // m_CommandBuffers.resize(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetImagesSize()); + + //! @note Creating our pools of command buffer structs + VkCommandPoolCreateInfo pool_create_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .pNext = nullptr, + .flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, + .queueFamilyIndex = vk::VulkanContext::GetPhysicalDriver().GetQueueIndices().Graphics + }; + + vk::vk_check(vkCreateCommandPool(vk::VulkanContext::GetDriver(), &pool_create_info, nullptr, &m_CommandPool), "vkCreateCommandPool", __FILE__, __LINE__, __FUNCTION__); + + //! @note Allocating our command buffers. + VkCommandBufferAllocateInfo cmd_buffer_alloc_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = m_CommandPool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = static_cast(m_CommandBuffers.size()), + }; + + vk::vk_check(vkAllocateCommandBuffers(vk::VulkanContext::GetDriver(), &cmd_buffer_alloc_info, m_CommandBuffers.data()), "vkAllocateCommandBuffers", __FILE__, __LINE__, __FUNCTION__); + + ConsoleLogInfo("CommandBuffers Size === {}", m_CommandBuffers.size()); + + // ConsoleLogInfo("CommandBuffers Size === {}", m_CommandBuffers.size()); + + // VkPushConstantRange push_const_range = { + // .stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, + // .offset = 0, + // .size = sizeof(SimplePushConstantData) + // }; + + // //! @note First initializing pipeline layout create info + // VkPipelineLayoutCreateInfo pipeline_layout_create_info = { + // .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + // .pNext = nullptr, + // .flags = 0, + // .setLayoutCount = 0, + // .pSetLayouts = nullptr, + // // .pushConstantRangeCount = 0, + // // .pPushConstantRanges = nullptr, + // .pushConstantRangeCount = 1, + // .pPushConstantRanges = &push_const_range + // }; + + // vk::vk_check(vkCreatePipelineLayout(vk::VulkanContext::GetDriver(), &pipeline_layout_create_info, nullptr, &m_PipelineLayout), "vkCreatePipelineLayout", __FILE__, __LINE__, __FUNCTION__); + + } + + void VulkanRenderer::StartFrame(){ + ConsoleLogInfo("VulkanRenderer::BeginFrame"); + m_CurrentFrameIndex = m_Swapchain->AcquireNextImage(); + ConsoleLogError("NOT AN ERROR JUST DEBUGGING: Current Frame Index === {}", m_CurrentFrameIndex); + auto current_cmd_buffer = CurrentCommandBuffer(); + + } + + //! @note When we reach the end of the frame that is when we start rendering. + //! @note Beginning of frame we record, and write. + //! @note End of frame we render and submit rendering commands. + void VulkanRenderer::FinishFrame(){ + + ConsoleLogInfo("VulkanRenderer::EndFrame()"); + auto current_cmd_buffer = CurrentCommandBuffer(); + + VkCommandBufferBeginInfo cmd_buf_begin_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO + }; + + // START OF COMMAND BUFFER RECORD + vk::vk_check(vkBeginCommandBuffer(current_cmd_buffer, &cmd_buf_begin_info), "vkBeginCommandBuffer", __FILE__, __LINE__, __FUNCTION__); + + // starting render pass + VkRenderPassBeginInfo rp_begin_info = { + .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, + .renderPass = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetRenderPass(), + .framebuffer = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetFramebuffer(m_CurrentFrameIndex), // Specifying which framebuffer to render pass to. + .renderArea = { + .offset = {0, 0}, + .extent = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()} + } + }; + + std::array clearValues; + clearValues[0].color = {0.1f, 0.1f, 0.1f, 1.0f}; + clearValues[1].depthStencil = {1.0f, 0}; + + rp_begin_info.clearValueCount = static_cast(clearValues.size()); + rp_begin_info.pClearValues = clearValues.data(); + + vkCmdBeginRenderPass(current_cmd_buffer, &rp_begin_info, VK_SUBPASS_CONTENTS_INLINE); + + //-------------------------------------- + //! @note Rendering stuff.... + //-------------------------------------- + // int i = 0; + // for(auto& obj : m_GameObjects){ + // i += 1; + // obj.m_Transform2D.rotation = glm::mod(obj.m_Transform2D.rotation + 0.001f * i, 2.f * glm::pi()); + // } + + + //! @note Essentially doing m_Pipeline->Bind(m_CommandBuffer[i]) + //! @note Starts when to start rendering!! + // vkCmdBindPipeline(current_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_Shader->GetGraphicsPipeline()); + + // for(auto& obj : m_GameObjects){ + // obj.m_Transform2D.rotation = glm::mod(obj.GetTransform().rotation + 0.001f, glm::two_pi()); + + // SimplePushConstantData push = { + // .Transform = obj.GetTransform().mat2(), + // // .Transform = glm::mod(obj.GetTransform().rotation * 0.1f, glm::two_pi()), + // .Offsets = obj.GetTransform().Translation, + // .Color = obj.GetColor(), + // }; + // vkCmdPushConstants( + // current_cmd_buffer, + // m_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); + // } + + + //-------------------------------------- + //! @note Rendering stuff.... + //-------------------------------------- + + + vkCmdEndRenderPass(current_cmd_buffer); + + // END OF COMMAND BUFFER RECORDING + vk::vk_check(vkEndCommandBuffer(current_cmd_buffer), "vkEndCommandBuffer", __FILE__, __LINE__, __FUNCTION__); + + ApplicationInstance::GetWindow().GetCurrentSwapchain()->SubmitCommandBuffer(¤t_cmd_buffer); + } + + + VkCommandBuffer VulkanRenderer::CurrentCommandBuffer(){ + return m_CommandBuffers[m_CurrentFrameIndex]; + } +}; \ No newline at end of file diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanSwapchain.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanSwapchain.cpp index a30a6ad..7e0b819 100644 --- a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanSwapchain.cpp +++ b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanSwapchain.cpp @@ -25,6 +25,7 @@ namespace engine3d::vk{ if(format_count == 1 and formats[0].format == VK_FORMAT_UNDEFINED){ m_SurfaceFormat.format = VK_FORMAT_B8G8R8A8_UNORM; + // m_SurfaceFormat.format = VK_FORMAT_R32G32B32A32_SFLOAT; } else{ m_SurfaceFormat = formats[0];