0% found this document useful (0 votes)
13 views25 pages

Lighting

Uploaded by

evan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

Lighting

Uploaded by

evan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

✬ ✩

CPSC 3710 Computer Graphics University of Lethbridge

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

• In “real world”, there can be many light sources.


• Some objects can emit light.
• Other objects can absorb, scatter, and/or reflect light.
• What we see is how much light is scattered and reflected to the viewer
from a particular point.
• When light bounces off a surface, it can then interact with something
else.
• Need to trace light recursively.
• Computationally intensive.

✫ ✪
Lighting 2 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Local Lighting

• To simplify calculations, we compute local lighting.


• For each position and for each light source, compute amount of light
arriving at the viewer.
• Consider only light directly from light sources.

✫ ✪
Lighting 3 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Surfaces

There are three main types of surfaces (can be a mixture of these):


• Specular: most of the light is reflected or scattered in a narrow range of
angles. Appears shiny.
• Diffuse: scattered in all directions. Appears matte or flat.
• Translucent: some light can pass through the surface. Can also bend
light (refraction).

✫ ✪
Lighting 4 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Light Sources

• Light is not simply an intensity. It is an intensity function depending on


wavelength (colour).
• We model a light source with a three component intensity

I = (Ir , Ig , Ib )

representing the intensity of the RGB components of the light.


• There may also be a direction.
• Four basic types: ambient, point, spotlights, distant light.

✫ ✪
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:

Ia = (Iar , Iag , Iab )

• Each point in the scene receives the same ambient light.

✫ ✪
Lighting 6 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Point Light Sources

• An ideal point source emits light equally in all direction.


• It is located at a point p0 :

I(p0 ) = (Ir (p0 ), Ig (p0 ), Ib (p0 ))

• 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

• It is simple but may not be realistic.


• Often bright or dark, but not smooth. Larger light sources can smooth
out shadows, for example.

✫ ✪
Lighting 7 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Spotlights

• Light is projected in a cone. The width of the cone is determined by an


angle.
• Usually light is more concentrated at the center of the cone, and
decreases moving away from center.
• If ~u is unit vector for the direction of the spotlight, and ~v is the unit
vector from spotlight to object, then θ = cos−1 (~u · ~v ) can be used to
determine intensity received.

✫ ✪
Lighting 8 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Distant Light Sources

• We often need to compute a vector from light source to a point.


• If a light source is very far away, this vector can be considered constant
for all points.
• Direction is simply represented as a vector (same for all points).

✫ ✪
Lighting 9 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Phong Lighting Model

• Four vectors are needed to compute the colour of each point p:


– ~n: normal vector to the surface at point p
– ~v : vector from point p to the viewer (center of projection for
perspective viewing)
– ~l: vector from point p to the light source (assuming point light source)
– ~r: direction of a perfectly reflected light from ~l (computed from ~n and
~l)

• All vectors are normalized to have length 1.


• Each light source has three components: ambient, diffuse, and specular,
each with RGB components.

✫ ✪
Lighting 10 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

• For each light source, this can be specified as a matrix:


 
Lra Lga Lba
 
L = Lrd Lgd Lbd 


Lrs Lgs Lbs

• We can use matrices or three different vectors (ambient, diffuse,


specular) in implementation.
• We can also use 4-component RGBA colours.

✫ ✪
Lighting 11 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Phong Lighting Model

• 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

• We can also use 4-component RGBA colours.


• The light seen for each component is simply the corresponding light
source multiplied by the reflection value.
• The amount of light seen is the combination of the three components.
For example, for the red component:
Ir = Rra · Lra + Rrd · Lrd + Rrs · Lrs + Iar

✫ ✪
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

• Ideally, diffuse reflector scatters light equally in all directions.


• Also called Lambertian surfaces.
• How much light is scattered depends on the angle between the surface
and light source: brightest if it is perpendicular, darkest if it is parallel:

Rd = kd · cos θ = kd (~l · ~n)

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

• If we wish to incorporate the distance to the light: divide by a + bd + cd2

✫ ✪
for some constants a, b, c and distance d.

Lighting 15 – 25 Howard Cheng


✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

• 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

• Allows for “shininess”


• Amount of light reflected depends on how close we are to the reflected
angle.
• Drops off quickly as we move further.
Is = ks · Ls cosα φ

• ks is property of the material


• α controls how quickly it drops off (shininess). A value of 100–500
correspond to most metallic surfaces.
• φ: angle between ~r and ~v , so
cos φ = ~r · ~v

• Note: if the dot product is negative, Is = 0 because the light is on the


✫ other side of the surface ✪
Lighting 17 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

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 18 – 25 Howard Cheng


✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

then ~v and ~l can be assumed to be constant on entire surface.

✫ ✪
Lighting 19 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Normal Vectors Computations

• Sometimes we can just specify the normals by hand.


• If we have a triangle specified by p1 , p2 , p3 in counterclockwise order,
then
~n′ = (p2 − p1 ) × (p3 − p1 )
and
~n′
~n = ′
k~n k
• How does ~n gets transformed by model-view matrix? If M is the
model-view matrix (only the top-left 3 × 3 portion), then transform ~n by
M · ~n does not work (e.g. scaling)!
• Need to transform ~n by the Normal matrix:
N = (M −1 )T
(see textbook for the math behind this)
✫ ✪
Lighting 20 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

Gourad vs Phong Shading

• Gourand Shading: determine the colours at each vertex. The fragments


inside the primitive are interpolated from the vertices.
• Can be done in vertex shader.
• Phong Shading: determine the normal vectors at each vertex. The
fragments interpolate the normal vectors and compute colours.
• Can be done in fragment shader.
• If a surface is flat and all vertices have same normal vector, these two
approaches are similar (not exactly the same)
• Otherwise, Phong shading produces more realistic results.

✫ ✪
Lighting 21 – 25 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge

OpenGL Implementation

• Represent all properties of light and surfaces (Ia , Id , Is , ka , kd , ks as a


vector of RGB components (can also add alpha).
• Specify light position as a 4-diemsnional vector.
• Many libraries will allow componentwise vector multiplication:

Ia ∗ ka = (Iar kar , Iag kag , Iab kab )

• In our matrix/vector library:


– normalize scales a vector to unit length
– dot computes dot product
– cross computes cross product
– Normal computes the normal matrix

✫ ✪
• 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

• Ray tracing is a technique to perform global shading computation.


• For each pixel, shoot a ray towards the centre of projection and see what
is the first object it hits. Then bounce it towards a light source (stength
adjusted by angles)
• If the object is reflective, recursively trace the reflected ray (up to some
limit)
• More realistic scenes, can handle shadows and reflections.
• Computationally more intensive.

✫ ✪
Lighting 25 – 25 Howard Cheng

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy