Lighting and Shading
Reading Assignment: Chapter 6
Lighting and Shading
Is this a plate or a ball?
What color should I set for each pixel? 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:
W hite Light Colours Absorbed
t n Ligh Gree
If a blue object is illuminated with red light, it will appear as ______. 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
Determine whether an object appears red or brown, light or dark, dull or shiny
Reflection of light from a surface can be:
Specular Diffuse Translucent 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:
ka × Ia
RGB Color Considerations
For an RGB colour description each intensity specification is a three element vector
Ia = ()IaR , IaG , IaB
Similarly, the reflectance is given as a vector:
ka = ()kaR ,kaG ,kaB
Red, green and blue ambient light intensities are
()kaR IaR ,kaG IaG ,kaB IaB 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
N
N where p0 is the center of the sphere 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:
•As θ increases, the brightness of the surface decreases by L L N θ N ______? d
Recall: Dot Product N
θ N∙L = |N||L|cos θ L
If N and L are unit vectors, then N∙L = cos θ Diffuse Reflection
If surface has brightness Ι when facing light, it has brightness Ι*cos(θ) when tilted at angle θ. N N∙L = cos θ θ L (N, L are unit vectors)
You will see the brightness written as I(N∙L)
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:
⎪⎧kd I(N ⋅ L) if N ⋅ L > 0 Id = ⎨ ⎩⎪ 0 if N ⋅ L ≤ 0
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
Hands‐on Session
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 Diffuse Lighting: What’s Missing?
⊗
Diffuse Ambient + Material Color Light Color 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!
White specular highlight
The white specular highlight is the reflection of white light from the source in the direction of the viewer 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 specular reflection angle
The specular reflection angle equals the angle of the incident light L = vector to light source N = vector normal to surface R = direction of reflected light V = 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 specular reflection as proportional to the angle φ between the viewing vector and the specular reflection vector:
α I s = I × k s × cos φ α = shineness exponent
ks = specular reflectivity of material
The Phong Specular Model
α I s = I × k s × cos φ α = 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
α I s = I × k s × cos φ α = shininess
ks = reflectivity
Recall that R∙V = cos φ ⎧ α if V ⋅ R > 0 and N ⋅ L > 0 ⎪ks I (V ⋅ R) I s = ⎨ ⎩⎪ 0.0 if V ⋅ R < 0 or N ⋅ L ≤ 0
Illumination: Specular Lighting
Ambient + Specular Diffuse + Material Color Light Color Specular
Create shining surface (surface perfectly reflects)
Viewpoint dependent 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 Putting it All Together
The intensity of light from one point is a sum of the diffuse, specular and ambient components:
++
Red Bluish Specular ambient diffuse Highlight
I = I a + I d + I s
Putting it All Together
0.2 0.4 0.6 0.8 1.0 diffuse specular = 0 shininess = 0
0.2 0.4 0.6 0.8 1.0 specular diffuse = 0.5 shininess = 120
10 30 60 160 250 shininess diffuse = 0.5 specular = 0.5 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 length so 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
n = (p2 – p1) ×(p1 - p0 ) n p2 normalize n ← n/ |n| p p1
p0
Note that right‐hand rule determines outward face.
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 Properties
Reflectivity coefficients: 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;
Material properties: 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);
The default is to light only front faces
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_CONSTANT_ATTENUATION –GL_AMBIENT – GL_LINEAR_ATTENUATION – GL_DIFFUSE – GL_QUADRATIC_ATTENUATION –GL_SPECULAR – GL_SPOT_DIRECTION – GL_SPOT_CUTOFF – GL_SPOT_EXPONENT Example
Setting the position of a point light source:
Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};
glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightv(GL_LIGHT0, GL_POSITION, light0_pos);
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 Point (Positional) Light for every object (Emit in all directions) –In OpenGL, it is referred to as “Positional Light”
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 Directional Light as “Directional Light” (Infinitely far away) Hands‐on Session
Run the lightposition tutor, play with light parameters
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
Setting the RGBA values for the ambient, diffuse and specular components of a point light source.
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);
Example
Black ambient White diffuse White specular
Black ambient Red diffuse Blue 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 Apply a light source Same sphere, with green emissive light. with red ambient light, no emissive light Effect similar to red diffuse light, and ambient light, until light white specular light. sources are introduced. 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 α = (k a + k d cos θ + k s cos φ )I
Pixel may not fall right on vertex
Shading: calculates color of interior pixels 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
No Shading Flat Shading
Gouraud Shading Phong 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:
n1 + n 2 + n3 + n 4 n = 4
Requires knowledge about adjacent faces 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 Ca each vertex. Let c0 = 60, c1 = 50, Cb and c2 = 20. Find Ca and Cb for a scanline with coordinates Pa = (0, 1.8) Pb = (0.8, 1.8).
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