You are on page 1of 9

1

Shading
Outline
OpenGL lighting & materials
Lighting details
Light in a medium (fog, etc.)
2
OpenGL lighting steps
Attach normals to vertices
Place lights in scene
Choose lighting model
Define material properties
OpenGL: Normals
Compute normals of
polygons using cross
product formula
But how to handle
shared edges and
vertices?
Average all normals at
shared vertices for smooth
shading
Dont average where you
want to preserve sharp
creases/folds (flat
shading)
FromRed book
3
Example: Vertex normal handling
Sharp edges maintained
(no averaging)
Adjacent vertices averaged
OpenGL: Defining Lights
glLight(light, pname, param)
light: Which light (GL_LIGHT0, GL_LIGHT1,
etc.)
pname: Which characteristic
Position
Specular, diffuse, ambient color
Spotlight direction, cutoff angle, etc.
Distance attenuation
param: Value(s) of pname
Transformed by modelviewmatrix like
geometric primitives
4
OpenGL lights: Example (from Red book)
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
Means infinite distance away
= Directional light
Consider two parallel lines l = (a, b, c)
T
and
l
0
= (a, b, c
0
)
T
The intersection of these two lines is given by
l l
0
= (d, e, 0)
T
This is not a finite point on the plane, but rather an
ideal point, or a point at infinity
For example, the lines x = 1 and x = 2 are
l = (-1, 0, 1)
T
and l
0
= (-1, 0, 2)
T
,
respectively, and their intersection is (0, 1, 0)
T
This is the point at infinity in the direction of the Y-axis
The Intersection of Parallel Lines
5
OpenGL: Lighting Model
To enable lighting
glEnable(GL_LIGHTING)
Set lighting model:
glLightModel()
Global ambient light:
GL_LIGHT_MODEL_AMBIENT
Local or infinite viewpoint:
GL_LIGHT_MODEL_LOCAL_VIEWER
fromWoo et al.
(a)
(b)
(c)
(a) One-sided lighting of cutaway view
(b) Two-sided lighting, same materials
(c) Two-sided lighting, different materials
OpenGL: Material properties
Applies to subsequent vertices:
glMaterial(face, pname, param)
face: Which face(s) to apply properties to
(GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK)
pname: Which characteristic
Ambient color
Diffuse color
Specular color
Shininess
param: Value(s) of pname
6
OpenGL materials: Example
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat mat_diffuse[] = { 0.1, 0.0, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
Specular highlight size: Relationship to
specular exponent
fromAkenine-Moller &Haines
7
Example: Material properties
No ambient light
Grey ambient light
Blue diffuse
material
Low shininess,
white specular light High shininess
fromWoo et al.
Lighting a point in color
Let s(l) be color of light l
Sum over all lights l for each color
channel (clamp overflow to [0, 1]):
fromHill
componentwise vector product
8
Shading methods: Notes
Flat: Compute c
total
at one vertex per polygon, use
same value for every pixel in polygon
Gouraud: Compute different c
total
at each vertex of
a polygon, interpolate to interior pixels
Different vertex colors because n (if averaging), l, v, r are
different at each vertex
Phong: Interpolate normals from
polygon vertices to interior, recompute
c
total
at each pixel
Interpolation changes length of normals, so
be sure to normalize them to unit length
before computing c
total
fromHill
Atmospheric effects
Scenes are generally assumed to be
imaged in a vacuumi.e., no
interaction with light between source
and objects
However...particles (water droplets,
dust, etc.) in air can have important
effects
Extinction: Fog makes more distant
objects dimmer
9
Example: Fog
Without fog
With fog
Objects are
blended with white-colored fog
fromAkenine-Moller &Haines
OpenGL Fog
Attenuation: Fog transparency f (in [0, 1] range) as
function of depth
Exponential: f = e
z
Fog color c
f
: As object color drops off, what to blend in
Final color for a pixel with unfogged color c
i
:
c = fc
i
+ (1 f)c
f
Gray, exponential fog ( = 0.35)
code
fromWoo et al.

You might also like