main.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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<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. int main(int argc, char * argv[])
  26. {
  27. igl::opengl::glfw::Viewer viewer;
  28. auto names = {"cube.obj","sphere.obj","xcylinder.obj","ycylinder.obj","zcylinder.obj"};
  29. for(const auto & name : names)
  30. {
  31. viewer.load_mesh_from_file(std::string(TUTORIAL_SHARED_PATH) + "/" + name);
  32. viewer.data().attr<MeshData>().name = name;
  33. viewer.data().attr<MeshData>().color = Eigen::RowVector3d::Random();
  34. }
  35. // Attach a custom menu
  36. igl::opengl::glfw::imgui::ImGuiMenu menu;
  37. viewer.plugins.push_back(&menu);
  38. // Customize default menu
  39. menu.draw_viewer_menu_func = [&]()
  40. {
  41. if (ImGui::Combo("Selected Mesh", (int *) &viewer.selected_data_index,
  42. [&](int i) { return viewer.data_list[i].attr<MeshData>().name.c_str(); },
  43. viewer.data_list.size())
  44. || last_colored_index != viewer.selected_data_index)
  45. {
  46. update_colors(viewer);
  47. }
  48. };
  49. // Color each mesh differently
  50. update_colors(viewer);
  51. viewer.callback_key_down =
  52. [&](igl::opengl::glfw::Viewer &, unsigned int key, int mod)
  53. {
  54. // Delete
  55. if(key == '3')
  56. {
  57. viewer.erase_mesh(viewer.selected_data_index);
  58. update_colors(viewer);
  59. return true;
  60. }
  61. return false;
  62. };
  63. viewer.launch();
  64. return EXIT_SUCCESS;
  65. }