main.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "tutorial_shared_path.h"
  2. #include <igl/opengl/glfw/Viewer.h>
  3. #include <GLFW/glfw3.h>
  4. #include <string>
  5. #include <iostream>
  6. #include <map>
  7. int main(int argc, char * argv[])
  8. {
  9. igl::opengl::glfw::Viewer viewer;
  10. const auto names =
  11. {"cube.obj","sphere.obj","xcylinder.obj","ycylinder.obj","zcylinder.obj"};
  12. std::map<int, Eigen::RowVector3d> colors;
  13. int last_selected = -1;
  14. for(const auto & name : names)
  15. {
  16. viewer.load_mesh_from_file(std::string(TUTORIAL_SHARED_PATH) + "/" + name);
  17. colors.emplace(viewer.data().id, 0.5*Eigen::RowVector3d::Random().array() + 0.5);
  18. }
  19. viewer.callback_key_down =
  20. [&](igl::opengl::glfw::Viewer &, unsigned int key, int mod)
  21. {
  22. if(key == GLFW_KEY_BACKSPACE)
  23. {
  24. int old_id = viewer.data().id;
  25. if (viewer.erase_mesh(viewer.selected_data_index))
  26. {
  27. colors.erase(old_id);
  28. last_selected = -1;
  29. }
  30. return true;
  31. }
  32. return false;
  33. };
  34. // Refresh selected mesh colors
  35. viewer.callback_pre_draw =
  36. [&](igl::opengl::glfw::Viewer &)
  37. {
  38. if (last_selected != viewer.selected_data_index)
  39. {
  40. for (auto &data : viewer.data_list)
  41. {
  42. data.set_colors(colors[data.id]);
  43. }
  44. viewer.data_list[viewer.selected_data_index].set_colors(Eigen::RowVector3d(0.9,0.1,0.1));
  45. last_selected = viewer.selected_data_index;
  46. }
  47. return false;
  48. };
  49. viewer.launch();
  50. return EXIT_SUCCESS;
  51. }