首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenGL纹理导致ImGUI窗口永久失焦

OpenGL纹理导致ImGUI窗口永久失焦
EN

Stack Overflow用户
提问于 2019-08-09 00:40:52
回答 1查看 948关注 0票数 2

我在OpenGL上关注TheCherno的教程(我已经做了一些修改)。我用的是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());之前,我必须解除着色器、顶点缓冲区/索引缓冲区和顶点数组的绑定。这实际上像预期的那样工作了一次,但在关闭并重新启动程序时,窗口拒绝聚焦,文本显示为矩形。比如this

我的代码:

代码语言:javascript
复制
#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.

编辑:我刚刚发现完全移除所有的纹理可以解决这个问题。在绘制之前取消绑定纹理不执行任何操作。

编辑2:显然有多个纹理是问题所在?!!只有一个纹理是可以的,但是有多个纹理会导致这个问题。同样,解除绑定不会做任何事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-09 01:22:49

明白了!出于某些原因,使用多个纹理槽会让ImGui感到不安。我只用了一个插槽,一切都像预期的一样!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57417202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档