W09-Lighting and Shading
W09-Lighting and Shading
Diffuse light
Ambient
Light
Spot Light
• Spotlight is a point source that emits light in restricted
set of directions,
– Requires color, point, direction, falloff parameter
– usually direction boundary forms a cone shape.
– Here θ is Cutoff Cone. No light is seen at points lying
outside Cutoff angle.
Intensity at P = I cosε(β)
α d
β
P
Types of Surface
Interaction between light and material can be classified as
• Specular surfaces – Ideal mirror
• Diffuse surfaces – Reflected light is ideally reflected
to all directions uniformly
• Translucent surfaces – Allow some lights to
penetrate the surface (e.g. refraction in glass, water)
Reflection
Interaction between light and material can be classified as
• Specular reflection – Ideal mirror
• Diffuse reflection – Reflected light is ideally
reflected to all directions uniformly
Surface
Ideal Diffuse Reflection
• Ideal diffuse reflectors reflect light according to
Lambert's cosine law.
• Lambert's law determines how much of the incoming
light energy is reflected. The reflected intensity is
independent of the viewing direction.
• But reflected light intensity depends on incident angle
of light.
Ideal Diffuse Reflection
• Suppose light is incident on diffuse surface S.
• m = direction of Normal of the surface s
• v = viewer’s direction
• s = direction of incident light m θ
Snell’s Laws:
– The incoming ray and reflected ray lie in a plane
with the surface normal
– The angle that the reflected ray forms with the
surface normal equals the angle formed by the
incoming ray and the surface normal
Non-ideal Reflectors
• Simple Empirical Model:
– We expect most of the reflected light to travel in the
direction of the ideal ray.
– However, because of microscopic surface variations we
might expect some of the light to be reflected just slightly
offset from the ideal reflected ray.
– As we move farther and farther, in the angular sense, from
the reflected ray we expect to see less light reflected.
Non-ideal Reflectors
• Phong model approximates the fall off of specular reflection.
• The cosine term is maximum when the surface is viewed from
the mirror direction and falls off to 0 when viewed at 90
degrees away from it.
• A scalar (shiny property, q in the figure below, which is the
exponent term, k in the specular reflection formula of slide 20)
more shiny surfaces have
controls the rate of this fall off
Specular reflectance more impact of specular
reflection, lower k
- angle angle
-900 00 900
Phong Model
• Compute the combined impact of ambient light,
diffuse and specular reflection at a point on surface
• Also called Local Illumination model as its main
focus is on the direct impact of light coming from a
source.
• This model has no physical basis, yet it is one of the
most commonly used illumination models in
computer graphics.
Phong Model
• Also models secondary effect of light.
• Consider a point light source p and viewpoint v. What
should be color of light reflected into viewer’s eye
from point Q of the surface?
– Consider diffuse component, specular component of the
incident light and ambient light present in the environment
N
P R
V
L θ θ
φ
Q
Mathematical Calculation of Phong Model
• First we consider, 2 extreme cases of light reflection.
– Diffuse Reflection :
Id α cosθ
= Ip kd cosθ [Using kd instead of previous ρd]
= Ip kd (L.N) L and N should be in same
direction
N
– Specular Reflection: P R
Is α (cosφ )k V
L θ θ
φ
= Ip ks (cosφ )k
= Ip ks (R.V)k Q
phi between R
and V
More specifically,
I = Iaka + Ip [kd max {(L.N),0} + ks max{(R.V)k ,0}]
k should be outside max()
Additional Issues
• When there are n light sources in the scene, their effects are
cumulative: Intensity at Q, sum over multiple sources, if present
I = Iaka + ∑(i=1 to n) Ipi {kd (L.N) + ks (R.V)k }
• The intesnsity of red, green and blue component of reflected
light,
Ir = Iakar + Ip kdr (L.N) + Ip ks (R.V)k
Ig = Iakag + Ip kdg (L.N) + Ip ks (R.V)k
Ib = Iakab + Ip kdb (L.N) + Ip ks (R.V)k
– ks : coefficient for specular component which is same as
the color of light source, not affected by surface color.
How to get vector R ?
• Find a formula to compute R, the reflection of vector
L with respect to normal vector?
R = 2 (L.N)N – L
(See Solved Problem 11.10 in Schaum(2nd edition))
If R is point to outwards,
then L is also point to
outwards
Blinn and Torrence Variation
• Calculation of R is computaionally expensive. So in
phong model the term R.V is somtimes replaced by
N.H , where H is a unit vector that bisect the angle
between L and V.
• angle between N and H measures the falloff of intensity.
• Though calculation of N.H is is computaionally
inexpensive relative to R.V, but N.H is not always equal to
R.V. In that case calculation of specular component will be
approximate.
(See Solved Problem 11.11 in Schaum(2nd edition)))
If L is point to outwards, H is
normalized avg. of L and V.
Effect of the Reflection Coefficients
• Diffuse Reflection Coefficient, kd
• Ambient Reflection Coefficient, ka
• Specular Reflection Coefficient, ks
• Specular Component Exponent Term, k
0.75
Color
• Color is constructed by adding certain amounts of red,
green, and blue light
• Light sources have three “types” of color:
Shading
Flat Shading
Smooth Shading
• Introduce vertex normals at each
vertex
– Used only for shading
– Think of as a better approximation of the real surface that
the polygons approximate
– Finds color value for each point in the polygon individually
• Two types
– Gouraud Shading
– Phong Shading (do not confuse with Phong Lighting Model)
Gouraud Shading
• Most common approach
• Perform Phong lighting at the vertices
• Linearly interpolate the resulting colors over faces
– Along edges
– Along scanline
Gouraud Shading
color3
ytop
y4 color4
Interpolate
color2 - along edges :
colorleft colorx
colorright color_left, color_right
ys - along scanline:
ybott color1 color_x
xleft xright
ys ybott
colorleft color1 color4 color1
y4 ybott
y s ybott
colorright color1 color2 color1
y 2 ybott
x xleft
colorx colorleft colorright colorleft x xright
left
Gouraud Shading
color3
ytop
y4 color4
color2
colorleft ' colorx '
ys colorright
colorleft
ybott color1
xleft xright
x xleft
colorx colorleft colorright colorleft x xright
left
C1
C3
x
x - xleft
mx= mleft + (mright - mleft)
x right - xleft
Calculate the surface normals along the scan line
and the edge using incremental approach
Phong vs Gouraud Shading
• Phong shading is more smooth
• If a highlight does not fall on a vertex, Gouraud shading may
miss it completely, but Phong shading does not.
Phong shading
can capture But, Phong
specular shading is
reflection. (1,2, 3) 6-8 times
computatio
Gouraud shading nally
will be okay on expensive.
4th. (bottom-left)
Texture
• Texture Types
– Bitmap textures: Using some image file
– Procedural textures: Changing pixel intensity in some
controlled fashion without using external image source
P2(0,1) P3(1,1) V2 V3
P4 (1,0) V1 V4
P1(0,0)
T(s,t)
Texture
Adding Texture to Flat Surface
• Texture P and Surface S has same shape
0,1 1,1
Without
= distortion
0,0 1,0
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, 2.0, 1.5); Mapping from P to S.
Reading (glTexCoord2f)
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 3.0, 1.5); and writing (glVertex3f)
glTexCoord2f(1.0, 1.0); glVertex3f(2.0, 3.0, 1.5); have equal change.
Look at first two
glTexCoord2f(1.0, 0.0); glVertex3f(2.0, 2.0, 1.5); coordinates of
glVertex3f
glEnd();
Texture
Adding Texture to Flat Surface
• Texture P and Surface S has same shape
0,1 1,1
= Stretched
0,0 1,0
glBegin(GL_QUADS);
Stretching original
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, 2.0, 1.5); texture.
Reading smaller
glTexCoord2f(0.0, 0.5); glVertex3f(1.0, 3.0, 1.5);
section, writing into
glTexCoord2f(0.5, 0.5); glVertex3f(2.0, 3.0, 1.5); larger section.
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,
GL.GL_REPLACE);
gl.glBindTexture(GL.GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, 2.0, 1.5); OpenGL code isn't
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 3.0, 1.5); required for theory
glTexCoord2f(1.0, 1.0); glVertex3f(2.0, 3.0, 1.5);
glTexCoord2f(1.0, 0.0); glVertex3f(2.0, 2.0, 1.5);
glEnd();
Creating Visual Effect Using Texture
2. Modulating the reflection coefficient
gl.glEnable(GL.GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_MODULATE);
gl.glBindTexture(GL.GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, 2.0, 1.5);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 3.0, 1.5);
glTexCoord2f(1.0, 1.0); glVertex3f(2.0, 3.0, 1.5);
glTexCoord2f(1.0, 0.0); glVertex3f(2.0, 2.0, 1.5);
glEnd();
Creating Visual Effect Using Texture
3. Simulating Roughness by Bump Mapping
– Technique to give surface wrinkled or dimpled appearance
without having to model each individual dimple
Computationally expensive
if calculated on each point.
We can approximate using
ideas from texture.