Adding a custom menu bar and widgets to the Easy3D Viewer with Dear ImGui.
The default Viewer already offers a rich interaction model, but applications often need their own menus, buttons, and dialogs. This tutorial shows how to layer a Dear ImGui GUI on top of the Viewer by subclassing it (ViewerImGui, shown after this file). The entry point is identical to Tutorial_201 except that it instantiates ViewerImGui instead of Viewer; all of the GUI logic lives in the subclass.
Dear ImGui is an immediate-mode GUI: rather than building a persistent widget tree, you describe the UI every frame by calling functions like BeginMainMenuBar(), MenuItem(), and Text(). Each call both renders the widget and reports whether the user interacted with it, so the same code that draws a "File > Open" item also triggers the open() action when it is clicked. The ViewerImGui subclass hooks into the viewer's frame at three points: init() creates the ImGui context and GLFW backend, pre_draw() starts an ImGui frame, lets draw_gui() emit the widgets, then converts ImGui's draw lists into Easy3D overlay draw commands, and post_resize() keeps ImGui's display size in sync with the window. Mouse/keyboard events are routed through callbacks so that clicks on the GUI are consumed by ImGui instead of rotating the scene. See the ViewerImGui class for the full integration.
A window opens with a menu bar at the top. Use File to open/save models or quit, View to take a snapshot, and Help to show an About dialog. The 3D scene underneath remains fully navigable; clicks on the menus are handled by ImGui rather than the camera.
29#include <easy3d/util/file_system.h>
30#include <easy3d/core/point_cloud.h>
31#include <easy3d/core/surface_mesh.h>
32#include <easy3d/renderer/camera.h>
34#define GLFW_INCLUDE_NONE
35#include <3rd_party/glfw/include/GLFW/glfw3.h>
37#include <3rd_party/imgui/misc/fonts/imgui_fonts_droid_sans.h>
38#include <3rd_party/imgui/imgui.h>
39#include <3rd_party/imgui/backends/imgui_impl_glfw.h>
50ViewerImGui::ViewerImGui(
const std::string &title,
53 ) :
Viewer(title, window, options)
63#if defined(_WIN32) && defined(_MSC_VER)
65 throw std::runtime_error(
"glfwInit failed for ViewerImGui's GLFW copy");
69ViewerImGui::~ViewerImGui() {
74 ImGui::SetCurrentContext(context_);
75 if (ImGui::GetIO().BackendPlatformUserData)
76 ImGui_ImplGlfw_Shutdown();
77 ImGui::DestroyContext(context_);
85void ViewerImGui::init() {
93 context_ = ImGui::CreateContext();
96 if (!ImGui_ImplGlfw_InitForOther(window(),
true))
97 throw std::runtime_error(
"could not initialize ImGui GLFW backend");
98 ImGui::GetIO().BackendRendererName =
"Easy3D annotations";
99 ImGui::GetIO().BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
100 ImGuiIO &io = ImGui::GetIO();
101 io.WantCaptureKeyboard =
true;
102 io.WantTextInput =
true;
103 io.IniFilename =
nullptr;
104 ImGui::StyleColorsLight();
105 ImGuiStyle &style = ImGui::GetStyle();
106 style.FrameRounding = 5.0f;
118 bool imgui_wants_mouse(ImGuiContext *context) {
121 ImGui::SetCurrentContext(context);
122 return ImGui::GetIO().WantCaptureMouse;
125 bool imgui_wants_keyboard(ImGuiContext *context) {
128 ImGui::SetCurrentContext(context);
129 return ImGui::GetIO().WantCaptureKeyboard ||
130 ImGui::IsPopupOpen(
nullptr, ImGuiPopupFlags_AnyPopup);
134bool ViewerImGui::mouse_press_event(
int x,
int y,
int button,
int modifiers) {
135 return imgui_wants_mouse(context_) || Viewer::mouse_press_event(x, y, button, modifiers);
138bool ViewerImGui::mouse_release_event(
int x,
int y,
int button,
int modifiers) {
139 return imgui_wants_mouse(context_) || Viewer::mouse_release_event(x, y, button, modifiers);
142bool ViewerImGui::mouse_drag_event(
int x,
int y,
int dx,
int dy,
int button,
int modifiers) {
143 return imgui_wants_mouse(context_) || Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
146bool ViewerImGui::mouse_free_move_event(
int x,
int y,
int dx,
int dy,
int modifiers) {
147 return imgui_wants_mouse(context_) || Viewer::mouse_free_move_event(x, y, dx, dy, modifiers);
150bool ViewerImGui::mouse_scroll_event(
int x,
int y,
int dx,
int dy) {
151 return imgui_wants_mouse(context_) || Viewer::mouse_scroll_event(x, y, dx, dy);
154bool ViewerImGui::key_press_event(
int key,
int modifiers) {
155 return imgui_wants_keyboard(context_) || Viewer::key_press_event(key, modifiers);
158bool ViewerImGui::key_release_event(
int key,
int modifiers) {
159 return imgui_wants_keyboard(context_) || Viewer::key_release_event(key, modifiers);
162bool ViewerImGui::char_input_event(
unsigned int codepoint) {
163 return imgui_wants_keyboard(context_) || Viewer::char_input_event(codepoint);
167float ViewerImGui::pixel_ratio() {
169 int fw = framebuffer_width();
171 return static_cast<float>(fw) /
static_cast<float>(vw);
176void ViewerImGui::reload_font(
int font_size) {
177 ImGuiIO &io = ImGui::GetIO();
179 io.Fonts->AddFontFromMemoryCompressedTTF(droid_sans_compressed_data, droid_sans_compressed_size,
180 static_cast<float>(font_size) * dpi_scaling());
181 io.FontGlobalScale = 1.0f / pixel_ratio();
183 unsigned char *pixels;
185 io.Fonts->GetTexDataAsRGBA32(&pixels, &w, &h);
187 sampler.
filter = graphics::TextureFilter::linear;
189 font_image_ = std::make_shared<graphics::Image>(
190 w, h, std::vector<unsigned char>(pixels, pixels +
size_t(w) * h * 4), sampler);
192 io.Fonts->SetTexID(ImTextureID(
reinterpret_cast<uintptr_t
>(font_image_.get())));
195void ViewerImGui::post_resize(
int w,
int h) {
196 Viewer::post_resize(w, h);
199 ImGui::GetIO().DisplaySize.x = float(w);
200 ImGui::GetIO().DisplaySize.y = float(h);
206void ViewerImGui::pre_draw() {
207 ImGui::SetCurrentContext(context_);
208 ImGui_ImplGlfw_NewFrame();
210 if (std::abs(ImGui::GetIO().FontGlobalScale - 1.0f / pixel_ratio()) > 1e-4f)
217 const auto *data = ImGui::GetDrawData();
218 const ImVec2 scale = data->FramebufferScale, origin = data->DisplayPos;
220 framebuffer_size(fw, fh);
221 std::vector<graphics::DrawCommand> commands;
222 for (
int n = 0; n < data->CmdListsCount; ++n) {
223 const auto *list = data->CmdLists[n];
224 for (
const auto &draw : list->CmdBuffer) {
225 if (draw.UserCallback) {
226 if (draw.UserCallback != ImDrawCallback_ResetRenderState)
227 draw.UserCallback(list, &draw);
234 const int x = std::max(0,
int(std::floor((draw.ClipRect.x - origin.x) * scale.x)));
235 const int y = std::max(0,
int(std::floor((draw.ClipRect.y - origin.y) * scale.y)));
236 const int right = std::min(fw,
int(std::ceil((draw.ClipRect.z - origin.x) * scale.x)));
237 const int bottom = std::min(fh,
int(std::ceil((draw.ClipRect.w - origin.y) * scale.y)));
238 if (right <= x || bottom <= y)
240 if (draw.GetTexID() != ImGui::GetIO().Fonts->TexID)
241 throw std::logic_error(
"this tutorial supports the ImGui font atlas texture only");
244 std::vector<graphics::Vertex> vertices;
245 vertices.reserve(draw.ElemCount);
246 for (
unsigned i = 0; i < draw.ElemCount; ++i) {
247 const auto &source = list->VtxBuffer[list->IdxBuffer[draw.IdxOffset + i] + draw.VtxOffset];
250 vec3((source.pos.x - origin.x) * scale.x - x, (source.pos.y - origin.y) * scale.y - y, 0);
252 v.
color =
vec4(
float((source.col >> IM_COL32_R_SHIFT) & 255) / 255,
253 float((source.col >> IM_COL32_G_SHIFT) & 255) / 255,
254 float((source.col >> IM_COL32_B_SHIFT) & 255) / 255,
255 float((source.col >> IM_COL32_A_SHIFT) & 255) / 255);
256 vertices.push_back(v);
259 command.overlay =
true;
260 command.texture = font_image_;
261 command.viewport =
ivec4(x, y, right - x, bottom - y);
262 command.model_view_projection = graphics::screen_space_projection(right - x, bottom - y);
263 command.geometry = std::make_shared<graphics::Geometry>(std::move(vertices));
264 commands.push_back(std::move(command));
268 set_overlays(std::move(commands),
true);
273void ViewerImGui::draw_gui() {
274 ImGui::SetCurrentContext(context_);
275 static bool show_about =
false;
279 ImGui::SetNextWindowPos(ImVec2(
static_cast<float>(w) * 0.5f,
static_cast<float>(h) * 0.5f),
280 ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
281 ImGui::Begin(
"About Easy3D ImGui Viewer", &show_about, ImGuiWindowFlags_NoResize);
282 ImGui::Text(
"This viewer shows how to use ImGui for GUI creation and event handling");
284 ImGui::Text(
"\n" "Liangliang Nan\n" "liangliang.nan@gmail.com\n" "https://3d.bk.tudelft.nl/liangliang/\n");
292 ImGui::PushStyleVar(ImGuiStyleVar_FramePadding,
293 ImVec2(5, ImGui::GetStyle().DisplaySafeAreaPadding.y));
295 if (ImGui::BeginMainMenuBar()) {
299 if (ImGui::BeginMenu(
"Help")) {
300 ImGui::MenuItem(
"About",
nullptr, &show_about);
303 menu_height_ = ImGui::GetWindowHeight();
304 ImGui::EndMainMenuBar();
306 ImGui::PopStyleVar();
312void ViewerImGui::draw_menu_file() {
313 if (ImGui::BeginMenu(
"File")) {
314 if (ImGui::MenuItem(
"Open",
"Ctrl+O"))
316 if (ImGui::MenuItem(
"Save As...",
"Ctrl+S"))
320 if (ImGui::MenuItem(
"Quit",
"Alt+F4"))
328void ViewerImGui::draw_menu_view() {
329 if (ImGui::BeginMenu(
"View")) {
330 if (ImGui::MenuItem(
"Snapshot",
nullptr))
Complete backend-independent description of a draw operation.
Definition render_device.h:219
Texture addressing and filtering configuration.
Definition render_device.h:165
TextureFilter filter
Interpolation used when sampling between texels.
Definition render_device.h:169
Per-vertex data consumed by a draw command.
Definition render_device.h:91
vec3 position
Position in geometry-local coordinates.
Definition render_device.h:93
vec2 texcoord
Texture coordinate.
Definition render_device.h:99
vec4 color
RGBA vertex color.
Definition render_device.h:97