main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <igl/readOFF.h>
  2. #include <igl/opengl/glfw/Viewer.h>
  3. #include <igl/opengl/glfw/imgui/ImGuiMenu.h>
  4. #include <igl/opengl/glfw/imgui/ImGuiHelpers.h>
  5. #include <imgui/imgui.h>
  6. #include <iostream>
  7. #include "tutorial_shared_path.h"
  8. int main(int argc, char *argv[])
  9. {
  10. Eigen::MatrixXd V;
  11. Eigen::MatrixXi F;
  12. // Load a mesh in OFF format
  13. igl::readOFF(TUTORIAL_SHARED_PATH "/bunny.off", V, F);
  14. // Init the viewer
  15. igl::opengl::glfw::Viewer viewer;
  16. // Attach a menu plugin
  17. igl::opengl::glfw::imgui::ImGuiMenu menu;
  18. viewer.plugins.push_back(&menu);
  19. // Customize the menu
  20. double doubleVariable = 0.1f; // Shared between two menus
  21. // Add content to the default menu window
  22. menu.callback_draw_viewer_menu = [&]()
  23. {
  24. // Draw parent menu content
  25. menu.draw_viewer_menu();
  26. // Add new group
  27. if (ImGui::CollapsingHeader("New Group", ImGuiTreeNodeFlags_DefaultOpen))
  28. {
  29. // Expose variable directly ...
  30. ImGui::InputDouble("double", &doubleVariable, 0, 0, "%.4f");
  31. // ... or using a custom callback
  32. static bool boolVariable = true;
  33. if (ImGui::Checkbox("bool", &boolVariable))
  34. {
  35. // do something
  36. std::cout << "boolVariable: " << std::boolalpha << boolVariable << std::endl;
  37. }
  38. // Expose an enumeration type
  39. enum Orientation { Up=0, Down, Left, Right };
  40. static Orientation dir = Up;
  41. ImGui::Combo("Direction", (int *)(&dir), "Up\0Down\0Left\0Right\0\0");
  42. // We can also use a std::vector<std::string> defined dynamically
  43. static int num_choices = 3;
  44. static std::vector<std::string> choices;
  45. static int idx_choice = 0;
  46. if (ImGui::InputInt("Num letters", &num_choices))
  47. {
  48. num_choices = std::max(1, std::min(26, num_choices));
  49. }
  50. if (num_choices != (int) choices.size())
  51. {
  52. choices.resize(num_choices);
  53. for (int i = 0; i < num_choices; ++i)
  54. choices[i] = std::string(1, 'A' + i);
  55. if (idx_choice >= num_choices)
  56. idx_choice = num_choices - 1;
  57. }
  58. ImGui::Combo("Letter", &idx_choice, choices);
  59. // Add a button
  60. if (ImGui::Button("Print Hello", ImVec2(-1,0)))
  61. {
  62. std::cout << "Hello\n";
  63. }
  64. }
  65. };
  66. // Draw additional windows
  67. menu.callback_draw_custom_window = [&]()
  68. {
  69. // Define next window position + size
  70. ImGui::SetNextWindowPos(ImVec2(180.f * menu.menu_scaling(), 10), ImGuiSetCond_FirstUseEver);
  71. ImGui::SetNextWindowSize(ImVec2(200, 160), ImGuiSetCond_FirstUseEver);
  72. ImGui::Begin(
  73. "New Window", nullptr,
  74. ImGuiWindowFlags_NoSavedSettings
  75. );
  76. // Expose the same variable directly ...
  77. ImGui::PushItemWidth(-80);
  78. ImGui::DragScalar("double", ImGuiDataType_Double, &doubleVariable, 0.1, 0, 0, "%.4f");
  79. ImGui::PopItemWidth();
  80. static std::string str = "bunny";
  81. ImGui::InputText("Name", str);
  82. ImGui::End();
  83. };
  84. // Plot the mesh
  85. viewer.data().set_mesh(V, F);
  86. viewer.launch();
  87. }