diff --git a/CMakeLists.txt b/CMakeLists.txt index aa0ad581dd257609e2d9a920190dcadd571245f7..1063d17c4d4034c7fcb753bea759bb1dee64bb52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,7 @@ set(COMMON_SOURCES src/itterative_PCISPH.cu src/spawn_particles.cpp src/Camera.cpp + src/ImGuiManager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/common/GL_utilities.c # Add other common source files if needed ) diff --git a/src/GUI_fluid_sim.cpp b/src/GUI_fluid_sim.cpp index 5ffb22f2f225857965bbe3cf8a5589f6b6c6a5d5..a4a4d5a873716493bd5c85c3ce44918e3b82fd8e 100644 --- a/src/GUI_fluid_sim.cpp +++ b/src/GUI_fluid_sim.cpp @@ -16,6 +16,8 @@ #include "imgui_impl_glfw.h" // Use the GLFW backend #include "imgui_impl_opengl3.h" // Use the OpenGL3 backend +#include "include/ImGuiManager.h" +#include "include/FluidConfig.h" #include "include/PCISPH_sim.h" // Header for CUDA function declarations #include "include/spawn_particles.h" #include "include/Camera.h" @@ -24,6 +26,8 @@ #define MAIN #include "VectorUtils4.h" +FluidConfig fluidConfig; + // Number of particles const int NUM_PARTICLES = 50000; const float GRID_SPACING = 0.25f; @@ -382,18 +386,8 @@ int main(int argc, char** argv) { // Initialize FBO with initial window size (1280x720) initFBO(initialWidth, initialHeight, fbo, fboTexture, depthRBO); - // Setup ImGui context - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); (void)io; - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable docking - - // Setup ImGui style - ImGui::StyleColorsDark(); - - // Setup ImGui Platform/Renderer backends - ImGui_ImplGlfw_InitForOpenGL(window, true); - ImGui_ImplOpenGL3_Init("#version 330"); + // Instantiate ImGuiManager + ImGuiManager imguiManager(window, fluidConfig); // Set GLFW callbacks glfwSetWindowUserPointer(window, cameraPtr); @@ -406,9 +400,10 @@ int main(int argc, char** argv) { glfwPollEvents(); // Start ImGui frame - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); + imguiManager.newFrame(); + + // Obtain ImGuiIO reference + ImGuiIO& io = imguiManager.getIO(); // Create a full-screen dockspace ImGui::SetNextWindowPos(ImVec2(0, 0)); @@ -423,10 +418,13 @@ int main(int argc, char** argv) { ImGui::End(); // Controls window - ImGui::Begin("Controls"); - ImGui::Text("Control Panel"); - // Add your control widgets here (sliders, buttons, etc.) - ImGui::End(); + imguiManager.createControlsWindow(); + + // Frame Stats window + imguiManager.createFrameStatsWindow(); + + // FBO Selection window + imguiManager.createFBOSelectionWindow(); // Simulation window ImGui::Begin("Simulation"); @@ -455,17 +453,13 @@ int main(int argc, char** argv) { ImGui::End(); // Render ImGui - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + imguiManager.render(); // Swap buffers glfwSwapBuffers(window); } - // Cleanup ImGui and OpenGL resources - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplGlfw_Shutdown(); - ImGui::DestroyContext(); + // Cleanup ImGui and OpenGL resources is handled by ImGuiManager destructor // Cleanup your own resources cleanup(); @@ -477,4 +471,4 @@ int main(int argc, char** argv) { glfwDestroyWindow(window); glfwTerminate(); return 0; -} +} \ No newline at end of file diff --git a/src/ImGuiManager.cpp b/src/ImGuiManager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c44413be47e9487bfd332745b578f1f542ea8d88 --- /dev/null +++ b/src/ImGuiManager.cpp @@ -0,0 +1,156 @@ +// ImGuiManager.cpp +#include "include/ImGuiManager.h" +#include "imgui_impl_glfw.h" +#include "imgui_impl_opengl3.h" + +// Constructor: Initializes ImGui context and sets up backends +ImGuiManager::ImGuiManager(GLFWwindow* window, FluidConfig& config) + : fluidConfig(config) // Initialize the reference +{ + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + + // Enable Docking + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + + // Optional: Enable Multi-Viewport / Platform Windows + // io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; + + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + // ImGui::StyleColorsClassic(); + + // Setup Platform/Renderer backends + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 330"); +} + +// Destructor: Cleans up ImGui resources +ImGuiManager::~ImGuiManager() { + // Cleanup ImGui backends + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + + // Destroy ImGui context + ImGui::DestroyContext(); +} + +// Starts a new ImGui frame +void ImGuiManager::newFrame() { + // Start the Dear ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); +} + +// Renders ImGui draw data +void ImGuiManager::render() { + // Rendering + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + // Update and Render additional Platform Windows (if enabled) + /* + ImGuiIO& io = ImGui::GetIO(); + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable){ + GLFWwindow* backup_current_context = glfwGetCurrentContext(); + ImGui::UpdatePlatformWindows(); + ImGui::RenderPlatformWindowsDefault(); + glfwMakeContextCurrent(backup_current_context); + } + */ +} + +// Provides access to ImGuiIO +ImGuiIO& ImGuiManager::getIO() { + return ImGui::GetIO(); +} + +void ImGuiManager::createControlsWindow() { + ImGui::Begin("Controls"); + + ImGui::Text("Fluid Simulation Parameters"); + ImGui::Separator(); // Draw a horizontal line to separate sections + ImGui::Spacing(); + + // Block: Fluid Properties + ImGui::Text("Fluid Properties:"); + ImGui::Spacing(); + ImGui::Text("Viscosity:"); + ImGui::SliderFloat("##ViscositySlider", &fluidConfig.viscosity, 0.0f, 5.0f, "%.2f"); + ImGui::Spacing(); + ImGui::Text("Particle Mass:"); + ImGui::SliderFloat("##ParticleMassSlider", &fluidConfig.particleMass, 0.0f, 2.0f, "%.2f"); + + ImGui::Spacing(); + ImGui::Separator(); // Separate sections visually + ImGui::Spacing(); + + // Block: Simulation Settings + ImGui::Text("Simulation Settings:"); + ImGui::Spacing(); + ImGui::Text("Rest Density:"); + ImGui::SliderFloat("##RestDensitySlider", &fluidConfig.restDensity, 1000.0f, 5000.0f, "%.0f"); + ImGui::Spacing(); + ImGui::Text("Smoothing Radius:"); + ImGui::SliderFloat("##SmoothingRadiusSlider", &fluidConfig.smoothingRadius, 0.1f, 1.0f, "%.2f"); + + ImGui::Spacing(); + ImGui::Separator(); // Separate sections visually + ImGui::Spacing(); + + // Additional blocks can be added as needed + // For example: + // ImGui::Text("Additional Settings:"); + // ImGui::Spacing(); + // ImGui::SliderFloat("SomeOtherParameter", &someConfigValue, min, max, "%.2f"); + + ImGui::End(); +} + +// Creates the "Frame Stats" window with FPS graphs +void ImGuiManager::createFrameStatsWindow() { + ImGui::Begin("Frame Stats"); + + // Example implementation using plot lines + static float fpsHistory[120] = {0}; + static float frameTimeHistory[120] = {0}; + static int historyIndex = 0; + + // Update history arrays (this should be called each frame) + ImGuiIO& io = ImGui::GetIO(); + float currentFps = io.Framerate; + float currentFrameTime = 1000.0f / currentFps; // in ms + + fpsHistory[historyIndex % 120] = currentFps; + frameTimeHistory[historyIndex % 120] = currentFrameTime; + historyIndex++; + + // Plot FPS + ImGui::Text("FPS"); + ImGui::PlotLines("##FPS", fpsHistory, 120, historyIndex % 120, NULL, 0.0f, 120.0f, ImVec2(0, 80)); + + // Plot Frame Time + ImGui::Text("Frame Time (ms)"); + ImGui::PlotLines("##FrameTime", frameTimeHistory, 120, historyIndex % 120, NULL, 0.0f, 50.0f, ImVec2(0, 80)); + + ImGui::End(); +} + +// Creates the "FBO Selection" window with a dropdown +void ImGuiManager::createFBOSelectionWindow() { + ImGui::Begin("FBO Selection"); + + static int selectedFBO = 0; + const char* fboOptions[] = { "Stage 1", "Stage 2", "Stage 3", "Stage 4" }; + int numFBOs = sizeof(fboOptions) / sizeof(fboOptions[0]); + + ImGui::Combo("Select FBO to Render", &selectedFBO, fboOptions, numFBOs); + + // You can store 'selectedFBO' to use it later for rendering + // For now, as per your request, no functionality is implemented + + ImGui::End(); +} diff --git a/src/include/FluidConfig.h b/src/include/FluidConfig.h new file mode 100644 index 0000000000000000000000000000000000000000..4a2ee1d1fba61496c828d01448912ca3a8189de8 --- /dev/null +++ b/src/include/FluidConfig.h @@ -0,0 +1,15 @@ +// FluidConfig.h +#ifndef FLUID_CONFIG_H +#define FLUID_CONFIG_H + +struct FluidConfig { + float viscosity = 0.2f; + float particleMass = 0.4f; + float restDensity = 3000.0f; + float smoothingRadius = 0.35f; // h_H + + // Constructor can initialize from constants if needed + FluidConfig() = default; +}; + +#endif // FLUID_CONFIG_H diff --git a/src/include/ImGuiManager.h b/src/include/ImGuiManager.h new file mode 100644 index 0000000000000000000000000000000000000000..13416be19132ce2d79f110c48ca6b9a810813304 --- /dev/null +++ b/src/include/ImGuiManager.h @@ -0,0 +1,39 @@ +// ImGuiManager.h +#ifndef IMGUI_MANAGER_H +#define IMGUI_MANAGER_H + +#include <GLFW/glfw3.h> +#include "imgui.h" +#include "include/FluidConfig.h" + +class ImGuiManager { +public: + // Constructor initializes ImGui + ImGuiManager(GLFWwindow* window, FluidConfig& config); + + // Destructor cleans up ImGui + ~ImGuiManager(); + + // Starts a new ImGui frame + void newFrame(); + + // Renders ImGui draw data + void render(); + + // Provides access to ImGuiIO + ImGuiIO& getIO(); + + // Function to create the Controls window + void createControlsWindow(); + + // Function to create the FPS graph + void createFrameStatsWindow(); + + // Function to create the FBO selection window + void createFBOSelectionWindow(); + +private: + FluidConfig& fluidConfig; // Reference to fluid configuration +}; + +#endif // IMGUI_MANAGER_H