Imgui: Example: Using imgui in a opengl scene

Created on 25 Oct 2017  路  4Comments  路  Source: ocornut/imgui

Hi,

I've made a small OpenGL graphics engine (using SDL2). I want to use imgui "on top" to control stuff in the graphics engine. How can I do this?

Here's my attempt (scene is rendered, but no imgui windows are visible):

keepRunning = true;
while (keepRunning)
{
    while (SDL_PollEvent(&event) != 0)
    {
        handleEvent(event); // Handle event in game engine, may set keepRunning=false
        ImGui_ImplSdlGL2_ProcessEvent(&event); // Handle event in imgui
    }

    // Render game engine, binds buffers, draws triangles, draws all kinds of stuff (including a GUI)
    app.render();

    // Render imgui
    ImGui_ImplSdlGL2_NewFrame(sdlWindow);
    // 1. Show a simple window
    // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
    {
        static float f = 0.0f;
        ImGui::Text("Hello, world!");
        ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
        ImGui::ColorEdit3("clear color", (float*) &clear_color);
        if (ImGui::Button("Test Window")) show_test_window ^= 1;
        if (ImGui::Button("Another Window")) show_another_window ^= 1;
        ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
    }

    // 2. Show another simple window, this time using an explicit Begin/End pair
    if (show_another_window)
    {
        ImGui::Begin("Another Window", &show_another_window);
        ImGui::Text("Hello from another window!");
        ImGui::End();
    }

    // 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
    if (show_test_window)
    {
        ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver);
        ImGui::ShowTestWindow(&show_test_window);
    }
    ImGui::Render();

    SDL_GL_SwapWindow(sdlWindow);
}
backenbinding

Most helpful comment

So I've upgraded my application (including the ImGui implementation) to GL3. I was disabling all the vertex attributes, and "un-using" the shader programs before. But now I removed all of those "cleanups" (leaving attributes enabled, shader programs "in use" even though the rendering is finished, etc) and ImGui is working flawlessly.

I'm so grateful for ImGui at this point, was really not looking forward to GUI coding before I discovered ImGui but now it's just so easy (and automagically good-looking)!

All 4 comments

Thanks for getting back to me! After half a day of attempting to figure out what I was doing wrong, it is now working as it should.
The problem was that vertex attribute arrays (that were enabled during rendering of my engine) were not disabled when invoking ImGui::Render(). Then, ImGui didn't render. I don't have to disable these vertex attribute arrays between using different shader programs, so I honestly didn't think it was a big deal to just keep them enabled (saving GL calls). Lesson learned.

imgui is awesome, thank you so much for all the effort!

The OpenGL 2 binding has to way to disable/bind other arrays because those concepts don't exist in GL2, this is why you should use the GL3 code and I think it should have worked with it.
I would be interested if you can confirm that (if it works with the GL3 code _without_ the change in your engine code).

So I've upgraded my application (including the ImGui implementation) to GL3. I was disabling all the vertex attributes, and "un-using" the shader programs before. But now I removed all of those "cleanups" (leaving attributes enabled, shader programs "in use" even though the rendering is finished, etc) and ImGui is working flawlessly.

I'm so grateful for ImGui at this point, was really not looking forward to GUI coding before I discovered ImGui but now it's just so easy (and automagically good-looking)!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mnemode2 picture mnemode2  路  3Comments

mkanakis picture mkanakis  路  3Comments

ILoveImgui picture ILoveImgui  路  3Comments

NPatch picture NPatch  路  3Comments

BlackWatersInc picture BlackWatersInc  路  3Comments