-
Notifications
You must be signed in to change notification settings - Fork 4
/
microfacet.cpp
229 lines (190 loc) · 6.46 KB
/
microfacet.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
#include <mitsuba/render/bsdf.h>
#include <mitsuba/hw/basicshader.h>
#include <mitsuba/core/warp.h>
MTS_NAMESPACE_BEGIN
class MicroFacet : public BSDF {
public:
MicroFacet(const Properties &props)
: BSDF(props) {
m_diffuseReflectance = props.getSpectrum("diffuseReflectance", Spectrum(0.02f));
m_A = props.getSpectrum("A", Spectrum(10.0f));
m_B = props.getFloat("B", 10.0f);
m_C = props.getFloat("C", 0.5f);
m_F0 = props.getFloat("F0", 1.0f);
}
MicroFacet(Stream *stream, InstanceManager *manager)
: BSDF(stream, manager) {
m_diffuseReflectance = Spectrum(stream);
m_A = Spectrum(stream);
m_B = stream->readFloat();
m_C = 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;
//m_F0 /= 10.0f;
BSDF::configure();
std::cout << toString();
}
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 hasDiffuse = (bRec.typeMask & EDiffuseReflection)
&& (bRec.component == -1 || bRec.component == 1);
/* eval spec */
Spectrum result(0.0f);
Vector H = normalize(bRec.wo+bRec.wi);
const Float cosThetaH = Frame::cosTheta(H);
if(cosThetaH > 0.0f)
{
// compute the S(f) function
const Float f2 = 1.0f-cosThetaH;
const Spectrum S = sFunc(f2);
// compute shadowing and masking
const Float Hwi = dot(bRec.wi, H);
const Float Hwo = dot(bRec.wo, H);
const Float G = std::min(1.0f, std::min(
2.0f * cosThetaH * Frame::cosTheta(bRec.wi) / Hwi,
2.0f * cosThetaH * Frame::cosTheta(bRec.wo) / Hwo));
// compute Fresnel
const Float F = fresnel(m_F0, cosThetaH);
// evaluate the MicroFacet model
result += S * 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;
Float specProb = 0.0f;
Vector H = bRec.wo+bRec.wi; Float Hlen = H.length();
if(Hlen == 0.0f) specProb = 0.0f;
else
{
H /= Hlen;
/* normalization constant: Mh*A */
Float MhA = 0.0f;
if (m_C == 1.0f)
MhA = m_B/(2.0f*M_PI*log10(1.0f+m_B));
else
MhA = m_B*(m_C-1.0f)/(2.0f*M_PI*(1.0f-pow(1.0f+m_B, 1.0f-m_C)));
/* proposal densify function of the incident direction*/
const Float f2 = 1.0f-Frame::cosTheta(H);
specProb = (MhA / pow(1.0f+m_B*f2, m_C)) / (4.0f * absDot(bRec.wo, H));
}
return specProb;
}
Spectrum sample(BSDFSamplingRecord &bRec, Float &pdf, const Point2 &_sample) const {
Point2 sample(_sample);
/* sample specular */
// compute theta
Float cosThetaM = 0.0f;
if (m_C == 1.0f)
cosThetaM = (1.0f + m_B - math::fastexp(sample.x*log10(1.0f+m_B)))/m_B;
else
cosThetaM = (1.0f + m_B - pow(1.0f+sample.x*(std::pow(1.0f+m_B, 1.0f-m_C) - 1.0f), -1.0f/(m_C-1.0f)))/m_B;
const Float sinThetaM = std::sqrt(std::max((Float) 0.0f, 1.0f - cosThetaM*cosThetaM));
// compute phi
Float phiM = (2.0f * M_PI) * sample.y;
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;
bRec.eta = 1.0f;
pdf = MicroFacet::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 MicroFacet::sample(bRec, pdf, sample);
}
void serialize(Stream *stream, InstanceManager *manager) const {
BSDF::serialize(stream, manager);
m_diffuseReflectance.serialize(stream);
m_A.serialize(stream);
stream->writeFloat( m_B );
stream->writeFloat( m_C );
stream->writeFloat( m_F0 );
}
std::string toString() const {
std::ostringstream oss;
oss << "MicroFacet[" << endl
<< " id = \"" << getID() << "\"," << endl
<< " diffuseReflectance = " << indent(m_diffuseReflectance.toString()) << ", " << endl
<< " A = " << indent(m_A.toString()) << ", " << endl
<< " B = " << m_B << ", " << endl
<< " C = " << m_C << ", " << endl
<< " F0 = " << m_F0 << ", " << 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);
}
// compute the S function
inline Spectrum sFunc(const Float& fSquare) const
{
return m_A/(pow(1.0f+m_B*fSquare, m_C));
}
// attribtues
Float m_F0;
Float m_C;
Float m_B;
Spectrum m_A;
Spectrum m_diffuseReflectance;
};
// ================ Hardware shader implementation ================
/* MicroFacet shader-- render as a 'black box' */
class MicroFacetShader : public Shader {
public:
MicroFacetShader(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 *MicroFacet::createShader(Renderer *renderer) const {
return new MicroFacetShader(renderer);
}
MTS_IMPLEMENT_CLASS(MicroFacetShader, false, Shader)
MTS_IMPLEMENT_CLASS_S(MicroFacet, false, BSDF)
MTS_EXPORT_PLUGIN(MicroFacet, "MicroFacet BSDF");
MTS_NAMESPACE_END