fix: fixed ImGui integration

This commit is contained in:
Lukian 2025-07-15 14:57:21 +02:00
parent 3eb73af274
commit d1b444bcef

View file

@ -19,6 +19,7 @@
void framebuffer_size_callback(GLFWwindow* window, int width, int height); void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window); 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_pos_callback(GLFWwindow* window, double xpos, double ypos);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods); void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
@ -55,6 +56,7 @@ int main()
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetKeyCallback(window, keyboard_callback);
glfwSetCursorPosCallback(window, mouse_pos_callback); glfwSetCursorPosCallback(window, mouse_pos_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetScrollCallback(window, mouse_scroll_callback); glfwSetScrollCallback(window, mouse_scroll_callback);
@ -79,7 +81,7 @@ int main()
ImGuiIO& io = ImGui::GetIO(); (void)io; ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange; io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, false); ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330 core"); ImGui_ImplOpenGL3_Init("#version 330 core");
Shader ourShader("resources/shaders/vert.glsl", "resources/shaders/frag.glsl"); Shader ourShader("resources/shaders/vert.glsl", "resources/shaders/frag.glsl");
@ -102,9 +104,10 @@ int main()
lastFrame = currentFrame; lastFrame = currentFrame;
glfwPollEvents(); glfwPollEvents();
processInput(window);
if (mouseCaptured || !io.WantCaptureMouse) if (mouseCaptured)
processInput(window); io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 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) if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS && mouseCaptured)
camera.ProcessKeyboard(RIGHT, deltaTime); 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); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
mouseCaptured = true; 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) void mouse_pos_callback(GLFWwindow* window, double xposIn, double yposIn)
{ {
float xpos = static_cast<float>(xposIn); float xpos = static_cast<float>(xposIn);
@ -220,19 +225,9 @@ void mouse_pos_callback(GLFWwindow* window, double xposIn, double yposIn)
lastX = xpos; lastX = xpos;
lastY = ypos; lastY = ypos;
if (mouseCaptured){ if (mouseCaptured)
camera.ProcessMouseMovement(xoffset, yoffset); 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) void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {}
{ void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {}
ImGui_ImplGlfw_MouseButtonCallback(window, button, action, mods);
}
void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
ImGui_ImplGlfw_ScrollCallback(window, xoffset, yoffset);
}