villanova university - lighting and shadingmdamian/past/graphicsfa10/notes/...reflection, and l, n...
TRANSCRIPT
Lighting and Shading
Reading Assignment: Chapter 6
Is this a plate or a ball?
What color should I set for each pixel?
Lighting and Shading
Physical Reality
As light hits the surface:
– Part is absorbed
– Part is reflected
Visible light includes all colors of the rainbow
Physical Reality
Physically, objects can be said to have the color of the light leaving their surfaces:
If a blue object is illuminated with red light, it will appear as ________.
White Light
Colours Absorbed
Green Light
Our Objective:
Understand and model what light does in a scene.
A Model for Lighting
Follow rays from light source
Only light that reaches the eye is ever seen
– direct light is seen as the color of the light source
– indirect light depends on interaction between material and light
Lighting in Computer Graphics
For Computer graphics we replace viewer with projection plane
Rays which reach COP after passing through viewing plane are actually seen
Color of pixels is determined by our interaction model
Light‐Material Interactions
Specular Diffuse Translucent
Determine whether an object appears red or brown, light or dark, dull or shiny
Reflection of light from a surface can be:
Specular Surfaces
Smooth, shiny, glossy
Mirror‐like
Reflection focused to one direction (angle of incidence = angle of reflection)
Diffuse Surfaces
Rough (grainy, matte)
Light reflected almost uniformly in all directions
Translucent Surfaces
Allow some light to pass through
Glass or water‐like
Light‐Material Interaction Summary
Surfaces may be
– Specular
– Diffuse
– Translucent
A surface is never perfectly diffuse or perfectly translucent or perfectly specular.
May have all these properties!
To model these properties, we must understand how light reflects off each of these surface types.
Basic Illumination Model
Basic Illumination Model
We will consider a basic illumination model which gives reasonably good results and is used in most graphics systems
The important components are:
– Diffuse reflection
– Specular reflection
– Ambient light
Example
The total reflected light from a surface is the sum of the contributions from light sources and reflected light
Ambient Light
Walk around the back of box. No light beams hit it directly. Can you see the back face?
This type of indirect light is called ambient light.
Ambient Light
Like sunlight on a totally clouded day!
Uniform level of light, position‐independent
Tells us how bright an object looks when no light source can directly reach it
Ambient Light
Also called background light
Not created by any light source
A constant lighting from all directions
Contributed by scattered light in a surrounding
When used alone, does not produce very interesting pictures Ambient light only
Ambient Light
To incorporate background light we simply set a
general brightness level Ia for a scene
Different surfaces may reflect different amount of ambient light, based on their reflectance properties. We model this by a constant factor for each surface:
aa Ik ×
RGB Color Considerations
For an RGB colour description each intensity specification is a three element vector
Similarly, the reflectance is given as a vector:
Red, green and blue ambient light intensities are
( )aBaGaRa IIII ,,=
( )aBaGaRa kkkk ,,=
( )aBaBaGaGaRaR IkIkIk ,,
Illumination: Ambient Light
Material Color Light Color
⊗
Hands‐on Session
Run the lightmaterial tutor, and right‐click select “Material Parameters”
Make the light source ambient‐only (set all other material reflectivity coefficients to 0).
Try different color combinations of ambient light and material, and see how the scene reacts to these changes.
Diffuse Reflection
• Light scattered with equal intensity in all directions (ideal diffuse reflection)
• Light from a point is independent on viewing direction (equally bright in all directions)
Intermezzo: Surface Normals
where p0 is the center of the sphere
N
N
Diffuse Reflection
The angle between the incoming light direction and a surface
normal is referred to as the angle of incidence, denoted θ.
L = unit vector to light source
N = unit vector normal to surface
Law of reflection: the angle of incidence equals the angle of reflection, and L, N and R(eflection) directions are co‐planar.
Modeling Diffuse Reflection
The amount of incident light depends on the orientation of the surface relative to the light source direction.
Diffuse Reflection
• The amount of incident light on a surface depends on the angle of incidence:
N
L
d
NL
θ
?
• As θ increases, the brightness of the surface decreases by
____________
Recall: Dot Product
N∙L = |N||L|cos θ
If N and L are unit vectors, then N∙L = cos θ
N
L θ
Diffuse Reflection
If surface has brightness Ι when facing light, it has brightness Ι*cos(θ) when tilted at angle θ.
N
L θN∙L = cos θ
You will see the brightness written as I(N∙L)
(N, L are unit vectors)
Diffuse Reflection
A parameter kd set for each surface determines the fraction of incident light scattered as diffuse reflections from that surface
This parameter is known as the diffuse reflection coefficient or the diffuse reflectivity
kd is assigned a value between 0.0 and 1.0
– 0.0 for dull surface that absorbs almost all light
– 1.0 for shiny surface that reflects almost all light
Diffuse Reflection
Diffuse reflections:
0 if
0 if
0
)(
≤⋅
>⋅
⎪⎩
⎪⎨⎧ ⋅
=LN
LNLNIkI
d
d
Diffuse Reflection
Spheres lit by diffuse (kd) values of
0.0, 0.25, 0.5, 0.75, 1 respectively
Ambient Light vs. Diffuse Reflection
Ambient lighting only With diffuse lighting
Run the lightmaterial tutor
Make the light source diffuse‐only
Check the color of a blue object illuminated with red light
Try different color combinations of diffuse light and material
Change the light position and observe the changes in the brightest spots on the torus
Combine diffuse and ambient light
Hands‐on Session
Diffuse Lighting: What’s Missing?
DiffuseMaterial Color Light Color
Ambient +Diffuse
⊗
Ambient + diffuse reflections produce shaded images that appear three‐dimensional
But the surfaces look dull, somewhat like chalk
What is missing?
Specular Reflection
Intensity depends on where the viewer is!
The white specular highlight is the reflection of white light from the source in the direction of the viewer
Whitespecularhighlight
Specular Reflection
The bright spot that we see on a shiny surface is the result of incident light reflected in a concentrated region around the specularreflection angle
The specular reflection angle equals the angle of the incident light
L = vector to light source N = vector normal to surfaceR = direction of reflected lightV = vector to viewer
Specular Reflection
A perfect mirror reflects light only in the specular‐reflection direction
Other objects exhibit specular reflections over a
finite range of viewing positions around vector R
The Phong Specular Reflection Model
The Phong model sets the intensity of specularreflection as proportional to the angle φbetween the viewing vector and the specularreflection vector:
φαcos××= ss kII
α = shineness exponent
ks = specular reflectivity of material
The Phong Specular Model
φαcos××= ss kII
α = shininess
ks = reflectivity
The shineness α is determined by the type of surface we want to display– Shiny surfaces have a very large value (>100)– Rough surfaces would have a value near 1
The larger the α, the more concentrated the light is around R. For mirrors, α infinity.
The Phong Specular Model
φαcos××= ss kII
α = shininess
ks = reflectivity
Recall that R∙V = cos φ
0or 0 if
0 and 0 if
0.0
)(
≤⋅<⋅
>⋅>⋅
⎪⎩
⎪⎨⎧ ⋅
=LNRV
LNRVRVIkI
s
s
α
Illumination: Specular Lighting
Create shining surface (surface perfectly reflects)
Viewpoint dependent
Ambient +Diffuse +SpecularMaterial Color Light Color
Specular
Specular+Diffuse Reflection Example
Phong Reflection Model Summary
Due to the ambient light, nothing can be entirely black
Mirror reflections are possible
Can be computed very fast (used in VR and games)
Very good approximation of diffuse surfaces
Physically inaccurate
Expressed in terms of vector geometry
Red ambient
Bluish diffuse
Specular Highlight
Putting it All Together
The intensity of light from one point is a sum of the diffuse, specular and ambient components:
sda IIII ++=
+ +
diffuse
specular
shininess
0.2 0.4 0.6 0.8
0.2 0.4 0.6 0.8
1.0
1.0
10 30 60 160 250
specular = 0shininess = 0
diffuse = 0.5specular = 0.5
diffuse = 0.5shininess = 120
Putting it All Together
OpenGL Lighting Functions
OpenGL Lighting Functions
OpenGL provides functions for
– setting up point light sources and spotlights,
– selecting surface reflection coefficients
– choosing values for several parameters in the basic illumination model
Implements the Phong reflection model
Steps in OpenGL Lighting
1. Enable lighting and select shading model
2. Specify normals
3. Specify material properties
4. Specify lights
Enable Lighting Calculations
Lighting calculations are enabled globally in an OpenGL program using
glEnable (GL_LIGHTING);
Once lighting is enabled, glColor*() ignored
Enable Individual Light Sources
Up to eight light sources (GL_MAX_LIGHTS) can be included in a standard OpenGL implementation
– Some implementations may allow more than 8
Each light source is referenced by its identifier: GL_LIGHT0, GL_LIGHT1, ..., GL_LIGHT7
A light source is enabled with:
glEnable (lightName);
Example: glEnable (GL_LIGHT0);
Select the Light Model
Select the light model:
glLightModeli(parameter, GL_TRUE);
Parameter can be:•GL_LIGHT_MODEL_LOCAL_VIEWER – do not use simplifying distant viewer assumption in calculation
•GL_LIGHT_MODEL_TWO_SIDED shades both sides of polygons independently
glLightModel(GL_LIGHT_MODEL_TWO_SIDED, …)
Either shade only the outside of polygons, or both sides
How does OpenGL know what is the outside?
2. Specify Normals
In OpenGL the normal vector is part of the state
Set by glNormal*()– glNormal3f(x, y, z);– glNormal3fv(p);
Usually we want to set the normal to have unit lengthso cosine calculations are correct
– Length can be affected by transformations
– Note that scaling does not preserved length
– glEnable(GL_NORMALIZE) allows for autonormalization at a performance penalty
Intermezzo: Normal for a Triangle
p0
p2
n
normalize n ← n/ |n|
p
Note that right‐hand rule determines outward face.
n = (p2 – p1) ×(p1 - p0 )
p1
Intermezzo: Vector Cross Product
Use the right-hand rule to determine a x b.
Intermezzo: Vector Cross Product
Intermezzo: Unit Vectors i, j, k
Intermezzo: Exercise
Calculate a x b if
a = (3, 0, 2) and b = (4, 1, 8)
Normal for a Sphere
Associate a normal with each vertex:
glBegin(…)
glNormal3f(x,y,z)
glVertex3f(x,y,z)
…
glEnd()
3. Specify Material PropertiesReflectivity coefficients:
Material properties:
The default is to light only front faces
GLfloat mat_ambient[] = {0.2, 0.2, 0.2, 1.0};GLfloat mat_diffuse[] = {1.0, 0.8, 0.0, 1.0};GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};GLfloat mat_shine = 100.0;
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);glMaterialfv(GL_FRONT, GL_SHININESS, mat_shine);
Transparency
Material properties are specified as RGBA values
The A value can be used to make the surface translucent
The default is that all surfaces are opaque regardless of A
4. Specify Light Sources
Properties of a light source such as position, type, color, attenuation, and spotlight effects can be specified using
glLight* (lightName, lightProperty, propertyValue);
Each light source is referenced by its identifier: GL_LIGHT0, GL_LIGHT1, ..., GL_LIGHT7
Light Properties
glLight* (lightName, lightProperty, propertyValue);
lightProperty can be one of:
– GL_POSITION
– GL_AMBIENT
– GL_DIFFUSE
– GL_SPECULAR
– GL_CONSTANT_ATTENUATION
– GL_LINEAR_ATTENUATION
– GL_QUADRATIC_ATTENUATION
– GL_SPOT_DIRECTION
– GL_SPOT_CUTOFF
– GL_SPOT_EXPONENT
Example
Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};
glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glLightv(GL_LIGHT0, GL_POSITION, light0_pos);
Setting the position of a point light source:
Light Types
Two types of lights
– Local (point) light sources that are near the objects
– Light sources at infinity
The type of a light source is independent of the 3d position we assign to a light source. It is determined by a fourth parameter value:
– 1.0 indicates local light source
– 0.0 indicates light source at infinity
Local Light Source
For a local light source, the emitted light radiates in all directions and the position of the light is used in the lighting calculations
– i.e., the light direction changes for every object
– In OpenGL, it is referred to as “Positional Light”
Point (Positional) Light(Emit in all directions)
Infinite Light Source
For a light source at infinity, the emitted light radiates in one direction only and this direction is applied to all objects in the scene
– I.e., the light direction is constant for every object
– In OpenGL, it is referred to as “Directional Light”
Directional Light(Infinitely far away)
Run the lightposition tutor, play with light parameters
Hands‐on Session
Positional light Directional light
Light Source Colors
You can specify three different color properties to a light source
– This is unrealistically flexible compared to real‐world lights which have one color
We can specify the ambient, diffuse, and specular components of a light source with different colors.
Example
GLfloat diffuse0[]={1.0, 0.0, 0.0, 1.0};GLfloat ambient0[]={1.0, 0.0, 0.0, 1.0};GLfloat specular0[]={1.0, 0.0, 0.0, 1.0};Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};
glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glLightv(GL_LIGHT0, GL_POSITION, light0_pos);glLightv(GL_LIGHT0, GL_AMBIENT, ambient0);glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0);glLightv(GL_LIGHT0, GL_SPECULAR, specular0);
Setting the RGBA values for the ambient, diffuse and specular components of a point light source.
Example
Black ambientWhite diffuseWhite specular
Black ambientRed diffuseBlue specular
OpenGL Light Defaults
Light 0 is unique in having a default diffuse and specular setting of fully bright white (1,1,1,1)
– saves us the hassle of setting up lights
All other lights have a default settings of totally dark (0,0,0,1).
All lights also have a default position of (0,0,1,0)
– this would effectively place it as a directional light, facing forward, from just behind the camera
Spotlights
a.k.a Directional Light Sources
Limit the light to a cone‐shaped region of space
Use glLightv to set
– Direction GL_SPOT_DIRECTION
– Cutoff (θ) GL_SPOT_CUTOFF
– Attenuation(α) GL_SPOT_EXPONENT
• Proportional to cosαφ
θ−θ φ
Directional Local Light Sources
Example:GLfloat dirVector [] = {1.0, 0.0, 0.0};
glLightf (GL_LIGHT3, GL_SPOT_DIRECTION, dirVector);
glLightf (GL_LIGHT3, GL_SPOT_CUTOFF, 30.0);
glLightf (GL_LIGHT3, GL_SPOT_EXPONENT, 2.5);
Example
Emissive Light
Used to add a fixed color to surfaces.
Do not add any actual light to the scene.
GLfloat mat_emission[] = {0.0, 0.3, 0.3, 1.0};glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
Emissive Light Example
3D sphere reflecting green emissive light.Effect similar to
ambient light, until light sources are introduced.
Apply a light source with red ambient light, red diffuse light, andwhite specular light.
Same sphere, withno emissive light
Global Ambient Light
Ambient light depends on color of light sources
– A red light in a white room will cause a red ambient term that disappears when the light is turned off
OpenGL also allows a global ambient term that is often helpful for testing:
GLfloat global_ambient[] = {0.1, 0.1, 0.1, 1.0}
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
Light Attenuation in OpenGL
Attenuation:
–
– glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, a);
– glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, b);
– glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, c);
Default OpenGL values:
– a = 1, b = 0, c = 0 (no attenuation with distance)
Applying Transformations
A light source is like an object in the scene and included in the scene description
Geometric transformation matrices and viewing transformation matrices are applied to light positions as applied to other objects
– If you want to fix your light position relative to the objects in your scene, you need to set the light position after you specify geometric and viewing transformations
– If we want the light move as the view point moves, set its position before gluLookAt(...)
Summary
To successfully model lighting effects, must consider:– Ambient light– Diffuse reflections– Specular reflections
Ambient component is independent of the surface orientation and viewer position
Diffuse component is independent of the viewer position but depends on the relative orientation of the light source and the surface
In specular reflection the position of the viewer is also important
Polygonal Shading
Lighting Revisited
Light calculation so far is at vertices
Vertex illumination from a light of intensity I
= ambient + diffuse + specular
Pixel may not fall right on vertex
Shading: calculates color of interior pixels
Ikkk sda )coscos( φθ α++=
Polygonal Shading
Technique to render solid surfaces
Determines how surfaces will be filled
Process for computing the color intensity value for each pixel contained in a polygon
The most common shading techniques are:
– Flat Shading
– Gouraud Shading
– Phong Shading
Shading Techniques
Flat Shading
Gouraud Shading Phong Shading
No Shading
Flat Shading
Simplest, Cheapest, Fastest Shading Method
– Works well for objects really made of flat faces.
– Appearance depends on number of polygons for
curved surface objects.
Fills an entire polygon with one color intensity
This model is only valid (realistic) if:
– The light source is imagined to be at infinity
– The viewer is at infinity
– The polygon is not an approximation to a curved surface
Flat Shading in OpenGL
Specify using
glShadeModel(GL_FLAT);
The color at the first vertex will determine the shade of the whole polygon
Mach Band Effect
Flat shading suffers from “mach band effect”
– human eyes accentuate the discontinuity at the boundary
Smooth Shading
Fix the mach band effect – remove edge discontinuity
Compute lighting for more points on each face
Two popular methods:
– Gouraud shading (used by OpenGL)
– Phong shading (better specular highlight, not in OpenGL)
Gourad Shading
The default shading algorithm used in OpenGL:
glShadeModel(GL_SMOOTH)
Lighting is calculated for each of the polygon vertices
Colors are interpolated for interior pixels
Flat Smooth
Gourad Shading
Per‐vertex lighting calculation
Normal is needed for each vertex
Per‐vertex normal can be computed by averaging the adjacent face normals:
Requires knowledge about
adjacent faces
4
4321 nnnnn
+++=
Gourad Shading
Use normals to compute vertex illumination (color) before the projection transformation
Shade interior pixels:
– color interpolation (normals not needed)
Gouraud Shading
Color Interpolation Algorithm– Interpolation along polygon edges
– Interpolation across polygon surfaces
Given:– Colors c0 ,c1, c2 ,c3
– Coordinates of points colored
c0 ,c1, c2 ,c3 , ca, cb
Determine the colors ca, cb using
linear interpolation.
In‐Class Exercise
Given the coordinates:
P0 = (0, 3)
P1 = (1, 2)
P2 = (0, 1)
and normals n1, n2, and n3, we can use the Phong reflection model to obtain a color ci for each vertex. Let c0 = 60, c1 = 50, and c2 = 20. Find Ca and Cb for a scanline with coordinates
Pa = (0, 1.8)
Pb = (0.8, 1.8).
Ca Cb
Gourad Shading Problem
Lighting in the polygon interior can be inaccurate:
Phong Shading
Instead of interpolation, we calculate lighting for each pixel inside the polygon (per pixel lighting)
Need normals for all the pixels – not provided by user
Phong shading algorithm interpolates the normals and compute lighting for each pixel
Phong Shading
Interpolation Over Normal Vector, NOT Vertex Color:
Slow! Not supported by OpenGL and most graphics hardware.
Polygonal Shading Summary
Gouraud shading
– Set vertex normals
– Calculate colors at vertices
– Interpolate colors across polygon
Must calculate vertex normals!
Must normalize vertex normals to unit length!
Phong Shading
– Creates greater realism than Gouraud shading
– Computationally intense
– Not supported by most graphics hardware