Lighting and Shading
Light
Source
Incoming
(Incident) light
Reflected light
Object
Surface
Refracted light
Light Source
Point Light Source
Light emits from a single point
in all directions
Parallel (Directional) Light Source
Light source is at infinity.
Light rays are parallel lines.
V
V:
Light direction vector
Spotlight
Light emits from a single point. Light rays are confined to a cone.
c
c:
V:
V
Cut-off angle
Light direction vector (Cone Axis)
Extended Light Source
Light source with a finite surface area.
Can be approximated as multiple point light sources.
Radial Intensity Attenuation
Light intensity decreases as light travels away from the light source.
Radial intensity attenuation factor
f r (d )
d
a0
a1
a2
1
a0 a1d a2 d 2
Distance from the light source
Constant attenuation coefficient
Linear attenuation coefficient
Quadratic attenuation coefficient
Radial intensity attenuation only applies to point light and spotlight.
Angular Intensity Attenuation
R (light ray)
c
V (Cone axis)
For spotlight, light intensity decreases as the light ray deviates from
the cone axis.
Angular intensity attenuation factor
cos m , 0 c
f a ( )
c
0,
m
cos R V
Light ray angle from cone axis
Angular intensity attenuation exponent
Phong Reflection Model
Ambient
Diffuse
Specular
Emissive
Ambient Light
Ambient Light: Background light that comes from all directions
and has constant intensity.
Oa ka I a
Ia
Oa
ka
Intensity of incoming ambient light
Intensity of reflected ambient light
Ambient reflectivity of object surface
Diffuse Reflection
• Incoming light comes from a certain direction.
• Reflected light is scattered in all directions.
Incident
light
N
L
N Unit Surface Normal
L Unit Direction Vector to Light
incident angle
kd I d ( N L), if N L 0
Od
if N L 0
0,
Id
Od
kd
Intensity of incoming diffuse light
Intensity of reflected diffuse light
Diffuse reflectivity of object surface
For most materials, kd = ka
Specular Reflection
• Incoming light comes from a certain direction.
• Reflected light concentrates around a certain direction.
Incident
light
N
R
L
Viewer
V
R Unit Vector in Ideal Specular Reflection Direction
V Unit Direction Vector to the Viewer
R ( 2 N L) N L
Phong Model
ks I s ( V R )n , if V R 0 and N L 0
Os
if V R 0 or N L 0
0,
Is
Os
ks
n
Intensity of incoming specular light
Intensity of specular reflection light
Specular reflectivity of object surface
Specular reflection exponent
Simplified Phong Model
Halfway vector
N H
R
H
LV
LV
L
V
ks I s ( N H)n , if N H 0 and N L 0
Os
if N H 0 or N L 0
0,
n controls the shininess of the surface
N
L
N
R
Shiny surface (large n)
Small specular highlight
L
R
Dull surface (small n)
Large specular highlight
Surface Light Emission
Light emitted by the object itself.
I e constant
Combine Everything Together
nl
O I e ka I a f rl f al kd I dl ( N L) ks I sl ( N H)n
l 1
nl: Number of light sources
Colored Light and Material
Light Intensity and Material Reflectivity are specified in RGB triples
I a ( I aR , I aG , I aB ), I d ( I dR , I dG , I dB ), I s ( I sR , I sG , I sB )
ka (kaR , kaG , kaB ), kd (kdR , kdG , kdB ), k s (k sR , k sG , k sB )
Lighting calculation is performed for each RGB component separately
OaR k aR I aR
G
G G
O
k
a
a Ia
O B k B I B
a a
a
OdR kdR I dR ( N L)
G
G G
O
k
d
d I d ( N L)
O B k B I B ( N L)
d d
d
OsR k sR I sR ( N H) n
G
G G
n
O
k
I
(
N
H
)
s
s s
O B k B I B ( N H)n
s s
s
Polygon Shading Models
• Flat shading
• Smooth shading (Gouraud shading)
• Phong shading
Flat Shading
Polygon surface is rendered using a single color.
The color usually comes from a vertex or polygon centroid under
certain illumination model.
Smooth Shading (Gouraud shading)
The color for each vertex of the polygon is calculated under certain
illumination model. The surface color of the polygon is interpolated
using the vertices’ colors.
1. Determine unit normal vector at each vertex.
2. Calculate the color of each vertex using an illumination model.
3. Linearly interpolate vertex colors across projected polygon.
Phong Shading
Interpolate vertex normal vectors instead of vertex colors
1.
2.
3.
Determine unit normal vector at each vertex.
Linearly interpolate vertex normals across projected polygon.
Apply an illumination model and calculate the color of each
projected pixel using the interpolated normal.
Advantage of Phong Shading over Smooth Shading:
• No Mach bands effect (bright and dark intensity streaks)
• Better specular highlight
Vertex Normal Calculation
Polygon Surface Normal and Plane Equation
z
N=(A, B, C)
Three points on the
polygon surface:
Outside
P1
P2
P3
y
Inside
P1 ( x1 , y1 , z1 )
P2 ( x2 , y2 , z 2 )
P ( x , y , z )
3
3
3
3
In right-handed coordinate system, P1, P2, P3
are specified in counter-clockwise direction
when viewed from outside to inside
x
Surface normal:
T P1P2 P1P3 (P2 P1 ) (P3 P1 ) ( A, B, C )
Plane equation:
Ax By Cz D 0
T P D
or
D T P1 T P2 T P3
1
N
y1
z1
x1 1 z1
x1
y1 1
x1
y1
z1
A 1 y2
z2
B x2 1 z 2
C x2
y2 1
D x2
y2
z2
1 y3
z3
x3 1 z3
x3
y3 1
x3
y3
z3
if Ax + By + Cz + D < 0, point (x, y, z) is inside.
if Ax + By + Cz + D > 0, point (x, y, z) is outside.
T
T
Averaged Vertex Normal of Polygon Surfaces
N2
N1
N
N3
Average normal vectors of all
polygons that share the vertex
n
N4
T Ni
i 1
T
N
T
True Normal of a Curved Surface
Non-parametric surface equation
f ( x, y , z ) 0
Parametric surface equation
( x, y, z ) P(u, v)
T
N
T
f f f
T , , f
x y z
T
P P
u v
Lighting in OpenGL
Create Light Source
void glLightf(GLenum light, GLenum pname, GLfloat pvalue)
void glLightfv(GLenum light, GLenum pname, GLfloat *pvalue)
Create a light source
light:
Light source identifier.
Values are GL_LIGHTi, (i = 0, 1, 2, … , 7)
pname: Parameter name
pvalue: Parameter value
To enable a light source, use glEnable(GL_LIGHTi);
To disable a light source, use glDisable(GL_LIGHTi);
To enable lighting calculation, use glEnable(GL_LIGHTING);
To disable lighting calculation, use glDisable(GL_LIGHTING);
Parameter name
Parameter value Meaning
GL_AMBIENT
(Ra, Ga, Ba, Aa)
GL_DIFFUSE
(Rd, Gd, Bd, Ad) diffuse intensity
GL_SPECULAR
(Rs, Gs, Bs, As)
specular intensity
GL_POSITION
(x, y, z, h)
light source position
h=0 indicates parallel
light source
GL_SPOT_DIRECTION
(x, y, z)
spotlight direction
GL_SPOT_CUTOFF
c
spotlight cutoff angle
(in degree)
GL_SPOT_EXPONENT
m
spotlight exponent
GL_CONSTANT_ATTENUATION
a0
constant attenuation
coefficient
GL_LINEAR_ATTENUATION
a1
linear attenuation
coefficient
GL_QUADRATIC_ATTENUATION
a2
quadratic attenuation
coefficient
ambient intensity
The light source position specified by GL_POSITION is transformed by
current Modelview matrix and stored in eye coordinates.
To make a light source
relatively fixed to the viewer
To make a light source
relatively fixed to the scene
Specify light source position
Viewing Transformation
Viewing Transformation
Specify light source position
Modeling Transformation
Modeling Transformation
Define Surface Material Properties
void glMaterialf(GLenum face, GLenum pname, GLfloat pvalue)
void glMaterialfv(GLenum face, GLenum pname, GLfloat *pvalue)
face:
Indicates which face the material properties are applied to.
GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
pname
pvalue
Meaning
GL_AMBIENT
(Ra, Ga, Ba, Aa)
ambient reflectivity
GL_DIFFUSE
(Rd, Gd, Bd, Ad)
diffuse reflectivity
GL_SPECULAR
(Rs, Gs, Bs, As)
specular reflectivity
GL_AMBIENT_AND_DIFFUSE (Rad, Gad, Bad, Aad) ambient and diffuse
reflectivity
GL_EMISSION
(Re, Ge, Be, Ae)
emissive color
GL_SHININESS
0 n 128
Specular reflection
exponent
Use Color Material Mode
void glColorMaterial( GLenum face, GLenum mode)
Cause material property specified by mode of polygon faces specified
by face to track current color specified by glColor*() command.
face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
mode: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR,
GL_AMBIENT_AND_DIFFUSE, GL_EMISSION
To enable color material mode,
use glEnable(GL_COLOR_MATERIAL);
To disable color material mode,
use glDisable(GL_COLOR_MATERIAL);
Select Lighting Model
void glLightModel{if}(GLenum pname, TYPE pvalue)
void glLightModel{if}v(GLenum pname, TYPE *pvalue)
Global Ambient Light
pname = GL_LIGHT_MODEL_AMBIENT
pvalue = (R, G, B, A)
Defines global ambient light intensity that is not from any
particular light source. Default value is (0.2, 0.2, 0.2, 1.0)
Two-Sided Lighting
pname = GL_LIGHT_MODEL_TWO_SIDE
pvalue = GL_FALSE
One-sided lighting. Only perform lighting
calculation on front faces. (default)
pvalue = GL_TRUE
Two-sided lighting. Perform lighting
calculation on both front and back faces
Local or Infinite Viewpoint
pname = GL_LIGHT_MODEL_LOCAL_VIEWER
pvalue = GL_FALSE
Infinite viewpoint (default)
pvalue = GL_TRUE
Local viewpoint
This parameter affects how direction to viewer vector V is calculated
for specular reflection.
For infinite viewpoint, V is a constant (0, 0, 1) in eye coordinates.
For local viewpoint, the viewpoint is placed at the origin in eye
coordinates and V is different for each vertex.
Secondary Specular Color (Added in OpenGL 1.2. Need glext.h header)
pname = GL_LIGHT_MODEL_COLOR_CONTROL
pvalue = GL_SINGLE_COLOR
Specular color is added to ambient, diffuse and emissive colors
before texture mapping. (default)
pvalue = GL_SEPARATE_SPECULAR_COLOR
Specular color is calculated separately and added after texture
mapping.
Select shading model
void glShadeModel(Glenum mode)
mode:
GL_FLAT
GL_SMOOTH
Flat shading
Smooth shading (Gouraud shading)
Specify Vertex Normal
void glNormal3{bsifd}(TYPE x, TYPE y , TYPE z)
void glNormal3{bsifd}v(TYPE * v)
Specify current normal vector
• Current normal vector is considered an OpenGL state variable.
Therefore the normal vector of a vertex should be specified before its
coordinates.
• Normal vector should be unit vector. By default, OpenGL does not
automatically normalize a normal vector. To enable automatically
normalization, use glEnable(GL_NORMALIZE).
Use glNormal*() in primitive rendering
glBegin(mode);
glNormal*(N0);
glVertex*(V0);
glNormal*(N1);
glVertex*(V1);
...
glNormal*(Nn1);
glVertex*(Vn1);
glEnd();
© Copyright 2026 Paperzz