606_AmbientOcclusion.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import sys, os
  2. import math
  3. # Add the igl library to the modules search path
  4. sys.path.insert(0, os.getcwd() + "/../")
  5. import pyigl as igl
  6. from shared import TUTORIAL_SHARED_PATH, check_dependencies
  7. dependencies = ["embree", "viewer"]
  8. check_dependencies(dependencies)
  9. # Mesh + AO values + Normals
  10. V = igl.eigen.MatrixXd()
  11. F = igl.eigen.MatrixXi()
  12. AO = igl.eigen.MatrixXd()
  13. N = igl.eigen.MatrixXd()
  14. viewer = igl.viewer.Viewer()
  15. def key_down(viewer, key, modifier):
  16. color = igl.eigen.MatrixXd([[0.9, 0.85, 0.9]])
  17. if key == ord('1'):
  18. # Show the mesh without the ambient occlusion factor
  19. viewer.data.set_colors(color)
  20. elif key == ord('2'):
  21. # Show the mesh with the ambient occlusion factor
  22. C = color.replicate(V.rows(), 1)
  23. for i in range(C.rows()):
  24. C.setRow(i, C.row(i) * AO[i, 0])
  25. viewer.data.set_colors(C)
  26. elif key == ord('.'):
  27. viewer.core.lighting_factor += 0.1
  28. elif key == ord(','):
  29. viewer.core.lighting_factor -= 0.1
  30. else:
  31. return False
  32. viewer.core.lighting_factor = min(max(viewer.core.lighting_factor, 0.0), 1.0)
  33. return True
  34. 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")
  35. # Load a surface mesh
  36. igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
  37. # Calculate vertex normals
  38. igl.per_vertex_normals(V, F, N)
  39. # Compute ambient occlusion factor using embree
  40. igl.embree.ambient_occlusion(V, F, V, N, 500, AO)
  41. AO = 1.0 - AO
  42. # Plot the generated mesh
  43. viewer.data.set_mesh(V, F)
  44. key_down(viewer, ord('2'), 0)
  45. viewer.callback_key_down = key_down
  46. viewer.core.show_lines = False
  47. viewer.core.lighting_factor = 0.0
  48. viewer.launch()