Graphics Reference
In-Depth Information
Note that flat variables can also be used as a way for a vertex shader to
pass “global” values down to a fragment shader.
Smooth (Gouraud) Shading
Smooth shading (or Gouraud shading) is a very helpful tool in creating atrac-
tive images with OpenGL, but it has some rather severe shortcomings. It does
not handle specular highlights well, because specular highlights do not lin-
early interpolate well on the interior of a polygon. Also, smooth shading is
vulnerable to Mach bands, meaning it shows the boundaries of polygons more
than it should. Its implementations may also do strange things for quads and
polygons that have very different colors at their vertices, because it is usu-
ally implemented by breaking the object into triangles and handling each tri-
angle separately. Unfortunately, implementing smooth shading in a fragment
shader will likely not solve these problems, but it may well be something you
would want to have in your shader arsenal.
An extremely simple fragment shader to do smooth shading is shown
below. This simply takes the color that has been computed for each vertex,
using whatever process the vertex shader has needed and saved there as
vmyColor . A vertex shader that does this was shown in the chapter on light-
ing, and a Gouraud-shaded teapot was shown in Figure 5.4. The computed
color is passed to the fragment shader and is interpolated across the primi-
tive and saved as the fragment color in fFragColor . Of course, this is a very
simple example, but it does everything that fixed-function smooth shading
does.
in vec3 vmyColor;
out vec4 fFracColor
void main( )
{
fFragColor = vec4( vmyColor, 1.0 );
}
Traditional Texture Mapping
Texture mapping is another fixed-function operation that can be readily han-
dled by fragment shaders. Texture coordinates are easily set up as input vari-
ables, so they are interpolated for each fragment, and sampler functions can
look up coordinates in a texture map to get the actual texels to be used in
determining each pixel's color. Rather than covering texture mapping in this
chapter, we discuss it fully in the next chapter.
Search WWH ::




Custom Search