Lighting Lighting

Lighting
Lighting
•
Computing the light
•
•
How much
Which colour
arriving
•
•
•
From a point in the scene
To the eye
Complex problem...
1
Lighting: relevant factors
Additional light
absorption
shade
light
Trasmission (with rifraction)
Inner reflection
reflection
eye
Absorption by
the environment
(e.g. fog)
Sub-surface scattering
object
Lighting: relevant factors
Multiple reflections
(indirect Lighting)
light
eye
object
2
Lighting: global VS local
Local lighting
Global lighting
– Only takes into account:
–
–
–
–
–
• Light conditions
– N. lights
– their position
– Their color
• Surface area to lite
– orientation (normal)
– Optical charcateristics
Multiple reflections
shadows
Sub-surface scattering
refraction
...
» E.g, color
Much easier
to do on our
Hardware
– Anything else is ignored
Local lighting
light
reflection
eye
object
3
What can be easily accomplished
•
Local lighting:
• Light reflections on objects
» Using simplified optical properties
• with multiple light sources
» But simplifies: point-sized
•
Global lighting:
• Multiple reflections
» VERY simplified
• Absorption by the environment
» Simplified case (uniform fog)
• Everything else only “ad-hoc"
» Finding ad-hoc algorithms that suit our HW
The 3 factors we take into account
total light
=
ambient
+
reflection
+
emission
For any factor we have the
R, G and B components.
Defined for both the object,
(vertex attributes)
And for each light used
The optical properties of the object,
(usually vertex attributes)
Collected together form the
"material"
OpenGL terminology
Lighting8& Shading
Pagina
4
Component emission
• LEDs, light bulbs...
• Not related to the lights
• Only to the object
• It is only an addittive Component
• costant for R, G and B
• Note: it does not send light to nearby
objects
• It isn’t Global lighting
• To accomplish this, I must introduce a light source
The 3 factors we take into
account
total light
=
ambient
+
reflection
+
emission
5
Component ambient
• It models (very approximately) the light
hitting the object after multiple reflections
• Assumption:
“a small amount of light hits any surface
from every direction"
• Even surfaces in shadow
• Small additive constant
• Does not change with the normal
Component ambient
• Product of:
• “ambient” color of the material ( RM GM BM)
• “ambient” color of the light ( RL GL BL)
• Note: the RGB colors can be different
• product component by component
6
Component ambient
• It models (very approximately) the light
hitting the object after multiple reflections
without
with
The 3 factors we take into account
total light
=
ambient
+
reflection
+
emission
only
Component
ambient
diffuse reflection
+
specular reflection
7
The 4 factors we take into account
total light
=
ambient
+
diffuse reflection
+
specular reflection
+
emission
Component diffuse reflection
• Effect shown by a few materials (e.g.):
• chalk
• wood (almost)
• Very opaque materials(not shiny)
• Also known as
• diffuse reflection
• Lambertian reflection
Johann
Heinrich
Lambert
1728 - 1777
8
Component diffuse reflection
The light hitting a
lambertian surface
is reflected in all
directions (in the
half-sphere) with
the same intensity
Component diffuse reflection
The light hitting a
lambertian surface
is reflected in all
directions (in the
half-sphere) with
the same intensity
9
Component diffuse reflection
The light hitting a
lambertian surface
is reflected in all
directions (in the
half-sphere) with
the same intensity
Component diffuse reflection
• It only depends on:
• Surface orientation
– the "normal”
• Light direction
– Light ray
10
Component diffuse reflection
• It only depends on:
• Surface orientation N
– the "normal"
• Light direction L
– Light ray
I diff = I light diff ⋅ k material diff ⋅ cos θ
R, G, B
(the "color" of the object)
R, G, B
(usually white: 1,1,1)
multiplication Component by Component
Component diffuse reflection
• It only depends on:
• Surface orientation N
– the "normal"
• Light direction L
– Light ray
I diff = I light diff ⋅ k material diff ⋅ cos θ
property of the
"material"
(associated to
the object)
Computer Graphics
11
Component diffuse reflection
• It only depends on:
• Surface orientation N
– the "normal”
• Light direction L
– Light ray
I diff = I light diff ⋅ k material diff ⋅ cos θ
Component diffuse reflection
• It only depends on:
• Surface orientation N
– the "normal”
• Light direction L
Angle between 0⁰ and 90⁰,
otherwise 0,
(object in its own shadow)
– Light ray
I diff = I light diff ⋅ k material diff ⋅ cos θ
= I light diff ⋅ k material diff ⋅ (N̂ ⋅ L̂)
12
Component diffuse reflection
L
L
L
N
N
N
Component
diffuse
small
⍬=70⁰
Component
diffuse
largest
⍬=0⁰
Component
diffuse
large
⍬=35⁰
Component diffuse reflection
L
L
N
N
Component
diffuse
ZERO
⍬=90⁰
Component
diffuse
ZERO
⍬>90⁰
(the surface is in
Its own shadow)
13
Component diffuse reflection
• Properties
– Accurate model of the optical characteristics
of a number of real materials ☺
– But only a small number
– The model is physically coherent ☺
• As an example, energy is conserved
– Very easy to compute ☺
The 4 factors we take into
account
total light
=
ambient
+
diffuse reflection
+
specular reflection
+
emission
14
Component specular reflection
• Specular reflection
• For shiny materials
• With bright reflections
• highlights
without
with
Component specular reflection
• Basic idea:
light is not reflected
by shiny materiale
equally in all directions
15
Component specular reflection
L: light ray
N: normal
R: reflected ray
V: view direction
N
L
θ
θ
R
α
in 3D
V
Component specular reflection
• Phong light model
• by Bui-Tuong Phong, 1975
I spec = I light spec ⋅ k material spec ⋅ cos α
in 3D
16
Component specular reflection
• By using higher exponents for the cosine, we can obtain
brighter and more concentrated reflexes
Component specular reflection
• Phong light model
• by Bui-Tuong Phong, 1975
I spec = I light spec ⋅ k material spec ⋅ cos α
I spec = I light spec ⋅ k material spec ⋅ cos n α
in 3D
Belong to the "material"
(properties of the object)
17
Component specular reflection
• Phong light model
• by Bui-Tuong Phong, 1975
I spec = I light spec ⋅ k material spec ⋅ cos α
I spec = I light spec ⋅ k material spec ⋅ cos n α
= I light spec ⋅ k material spec ⋅ ( Rˆ ⋅ Vˆ ) n
in 3D
Component specular reflection
⋅
n =1
n=5
n = 10
n = 100
18
Component specular reflection
• Blinn-Phong light model:
• Simplification of the Phong light model
• Similar results, different formula:
phong:
blinn-phong:
L
I spec = I light spec ⋅ k material spec ⋅ ( Rˆ ⋅Vˆ ) n
I spec = I light spec ⋅ k material spec ⋅ ( Hˆ ⋅ Nˆ ) n
N
θ θ
H
α
"half-way" vector
R
V
( Lˆ + Vˆ )
H=
Lˆ + Vˆ
Component specular reflection
• Blinn-Phong light model:
• Simplification of the Phong light model
• Similar results, different formula:
phong:
blinn-phong:
I spec = I light spec ⋅ k material spec ⋅ ( Rˆ ⋅Vˆ ) n
I spec = I light spec ⋅ k material spec ⋅ ( Hˆ ⋅ Nˆ ) n
Jim Blinn
19
The 4 factors we take into
account
total light
=
ambient
+
diffuse reflection
+
specular reflection
+
emission
Complete lighting equation
I tot = I light ambient ⋅ k material ambient +
I light diffuse ⋅ k material diffuse ⋅ ( L ⋅ N ) +
I light specular ⋅ k material specular ⋅ ( H ⋅ N ) n +
k material emission
property of the material
property of the light
20
Materials...
Lighting41
Pagina
& Shading
Computer Graphics
Lighting equation: lights
modeling
I tot = I light ambient ⋅ k material ambient +
I light diffuse ⋅ k material diffuse ⋅ ( L ⋅ N ) +
I light specular ⋅ k material specular ⋅ ( H ⋅ N ) n +
k materiale emission
property of the light
( Lˆ + Vˆ )
Lˆ + Vˆ
21
Lights modeling
• How does L change ?
• constant in the scene: "directional" light sources
– Used for light sources very far away, e.g. the sun
• Variable in the scene: "positional" light sources
– Used for light sources nearby, e.g. light bulbs
Lights modeling: positional lights
• The positional lights, have their intensity
dimmed according to the distance
• In theory (according to physics)
intensity = 1 / distance2
 1 

f light attentuati on = 
2 
 c ⋅ dL 
22
Lights modeling: positional lights
• In practice, it leads to light attenuations
that are too quick
• We rather use:
f light attenuation


1

= min 
, 1
2
 c1 + c2 d L + c3 d L 
Lighting equation
I light ambient ⋅ kmaterial ambient +
I tot =
I light diffuse ⋅ kmaterial diffuse ⋅ ( L ⋅ N ) +
⋅ f light attenuatio n
I light specular ⋅ kmaterial specular ⋅ ( H ⋅ N )n
+ kmaterial emission


1

f light attenuation = min 
,
1
2
c
+
c
d
+
c
d
 1 2 L 3 L 
23
Kind of lights
• Kind of lights:
– positional
– directional
– spot-lights
Spotlights
• Defined by three parameters:
Lighting48
Pagina
& Shading
Computer Graphics
24
Lighting equation
I light ambient ⋅ kmaterial ambient +
I tot =
⋅ f light attenuatio n
I light diffuse ⋅ kmaterial diffuse ⋅ ( L ⋅ N ) +
I light specular ⋅ kmaterial specular ⋅ ( H ⋅ N )
n
⋅ f spotlight effect
+ kmaterial emission


1

flightattenuation = min
,
1
2
c
+
c
d
+
c
d
 1 2 L 3 L 
f spotlight effect = f (L, spot direction , spotcutoff Angle , spotbeam width )
Lighting49
Pagina
& Shading
Computer Graphics
The 4 factors we take into
account
total light
=
ambient
+
diffuse reflection
+
specular reflection
+
emission
25
properties of the light
properties of the material
geometric data
Lighting equation
I light ambient ⋅ k material ambient +
I tot =
⋅ f light attenuation
I luight diffuse ⋅ k material diffuse ⋅ ( Lˆ ⋅ Nˆ ) +
I light specular ⋅ k material specular ⋅ ( Hˆ ⋅ Nˆ )
⋅ f spotlight effect
n
+ k material emission


1
f light attenuation = min 
, 1
2
 c1 + c2 d L + c3 d L 
Normal of a triangle
• That is its orientation in space
v1
N^
v0
N = (v1 − v0 ) × (v2 − v0 )
v2
N
Nˆ =
|N|
26
Lighting face by face: flat shading
1.
Initial geometry
2.
For each face,
Compute the normal
3.
Compute lighting
for each normal
Definition
•
Shading:
•
•
Recipe to apply lighting
As an example:
flatshading
1. Apply lighting to each face normal obtaining a color
2. Cover the whole face with this color
27
Flat shading: problem
When we approximate curved surfaces with
triangles and apply flat shading the result
is:
– Edges very visible on curved surfaces
bad
artifact!
Does not look like a sphere
Flat shading: problem
• Other example:
Lighting56
Pagina
& Shading
Computer Graphics
28
Flat shading: problem
• Increasing the
number of faces,
makes the problem
less visible
>10.000 faces,
and we still see
the artifact edges
Flat shading: problem
the Mach-band optical effect
The contrast between different
uniform areas catches our eye.
(even when areas are a lot,
and their difference is small).
The brain increases the contrast
between uniform areas.
This artifact is difficult to correct.
29
Idea
• Use color interpolation within a face
Idea
• Interpolation between vertex attributes
• Compute lighting (color) for each vertex
• Interpolate the colors in the triangle
To apply the lighting, I need to have the normal!
The normal is defined for a face, not for a vertex
Computer Graphics
30
Vertex normals
v1
Normal of a triangle:
v1×v2
v2
Normal of a vertex
Shared by n triangles:
N̂ v
N = Nˆ1 + Nˆ 2 + ...+ Nˆ n
N̂1
N̂ 6
N̂ 2
N
Nˆ =
|N|
N̂ 5
N̂ 3
N̂ 4
Gouraud shading
• Results:
Lighting62
Pagina
& Shading
Computer Graphics
31
How to improve the result
• Rather than interpolating the color after the lighting, we
interpolate the normal before of the lighting!
• Be careful:
interpolating two normal vectors, the result is not always
a normal:
• need to normalize after interpolation
How to improve the result
• Rather than interpolating the color after the lighting, we
*
interpolate the normal before of the lighting!
"Phong" Shading
by BuiBui-Tuong Phong , 1973
1- Interpolate the normal in the face
2- Normalize
3- Compute lighting
* Do not confuse Phong Shading with the Phong Lighting Model
32
Gouraud vs Phong shading
•
Goraud Shading - lighting per vertex much less computationally
expensive:
Compute the lighting once per vertex!
•
Phong Shading - lighting per fragment offers better results
Especially suited for bright and small highlights (high shininess)
Flat shading
Goraund shading
Phong Shading
Gouraud and Phong shading
flat shading
Goraud shading
(Phong shading is similar)
33
Gouraud and Phong shading
• Goraud and Phong suited for curved
surfaces
– They hide the artifact edges
– Also hide the REAL edges
• Solution: duplicate the vertices
OpenGL lighting
• Enable lighting:
glEnable(GL_LIGHTING);
• nb. Current color (glColor3f) is now ignored
• Color is now specified by the material.
• We need to specify lights, materials and
normals
Computer Graphics
34
Normals
• We set the “current normal". Similarly to
colors:
glNormal3d(x,y,z);
The fourth coordinate is always ZERO!
(these are vectors)
Are normals preserved in the
Transform?
• Only when the modeling-view
transformations are rigid
modeling-view = V ‧ M
rotations,
translations
(always rigid)
rotations,
translations
and maybe scaling
(not always rigid)
Lighting70
Pagina
& Shading
Computer Graphics
35
Are normals preserved in the Transform?
• Note: if the ModelView includes scaling we need to
normalize the normals before Lighting
• Tell OpenGL to normalize:
glEnable(GL_NORMALIZE);
or not to do it: (default)
glDisable(GL_NORMALIZE);
Normals as attributes
glBegin(GL_TRIANGLES);
glNormal3fv( n );
glVertex3fv( v0 );
glVertex3fv( v1 );
glVertex3fv( v2 );
glBegin(GL_END);
Gouraud shading
(or maybe Phong)
flat shading !
glBegin(GL_TRIANGLES);
glNormal3fv( n0 );
glVertex3fv( v0 );
glNormal3fv( n1 );
glVertex3fv( v1 );
glNormal3fv( n2 );
glVertex3fv( v2 );
glBegin(GL_END);
36
Normals as attributes
• Shortcut: when we set
glShadeModel(GL_FLAT);
The attributes are not interpolated, but are constant in a
face (useful for triangle strip and triangle fan), until we
change back to
glShadeModel(GL_SMOOTH);
OpenGL: lights
• We have N lights available
• ambient + diffuse + specular
• How many ?
• Dependent on the specific OpenGL
implementation
• The OpenGL standard requires at least 8
• The exact number is the value of the constant
GL_MAX_LIGHT
37
OpenGL: lights
• Each light can be on or off
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
...
• where GL_LIGHT0, GL_LIGHT1 etc are costants
– note: GL_LIGHTk is GL_LIGHT0+k. useful for the for
• By default, the light 0 is the only one on
OpenGL: lights
• For each light, we set the colors
glLightfv(GL_LIGHT0, GL_DIFFUSE, v);
glLightfv(GL_LIGHT0, GL_AMBIENT, v);
glLightfv(GL_LIGHT0, GL_SPECULAR, v);
38
OpenGL: lights
• For each light, we can also set:
• The spotlight effect:
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,v);
glLightf (GL_LIGHT0,GL_SPOT_CUTOFF,v);
glLightf (GL_LIGHT0,GL_SPOT_EXPONENT,v);
default:
(0,0,-1)
180
0.0
– distance attenuation:
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,a);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,b);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,c);
default:
1
0
0
OpenGL: lights
• For each light, we set the position:
glLightfv(GL_LIGHT0,GL_POSITION,v);
– If it is a positional light,
v = {x,y,z,1}
– If it is a directional light, (“infinite distance")
v = {x,y,z,0}
– NB. Homogeneous coordinates
39
OpenGL: lights
• For each light, we set the position :
glLightfv(GL_LIGHT0,GL_POSITION,v);
• Note: the position of the lights is
multiplied by the MODEL_VIEW matrix
As an example: how to create an headlight?
properties of the light
properties of the material
geometric data
Lighting equation
I light ambient ⋅ k material ambient +
I tot =
I luight diffuse ⋅ k material diffuse ⋅ ( Lˆ ⋅ Nˆ ) +
I light specular ⋅ k material specular ⋅ ( Hˆ ⋅ Nˆ ) n
⋅ f light attenuation
⋅ f spotlight effect
+ k material emission


1
f light attenuation = min 
, 1
2
 c1 + c2 d L + c3 d L 
f spotlight effect = f (L, spot direction , spot cutoff Angle , spotbeam width )
40
OpenGL: materials
• Let us set the parameters of the materials:
– the colors:
glMaterialfv(face,
glMaterialfv(face,
glMaterialfv(face,
glMaterialfv(face,
GL_AMBIENT, colorvec);
GL_EMISSION, colorvec);
GL_DIFFUSE, colorvec);
GL_SPECULAR, colorvec);
– The specular exponent:
glMaterialf (face, GL_SHININESS, intval);
shortcut: we can use
GL_AMBIENT_AND_DIFFUSE
asdasdsadasdasd
"GL_FRONT"
To set both colors to the same value
Other option: Color - Material
activate:
glEnable(GL_COLOR_MATERIAL);
use:
glColorMaterial(face, mode);
e.g.: when we set
glColorMaterial(GL_FRONT, GL_DIFFUSE);
The diffuse color of the material will be the
(otherwise ignored) current color
- the one set by glColor3f
41
Two-sided Lighting
activation:
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
use:
glColorMaterial3f(GL_BACK, ... );
glColorMaterial3f(GL_FRONT, ... );
glColorMaterial3f(GL_FRONT_AND_BACK, ... );
42