Graphics Reference
In-Depth Information
Additional Shader Effects
The main value in fragment shaders, of course, lies in the capabilities that go
beyond the functionality that is available from the fixed-function graphics
pipeline. In the sections below, we'll talk about some of these capabilities and
give examples of fragment shaders that support them.
Discarding Pixels
A unique capability of fragment shaders (that
is, unavailable with standard fixed-function
processing) is the ability to discard pixels.
This function is much stronger than sim-
ply seting the alpha value of pixels to zero,
because it makes the pixel disappear in any
view. We mentioned this in the earlier chapter
on general shader concepts, so here we simply
remind ourselves of this capability, shown in
Figure 8.5. The key factor is the discard key-
word in the fragment shader that instructs the
shader to stop processing the pixel and not
record it in the framebuffer.
Figure 8.5. The standard teapot with some pixels
discarded by a noise process.
Phong Shading
In the previous chapter we introduced a function that computes the ambient-
diffuse-specular lighting model from a set of light and material properties. In
that chapter, we showed how that function could be used in a vertex shader to
set colors for each vertex, so that the rasterizer could smoothly interpolate the
colors or intensities across a polygon. Here, we want to see how to do lighting
at each fragment, instead of at each vertex. This is known as Phong shading .
A Phong shading fragment shader takes the normal as a varying variable
from the vertex shader, has it interpolated in the rasterizer, and uses the inter-
polated normals to compute the ADS color at each fragment. The fragment
shader that created the right hand image in Figure 8.6 is shown below. This
uses the ADSLightModel( ) function introduced in the lighting chapter.
in vec3 vNormal; // interpolated from the vertex shader
in vec4 vPos; // interpolated from the vertex shader
out vec4 fFragColor;
Search WWH ::




Custom Search