606_AmbientOcclusion.py 2.1 KB

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