-
Notifications
You must be signed in to change notification settings - Fork 1
/
glass.cc
232 lines (177 loc) · 6.78 KB
/
glass.cc
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
#include "glass.h"
#include "onb.h"
#include "random.h"
#include "log.h"
namespace Renzoku {
Glass::Glass(Float inner) : inner(inner), outer(1.f), kd(DefaultRgb::white) {
}
Glass::Glass(Float inner, Float outer) : inner(inner), outer(outer), kd(DefaultRgb::white) {
}
Glass::Glass(Float inner, const Rgb &kd) : inner(inner), outer(1.f), kd(kd) {
}
Glass::Glass(Float inner, Float outer, const Rgb &kd) : inner(inner), outer(outer), kd(kd) {
}
/**
* Convention:
*
* Glass normal vector is always facing to outer environment.
*
* Glass surface is not two-sided. In other words, glass objects should be modelled as thick object.
*/
Rgb Glass::sample(Random &rd, const Vec3 &nn, Float nr, Float nt, const Vec3 &wo, Vec3 &wi, Float &pdf) {
Float dot_wn = dot(wo, nn);
Float n1 = nr;
Float n2 = nt;
if (dot_wn < 0.0f) {
n1 = nt;
n2 = nr;
}
Float n1n2 = n1 / n2;
Float delta = 1.f - (n1n2 * n1n2) * (1.f - dot_wn * dot_wn);
if (delta <= ZERO_EPSILON) { // total internal reflection
wi = 2.0f * dot_wn * nn - wo;
pdf = 1.f;
if (fabs(dot(wi, nn)) > ZERO_EPSILON) {
Float f = 1.0f / fabs(dot(wi, nn)); // reflection BSDF based on delta distribution
return kd * f * INV_PI;
} else return DefaultRgb::black;
} else {
// compute Fresnel term to determine the amount of energy that reflects
//
// total energy:
// E = F * reflection + (1 - F) * transmission
//
// Each time we choose either reflection or transmission to trace
// with probability F for reflection and 1 - F for transmission.
//
// The estimator of E is:
// E' = F * reflection / pdf with probability F (so pdf = F and F cancels)
// E' = (1 - F) * transmission / pdf with probability 1 - F
//
// The average value of estimator E' is F * reflection + (1 - F) * transmission, which is the value we want.
//
wi = 2.0f * dot_wn * nn - wo;
Float F0 = (n1 - n2) / (n1 + n2);
F0 *= F0;
// due to perfect reflection, the half vector coincides with the normal
// so, F = F0.
/*
Vec3 wh = unit_vector(wi + wo);
Float p = 1.f - dot(nn, wh);
Float F = F0 + (1.f - F0) * p * p * p * p * p;
*/
Float F = F0;
Float u = rd(); // follow either reflection or transmission
if (u < F) {
pdf = F;
if (fabs(dot(wi, nn)) > ZERO_EPSILON) {
Float f = 1.0f / fabs(dot(wi, nn));
return F * kd * f * INV_PI;
} else {
return DefaultRgb::black;
}
} else {
if (dot_wn > 0.0f) {
wi = - n1n2 * (wo - dot_wn * nn) - sqrt(delta) * nn;
} else {
wi = - n1n2 * (wo - dot_wn * nn) + sqrt(delta) * nn;
}
if (dot(wi, wo) > 0.0f) {
pdf = 0.0f;
wi = Vec3(0.0f, 0.0f, 0.0f);
Log::info() << "Glass: wi, wo same direction" << endn;
return DefaultRgb::black;
}
pdf = 1.f - F;
if (fabs(dot(wi, nn)) > ZERO_EPSILON) {
Float f = (n2*n2 / n1*n1) / fabs(dot(wi, nn)); // refraction BSDF based on delta distribution
return (1.f - F) * kd * f * INV_PI;
} else {
return DefaultRgb::black;
}
}
}
}
Rgb Glass::sample(Random &rd, const Onb &uvn, const Vec3 &wo, Vec3 &wi, Float &pdf) {
return sample(rd, uvn.n(), outer, inner, wo, wi, pdf);
}
Rgb Glass::eval(const Onb &uvn, const Vec3 &wo, const Vec3 &wi) {
Vec3 nn = uvn.n();
Float dot_wn = dot(wo, nn);
Float n1 = outer;
Float n2 = inner;
if (dot_wn < 0.0f) {
n1 = inner;
n2 = outer;
}
Float n1n2 = n1 / n2;
Float delta = 1.f - (n1n2 * n1n2) * (1.f - dot_wn * dot_wn);
if (delta <= ZERO_EPSILON) {
Vec3 mirror_wi = 2.0f * dot_wn * nn - wo;
if (1.0f - fabs(dot(wi, mirror_wi)) <= ZERO_EPSILON) {
if (fabs(dot(wi, nn)) > ZERO_EPSILON) {
Float f = 1.0f / fabs(dot(wi, nn));
return kd * f * INV_PI;
}
}
} else {
Float F0 = (n1 - n2) / (n1 + n2);
F0 *= F0;
Float F = F0;
Vec3 mirror_wi = 2.0f * dot_wn * nn - wo;
if (1.0f - fabs(dot(wi, mirror_wi)) <= ZERO_EPSILON) {
if (fabs(dot(wi, nn)) > ZERO_EPSILON) {
Float f = 1.0f / fabs(dot(wi, nn));
return F * kd * f * INV_PI;
}
}
Vec3 transmit_wi;
if (dot_wn >= 0.0f)
transmit_wi = - n1n2 * (wo - dot_wn * nn) - sqrt(delta) * nn;
else
transmit_wi = - n1n2 * (wo - dot_wn * nn) + sqrt(delta) * nn;
if (1.0f - fabs(dot(wi, transmit_wi)) <= ZERO_EPSILON) {
if (fabs(dot(wi, nn)) > ZERO_EPSILON) {
Float f = (n2*n2 / n1*n1) / fabs(dot(wi, nn));
return (1.f - F) * kd * f * INV_PI;
}
}
}
return DefaultRgb::black;
}
/**
* This pdf cannot just return 0.0f since we need to pdf(wi) "as if"
* wi is sampled from wo.
*
* This is important for MIS weight calculation in bidir path tracing.
*/
Float Glass::pdf(const Onb &uvn, const Vec3 &wo, const Vec3 &wi) {
Vec3 nn = uvn.n();
Float dot_wn = dot(wo, nn);
Float n1 = outer;
Float n2 = inner;
if (dot_wn < 0.0f) {
n1 = inner;
n2 = outer;
}
Float n1n2 = n1 / n2;
Float delta = 1.f - (n1n2 * n1n2) * (1.f - dot_wn * dot_wn);
if (delta <= ZERO_EPSILON) {
Vec3 mirror_wi = 2.0f * dot_wn * nn - wo;
if (1.0f - fabs(dot(wi, mirror_wi)) <= ZERO_EPSILON) return 1.0f;
} else {
Float F0 = (n1 - n2) / (n1 + n2);
F0 *= F0;
Float F = F0;
Vec3 mirror_wi = 2.0f * dot_wn * nn - wo;
if (1.0f - fabs(dot(wi, mirror_wi)) <= ZERO_EPSILON) return F;
Vec3 transmit_wi;
if (dot_wn >= 0.0f)
transmit_wi = - n1n2 * (wo - dot_wn * nn) - sqrt(delta) * nn;
else
transmit_wi = - n1n2 * (wo - dot_wn * nn) + sqrt(delta) * nn;
if (1.0f - fabs(dot(wi, transmit_wi)) <= ZERO_EPSILON) return 1.0f - F;
}
return 0.0f;
}
} // end namspace Renzoku