2 * Adapted from Open Shading Language with this license:
4 * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
7 * Modifications Copyright 2011, Blender Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of Sony Pictures Imageworks nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <OpenImageIO/fmath.h>
35 #include <OSL/genclosure.h>
37 #include "osl_closures.h"
39 #include "util_math.h"
45 // anisotropic ward - leaks energy at grazing angles
46 // see http://www.graphics.cornell.edu/~bjw/wardnotes.pdf
47 class WardClosure : public BSDFClosure {
52 WardClosure() : BSDFClosure(Labels::GLOSSY) {}
56 m_ax = clamp(m_ax, 1e-5f, 1.0f);
57 m_ay = clamp(m_ay, 1e-5f, 1.0f);
60 bool mergeable(const ClosurePrimitive *other) const {
61 const WardClosure *comp = (const WardClosure *)other;
62 return m_N == comp->m_N && m_T == comp->m_T &&
63 m_ax == comp->m_ax && m_ay == comp->m_ay &&
64 BSDFClosure::mergeable(other);
67 size_t memsize() const { return sizeof(*this); }
69 const char *name() const { return "ward"; }
71 void print_on(std::ostream &out) const {
72 out << name() << " ((";
73 out << m_N[0] << ", " << m_N[1] << ", " << m_N[2] << "), (";
74 out << m_T[0] << ", " << m_T[1] << ", " << m_T[2] << "), ";
75 out << m_ax << ", " << m_ay << ")";
78 float albedo(const Vec3 &omega_out) const
83 Color3 eval_reflect(const Vec3 &omega_out, const Vec3 &omega_in, float& pdf) const
85 float cosNO = m_N.dot(omega_out);
86 float cosNI = m_N.dot(omega_in);
87 if (cosNI > 0 && cosNO > 0) {
88 // get half vector and get x,y basis on the surface for anisotropy
89 Vec3 H = omega_in + omega_out;
90 H.normalize(); // normalize needed for pdf
92 make_orthonormals(m_N, m_T, X, Y);
94 float dotx = H.dot(X) / m_ax;
95 float doty = H.dot(Y) / m_ay;
96 float dotn = H.dot(m_N);
97 float exp_arg = (dotx * dotx + doty * doty) / (dotn * dotn);
98 float denom = (4 * (float) M_PI * m_ax * m_ay * sqrtf(cosNO * cosNI));
99 float exp_val = expf(-exp_arg);
100 float out = cosNI * exp_val / denom;
101 float oh = H.dot(omega_out);
102 denom = 4 * (float) M_PI * m_ax * m_ay * oh * dotn * dotn * dotn;
103 pdf = exp_val / denom;
104 return Color3(out, out, out);
106 return Color3(0, 0, 0);
109 Color3 eval_transmit(const Vec3 &omega_out, const Vec3 &omega_in, float& pdf) const
111 return Color3(0, 0, 0);
114 ustring sample(const Vec3 &Ng,
115 const Vec3 &omega_out, const Vec3 &domega_out_dx, const Vec3 &domega_out_dy,
116 float randu, float randv,
117 Vec3 &omega_in, Vec3 &domega_in_dx, Vec3 &domega_in_dy,
118 float &pdf, Color3 &eval) const
120 float cosNO = m_N.dot(omega_out);
122 // get x,y basis on the surface for anisotropy
124 make_orthonormals(m_N, m_T, X, Y);
125 // generate random angles for the half vector
126 // eq. 7 (taking care around discontinuities to keep
127 // output angle in the right quadrant)
128 // we take advantage of cos(atan(x)) == 1/sqrt(1+x^2)
129 // and sin(atan(x)) == x/sqrt(1+x^2)
130 float alphaRatio = m_ay / m_ax;
131 float cosPhi, sinPhi;
133 float val = 4 * randu;
134 float tanPhi = alphaRatio * tanf((float) M_PI_2 * val);
135 cosPhi = 1 / sqrtf(1 + tanPhi * tanPhi);
136 sinPhi = tanPhi * cosPhi;
138 else if (randu < 0.5) {
139 float val = 1 - 4 * (0.5f - randu);
140 float tanPhi = alphaRatio * tanf((float) M_PI_2 * val);
141 // phi = (float) M_PI - phi;
142 cosPhi = -1 / sqrtf(1 + tanPhi * tanPhi);
143 sinPhi = -tanPhi * cosPhi;
145 else if (randu < 0.75f) {
146 float val = 4 * (randu - 0.5f);
147 float tanPhi = alphaRatio * tanf((float) M_PI_2 * val);
148 //phi = (float) M_PI + phi;
149 cosPhi = -1 / sqrtf(1 + tanPhi * tanPhi);
150 sinPhi = tanPhi * cosPhi;
153 float val = 1 - 4 * (1 - randu);
154 float tanPhi = alphaRatio * tanf((float) M_PI_2 * val);
155 // phi = 2 * (float) M_PI - phi;
156 cosPhi = 1 / sqrtf(1 + tanPhi * tanPhi);
157 sinPhi = -tanPhi * cosPhi;
160 // we take advantage of cos(atan(x)) == 1/sqrt(1+x^2)
161 // and sin(atan(x)) == x/sqrt(1+x^2)
162 float thetaDenom = (cosPhi * cosPhi) / (m_ax * m_ax) + (sinPhi * sinPhi) / (m_ay * m_ay);
163 float tanTheta2 = -logf(1 - randv) / thetaDenom;
164 float cosTheta = 1 / sqrtf(1 + tanTheta2);
165 float sinTheta = cosTheta * sqrtf(tanTheta2);
167 Vec3 h; // already normalized becaused expressed from spherical coordinates
168 h.x = sinTheta * cosPhi;
169 h.y = sinTheta * sinPhi;
171 // compute terms that are easier in local space
172 float dotx = h.x / m_ax;
173 float doty = h.y / m_ay;
175 // transform to world space
176 h = h.x * X + h.y * Y + h.z * m_N;
177 // generate the final sample
178 float oh = h.dot(omega_out);
179 omega_in.x = 2 * oh * h.x - omega_out.x;
180 omega_in.y = 2 * oh * h.y - omega_out.y;
181 omega_in.z = 2 * oh * h.z - omega_out.z;
182 if (Ng.dot(omega_in) > 0) {
183 float cosNI = m_N.dot(omega_in);
186 float exp_arg = (dotx * dotx + doty * doty) / (dotn * dotn);
187 float denom = 4 * (float) M_PI * m_ax * m_ay * oh * dotn * dotn * dotn;
188 pdf = expf(-exp_arg) / denom;
189 // compiler will reuse expressions already computed
190 denom = (4 * (float) M_PI * m_ax * m_ay * sqrtf(cosNO * cosNI));
191 float power = cosNI * expf(-exp_arg) / denom;
192 eval.setValue(power, power, power);
193 domega_in_dx = (2 * m_N.dot(domega_out_dx)) * m_N - domega_out_dx;
194 domega_in_dy = (2 * m_N.dot(domega_out_dy)) * m_N - domega_out_dy;
196 /* disabled for now - gives texture filtering problems */
198 // Since there is some blur to this reflection, make the
199 // derivatives a bit bigger. In theory this varies with the
200 // roughness but the exact relationship is complex and
201 // requires more ops than are practical.
208 return Labels::REFLECT;
214 ClosureParam bsdf_ward_params[] = {
215 CLOSURE_VECTOR_PARAM(WardClosure, m_N),
216 CLOSURE_VECTOR_PARAM(WardClosure, m_T),
217 CLOSURE_FLOAT_PARAM(WardClosure, m_ax),
218 CLOSURE_FLOAT_PARAM(WardClosure, m_ay),
219 CLOSURE_STRING_KEYPARAM("label"),
220 CLOSURE_FINISH_PARAM(WardClosure)
223 CLOSURE_PREPARE(bsdf_ward_prepare, WardClosure)