From d1b444bcef0a39db58e4dc35510fa831fa5020e9 Mon Sep 17 00:00:00 2001 From: Lukian Date: Tue, 15 Jul 2025 14:57:21 +0200 Subject: [PATCH] fix: fixed ImGui integration --- main.cpp | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/main.cpp b/main.cpp index 7f0bbdc..3b17bd9 100644 --- a/main.cpp +++ b/main.cpp @@ -19,6 +19,7 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); +void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods); void mouse_pos_callback(GLFWwindow* window, double xpos, double ypos); void mouse_button_callback(GLFWwindow* window, int button, int action, int mods); void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset); @@ -55,6 +56,7 @@ int main() } glfwMakeContextCurrent(window); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + glfwSetKeyCallback(window, keyboard_callback); glfwSetCursorPosCallback(window, mouse_pos_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetScrollCallback(window, mouse_scroll_callback); @@ -79,7 +81,7 @@ int main() ImGuiIO& io = ImGui::GetIO(); (void)io; io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange; ImGui::StyleColorsDark(); - ImGui_ImplGlfw_InitForOpenGL(window, false); + ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init("#version 330 core"); Shader ourShader("resources/shaders/vert.glsl", "resources/shaders/frag.glsl"); @@ -102,9 +104,10 @@ int main() lastFrame = currentFrame; glfwPollEvents(); + processInput(window); - if (mouseCaptured || !io.WantCaptureMouse) - processInput(window); + if (mouseCaptured) + io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -195,13 +198,15 @@ void processInput(GLFWwindow *window) if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS && mouseCaptured) camera.ProcessKeyboard(RIGHT, deltaTime); - if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS && !mouseCaptured) + if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS && !mouseCaptured && !ImGui::GetIO().WantCaptureMouse) { glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); mouseCaptured = true; } } +void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {} + void mouse_pos_callback(GLFWwindow* window, double xposIn, double yposIn) { float xpos = static_cast(xposIn); @@ -220,19 +225,9 @@ void mouse_pos_callback(GLFWwindow* window, double xposIn, double yposIn) lastX = xpos; lastY = ypos; - if (mouseCaptured){ + if (mouseCaptured) camera.ProcessMouseMovement(xoffset, yoffset); - ImGui_ImplGlfw_CursorPosCallback(window, -FLT_MAX, -FLT_MAX); - } else - ImGui_ImplGlfw_CursorPosCallback(window, xpos, ypos); } -void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) -{ - ImGui_ImplGlfw_MouseButtonCallback(window, button, action, mods); -} - -void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) -{ - ImGui_ImplGlfw_ScrollCallback(window, xoffset, yoffset); -} +void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {} +void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {}