gpt4 book ai didi

c++ - OpenGL 纹理导致 ImGUI 窗口永久失焦

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:40 25 4
gpt4 key购买 nike

我正在学习 TheCherno 的 OpenGL 教程(尽管我已经继续并修改了一些内容)。我在 MacOS mojave 上使用 OpenGL 2.1。然而,当我到达教程的 ImGui 部分时,事情开始变得奇怪。

因为我有一个旧版本的 OpenGL,我正在使用 ImGui 的 glfw_opengl2_impl,示例代码在 https://github.com/ocornut/imgui/blob/master/examples/example_glfw_opengl2/main.cpp有效,但除了 .h 文件(见于我的代码顶部)之外,我还必须包含一些其他 cpp 文件。

当我用我自己的代码尝试这个时,我发现我必须在调用 ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); 之前取消绑定(bind)着色器、顶点缓冲区/索引缓冲区和顶点数组; 这实际上曾按预期工作过一次,但在关闭并重新启动程序后,窗口拒绝聚焦并且文本显示为矩形。喜欢 this .

我的代码:

#include "imgui.h"
#include "imgui_impl_opengl2.h"
#include "imgui_impl_glfw.h"

#include "imgui.cpp"
#include "imgui_impl_glfw.cpp"
#include "imgui_impl_opengl2.cpp"
#include "imgui_draw.cpp"
#include "imgui_widgets.cpp"
#include "imgui_demo.cpp"
// other includes

// Init GLEW, GLFW, etc

// Removing this chunk of code fixes the problem
localBuf = stbi_load(path.c_str(), &width, &height, &bits, 4);
glGenTextures(1, &id);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id);
setRenderHints({{GL_TEXTURE_MIN_FILTER, GL_NEAREST}, {GL_TEXTURE_MAG_FILTER, GL_NEAREST}});
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, localBuf);
if (localBuf) {
stbi_image_free(localBuf);
}
glGenerateMipmap(GL_TEXTURE_2D);



IMGUI_CHECKVERSION();
ImGui::CreateContext();

ImGuiIO& io = ImGui::GetIO(); (void)io;

ImGui::StyleColorsClassic();

ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL2_Init();

while (!glfwWindowShouldClose(win)) {
// some logic for the camera ...

ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();

{
ImGui::Begin("Hello, world!");
ImGui::ColorEdit3("Tint: ", (float *) &tint);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
ImGui::Text("Count: %iu", counter);
ImGui::End();
}


ImGui::Render();

glClearColor(0.25f, 0.25f, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDrawElements(GL_QUADS, 24, GL_UNSIGNED_INT, nullptr);

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glUseProgram(0);

ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());

glUseProgram(shader);
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);

/* Swap front and back buffers */
glfwSwapBuffers(window);

/* Poll for and process events */
glfwPollEvents();
}

ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();

// terminate glfw.

编辑:我刚刚发现完全移除所有纹理可以解决问题。在绘制之前取消绑定(bind)纹理什么都不做。

编辑 2:显然有多个纹理是问题所在?!!有一个纹理很好,但有多个纹理会导致这个问题。同样,取消绑定(bind)没有任何作用。

最佳答案

明白了!出于某种原因,使用多个纹理槽让 ImGui 感到不安。我只使用了一个插槽,一切都按预期工作!

关于c++ - OpenGL 纹理导致 ImGUI 窗口永久失焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57417202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com