Game Development Reference
In-Depth Information
Fragment Shader Language (FSL)
The FSL is used to change the color attributes (RGBA) of the current pixel. For example, the
following fragment sets the color of the current pixel to red RGBA (1, 0, 0, 0):
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);
}
gl_FragColor
is the built-in variable used to set the color of the current pixel. As with any
programming language, GLSL provides all the things you would expect from a computer
language, including:
Variables and functions
: All variables and functions must be declared
before being used.
Basic types
: This includes
void
,
bool
,
int
,
float
,
vec2
(two-component
float point vector); boolean or integer 2, 3, or 4 component vectors;
2 × 2, 3 × 3, or 4 × 4 float matrices.
Declaration scope
: Determines where the declaration is visible. This
includes global and local variables, name spaces, and re-declarations
within the same scope.
Storage qualifiers
: Qualifiers specified in front of the type—pure
traditional C-style—including local variables and constants.
New to GLSL are the following items:
attribute
: Defines the linkage between a vertex shader and OpenGL ES
for per-vertex data.
uniform
: Tells that the value does not change across the primitive being
processed. It also forms the linkage between a shader, OpenGL ES, and
the application.
varying
: Defines that linkage between a vertex shader and a fragment
shader for interpolated data.
Parameter qualifiers
: These are the qualifiers passed to the arguments of
a function, including:
in: A parameter is passed into a function.
out: A parameter passed back out of a function, but not initialized.
inout: A parameter is passed both into and out of a function.
Precision qualifiers
: For floating point precision, including
highp
,
mediump
, and
lowp
for high, medium, and low precision, respectively.
Variance and the invariant qualifier
: These are used to handle the
possibility of getting different values from the same expression in
different shaders. It is possible, in independent compilation, that two