Graphics Reference
In-Depth Information
Listing 33.10: The fragment shader for the improved toon-shading program.
1
2
3
4
5
6
7
8
9
10
11
12
13
uniform vec3 diffuseColor; / * Surface color * /
uniform vec3 wsLight; / * Unit world sp. dir'n to light * /
varying vec3 wsInterpolatedNormal; / * Surface normal. * /
void main() {
float intensity = dot(normalize(wsInterpolatedNormal),wsLight);
if (intensity > 0.95)
gl_FragColor.rgb = diffuseColor;
else if (intensity > 0.4)
gl_FragColor.rgb = diffuseColor * 0.6;
else
gl_FragColor.rgb = diffuseColor * 0.2;
}
Inline Exercise 33.2: Suppose we had omitted the normalization in the frag-
ment shader in Listing 33.10. How would the resultant image have differed?
How would it have differed from our first-draft toon shader? If you don't know,
implement both and compare.
33.8 Basic XToon Shading
Finally, we provide an implementation of a tiny portion of XToon shading: a
shader where a 2D texture map (see Figure 33.8) is used to govern appearance, but
in a somewhat unusual way. We index into the vertical coordinate using distance
from the eye so that more-distant points are bluer, resulting in a weak approxima-
tion of atmospheric perspective, which is based on the observation that in out-
door scenes, more-distant objects (e.g., mountains) tend to look bluer, and hence
we can provide a distance cue by mimicking this. We index into the horizontal
coordinate using the dot product of the view vector with the normal vector. When-
ever this dot product is zero (i.e., on a contour), we index into mid texture (i.e.,
the black area), resulting in black contour lines being drawn. In our texture, we've
made the black line larger at the bottom than the top, resulting in wider contours
at distant points than at nearby ones; endless other stylistic variations are possible.
Figure 33.8: The 2D texture map
for XToon shading. We use dis-
tance as an index into the vertical
direction, and v
ยท
n as the horizon-
tal index.
The shader code is once again very simple. In the vertex shader, we compute
the distance to the eye at each vertex; see Listing 33.11.
The results are shown in Figure 33.9. The teapot's contours are drawn with
gray-to-black lines, thicker in the distance; the handle of the teapot is slightly
bluer than the spout.
Figure 33.9: XToon-shaded tea-
pot, using the texture map from
the previous figure.
Listing 33.11: The vertex and fragment shaders for the XToon shading program.
... Vertex Shader ...
uniform vec3 wsEyePosition;
varying vec3 wsInterpolatedEye;
varying vec3 wsInterpolatedNormal;
1
2
3
4
5
6
7
8
varying float dist;
void main( void ){
 
 
 
Search WWH ::




Custom Search