-
Notifications
You must be signed in to change notification settings - Fork 2
/
cooktorrance.cpp
280 lines (224 loc) · 8.37 KB
/
cooktorrance.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
This cooktorrance plugin implements the following BRDF equation:
fr(wi, wo) = kd / pi + ks * D(roughness, wi, wo) * G(wi, wo) * F(F0, wi, wo) / (pi * <N, wi> * <N, wo>).
And is from https://github.com/yongsen/mitsuba/blob/master/cooktorrance.cpp
*/
#include <mitsuba/render/bsdf.h>
#include <mitsuba/hw/basicshader.h>
#include <mitsuba/core/warp.h>
MTS_NAMESPACE_BEGIN
class CookTorrance : public BSDF {
public:
CookTorrance(const Properties &props)
: BSDF(props) {
m_diffuseReflectance = props.getSpectrum("diffuseReflectance", Spectrum(0.5f));
m_specularReflectance = props.getSpectrum("specularReflectance", Spectrum(0.2f));
m_roughness = props.getFloat("roughness", 0.1f);
m_F0 = props.getFloat("F0", 0.1f);
}
CookTorrance(Stream *stream, InstanceManager *manager)
: BSDF(stream, manager) {
m_diffuseReflectance = Spectrum(stream);
m_specularReflectance = Spectrum(stream);
m_roughness = stream->readFloat();
m_F0 = stream->readFloat();
configure();
}
void configure() {
m_components.clear();
m_components.push_back(EGlossyReflection | EFrontSide );
m_components.push_back(EDiffuseReflection | EFrontSide );
m_usesRayDifferentials = false;
Float dAvg = m_diffuseReflectance.getLuminance(),
sAvg = m_specularReflectance.getLuminance();
m_specularSamplingWeight = sAvg / (dAvg + sAvg);
BSDF::configure();
}
Spectrum eval(const BSDFSamplingRecord &bRec, EMeasure measure) const {
/* sanity check */
if(measure != ESolidAngle ||
Frame::cosTheta(bRec.wi) <= 0 ||
Frame::cosTheta(bRec.wo) <= 0)
return Spectrum(0.0f);
/* which components to eval */
bool hasSpecular = (bRec.typeMask & EGlossyReflection)
&& (bRec.component == -1 || bRec.component == 0);
bool hasDiffuse = (bRec.typeMask & EDiffuseReflection)
&& (bRec.component == -1 || bRec.component == 1);
/* eval spec */
Spectrum result(0.0f);
if (hasSpecular) {
Vector H = normalize(bRec.wo+bRec.wi);
if(Frame::cosTheta(H) > 0.0f)
{
// evaluate NDF
const Float roughness2 = m_roughness*m_roughness;
const Float cosTheta2 = Frame::cosTheta2(H);
const Float Hwi = dot(bRec.wi, H);
const Float Hwo = dot(bRec.wo, H);
const Float D = math::fastexp(-Frame::tanTheta2(H)/roughness2) / (roughness2 * cosTheta2*cosTheta2);
// compute shadowing and masking
const Float G = std::min(1.0f, std::min(
2.0f * Frame::cosTheta(H) * Frame::cosTheta(bRec.wi) / Hwi,
2.0f * Frame::cosTheta(H) * Frame::cosTheta(bRec.wo) / Hwo ));
// compute Fresnel
const Float F = fresnel(m_F0, Hwi);
// evaluate the microfacet model
result += m_specularReflectance * INV_PI * D * G * F / Frame::cosTheta(bRec.wi);
}
}
/* eval diffuse */
if (hasDiffuse)
result += m_diffuseReflectance * INV_PI * Frame::cosTheta(bRec.wo);
// Done.
return result;
}
Float pdf(const BSDFSamplingRecord &bRec, EMeasure measure) const {
if (measure != ESolidAngle ||
Frame::cosTheta(bRec.wi) <= 0 ||
Frame::cosTheta(bRec.wo) <= 0 ||
((bRec.component != -1 && bRec.component != 0) ||
!(bRec.typeMask & EGlossyReflection)))
return 0.0f;
bool hasSpecular = (bRec.typeMask & EGlossyReflection)
&& (bRec.component == -1 || bRec.component == 0);
bool hasDiffuse = (bRec.typeMask & EDiffuseReflection)
&& (bRec.component == -1 || bRec.component == 1);
Float diffuseProb = 0.0f, specProb = 0.0f;
//* diffuse pdf */
if (hasDiffuse)
diffuseProb = warp::squareToCosineHemispherePdf(bRec.wo);
/* specular pdf */
if (hasSpecular) {
Vector H = bRec.wo+bRec.wi; Float Hlen = H.length();
if(Hlen == 0.0f) specProb = 0.0f;
else
{
H /= Hlen;
const Float roughness2 = m_roughness*m_roughness;
const Float cosTheta2 = Frame::cosTheta2(H);
specProb = INV_PI * Frame::cosTheta(H) * math::fastexp(-Frame::tanTheta2(H)/roughness2) / (roughness2 * cosTheta2*cosTheta2) / (4.0f * absDot(bRec.wo, H));
}
}
if (hasDiffuse && hasSpecular)
return m_specularSamplingWeight * specProb + (1.0f-m_specularSamplingWeight) * diffuseProb;
else if (hasDiffuse)
return diffuseProb;
else if (hasSpecular)
return specProb;
else
return 0.0f;
}
Spectrum sample(BSDFSamplingRecord &bRec, Float &pdf, const Point2 &_sample) const {
Point2 sample(_sample);
bool hasSpecular = (bRec.typeMask & EGlossyReflection)
&& (bRec.component == -1 || bRec.component == 0);
bool hasDiffuse = (bRec.typeMask & EDiffuseReflection)
&& (bRec.component == -1 || bRec.component == 1);
if (!hasSpecular && !hasDiffuse)
return Spectrum(0.0f);
// determine which component to sample
bool choseSpecular = hasSpecular;
if (hasDiffuse && hasSpecular) {
if (sample.x <= m_specularSamplingWeight) {
sample.x /= m_specularSamplingWeight;
} else {
sample.x = (sample.x - m_specularSamplingWeight)
/ (1.0f-m_specularSamplingWeight);
choseSpecular = false;
}
}
/* sample specular */
if (choseSpecular) {
Float cosThetaM = 0.0f, phiM = (2.0f * M_PI) * sample.y;
Float tanThetaMSqr = -m_roughness*m_roughness * math::fastlog(1.0f - sample.x);
cosThetaM = 1.0f / std::sqrt(1.0f + tanThetaMSqr);
const Float sinThetaM = std::sqrt(std::max((Float) 0.0f, 1.0f - cosThetaM*cosThetaM));
Float sinPhiM, cosPhiM;
math::sincos(phiM, &sinPhiM, &cosPhiM);
const Normal m = Vector(sinThetaM * cosPhiM,sinThetaM * sinPhiM,cosThetaM);
// Perfect specular reflection based on the microsurface normal
bRec.wo = 2.0f * dot(bRec.wi, m) * Vector(m) - bRec.wi;
bRec.sampledComponent = 0;
bRec.sampledType = EGlossyReflection;
/* sample diffuse */
} else {
bRec.wo = warp::squareToCosineHemisphere(sample);
bRec.sampledComponent = 1;
bRec.sampledType = EDiffuseReflection;
}
bRec.eta = 1.0f;
pdf = CookTorrance::pdf(bRec, ESolidAngle);
/* unoptimized evaluation, explicit division of evaluation / pdf. */
if (pdf == 0 || Frame::cosTheta(bRec.wo) <= 0)
return Spectrum(0.0f);
else
return eval(bRec, ESolidAngle) / pdf;
}
Spectrum sample(BSDFSamplingRecord &bRec, const Point2 &sample) const {
Float pdf;
return CookTorrance::sample(bRec, pdf, sample);
}
void serialize(Stream *stream, InstanceManager *manager) const {
BSDF::serialize(stream, manager);
m_diffuseReflectance.serialize(stream);
m_specularReflectance.serialize(stream);
stream->writeFloat( m_roughness );
stream->writeFloat( m_F0 );
}
Float getRoughness(const Intersection &its, int component) const {
return m_roughness;
}
std::string toString() const {
std::ostringstream oss;
oss << "Cook-Torrance[" << endl
<< " id = \"" << getID() << "\"," << endl
<< " diffuseReflectance = " << indent(m_diffuseReflectance.toString()) << ", " << endl
<< " specularReflectance = " << indent(m_specularReflectance.toString()) << ", " << endl
<< " F0 = " << m_F0 << ", " << endl
<< " roughness = " << m_roughness << endl
<< "]";
return oss.str();
}
Shader *createShader(Renderer *renderer) const;
MTS_DECLARE_CLASS()
private:
// helper method
inline Float fresnel(const Float& F0, const Float& c) const
{
return F0 + (1.0f - F0)*pow(1.0-c, 5.0f);
}
// attribtues
Float m_F0;
Float m_roughness;
Spectrum m_diffuseReflectance;
Spectrum m_specularReflectance;
Float m_specularSamplingWeight;
};
// ================ Hardware shader implementation ================
/* CookTorrance shader-- render as a 'black box' */
class CookTorranceShader : public Shader {
public:
CookTorranceShader(Renderer *renderer) :
Shader(renderer, EBSDFShader) {
m_flags = ETransparent;
}
void generateCode(std::ostringstream &oss,
const std::string &evalName,
const std::vector<std::string> &depNames) const {
oss << "vec3 " << evalName << "(vec2 uv, vec3 wi, vec3 wo) {" << endl
<< " return vec3(0.0);" << endl
<< "}" << endl;
oss << "vec3 " << evalName << "_diffuse(vec2 uv, vec3 wi, vec3 wo) {" << endl
<< " return vec3(0.0);" << endl
<< "}" << endl;
}
MTS_DECLARE_CLASS()
};
Shader *CookTorrance::createShader(Renderer *renderer) const {
return new CookTorranceShader(renderer);
}
MTS_IMPLEMENT_CLASS(CookTorranceShader, false, Shader)
MTS_IMPLEMENT_CLASS_S(CookTorrance, false, BSDF)
MTS_EXPORT_PLUGIN(CookTorrance, "CookTorrance BSDF");
MTS_NAMESPACE_END