ImGuiMenu.cpp 11 KB

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