Lighting
Lighting
Lighting
• So far, we assumed that each surface has a solid colour (or a blend). The
colour is completely dependent on the surface.
• In reality, colour depends on both the properties of the surface and the
light source.
• A flat surface does not look uniform throughout generally.
• We now look at how to model lighting effects.
• Realistic modelling of lighting requires knowledge in physics: many
approximations and simplifications in computer graphics
✫ ✪
Lighting 1 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Global Lighting
✫ ✪
Lighting 2 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Local Lighting
✫ ✪
Lighting 3 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Surfaces
✫ ✪
Lighting 4 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Light Sources
I = (Ir , Ig , Ib )
✫ ✪
Lighting 5 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Ambient Light
• There is often “general” lighting (e.g. sun, overhead lights, etc.) that
provide somewhat uniform lighting in the whole scene.
• Modelling each such source can be computationally intensive.
• Instead, we define a general uniform lighting as the ambient light and
apply this uniformly everywhere:
✫ ✪
Lighting 6 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
• For any given point p, the amount of light received from p0 depends on
the distance (squared):
1
i(p, p0 ) = 2
I(p0 )
kp − p0 k
✫ ✪
Lighting 7 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Spotlights
✫ ✪
Lighting 8 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
✫ ✪
Lighting 9 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
✫ ✪
Lighting 10 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
✫ ✪
Lighting 11 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
• Each point also has a reflective value (in [0, 1]) for each type of light and
colour to determine the proportion of light that is reflected:
Rra Rga Rba
R = Rrd Rgd Rbd
Rrs Rgs Rbs
✫ ✪
Iar is an optional global ambient term
Lighting 12 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
• If there are multiple light sources, need to add the contribution from
each light source.
• To simplify presentation, we will remove the r, g, b subscripts
✫ ✪
Lighting 13 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Ambient Reflection
• Each point in the surface has the same reflection coefficient ka ∈ [0, 1].
• I a = k a · La .
• La can be any individual light sources, or global ambient term.
• The RGB components of ka can describe the colour of the surface
(assuming white light)
✫ ✪
Lighting 14 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Diffuse Reflection
where θ is the angle between ~l and ~n, and kd is some constant (property
of surface)
• The intensity from diffuse term is:
Id = kd (~l · ~n) · Ld
✫ ✪
for some constants a, b, c and distance d.
• If the light source is below the surface, this can be negative (set to 0
instead).
✫ ✪
Lighting 16 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Specular Reflection
Blinn-Phong Model
•
Is = ks · Ls (~r · ~v )α
• φ: angle between ~r and ~v .
• ~r has to be computed for each point
• One approximation: use the “halfway vector”:
~ ~v + ~l
H=
k~v + ~lk
~ and increase α (heuristically 4×):
• Replace ~r · ~v by ~n · H
~ 4α
Is = ks · Ls (~n · H)
~ < 0, set Is = 0.
• If ~n · H
✫ ✪
• Why can this be faster? If we assume camera and light are “far away”,
✫ ✪
Lighting 19 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
✫ ✪
Lighting 21 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
OpenGL Implementation
✫ ✪
• Pass them as uniform variables to vertex shader.
Lighting 22 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
OpenGL Implementation
In vertex shader:
position = view * model * vPosition;
gl_position = projection * position;
N = Normal * aNormal;
// aNormal passed in as normal in model space
Make the normal vector and position part of the output variable to the
fragment shader (interpolated).
✫ ✪
Lighting 23 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
OpenGL Implementation
In fragment shader:
L = normalize(lightPosition - position)
V = normalize(-position);
// viewer is at origin
H = normalize(V + L);
Light and surface properties can be passed to fragment shader as uniform
variables from application.
Compute ambient, diffuse and specular terms separately and add them.
✫ ✪
Lighting 24 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Ray Tracing
✫ ✪
Lighting 25 – 25 Howard Cheng