O'Reilly logo

Graphics Shaders, 2nd Edition by Steve Cunningham, Mike Bailey

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

123
Lighting
6
The simplest way to perform lighting is by computing it per-vertex, which
would place responsibility for most of the work squarely on the shoulders of
the vertex shader. If lighting is performed this way, the color is computed based
on light and material properties that determine the color of each vertex based
on the standard ambient-diuse-specular (ADS) lighting model. This per-vertex
color can be used for either at or smooth shading. However, if a more complex
shading model is to be used, such as Phong or anisotropic shading, the color
computation will probably be deferred until the fragment shader, where per-
pixel color can be computed.
In this chapter, we will discuss both per-vertex and per-fragment lighting
methods.
124
6. Lighting
The ADS Lighting Model
This lighting model is the basis for xed-function OpenGL lighting, and we
want to see how to handle this in shaders you write yourself. You were prob-
ably introduced to this in your beginning computer graphics course, but let’s
review it to be sure we’re all using the same terminology and notation. The
three kinds of light used in this model are
Ambient light, or light that is always present at all points in a scene.
Diuse light, or light that comes directly from a light source.
Specular light, or light that is reected in a “shiny” way from a light source
by an object.
Each of these kinds of light contributes to the overall lighting at any
point in a separate way. The general context for these contributions is shown
in Figure 6.1, which illustrates a point on a surface with normalized (unit) vec-
tors from the point to the eye,
ˆ
E
; from the point to a light source,
ˆ
L
; the normal
to the surface at the point,
ˆ
N
; and the reected light direction
ˆ
R
.
Ambient light contributes to the lighting as a product of the ambient light
itself
A
L
and the ambient light color of the material being lighted
:
A
M
AA
AL M=*
.
Diuse light contributes to the lighting as a product of the diuse light
itself
,
D
L
the diuse light color of the material being lighted
,
D
M
and the
cosine of the angle Θ between the light and the normal, (
ˆ
L
ˆ
N
):
D = L
D
* M
D
* (
ˆ
L
ˆ
N
).
Figure 6.1. The setup for ADS lighting.
125
The ADS Lighting Model
Specular light contributes to the lighting as a product of the specular
light itself
,
S
L
the specular light color of the material being lighted
,
S
M
and a
power (the “shininess” coecient SH) of the cosine of the angle Φ between the
eye vector and the light reection vector, (
ˆ
R
ˆ
E
)
SH 
:
S = L
S
* M
S
* (
ˆ
R
ˆ
E
)
SH
.
Then the total lighting at the point is the sum of these:
A + D + S = L
A
* M
A
+ L
D
* M
D
* (
ˆ
L
ˆ
N
) + L
S
* M
S
* (
ˆ
R
ˆ
E
)
SH
.
The reection vector R is calculated by R = 2(
ˆ
N
ˆ
L
)
ˆ
N
ˆ
L
. Details on how
these individual formulas are derived may be found in any introductory graph-
ics text, such as [14]. Also, GLSL has a built-in function called
reflect( ),
which will do this for you.
This model can also take into account aenuation, or the reduction in
light intensity with distance. OpenGL models this with three factors: a con-
stant aenuation A
C 
, a linear aenuation A
L 
, and a quadratic aenuation A
Q
. If
a point is at a distance D from a light, the overall aenuation A is calculated as
A
AADAD
CL Q
=
++
1
2
.
The distance can be calculated from the light and vertex positions in eye
space, and this value of A then multiplies the diuse and specular terms above.
In the ADS lighting function in the next section, we use the reected-light
formulation because we have access to the reection for each pixel, using the
GLSL function
reect( ) to compute the reection vector. However, xed-
function OpenGL uses the half-angle formulation for specular light because it
is easier to compute for each vertex.
The ADS Lighting Model Function
Below is a function that computes the color at a vertex based on the ADS light-
ing model with standard light and material denitions. It is intended for use
with glman, so it uses stubs for the values it would get from another source.
These stubbed values would come from system uniform variables, as noted in
the function’s comments.
You can use this function in a vertex shader if you are computing the
color at each vertex, as you would if you were planning to interpolate the color
across the graphics primitive, as in smooth shading, or you can use it in a frag-

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required