17.5. Frequency Clamping
Certain functions do not have an analytic solution, or they are just too difficult to solve. If this is the case, you might try a technique called frequency clamping. In this technique, the average value of the function replaces the actual value of the function when the filter width is too large. This is convenient for functions such as sine and noise whose average is known.
17.5.1. Antialiased Checkerboard Fragment Shader
The checkerboard pattern is the standard measure of the quality of an antialiasing technique (see Figure 17.9). Larry Gritz wrote a checkerboard RenderMan shader that performs antialiasing by frequency sampling, and Dave Baldwin translated this shader to GLSL. Listing 17.3 shows a fragment shader that produces a procedurally generated, antialiased checkerboard pattern. The vertex shader transforms the vertex position and passes along the texture coordinate, nothing more. The application provides values for the two colors of the checkerboard pattern, the average of these two colors (the application can compute this and provide it through a uniform variable, rather than having the fragment shader compute it for every fragment), and the frequency of the checkerboard pattern.
Figure 17.9. Checkerboard pattern rendered with the antialiased checkerboard shader. On the left, the filter width is set to 0, so aliasing occurs. On the right, the filter width is computed using the fwidth function.
The fragment shader computes the appropriate size of the filter and uses it to perform smooth interpolation between adjoining checkerboard squares. If the filter is too wide (i.e., the varying parameter is changing too quickly for proper filtering), the average color is substituted. Even though this fragment shader uses a conditional statement, care is taken to avoid aliasing. In the transition zone between the if clause and the else clause, a smooth interpolation is performed between the computed color and the average color.
Listing 17.3. Source code for an antialiased checkerboard fragment shader