Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bsdf_oren_nayar.cpp « osl « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a42c81e78f3c036e81fadc778a5051350b338d9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright 2011, Blender Foundation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/*
 *	An implementation of Oren-Nayar reflectance model, public domain
 *		http://www1.cs.columbia.edu/CAVE/publications/pdfs/Oren_SIGGRAPH94.pdf
 *
 *	NOTE:
 *		BSDF = A + B * cos() * sin() * tan()
 *
 *		The parameter sigma means different from original.
 *		A and B are calculated by the following formula:
 *			0 <= sigma <= 1
 *			A =     1 / ((1 + sigma / 2) * pi);
 *			B = sigma / ((1 + sigma / 2) * pi);
 *
 *		This formula is derived as following:
 *
 *		0. Normalize A-term and B-term of BSDF *individually*.
 *		   B-term is normalized at maximum point: dot(L, N) = 0.
 *			A = (1/pi) * A'
 *			B = (2/pi) * B'
 *
 *		1. Solve the following equation:
 *			A' + B' = 1
 *			B / A = sigma
 */

#include <OpenImageIO/fmath.h>
#include <OSL/genclosure.h>
#include "osl_closures.h"

CCL_NAMESPACE_BEGIN

using namespace OSL;


class OrenNayarClosure: public BSDFClosure {
public:
	Vec3 m_N;
	float m_sigma;
	float m_a, m_b;

	OrenNayarClosure(): BSDFClosure(Labels::DIFFUSE) {}

	void setup() {
		m_sigma = clamp(m_sigma, 0.0f, 1.0f);
		m_a =    1.0f / ((1.0f + 0.5f * m_sigma) * M_PI);
		m_b = m_sigma / ((1.0f + 0.5f * m_sigma) * M_PI);
	}

	bool mergeable(const ClosurePrimitive* other) const {
		const OrenNayarClosure* comp = static_cast<const OrenNayarClosure*>(other);
		return
			m_N == comp->m_N &&
			m_sigma == comp->m_sigma &&
			BSDFClosure::mergeable(other);
	}

	size_t memsize() const {
		return sizeof(*this);
	}

	const char* name() const {
		return "oren_nayar";
	}

	void print_on(std::ostream& out) const {
		out << name() << " (";
		out << "(" << m_N[0] << ", " << m_N[1] << ", " << m_N[2] << "), ";
		out << m_sigma;
		out << ")";
	}

	float albedo(const Vec3& omega_out) const {
		return 1.0f;
	}

	Color3 eval_reflect(const Vec3& omega_out, const Vec3& omega_in, float& pdf) const {
		if (m_N.dot(omega_in) > 0.0f) {
			pdf = float(0.5 * M_1_PI);
			float is = get_intensity(m_N, omega_out, omega_in);
			return Color3(is, is, is);
		}
		else {
			pdf = 0.0f;
			return Color3(0.0f, 0.0f, 0.0f);
		}
	}

	Color3 eval_transmit(const Vec3& omega_out, const Vec3& omega_in, float& pdf) const {
		return Color3(0.0f, 0.0f, 0.0f);
	}

	ustring sample(
		const Vec3& Ng,
		const Vec3& omega_out, const Vec3& domega_out_dx, const Vec3& domega_out_dy,
		float randu, float randv,
		Vec3& omega_in, Vec3& domega_in_dx, Vec3& domega_in_dy,
		float& pdf, Color3& eval
	) const {
		sample_uniform_hemisphere (m_N, omega_out, randu, randv, omega_in, pdf);

		if (Ng.dot(omega_in) > 0.0f) {
			float is = get_intensity(m_N, omega_out, omega_in);
			eval.setValue(is, is, is);

			// TODO: find a better approximation for the bounce
			domega_in_dx = (2.0f * m_N.dot(domega_out_dx)) * m_N - domega_out_dx;
			domega_in_dy = (2.0f * m_N.dot(domega_out_dy)) * m_N - domega_out_dy;
			domega_in_dx *= 125.0f;
			domega_in_dy *= 125.0f;
		}
		else {
			pdf = 0.0f;
		}

		return Labels::REFLECT;
	}

private:
	float get_intensity(Vec3 const& n, Vec3 const& v, Vec3 const& l) const {
		float nl = max(n.dot(l), 0.0f);
		float nv = max(n.dot(v), 0.0f);

		Vec3 al = l - nl * n;
		al.normalize();
		Vec3 av = v - nv * n;
		av.normalize();
		float t = max(al.dot(av), 0.0f);

		float cos_a, cos_b;
		if (nl < nv) {
			cos_a = nl;
			cos_b = nv;
		}
		else {
			cos_a = nv;
			cos_b = nl;
		}

		float sin_a = sqrtf(1.0f - cos_a * cos_a);
		float tan_b = sqrtf(1.0f - cos_b * cos_b) / (cos_b + FLT_MIN);

		return nl * (m_a + m_b * t * sin_a * tan_b);
	}
};

ClosureParam bsdf_oren_nayar_params[] = {
	CLOSURE_VECTOR_PARAM	(OrenNayarClosure, m_N),
	CLOSURE_FLOAT_PARAM		(OrenNayarClosure, m_sigma),
	CLOSURE_STRING_KEYPARAM ("label"),
	CLOSURE_FINISH_PARAM	(OrenNayarClosure)
};

CLOSURE_PREPARE(bsdf_oren_nayar_prepare, OrenNayarClosure)


CCL_NAMESPACE_END