forked from libigl/libigl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImGuiMenu.cpp
433 lines (388 loc) · 12.7 KB
/
ImGuiMenu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2018 Jérémie Dumas <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
////////////////////////////////////////////////////////////////////////////////
#include "ImGuiMenu.h"
#include <igl/project.h>
#include <imgui/imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <imgui_fonts_droid_sans.h>
#include <GLFW/glfw3.h>
#include <iostream>
////////////////////////////////////////////////////////////////////////////////
namespace igl
{
namespace opengl
{
namespace glfw
{
namespace imgui
{
IGL_INLINE void ImGuiMenu::init(igl::opengl::glfw::Viewer *_viewer)
{
ViewerPlugin::init(_viewer);
if (_viewer)
{
init_imgui();
}
}
IGL_INLINE void ImGuiMenu::init_imgui()
{
// Setup ImGui binding
IMGUI_CHECKVERSION();
ImGuiContext *ctx = ImGui::GetCurrentContext();
ImFontAtlas* shared_font_atlas = (ctx ? ImGui::GetIO().Fonts : nullptr);
context_ = ImGui::CreateContext(shared_font_atlas);
ScopedContext scope(context_);
ImGui::GetIO().IniFilename = nullptr;
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
style.FrameRounding = 5.0f;
const char* glsl_version = "#version 150";
glfwMakeContextCurrent(viewer->window);
ImGui_ImplGlfw_InitForOpenGL(viewer->window, false);
ImGui_ImplOpenGL3_Init(glsl_version);
if (!ctx)
{
reload_font();
} else {
hidpi_scaling_ = hidpi_scaling();
pixel_ratio_ = pixel_ratio();
}
}
IGL_INLINE void ImGuiMenu::reload_font(int font_size)
{
hidpi_scaling_ = hidpi_scaling();
pixel_ratio_ = pixel_ratio();
ImGuiIO& io = ImGui::GetIO();
io.Fonts->Clear();
io.Fonts->AddFontFromMemoryCompressedTTF(droid_sans_compressed_data,
droid_sans_compressed_size, font_size * hidpi_scaling_);
io.FontGlobalScale = 1.0 / pixel_ratio_;
}
IGL_INLINE void ImGuiMenu::shutdown()
{
// Keep existing bindings, since we share the OpenGL context between windows.
glfwMakeContextCurrent(viewer->window);
ScopedContext scope(context_);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext(context_);
context_ = nullptr;
}
IGL_INLINE bool ImGuiMenu::pre_draw()
{
glfwPollEvents();
glfwMakeContextCurrent(viewer->window);
old_context_ = ImGui::GetCurrentContext();
ImGui::SetCurrentContext(context_);
// Check whether window dpi has changed
float scaling = hidpi_scaling();
if (std::abs(scaling - hidpi_scaling_) > 1e-5)
{
reload_font();
ImGui_ImplOpenGL3_DestroyDeviceObjects();
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushID((long long) this);
return false;
}
IGL_INLINE bool ImGuiMenu::post_draw()
{
draw_menu();
ImGui::PopID();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui::SetCurrentContext(old_context_);
return false;
}
IGL_INLINE void ImGuiMenu::post_resize(int width, int height)
{
if (context_)
{
ScopedContext scope(context_);
ImGui::GetIO().DisplaySize.x = float(width);
ImGui::GetIO().DisplaySize.y = float(height);
}
}
// Mouse IO
IGL_INLINE bool ImGuiMenu::mouse_down(int button, int modifier)
{
ScopedContext scope(context_);
ImGui_ImplGlfw_MouseButtonCallback(viewer->window, button, GLFW_PRESS, modifier);
return ImGui::GetIO().WantCaptureMouse;
}
IGL_INLINE bool ImGuiMenu::mouse_up(int button, int modifier)
{
ScopedContext scope(context_);
return ImGui::GetIO().WantCaptureMouse;
}
IGL_INLINE bool ImGuiMenu::mouse_move(int mouse_x, int mouse_y)
{
ScopedContext scope(context_);
return ImGui::GetIO().WantCaptureMouse;
}
IGL_INLINE bool ImGuiMenu::mouse_scroll(float delta_y)
{
ScopedContext scope(context_);
ImGui_ImplGlfw_ScrollCallback(viewer->window, 0.f, delta_y);
return ImGui::GetIO().WantCaptureMouse;
}
// Keyboard IO
IGL_INLINE bool ImGuiMenu::key_pressed(unsigned int key, int modifiers)
{
ScopedContext scope(context_);
ImGui_ImplGlfw_CharCallback(nullptr, key);
return ImGui::GetIO().WantCaptureKeyboard;
}
IGL_INLINE bool ImGuiMenu::key_down(int key, int modifiers)
{
ScopedContext scope(context_);
ImGui_ImplGlfw_KeyCallback(viewer->window, key, 0, GLFW_PRESS, modifiers);
return ImGui::GetIO().WantCaptureKeyboard;
}
IGL_INLINE bool ImGuiMenu::key_up(int key, int modifiers)
{
ScopedContext scope(context_);
ImGui_ImplGlfw_KeyCallback(viewer->window, key, 0, GLFW_RELEASE, modifiers);
return ImGui::GetIO().WantCaptureKeyboard;
}
// Draw menu
IGL_INLINE void ImGuiMenu::draw_menu()
{
ScopedContext scope(context_);
// Text labels
draw_labels_window();
// Viewer settings
if (callback_draw_viewer_window) { callback_draw_viewer_window(); }
else { draw_viewer_window(); }
// Other windows
if (callback_draw_custom_window) { callback_draw_custom_window(); }
else { draw_custom_window(); }
}
IGL_INLINE void ImGuiMenu::draw_viewer_window()
{
ScopedContext scope(context_);
float menu_width = 180.f * menu_scaling();
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f), ImGuiSetCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f), ImGuiSetCond_FirstUseEver);
ImGui::SetNextWindowSizeConstraints(ImVec2(menu_width, -1.0f), ImVec2(menu_width, -1.0f));
bool _viewer_menu_visible = true;
ImGui::Begin(
"Viewer", &_viewer_menu_visible,
ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_AlwaysAutoResize
);
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.4f);
if (callback_draw_viewer_menu) { callback_draw_viewer_menu(); }
else { draw_viewer_menu(); }
ImGui::PopItemWidth();
ImGui::End();
}
IGL_INLINE void ImGuiMenu::draw_viewer_menu()
{
ScopedContext scope(context_);
// Workspace
if (ImGui::CollapsingHeader(("Workspace##" + std::to_string((long long) this)).c_str(), ImGuiTreeNodeFlags_DefaultOpen))
{
float w = ImGui::GetContentRegionAvailWidth();
float p = ImGui::GetStyle().FramePadding.x;
if (ImGui::Button("Load##Workspace", ImVec2((w-p)/2.f, 0)))
{
viewer->load_scene();
}
ImGui::SameLine(0, p);
if (ImGui::Button("Save##Workspace", ImVec2((w-p)/2.f, 0)))
{
viewer->save_scene();
}
}
// Mesh
if (ImGui::CollapsingHeader("Mesh", ImGuiTreeNodeFlags_DefaultOpen))
{
float w = ImGui::GetContentRegionAvailWidth();
float p = ImGui::GetStyle().FramePadding.x;
if (ImGui::Button("Load##Mesh", ImVec2((w-p)/2.f, 0)))
{
viewer->open_dialog_load_mesh();
}
ImGui::SameLine(0, p);
if (ImGui::Button("Save##Mesh", ImVec2((w-p)/2.f, 0)))
{
viewer->open_dialog_save_mesh();
}
}
// Viewing options
if (ImGui::CollapsingHeader("Viewing Options", ImGuiTreeNodeFlags_DefaultOpen))
{
if (ImGui::Button("Center object", ImVec2(-1, 0)))
{
viewer->core.align_camera_center(viewer->data().V, viewer->data().F);
}
if (ImGui::Button("Snap canonical view", ImVec2(-1, 0)))
{
viewer->snap_to_canonical_quaternion();
}
// Zoom
ImGui::PushItemWidth(80 * menu_scaling());
ImGui::DragFloat("Zoom", &(viewer->core.camera_zoom), 0.05f, 0.1f, 20.0f);
// Select rotation type
int rotation_type = static_cast<int>(viewer->core.rotation_type);
static Eigen::Quaternionf trackball_angle = Eigen::Quaternionf::Identity();
static bool orthographic = true;
if (ImGui::Combo("Camera Type", &rotation_type, "Trackball\0Two Axes\0002D Mode\0\0"))
{
using RT = igl::opengl::ViewerCore::RotationType;
auto new_type = static_cast<RT>(rotation_type);
if (new_type != viewer->core.rotation_type)
{
if (new_type == RT::ROTATION_TYPE_NO_ROTATION)
{
trackball_angle = viewer->core.trackball_angle;
orthographic = viewer->core.orthographic;
viewer->core.trackball_angle = Eigen::Quaternionf::Identity();
viewer->core.orthographic = true;
}
else if (viewer->core.rotation_type == RT::ROTATION_TYPE_NO_ROTATION)
{
viewer->core.trackball_angle = trackball_angle;
viewer->core.orthographic = orthographic;
}
viewer->core.set_rotation_type(new_type);
}
}
// Orthographic view
ImGui::Checkbox("Orthographic view", &(viewer->core.orthographic));
ImGui::PopItemWidth();
}
// Draw options
if (ImGui::CollapsingHeader("Draw Options", ImGuiTreeNodeFlags_DefaultOpen))
{
if (ImGui::Checkbox("Face-based", &(viewer->data().face_based)))
{
viewer->data().dirty = MeshGL::DIRTY_ALL;
}
ImGui::Checkbox("Show texture", &(viewer->data().show_texture));
if (ImGui::Checkbox("Invert normals", &(viewer->data().invert_normals)))
{
viewer->data().dirty |= igl::opengl::MeshGL::DIRTY_NORMAL;
}
ImGui::Checkbox("Show overlay", &(viewer->data().show_overlay));
ImGui::Checkbox("Show overlay depth", &(viewer->data().show_overlay_depth));
ImGui::ColorEdit4("Background", viewer->core.background_color.data(),
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_PickerHueWheel);
ImGui::ColorEdit4("Line color", viewer->data().line_color.data(),
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_PickerHueWheel);
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.3f);
ImGui::DragFloat("Shininess", &(viewer->data().shininess), 0.05f, 0.0f, 100.0f);
ImGui::PopItemWidth();
}
// Overlays
if (ImGui::CollapsingHeader("Overlays", ImGuiTreeNodeFlags_DefaultOpen))
{
ImGui::Checkbox("Wireframe", &(viewer->data().show_lines));
ImGui::Checkbox("Fill", &(viewer->data().show_faces));
ImGui::Checkbox("Show vertex labels", &(viewer->data().show_vertid));
ImGui::Checkbox("Show faces labels", &(viewer->data().show_faceid));
}
}
IGL_INLINE void ImGuiMenu::draw_labels_window()
{
ScopedContext scope(context_);
// Text labels
ImGui::SetNextWindowPos(ImVec2(0,0), ImGuiSetCond_Always);
ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize, ImGuiSetCond_Always);
bool visible = true;
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0,0,0,0));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0);
ImGui::Begin("ViewerLabels", &visible,
ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoScrollbar
| ImGuiWindowFlags_NoScrollWithMouse
| ImGuiWindowFlags_NoCollapse
| ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_NoInputs);
for (const auto & data : viewer->data_list)
{
draw_labels(data);
}
ImGui::End();
ImGui::PopStyleColor();
ImGui::PopStyleVar();
}
IGL_INLINE void ImGuiMenu::draw_labels(const igl::opengl::ViewerData &data)
{
ScopedContext scope(context_);
if (data.show_vertid)
{
for (int i = 0; i < data.V.rows(); ++i)
{
draw_text(data.V.row(i), data.V_normals.row(i), std::to_string(i));
}
}
if (data.show_faceid)
{
for (int i = 0; i < data.F.rows(); ++i)
{
Eigen::RowVector3d p = Eigen::RowVector3d::Zero();
for (int j = 0; j < data.F.cols(); ++j)
{
p += data.V.row(data.F(i,j));
}
p /= (double) data.F.cols();
draw_text(p, data.F_normals.row(i), std::to_string(i));
}
}
if (data.labels_positions.rows() > 0)
{
for (int i = 0; i < data.labels_positions.rows(); ++i)
{
draw_text(data.labels_positions.row(i), Eigen::Vector3d(0.0,0.0,0.0),
data.labels_strings[i]);
}
}
}
IGL_INLINE void ImGuiMenu::draw_text(Eigen::Vector3d pos, Eigen::Vector3d normal, const std::string &text)
{
ScopedContext scope(context_);
pos += normal * 0.005f * viewer->core.object_scale;
Eigen::Vector3f coord = igl::project(Eigen::Vector3f(pos.cast<float>()),
viewer->core.view, viewer->core.proj, viewer->core.viewport);
// Draw text labels slightly bigger than normal text
ImDrawList* drawList = ImGui::GetWindowDrawList();
drawList->AddText(ImGui::GetFont(), ImGui::GetFontSize() * 1.2,
ImVec2(coord[0]/pixel_ratio_, (viewer->core.viewport[3] - coord[1])/pixel_ratio_),
ImGui::GetColorU32(ImVec4(0, 0, 10, 255)),
&text[0], &text[0] + text.size());
}
IGL_INLINE float ImGuiMenu::pixel_ratio()
{
// Computes pixel ratio for hidpi devices
int buf_size[2];
int win_size[2];
GLFWwindow* window = glfwGetCurrentContext();
glfwGetFramebufferSize(window, &buf_size[0], &buf_size[1]);
glfwGetWindowSize(window, &win_size[0], &win_size[1]);
return (float) buf_size[0] / (float) win_size[0];
}
IGL_INLINE float ImGuiMenu::hidpi_scaling()
{
// Computes scaling factor for hidpi devices
float xscale, yscale;
GLFWwindow* window = glfwGetCurrentContext();
glfwGetWindowContentScale(window, &xscale, &yscale);
return 0.5 * (xscale + yscale);
}
} // end namespace
} // end namespace
} // end namespace
} // end namespace