607_ScreenCapture.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. def key_down(viewer, key, modifier):
  9. if key == ord('1'):
  10. # Allocate temporary buffers
  11. R = igl.eigen.MatrixXuc(1280, 800)
  12. G = igl.eigen.MatrixXuc(1280, 800)
  13. B = igl.eigen.MatrixXuc(1280, 800)
  14. A = igl.eigen.MatrixXuc(1280, 800)
  15. # Draw the scene in the buffers
  16. viewer.core.draw_buffer(viewer.data, viewer.opengl, False, R, G, B, A)
  17. # Save it to a PNG
  18. igl.png.writePNG(R, G, B, A, "out.png")
  19. elif key == ord('2'):
  20. # Allocate temporary buffers
  21. R = igl.eigen.MatrixXuc()
  22. G = igl.eigen.MatrixXuc()
  23. B = igl.eigen.MatrixXuc()
  24. A = igl.eigen.MatrixXuc()
  25. # Read the PNG
  26. igl.png.readPNG("out.png", R, G, B, A)
  27. # Replace the mesh with a triangulated square
  28. V = igl.eigen.MatrixXd([[-0.5, -0.5, 0],
  29. [0.5, -0.5, 0],
  30. [0.5, 0.5, 0],
  31. [-0.5, 0.5, 0]])
  32. F = igl.eigen.MatrixXi([[0, 1, 2], [2, 3, 0]])
  33. UV = igl.eigen.MatrixXd([[0, 0], [1, 0], [1, 1], [0, 1]])
  34. viewer.data.clear()
  35. viewer.data.set_mesh(V, F)
  36. viewer.data.set_uv(UV)
  37. viewer.core.align_camera_center(V)
  38. viewer.core.show_texture = True
  39. # Use the image as a texture
  40. viewer.data.set_texture(R, G, B)
  41. else:
  42. return False
  43. return True
  44. if __name__ == "__main__":
  45. V = igl.eigen.MatrixXd()
  46. F = igl.eigen.MatrixXi()
  47. # Load meshes in OFF format
  48. igl.readOFF(TUTORIAL_SHARED_PATH + "bunny.off", V, F)
  49. viewer = igl.viewer.Viewer()
  50. print(
  51. "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.")
  52. viewer.callback_key_down = key_down
  53. viewer.data.set_mesh(V, F)
  54. viewer.launch()
  55. os.remove("out.png")