r/opengl • u/Usual_Office_1740 • 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?
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/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/AdministrativeRow904 11d ago
GetBackgroundDrawList will let you render directly with the supplied draw functions anywhere in the window.