ImGuiMenu.cpp 12 KB

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