DEV Community

Cover image for An Introduction to the Dear ImGui Library

An Introduction to the Dear ImGui Library

Carlos Zoido
Originally published at ・6 min read

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.

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 below:

Different Dear ImGui widgets

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.69 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.

// Setup Dear ImGui context
ImGuiIO &io = ImGui::GetIO();
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
// Setup Dear ImGui style
Enter fullscreen mode Exit fullscreen mode

Then you enter the main application loop where you can clearly see the difference with the classical retained mode GUI's.

while (!glfwWindowShouldClose(window))
    glClearColor(0.45f, 0.55f, 0.60f, 1.00f);

    // feed inputs to dear imgui, start new frame

    // rendering our geometries
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);

    // render your GUI
    ImGui::Begin("Demo window");

    // Render dear imgui into screen

    int display_w, display_h;
    glfwGetFramebufferSize(window, &display_w, &display_h);
    glViewport(0, 0, display_w, display_h);
Enter fullscreen mode Exit fullscreen mode

And, we must do some cleanup when the loop ends.

Enter fullscreen mode Exit fullscreen mode

So, this is what we get:

ImGui Hello World

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:

// render your GUI
ImGui::Begin("Triangle Position/Color");
static float rotation = 0.0;
ImGui::SliderFloat("rotation", &rotation, 0, 2 * PI);
static float translation[] = {0.0, 0.0};
ImGui::SliderFloat2("position", translation, -1.0, 1.0);
static float color[4] = { 1.0f,1.0f,1.0f,1.0f };
// pass the parameters to the shader
triangle_shader.setUniform("rotation", rotation);
triangle_shader.setUniform("translation", translation[0], translation[1]);
// color picker
ImGui::ColorEdit3("color", color);
// multiply triangle's color with this color
triangle_shader.setUniform("color", color[0], color[1], color[2]);
Enter fullscreen mode Exit fullscreen mode

Change triangle's color

There are some basic drawing tools as well.

Render Conan logo

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 different examples.

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:

git clone
cd examples/libraries/dear-imgui/basic
Enter fullscreen mode Exit fullscreen mode

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.

cmake_minimum_required(VERSION 3.0)
project(dear-imgui-conan CXX)


# CONFIG option is important so
# CMake doesn't search for modules in default directory
find_package(imgui CONFIG)
find_package(glfw CONFIG)
find_package(glew CONFIG)

add_executable( dear-imgui-conan

                    ${CMAKE_COMMAND} -E copy
                    ${CMAKE_COMMAND} -E copy


Enter fullscreen mode Exit fullscreen mode

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.



./misc/bindings, imgui_impl_glfw.cpp -> ../bindings
./misc/bindings, imgui_impl_opengl3.cpp -> ../bindings
./misc/bindings, imgui_impl_glfw.h -> ../bindings
./misc/bindings, imgui_impl_opengl3.h -> ../bindings
Enter fullscreen mode Exit fullscreen mode

Now let's build the project and run the application.

cd dear-imgui-conan-example
mkdir build
cd build
conan install .. -s build_type=Release
conan install .. -s build_type=Debug
cmake .. -G "Visual Studio 15 2017 Win64"
cmake --build . --config Release cd Release
Enter fullscreen mode Exit fullscreen mode


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!

Discussion (0)