Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  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.  
  8. // This function is called every time a keyboard button is pressed
  9. bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifier)
  10. {
  11. std::cout << "detected" << key << std::endl;
  12. if (key == '1')
  13. {
  14. // Allocate temporary buffers
  15. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> R(1280,800);
  16. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> G(1280,800);
  17. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> B(1280,800);
  18. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> A(1280,800);
  19.  
  20. // Draw the scene in the buffers
  21. viewer.core().draw_buffer(
  22. viewer.data(),false,R,G,B,A);
  23.  
  24. // Save it to a PNG
  25. igl::png::writePNG(R,G,B,A,"out.png");
  26. }
  27.  
  28. if (key == '2')
  29. {
  30. // Allocate temporary buffers
  31. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> R,G,B,A;
  32.  
  33. // Read the PNG
  34. igl::png::readPNG("out.png",R,G,B,A);
  35.  
  36. // Replace the mesh with a triangulated square
  37. Eigen::MatrixXd V(4,3);
  38. V <<
  39. -0.5,-0.5,0,
  40. 0.5,-0.5,0,
  41. 0.5, 0.5,0,
  42. -0.5, 0.5,0;
  43. Eigen::MatrixXi F(2,3);
  44. F <<
  45. 0,1,2,
  46. 2,3,0;
  47. Eigen::MatrixXd UV(4,2);
  48. UV <<
  49. 0,0,
  50. 1,0,
  51. 1,1,
  52. 0,1;
  53.  
  54. viewer.data().clear();
  55. viewer.data().set_mesh(V,F);
  56. viewer.data().set_uv(UV);
  57. viewer.core().align_camera_center(V);
  58. viewer.data().show_texture = true;
  59.  
  60. // Use the image as a texture
  61. viewer.data().set_texture(R,G,B);
  62.  
  63. }
  64. if (key == 'E')
  65. {
  66. viewer.core().camera_translation = viewer.core().camera_translation + Eigen::Vector3f(0, 0.05, 0);
  67. }
  68. if (key == 'Q')
  69. {
  70. viewer.core().camera_translation = viewer.core().camera_translation + Eigen::Vector3f(0, -0.05, 0);
  71. }
  72. if (key == 'A')
  73. {
  74. viewer.core().camera_translation = viewer.core().camera_translation + Eigen::Vector3f(0.05,0, 0);
  75. }
  76. if (key == 'D')
  77. {
  78. viewer.core().camera_translation = viewer.core().camera_translation + Eigen::Vector3f(-0.05, 0, 0);
  79. }
  80. if (key == 'W')
  81. {
  82. viewer.core().camera_translation = viewer.core().camera_translation + Eigen::Vector3f(0, 0, 0.05);
  83. }
  84. if (key == 'S')
  85. {
  86. viewer.core().camera_translation = viewer.core().camera_translation + Eigen::Vector3f(0, 0, -0.05);
  87. }
  88. return false;
  89. }
  90.  
  91. int main(int argc, char *argv[])
  92. {
  93. // Load a mesh in OFF format
  94. Eigen::MatrixXd V;
  95. Eigen::MatrixXi F;
  96. igl::readOFF(TUTORIAL_SHARED_PATH "/bunny.off", V, F);
  97.  
  98. std::cerr << "Press 1 to render the scene and save it in a png." << std::endl;
  99. std::cerr << "Press 2 to load the saved png and use it as a texture." << std::endl;
  100. std::cerr << "Use WASD to move around and use Q and E to go up or down" << std::endl;
  101. // Plot the mesh and register the callback
  102. igl::opengl::glfw::Viewer viewer;
  103. viewer.callback_key_down = &key_down;
  104. viewer.data().set_mesh(V, F);
  105. viewer.launch();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement