main.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "tutorial_shared_path.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 <string>
  6. #include <map>
  7. #include <iostream>
  8. std::vector<std::string> names = {"cube.obj","sphere.obj","xcylinder.obj","ycylinder.obj","zcylinder.obj"};
  9. std::map<std::string, Eigen::RowVector3d> colors;
  10. int last_colored_index = -1;
  11. // Set colors of each mesh
  12. void update_colors(igl::opengl::glfw::Viewer &viewer)
  13. {
  14. for (int i = 0; i < (int) viewer.data_list.size(); ++i)
  15. {
  16. viewer.data_list[i].set_colors(colors[names[i]]);
  17. }
  18. viewer.data_list[viewer.selected_data_index].set_colors(Eigen::RowVector3d(0.9,0.1,0.1));
  19. last_colored_index = viewer.selected_data_index;
  20. }
  21. // Menu for selecting a mesh
  22. class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
  23. {
  24. virtual void draw_viewer_menu() override
  25. {
  26. if (ImGui::Combo("Selected Mesh", (int *) &viewer->selected_data_index, names))
  27. {
  28. update_colors(*viewer);
  29. }
  30. if (last_colored_index != viewer->selected_data_index)
  31. {
  32. update_colors(*viewer);
  33. }
  34. }
  35. };
  36. int main(int argc, char * argv[])
  37. {
  38. igl::opengl::glfw::Viewer viewer;
  39. for(const auto & name : names)
  40. {
  41. viewer.load_mesh_from_file(std::string(TUTORIAL_SHARED_PATH) + "/" + name);
  42. colors[name] = Eigen::RowVector3d::Random();
  43. }
  44. // Attach a custom menu
  45. MyMenu menu;
  46. viewer.plugins.push_back(&menu);
  47. // Color each mesh differently
  48. update_colors(viewer);
  49. viewer.callback_key_down =
  50. [&](igl::opengl::glfw::Viewer &, unsigned int key, int mod)
  51. {
  52. // Delete
  53. if(key == '3')
  54. {
  55. int old_index = viewer.selected_data_index;
  56. if (viewer.erase_mesh(viewer.selected_data_index))
  57. {
  58. names.erase(names.begin() + old_index);
  59. update_colors(viewer);
  60. }
  61. return true;
  62. }
  63. return false;
  64. };
  65. viewer.launch();
  66. return EXIT_SUCCESS;
  67. }