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
22 /* HENYEY-GREENSTEIN CLOSURE */
24 /* Given cosine between rays, return probability density that a photon bounces
25 * to that direction. The g parameter controls how different it is from the
26 * uniform sphere. g=0 uniform diffuse-like, g=1 close to sharp single ray. */
27 ccl_device float single_peaked_henyey_greenstein(float cos_theta, float g)
30 return M_1_PI_F * 0.25f;
32 return ((1.0f - g * g) / safe_powf(1.0f + g * g - 2.0f * g * cos_theta, 1.5f)) * (M_1_PI_F * 0.25f);
35 ccl_device int volume_henyey_greenstein_setup(ShaderClosure *sc)
37 sc->type = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
39 /* clamp anisotropy to avoid delta function */
40 sc->data0 = signf(sc->data0) * min(fabsf(sc->data0), 1.0f - 1e-3f);
42 return SD_BSDF|SD_BSDF_HAS_EVAL;
45 ccl_device float3 volume_henyey_greenstein_eval_phase(const ShaderClosure *sc, const float3 I, float3 omega_in, float *pdf)
49 /* note that I points towards the viewer */
50 float cos_theta = dot(-I, omega_in);
52 *pdf = single_peaked_henyey_greenstein(cos_theta, g);
54 return make_float3(*pdf, *pdf, *pdf);
57 ccl_device int volume_henyey_greenstein_sample(const ShaderClosure *sc, float3 I, float3 dIdx, float3 dIdy, float randu, float randv,
58 float3 *eval, float3 *omega_in, float3 *domega_in_dx, float3 *domega_in_dy, float *pdf)
61 float cos_phi, sin_phi, cos_theta;
63 /* match pdf for small g */
64 if(fabsf(g) < 1e-3f) {
65 cos_theta = (1.0f - 2.0f * randu);
68 float k = (1.0f - g * g) / (1.0f - g + 2.0f * g * randu);
69 cos_theta = (1.0f + g * g - k * k) / (2.0f * g);
72 float sin_theta = safe_sqrtf(1.0f - cos_theta * cos_theta);
74 float phi = M_2PI_F * randv;
78 /* note that I points towards the viewer and so is used negated */
80 make_orthonormals(-I, &T, &B);
81 *omega_in = sin_theta * cos_phi * T + sin_theta * sin_phi * B + cos_theta * (-I);
83 *pdf = single_peaked_henyey_greenstein(cos_theta, g);
84 *eval = make_float3(*pdf, *pdf, *pdf); /* perfect importance sampling */
86 #ifdef __RAY_DIFFERENTIALS__
87 /* todo: implement ray differential estimation */
88 *domega_in_dx = make_float3(0.0f, 0.0f, 0.0f);
89 *domega_in_dy = make_float3(0.0f, 0.0f, 0.0f);
92 return LABEL_VOLUME_SCATTER;
95 /* ABSORPTION VOLUME CLOSURE */
97 ccl_device int volume_absorption_setup(ShaderClosure *sc)
99 sc->type = CLOSURE_VOLUME_ABSORPTION_ID;
106 ccl_device float3 volume_eval_phase(const ShaderClosure *sc, const float3 I, float3 omega_in, float *pdf)
111 case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID:
112 eval = volume_henyey_greenstein_eval_phase(sc, I, omega_in, pdf);
115 eval = make_float3(0.0f, 0.0f, 0.0f);
122 ccl_device int volume_sample(const ShaderData *sd, const ShaderClosure *sc, float randu,
123 float randv, float3 *eval, float3 *omega_in, differential3 *domega_in, float *pdf)
128 case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID:
129 label = volume_henyey_greenstein_sample(sc, sd->I, sd->dI.dx, sd->dI.dy, randu, randv, eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
132 *eval = make_float3(0.0f, 0.0f, 0.0f);