From 1b54280c5211422bc2fa6e51b294564412ebb949 Mon Sep 17 00:00:00 2001 From: Jack Date: Mon, 26 Aug 2024 16:53:49 -0400 Subject: [PATCH] Create Interface struct and functions to control ImGui windows Should now be able to get and manipulate data pertaining to the ImGui windows so I can properly update the camera's aspect. --- imgui.ini | 30 +++++++++---- include/interface.h | 26 +++++++++++ include/scene.h | 6 ++- source/callbacks.cpp | 3 ++ source/interface.cpp | 71 +++++++++++++++++++++++++++++ source/scene.cpp | 103 ++++++++++--------------------------------- 6 files changed, 151 insertions(+), 88 deletions(-) create mode 100644 include/interface.h create mode 100644 source/interface.cpp diff --git a/imgui.ini b/imgui.ini index 5d31e30..75331ee 100644 --- a/imgui.ini +++ b/imgui.ini @@ -4,8 +4,8 @@ Size=400,400 Collapsed=0 [Window][Terrain Controls] -Pos=936,19 -Size=344,654 +Pos=861,19 +Size=419,701 Collapsed=0 DockId=0x00000002,0 @@ -19,17 +19,31 @@ Collapsed=0 [Window][TerraVisor] Pos=0,0 -Size=1280,673 +Size=2199,944 Collapsed=0 [Window][Scene Window] Pos=0,19 -Size=934,654 +Size=859,701 Collapsed=0 -DockId=0x00000001,0 +DockId=0x00000003,0 + +[Window][Viewport] +Pos=0,19 +Size=1603,925 +Collapsed=0 +DockId=0x00000003,0 + +[Window][Scene Settings] +Pos=1605,19 +Size=594,925 +Collapsed=0 +DockId=0x00000004,0 [Docking][Data] -DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1280,654 Split=X Selected=0x9F2D9299 - DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=934,701 CentralNode=1 Selected=0x9F2D9299 - DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=344,701 Selected=0xF69494A7 +DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=2199,925 Split=X Selected=0x9F2D9299 + DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=0,0 Split=X Selected=0x13926F0B + DockNode ID=0x00000003 Parent=0x00000001 SizeRef=1603,701 CentralNode=1 Selected=0x13926F0B + DockNode ID=0x00000004 Parent=0x00000001 SizeRef=594,701 Selected=0x413E6147 + DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=419,701 Selected=0xF69494A7 diff --git a/include/interface.h b/include/interface.h new file mode 100644 index 0000000..aac385a --- /dev/null +++ b/include/interface.h @@ -0,0 +1,26 @@ +#ifndef INTERFACE_H_ +#define INTERFACE_H_ + +#include +#include +#include +#include + +struct Window { + std::string name; + ImVec2 size; + ImVec2 position; + bool isOpen = true; + + std::function renderContent; + + Window(const std::string& name, std::function renderFunc) + : name(name), size(ImVec2(512, 512)), position(ImVec2(0, 0)), renderContent(renderFunc) {} +}; + +namespace Interface { + void AddWindow(std::vector& windows, const std::string& name, std::function renderFunc); + void RenderWindows(std::vector& windows); +} + +#endif // INTERFACE_H_ \ No newline at end of file diff --git a/include/scene.h b/include/scene.h index 8f5bffd..1973f16 100644 --- a/include/scene.h +++ b/include/scene.h @@ -8,6 +8,7 @@ #include "fbo.h" #include "camera.h" +#include "interface.h" struct Vertex { glm::vec3 position; @@ -25,6 +26,8 @@ class Scene { void DrawGui(GLFWwindow* window); void Idle(); void ReloadShader(); + void UpdateCamera(); + void UpdateViewport(); std::vector LoadHGT(const std::string& filename, int width, int height); int window_width; @@ -34,6 +37,7 @@ class Scene { void InitBuffers(); void InitQuadBuffers(); void InitShaders(); + void InitUI(); Camera activeCamera_; Framebuffer geo_fbo_; @@ -48,11 +52,11 @@ class Scene { float near_z_; float far_z_; float fov_; + std::vector windows_; glm::mat4 view_matrix_; glm::mat4 projection_matrix_; - void UpdateCamera(); int16_t SwapEndian(int16_t val); GLuint CreateHeightmapTexture(std::vector data, int width, int height); }; diff --git a/source/callbacks.cpp b/source/callbacks.cpp index 936259b..4f88f8c 100644 --- a/source/callbacks.cpp +++ b/source/callbacks.cpp @@ -57,6 +57,9 @@ void Callbacks::Resize(GLFWwindow* window, int width, int height) //Set viewport to cover entire framebuffer glViewport(0, 0, width, height); //Set aspect ratio used in view matrix calculation + // scene_->window_width = width; + // scene_->window_height = height; + // scene_->Scene::UpdateCamera(); //Scene::Camera::Aspect = float(width) / float(height); //Scene::Camera::UpdateP(); } \ No newline at end of file diff --git a/source/interface.cpp b/source/interface.cpp new file mode 100644 index 0000000..a629ed6 --- /dev/null +++ b/source/interface.cpp @@ -0,0 +1,71 @@ +#include "interface.h" + +#include +#include + +namespace Interface { + +void AddWindow(std::vector& windows, const std::string& name, std::function renderFunc) { + windows.emplace_back(name, renderFunc); +} + +void RenderWindows(std::vector& windows) { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + + ImGui::NewFrame(); + + static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode; + ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; + + // Style windows + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(viewport->WorkPos); + ImGui::SetNextWindowSize(viewport->WorkSize); + ImGui::SetNextWindowViewport(viewport->ID); + ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); + window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; + window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; + window_flags |= ImGuiWindowFlags_NoBackground; + + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); + ImGui::Begin("TerraVisor", nullptr, window_flags); + ImGui::PopStyleVar(3); + + // Submit the DockSpace + if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) { + ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); + ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); + } + + if (ImGui::BeginMenuBar()) { + if (ImGui::BeginMenu("Options")) { + ImGui::Text("Nothing Here Yet, Check Back Later!"); + + ImGui::EndMenu(); + } + + ImGui::EndMenuBar(); + } + + ImGui::End(); + + for (auto& window : windows) { + if (window.isOpen && ImGui::Begin(window.name.c_str(), &window.isOpen)) { + window.size = ImGui::GetWindowSize(); + window.position = ImGui::GetWindowPos(); + if (window.renderContent) { + window.renderContent(); + } + } + ImGui::End(); + } + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); +} +} \ No newline at end of file diff --git a/source/scene.cpp b/source/scene.cpp index e6a8208..6c5cae5 100644 --- a/source/scene.cpp +++ b/source/scene.cpp @@ -9,10 +9,6 @@ #include #include -#include -#include -#include - #include #include #include @@ -29,6 +25,7 @@ #include "InitShader.h" #include "DebugCallback.h" #include "load_texture.h" +#include "interface.h" namespace { @@ -91,6 +88,7 @@ void Scene::Init() { InitBuffers(); ReloadShader(); InitQuadBuffers(); + InitUI(); CameraControls::SetPosition(activeCamera_, glm::vec3(1.4f, 1.4f, 0.0f)); CameraControls::SetRotation(activeCamera_, -glm::normalize(CameraControls::GetPosition(activeCamera_))); @@ -197,6 +195,22 @@ void Scene::InitQuadBuffers() { glBindVertexArray(0); } +void Scene::InitUI() { + Interface::AddWindow(windows_, "Viewport", [this]() { + ImGui::Image((void*)(intptr_t)lht_fbo_.albedoTexture, ImVec2(window_width, window_height), ImVec2(0, 1), ImVec2(1, 0)); + }); + + Interface::AddWindow(windows_, "Scene Settings", [this]() { + // if (ImGui::Button("Quit")) { + // glfwSetWindowShouldClose(window, GLFW_TRUE); + // } + + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + + ImGui::SliderFloat("Tessellation Level", &Uniforms::GeoData.maxTessellation, 1.0f, 64.0f); + ImGui::SliderInt("Patch Grid Density", &Uniforms::GeoData.gridDensity, 1, 24); + }); +} // Allows for runtime shader updates void Scene::ReloadShader() { @@ -274,84 +288,15 @@ void Scene::Display(GLFWwindow* window) { FBO::Unbind(); - DrawGui(window); + //DrawGui(window); + Interface::RenderWindows(windows_); glfwSwapBuffers(window); } -void Scene::DrawGui(GLFWwindow* window) { - // Begin ImGui frame - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - - // Enable docking - ImGuiIO& io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; - - ImGui::NewFrame(); - - static ImGuiDockNodeFlags dockspace_flags = ImGuiDockNodeFlags_PassthruCentralNode; - - // Add menu bar - ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking; - - // Style windows - const ImGuiViewport* viewport = ImGui::GetMainViewport(); - ImGui::SetNextWindowPos(viewport->WorkPos); - ImGui::SetNextWindowSize(viewport->WorkSize); - ImGui::SetNextWindowViewport(viewport->ID); - ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); - ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f); - window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; - window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus; - window_flags |= ImGuiWindowFlags_NoBackground; - - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); - ImGui::Begin("TerraVisor", nullptr, window_flags); - ImGui::PopStyleVar(3); - - // Submit the DockSpace - if (io.ConfigFlags & ImGuiConfigFlags_DockingEnable) { - ImGuiID dockspace_id = ImGui::GetID("MyDockSpace"); - ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags); - } - - if (ImGui::BeginMenuBar()) { - if (ImGui::BeginMenu("Options")) { - ImGui::Text("Nothing Here Yet, Check Back Later!"); - - ImGui::EndMenu(); - } - - ImGui::EndMenuBar(); - } - - ImGui::End(); - - // Draw Gui - ImGui::Begin("Terrain Controls"); - if (ImGui::Button("Quit")) { - glfwSetWindowShouldClose(window, GLFW_TRUE); - } - - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); - - ImGui::SliderFloat("Tessellation Level", &Uniforms::GeoData.maxTessellation, 1.0f, 64.0f); - ImGui::SliderInt("Patch Grid Density", &Uniforms::GeoData.gridDensity, 1, 24); - ImGui::End(); - - // Draw FBO to ImGui window - ImGui::Begin("Scene Window"); - ImVec2 windowSize = ImGui::GetContentRegionAvail(); - if (ImGui::GetWindowSize().x != window_width || ImGui::GetWindowSize().y != window_height) { - window_width = ImGui::GetWindowSize().x; - window_height = ImGui::GetWindowSize().y; - UpdateCamera(); - } - ImGui::Image((void*)(intptr_t)lht_fbo_.albedoTexture, ImVec2(window_width, window_height), ImVec2(0, 1), ImVec2(1, 0)); - ImGui::End(); - - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); +void Scene::UpdateViewport() { + window_width = ImGui::GetWindowSize().x; + window_height = ImGui::GetWindowSize().y; + UpdateCamera(); } void Scene::Idle() {