main.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <igl/readOFF.h>
  2. #include <igl/viewer/Viewer.h>
  3. #include <iostream>
  4. #include "tutorial_shared_path.h"
  5. #include <igl/png/writePNG.h>
  6. #include <igl/png/readPNG.h>
  7. // This function is called every time a keyboard button is pressed
  8. bool key_down(igl::viewer::Viewer& viewer, unsigned char key, int modifier)
  9. {
  10. if (key == '1')
  11. {
  12. // Allocate temporary buffers
  13. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> R(1280,800);
  14. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> G(1280,800);
  15. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> B(1280,800);
  16. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> A(1280,800);
  17. // Draw the scene in the buffers
  18. viewer.core.draw_buffer(viewer.data,viewer.opengl,false,R,G,B,A);
  19. // Save it to a PNG
  20. igl::png::writePNG(R,G,B,A,"out.png");
  21. }
  22. if (key == '2')
  23. {
  24. // Allocate temporary buffers
  25. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> R,G,B,A;
  26. // Read the PNG
  27. igl::png::readPNG("out.png",R,G,B,A);
  28. // Replace the mesh with a triangulated square
  29. Eigen::MatrixXd V(4,3);
  30. V <<
  31. -0.5,-0.5,0,
  32. 0.5,-0.5,0,
  33. 0.5, 0.5,0,
  34. -0.5, 0.5,0;
  35. Eigen::MatrixXi F(2,3);
  36. F <<
  37. 0,1,2,
  38. 2,3,0;
  39. Eigen::MatrixXd UV(4,2);
  40. UV <<
  41. 0,0,
  42. 1,0,
  43. 1,1,
  44. 0,1;
  45. viewer.data.clear();
  46. viewer.data.set_mesh(V,F);
  47. viewer.data.set_uv(UV);
  48. viewer.core.align_camera_center(V);
  49. viewer.core.show_texture = true;
  50. // Use the image as a texture
  51. viewer.data.set_texture(R,G,B);
  52. }
  53. return false;
  54. }
  55. int main(int argc, char *argv[])
  56. {
  57. // Load a mesh in OFF format
  58. Eigen::MatrixXd V;
  59. Eigen::MatrixXi F;
  60. igl::readOFF(TUTORIAL_SHARED_PATH "/bunny.off", V, F);
  61. std::cerr << "Press 1 to render the scene and save it in a png." << std::endl;
  62. std::cerr << "Press 2 to load the saved png and use it as a texture." << std::endl;
  63. // Plot the mesh and register the callback
  64. igl::viewer::Viewer viewer;
  65. viewer.callback_key_down = &key_down;
  66. viewer.data.set_mesh(V, F);
  67. viewer.launch();
  68. }