Graphics Reference
In-Depth Information
transmissive component, where the weights sum to one or less. If they sum to less
than one, there's some absorption. The weights are specified for R, G, and B, and
the sum must be no more than one in each component.
The glossy portion of the model has an exponent that can be any positive num-
ber or infinity. If it's infinity, then we have a mirror reflection; otherwise, we have
a Blinn-Phong-like reflection, which is scaled by a Fresnel term, F . Listing 32.9
shows this code.
Finally, we compute the transmissive scattering due to refraction, with the
code shown in Listing 32.10. The only subtle point is that the Fresnel coefficient
for the transmitted light is one minus the coefficient for the reflected light.
Listing 32.10: Scattering due to transmission.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
if (material.transmit.nonZero()) {
// Fresnel transmissive coefficient
Color3 F_t;
if (Finit) {
F_t = (Color3::one() - F);
} else {
// Cosine of the angle of incidence, for computing F
const float cos_i = max(0.001f, w_i.dot(n));
// Schlick approximation.
F_t.r = F_t.g = F_t.b = 1.0f - pow5(1.0f - cos_i);
}
const Color3& T0
= material.transmit;
const Color3& p_transmit = F_t * T0;
const float p_transmitAvg = p_transmit.average();
r -= p_transmitAvg;
if (r < 0.0f) {
weight_o = p_transmit * (1.0f / p_transmitAvg);
w_o = (-w_i).refractionDirection(n, material.etaTransmit,
material.etaReflect);
density = p_transmitAvg;
eta_o = material.etaTransmit;
extinction_o = material.extinctionTransmit;
24
25
26
27
28
29
30
31
32
33
34
35
// w_o is zero on total internal refraction
return ! w_o.isZero();
}
}
// Absorbed
return false;
}
The code in Listing 32.10 is messy. It's full of branches, and there are several
approximations and apparently ad hoc tricks, like the Schlick approximation of
the Fresnel coefficient, and the setting of the cosine of the incident angle to be at
least 0.001, embedded in it. This is typical of scattering code. Scattering is a messy
process, and we must expect the code to reflect this, but the messiness also arises
from the challenges of floating-point arithmetic on machines of finite precision.
Perhaps a more positive view is that the code will be called many times with
 
 
Search WWH ::




Custom Search