我将 GLFW 和 ImGui 用于涉及打开多个窗口的项目。到目前为止,我已经进行了设置,因此每次必须打开一个新窗口时,我都会生成一个线程来创建它自己的 GLFW 窗口和 OpenGL 上下文。线程函数看起来像这样:
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
// Check for creation error...
glfwMakeContextCurrent(window);
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); // Is this supposed to be done per-thread?
// Calling specific impl-specific ImGui setup methods for GLFW & OpenGL3...
// Set up OpenGL stuff ...
while (!glfwWindowShouldClose(window))
{
// Some heavy-duty processing happens here...
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// ImGui code is here...
// Rendering some stuff in the window here...
// Render ImGui last...
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// Calling impl-specific ImGui shutdown here...
glfwDestroyWindow(window);
我知道 GLFW 要求您从主线程(调用的那个glfwInit()
)轮询事件,所以我的主线程上有一个循环来执行此操作:
while (!appMustExit)
{
glfwWaitEvents();
}
// appMustExit is set from another thread that waits for console input
所以我遇到的问题是我的 ImGui 控件不响应任何类型的输入,并且glfwWindowShouldClose()
如果我单击关闭按钮,则永远不会返回 true。似乎输入状态仅在调用的线程上可用glfwPollEvents()
,这让我相信你不能在仍然使用单独的线程进行渲染的同时结合 ImGui 和 GLFW!
如何解决此问题以允许 ImGui 和这些窗口响应 GLFW 事件?
我之前的尝试使用单个线程来迭代每个窗口并更新/渲染它,但我希望使用线程来帮助应用程序在打开许多窗口时更好地扩展。
更新:我想澄清一下,这个应用程序涉及实时处理复杂的机器视觉,并且 ImGui 代码部分与控制和响应这个机器视觉代码高度集成。因此,我希望能够在与此处理相同的线程上调用 ImGui 函数,这也意味着该线程必须能够响应 glfw 输入。