Search code examples
c++macosglfwopengl-2.0imgui

OpenGL Texture cause ImGUI windows to be permanently out of focus


I'm following TheCherno's tutorials on OpenGL (I've gone ahead and modified a few things though). I'm on MacOS mojave and have OpenGL 2.1. However, when I got to the ImGui part of the tutorial, things started behaving weirdly.

Since I have an older version of OpenGL, I'm using the glfw_opengl2_impl of ImGui, and the example code at https://github.com/ocornut/imgui/blob/master/examples/example_glfw_opengl2/main.cpp works, but I have to include a few other cpp files in addition to the .h files (seen at the top of my code).

When I tried this with my own code, I've found that I had to unbind shaders, vertex buffers/index buffers, and vertex arrays before calling ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); This actually worked as intended once, but upon closing and restarting the program, the windows have refused to focus and the text is displaying as rectangles. Like this.

My code:

#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.

Edit: I just discovered that completely removing all the textures fixes the problem. Unbinding the textures before the draw does nothing.

Edit 2: apparently having multiple textures is the problem?!! Having one texture is fine, but having multiple causes this issue. Again, unbinding does nothing.


Solution

  • Got it! For some reason using multiple texture slots is upsetting ImGui. I just only used one slot and everything works as intended!