Variable Lighting
Previously, we have seen textures used to vary surface parameters. But we can use textures
to vary something else: light intensity. In this way, we can simulate light sources who's
intensity changes with something more than just distance from the light.
Our first effort in varying light intensity with textures will be to build an incandescent
flashlight. The light beam from a flashlight is not a single solid intensity, due to the way
the mirrors focus the light. A texture is the simplest way to define this pattern of light
intensity.
Post-Projection Space
Before we can look at how to use a texture to make a flashlight, we need to understand
a special piece of mathematics. We need to revisit perspective projection. Specifically,
we need to look at what happens when transforming after a projection operation.
In Review
In this tutorial, you have learned the following:
Vertex positions can be further manipulated after a perspective projection.
Thus the perspective transform is not special. The shape of objects in
post-projective space can be unusual and unexpected.
Textures can be projected onto meshes. This is done by transforming those
meshes into the space of the texture, which is equivalent to transforming the
texture into the space of the meshes. The transform is governed by its own
camera matrix, as well as a projection matrix and a post-projective
transform.
Cube maps are textures that have 6 face images for every mipmap level. The 6
faces are arranged in a cube. Texture coordinates are effectively directions of
a vector centered within the cube. Thus a cube map can provide a varying value
based on a direction in space.
Further Study
Try doing these things with the given programs.
Instead of using a projective texture, build a lighting system for spot
lights entirely within the shader. It should have a maximum angle; the
larger the angle, the wider the spotlight. It should also have an inner
angle that is smaller than the maximum angle. This the the point where the
light starts falling off. At the maximum angle, the light intensity goes to
zero; at the minimum angle, the light intensity is full. The key here is
remembering that the dot product between the spotlight's direction and the
direction from the surface to the light is the cosine of the angle between
the two vectors. The acos function can be used to
compute the angle (in radians) from the cosine.
GLSL Functions of Note
vec4 textureProj
sampler texSampler
vec texCoord
Accesses the texture associated with texSampler, using
post-projective texture coordinates specified by texCoord.
The sampler type can be many of the sampler types, but not
samplerCube, among a few others. The texture coordinates are in
homogeneous space, so they have one more components than the number of dimensions of
the texture. Thus, the number of components in texCoord for a
sampler of type sampler1D is vec2. For
sampler2D, it is vec3.