Lighting and Shading Updated 03
Lighting and Shading Updated 03
Computer Graphics
Lighting & Shading
• Ray tracing: ray tracing is a rendering technique for generating an image
by tracing the path of light as pixels in an image plane and simulating the
effects of its encounters with virtual objects.
– Follow light rays through a scene
– Accurate, but expensive
• Phong Illumination model (this lecture): Illumination model is used to
calculate the intesitiy of the light that is reflected given point on surface
– Approximate only interaction light, surface, viewer
– Relatively fast , supported in OpenGL
Geometric Ingredients
• Three ingredients
– Normal vector m at point P of the surface
– Vector v from P to the viewers eye
– Vector s from P to the light source
P
Types of Light Sources
• Ambient light: no identifiable source or direction
• Diffuse light - Point: given only by point
• Diffuse light - Direction: given only by direction
• Spot light: from source in direction
– Cut-off angle defines a cone of light
– Attenuation function (brighter in center)
• Light source described by a luminance
– Each color is described separately
– I = [I r I g I b ] T (I for intensity)
– Sometimes calculate generically (applies to r, g, b)
Ambient Light
• Global ambient light
– Independent of light source
– Lights entire scene
• Local ambient light
– Contributed by additional light sources
– Can be different for each light and primary color
• Computationally inexpensive
Diffuse Light
• Point Source
– Given by a point
– Light emitted equally in all directions
– Intensity decreases with square of distance
– Point source [x y z 1]T
• Directional Source
– Given by a direction
– Simplifies some calculations
– Intensity dependents on angle between surface
normal and direction of light
– Distant source [x y z 0]T
Spot Lights
Example:
Vertex Normal
• Assumptions
– Light source at infinity
– Viewer at infinity
– The polygon represents the
actual surface being modeled
How to do shading
Find the intensity of the pixel. Then using the Intensity
coloring the whole object
Drawback flat shading
• Match Bone effect
Flat Shading
Wire-frame Model
Flat Shading
Smooth Shading
• Introduce vertex normals at each
vertex
– Usually different from facet normal
– Used only for shading
– Think of as a better approximation of the real surface
that the polygons approximate
• Two types
– Gouraud Shading
– Phong Shading (do not confuse with Phong Lighting
Model)
Smooth Shading
1.Determine average unit normal vector at each
vertex of the polygon
2. Apply an illumination model at each polygon
vertex to obtain the light intensity at the
position
3. Linearly interpolate the vertex intensities over
the projected area of the polygon
Gouraud Shading
• This is the most common approach
– Perform Phong lighting at the vertices
– Color are appropriated across polygon
How to do Gouraud Shading
• Step 1: To obtain average unit normal Vector
• Step 2: Apply Illumination model for
calculating intensity for finding the vertex
intensity.
• Step 3: Intensity Interpolation
Gouraud Shading
Wire-frame
Gouraud
Flat Shading
Shading
Model
Phong Shading
Instead of interpolating vertex intensities
This results in
Wire-frame
Gouraud
Phong
Flat Shading
Shading
Shading
Model
Complexity of Phong Shading..
Requires more computation than Gouraud shading…
• Flat Shading
– Compute Phong lighting once for entire polygon
• Gouraud Shading
– Compute Phong lighting at the vertices and
interpolate lighting values across polygon
• Phong Shading
– Interpolate normals across polygon and perform
Phong lighting across polygon
Lighting in OpenGL
Lighting in OpenGL [1/2]
• Enabling shading
– glShadeModel(GL_FLAT)
– glShadeModel(GL_SMOOTH); // Gouraud Shading
only
• Using light sources
– Up to 8 light sources
– To create a light
• GLfloat light0_position[] = { 600, 40, 600, 1.0};
• glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
• glEnable(GL_LIGHT0);
• glEnable(GL_LIGHTING);
Lighting in OpenGL [2/2]
– Changing light properties
• GLfloat light0_ambient[] = { 0.4, 0.1, 0.0, 1.0 };
• GLfloat light0_diffuse[] = { 0.9, 0.3, 0.3, 1.0 };
• GLfloat light0_specular[] = { 0.0, 1.0, 1.0, 1.0 };
• glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
• glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
• glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);