CSC 706 Computer GraphicsCSC 706 Computer Graphics
LightLight
Definitions
Illumination: the transport of energy from light sources to surfaces & points– Note: includes direct and indirect illuminationLighting: the process of computing the luminous intensity (i.e., outgoing light) at a particular 3-D point, usually on a surfaceShading: the process of assigning colors to pixels
Components of Illumination
Two components of illumination: light sources and surface propertiesLight sources (or emitters)– Spectrum of emittance (i.e., color of the light)– Geometric attributes
PositionDirectionShape
– Directional attenuation
Components of Illumination
Surface properties– Reflectance spectrum (i.e., color of the surface)– Geometric attributes
PositionOrientationMicro-structure
Common simplifications in interactive graphics– Only direct illumination from emitters to surfaces– Simplify geometry of emitters to trivial cases
LightingThree vectors are needed to compute theintensity of reflected light
– m: normal vector at a surface point
– v: vector from a surface point to the viewer
– s: vector from a surface point to the light
LightingTwo types of lights in scenes– point light sources
– ambient light• Light is
– absorbed, reflected, transmitted
• Reflected light– diffuse
– specular
Ambient Light Sources
Objects not directly lit are typically still visible– E.g., the ceiling in this room, undersides of desks
This is the result of indirect illumination from emitters, bouncing off intermediate surfacesToo expensive to calculate (in real time), so we use a hack called an ambient light source– No spatial or directional characteristics;
illuminates all surfaces equally– Amount reflected depends on surface properties
Ambient LightUniform background glow– the amount of ambient light reflected depends on
the material of the surface
tcoefficien reflectionambient theis where, aaaII ρρ=
0.7 0.5, ,3.0 ,0.1 ,0=aρ
Ambient Light Sources
For each sampled wavelength, the ambient light reflected from a surface depends on– The surface properties, kambient
– The intensity of the ambient light source (constant for all points on all surfaces )
Ireflected = kambient Iambient
Ambient Light Sources
A scene lit only with an ambient light source:
Directional Light Sources
For a directional light source we make the simplifying assumption that all rays of light from the source are parallel– As if the source were infinitely far away
from the surfaces in the scene– A good approximation to sunlight
The direction from a surface to the light source is important in lighting the surfaceWith a directional light source, this direction is constant for all surfaces in the scene
Directional Light Sources
The same scene lit with a directional and an ambient light source
Point Light Sources
A point light source emits light equally in all directions from a single point The direction to the light from a point on a surface thus differs for different points:– So we need to calculate a
normalized vector to the light source for every point we light:
p
l
Point Light Sources
Using an ambient and a point light source:
Other Light Sources
Spotlights are point sources whose intensity falls off directionally. – Requires color, point
direction, falloffparameters
– Supported by OpenGL
Other Light Sources
Area light sources define a 2-D emissive surface (usually a disc or polygon)– Good example: fluorescent light panels
– Capable of generating soft shadows (why? )
Ideal diffuse reflection– An ideal diffuse reflector, at the microscopic level,
is a very rough surface (real-world example: chalk) – Because of these microscopic variations, an
incoming ray of light is equally likely to be reflected in any direction over the hemisphere:
– What does the reflected intensity depend on?
The Physics of Reflection
Diffuse reflectionReflected intensity does not depend on thedirection of the viewer– no contribution from the surface point to viewer
vectorIncoming light does depend on the directionof the light– proportional to the area subtended by the facet
Lamberts Law: intensity relationship betweenlight source and surface orientation
Diffuse reflectionLight to surface point vector parallel ornearly parallel to surface normal– little variation in reflected intensity
As light to surface point vector vector isperpendicular or nearly perpendicular to thesurface normal– small amount of reflection
Model with the cosine– dot product between s and m
Lambert’s Cosine Law
Ideal diffuse surfaces reflect according to Lambert’s cosine law:
The energy reflected by a small portion of a surface from a light source in a given direction is proportional to the cosine of the angle between that direction and the surface normal
These are often called Lambertian surfacesNote that the reflected intensity is independent of the viewing direction, but does depend on the surface orientation with regard to the light source
Lambert’s Law
Computing Diffuse Reflection
The angle between the surface normal and the incoming light is the angle of incidence:
Idiffuse = kd Ilight cos θIn practice we use vector arithmetic:
Idiffuse = kd Ilight (n • l)
nl
θ
Diffuse Lighting Examples
We need only consider angles from 0° to 90° (Why?)A Lambertian sphere seen at several different lighting angles:
Diffuse reflectionLambert’s model– diffuse reflection
constant reflective diffuse theis where
0 ,MAX
d
dsd msmsII
ρ
ρ
⋅=
1 ,8.0 ,6.0 ,4.0 ,2.0 ,0=dρ
Specular Reflection
Shiny surfaces exhibit specular reflection– Polished metal– Glossy car finish
A light shining on a specular surface causes a bright spot known as a specular highlightWhere these highlights appear is a function of the viewer’s position, so specular reflectance is view-dependent
The Optics of Reflection
Reflection follows 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:
θ(l)ight = θ(r)eflection
Non-Ideal Specular ReflectanceSnell’s law applies to perfect mirror-like surfaces, but aside from mirrors (and chrome) few surfaces exhibit perfect specularityIn general, we expect most reflected light to travel in direction predicted by Snell’s LawBut because of microscopic surface variations, some light may be reflected in a direction slightly off the ideal reflected rayAs the angle from the ideal reflected ray increases, we expect less light to be reflected
Non-Ideal Specular Reflectance: An Empirical Approximation
An illustration of this angular falloff:
How might we model this falloff?
Phong Lighting
The most common lighting model in computer graphics was suggested by Phong:
( ) shinynlightsspecular IkI φcos=
The nshiny term is a purelyempirical constant that varies the rate of falloffThough this model has no physical basis, it works (sort of) in practice
v
Specular reflectionLighting highlights for shiny surfaces– Phong model
• simple, reasonable results, OpenGL support• Amount of reflected light is greatest when the
reflection angle equals the incident angle• reflected light falls off and reflection differes
Specular reflection– r depends on s and m
– specular light falls off as the angle between r andv increases
– Phong models this drop-off as a cosine of theangle raised to a power (1 - 200)
constant reflectivespecular theis where
0 ,MAX
s
sssp vrvrII
ρ
ρ
⋅=
( ) mm
mssr 22 - ⋅+=
Phong Lighting: The nshiny Term
This diagram shows how the Phong reflectance term drops off with divergence of the viewing angle from the ideal reflected ray:
What does this term control, visually?
Calculating Phong Lighting
The cos term of Phong lighting can be computed using vector arithmetic:
– V is the unit vector towards the viewer– R is the ideal reflectance direction
An aside: we can efficiently calculate R
( ) shinynlightsspecular RVIkI ˆˆ ⋅=
( )( ) LNLNR ˆˆˆˆ2ˆ −⋅=
Calculating The R Vector
This is illustrated below:
( )( ) LNLNR ˆˆˆˆ2ˆ −⋅=
( )( )NLNLR ˆˆˆ2ˆˆ ⋅=+
Phong Examples
These spheres illustrate the Phong model as L and nshiny are varied:
Specular reflection
200 25, 9, 6, 3, f0.75 ,5.0 ,25.0
==dρ
Total LightAdd diffuse, specular, and ambient terms tocompute the total amount of light that reachesthe eye from a point P
⋅+
⋅+=
mhmhI
msmsIII sspddaa ,0max,0max ρρρ
ColorsOpenGL deals with colors individually– computes red, green, and blue components
separately)phong()lambert( sprsprdrdrararr IIII ρρρ ++=
)phong()lambert( spgspgdgdgagagg IIII ρρρ ++=
)phong()lambert( spbspbdbdbababb IIII ρρρ ++=
⋅=
msms,0max lambert
⋅=
mhmh,0max phong
40
41
OpenGL Lighting
• Making a light source– maximum of 8 light sources
• GL_LIGHT0, GL_LIGHT1, … GL_LIGHT7– properties
• position• ambient, diffuse, and specular components• distance attenuation• local or remote viewpoint• front and back face lighting• global ambient light
42
OpenGL Lighting
– Position• directional or positional• maintained in my LightInfo struct
• initialized with a single white light
// Lightingstatic int numActiveLights;typedef struct _LightInfo { GLfloat xyz[4]; GLfloat *rgb; int enable;} LightInfo;
// Light informationLightInfo linfo[] = {
{{ 0.0f, 0.0f, 2.0f, 0.0f}, white},};const int MAX_LIGHTS = (sizeof(linfo) / sizeof(linfo[0]));
43
OpenGL Lighting
– Initialize OpenGL with LightInfo
– Tell OpenGL to perform lighting calculations
• for light num
– Bind material properties
glLightfv(G L_LIGHT0 + num, G L_SPECU LAR, dim); glLightfv(G L_LIGHT0 + num, G L_PO SITION, linfo[num].xyz); glLightfv(G L_LIGHT0 + num, G L_D IFFUSE, linfo[num].rgb);
glEnable(GL_LIGHT0 + num);
glEnable(GL_LIGHTING);
glM aterialfv(G L_F R O N T , G L_A M B IE N T , m atA m b); glM aterialfv(G L_F R O N T , G L_D IFFU SE , m atD iff); glM aterialfv(G L_F R O N T , G L_SP E C U L A R , m atSpec); glM aterialfv(G L_F R O N T , G L_E M ISSIO N , m atE m ission); glM aterialf(G L_F R O N T , G L_SH IN IN E S S, 100 .0);
44
OpenGL Lighting
• Front and back face rendering, default
• front face rendering only glCullFace(GL_BACK); glEnable(GL_CULL_FACE);
45
OpenGL Lighting
– movable, directional, light• change position after setting viewpoint
– global ambient light
// Global ambient lightGLfloat modelAmb[4] = {0.7, 0.1, 0.1, 1.0};glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmb);
// C h an ge ligh t an gle , rese t ligh t p o sitio nif (sp in n in g_ ligh t) { ligh t_an g le = (ligh t_an gle + 1 ) % 360 ; lin fo [0].x yz [1 ] = ligh t_d is t * s in (D E G 2 R A D (ligh t_ an gle)); lin fo [0].x yz [2 ] = ligh t_d is t * co s(D E G 2 R A D (ligh t_ an gle)); g lL igh tfv (G L _ L IG H T 0 , G L _P O S IT IO N , lin fo [0 ].x yz);}
46
OpenGL Lighting
• Backface lighting– assign material properties to backfaces
– bind material properties
– enable two sided lighting
// B ackface m aterial properties for all shapesG L float m atA m bB ack[4] = {0 .0 , 0 .0, 0 .2 , 1 .0};G L float m atD iffB ack[4] = {0.0 , 0 .0 , 0 .8 , 1 .0};G L float m atSpecB ack[4] = {0 .0 , 0.0 , 0 .4 , 1 .0};G L float m atE m issionB ack[4] = {0 .0 , 0 .0 , 0 .2 , 1 .0};
// bind backface material propertiesglMaterialfv(GL_BACK, GL_AMBIENT, matAmbBack);glMaterialfv(GL_BACK, GL_DIFFUSE, matDiffBack);glMaterialfv(GL_BACK, GL_SPECULAR, matSpecBack);glMaterialfv(GL_BACK, GL_EMISSION, matEmissionBack);glMaterialf(GL_BACK, GL_SHININESS, 1000.0);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
47
Flat Shading
The simplest approach, flat shading, calculates illumination at a single point for each polygon:
If an object really is faceted, is this accurate?
Is flat shading realistic for faceted object?
No:– For point sources, the direction to light varies
across the facet
– For specular reflectance, direction to eye varies across the facet
Flat Shading
We can refine it a bit by evaluating the Phong lighting model at each pixel of each polygon, but the result is still clearly faceted:To get smoother-looking surfaceswe introduce vertex normals at eachvertex– Usually different from facet normal– Used only for shading– Think of as a better approximation of the real
surface that the polygons approximate
Vertex Normals
Vertex normals may be – Provided with the model– Computed from first principles – Approximated by averaging the normals of the
facets that share the vertex
52
Flat Shading
Gouraud Shading
This is the most common approach– Perform Phong lighting at the vertices– Linearly interpolate the resulting colors over faces
Along edgesAlong scanlines C1
C2
C3
c1 + t1(c2-c1)
c1 + t2(c3-c1)c1 + t1(c2-c1) + t3(c1 + t2(c3-c1)- c1 + t1(c2-c1))
– This is what OpenGL doesDoes this eliminate the facets?
54
Gouraud ShadingGouraud Shading• Gouraud shading
attempts to smooth out the shading across the polygon facets
• Begin by calculating the normal at each vertex
N
55
Gouraud ShadingGouraud Shading• A feasible way to do this
is by averagingaveraging the normals from surrounding facets
• Then apply the reflection model to calculate intensitiesintensities at each vertex
N
56
Gouraud ShadingGouraud Shading• We use linear linear
interpolation interpolation to calculate intensity at edge intersection P
IPRED = (1-α)IP1
RED + αIP2
RED
where P divides P1P2 in the ratio α : 1−α
• Similarly for Q
P4
P2
P1
P3
P Q
57
Gouraud ShadingGouraud Shading• Then we do further
linear interpolation to calculate colour of pixels on scanlinePQ
P2
P1
P3
P Q
58
Gouraud Shading
59
Gouraud Shading Limitations -Specular Highlights
Gouraud Shading Limitations -Specular Highlights
• Gouraud shading gives intensities within a polygon which are a weighted average of the intensities at vertices– a specular highlight at a vertex tends to be
smoothed out over a larger area than it should cover
– a specular highlight in the middle of a polygon will never be shown
Gouraud ShadingArtifacts– Often appears dull, chalky– Lacks accurate specular component
If included, will be averaged over entire polygon
C1
C2
C3
Can’t shade that effect!
61
Phong ShadingPhong Shading• Phong shading has a
similar first step, in that vertex normals are calculated - typically as average of normals of surrounding faces
N
62
Phong ShadingPhong Shading• However rather than
calculate intensity at vertices and then interpolate intensities as we do in Gouraudshading ...
• In Phong shading we interpolate normals at each pixel ...
P4
P2
P1
P3
P Q
N2
N1
N
63
Phong ShadingPhong Shading• ... and apply the
reflection model at each pixel to calculate the intensity - IRED, IGREEN, IBLUE
P4
P2
P1
P3
P Q
N2
N1
N
Phong Shading
Phong shading is not the same as Phong lighting, though they are sometimes mixed up– Phong lighting: the empirical model we’ve been
discussing to calculate illumination at a point on a surface
– Phong shading: linearly interpolating the surface normal across the facet, applying the Phong lighting model at every pixel
Same input as Gouraud shadingUsually very smooth-looking results:But, considerably more expensive
Phong Shading
Linearly interpolate the vertex normals– Compute lighting equations at each pixel– Can use specular component
N1
N2
N3
N4
( ) ( )∑=
⋅+⋅+=
lights
i
n
sdiambientatotalshinyRVkLNkIIkI
#
1
ˆˆˆˆ
Remember: Normals used in diffuse and specular terms
Discontinuity in normal’s rate of change is harder to detect
66
Phong Shading
67
Phong versus Gouraud ShadingPhong versus Gouraud Shading• A major advantage of Phong shading over
Gouraud is that specular highlights tend to be much more accurate– vertex highlight is much sharper– a highlight can occur within a polygon
• Also Mach banding greatly reduced• The cost is a substantial increase in
processing time because reflection model applied per pixel
• But there are limitations to both Gouraudand Phong
Shortcomings of Shading
Polygonal silhouettes remain
Gouraud Phong
69
Gouraud versus Phong
70
Wall Lights
71
Wall Lights with Fewer Polygons
72
Simple Shading -Without Taking Account of Normals
73
Constant or Flat Shading -Each Polygon has Constant
Shade
74
Gouraud Shading
75
Phong Shading
76
Phong Shading with Curved Surfaces
77
Better Illumination Model