The document describes implementing Phong shading over polygonal surfaces using OpenGL. Key aspects include reading mesh files to obtain vertex and face data, calculating vertex normals, setting up a light source, and applying the Phong illumination model at each point. Phong shading is computationally expensive but produces higher quality results than Gouraud shading by interpolating normals. The implementation subdivides triangles recursively until the pixel level to apply Phong's equations. Results using pyramid and octahedron meshes demonstrated Phong shading generated superior images compared to Gouraud shading.
The Codex of Business Writing Software for Real-World Solutions 2.pptx
Phong Shading over any Polygonal Surface
1. 1
IMPLEMENTATION OF PHONG
SHADING MODEL OVER ANY
POLYGONAL SURFACE
By
Bhuvnesh Pratap Singh
Undergraduate Student
Department of Computer Science & Engineering
PDPM IIIT DM, Jabalpur
Submitted to Prof. Aparajita Ojha
Department of Computer Science & Engineering
PDPM IIIT DM, Jabalpur
2. 2
Abstract
Using OpenGL Graphics API in C++, the Phongs shading was performed over
polygonal surfaces formed with mesh which skinned the surface. Two mesh
objects such as a pyramid and a round octahedron were taken and the surfaces
were stored in the terms of vertices, edges, and faces. For any object the mesh file
created was read in the programme to obtain the vertices, and then the face normals
were calculated so that finally vertex normal for each vertex could be obtained .
Light source was set up at a desired location in the scene to alleviate the lightning
effect in the scene . Calculation of vertex normals facilitated the application of
Phongs illumination model by interpolating the normals and applying the Phongs
illumination equations at each point on the surface .However the process of
applying Phongs equation at each and every point suffered from graphics card
hardware limitation because of extensive calculations per pixel.
3. 3
TABLE OF CONTENTS
OpenGL …………………………………… 4
Modeling Shapes With Polygonal Meshes…………………………………. 5
Illumination Models………………………………………… 8
Shading Models & Phongs Shading ……………………………………………11
Comparison Between Shading Models…………………………………………17
The key Programming Portion………………………………………………….20
Results & Outputs………………………………………………………… 21
Summary………………………………………………………………… 23
References……………………………………………………………24
4. 4
OpenGL
OpenGL is strictly defined as “a software interface to graphics hardware.” In
essence, it is a 3D graphics and modeling library that is extremely portable and
very fast. Using OpenGL, we can create elegant and beautiful 3D graphics with
nearly the visual quality of a ray tracer. The greatest advantage to using OpenGL is
that it is orders of magnitude faster than a ray-tracer. It uses algorithms carefully
developed and optimized by Silicon Graphics, Inc.(SGI), an acknowledged world
leader in computer graphics and animation.
OpenGL is not a programming language; it is an API (Application Programming
Interface).Whenever we say that a program is OpenGL-based or an OpenGL
application, we mean that it was written in some programming language (such as
C or C++) that makes calls to one or more of the OpenGL libraries. We are not
saying that the program uses OpenGL exclusively to do drawing. It may combine
the best features of two different graphics packages. Or it may use OpenGL for
only a few specific tasks and environment-specific graphics (such as the Windows
GDI) for others.
OpenGL has been extensively used in the creation of this project, in fact most of
the graphics work has been made trivial with the help of OpenGL.
5. 5
Modeling Shapes with Polygonal Meshes
A polygon mesh or unstructured grid is a collection of vertices, edges and faces
that defines the shape of a polyhedral object in 3D Computer Graphics and solid
modeling. The faces usually consist of triangles, quadrilaterals or other simple
convex polygons, since this simplifies rendering, but may also be composed of
more general concave polygons, or polygons with holes.
Fish model using triangular mesh
Triangle meshes:
• Relevant properties:
_ Exactly 3 vertices per face.
_ Any number of faces per vertex.
• Easy adjacency structure
_ Faces store refs to vertices and neighboring faces.
_ Can find most adjacencies in constant time.
6. 6
Triangular Mesh
Polygon meshes may be represented in a variety of ways, using different methods
to store the vertex, edge and face data one of them is:
Face-Vertex Meshes - A simple list of vertices, and a set of polygons that point to
the vertices it uses , and this is the way in which mesh has been stored in this
project.
In the project a 3D pyramid mesh has been used to successfully render the phongs
shading model which is shown in the picture below:
A 3D pyramid mesh used in the Project.
7. 7
This mesh stores vertices list and face list in the following manner :
Vertices list:
Vertex Index X Coordinate Y Coordinate Z Coordinate
1 -1 0 1
2 -1 0 -1
3 1 0 -1
4 1 0 1
5 0 1 0
Similarly the Face list is as follows :
Face Index Type Vertex 1 Vertex 2 Vertex 3 Vertex 4
1 Triangle 1 2 5
2 Triangle 2 3 5
3 Triangle 3 4 5
4 Triangle 4 1 5
5 Quadrilateral 1 2 3 4
Similarly the edge list can also be formulated.
8. 8
Illumination Models
An illumination model (equation) expresses the components of light reflected from
or transmitted(refracted) through a surface. There are three basic light components:
ambient, diffuse, and specular. We will develop local illumination models that
contain some or all of these components. The models are local in that they do not
consider light from objects in the scene. Only light sources generate
light. Light reflected from other objects does has no effect on other objects. Ray
tracing and radiosity models provide these global lighting effects.
Ambient Reflected Light
Ambient light component - non-directional light source that is the product of
multiple reflections from the surrounding environment.
- Assume the intensity Ia of ambient light is constant for all objects
- The ambient-reaction coefficient Ka, which ranges from 0 to 1, determines
the amount of ambient light reflected by the object's surface.
- The ambient-reflection coefficient is a material property.
- The ambient illumination equation is I =KaIa where I is the intensity of
reflected light from a surface with ambient-reflection coefficient ka in an
environment with ambient intensity Ia
Diffuse light
Diffuse light is reflected (or transmitted) from a point with equal intensity in all
directions. Diffusely reflected light is typical for dull, matte surfaces such a paper
or a at wall paint. Diffusely refracted light is typical for frosted glass. Diffuse
reaction is modeled by the Lambert's laws, which basically states that brightness
depends only on the angle θ between the light source direction L and the surface
normal N .
9. 9
Diffuse Illumination Model.
What all this means is that the diffuse illumination equation we'll
use is I = kdIp cos θ;
where
1. θ is the angle between the surface normal N and the light vector L;
2. Ip is the intensity of a point light source.
3. kd is the diffuse-reaction coefficient or diffuse reactivity of the surface which
varies between 0 and 1 and depends on the surface material .Assuming that ~N and
~L are unit length vectors, we can write the cosine as a simple dot product and the
diffuse illumination equation as
I = kd.Ip(N .L)
Specular Light
The specular component of reflection (or refracted) light accounts for highlights
caused by light reflecting (or refracting) primarily in one direction. Specular
reflection is mirror-like; it gives rise to shiny spots on surfaces. The amount of
specularly reflected light seen by a viewer depends on the angle θ between L and N
and the angle φ between the viewer V and the reflected ray R.
Specularly reflected light, unlike diffusely reflected light, is view dependent. In
drawing the specular component we often refer to the “specular bump" which
shows that most light is reflected in a particular direction.
Bui-Thong Phong developed a popular approximation to the specular component of
light. Phong's model is
I = W(θ)Ip cos
n
(φ);
where W(θ) is the fraction striking the surface that is specularly reflected, and n is
the Phong specular-reflection exponent. W(θ) is often set to a constant, call it ks,
and refer to it as the specular-reflection coefficient or specular reflectivity between
1 and several hundred. The Phong exponent n varies from 1 to several hundred.
10. 10
A setting n = 1 gives broad gentle fall off to the highlight, while large settings of n
give focused highlight.
The Complete Illumination Model
Light is additive. To model reflected light we simply add the ambient, diffuse, and
specular terms. That is, our basic illumination model is
Iβ = ka. Iaβ .Odβ + Ipβ [ kd .Odβ .(N . L) + ks . Osβ .(R .V )n
]
Where O sβ is the object specular color component and β represents color
component.
11. 11
Shading Models & Phongs Shading
Basically OpenGL supports have two types of shading techniques :
1. Flat Shading
2. Smooth Shading
We may choose either, but smooth shading is usually more pleasing and can be
more realistic. Unless there is a sound reason to use flat shading in order to
represent data or other communication concepts more accurately, we will probably
want to use smooth shading for our images.
Flat shading of a polygon displays all the points in a polygon with a single color.
This is the effect if you set a color for the polygon, or define a polygon with
glMaterial calls, and then display the polygon with a simple glBegin(...) - glEnd()
pair that includes one single normal vector definition for each polygon.
Smooth shading of a polygon displays the points in a polygon with smoothly-
changing colors across the surface of the polygon. This requires us to define a
separate color for each vertex of our polygon, because the smooth color change is
computed by interpolating the vertex colors across the interior of the triangle.
Computing the color for each vertex is done as usual for a full lighting model, with
the material properties defined, but it requires us to define a normal vector for each
vertex of the polygon because the color of the vertex is determined by the lighting
model that includes this normal.
Figure 8.1 shows see two different images of the same function surface, one with
flat shading (left) and one with smooth shading (right), to illustrate the difference.
12. 12
Clearly the smooth-shaded image is much cleaner, but there are still some areas
where the triangles change direction very quickly and the boundaries between the
triangles still show in the smoothly-shaded image. Smooth shading is very nice,
but it isn't perfect.
The difference between the programming for these two shading models is that the
flat-shaded model uses only one normal per polygon and the smooth-shaded model
uses a separate normal per vertex. It can take a bit more work to compute the
normal at each vertex instead of only once per polygon, but that is the price for
smoothing.
Smooth shading attempts to de-emphasize edges between faces by computing
colors at more points on each face. The two principal types of smooth shading are
Gouraud shading and Phong shading .OpenGL supports only Gouraud shading.
Gouraud Shading
Gouaraud shading computes a different value of color for each pixel. Gouraud
applies the illumination only at triangle vertices, and linearly interpolates the
resulting colors across the triangle. Linear interpolation, however, cannot express
the light intensity peaks, or highlights, at regions of high curvature. Gouraud
shading has a common artifact that it is easy to recognize the underlying triangular
mesh, particularly for low-resolution meshes. This results from the fact that the
derivative of the piece-wise linear color function is not continuous across the
triangle edges. These discontinuities are accentuated by the human visual system,
through a psychophysical Phenomenon know as Mach banding .
Triangle in Gouraud shading
13. 13
In the triangle shown above in the figure in Gouraud shading the three vertex
normals for the vertex A,B,C will be calculated , so that the color at these vertices
can be known and then this color can be interpolated across at each point on the
surface of the triangle.
The above picture demonstrate the assignment of vectors per vertex of the triangle
in the presence of a light source while the picture below demonstrate the process of
interpolation.
14. 14
Phongs Shading
Phong shading was introduced by Phong as the appropriate shading method to go
along with his illumination model. Triangles are shaded by linearly interpolating
the surface normal, defined at each vertex, and re-evaluating the illumination
equation at each pixel. Qualitatively, images generated using Phong shading are
superior to those generated by other methods in the standard rendering pipeline.
However, Phong shading is computationally demanding and it does not run in real-
time even on modern hardware.
Phong shading is expensive not only because illumination has to be evaluated at
each pixel, but also because linearly interpolating the normal is not as simple as
linearly interpolating a scalar. Most commonly, the normal is interpolated by
linearly interpolating each of its scalar components and re-normalizing at each
pixel. Normalization includes a divide, a square root, and a few multiplications and
additions.
15. 15
Some Common aspects of Phongs shading model are as follows:
Phongs shading is a normal - vector interpolating shading.
The normals at vertices are computed as in Goaraud shading.
The normals are interpolated across triangle edges and between edges on
scan line.
The interpolation can be done incrementally for each scan line.
At each pixel, the normal is made to be unit vector and inversed mapped into
world coordinates.
The illumination equation is evaluated to determine the intensity at the pixel.
Phong shading is better than Gouraud when specular components appear in
the illumination model.
*) Gouraud shading may miss highlight altogether if they don’t appear at vertices.
*) Gouraud shading spreads the highlights at a vertex across the surface.
*)Mach bands are reduced in most cases.
16. 16
The above three pictures describe and demonstrate the phongs shading procedure.
17. 17
Comparison between Flat , Gouraud and phongs
shading model generated pictures
A cow generated by Flat Shading
20. 20
The Key Programming Portion
Since the major aim of this project was to shade any polygonal surface with the
phongs shading model so much obviously it becomes the most important part of
the programming part indeed .
As the theory above states that to implement phongs shading we must find the
intensity per pixel by interpolating the vertex normals all across the surface ; so in
the code written in C++ for this project a function called
void phong(float p[3][3],float n[3][3])
has been created to accomplish this task , this is done by a triangle subdivision
technique which specifically is to divide the mesh triangle in to 9 smaller triangles
as shown in the picture below:
And this way only the trialgle is recursively subdivided until the it goes to the pixel
level.this is how exactly phongs shading has been done in this project.
The second comes computing vertex normals which is done by the function
void computeVertexNormals()
21. 21
Results & Outputs
The pyramid and round octahedron meshes are used for the shading purpose and
below are the results of them along with their Gouraud counterpart:
Pyramid with phong shading
Pyramid with Gouraud shading
22. 22
Round Octa with Phongs shading
Round Octa with Gouraud shading
The above results clearly show that the phongs shading generarated results are far
better then generated with Gouraud shading.
23. 23
Summary
The quality of rendering good 3D scenes with OpenGL is directly proportional to
the amount of time spend with 3D and OpenGL text and programming
environment .To shade any polygonal surface by Phongs shading and by storing
the surface characteristics in face list ,vertex list and edge list requires a thorough
understanding of meshes used to skin the surfaces.
The real chore is to make the Phongs shading happen , since OpenGL itself does a
lot of work ,in fact flat and Gouraud shading are what OpenGL does on its own
just by using few simple predefined functions .But the process of implementing of
Phongs shading is what which can be regarded as the findings of the project. The
way in which almost each and every pixel of the surface is reached and the
interpolation of the vertex normals is carried out to conduct the action is the heart
and soul of the whole task.
To get done with the shading it’s really vital to know that where in the graphics
pipeline the shading is done, in OpenGL the shading process is applied during the
projection matrix after which it’s clipped and viewed . One major problem that
came up during the course of this work was the limitation of separate dedicated
graphics card for high computations because of per pixel shading but this problem
could be rectified by using high end hardware support and the graphics support.
24. 24
References
Accomplishment of any work requires the efforts accumulated from various
sources and this piece of work is no exception either, the project is developed with
the help from following source:
Books
*) “Computer Graphics using OpenGL” by F.S.Hill ,Jr.
*) “OpenGL Super bible” by Richard .S. Wright.
*)“The Red Book”.
Web pages:
. http://local.cis.strath.ac.uk/teaching/ug/classes/52.359/lect13.pdf
. http://groups.csail.mit.edu/graphics/pubs/thesis_drdaniel2.pdf
. http://www.nbb.cornell.edu/neurobio/land/OldStudentProjects/cs490-
95to96/guo/report.html
. http://www.cc.gatech.edu/classes/AY2004/cs4451a_fall/smodels/linint.html
The notes provided by Prof. Suzuki Hiromasa during his recent visit to PDPM
IIITDM, Jabalpur on OpenGL programming.