Basic Function of a Fragment Shader
quite familiar, but we want to remind our-
selves of them in the fragment shader context.
Fragment shaders can use uniform variables
that are provided by the system or by the
application. Because uniform variables do
not change within a graphics primitive, they
will not change during the interpolations that
the rasterizer performs. However, they can
be used for any computations that might be
needed in fragment processing.
As a preview, there is a special kind of
uniform variable for textures that is avail-
able to both vertex and fragment shaders,
but which is particularly important for frag-
ment shaders: the uniform sampler variable.
The uniform sampler variables correspond to
textures, so with the 1D, 2D, and 3D textures,
we have sampler types
sampler3D. Since GLSL also allows cube
mapping textures, we have the additional
samplerCube. The particular
sampler type that you use must correspond to
the texture type that was dened in your application. The value associated with
a sampler variable is the texture image number associated with the texture it
represents, which is also set up in the texture denition functions in your appli-
texture( ) function will be used with the texture unit and texture
coordinates to return the texture value at those coordinates, as in the line
vec4 textureValue = texture( TexUnit, coordinateVector );
We will see examples of this use in texture-based fragment shader code later
in this chapter.
Input and Output Variables
Perhaps the most important inputs to fragment shaders are the variables that
are passed to the fragment shader as
out variables by the pipeline stage that
occurs right before the rasterizer and fragment processor. This could be a
vertex, geometry, or tessellation shader. These variables are the data that are
interpolated across a graphics primitive in order to give the fragment shader
enough information to set the colors of each pixel.
Figure 8.2. The vertices of a graphics primitive
and the fragments that are processed to make up
the primitive being displayed.