Visvesvaraya Technological University Belagavi: Computer Graphics Laboratory With Mini Project
Visvesvaraya Technological University Belagavi: Computer Graphics Laboratory With Mini Project
BELAGAVI
STUDENT MANUAL
VI-SEMESTER
Dr Antony P J
Professor & Head
Department of Computer Science & Engineering
Lab Experiments:
PART A
Design, develop, and implement the following programs using OpenGL API
Project:
Student should develop mini project on the topics mentioned below or similar applications
using Open GL API. Consider all types of attributes like color, thickness, styles, font,
background, speed etc., while doing mini project.
(During the practical exam: the students should demonstrate and answer Viva-Voce)
Sample Topics:
Simulation of concepts of OS, Data structures, algorithms etc.
Course outcomes: The students should be able to:
Construct 2D and 3D shapes using basic graphic primitives and animating the same
using OpenGL
Apply line drawing ,clipping and curve generating algorithms using OpenGL functions
Create the objects by adding lighting and shading effects
Develop objects that includes user interaction using input device functions (like
keyboard and mouse ) for transforming the object
Develop a graphical model using the concepts learnt in theory and lab exercises.
Conduction of Practical Examination:
Experiment distribution
o For laboratories having only one part: Students are allowed to pick one experiment
from the lot with equal opportunity.
o For laboratories having PART A and PART B: Students are allowed to pick one
experiment from PART A and one experiment from PART B, with equal opportunity.
• Change of experiment is allowed only once and marks allotted for procedure to be
made zero of the changed part only.
• Marks Distribution (Courseed to change in accoradance with university regulations)
o) For laboratories having only one part – Procedure + Execution + Viva-Voce:
15+70+15 =100 Marks
p) For laboratories having PART A and PART B
i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks
ii. Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks
Reference books:
1. Donald Hearn & Pauline Baker: Computer Graphics-OpenGL Version,3rd Edition,
Pearson Education,2011
2. Edward Angel: Interactive computer graphics- A Top Down approach with OpenGL,
5th edition. Pearson Education, 2011
3. M MRaikar, Computer Graphics using OpenGL, Fillip Learning / Elsevier,
Bangalore / New Delhi (2013)
Introduction to OpenGL
OpenGL is a low-level graphics library specification. It makes available to the programmer a small
set of geometric primitives - points, lines, polygons, images, and bitmaps. OpenGL provides a set of
commands that allow the specification of geometric objects in two or three dimensions, using the
provided primitives, together with commands that control how these objects are rendered (drawn).
Since OpenGL drawing commands are limited to those that generate simple geometric primitives
(points, lines, and polygons), the OpenGL Utility Toolkit (GLUT) has been created to aid in the
development of more complicated three-dimensional objects such as a sphere, a torus, and even a
teapot. GLUT may not be satisfactory for full-featured OpenGL applications, but it is a useful starting
point for learning OpenGL.
GLUT is designed to fill the need for a window system independent programming interface for
OpenGL programs. The interface is designed to be simple yet still meet the needs of useful OpenGL
programs. Removing window system operations from OpenGL is a sound decision because it allows
the OpenGL graphics system to be retargeted to various systems including powerful but expensive
graphics workstations as well as mass-production graphics systems like video games, set-top boxes
for interactive television, and PCs.
GLUT simplifies the implementation of programs using OpenGL rendering. The GLUT application
programming interface (API) requires very few routines to display a graphics scene rendered using
OpenGL. The GLUT routines also take relatively few parameters.
Display Lists: All data, whether it describes geometry or pixels, can be saved in a display list for
current or later use. (The alternative to retaining data in a display list is processing the data
immediately-known as immediate mode.) When a display list is executed, the retained data is sent
from the display list just as if it were sent by the application in immediate mode.
Evaluators: All geometric primitives are eventually described by vertices. Evaluators provide a
method for deriving the vertices used to represent the surface from the control points. The method is
a polynomial mapping, which can produce surface normal, colors, and spatial coordinate values from
the control points.
Per-Vertex and Primitive Assembly: For vertex data, the next step converts the vertices into
primitives. Some types of vertex data are transformed by 4x4 floating-point matrices. Spatial
coordinates are projected from a position in the 3D world to a position on your screen. In some cases,
this is followed by perspective division, which makes distant geometric objects appear smaller than
closer objects. Then view port and depth operations are applied. The results at this point are geometric
primitives, which are transformed with related color and depth values and guidelines for the
rasterization step.
Pixel Operations: While geometric data takes one path through the OpenGL rendering pipeline, pixel
data takes a different route. Pixels from an array in system memory are first unpacked form one of a
variety of formats into the proper number of components. Next the data is scaled, biased, processed
by a pixel map, and sent to the rasterization step.
Rasterization: Rasterization is the conversion of both geometric and pixel data into fragments. Each
fragment square corresponds to a pixel in the frame buffer. Line width, point size, shading model,
and coverage calculations to support antialiasing are taken it to consideration as vertices are
connected into lines or the interior pixels are calculated for a filled polygon. Color and depth values
are assigned for each fragment square. The processed fragment is then drawn into the appropriate
buffer, where it has finally advanced to be a pixel and achieved its final resting place.
1.2 Libraries
OpenGL provides a powerful but primitive set of rendering command, and all higher-level drawing
must be done in terms of these commands. There are several libraries that allow you to simplify your
programming tasks, including the following:
OpenGL Utility Library (GLU) contains several routines that use lower-level OpenGL commands
to perform such tasks as setting up matrices for specific viewing orientations and projections and
rendering surfaces.
OpenGL Utility Toolkit (GLUT) is a window-system-independent toolkit, written by Mark
Kilgard, to hide the complexities of differing window APIs.
If you are using the OpenGL Utility Toolkit (GLUT) for managing your window manager tasks, you
should include:
#include <GL/glut.h>
Note that glut.h guarantees that gl.h and glu.h are properly included for you so including these three
files is redundant. To make your GLUT programs portable, include glut.h and do not include gl.h or
glu.h explicitly.
1. Create a new project: o choose File | New from the File Menu
2. Designate library files for the linker to use: o choose Project | Settings from the File Menu
3. Add/Create files to the project: o choose Project | Add to Project | Files from the File
menu
1.5 Initialization
The first thing we need to do is call the glutInit() procedure. It should be called before any other
GLUT routine because it initializes the GLUT library. The parameters to glutInit() should be the same
as those to main(), specifically main(intargc, char** argv) and glutInit(&argc, argv), where argcp is
a pointer to the program's unmodified argc variable from main. Upon return, the value pointed to by
argcp will be updated, and argv is the program's unmodified argv variable from main. Like argcp, the
data for argv will be updated.
The next thing we need to do is call the glutInitDisplayMode() procedure to specify the display mode
for a window. You must first decide whether you want to use an RGBA (GLUT_RGBA) or color-
index (GLUT_INDEX) color model. The RGBA mode stores its color buffers as red, green, blue, and
alpha color components. The forth color component, alpha, corresponds to the notion of opacity. An
alpha value of 1.0 implies complete opacity, and an alpha value of 0.0 complete transparancy. Color-
index mode, in contrast, stores color buffers in indices. Your decision on color mode should be based
on hardware availability and what you application requires. More colors can usually be
simultaneously represented with RGBA mode than with color-index mode. And for special effects,
such as shading, lighting, and fog, RGBA mode provides more flexibility. In general, use RGBA
mode whenever possible. RGBA mode is the default.
Another decision you need to make when setting up the display mode is whether you want to use
single buffering (GLUT_SINGLE) or double buffering (GLUT_DOUBLE). Applications that use
both front and back color buffers are double-buffered. Smooth animation is accomplished by
rendering into only the back buffer (which isn't displayed), then causing the front and back buffers to
be swapped. If you aren't using animation, stick with single buffering, which is the default.
Finally, you must decide if you want to use a depth buffer (GLUT_DEPTH), a stencil buffer
(GLUT_STENCIL) and/or an accumulation buffer (GLUT_ACCUM). The depth buffer stores a
depth value for each pixel. By using a "depth test", the depth buffer can be used to display objects
with a smaller depth value in front of objects with a larger depth value. The second buffer, the stencil
buffer is used to restrict drawing to certain portions of the screen, just as a cardboard stencil can be
used with a can of spray paint to make a printed image. Finally, the accumulation buffer is used for
accumulating a series of images into a final composed image. None of these are default buffers.
We need to create the characteristics of our window. A call to glutInitWindowSize() will be used to
specify the size, in pixels, of your initial window. The arguments indicate the height and width (in
pixels) of the requested window. Similarly, glutInitWindowPosition() is used to specify the screen
location for the upper-left corner of your initial window. The arguments, x and y, indicate the location
of the window relative to the entire display.
parameter which may appear in the title bar if the window system you are using supports it. The
window is not actually displayed until the glutMainLoop() is entered.
Points: A point is represented by a single vertex. Vertices specified by the user as two-dimensional
(only x- and y-coordinates) are assigned a z-coordinate equal to zero. To control the size of a rendered
point, use glPointSize() and supply the desired size in pixels as the argument. The default is as 1 pixel
by 1 pixel point. If the width specified is 2.0, the point will be a square of 2 by 2 pixels. glVertex*()
is used to describe a point, but it is only effective between a glBegin() and a glEnd() pair. The
argument passed to glBegin() determines what sort of geometric primitive is constructed from the
vertices.
Lines: In OpenGL, the term line refers to a line segment, not the mathematician's version that extends
to infinity in both directions. The easiest way to specify a line is in terms of the vertices at the
endpoints. As with the points above, the argument passed to glBegin() tells it what to do with the
vertices. The option for lines includes:
GL_LINES: Draws a series of unconnected line segments drawn between each set of vertices. An
extraneous vertex is ignored. GL_LINE_STRIP: Draws a line segment from the first vertex to the
last. Lines can intersect arbitrarily.
GL_LINE_LOOP: Same as GL_STRIP, except that a final line segment is drawn from the last vertex
back to the first.
With OpenGL, the description of the shape of an object being drawn is independent of the description
of its color. When a particular geometric object is drawn, it's drawn using the currently specified
coloring scheme. In general, an OpenGL programmer first sets the color, using glColor*() and then
draws the objects. Until the color is changed, all objects are drawn in that color or using that color
scheme.
Polygons: Polygons are the areas enclosed by single closed loops of line segments, where the line
segments are specified by the vertices at their endpoints. Polygons are typically drawn with the pixels
in the interior filled in, but you can also draw them as outlines or a set of points. In OpenGL, there
are a few restrictions on what constitutes a primitive polygon. For example, the edges of a polygon
cannot intersect and they must be convex (no indentations). There are special commands for a three-
sided (triangle) and four-sided (quadrilateral) polygons, glBegin(GL_TRIANGLES) and
glBegin(GL_QUADS), respectively. However, the general case of a polygon can be defined using
glBegin(GL_POLYGON).
2.3 Transformations
A modeling transformation is used to position and orient the model. For example, you can rotate,
translate, or scale the model - or some combination of these operations. To make an object appear
further away from the viewer, two options are available - the viewer can move closer to the object or
the object can be moved further away from the viewer. Moving the viewer will be discussed later
when we talk about viewing transformations. For right now, we will keep the default "camera"
location at the origin, pointing toward the negative z-axis, which goes into the screen perpendicular
to the viewing plane.
When transformations are performed, a series of matrix multiplications are actually performed to
affect the position, orientation, and scaling of the model. You must, however, think of these matrix
multiplications occuring in the opposite order from how they appear in the code. The order of
transformations is critical. If you perform transformation A and then perform transformation B, you
almost always get something different than if you do them in the opposite order.
Scaling: The scaling command glScale() multiplies the current matrix by a matrix that stretches,
shrinks, or reflects an object along the axes. Each x-, y-, and z-coordinate of every point in the object
is multiplied by the corresponding argument x, y, or z. The glScale*() is the only one of the three
modeling transformations that changes the apparent size of an object: scaling with values greater than
1.0 stretches an object, and using values less than 1.0 shrinks it. Scaling with a -1.0 value reflects an
object across an axis.
Translation: The translation command glTranslate() multiplies the current matrix by a matrix that
moves (translates) an object by the given x-, y-, and z-values.
Rotation: The rotation command glRotate() multiplies the current matrix that rotates an object in a
counterclockwise direction about the ray from the origin through the point (x,y,z). The angle
parameter specifies the angle of rotation in degrees. An object that lies farther from the axis of rotation
is more dramatically rotated (has a larger orbit) than an object drawn near the axis.
Sample Programs
Sample Program: 01
/* A basic Open GL window*/
#include<GL/glut.h>
void display (void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
gluLookAt (0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
glFlush ();
}
Sample Program: 02
/*point.c
/* Program to draw/display points */
#include<GL/glut.h>
#include<stdlib.h>
voidmyInit(void)
{
glClearColor(2.0,2.0,2.0,4.0);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2i(100,200);
glVertex2i(400,200);
glVertex2i(200,100);
glVertex2i(200,400);
glEnd();
glFlush();
}
Sample Program: 03
/*program to implement horizontal and vertical lines*/
#include<GL/glut.h>
#include<stdlib.h>
voidmyInit(void)
{
glClearColor(2.0,2.0,2.0,4.0);
glColor3f(0.0f,0.0f,0.0f);
glLineWidth(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,640.0,0.0,480.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2i(100,200);
glVertex2i(400,200);
glVertex2i(200,100);
glVertex2i(200,400);
glEnd();
glFlush();
}
Laboratory Programs
OUTPUT:
Viva questions:
3. What is the difference between DDA and Bresenham line drawing algorithm?
2. Create and rotate a triangle about the origin and a fixed point.
OUTPUT:
Viva Questions:
1. What is transformation?
2. Explain the OpenGL functions used for translation, rotation and scaling?
OUTPUT:
Viva Questions:
1. What are vertex arrays?
3. How do you consider the inward and outward pointing of the faces?
4. Draw a color cube and allow the user to move the camera suitably to experiment with
perspective viewing.
OUTPUT:
Viva Questions:
1. Explain the keyboard and mouse events used in this program?
OUTPUT:
Viva Questions:
4. What is an outcode?
6. To draw a simple shaded scene consisting of a tea pot on a table. Define suitably the
position and properties of the light source along with the properties of the surfaces of the
solid object used in the scene.
OUTPUT:
Viva Questions:
5. What Is Transformation?
OUTPUT:
Viva Questions:
4. What is Rasterisation?
OUTPUT:
Viva Questions:
3. Define clipping?
9. Develop a menu driven program to fill the polygon using scan line algorithm
OUTPUT:
Viva Questions:
1. Explain scanline filling algorithm?
*****
Dept. of CSE, AJIET, Mangaluru Page 20