2 * Copyright 2011, Blender Foundation.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <OpenImageIO/fmath.h>
20 #include <OSL/genclosure.h>
21 #include "osl_closures.h"
28 class OrenNayarClosure: public BSDFClosure {
34 OrenNayarClosure(): BSDFClosure(Labels::DIFFUSE) {}
37 m_sigma = clamp(m_sigma, 0.0f, 1.0f);
39 float div = 1.0f / (M_PI + ((3.0f * M_PI - 4.0f) / 6.0f) * m_sigma);
45 bool mergeable(const ClosurePrimitive* other) const {
46 const OrenNayarClosure* comp = static_cast<const OrenNayarClosure*>(other);
49 m_sigma == comp->m_sigma &&
50 BSDFClosure::mergeable(other);
53 size_t memsize() const {
57 const char* name() const {
61 void print_on(std::ostream& out) const {
62 out << name() << " (";
63 out << "(" << m_N[0] << ", " << m_N[1] << ", " << m_N[2] << "), ";
68 float albedo(const Vec3& omega_out) const {
72 Color3 eval_reflect(const Vec3& omega_out, const Vec3& omega_in, float& pdf) const {
73 if (m_N.dot(omega_in) > 0.0f) {
74 pdf = float(0.5 * M_1_PI);
75 float is = get_intensity(m_N, omega_out, omega_in);
76 return Color3(is, is, is);
80 return Color3(0.0f, 0.0f, 0.0f);
84 Color3 eval_transmit(const Vec3& omega_out, const Vec3& omega_in, float& pdf) const {
85 return Color3(0.0f, 0.0f, 0.0f);
90 const Vec3& omega_out, const Vec3& domega_out_dx, const Vec3& domega_out_dy,
91 float randu, float randv,
92 Vec3& omega_in, Vec3& domega_in_dx, Vec3& domega_in_dy,
93 float& pdf, Color3& eval
95 sample_uniform_hemisphere (m_N, omega_out, randu, randv, omega_in, pdf);
97 if (Ng.dot(omega_in) > 0.0f) {
98 float is = get_intensity(m_N, omega_out, omega_in);
99 eval.setValue(is, is, is);
101 // TODO: find a better approximation for the bounce
102 domega_in_dx = (2.0f * m_N.dot(domega_out_dx)) * m_N - domega_out_dx;
103 domega_in_dy = (2.0f * m_N.dot(domega_out_dy)) * m_N - domega_out_dy;
104 domega_in_dx *= 125.0f;
105 domega_in_dy *= 125.0f;
111 return Labels::REFLECT;
115 float get_intensity(Vec3 const& n, Vec3 const& v, Vec3 const& l) const {
116 float nl = max(n.dot(l), 0.0f);
117 float nv = max(n.dot(v), 0.0f);
118 float t = l.dot(v) - nl * nv;
121 t /= max(nl, vl) + 1e-8f;
123 return nl * (m_a + m_b * t);
127 ClosureParam bsdf_oren_nayar_params[] = {
128 CLOSURE_VECTOR_PARAM (OrenNayarClosure, m_N),
129 CLOSURE_FLOAT_PARAM (OrenNayarClosure, m_sigma),
130 CLOSURE_STRING_KEYPARAM ("label"),
131 CLOSURE_FINISH_PARAM (OrenNayarClosure)
134 CLOSURE_PREPARE(bsdf_oren_nayar_prepare, OrenNayarClosure)