607_ScreenCapture.py 2.5 KB

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