An introduction to the Dear ImGui library
As developers, many of us have faced the pain of introducing graphical interfaces to our programs. Traditional GUI libraries add a degree of complexity which you may not want if you are making tools that are intended for a variety of tasks such as debugging. Here we present a library that makes it possible to create loggers, profilers, debuggers or even an entire game making editor quickly and easily. The entire example presented here is available on Github.
Update 2021-02-23 : Screencast Available
Based on the feedback from this blog post, we’ve recorded a screencast where we perform all the steps and show the results live. We plan to do more of these screencasts in the future, so pleased stay tuned to this blog for future updates.
Also, for more video content about using Conan, we’ve published a large collection of video-based content organized into self-paced training courses in the JFrog Academy.
Dear ImGui is an amazing C++ GUI library mainly used in game developement. The project is open-source software, licensed under MIT license. Dear ImGui focuses on simplicity and productivity using what is called Immediate Mode GUI paradigm.
Immediate mode GUI’s are different from the traditional retained-mode interfaces in that widgets are created and drawn on each frame vs the traditional approach of first creating a widget and adding callbacks to it. Some of the benefits of this paradigm are your UI “lives closer” to your data and that it allows for fast prototyping.
Dear ImGui is mainly designed for developers to use in content creation and debug tools. It’s renderer agnostic in the way that you have to provide the tools to render the data but It’s very easy to integrate into your own code as it has multiple bindings for different window and events handling libraries (like GLFW, SDL2 and GLUT) and multiple renderers (like OpenGL, DirectX and Vulkan).
Dear ImGui comes with lots of widgets like windows, labels, input boxes, progress bars, buttons, sliders, trees, etc. You can see some examples in the image beneath.
Integrating Dear ImGui in your application
The typical use of ImGui is when you already have a 3D-pipeline enabled application like a content creation or game development tool where you want to add a GUI. Let’s see how easy it is to integrate ImGui in our application. Our example application renders a triangle using OpenGL3. We will use GLFW to manage window creation and events handling. As ImGui is independent of the rendering system and platform we have to introduce some binding for our rendering system. Fortunately, there are many premade bindings in Dear ImGui’s repo. As we will use Dear ImGui v1.74 these are the ones we will need:
The minimal code to make this work is in
main.cpp. First, you initialize the
window for rendering and then you have to initialize a Dear ImGui context and
the helper platform and Renderer bindings. You can change the rendering style if
you want as well.
Then you enter the main application loop where you can clearly see the difference with the classical retained mode GUI’s.
And, we must do some cleanup when the loop ends.
So, this is what we get:
Let’s say, for example, that we want to change the triangle’s position/orientation and colour. That would be as simple as calling some sliders and a colour picker and passing the data to the triangle via shader uniforms:
There are some basic drawing tools as well.
If you want to explore the different library widgets and options the best way to
do it is to make a call to
ImGui::ShowDemoWindow() and have a look at the
Setting up a project with Conan
Setting up a project that uses ImGui is a matter of minutes with Conan. A Conan package for ImGui has been created and added to Conan-Center already. The example shown here is using Windows and Visual Studio 2017 but it is very similar in MacOS or Linux.
If you want to give a try tou can download all the files from the Conan examples repo:
First, let’s inspect the CMake project. It has the bindings for GLFW and OpenGL3 and two more files to handle OpenGL shaders and file reading. It will also copy the shaders that render the triangle to the working directory each time the application is recompiled.
We will also need the conanfile to declare the libraries it depends on.
Besides from the GLFW library we already talked about we need the GLEW library
to handle OpenGL functions loading. We will use
cmake_multi to generate
projects for Debug and Release configurations. An imports section was also added
to download the required bindings for GLFW and OpenGL3.
Now let’s build the project and run the application.
Dear ImGui is a powerful library with an easy to use API which integrates into 3D-pipeline enabled applications almost seamlessly. It has lots of widgets and can be a great tool to make debugging software such as profilers, loggers or object editors of any kind. Also, new functionalities like docking or multiple viewports are currently being developed. Now it’s time to experiment with the library and making it interact with your own code!