bsdf_oren_nayar.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __BSDF_OREN_NAYAR_H__
  17. #define __BSDF_OREN_NAYAR_H__
  18. CCL_NAMESPACE_BEGIN
  19. typedef ccl_addr_space struct OrenNayarBsdf {
  20. SHADER_CLOSURE_BASE;
  21. float roughness;
  22. float a;
  23. float b;
  24. } OrenNayarBsdf;
  25. static_assert(sizeof(ShaderClosure) >= sizeof(OrenNayarBsdf), "OrenNayarBsdf is too large!");
  26. ccl_device float3 bsdf_oren_nayar_get_intensity(const ShaderClosure *sc,
  27. float3 n,
  28. float3 v,
  29. float3 l)
  30. {
  31. const OrenNayarBsdf *bsdf = (const OrenNayarBsdf *)sc;
  32. float nl = max(dot(n, l), 0.0f);
  33. float nv = max(dot(n, v), 0.0f);
  34. float t = dot(l, v) - nl * nv;
  35. if (t > 0.0f)
  36. t /= max(nl, nv) + FLT_MIN;
  37. float is = nl * (bsdf->a + bsdf->b * t);
  38. return make_float3(is, is, is);
  39. }
  40. ccl_device int bsdf_oren_nayar_setup(OrenNayarBsdf *bsdf)
  41. {
  42. float sigma = bsdf->roughness;
  43. bsdf->type = CLOSURE_BSDF_OREN_NAYAR_ID;
  44. sigma = saturate(sigma);
  45. float div = 1.0f / (M_PI_F + ((3.0f * M_PI_F - 4.0f) / 6.0f) * sigma);
  46. bsdf->a = 1.0f * div;
  47. bsdf->b = sigma * div;
  48. return SD_BSDF | SD_BSDF_HAS_EVAL;
  49. }
  50. ccl_device bool bsdf_oren_nayar_merge(const ShaderClosure *a, const ShaderClosure *b)
  51. {
  52. const OrenNayarBsdf *bsdf_a = (const OrenNayarBsdf *)a;
  53. const OrenNayarBsdf *bsdf_b = (const OrenNayarBsdf *)b;
  54. return (isequal_float3(bsdf_a->N, bsdf_b->N)) && (bsdf_a->roughness == bsdf_b->roughness);
  55. }
  56. ccl_device float3 bsdf_oren_nayar_eval_reflect(const ShaderClosure *sc,
  57. const float3 I,
  58. const float3 omega_in,
  59. float *pdf)
  60. {
  61. const OrenNayarBsdf *bsdf = (const OrenNayarBsdf *)sc;
  62. if (dot(bsdf->N, omega_in) > 0.0f) {
  63. *pdf = 0.5f * M_1_PI_F;
  64. return bsdf_oren_nayar_get_intensity(sc, bsdf->N, I, omega_in);
  65. }
  66. else {
  67. *pdf = 0.0f;
  68. return make_float3(0.0f, 0.0f, 0.0f);
  69. }
  70. }
  71. ccl_device float3 bsdf_oren_nayar_eval_transmit(const ShaderClosure *sc,
  72. const float3 I,
  73. const float3 omega_in,
  74. float *pdf)
  75. {
  76. return make_float3(0.0f, 0.0f, 0.0f);
  77. }
  78. ccl_device int bsdf_oren_nayar_sample(const ShaderClosure *sc,
  79. float3 Ng,
  80. float3 I,
  81. float3 dIdx,
  82. float3 dIdy,
  83. float randu,
  84. float randv,
  85. float3 *eval,
  86. float3 *omega_in,
  87. float3 *domega_in_dx,
  88. float3 *domega_in_dy,
  89. float *pdf)
  90. {
  91. const OrenNayarBsdf *bsdf = (const OrenNayarBsdf *)sc;
  92. sample_uniform_hemisphere(bsdf->N, randu, randv, omega_in, pdf);
  93. if (dot(Ng, *omega_in) > 0.0f) {
  94. *eval = bsdf_oren_nayar_get_intensity(sc, bsdf->N, I, *omega_in);
  95. #ifdef __RAY_DIFFERENTIALS__
  96. // TODO: find a better approximation for the bounce
  97. *domega_in_dx = (2.0f * dot(bsdf->N, dIdx)) * bsdf->N - dIdx;
  98. *domega_in_dy = (2.0f * dot(bsdf->N, dIdy)) * bsdf->N - dIdy;
  99. #endif
  100. }
  101. else {
  102. *pdf = 0.0f;
  103. *eval = make_float3(0.0f, 0.0f, 0.0f);
  104. }
  105. return LABEL_REFLECT | LABEL_DIFFUSE;
  106. }
  107. CCL_NAMESPACE_END
  108. #endif /* __BSDF_OREN_NAYAR_H__ */