Professional Documents
Culture Documents
UNIT-7
LIGHTING AND SHADING
Gradations or shades of color give 2-D images the appearance of being 3-D. The reason for
shading of colors is the interaction between light and the surfaces in the models.
The color of point seen in an object is determined by multiple interactions among light
sources and reflective surfaces. These interactions can be viewed as a recursive process.
Consider the simple scene below.
This recursive reflection of light between surfaces accounts tor subtle shading effects, such as
the bleeding of colors between adjacent surfaces.
There are various approximate approaches, such as radiosity and ray tracing. Though these
approximations are near to rendering equations, they depend on particular types of surfaces and
they are slow.
Hence Phong reflection model is used which provides a compromise between physical
Priyanka H V
Page 1
Computer Graphics
Conceptually, the clipping window in this plane is mapped to the screen; thus, the
projection plane can be considered as ruled into rectangles, each corresponding to a pixel.
The color of the light source and of the surfaces determines the color of one or more
surface.
If the surface is translucent, some of the light is transmitted through the material and emerges
surface.
The shading of objects also depends on the orientation of their surfaces, a factor that is
2. Diffuse surfaces:
They are characterized by reflected light being scattered in all directions.
Walls painted with matte or flat paint are diffuse reflectors, as are many natural
3. Translucent surfaces:
allow some light to penetrate the surface and to emerge from another location on
Self-emission and
Reflection.
Light source is an object that emits light only through internal energy sources.
Priyanka H V
Page 2
Computer Graphics
A light source, such as a light bulb, can also reflect some light that is incident on it from the
surrounding environment.
For a distributed light source, such as a light bulb, the evaluation of this integral (using
analytic or numerical methods) is difficult.
It is easier to model the distributed source with polygons, each of which is a simple source,
or with an approximating set of point sources.
Light sources are modeled as having three components-red, green, and blue and can be
each of whose
components is the intensity of the independent red, green, and blue components.
The red component of a light source can be used for the calculation of the red component of
the image.
Because color-light computations involve three similar but independent calculations, a single
scalar equation can be used which may represent any of the three color components.
The following four lighting types are sufficient for rendering most simple scenes.
1. Ambient lighting
Uniform lighting is called ambient light.
In some rooms, such as in certain classrooms or kitchens, the lights have been designed
and positioned to provide uniform illumination throughout the room.
Such illumination is achieved through large sources that have diffusers whose purpose is
to scatter light in all directions.
Priyanka H V
Page 3
Computer Graphics
Such illumination can be simulated, at least in principle, by modeling all the distributed
sources, and then integrating the illumination from these sources at each point on a
reflecting surface.
Making such a model and rendering a scene with it would be a daunting task for a
graphics system, especially one for which real-time performance is desirable.
Alternatively, ambient light can be used for uniform lighting. Thus, ambient illumination
is characterized by an ambient intensity, Ia, that is identical at every point in the scene.
The ambient source has three color components: I =
Scalar Ia is used to denote anyone of the red, green, or blue components of Ia.
Every point in the scene receives the same illumination from Ia, each surface can reflect
this light differently.
2. Point sources
An ideal point source emits light equally in all directions.
A point source located at a point po can be characterized by a three-component color
matrix:
I=
The intensity of illumination received from a point source is proportional to the inverse
square of the distance between the source and surface.
Hence, at a point p (in figure), the intensity of light
received from the point source is
Priyanka H V
Page 4
Computer Graphics
Thus, the intensity is a function of the angle between the direction of the
source and a vector s to a point on the surface (as long as this angle is less
than ; figure).
This function is usually defined by cose, where the exponent e (figure)
determines how rapidly the light intensity drops off. Cosines are
convenient functions for lighting calculations. If both s and l are unitlength vectors, the cosine can be computed with the dot product cos = s. l, a calculation
that requires only three multiplications and two additions.
4. Distant light
Most shading calculations require the direction from the point on the
surface to the light source. This vector must be computed repeatedly to
calculate the intensity at each point on the surface. This is a significant part
of the shading calculation.
However, if the light source is far from the surface, the vector does not change for
different points on the surface just as the light from the sun strikes all objects that are in
close proximity to one other at the same angle.
Then a point source of light can be replaced with a source that illuminates objects with
parallel rays of light -a parallel source. In practice, the calculations for distant light
sources are similar to the calculations for parallel projections; they replace the location of
the light source with the direction of the light source.
Hence, in homogeneous coordinates, a point light source at po is represented internally as
a 4-D column matrix:
p0 =
In contrast, the distant light source is described by a direction vector whose
representation in homogeneous coordinates is the matrix
p0 =
The graphics system can carry out rendering calculations more efficiently for distant light
sources than for near ones. Of course, a scene rendered with distant light sources looks
different from a scene rendered with near sources. OpenGL allows both types of sources.
7.3 The Phong Reflection Model
determined by n and l.
The Phong model supports the three types of material-light interactions:
Priyanka H V
Page 5
Computer Graphics
Consider a set of point sources. Each source let have separate ambient, diffuse, and specular
components for each of the three primary colors.
Thus at any point p on the surface, a 3 x 3 illumination matrix for the i th light source is:
The first row contains the ambient intensities for the red, green, and blue terms from source i.
The second row contains the diffuse terms; the third contains the specular terms.
The amount of the incident light that is reflected, at the point of interest, can be computed.
Thus, for each point, the computed matrix of reflection is:
The value of Ri depends on the material properties, the orientation of the surface, the
direction of the light source, and the distance between the light source and the viewer.
Then contribution for each color source can be computed by adding the ambient, diffuse, and
specular components.
Total intensity is obtained by adding the contributions of all sources and, possibly, a global
ambient term. Thus, the red term is
Notation can be simplified by noting that the necessary computations are the same for each
source and for each primary color.
They differ depending on whether the ambient, diffuse, or specular terms are considered.
1. Ambient Reflection
The intensity of ambient light Ia is the same at every point on the surface.
The amount reflected is given by the ambient reflection coefficient, Ra = ka. Because only a
positive fraction of the light is reflected, 0 Ka 1
La can be any of the individual light sources, or it can be a global ambient term.
A surface has three ambient coefficients-Kar, Kag, and Kab and they can differ.
2. Diffuse Reflection
Priyanka H V
Page 6
Computer Graphics
A perfectly diffuse reflector scatters the light that it reflects equally in all directions.
The amount of light reflected depends both on the material, because some of the incoming
light is absorbed, and on the position of the light source relative to the surface.
Rays of light that hit the surface at only slightly different angles are
reflected back at markedly different angles.
Consider a diffuse planar surface shown below, illuminated by the sun (point source).
If a reflection coefficient Kd that represents the fraction of incoming diffuse light that is
reflected, is added, then diffuse reflection term: ld = Kd (l. n) Ld.
If the facet (surface point) is aimed away from the eye (> 90) this dot product and hence
the Id is negative. But model evaluates Id to 0 with the modified computation replacing
(l.n) with max[(l.n), 0].
For near 0, brightness varies only slightly with angle, because cosine changes
slightly there. As approaches 90, however, the brightness falls rapidly to 0.
3. Specular Reflection
The images with only ambient and diffuse reflections appear shaded and 3-D, but with all the
surfaces dull, somewhat like chalk because the highlights reflected from shiny objects are not
considered.
Priyanka H V
Page 7
Computer Graphics
These highlights usually show a color different from the color of the reflected ambient and
diffuse light.
E.g. A red ball, viewed under white light, has a white highlight that is the reflection of some
of light from the source in the direction of the viewer.
Specular surface is smooth. The smoother the surface is, the more it resembles a mirror;
Figure shows that as the surface gets smoother, the reflected light is concentrated in a smaller
range of angles, centered about the angle of a perfect reflector-a mirror or a perfectly
specular surface.
Modeling specular surfaces realistically can be complex, because the pattern by which the
light is scattered is not symmetric, it depends on the wavelength of the incident light, and it
changes with the reflection angle.
Phong proposed an approximate model that can be computed with only a slight increase over
the work done for diffuse surfaces.
The model adds a term for specular reflection. Hence, the surface can be considered as being
rough for the diffuse term and smooth for the specular term.
The amount of light that the viewer sees depends on the angle between r, the direction of a
perfect reflector, and v, the direction of the viewer.
The coefficient Ks (0 Ks 1) is the fraction of the incoming specular light that is reflected.
Figure below shows how, as is increased, the reflected light is concentrated in a narrower
region,
centered
In the limit, as goes to infinity, it is a mirror surface; values in the range 100 to 500
correspond to most metallic surfaces, and smaller values (<100) correspond to materials that
show broad highlights.
The computational advantage of the Phong model is that, if the r and n are normalized to unit
length, the dot product can again be used, and the specular term becomes
Priyanka H V
Page 8
Computer Graphics
7.3.1 Use of the Halfway Vector (Blinn Phong / Modified Phong Model)
If the Phong model with specular reflections is used in rendering, the dot product r . v should
be recalculated at every point on the surface.
An approximation can be obtained by using the unit vector half-way between the viewer
Most of the calculations for rendering a scene involve the determination of the required
vectors and dot products.
E.g. if the surface is a flat polygon, the normal is the same at all points on the surface.
If the light source is far from the surface, the light direction is the same at all points.
Normal Vectors
For smooth surfaces, the normal vector to the surface exists at every point and gives the local
orientation of the surface.
In terms of the normal to the plane, n, and a point po on the plane, the same equation is
n . (p - po) = 0,
Assume three non-collinear points - po, p1, p2 in this plane (they are sufficient to determine
it uniquely).
The vectors p2 p0 and p1-p0 are parallel to the plane, and their cross product can be used to
find the normal
n = (p2 p0) x (p1-p0)
Priyanka H V
Page 9
Computer Graphics
Order of the vectors in the cross product is important because reversing the order changes the
surface from outward pointing to inward pointing, and that can affect the lighting
calculations.
The usual equation tor this sphere is the implicit equation f(x, y, z) =
The normal is given by the gradient vector, which is represented by the column matrix,
The sphere could also be represented in parametric form. In this form, the x, y, and z values
of a point on the sphere are represented independently in terms of two parameters u and v:
x = x(u, v),
y = y(u, v),
z = z(u, v)
For a particular surface, there may be multiple parametric representations. One parametric
representation for the sphere is
x(u, v) =cos u sin v, y(u, v) = cos u cos v, z(u, v) = sin u
< u<
< v<
Then the normal can be obtained from the tangent plane at a point p(u, v) = [x(u, v) y(u, v)
z(u, v)]T on the surface, as shown.
The tangent plane gives the local orientation of the surface at a point.
Tangent plane can be derived by taking the linear terms of the Taylor
series expansion of the surface at p.
Taking
their
For
the
As only the direction of n is needed, the above expression can be divided by cos u to obtain
cross
product,
the
normal
sphere,
If a normal is defined before a sequence of vertices through glVertex calls, this normal is
associated with all the vertices, and is used for the lighting calculations at all the vertices.
Priyanka H V
Page 10
Computer Graphics
The calculated normal at a point and the direction of the light source can be used to compute
the direction of a perfect reflection.
An ideal mirror is characterized by the following statement: The angle of incidence is equal
to the angle of reflection. These angles are as pictured below.
The angle of incidence is the angle between the normal and the light
source (assumed to be a point source).
The angle of reflection is the angle between the normal and the
direction in which the light is reflected.
At a point p on the surface, the incoming light ray and the normal at the point must all lie in
the same plane. These two conditions are sufficient to determine r from n and l.
Assuming both l and n have been normalized (to have unit vectors) such that
It is needed that
If i = r, then,
cos i = cos r.
A second condition between and can be from the requirement that r also be of unit length;
thus,
l = r . r = 2 + 2 l . n + 2,
Solving these two equations,
r = 2(1. n) n -1.
7.5 Polygonal Shading
The three vectors l , n, and v - can vary from point to point on a surface.
1. Flat/Constant shading
Priyanka H V
Page 11
Computer Graphics
E.g. The location of the source must be changed to the direction of the source.
The size of the polygon relative to the distance of the polygon from the source or viewer.
If the three vectors are constant, then the shading calculation needs to be carried out only
once for each polygon, and each point on the polygon is assigned the same shade. This
first triangle, the normal of the fourth for the second, and so on.
Similar rules hold for other primitives, such as quadrilateral strips.
Considering a mesh constructed using polygons.
Flat shading will show differences in shading for the polygons in that mesh. E.g.
If the light sources and viewer are near the polygon, the vectors l and v will be different
for each polygon. However, if the polygonal mesh has been designed to model smooth
surface, flat shading will almost always be disappointing, because shading between
employing smoother shading techniques that do not produce such large differences.
2. Interpolative / Gouraud shading
OpenGL interpolates colors assigned to vertices across a polygon.
OpenGL interpolates colors for other primitives such as lines, if shading model is smooth via
glShadeModel(GL_SMOOTH);
If both smooth shading and lighting are enabled, and the normal is assigned to each vertex of
Then the idea of a normal existing at a vertex may complicate the mathematics.
Gouraud realized that the normal at the vertex n could be defined in a way to achieve
Priyanka H V
Page 12
n=
Computer Graphics
Even the smoothness introduced by Gouraud shading may not prevent the appearance of
Mach bands.
Phong proposed that, instead of interpolating vertex intensities, as in Gouraud shading,
The interpolated
n() = (l - )nA + nB
Similar
interpolation can be done on all the edges.
The normal at any interior point can be obtained from points on the edges by
n(, ) = (l - )nC + nD
Once the normal at each point is obtained, an independent shading calculation can be done.
Phong shading produces renderings smoother than those of Gouraud shading, but at a
slight increase in the time required for rendering; neither is true, however, for Phong shading.
Consequently, Phong shading is almost always done offline.
7.6 Approximation of a Sphere by Recursive Subdivision
To illustrate the interactions between shading parameters and polygonal
approximations to curved surfaces, here, a sphere is used as curved surface to
illustrate shading calculations and the sphere is approximated using polygons.
Recursive subdivision is introduced which is a powerful technique for generating
approximations to curves and surfaces to any desired level of accuracy.
Step 1
The regular tetrahedron is composed of four equilateral triangles, determined by four vertices
say, (0,0,1), (0, 22/3, -1/3), (-6/3, -2/3, -1/3), (6/3,-2/3, -1/3).
All four vertices lie on the unit sphere, centered at the origin.
Priyanka H V
Page 13
Computer Graphics
The order of vertices obeys the right-hand rule, so the code can be converted to draw shaded
polygons with little difficulty.
Step 2
Closer approximation to the sphere can be obtained by subdividing each facet of the
tetrahedron into smaller triangles.
Subdividing into triangles will ensure that all the new facets will be flat.
The bisectors of the sides of the triangle are connected forming four equilateral triangles.
After each facet is subdivided, the four new triangles in each facet will still be in the same
plane as the original triangle.
The new vertices created by bisection can be moved to the unit sphere by normalizing each
bisected vertex, using a simple normalization function such as
void normal(point3 p)
{
double d = 0.0;
int i;
for ( i =0; i <3 ; i++) d += p[i]*p[i];
d=sqrt(d) ;
if (d > 0.0) for(i = 0; i < 2; i++) p[i]/=d;
}
Step 3:
A single triangle, defined by the vertices numbered a, b, and c, can be subdivided by the
following code:
point3 v1, v2, v3;
int j;
for (j=0; j < 3; j++) vl[j]=v[a][j]+v[b][j];
normal (vl);
Priyanka H V
Page 14
Computer Graphics
This code can be used in the tetrahedron routine to generate 16 triangles rather than 4, but
there must be a provision to repeat the subdivision process n times to generate successively
closer approximations to the sphere.
Hence tetrahedron routine is made depend on the depth of recursion by adding an argument
n:
void tetrahedron(int n)
{
divide_triangle(v[0], v[l], v[2], n);
divide_triangle(v[3], v[2], v[l], n );
divide_triangle(v[0], v[3], v[l], n );
divide_triangle(v[0], v[2], v[3], n );
}
Step 5:
The divide_triangle function calls itself to subdivide further if n is greater than zero, but
generates triangles if n has been reduced to zero. Here is the code:
divide_triangle(point3 a, point3 b, point3 c, int n)
{
point3 v1, v2,v3;
int j;
if (n > 0)
{
for(j=0; j<3; j++) vl[j]=a[j]+b[j];
normal(vl);
for(j=0; j<3; j++) v2[j]=a[j]+c[j];
normal(v2);
for(j=0;j<3; j++) v3[j]=c[j]+b[j];
normal (v3);
divide_triangle(a, v2, v1, n-l);
divide_triangle(c, v3, v2, n-l);
divide_triangle(b, v1, v3, n-1);
divide_triangle(vl, v2, v3, n-l);
Priyanka H V
Page 15
Computer Graphics
}
else triangle(a. b. c);
}
Figure above shows an approximation to the sphere drawn with this code.
Lighting and shading can be added to the sphere approximation after the following section.
OpenGL supports the four types of light sources and allows at least eight light sources in a
program.
Each must be individually specified and enabled. Although there are many parameters that
must be specified, they are exactly the parameters required by the Phong model.
There are four vector parameters that can be set: the position (or direction) of the light
source, and the amount of ambient, diffuse, and specular light associated with the source.
E.g:
To specify the first source GL_LIGHT0, and to locate it at the point (1.0,2.0,3.0). Its
position can be stored as a point in homogeneous coordinates:
GLfloat light0_pos[ ]={l.0, 2.0, 3.0, 1.0};
With the fourth component set to zero, the point source becomes a distant source with
direction vector
GLfloat light0_dir[ ]={l.0, 2.0, 3.0, 0.0};
To add a white specular component and red ambient and diffuse components to the single
light source:
GLfloat diffuse0[]={l.0, 0.0, 0.0, 1.0};
GLfloat ambient0[]={1.0, 0.0, 0.0, 1.0};
GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
To add a global ambient term that is independent of any of the sources. E.g. To add a
small amount of white light:
GLfloat globa1_ambient[]={0.1, 0.1, 0.1, 1.0};
GlLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
Priyanka H V
Page 16
Computer Graphics
The distance terms are based on the distance-attenuation model, f (d) = 1 / (a + bd + cd2)
which contains constant, linear, and quadratic terms. These terms are set individually via
glLightf; E.g.
GlLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, a);
(GL_SPOT_CUTOFF).
All three are specified by glLightf and glLightfv.
GL_LIGHT_MODEL_LOCAL_VIEWER and GL_LIGHT_MODEL_TWO_SIDE:
For polygons, these faces are determined by the order in which the vertices are specified,
using the right-hand rule.
For most objects, since only the front faces are seen, the shading of the back-facing surfaces
is neglected.
To ensure that the properly placed viewer may see a back face, both the front and back faces
correctly.
defined.
7.8 Specification of Materials in OpenGL
Material properties in OpenGL match up directly with the supported light sources and with
the Phong reflection model.
Different material properties can be also be specified for the front and faces of a surface.
Priyanka H V
Page 17
Computer Graphics
E.g. To define ambient, diffuse, and specular reflectivity coefficients (ka, kd, ks) for each
primary color:
GLfloat ambient[ ] = {0.2, 0.2, 0.2, 1.0};
GLfloat diffuse[ ] = {1.0, 0.8, 0.0, 1.0};
GLfloat specular[ ]={l.0, 1.0, 1.0, 1.0};
Here, small amount of white ambient reflectivity, yellow diffuse properties, and white specular
reflections are defined.
To set the material properties for the front and back faces by the calls
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
Note that if both the specular and diffuse coefficients are the same (as is often the case), both can
be specified by using GL_DIFFUSE_AND_SPECULAR for the type parameter. To specify
different front- and back-face properties, GL_FRONT and GL_BACK can be used.
glMaterialf. E.g.
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100.0);
Material properties are modal: Their values remain the same until changed, and, when
To shade the approximate spheres with OpenGL's shading model, normals must be assigned.
E.g. To flat shade each triangle, using the three vertices to determine a normal, and then to
assign this normal to the first vertex.
Assuming that light sources have been defined and enabled, triangle routine can be changed to
produce shaded spheres:
Priyanka H V
Page 18
Computer Graphics
As the number of sub-divisions is increased the interior of the spheres appears smooth. But
the edges of polygons around the outside of the sphere image are still visible. This type of
Page 19
Computer Graphics
If an array of spheres is illuminated by a distant source, the spheres close to the source
prevent some of the light from the source from reaching the other spheres.
In a real scene these spheres are specular and some light is scattered among spheres. But
if local model is used, each sphere is shaded independently and hence all appear the same
to the viewer.
Local model also cannot produce shadows.
These effects can be achieved by more sophisticated (and slower) global rendering
techniques, such as:
Ray tracing: It works well for highly specular surfaces. E.g. the scenes composed of
There are many light rays leaving a source. But, only those light rays entering the lens of the
synthetic camera and pass through the center of projection contribute to the image.
Figure shows a single point source with several of the possible interactions with perfectly
specular surfaces.
Rays can enter the lens of the camera directly from the source, from
interactions with a surface visible to the camera, after multiple
reflections from surfaces, or after transmission through one or more
surfaces.
Most of the rays that leave a source do not enter the lens and do not
contribute to the image. Hence, attempting to follow all rays from a
The cast rays that contribute to the image,which can be obtained if the rays are traced in the
reverse direction i.e. the rays starting at the center of projection
A color must be assigned to every pixel. Hence, at least one ray is cast
through each pixel.
Each cast ray either intersects a surface or a light source, or goes off to
infinity without striking anything.
Rays that strike say opaque surfaces need a shading calculation for their point of intersection.
Doing so and then using Phong model the image produced will be the same as could be
produced by the local renderer.
Pipeline renderer
Priyanka H V
Ray racer
Page 20
Computer Graphics
The sequence of steps in the pipeline renderer The sequence in which the calculations are
are:
Object modeling
Projection
Visible surface determination.
Shadow or feeler rays from the point on the surface to each source are computed. If a
shadow ray intersects a surface before it meets the source, the light is blocked from reaching
the point under consideration and this point is in shadow, at least from this source. No
lighting calculation needs to be done for sources that are blocked from a point on the surface.
If all surfaces are opaque and the light scattered from surface to surface is not considered, an
image that has shadows added is obtained. This is what that can be accomplished without ray
tracing. But the needed hidden-surface calculation for each point of intersection between a
surfaces.
Ray tracing is particularly good at handling surfaces that are both reflecting and transmitting.
A cast ray to a surface can be followed with the property that: if a ray from a source strikes a
point, then the light from the source is partially absorbed, and some of this light contributes
to the diffuse reflection term. The rest of the incoming light is divided between a transmitted
model.
2. A ray is cast in the direction of a perfect reflection.
3. A ray is cast in the direction of the transmitted ray.
These two rays are treated just like the original cast ray; that is, they are intersected with
Radiosity:
It breaks up the scene into small flat polygons, or patches, each of which can be assumed to
be perfectly diffuse and renders in a constant shade, as shown:
There are two steps to find the shades needed:
Priyanka H V
Page 21
Computer Graphics
1. Patches are considered pair wise to determine form factors that describe how the light
energy leaving one patch affects the other.
2. Once the form factors are determined, the rendering equation, which starts as an integral
equation, can be reduced to a set of linear equations for the radiosities - essentially the
reflectivity-of the facets.
Even the distributed light sources, if modeled as emissive patches, appear in the rendering.
Priyanka H V
Page 22