Search code examples
c++openglrendergame-engineimgui

unable to add imgui to existing scene OpenGL


I am trying to add imgui for debugging the scene but imgui always render at back irrespective of order in which i call render. scene and imgui render fine if done individually(commenting other one). i tried disabling depth testing, using stencil function ,everytime imgui is rendered first . render loop is as

void render() {
            bool show_demo_window = true;
            bool show_another_window = false;
            ImVec4 clear_color = ImVec4(0.1f, 0.1f, 0.1f, 1.0f);
            float m_time = glfwGetTime();
            while (!glfwWindowShouldClose(window)) 
                glfwGetFramebufferSize(window, &framebufferwidth, &framebufferheight);
                glViewport(0, 0, framebufferwidth, framebufferheight);

                glBindFramebuffer(GL_FRAMEBUFFER, 0);
                glBindVertexArray(0);
                glBindTexture(GL_TEXTURE_2D, 0);

                finalShader->Use();
                for (int i = 0; i < finTex.size(); i++) {
                    finTex[i]->bind();
                    std::string top = "texture" + std::to_string(i);
                    finalShader->setUniform1i(top.c_str(), finTex[i]->getTextureUnit());
                }
                window2D->updateProjMatrix(window);
                winCam->sendToShader(finalShader);

                window2D->updateModelMatrix();

                window2D->Draw();
                finalShader->unUse();


                // Rendering ImGui
                ImGuiIO& io = ImGui::GetIO();
               int framebufferwidth;
               int framebufferheight;
               glfwGetFramebufferSize(window, &framebufferwidth, &framebufferheight);
               io.DisplaySize = ImVec2(framebufferwidth, framebufferheight);
               float time = glfwGetTime();
               io.DeltaTime = 1 / 60.f;

               // Start the Dear ImGui frame
               ImGui_ImplOpenGL3_NewFrame();
               ImGui_ImplGlfw_NewFrame();
               ImGui::NewFrame();
                if (show_demo_window)
                    ImGui::ShowDemoWindow(&show_demo_window);
                ImGui::EndFrame();


                 ImGui::Render();


                preRender();
                postRender();
                glfwSwapBuffers(window);
                glfwPollEvents();
                glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
                glClear(GL_COLOR_BUFFER_BIT);
                ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

            }
            ImGui_ImplOpenGL3_Shutdown();
            ImGui_ImplGlfw_Shutdown();
            ImGui::DestroyContext();
        }

also for initializing ImGui since i initialized window core with opengl 4.4

ImGui_ImplOpenGL3_Init("#version 440");

also this code is in different namespace and makes a dll file which is then linked .

Edit: i got the error it was not actually i was using ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); after glfwSwapBuffers(window);


Solution

  • It would seem that

    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
    

    is what is actually rendering the Imgui interface, but it happens right after the

    glClear(GL_COLOR_BUFFER_BIT);
    

    while it happens at the endof the loop in practice what will happen is

    1. Clear

    2. Draw ImGUi

    3. DrawRest

    4. Clear

    etc..

    so imGui will always be at the back. To fix it try to put

    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
    

    before

    glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT)
    

    and see if that fixes it.