607_ScreenCapture.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import sys, os
  2. # Add the igl library to the modules search path
  3. sys.path.insert(0, os.getcwd() + "/../")
  4. import pyigl as igl
  5. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  6. dependencies = ["png", "viewer"]
  7. check_dependencies(dependencies)
  8. temp_png = os.path.join(os.getcwd(),"out.png")
  9. def key_down(viewer, key, modifier):
  10. if key == ord('1'):
  11. # Allocate temporary buffers
  12. R = igl.eigen.MatrixXuc(1280, 800)
  13. G = igl.eigen.MatrixXuc(1280, 800)
  14. B = igl.eigen.MatrixXuc(1280, 800)
  15. A = igl.eigen.MatrixXuc(1280, 800)
  16. # Draw the scene in the buffers
  17. viewer.core.draw_buffer(viewer.data, viewer.opengl, False, R, G, B, A)
  18. # Save it to a PNG
  19. igl.png.writePNG(R, G, B, A, temp_png)
  20. elif key == ord('2'):
  21. # Allocate temporary buffers
  22. R = igl.eigen.MatrixXuc()
  23. G = igl.eigen.MatrixXuc()
  24. B = igl.eigen.MatrixXuc()
  25. A = igl.eigen.MatrixXuc()
  26. # Read the PNG
  27. igl.png.readPNG(temp_png, R, G, B, A)
  28. # Replace the mesh with a triangulated square
  29. V = igl.eigen.MatrixXd([[-0.5, -0.5, 0],
  30. [0.5, -0.5, 0],
  31. [0.5, 0.5, 0],
  32. [-0.5, 0.5, 0]])
  33. F = igl.eigen.MatrixXi([[0, 1, 2], [2, 3, 0]])
  34. UV = igl.eigen.MatrixXd([[0, 0], [1, 0], [1, 1], [0, 1]])
  35. viewer.data.clear()
  36. viewer.data.set_mesh(V, F)
  37. viewer.data.set_uv(UV)
  38. viewer.core.align_camera_center(V)
  39. viewer.core.show_texture = True
  40. # Use the image as a texture
  41. viewer.data.set_texture(R, G, B)
  42. else:
  43. return False
  44. return True
  45. if __name__ == "__main__":
  46. V = igl.eigen.MatrixXd()
  47. F = igl.eigen.MatrixXi()
  48. # Load meshes in OFF format
  49. igl.readOFF(TUTORIAL_SHARED_PATH + "bunny.off", V, F)
  50. viewer = igl.viewer.Viewer()
  51. print(
  52. "Usage: Press 1 to render the scene and save it in a png. \nPress 2 to load the saved png and use it as a texture.")
  53. viewer.callback_key_down = key_down
  54. viewer.data.set_mesh(V, F)
  55. viewer.launch()
  56. os.remove(temp_png)