606_AmbientOcclusion.py 1.6 KB

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