Graphics Reference
In-Depth Information
computed at any point where the quantity is defined. So at each point where the
teapot appears in the image, the rates of change of the coordinates of the normal
vector with respect to the pixel coordinates are computed and used to select a
MIP-mapping level that's appropriate.
Listing 33.7: The fragment shader for the Phong shading program.
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
/ ** Unit world space direction to the (infinite, directional)
light source * /
uniform vec3 wsLight;
Figure 33.5: A shiny teapot re-
flects its surroundings, the plaza
of the Uffizi gallery.
/ ** Environment cube map used for reflections * /
uniform samplerCube environmentMap;
/ ** Color for specular reflections * /
uniform vec3 specularColor;
varying vec3 wsInterpolatedNormal;
varying vec3 wsInterpolatedEye;
void main() {
// Unit normal in world space
vec3 wsNormal = normalize(wsInterpolatedNormal);
// Unit vector from the pixel to the eye in world space
vec3 wsEye = normalize(wsInterpolatedEye);
// Unit vector giving direction of reflection into the eye
vec3 wsReflect = 2.0 * dot(wsEye, wsNormal)
* wsNormal - wsEye;
gl_FragColor.rgb =
specularColor * textureCube(environmentMap,
wsReflect).rgb;
}
33.7 Two Versions of Toon Shading
We now turn to a rather different style, the toon shading of Chapter 34. In toon
shading, we compute the dot product of the normal and the light direction (as we
would for any Lambertian surface), but then choose a color value by thresholding
the result so that the resultant picture is drawn with just two or three colors, much
as a cartoon might be. There are, of course, many possible variations: We could
do thresholded shading using the Phong model, or any other; we could use two
or five thresholds; we could have varying light intensity rather than the simple
“single bright light” model we're using here.
The first (and not very wise) approach we'll take is to compute the inten-
sity (the dot product of the normal and light vectors) at each vertex in the vertex
shader (Listing 33.8), and let GL interpolate this value across each triangle and
then threshold the resulting intensities (Listing 33.9).
Listing 33.8: The vertex shader for the first toon-shading program.
1
2
3
4
/ * Camera origin in world space * /
uniform vec3 wsEyePosition;
/ * Non-unit vector to eye from vertex * /
varying vec3 wsInterpolatedEye;
 
 
 
Search WWH ::




Custom Search