-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathviewer_imgui.cpp
More file actions
439 lines (369 loc) · 15.1 KB
/
viewer_imgui.cpp
File metadata and controls
439 lines (369 loc) · 15.1 KB
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
434
435
436
437
438
439
/*
* Copyright (C) 2015 by Liangliang Nan (liangliang.nan@gmail.com)
* https://3d.bk.tudelft.nl/liangliang/
*
* This file is part of Easy3D. If it is useful in your research/work,
* I would be grateful if you show your appreciation by citing it:
* ------------------------------------------------------------------
* Liangliang Nan.
* Easy3D: a lightweight, easy-to-use, and efficient C++
* library for processing and rendering 3D data. 2018.
* ------------------------------------------------------------------
*
* Easy3D is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 3
* as published by the Free Software Foundation.
*
* Easy3D is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "viewer_imgui.h"
#include <cmath>
#include <iostream>
#include <easy3d/util/file.h>
#include <easy3d/core/point_cloud.h>
#include <easy3d/core/surface_mesh.h>
#include "imgui_fonts_droid_sans.h"
#include "Easy3D/tutorials/Tutorial_302_Viewer_imgui/imgui/imgui.h"
#include "Easy3D/tutorials/Tutorial_302_Viewer_imgui/imgui/impl/imgui_impl_glfw.h"
#include "Easy3D/tutorials/Tutorial_302_Viewer_imgui/imgui/impl/imgui_impl_opengl3.h"
#include <3rd_party/glfw/include/GLFW/glfw3.h>
#include <easy3d/viewer/drawable.h>
namespace easy3d {
ImGuiContext* ViewerImGui::context_ = nullptr;
ViewerImGui::ViewerImGui(
const std::string& title /* = "Easy3D ImGui Viewer" */,
int samples /* = 4 */,
int gl_major /* = 3 */,
int gl_minor /* = 2 */,
bool full_screen /* = false */,
bool resizable /* = true */,
int depth_bits /* = 24 */,
int stencil_bits /* = 8 */
)
: Viewer(title, samples, gl_major, gl_minor, full_screen, resizable, depth_bits, stencil_bits)
, alpha_(0.8f)
, movable_(true)
, update_gpu_(false)
{
}
bool ViewerImGui::callback_event_timer() {
//std::cout << "a sec has passed!" << std::endl;
update_gpu_ = true;
update();
return true;
}
void ViewerImGui::init() {
Viewer::init();
if (!context_) {
// Setup ImGui binding
IMGUI_CHECKVERSION();
context_ = ImGui::CreateContext();
const char* glsl_version = "#version 150";
ImGui_ImplGlfw_InitForOpenGL(window_, false);
ImGui_ImplOpenGL3_Init(glsl_version);
ImGuiIO& io = ImGui::GetIO();
io.WantCaptureKeyboard = true;
io.WantTextInput = true;
io.IniFilename = nullptr;
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
style.FrameRounding = 5.0f;
// load font
reload_font();
t.set_interval(100, &ViewerImGui::callback_event_timer, this);
}
}
double ViewerImGui::pixel_ratio() {
// Computes pixel ratio for hidpi devices
int fbo_size[2], win_size[2];
glfwGetFramebufferSize(window_, &fbo_size[0], &fbo_size[1]);
glfwGetWindowSize(window_, &win_size[0], &win_size[1]);
return static_cast<double>(fbo_size[0]) / static_cast<double>(win_size[0]);
}
void ViewerImGui::reload_font(int font_size)
{
ImGuiIO& io = ImGui::GetIO();
io.Fonts->Clear();
io.Fonts->AddFontFromMemoryCompressedTTF(droid_sans_compressed_data, droid_sans_compressed_size, font_size * dpi_scaling());
io.FontGlobalScale = 1.0f / pixel_ratio();
ImGui_ImplOpenGL3_DestroyDeviceObjects();
}
void ViewerImGui::post_resize(int w, int h) {
Viewer::post_resize(w, h);
if (context_) {
ImGui::GetIO().DisplaySize.x = float(w);
ImGui::GetIO().DisplaySize.y = float(h);
}
}
bool ViewerImGui::callback_event_cursor_pos(double x, double y) {
if (ImGui::GetIO().WantCaptureMouse)
return true;
else
return Viewer::callback_event_cursor_pos(x, y);
}
bool ViewerImGui::callback_event_mouse_button(int button, int action, int modifiers) {
if (ImGui::GetIO().WantCaptureMouse)
return true;
else
return Viewer::callback_event_mouse_button(button, action, modifiers);
}
bool ViewerImGui::callback_event_keyboard(int key, int action, int modifiers) {
if (ImGui::GetIO().WantCaptureKeyboard)
return true;
else
return Viewer::callback_event_keyboard(key, action, modifiers);
}
bool ViewerImGui::callback_event_character(unsigned int codepoint) {
if (ImGui::GetIO().WantCaptureKeyboard)
return true;
else
return Viewer::callback_event_character(codepoint);
}
bool ViewerImGui::callback_event_scroll(double dx, double dy) {
if (ImGui::GetIO().WantCaptureMouse)
return true;
else
return Viewer::callback_event_scroll(dx, dy);
}
void ViewerImGui::cleanup() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext(context_);
Viewer::cleanup();
}
void ViewerImGui::pre_draw() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
Viewer::pre_draw();
}
void ViewerImGui::draw_overlay(bool* visible)
{
ImGui::SetNextWindowSize(ImVec2(300 * widget_scaling(), 200 * widget_scaling()), ImGuiCond_FirstUseEver);
const float distance = 10.0f;
static int corner = 1;
ImVec2 window_pos = ImVec2(ImGui::GetIO().DisplaySize.x - distance , distance + 30);
ImVec2 window_pos_pivot = ImVec2((corner & 1) ? 1.0f : 0.0f, (corner & 2) ? 1.0f : 0.0f);
if (corner != -1)
ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot);
ImGui::SetNextWindowBgAlpha(alpha_); // Transparent background
if (ImGui::Begin("Easy3D: Information", visible, (corner != -1 ? ImGuiWindowFlags_NoMove : 0) | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize/* | ImGuiWindowFlags_AlwaysAutoResize*/ | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav))
{
ImGui::Text("Info (right-click to change position)");
ImGui::Separator();
ImGui::Text("Frame rate: %.1f", ImGui::GetIO().Framerate);
ImGui::Text("GPU time (ms): %4.1f", gpu_time_);
if (current_model()) {
const std::string& name = "Current model: " + file::simple_name(current_model()->name());
ImGui::Text("%s", name.c_str());
if (dynamic_cast<PointCloud*>(current_model())) {
PointCloud* cloud = dynamic_cast<PointCloud*>(current_model());
ImGui::Text("Type: point cloud");
ImGui::Text("#Vertices: %i", cloud->n_vertices());
}
else if (dynamic_cast<SurfaceMesh*>(current_model())) {
SurfaceMesh* mesh = dynamic_cast<SurfaceMesh*>(current_model());
ImGui::Text("Type: surface mesh");
ImGui::Text("#Faces: %i", mesh->n_faces());
ImGui::Text("#Vertices: %i", mesh->n_vertices());
ImGui::Text("#Edges: %i", mesh->n_edges());
}
}
ImGui::Separator();
int w, h;
glfwGetWindowSize(window_, &w, &h);
float x = ImGui::GetIO().MousePos.x;
float y = ImGui::GetIO().MousePos.y;
if (x >= 0 && x <= w && y >= 0 && y <= h) {
ImGui::Text("Mouse Position: (%i, %i)", static_cast<int>(x), static_cast<int>(y));
bool target = false;
const vec3& p = point_under_pixel(static_cast<int>(x), static_cast<int>(y), target);
if (target)
ImGui::Text("Point Under Mouse: (%.5f, %.5f, %.5f)", p.x, p.y, p.z);
else
ImGui::Text("Point Under Mouse: <invalid>");
}
else {
ImGui::Text("Mouse Position: <invalid>");
ImGui::Text("Point Under Mouse: <invalid>");
}
if (ImGui::BeginPopupContextWindow())
{
if (ImGui::MenuItem("Custom", nullptr, corner == -1)) corner = -1;
if (ImGui::MenuItem("Top-left", nullptr, corner == 0)) corner = 0;
if (ImGui::MenuItem("Top-right", nullptr, corner == 1)) corner = 1;
if (ImGui::MenuItem("Bottom-left", nullptr, corner == 2)) corner = 2;
if (ImGui::MenuItem("Bottom-right", nullptr, corner == 3)) corner = 3;
if (visible && ImGui::MenuItem("Close")) *visible = false;
ImGui::EndPopup();
}
}
ImGui::End();
}
void ViewerImGui::post_draw() {
static bool show_overlay = true;
if (show_overlay)
draw_overlay(&show_overlay);
static bool show_about = false;
if (show_about) {
ImGui::SetNextWindowPos(ImVec2(width() * 0.5f, height() * 0.5f), ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
ImGui::Begin("About Easy3D ImGui Viewer", &show_about, ImGuiWindowFlags_NoResize);
ImGui::Text(
"This viewer shows how to use ImGui for GUI creation and event handling"
);
ImGui::Separator();
ImGui::Text(
"\n"
"Liangliang Nan\n"
"liangliang.nan@gmail.com\n"
"https://3d.bk.tudelft.nl/liangliang/\n"
);
ImGui::End();
}
static bool show_manual = false;
if (show_manual) {
int w, h;
glfwGetWindowSize(window_, &w, &h);
ImGui::SetNextWindowPos(ImVec2(w * 0.5f, h * 0.5f), ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f));
ImGui::Begin("Easy3D Manual", &show_manual, ImGuiWindowFlags_NoResize);
ImGui::Text("%s", usage().c_str());
ImGui::End();
}
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(5, 8));
if (ImGui::BeginMainMenuBar())
{
draw_menu_file();
draw_menu_view();
if (ImGui::BeginMenu("Help"))
{
ImGui::MenuItem("Manual", nullptr, &show_manual);
ImGui::Separator();
ImGui::MenuItem("About", nullptr, &show_about);
ImGui::EndMenu();
}
menu_height_ = ImGui::GetWindowHeight();
ImGui::EndMainMenuBar();
}
ImGui::PopStyleVar();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
Viewer::post_draw();
}
void ViewerImGui::draw_menu_file() {
if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("Open", "Ctrl+O"))
open();
if (ImGui::MenuItem("Save As...", "Ctrl+S"))
save();
//ImGui::Separator();
//if (ImGui::BeginMenu("Recent Files...")) {
// std::string file_name;
// std::vector<Model*>::const_reverse_iterator it = models_.rbegin();
// for (; it != models_.rend(); ++it) {
// if (ImGui::MenuItem((*it)->name().c_str())) {
// file_name = (*it)->name();
// }
// }
// if (!file_name.empty())
// open(file_name);
// ImGui::EndMenu();
//}
ImGui::Separator();
if (ImGui::MenuItem("Quit", "Alt+F4"))
glfwSetWindowShouldClose(window_, GLFW_TRUE);
ImGui::EndMenu();
}
}
void ViewerImGui::draw_menu_view() {
if (ImGui::BeginMenu("View"))
{
if (ImGui::MenuItem("Snapshot", nullptr))
snapshot();
ImGui::Separator();
if (ImGui::BeginMenu("Options"))
{
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.50f);
static int style_idx = 1;
if (ImGui::Combo("Style", &style_idx, "Classic\0Dark\0Light\0")) {
switch (style_idx) {
case 0: ImGui::StyleColorsClassic(); break;
case 1: ImGui::StyleColorsDark(); break;
case 2: ImGui::StyleColorsLight(); break;
}
}
ImGui::Checkbox("Panel Movable", &movable_);
ImGui::ColorEdit3("Background Color", (float*)background_color_, ImGuiColorEditFlags_NoInputs);
ImGui::DragFloat("Transparency", &alpha_, 0.005f, 0.0f, 1.0f, "%.1f");
ImGui::PopItemWidth();
ImGui::EndMenu();
}
ImGui::EndMenu();
}
}
void ViewerImGui::draw() {
if (update_gpu_) {
easy3d::Model* model = this->current_model();
#if 1
if (model)
{
easy3d::PointCloud* cloud = dynamic_cast<PointCloud*>(model);
if (cloud)
{
// update vertices color
easy3d::PointsDrawable* drawable = cloud->points_drawable("vertices");
if (drawable)
{
PointCloud::VertexProperty<vec3> colors = cloud->get_vertex_property<vec3>("v:color");
PointCloud::VertexProperty<vec3> points = cloud->get_vertex_property<vec3>("v:point");
drawable->update_vertex_buffer(points.vector());
drawable->update_color_buffer(colors.vector());
drawable->set_per_vertex_color(true);
}
else
{
printf("drawable is null!!\n");
}
//if (gValidPointedNode)
{
#if 0
// update connections
easy3d::LinesDrawable* line_drawable = cloud->lines_drawable("connections");
std::vector<vec3> connections_points;
auto points = cloud->get_vertex_property<vec3>("v:point");
const vec3& s = gCurrentNode;//points.vector()[rand()%100];
const vec3& t = points.vector()[100 + (rand()%100)];
connections_points.push_back(s);
connections_points.push_back(t);
line_drawable->update_vertex_buffer(connections_points);
line_drawable->set_per_vertex_color(true);
line_drawable->set_default_color(vec3(1.0f, 0.0f, 0.0f));
//line_drawable->set_line_width(1);
PointCloud::VertexProperty<vec3> line_colors = cloud->get_vertex_property<vec3>("v:color");
line_colors.vector()[0] = random_color(); // assign a random color to point 'v'
line_drawable->update_color_buffer(line_colors.vector());
#endif
}
update();
}
else
{
printf("cloud is null!!\n");
}
}
else
{
printf("Model is null!!\n");
}
#endif
update_gpu_ = false;
}
Viewer::draw();
}
}