606_AmbientOcclusion.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 = ["embree", "glfw"]
  14. check_dependencies(dependencies)
  15. # Mesh + AO values + Normals
  16. V = igl.eigen.MatrixXd()
  17. F = igl.eigen.MatrixXi()
  18. AO = igl.eigen.MatrixXd()
  19. N = igl.eigen.MatrixXd()
  20. viewer = igl.glfw.Viewer()
  21. def key_down(viewer, key, modifier):
  22. color = igl.eigen.MatrixXd([[0.9, 0.85, 0.9]])
  23. if key == ord('1'):
  24. # Show the mesh without the ambient occlusion factor
  25. viewer.data().set_colors(color)
  26. elif key == ord('2'):
  27. # Show the mesh with the ambient occlusion factor
  28. C = color.replicate(V.rows(), 1)
  29. for i in range(C.rows()):
  30. C.setRow(i, C.row(i) * AO[i, 0])
  31. viewer.data().set_colors(C)
  32. elif key == ord('.'):
  33. viewer.core.lighting_factor += 0.1
  34. elif key == ord(','):
  35. viewer.core.lighting_factor -= 0.1
  36. else:
  37. return False
  38. viewer.core.lighting_factor = min(max(viewer.core.lighting_factor, 0.0), 1.0)
  39. return True
  40. print("Press 1 to turn off Ambient Occlusion\nPress 2 to turn on Ambient Occlusion\nPress . to turn up lighting\nPress , to turn down lighting")
  41. # Load a surface mesh
  42. igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
  43. # Calculate vertex normals
  44. igl.per_vertex_normals(V, F, N)
  45. # Compute ambient occlusion factor using embree
  46. igl.embree.ambient_occlusion(V, F, V, N, 500, AO)
  47. AO = 1.0 - AO
  48. # Plot the generated mesh
  49. viewer.data().set_mesh(V, F)
  50. key_down(viewer, ord('2'), 0)
  51. viewer.callback_key_down = key_down
  52. viewer.data().show_lines = False
  53. viewer.core.lighting_factor = 0.0
  54. viewer.launch()