From 198f7e1269bdd7a7f8c84c351248adf3a72c1554 Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Fri, 18 Feb 2022 16:28:20 +0100 Subject: [PATCH] added imgui --- .gitmodules | 3 +++ CMakeLists.txt | 13 +++++++++++++ src/Application.cpp | 31 ++++++++++++++++++++++++++++--- src/Application.hpp | 2 ++ src/CMakeLists.txt | 5 +++++ src/Canvas.cpp | 2 +- src/Window.cpp | 7 +++++++ src/Window.hpp | 2 ++ vendor/imgui | 1 + 9 files changed, 62 insertions(+), 4 deletions(-) create mode 160000 vendor/imgui diff --git a/.gitmodules b/.gitmodules index 2cf019a..d6e3e1f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "vendor/glfw"] path = vendor/glfw url = git@github.com:glfw/glfw.git +[submodule "vendor/imgui"] + path = vendor/imgui + url = git@github.com:ocornut/imgui.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b78510..0e1e093 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,19 @@ cmake_minimum_required (VERSION 3.8) project ("Mandelbrot") +set(IMGUI_SOURCE_DIR "${CMAKE_SOURCE_DIR}/vendor/imgui") + +set(IMGUI_INCLUDE_DIRS ${IMGUI_SOURCE_DIR}) +set(IMGUI_SOURCE_FILES + "${IMGUI_SOURCE_DIR}/imgui.cpp" + "${IMGUI_SOURCE_DIR}/imgui_demo.cpp" + "${IMGUI_SOURCE_DIR}/imgui_draw.cpp" + "${IMGUI_SOURCE_DIR}/imgui_tables.cpp" + "${IMGUI_SOURCE_DIR}/imgui_widgets.cpp" + "${IMGUI_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp" + "${IMGUI_SOURCE_DIR}/backends/imgui_impl_glfw.cpp" +) + # Include sub-projects. add_subdirectory ("vendor/glfw") add_subdirectory ("vendor/glad") diff --git a/src/Application.cpp b/src/Application.cpp index 9cf2779..5e2c03f 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -3,8 +3,11 @@ #include +#include "backends/imgui_impl_glfw.h" +#include "backends/imgui_impl_opengl3.h" + Application::Application() : - window(new Window(1280, 720, "Mandelbrot")), canvas(nullptr) + window(new Window(1280, 720, "Mandelbrot")), canvas(nullptr), backgroundColor{ 0.1f, 0.01f, 0.19f } { window->MakeContextCurrent(); @@ -16,10 +19,22 @@ Application::Application() : glViewport(0, 0, 1280, 720); canvas = new Canvas(); + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + + window->InitImGui(); + ImGui_ImplOpenGL3_Init("#version 460 core"); + + ImGui::StyleColorsDark(); } Application::~Application() { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); } void Application::Launch() @@ -27,12 +42,22 @@ void Application::Launch() while (!window->ShouldClose()) { glfwPollEvents(); - - glClearColor(0.1f, 0.01f, 0.19f, 1.0f); + glClearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], 1.0f); glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + canvas->Render(); + ImGui::Begin("Settings"); + ImGui::SliderFloat3("Background Color", backgroundColor, 0.0f, 1.0f); + ImGui::End(); + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + window->Display(); } } diff --git a/src/Application.hpp b/src/Application.hpp index bd0def6..9434547 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -14,4 +14,6 @@ public: private: Window* window; Canvas* canvas; + + float backgroundColor[3]; }; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 91e8a16..224d12f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,9 +6,14 @@ cmake_minimum_required (VERSION 3.8) # Add source to this project's executable. add_executable (Mandelbrot "main.cpp" "Window.cpp" "Application.cpp" "Canvas.cpp") +target_sources(Mandelbrot PRIVATE + ${IMGUI_SOURCE_FILES} +) + target_include_directories(Mandelbrot PRIVATE glfw glad + ${IMGUI_INCLUDE_DIRS} ) target_link_libraries(Mandelbrot diff --git a/src/Canvas.cpp b/src/Canvas.cpp index 17de5d1..2928c31 100644 --- a/src/Canvas.cpp +++ b/src/Canvas.cpp @@ -90,7 +90,7 @@ void Canvas::CreateShaderProgram() void main() { - FragColor = vec4(1.0f, 0.0f, 1.0f, 1.0f); + FragColor = vec4(0.1f, 0.3f, 0.3f, 1.0f); } )"; shaderSourceCString = fragmentShaderSource.c_str(); diff --git a/src/Window.cpp b/src/Window.cpp index 2ddd9ab..91eee50 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -3,6 +3,8 @@ #include +#include + Window::Window(uint32_t width, uint32_t height, const std::string& title) : handle(nullptr) { @@ -34,3 +36,8 @@ void Window::Display() { glfwSwapBuffers(handle); } + +void Window::InitImGui() +{ + ImGui_ImplGlfw_InitForOpenGL(handle, true); +} diff --git a/src/Window.hpp b/src/Window.hpp index b411c68..540b6df 100644 --- a/src/Window.hpp +++ b/src/Window.hpp @@ -14,6 +14,8 @@ public: inline void MakeContextCurrent() { glfwMakeContextCurrent(handle); } void Display(); + void InitImGui(); + private: GLFWwindow* handle; }; \ No newline at end of file diff --git a/vendor/imgui b/vendor/imgui new file mode 160000 index 0000000..29d462e --- /dev/null +++ b/vendor/imgui @@ -0,0 +1 @@ +Subproject commit 29d462ebce0275345a6ce4621d8fff0ded57c9e5