ibl.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import Image as img
  2. import numpy as np
  3. import pylab as pl
  4. n = 3
  5. GridI, GridJ = np.meshgrid(np.linspace(-1, 1, n), np.linspace(-1, 1, n))
  6. img = np.ones((n,n))
  7. # constant factor of Ylm
  8. c00 = 0.282095
  9. c1minus1 = 0.488603
  10. c10 = 0.488603
  11. c11 = 0.488603
  12. c2minus2 = 1.092548
  13. c2minus1 = 1.092548
  14. c21 = 1.092548
  15. c20 = 0.315392
  16. c22 = 0.546274
  17. def computeYmlOnGrid(Xgrid, Ygrid, Zgrid):
  18. "compute Yml from Y00 to Y22 on Xgrid/Ygrid/Zgrid"
  19. norm = np.sqrt(Xgrid * Xgrid + Ygrid * Ygrid + Zgrid * Zgrid)
  20. Xg = Xgrid / norm
  21. Yg = Ygrid / norm
  22. Zg = Zgrid / norm
  23. Y00 = c00
  24. Y1minus1 = c1minus1 * Yg
  25. Y10 = c10 * Zg
  26. Y11 = c11 * Xg
  27. Y2minus2 = c2minus2 * Xg * Yg
  28. Y2minus1 = c2minus1 * Yg * Zg
  29. Y21= c21 * Xg * Zg
  30. Y20 = c20 * (3 * Zg * Zg - 1)
  31. Y22 = c22 * (Xg * Xg - Yg * Yg)
  32. return (Y00, Y1minus1, Y10, Y11, Y2minus2, Y2minus1, Y20, Y21, Y22)
  33. # From http://www.rorydriscoll.com/2012/01/15/cubemap-texel-solid-angle/
  34. def areaToPoint(x, y):
  35. return np.arctan2(x * y, np.sqrt(x * x + y * y + 1))
  36. def getSolidAngleGrid(Xgrid, Ygrid):
  37. "Compute solid angles using Xgrid/Ygrid/Zgrid texel position"
  38. (step, _) = np.shape(Xgrid)
  39. x0 = Xgrid - (1. / step)
  40. x1 = Xgrid + (1. / step)
  41. y0 = Ygrid - (1. / step)
  42. y1 = Ygrid + (1. / step)
  43. return areaToPoint(x0,y0) - areaToPoint(x1, y0) - areaToPoint(x0, y1) + areaToPoint(x1, y1)
  44. def computeCoefficients():
  45. "Compute coefficient SH00 to SH22 for monochromatic img"
  46. FaceGrid = [(np.ones((n,n)), -GridI, -GridJ), #GL_TEXTURE_CUBE_MAP_POSITIVE_X
  47. (-1 * np.ones((n,n)), -GridI, GridJ), #GL_TEXTURE_CUBE_MAP_NEGATIVE_X
  48. (GridJ, np.ones((n,n)), GridI), #GL_TEXTURE_CUBE_MAP_POSITIVE_Y
  49. (GridJ, -1 * np.ones((n,n)), -GridI), #GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
  50. (GridJ, GridI, np.ones((n,n))), #GL_TEXTURE_CUBE_MAP_POSITIVE_Z
  51. (GridJ, -GridI, -1 * np.ones((n,n)))] #GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
  52. incomingIrradiance = getSolidAngleGrid(GridI, GridJ)
  53. SH00 = 0
  54. SH1minus1 = 0
  55. SH10 = 0
  56. SH11 = 0
  57. SH2minus2 = 0
  58. SH2minus1 = 0
  59. SH20 = 0
  60. SH21 = 0
  61. SH22 = 0
  62. for (Xgrid, Ygrid, Zgrid) in FaceGrid:
  63. (Y00, Y1minus1, Y10, Y11, Y2minus2, Y2minus1, Y20, Y21, Y22) = computeYmlOnGrid(Xgrid, Ygrid, Zgrid)
  64. SH00 += np.sum(Y00 * incomingIrradiance * img)
  65. SH1minus1 += np.sum(Y1minus1 * incomingIrradiance * img)
  66. SH10 += np.sum(Y10 * incomingIrradiance * img)
  67. SH11 += np.sum(Y11 * incomingIrradiance * img)
  68. SH2minus2 += np.sum(Y2minus2 * incomingIrradiance * img)
  69. SH2minus1 += np.sum(Y2minus1 * incomingIrradiance * img)
  70. SH20 += np.sum(Y20 * incomingIrradiance * img)
  71. SH21 += np.sum(Y21 * incomingIrradiance * img)
  72. SH22 += np.sum(Y22 * incomingIrradiance * img)
  73. return (SH00, SH1minus1, SH10, SH2minus2, SH2minus1, SH20, SH21, SH22)
  74. print(computeCoefficients())
  75. #res = []
  76. #for (Xd, Yd, Zd) in FaceGrid:
  77. # res.append(computeYmlOnGrid(Xd, Yd, Zd))
  78. #I = img.open("C:/Users/vljn_000/Documents/GitHub/stk-assets/textures/ants.png")
  79. #m = np.array(I)
  80. #print(type(m))
  81. #pl.imshow(m)