606_AmbientOcclusion.py 1.7 KB

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