607_ScreenCapture.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # This file is part of libigl, a simple c++ geometry processing library.
  2. #
  3. # Copyright (C) 2017 Sebastian Koch <s.koch@tu-berlin.de> and Daniele Panozzo <daniele.panozzo@gmail.com>
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public License
  6. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. # obtain one at http://mozilla.org/MPL/2.0/.
  8. import sys, os
  9. # Add the igl library to the modules search path
  10. sys.path.insert(0, os.getcwd() + "/../")
  11. import pyigl as igl
  12. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  13. dependencies = ["png", "viewer"]
  14. check_dependencies(dependencies)
  15. temp_png = os.path.join(os.getcwd(),"out.png")
  16. def key_down(viewer, key, modifier):
  17. if key == ord('1'):
  18. # Allocate temporary buffers
  19. R = igl.eigen.MatrixXuc(1280, 800)
  20. G = igl.eigen.MatrixXuc(1280, 800)
  21. B = igl.eigen.MatrixXuc(1280, 800)
  22. A = igl.eigen.MatrixXuc(1280, 800)
  23. # Draw the scene in the buffers
  24. viewer.core.draw_buffer(viewer.data, viewer.opengl, False, R, G, B, A)
  25. # Save it to a PNG
  26. igl.png.writePNG(R, G, B, A, temp_png)
  27. elif key == ord('2'):
  28. # Allocate temporary buffers
  29. R = igl.eigen.MatrixXuc()
  30. G = igl.eigen.MatrixXuc()
  31. B = igl.eigen.MatrixXuc()
  32. A = igl.eigen.MatrixXuc()
  33. # Read the PNG
  34. igl.png.readPNG(temp_png, R, G, B, A)
  35. # Replace the mesh with a triangulated square
  36. V = igl.eigen.MatrixXd([[-0.5, -0.5, 0],
  37. [0.5, -0.5, 0],
  38. [0.5, 0.5, 0],
  39. [-0.5, 0.5, 0]])
  40. F = igl.eigen.MatrixXi([[0, 1, 2], [2, 3, 0]])
  41. UV = igl.eigen.MatrixXd([[0, 0], [1, 0], [1, 1], [0, 1]])
  42. viewer.data.clear()
  43. viewer.data.set_mesh(V, F)
  44. viewer.data.set_uv(UV)
  45. viewer.core.align_camera_center(V)
  46. viewer.core.show_texture = True
  47. # Use the image as a texture
  48. viewer.data.set_texture(R, G, B)
  49. else:
  50. return False
  51. return True
  52. if __name__ == "__main__":
  53. V = igl.eigen.MatrixXd()
  54. F = igl.eigen.MatrixXi()
  55. # Load meshes in OFF format
  56. igl.readOFF(TUTORIAL_SHARED_PATH + "bunny.off", V, F)
  57. viewer = igl.viewer.Viewer()
  58. print(
  59. "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.")
  60. viewer.callback_key_down = key_down
  61. viewer.data.set_mesh(V, F)
  62. viewer.launch()
  63. os.remove(temp_png)