Imgui: [Linux only] ImGUI + SDL2 + OpenGL 3.0 + OpenCV work, but not using imgui_impl_sdl_gl3.cpp

Created on 23 Apr 2017  路  5Comments  路  Source: ocornut/imgui

LINKS and REFERENCES :

Hello,
First over all, I'd like to say a big thank you to ocornut, for his fantastic Imgui ! I'm currently using it on Linux (Debian like) and it works very well. Congratulations, this is an awesome piece of software !

BTW : I just hope to meet you a day in France for true (I'm french too) and pay you a beer or whatever you want to drink ;-)

Back to the issue : since a long time I was trying to make it work using all the above libraries (mainly SDL2). After a lot of tries, I finaly got it working. It was not simple, because the SDL2 has a problem with the renderer (at least on Linux), but now it's ok (see the link above, and the discussion with Tom Seddon).

My work-in-progress aims to mix several libraries, for instance ImGUI + SDL2 + OpenGL 3.0 (maxi) + OpenCV + (external webcam)

The issue is : imgui_impl_sdl_gl3.cpp does not work as expected. More precisely, everything works, but nothing on the ImGUI side is displayed ... while it perfectly works using imgui_impl_sdl.cpp + OpenGL 3.0 and the SDL2 renderer (+ SDL_RenderGeometry).

What makes me think there is an issue with this file, is that I can make it work using textures and FBO + SDL2 (events, window) + OpenGL (3.3. is returned in this case) + OpenCV + the same ImGUI.

Of course, I'll continue to investigate, and if I find something, I'll add the information here.

More info : the result is the same, linking with libGLEW (dynamic lib, version 1.13 + glewExperimental = GL_TRUE ) or using the gl3w shipped with ImGUI : no ImGUI window appears.

Libraries are:

  • SDL2 from Tom Seddon repository + my own SDL_RenderGeometry patch applied
  • OpenCV 3.1.0 (home build)
  • OpenGL 3.0 (the one shipped with LinuxMint 18.2 )
  • ImGUI 1.50_wip

Runtime figures are :

  • average load is 52% of 1 core (other are 3 to 10% only) + 90 MB of RAM (because of the OpenCV side, but that's completely normal)
  • current image size is 1280 x 720 pixels, as a background (for devel purpose, of course)
  • around 30 fps (the flow depends on the webcam), but I'm sure without the cam, it could be very faster in a separated thread

Below : a screenshot, as proof of concept ;-)

capture du 2017-04-23 21-07-08_002

backenbinding opengl

Most helpful comment

Uff ... got finaly ImGui 1.54 + SDL2.0.5 + OpenGL 3.3 + OpenCV 3.4.0 working on Linux !

(1.54 means I'll be able to update to 1.60 soon)

It was my fault. I cannot recall where I read SDL_Init() had to be called after SDL_GLSetAttribute(), but it was plain wrong, and the trick was simply to call SDL_Init() BEFORE SDL_GL_SetAttribute() ...

For the record, the code + the link as reminder follows:

    // about issues between OpenGL 3.3 e.g. on Windows and OpenGL 3.0 (only) on Linux :
    // https://discourse.libsdl.org/t/confused-about-what-opengl-context-is-being-used-with-sdl/22860
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
   ....
   (other code in e.g. Application::Init() )

Last but not least : at the end of the render loop, just do

  ImGui::Render();
  // IMPORTANT : DO NOT USE SDL_RenderPresent(), use SDL_GL_SwapWindow() instead !
  //  SDL_RenderPresent(renderer);    
  SDL_GL_SwapWindow(window);

And everything will be displayed :-)

This issue can really be closed now. Thanks a lot for your kind help.

Edit : removed useless code + added important comment

All 5 comments

Moving discussion to #1116

PS: 30 fps is an extremely low framerate so I would assume the CPU/GPU time is going into whatever the rest of your application is doing (OpenCV, etc.) and has nothing to do with dear imgui.

@ocornut

The software I'm working on, aims to be a tool for some sport trainers, and I don't need more than 30 fps, but indeed, some value closer to 60 fps could be more comfortable, at least on the user experience side. The 30 fps is caused by the webcam, because the loop is currently following the rythm the frames are collected (Logitech C920). This is the current state of development, and I'll surely find a better solution (maybe collect frames in a separate thread, or something close).

To tell you more, I'm experimenting on Linux (develop on Linux is extremely easy), with the bet I will be able to either cross-compile or port it under Windows with the minimal amount of work (hmm) using SDL2 + ImGUI and OpenGL + OpenCV some day. An orthogonal development could use Qt + OpenCV + OpenGL (second track I'll explore soon too, but I'd like to avoid the heavy Qt thing.

As you certainly know, find a portable GUI on Linux is a serious issue, and after a long time searching, say a compromise (since August 2016 ...), ImGUI + imgui_tabs + some other light libs appear as extremely interesting. After some search on the web, I naturaly tried OpenCV + SDL2 (for events + directx. And the last main problem is I'm mainly using Linux, and the future users Windows (what else ;-) )

I hope this will enlight your point of view about the issue : mostly the SDL_Renderer, reading the last information.

I forgot : without webcam frame, I hit a nice 60 fps without problem with all of the examples you provide, and I confirm the issue is on my side. Apologies, I didn't answer well to your question.

Uff ... got finaly ImGui 1.54 + SDL2.0.5 + OpenGL 3.3 + OpenCV 3.4.0 working on Linux !

(1.54 means I'll be able to update to 1.60 soon)

It was my fault. I cannot recall where I read SDL_Init() had to be called after SDL_GLSetAttribute(), but it was plain wrong, and the trick was simply to call SDL_Init() BEFORE SDL_GL_SetAttribute() ...

For the record, the code + the link as reminder follows:

    // about issues between OpenGL 3.3 e.g. on Windows and OpenGL 3.0 (only) on Linux :
    // https://discourse.libsdl.org/t/confused-about-what-opengl-context-is-being-used-with-sdl/22860
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
   ....
   (other code in e.g. Application::Init() )

Last but not least : at the end of the render loop, just do

  ImGui::Render();
  // IMPORTANT : DO NOT USE SDL_RenderPresent(), use SDL_GL_SwapWindow() instead !
  //  SDL_RenderPresent(renderer);    
  SDL_GL_SwapWindow(window);

And everything will be displayed :-)

This issue can really be closed now. Thanks a lot for your kind help.

Edit : removed useless code + added important comment

Thanks for posting this! I am clueless with SDL but several people seem to be having problem integrating OpenGL in there so any posted info is useful.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mnemode2 picture mnemode2  路  3Comments

KaungZawHtet picture KaungZawHtet  路  3Comments

DarkLinux picture DarkLinux  路  3Comments

bogdaNNNN1 picture bogdaNNNN1  路  3Comments

inflex picture inflex  路  3Comments