main.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. int main(int argc, char * argv[])
  7. {
  8. igl::opengl::glfw::Viewer viewer;
  9. const auto names =
  10. {"cube.obj","sphere.obj","xcylinder.obj","ycylinder.obj","zcylinder.obj"};
  11. std::vector<Eigen::RowVector3d> colors;
  12. int last_selected = -1;
  13. for(const auto & name : names)
  14. {
  15. viewer.load_mesh_from_file(std::string(TUTORIAL_SHARED_PATH) + "/" + name);
  16. colors.push_back(0.5*Eigen::RowVector3d::Random().array() + 0.5);
  17. }
  18. viewer.callback_key_down =
  19. [&](igl::opengl::glfw::Viewer &, unsigned int key, int mod)
  20. {
  21. if(key == GLFW_KEY_BACKSPACE)
  22. {
  23. int old_index = viewer.selected_data_index;
  24. if (viewer.erase_mesh(viewer.selected_data_index))
  25. {
  26. colors.erase(colors.begin() + old_index);
  27. last_selected = -1;
  28. }
  29. return true;
  30. }
  31. return false;
  32. };
  33. // Refresh selected mesh colors
  34. viewer.callback_pre_draw =
  35. [&](igl::opengl::glfw::Viewer &)
  36. {
  37. if (last_selected != viewer.selected_data_index)
  38. {
  39. for (size_t i = 0; i < viewer.data_list.size(); ++i)
  40. {
  41. viewer.data_list[i].set_colors(colors[i]);
  42. }
  43. viewer.data_list[viewer.selected_data_index].set_colors(Eigen::RowVector3d(0.9,0.1,0.1));
  44. last_selected = viewer.selected_data_index;
  45. }
  46. return false;
  47. };
  48. viewer.launch();
  49. return EXIT_SUCCESS;
  50. }