From 9656816e5205a3d70227384d074b744e4a9d8dc5 Mon Sep 17 00:00:00 2001 From: Karim Abdel Hamid Date: Mon, 10 Jul 2023 01:10:11 +0300 Subject: [PATCH] Scaffolding for Imgui Resizing The actual resizing of ImGui Pipelines so it can actually work will come later. In the meantime, I've asked here: https://github.com/ocornut/imgui/issues/6515 --- sources/code/Common/Window/Win32Window.hpp | 2 + .../code/Editor/ImguiEditor/ImguiEditor.cpp | 37 ++++++++++++++----- .../code/Editor/ImguiEditor/ImguiEditor.hpp | 5 +++ .../code/Editor/ImguiEditor/ImguiRenderer.hpp | 1 + .../ImguiEditor/ImguiRendererOpenGL.cpp | 4 ++ .../ImguiEditor/ImguiRendererOpenGL.hpp | 1 + .../ImguiEditor/ImguiRendererVulkan.cpp | 8 ++++ .../ImguiEditor/ImguiRendererVulkan.hpp | 1 + .../EngineCore/CoreSystems/RenderSystem.cpp | 10 +++-- .../Plugins/GraphicsVulkan/VulkanCore.cpp | 4 ++ .../Plugins/GraphicsVulkan/VulkanCore.hpp | 1 + 11 files changed, 60 insertions(+), 14 deletions(-) diff --git a/sources/code/Common/Window/Win32Window.hpp b/sources/code/Common/Window/Win32Window.hpp index 2a2b9216..c4eac681 100644 --- a/sources/code/Common/Window/Win32Window.hpp +++ b/sources/code/Common/Window/Win32Window.hpp @@ -1,6 +1,8 @@ #pragma once +#ifndef NOMINMAX #define NOMINMAX +#endif #include "Window.hpp" #include diff --git a/sources/code/Editor/ImguiEditor/ImguiEditor.cpp b/sources/code/Editor/ImguiEditor/ImguiEditor.cpp index d167504c..4cd67068 100644 --- a/sources/code/Editor/ImguiEditor/ImguiEditor.cpp +++ b/sources/code/Editor/ImguiEditor/ImguiEditor.cpp @@ -1,22 +1,14 @@ #include #include #include -#if EDITOR_USE_OPENGL -#include -#include "GL/gl3w.h" -#else -#include -#include -#include -#include -#endif -#include #include #include #include #include "Common/Window/WindowManager.hpp" #include "EngineCore/EngineCore.hpp" +#include "EngineCore/Events/Dispatcher.hpp" +#include "Common/Event/WindowEvent.hpp" #include "Editor/EditorManager.hpp" #include "Modals/ModelConverterModal.hpp" #include "Modals/ImageConverterModal.hpp" @@ -68,6 +60,12 @@ ImguiEditor::ImguiEditor(EngineCore* engineCore) : engineCore(engineCore) { controlBar = new ControlBar(imguiRenderer); menubar = new Menubar(this); statusBar = new StatusBar(imguiRenderer); + + auto eventDispatcher = engineCore->GetEventDispatcher(); + eventDispatcher->AddEventListener( + Events::EventType::WindowResize, + std::bind(&ImguiEditor::OnWindowResize, this, std::placeholders::_1) + ); } ImguiEditor::~ImguiEditor() { @@ -91,6 +89,20 @@ ImguiEditor::~ImguiEditor() { delete statusBar; } +void ImguiEditor::PerformResize() { + queueResize = false; + imguiRenderer->Resize(); +} + +bool ImguiEditor::OnWindowResize(Events::BaseEvent* ev) { + if (ev->GetEventType() == Events::EventType::WindowResize) { + Events::WindowResizeEvent* winResizeEvent = (Events::WindowResizeEvent*)ev; + queueResize = true; + } + + return false; +} + void ImguiEditor::SetupFonts() { auto& io = ImGui::GetIO(); @@ -210,6 +222,11 @@ void ImguiEditor::SetupStyles() { } void ImguiEditor::Update() { + if (queueResize) { + PerformResize(); + return; + } + if (!imguiRenderer->PreRender()) { return; } diff --git a/sources/code/Editor/ImguiEditor/ImguiEditor.hpp b/sources/code/Editor/ImguiEditor/ImguiEditor.hpp index 5f065dd6..f529de67 100644 --- a/sources/code/Editor/ImguiEditor/ImguiEditor.hpp +++ b/sources/code/Editor/ImguiEditor/ImguiEditor.hpp @@ -4,6 +4,8 @@ #include #include +#include + namespace Grindstone { class EngineCore; @@ -48,7 +50,10 @@ namespace Grindstone { void SetupFonts(); void SetupStyles(); void SetupColors(); + bool OnWindowResize(Events::BaseEvent* ev); + void PerformResize(); private: + bool queueResize = false; EngineCore* engineCore = nullptr; ImguiInput* input = nullptr; ImageConverterModal* imageConverterModal = nullptr; diff --git a/sources/code/Editor/ImguiEditor/ImguiRenderer.hpp b/sources/code/Editor/ImguiEditor/ImguiRenderer.hpp index 7b12e9c1..ad6d0606 100644 --- a/sources/code/Editor/ImguiEditor/ImguiRenderer.hpp +++ b/sources/code/Editor/ImguiEditor/ImguiRenderer.hpp @@ -18,6 +18,7 @@ namespace Grindstone { virtual bool PreRender() = 0; virtual void PrepareImguiRendering() = 0; virtual void PostRender() = 0; + virtual void Resize() = 0; virtual ImTextureID CreateTexture(std::filesystem::path path) = 0; }; } diff --git a/sources/code/Editor/ImguiEditor/ImguiRendererOpenGL.cpp b/sources/code/Editor/ImguiEditor/ImguiRendererOpenGL.cpp index 07c8ad7c..b1458b25 100644 --- a/sources/code/Editor/ImguiEditor/ImguiRendererOpenGL.cpp +++ b/sources/code/Editor/ImguiEditor/ImguiRendererOpenGL.cpp @@ -58,6 +58,10 @@ void ImguiRendererOpenGL::PostRender() { ImGui::RenderPlatformWindowsDefault(); } +void ImguiRendererOpenGL::Resize() { + +} + ImTextureID ImguiRendererOpenGL::CreateTexture(std::filesystem::path path) { std::filesystem::path fullPath = "../engineassets/editor/" / path; auto assetManager = Editor::Manager::GetEngineCore().assetManager; diff --git a/sources/code/Editor/ImguiEditor/ImguiRendererOpenGL.hpp b/sources/code/Editor/ImguiEditor/ImguiRendererOpenGL.hpp index 139f21ee..4472c88f 100644 --- a/sources/code/Editor/ImguiEditor/ImguiRendererOpenGL.hpp +++ b/sources/code/Editor/ImguiEditor/ImguiRendererOpenGL.hpp @@ -14,6 +14,7 @@ namespace Grindstone { virtual bool PreRender() override; virtual void PrepareImguiRendering() override; virtual void PostRender() override; + virtual void Resize() override; virtual ImTextureID CreateTexture(std::filesystem::path path) override; }; } diff --git a/sources/code/Editor/ImguiEditor/ImguiRendererVulkan.cpp b/sources/code/Editor/ImguiEditor/ImguiRendererVulkan.cpp index cdfe72bb..cf11ed3a 100644 --- a/sources/code/Editor/ImguiEditor/ImguiRendererVulkan.cpp +++ b/sources/code/Editor/ImguiEditor/ImguiRendererVulkan.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -140,6 +141,13 @@ void ImguiRendererVulkan::PostRender() { window->PresentSwapchain(); } +void ImguiRendererVulkan::Resize() { + Grindstone::EngineCore& engineCore = Grindstone::Editor::Manager::GetEngineCore(); + auto vulkanCore = static_cast(engineCore.GetGraphicsCore()); + auto window = static_cast(engineCore.windowManager->GetWindowByIndex(0)); + auto wgb = window->GetWindowGraphicsBinding(); +} + ImTextureID ImguiRendererVulkan::CreateTexture(std::filesystem::path path) { std::filesystem::path fullPath = "../engineassets/editor/" / path; auto assetManager = Editor::Manager::GetEngineCore().assetManager; diff --git a/sources/code/Editor/ImguiEditor/ImguiRendererVulkan.hpp b/sources/code/Editor/ImguiEditor/ImguiRendererVulkan.hpp index 2e8afcb3..a73c63a9 100644 --- a/sources/code/Editor/ImguiEditor/ImguiRendererVulkan.hpp +++ b/sources/code/Editor/ImguiEditor/ImguiRendererVulkan.hpp @@ -15,6 +15,7 @@ namespace Grindstone { virtual bool PreRender() override; virtual void PrepareImguiRendering() override; virtual void PostRender() override; + virtual void Resize() override; virtual ImTextureID CreateTexture(std::filesystem::path path) override; private: VkDescriptorPool imguiPool = nullptr; diff --git a/sources/code/EngineCore/CoreSystems/RenderSystem.cpp b/sources/code/EngineCore/CoreSystems/RenderSystem.cpp index 48e0c626..fc7a7ad9 100644 --- a/sources/code/EngineCore/CoreSystems/RenderSystem.cpp +++ b/sources/code/EngineCore/CoreSystems/RenderSystem.cpp @@ -17,10 +17,12 @@ glm::vec3 EulerToForward(glm::vec3 eulerAngle) { float pitch = eulerAngle.x; float yaw = eulerAngle.y; - glm::vec3 forwardVector; - forwardVector.x = cos(yaw) * cos(pitch); - forwardVector.y = sin(pitch); - forwardVector.z = sin(yaw) * cos(pitch); + glm::vec3 forwardVector { + cos(yaw)* cos(pitch), + sin(pitch), + sin(yaw)* cos(pitch) + }; + return glm::normalize(forwardVector); } diff --git a/sources/code/Plugins/GraphicsVulkan/VulkanCore.cpp b/sources/code/Plugins/GraphicsVulkan/VulkanCore.cpp index 9d909834..c5fe55a8 100644 --- a/sources/code/Plugins/GraphicsVulkan/VulkanCore.cpp +++ b/sources/code/Plugins/GraphicsVulkan/VulkanCore.cpp @@ -515,6 +515,10 @@ VkCommandBuffer VulkanCore::BeginSingleTimeCommands() { return GraphicsAPI::BeginSingleTimeCommands(); } +uint32_t VulkanCore::GetGraphicsFamily() { + return graphicsFamily; +} + void VulkanCore::EndSingleTimeCommands(VkCommandBuffer commandBuffer) { GraphicsAPI::EndSingleTimeCommands(commandBuffer); } diff --git a/sources/code/Plugins/GraphicsVulkan/VulkanCore.hpp b/sources/code/Plugins/GraphicsVulkan/VulkanCore.hpp index 2865e725..b474c5c6 100644 --- a/sources/code/Plugins/GraphicsVulkan/VulkanCore.hpp +++ b/sources/code/Plugins/GraphicsVulkan/VulkanCore.hpp @@ -42,6 +42,7 @@ namespace Grindstone { virtual VkDevice GetDevice(); virtual VkPhysicalDevice GetPhysicalDevice(); virtual VkCommandBuffer BeginSingleTimeCommands(); + virtual uint32_t GetGraphicsFamily(); virtual void EndSingleTimeCommands(VkCommandBuffer commandBuffer); VkCommandPool GetGraphicsCommandPool(); std::function logFunction;