Lighting
Calculating the color of objects
•
The incident light
–
–
–
•
Direction
Color
The object
–
•
Position of light source
Reflectance
Viewer
–
Position
2
Overview
Illumination: how to calculate the colour?
Shading: how to colour the whole surface?
3
Illumination
•
Simple 3 parameter model
–
The sum of 3 illumination terms:
•
•
•
Ambient : 'background' illumination
Specular : bright, shiny reflections
Diffuse : non-shiny illumination and shadows
+
=
+
Ambient
Diffuse
Specular
(colour)
(directional)
(highlights)
R
4
c
Ambient Lighting
•
•
•
Light from the environment
–
–
–
Light reflected or scattered from other objects
Coming uniformly from all directions
and then reflected equally to all directions
Ex:
–
Backlighting in a room has a large ambient component, since most
of the light that bounced off many surfaces
Example: sphere
Result: globally uniform colour for object
I = kaIa
I = resulting intensity
Ia = light intensity
Object
ka = reflectance
5
Diffuse Lighting
•
•
Light reflected to all directions
–
–
–
the light that comes from one direction
considers the angle of incidence of light on surface
scattered equally in all directions, no matter where the eye is located
Result: lighting varies over surface with orientation to light
Example: sphere
(lit from left)
Infinite point
I = Ip kd cosθ
Ip : Light Intensity
N
light source
Ln
V
θ
θ : the angle between the normal vector and
direction toward the light
Object
No dependence on camera
angle!
kd : diffuse reflectivity
6
Specular Lighting
•
•
Direct reflections of light source off shiny object
–
specular intensity n = shiny reflectance of object
Result: specular highlight on object
I = Ip ks cosnα
Infinite point
N
light source
R (Reflection)
Ip: light intensity
I : output color
Ln
θ
θ
α
V
R : reflection vector
V : direction towards the camera
α : angle between R and V
No dependence on object colour
Object
7
Specular Light
•
Specular light with different n values
8
Combined Lighting Models
•
Summing it altogether : Phong Illumination Model
n
Iλ = Ia ka + Ip (kd cosθ + ks cos α )
+
=
+
Ambient
Diffuse
Specular
(colour)
(directional)
(highlights)
R
c
9
When you implement it …
•
Use dot product of the vectors instead of calculating the angles
Iλ = Ia ka + Ip (kd N• L + ks (V• R)n)
V : Vector from the surface to the viewer
N : Normal vector at the colored point
R : Normalized reflection vector
L : Normalized vector from
the coloured point
towards the light source
Infinite point
N
light source
R (Reflection)
Ln
θ
θ
α
V
Object
10
Phong Principles
•
Lighting simulates how objects reflect light
–
–
–
material composition of object
light’s color and position
global lighting parameters
•
•
•
ambient light
two sided lighting
available in both color index
and RGBA mode
11
Types of Shading
•
There are several well-known / commonly-used shading methods
–
–
–
Flat shading
Gouraud shading
Phong shading
Shading Comparison
How OpenGL Simulates Lights
•
Phong lighting model
–
•
Computed at vertices
Lighting contributors
–
–
–
Surface material properties
Light properties
Lighting model properties
14
Adding Lighting to Your Scene
•
These are the steps required to add lighting to a scene:
1.
2.
3.
4.
Define normals for all vertices of all objects
Create, select, and position one or more lights
Create and select a lighting model
Define material properties of the objects in the scene
Surface Normals
•
Normals define how a surface reflects light
–
–
–
glNormal3f(x, y, z)
Current normal is used to
compute vertex’s color
Use unit normals for proper lighting
scaling affects a normal’s length
glEnable(GL_NORMALIZE)
or
glEnable(GL_RESCALE_NORMAL)
16
Material Properties
•
Define the surface properties of a primitive
glMaterialfv(face, property, value);
–
face: separate materials for front and back by specifying either GL_FRONT or GL_BACK,
or for both faces simultaneously using GL_FRONT_AND_BACK.
–
The OpenGL material properties are:
GL_DIFFUSE
Base color
Khuyếch tán
GL_SPECULAR
Highlight Color
Phản chiếu
GL_AMBIENT
Low-light Color
Xung quanh
GL_EMISSION
Glow Color
Phát sáng
GL_SHININESS
Surface Smoothness
Độ bóng
17
Material Properties Example
float mat_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
float mat_shininess[] = { 50.0f };
gl.glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
gl.glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
gl.glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
18
19
Light Material Demo
21
Light Properties
•
OpenGL approximates light and lighting as if light can be broken into red, green,
and blue components.
–
•
Thus, the color of a light source is characterized by the amounts of red, green, and blue light
it emits
glLightfv(light, property, value);
–
–
light specifies which light
•
multiple lights, starting with GL_LIGHT0
glGetIntegerv(GL_MAX_LIGHTS, n);
property
•
•
•
colors
position and type
attenuation
22
Light Sources (cont.)
•
Light color properties
–
–
–
GL_AMBIENT
GL_DIFFUSE
GL_SPECULAR
float light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
float light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
float light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
float 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);
23
24
Turning on the Lights
•
Flip each light’s switch
–
•
glEnable( GL_LIGHTn );
Turn on the power
–
glEnable( GL_LIGHTING );
25
Types of Lights
•
OpenGL supports two types of Lights
–
–
•
Local (Point) light sources
Infinite (Directional) light sources
Type of light controlled by w coordinate
w light
= 0source
Infinite
Local
w≠0
Light directed along ( x
Local Light positioned at
float light_position[] = { 1.0f, 1.0f, 1.0f, 1.0f };
gl.glLightfv(GL_LIGHT0, GL_POSITION,light_position);
(xw
y
Infinite light source
float light_position[] = { 1.0f, 1.0f, 1.0f, 0.0f };
gl.glLightfv(GL_LIGHT0, GL_POSITION, light_position);
26
z)
y
w
z
w
)