Game Development Reference
In-Depth Information
We can write a couple of simple vertex shaders to perform edge extrusion
and transformation to clip space. In each of the shaders that follow, we assume
that we have access to a uniform variable named mvpMatrix containing the four
rows of the product of the projection matrix and model-view matrix and another
uniform variable named lightPosition containing the object-space light
position.
The vertex shader shown in Listing 10.2 demonstrates a safe way to extrude
vertices away from a point light source, where by “safe”, we mean that it is im-
mune to floating-point round-off errors. A similar vertex shader for infinite light
sources is shown in Listing 10.3.
Listing 10.2. This vertex shader extrudes vertices having a w coordinate of 0 away from a point
light source and leaves vertices having a w coordinate of 1 unchanged. Vertex positions are then
transformed into homogeneous clip space.
in vec4 vertexPosition; // The object-space vertex position.
uniform vec4 mvpMatrix[4]; // The model-view-projection matrix.
uniform vec3 lightPosition; // The object-space light position.
void main()
{
float t = (vertexPosition.w < 0.5) ? 1.0 : 0.0;
vec4 extrudedPosition = vec4 (vertexPosition.xyz - lightPosition * t,
vertexPosition.w);
gl_Position = vec4 (dot(mvpMatrix[0], extrudedPosition),
dot(mvpMatrix[1], extrudedPosition),
dot(mvpMatrix[2], extrudedPosition),
dot(mvpMatrix[3], extrudedPosition));
}
Listing 10.3. This vertex shader extrudes vertices having a w coordinate of 0 away from an
infinite light source and leaves vertices having a w coordinate of 1 unchanged. Vertex positions
are then transformed into homogeneous clip space.
in vec4 vertexPosition; // The object-space vertex position.
uniform vec4 mvpMatrix[4]; // The model-view-projection matrix.
uniform vec3 lightDirection; // The object-space light direction.