Graphics Reference
In-Depth Information
Listing 15.17: The single-bounce shading code.
1
void
shade(
const
Scene
& scene,
const
Triangle
&T,
const
Point3
&P,
const
Vector3
&n,
const
Vector3
& w_o,
Radiance3
& L_o) {
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
L_o =
Color3
(0.0f, 0.0f, 0.0f);
// For each direction (to a light source)
for
(
unsigned int
i = 0; i < scene.lightArray.size(); ++i) {
const
Light
& light = scene.lightArray[i];
const
Vector3
& offset = light.position - P;
const float
distanceToLight = offset.length();
const
Vector3
& w_i = offset / distanceToLight;
if (visible(P, w_i, distanceToLight, scene)) {
const
Radiance3
& L_i = light.power / (4
*
PI
*
square(distanceToLight));
// Scatter the light
L_o +=
L_i
*
T.bsdf(n).evaluateFiniteScatteringDensity(w_i, w_o)
*
max(0.0, dot(w_i, n));
}
}
}
of the angle between
w_i
and
n
to account for the projected area over which
incident radiance is distributed (by the Tilting principle).
The simplest implementation of the BSDF assumes a surface appears to
be the same brightness independent of the viewer's orientation. That is,
evaluateFiniteScatteringDensity
returns a constant. This is called
Lam-
bertian reflectance,
and it is a good model for matte surfaces such as paper and
flat wall paint. It is also trivial to implement. Listing 15.18 gives the implementa-
tion (see Section 14.9.1 for a little more detail and Chapter 29 for a lot more). It
has a single member,
lambertian
, that is the “color” of the surface. For energy
conservation, this value should have all fields on the range
[
0, 1
]
.
Listing 15.18: Lambertian BSDF implementation, following Listing 14.6.
1
2
3
4
5
6
7
8
9
10
11
12
class
BSDF
{
public
:
Color3
k_L;
/
**
Returns f = L_o / (L_i * w_i.dot(n)) assuming
incident and outgoing directions are both in the
positive hemisphere above the normal
*
/
Color3
evaluateFiniteScatteringDensity
(
const
Vector3
& w_i,
const
Vector3
& w_o)
const
{
return k_L / PI;
}
};
Figure 15.7 shows our triangle scene rendered with the Lambertian
BSDF using
k_L=Color3(0.0f, 0.0f, 0.8f)
. Because our triangle's vertex
Figure 15.7: A green Lambertian
triangle.