Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imgui doesnot render in opengl 2.1 even when unbinding all buffers and shaders #5167

Closed
Anuj-Bhattarai opened this issue Apr 4, 2022 · 1 comment

Comments

@Anuj-Bhattarai
Copy link

I am trying to render a simple triangle and a simple imgui Window,but when i try to render it just shows a black window without any triangle or imgui window(sometimes also crashes).I am Unbinding my VertexBuffer but it still doesnot let the traingle and imgui window render(my driver has s deafult white color shader so i am not writing it) and whenever i comment glBindBuffer(GL_ARRAY_BUFFER,VBO); it works.So,its a problem with the VertexBuffer
But how to fix it??.I think i am doing all of the procedures right(I am a beginner so i dont know how to fix it even after trying for many hours).

My Code:

#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "imgui_impl_opengl2.h"
#include "imgui_impl_glfw.h"
#include "imgui.h"
#include <vector>

std::vector<float> Vertices = {
    -0.5,0.5,
    0.5,0.0,
    0.5,-0.5
};

int main()
{
    glfwInit();
    GLFWwindow* window = glfwCreateWindow(900,500,"WWW",NULL,NULL);
    glfwMakeContextCurrent(window);
    glewInit();
    
    unsigned int VBO;
    glGenBuffers(1,&VBO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    glBufferData(GL_ARRAY_BUFFER,6 * sizeof(float),&Vertices[0],GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER,0);
    
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO(); (void)io;
    ImGui::StyleColorsDark();
    
    bool show_another_window = true;
    
    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL2_Init();
    
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        // Start the Dear ImGui frame
        ImGui_ImplOpenGL2_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();
        
        glBindBuffer(GL_ARRAY_BUFFER,VBO);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2* sizeof(float),0);

        glDrawArrays(GL_TRIANGLES,0,3);
        glBindBuffer(GL_ARRAY_BUFFER,0);

        if (show_another_window)
        {
            ImGui::Begin("Another Window", &show_another_window);   // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
            ImGui::Text("Hello from another window!");
            if (ImGui::Button("Close Me"))
                show_another_window = false;
            ImGui::End();
        }
    
        // Rendering
        ImGui::Render();
        int display_w, display_h;
        glfwGetFramebufferSize(window, &display_w, &display_h);
        glViewport(0, 0, display_w, display_h);
        glClearColor(0.2f,0.2f,0.2f,1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());

        glfwMakeContextCurrent(window);
        glfwSwapBuffers(window);
    }
    // Cleanup
    ImGui_ImplOpenGL2_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();

    glfwDestroyWindow(window);glfwTerminate();
    return 0;
}

my Compiler Settings:

g++ Test.cpp imgui.cpp imgui_impl_glfw.cpp imgui_impl_opengl2.cpp imgui_draw.cpp imgui_widgets.cpp imgui_tables.cpp        -o Test -lGL -lGLEW -lglfw -lGLU
@ocornut
Copy link
Owner

ocornut commented Apr 4, 2022

You have access to examples/example_glfw_opengl2 you can compare your code to it and see the difference and track your issue from there. If examples/example_glfw_opengl2/ unmodified works and your code doesn't, then your added code is the culprit, however confusing OpenGL states are this becomes an OpenGL problem not a Dear ImGui problem.

I suggest you use the imgui_impl_opengl3 backend which is more in line with your coding style. Its not guaranteed that OpenGL driver handle both old and new style perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants