r/opengl • • 11d ago

Rendering text through ImGui?

I'm hoping to get a basic repl running with ImGui, OpenGL, and GLFW. I'm comfortable with GLfW and OpenGL rendering but not the ImGui portion.

My question is, can I use the OpenGL context and the GLFW window and then get basic text editing functionality with just ImGui? No opengl buffers, no custom shaders. I think the opengl context needs a default vertex and fragment shader to function but I'm looking to keep that very minimal and handle basic repl input and text rendering between imgui::begin() and imgui::end(). A basic per keypress render loop where each GLFW key event updates a string and then ImGui renders a new frame for the window. I haven't worked with ImGui before. Do you think this is possible?

2 Upvotes

11 comments sorted by

5

u/Western-Bowl2011 10d ago

you don't even need a vertex or fragment shader to get ImGui to work, it provides its own. when you call ImGui::Render it saves the context, switches to it's own, then restores it back, it's seamless. You also don't have to gather keypresses, ImGui does that for you. To draw a text box to enter text into, you just do

ImGui::TextInput(const char* label, char* buf);

The box edits the character buffer directly. There exists a whole widget already that provides a full featured text editor if you need more than a single text input box:

https://github.com/BalazsJako/ImGuiColorTextEdit

1

u/Usual_Office_1740 10d ago

That git repo has got me off to a great start. Thanks for your help and explanation in this post and your other response.

3

u/Western-Bowl2011 10d ago edited 10d ago

Heads up that by default, ImGui only works on c-style strings, i.e. const char*. Look up imgui_stdlib, make sure you add it to your project. It provides overloaded functions that work with c++ style strings

Make sure you check out the demo window that ImGui comes with by default. It's the easiest way to learn ImGui. Just open the demo window, check out the features you have, then go to the demo cpp file and search for the section name. i.e. if you want to add a checkbox to your program, search the demo cpp file for "checkbox" and then copy and paste the code directly into your own program. Thats how I rapidly develop my own programs using ImGui.

1

u/Usual_Office_1740 10d ago

Thanks. For the tip. You've been a big help

2

u/Western-Bowl2011 10d ago

Oh, and one other tip, since I assume you want to develop Gui programs with this: https://github.com/mlabbe/nativefiledialog

This library, native file dialog, is a godsend. It provides a common interface that pulls up and uses an operating system's desktop environment's native file dialog boxes. It lets you pull up open dialogs, folder picker dialogs, and save dialogs. It returns the results of these dialogs as a c-style string. This, combined with ImGui, lets you write incredibly cross-platform GUI programs with absolute ease. Write once, and it'll run on Windows, KDE, Gnome, and OSX, from a single source. I combine this with ImGui all the time, it's such a cool library. I legitimately think these two libraries together are the greatest cross-platform UI around these days. Super simple to use.

1

u/lithium 11d ago

You absolutely need at least a vertex/index buffer and a shader to render your imgui draw commands, but imgui comes with a glfw backend that will provide most of it for you. Just run the glfw demo that comes with a stock ImGui clone and you're 90% of the way there.

1

u/Usual_Office_1740 11d ago

Will I need to setup a way to get a font into the gpu as a texture or array of textures or can ImGui do that part for me?

1

u/lithium 10d ago

ImGui comes with a default bitmap font, but you can setup the theme and load fonts via their internal API

ImGuiIO& io = ImGui::GetIO();
ImFont* customFont = io.Fonts->AddFontFromFileTTF("path/to/font.ttf");

Just look at the demo and library source code, it's heavily documented.

1

u/Usual_Office_1740 10d ago

I'll dig into it more. Thanks.

1

u/Western-Bowl2011 10d ago

That's how ImGui actually works. When you load a font into ImGui it creates a bitmap texture atlus for the font. For this reason, you actually need to setup all sizes of fonts you want available upfront at compile time. Otherwise, all you can really do is scale the texture. Example:

ImFont* editorFont_13;
ImFont* editorFont_16;
ImFont* editorFont_19;
ImFont* editorFont_22;

// Setup Dear ImGui context
IMGUI_GL_EXTENSION_LOADER;
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows

// Setup Dear ImGui style
ImGui::StyleColorsDark();

/* Setup Editor Fonts */
editorFont_13 = io.Fonts->AddFontFromFileTTF("include/ImGuiTextEditor/ProggyClean.ttf", 13.0f);
editorFont_16 = io.Fonts->AddFontFromFileTTF("include/ImGuiTextEditor/ProggyClean.ttf", 16.0f);
editorFont_19 = io.Fonts->AddFontFromFileTTF("include/ImGuiTextEditor/ProggyClean.ttf", 19.0f);
editorFont_22 = io.Fonts->AddFontFromFileTTF("include/ImGuiTextEditor/ProggyClean.ttf", 22.0f);

// Setup Platform/Renderer bindings
ImGui_ImplSDL2_InitForOpenGL(window, context);
ImGui_ImplOpenGL3_Init(GLSL_VERSION);

This gives me the ability to switch between 13, 16, 19, and 22 point fonts in my program. Beyond that, I can scale the selected font.

1

u/AdministrativeRow904 11d ago

GetBackgroundDrawList will let you render directly with the supplied draw functions anywhere in the window.