Game Development Reference
In-Depth Information
The fragment shader
After the vertex shader has worked on vertex attributes (such as position), the next
phase is the
primitive assembly
stage. In the primitive assembly stage, primitive
objects such as lines and triangles are clipped and culled. If a primitive object lies
partly outside the view frustum (the 3D region that is visible on the screen), then it is
clipped; if it lies completely outside, then it is culled. The next phase is
rasterization
.
In this phase, all primitives are converted to two-dimensional fragments, also called
pixels
. These fragments are then processed by the fragment shader.
A fragment shader performs the following functions:
•
Operations on interpolated values
•
Texture access
•
Texture application
•
Fog
•
Color sum
The following listed code is the most basic fragment shader. In this code, we simply
apply a constant color value to each fragment of our scene:
<scriptid="shader-fs" type="x-shader/x-fragment">
precisionmediump float;
void main(void) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
</script>
The
gl_FragColor
variable is declared automatically and the color for each fragment
is set. For the purpose of making this shader extremely simple, we did not load any
data to the shader; we generally passed these values to the fragment shader. The
values are as follows:
•
Varying variables
: Outputs of the vertex shader that are generated by the
rasterization unit for each fragment using interpolation
•
Uniforms
: Constant data used by the fragment shader
•
Sampler
: A specific type of uniform that represents textures used by the
fragment shader
Shader variable qualifiers
Shader variables can be qualified as uniform, attribute, varying, and constant.