main.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.core.is_animating = true;
  47. viewer.plugins.push_back(&menu);
  48. // Color each mesh differently
  49. update_colors(viewer);
  50. viewer.callback_key_down =
  51. [&](igl::opengl::glfw::Viewer &, unsigned int key, int mod)
  52. {
  53. // Delete
  54. if(key == '3')
  55. {
  56. int old_index = viewer.selected_data_index;
  57. if (viewer.erase_mesh(viewer.selected_data_index))
  58. {
  59. names.erase(names.begin() + old_index);
  60. update_colors(viewer);
  61. }
  62. return true;
  63. }
  64. return false;
  65. };
  66. viewer.launch();
  67. return EXIT_SUCCESS;
  68. }