From f526e9b15210e93d9da3a5a1418deef9847abbae Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Mon, 18 Sep 2023 17:29:55 +0200 Subject: [PATCH] create debug window with imgui --- .gitignore | 3 ++- CMakeLists.txt | 4 ++-- include/debugwindow.hpp | 19 +++++++++++++++++ src/CMakeLists.txt | 3 ++- src/debugwindow.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 10 +++++++++ src/renderer.cpp | 10 ++++++++- 7 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 include/debugwindow.hpp create mode 100644 src/debugwindow.cpp diff --git a/.gitignore b/.gitignore index b3dedbe..864e63f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ gmon.out* cscope* test.cpp a.out -*screenshot* \ No newline at end of file +*screenshot* +imgui.ini diff --git a/CMakeLists.txt b/CMakeLists.txt index b4183c6..de73763 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.2) project(cmake-project-template) -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3") +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -O3") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g") set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}) diff --git a/include/debugwindow.hpp b/include/debugwindow.hpp new file mode 100644 index 0000000..5707917 --- /dev/null +++ b/include/debugwindow.hpp @@ -0,0 +1,19 @@ +#ifndef DEBUG_WINDOW_H +#define DEBUG_WINDOW_H + +#include +#include +#include + +namespace debug{ + namespace window { + void init(GLFWwindow* window); + void prerender(); + void render(); + void destroy(); + + void set_parameter(std::string key, std::any value); + } +} + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cfbbf1e..6775018 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.2) project(OpenGLTest) -set(SOURCE_FILES main.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenerator.cpp renderer.cpp spacefilling.cpp stb_image.cpp utils.cpp OpenSimplexNoise.cpp) +set(SOURCE_FILES main.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenerator.cpp + debugwindow.cpp renderer.cpp spacefilling.cpp stb_image.cpp utils.cpp OpenSimplexNoise.cpp) add_executable(OpenGLTest ${SOURCE_FILES}) diff --git a/src/debugwindow.cpp b/src/debugwindow.cpp new file mode 100644 index 0000000..eed0a6f --- /dev/null +++ b/src/debugwindow.cpp @@ -0,0 +1,47 @@ +#include "debugwindow.hpp" + +#include +#include +#include + +namespace debug{ + namespace window{ + + + void init(GLFWwindow* window){ + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + + // Setup Platform/Renderer backends + ImGui_ImplGlfw_InitForOpenGL(window, true); // Second param install_callback=true will install GLFW callbacks and chain to existing ones. + ImGui_ImplOpenGL3_Init(); + } + + void prerender(){ + // Start the Dear ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + ImGui::ShowDemoWindow(); // Show demo window! :) + } + + void render(){ + // (Your code clears your framebuffer, renders your other stuff etc.) + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + // (Your code calls glfwSwapBuffers() etc.) + } + + void destroy(){ + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + } + } +} diff --git a/src/main.cpp b/src/main.cpp index 7123cf8..f50dc62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "chunkmanager.hpp" #include "main.hpp" +#include "debugwindow.hpp" #include "renderer.hpp" #include "spacefilling.hpp" #include "shader.hpp" @@ -22,6 +23,7 @@ int frames = 0; float lastBlockPick=0.0; bool blockpick = false; bool canChangeWireframe = true; +bool cursor = false; int main() { @@ -67,8 +69,10 @@ int main() sines[i] = sin(3.14 / 180 * i); cosines[i] = cos(3.14 / 180 * i); } + SpaceFilling::initLUT(); chunkmanager::init(); + debug::window::init(window); renderer::init(window); while (!glfwWindowShouldClose(window)) @@ -112,6 +116,7 @@ int main() // Cleanup allocated memory chunkmanager::destroy(); renderer::destroy(); + debug::window::destroy(); glfwTerminate(); return 0; @@ -157,5 +162,10 @@ void processInput(GLFWwindow *window) if(glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS) renderer::saveScreenshot(); if(glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS) renderer::saveScreenshot(true); + if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) { + cursor = !cursor; + glfwSetInputMode(window, GLFW_CURSOR, cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED); + } + } diff --git a/src/renderer.cpp b/src/renderer.cpp index ddd8a28..4027e5c 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -5,6 +5,7 @@ #include "chunkmanager.hpp" #include "chunkmesher.hpp" +#include "debugwindow.hpp" #include "globals.hpp" #include "stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION @@ -118,6 +119,10 @@ namespace renderer{ glClearColor(0.431f, 0.694f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + /* UPDATE IMGUI */ + debug::window::prerender(); + + /* RENDER THE WORLD TO TEXTURE */ int total{0}, toGpu{0}; glm::vec4 frustumPlanes[6]; theCamera.getFrustumPlanes(frustumPlanes, true); @@ -205,7 +210,7 @@ namespace renderer{ } render_todelete.clear(); - + /* DISPLAY TEXTURE ON A QUAD THAT FILLS THE SCREEN */ // Now to render the quad, with the texture on top // Switch to the default frame buffer glBindFramebuffer(GL_FRAMEBUFFER, 0); @@ -222,6 +227,8 @@ namespace renderer{ quadShader->setInt("crosshairType", 1); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); + + debug::window::render(); } void framebuffer_size_callback(GLFWwindow *window, int width, int height){ @@ -271,6 +278,7 @@ namespace renderer{ void destroy(){ delete theShader; + delete quadShader; }