2 * Copyright 2011-2013 Blender Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __BSDF_OREN_NAYAR_H__
18 #define __BSDF_OREN_NAYAR_H__
22 typedef ccl_addr_space struct OrenNayarBsdf {
30 ccl_device float3 bsdf_oren_nayar_get_intensity(const ShaderClosure *sc, float3 n, float3 v, float3 l)
32 const OrenNayarBsdf *bsdf = (const OrenNayarBsdf*)sc;
33 float nl = max(dot(n, l), 0.0f);
34 float nv = max(dot(n, v), 0.0f);
35 float t = dot(l, v) - nl * nv;
38 t /= max(nl, nv) + FLT_MIN;
39 float is = nl * (bsdf->a + bsdf->b * t);
40 return make_float3(is, is, is);
43 ccl_device int bsdf_oren_nayar_setup(OrenNayarBsdf *bsdf)
45 float sigma = bsdf->roughness;
47 bsdf->type = CLOSURE_BSDF_OREN_NAYAR_ID;
49 sigma = saturate(sigma);
51 float div = 1.0f / (M_PI_F + ((3.0f * M_PI_F - 4.0f) / 6.0f) * sigma);
54 bsdf->b = sigma * div;
56 return SD_BSDF|SD_BSDF_HAS_EVAL;
59 ccl_device bool bsdf_oren_nayar_merge(const ShaderClosure *a, const ShaderClosure *b)
61 const OrenNayarBsdf *bsdf_a = (const OrenNayarBsdf*)a;
62 const OrenNayarBsdf *bsdf_b = (const OrenNayarBsdf*)b;
64 return (isequal_float3(bsdf_a->N, bsdf_b->N)) &&
65 (bsdf_a->roughness == bsdf_b->roughness);
68 ccl_device float3 bsdf_oren_nayar_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
70 const OrenNayarBsdf *bsdf = (const OrenNayarBsdf*)sc;
71 if(dot(bsdf->N, omega_in) > 0.0f) {
72 *pdf = 0.5f * M_1_PI_F;
73 return bsdf_oren_nayar_get_intensity(sc, bsdf->N, I, omega_in);
77 return make_float3(0.0f, 0.0f, 0.0f);
81 ccl_device float3 bsdf_oren_nayar_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
83 return make_float3(0.0f, 0.0f, 0.0f);
86 ccl_device int bsdf_oren_nayar_sample(const ShaderClosure *sc, float3 Ng, float3 I, float3 dIdx, float3 dIdy, float randu, float randv, float3 *eval, float3 *omega_in, float3 *domega_in_dx, float3 *domega_in_dy, float *pdf)
88 const OrenNayarBsdf *bsdf = (const OrenNayarBsdf*)sc;
89 sample_uniform_hemisphere(bsdf->N, randu, randv, omega_in, pdf);
91 if(dot(Ng, *omega_in) > 0.0f) {
92 *eval = bsdf_oren_nayar_get_intensity(sc, bsdf->N, I, *omega_in);
94 #ifdef __RAY_DIFFERENTIALS__
95 // TODO: find a better approximation for the bounce
96 *domega_in_dx = (2.0f * dot(bsdf->N, dIdx)) * bsdf->N - dIdx;
97 *domega_in_dy = (2.0f * dot(bsdf->N, dIdy)) * bsdf->N - dIdy;
102 *eval = make_float3(0.0f, 0.0f, 0.0f);
105 return LABEL_REFLECT|LABEL_DIFFUSE;
111 #endif /* __BSDF_OREN_NAYAR_H__ */