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