main.cpp 2.1 KB

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