ImGuiMenu.cpp 12 KB

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