Professional Documents
Culture Documents
501354-3 - Lab Manual
501354-3 - Lab Manual
Prepared By:
Mohamed Rafi
Fayas Asharindavida
2012
COMPUTER GRAPHICS Lab Manual
COMPUTER
GRAPHICS
LAB MANUAL(501354-3)
Prepared By:
Mohammed Rafi
Fayas Asharindavida
College of Computers and IT, Taif University 2
COMPUTER GRAPHICS Lab Manual
Table of Contents
Course Outline ................................................................................................................... 4
References ......................................................................................................................... 49
Week 1
Introduction to OpenGL
Objective:
By the end of this Week the student must be able to write a complete
OpenGL program using C++.
Procedure:
To install the OpenGL library all you have to do are the following steps:
1.Get the OpenGL library or the OpenGL Utility Toolkit Library files ,
and you can find GLUT on the following link:
http://www.xmission.com/~nate/glut/glut-3.7.6-bin.zip
NOTE:
Including the glut library will also provide the main GL functions
which means you don't have to include the both libraries, you can only
include the GLUT and the program will successfully works.
• Glut32.dll.
• Glut32.LIB
• Glut.h
Program 1:
#include <glut.h>
void PatternSegment(void);
void init (void);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400, 300);
glutInitWindowPosition (50, 100);
glutCreateWindow ("Muhammad");
init ();
glutDisplayFunc(PatternSegment);
glutMainLoop();
return 0;
}
void PatternSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glFlush ();
}
Output:
Exercise 1:
1. With the same program change the name of the window to “Taif
University”
2. Change the width and height of the window as 500 and 600
respectively
3. Change the Fore color to Yellow.
4. Change the window position to 100, 75
5. Write a program to accept the input from the user for window
size and create a window.
inputData();
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (winWidth, winHeight);
glutInitWindowPosition (50, 100);
glutCreateWindow ("Muhammad");
init ();
glutDisplayFunc(PatternSegment);
glutMainLoop();
return 0;
}
void PatternSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glFlush ();
}
V
oid init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void inputData()
{
cout<<"Enter the width of the window ";
cin>>winWidth;
cout<<"Enter the height of the window ";
cin>>winHeight;
}
Week 2
OpenGL functions
Objective:
Procedure:
Program 2:
#include <glut.h>
void PatternSegment(void);
void init (void);
Output:
Exercise:
1. With the same program change the color of the dot displayed
2. How you will draw dots with different colors in the same
program.
3. Change the Fore color to Yellow and background color to Black.
4. Write a program to accept input from user for Xmin,Xmax,Ymin
and Ymax .
5. Write a program to create the following pattern.
Week 3
Drawing Primitives
Part 1:
Objective:
Procedure:
Use the For… loop command along with GL_POINTS to create a line
generating program.
Program:
#include <glut.h>
{
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize ( 400, 300);
glutInitWindowPosition ( 50, 50 );
glutCreateWindow ( "Muhammad" );
init ();
glutDisplayFunc ( pointSegment );
glutMainLoop();
return 0;
}
for(int i=10;i<=60;i++)
glVertex2i ( i, 60 );
for(i =10;i<=60;i++)
glVertex2i ( 60, i );
for(i=60;i>=10;i--)
glVertex2i ( i,10 );
for(i=60;i>=10;i--)
glVertex2i (10, i );
glEnd();
glFlush ();
}
void init (void)
{
glClearColor( 1.0 , 1.0, 1.0,0.0 );
glMatrixMode (GL_PROJECTION);
gluOrtho2D ( 0.0, 200.0, 0.0, 150.0 );
}
Output:
Exercise:
1. Modify the above program to draw shapes of different colors and
sizes.
Part 2:
Program:
/* Program to draw lines using glVertex2iv */
#include <glut.h>
Output:
Exercise:
Week 4:
Fill Area Functions
Part 1:
Objective:
To understand the working of GL_POLYGON command.
To work with different versions of GL_QUADS and GL_TRIANGLES.
To draw different shapes with these commands.
Procedure:
We start with the program to draw simple polygon and convert it with
GL_QUADS command for drawing different shape.
Program:
#include <glut.h>
glBegin(GL_POLYGON);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glVertex2iv(p5);
glVertex2iv(p6);
glEnd();
glFlush ();
}
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
Output:
Exercise:
1.Experiment with the above program to create different shapes by
changing GL_POLYGON with GL_TRIANGLES and its different versions as
shown below.
Part 2:
Procedure:
Program:
glBegin(GL_QUADS);
glVertex2iv(p1);
glVertex2iv(p2);
glVertex2iv(p3);
glVertex2iv(p4);
glColor3f (1.0, 1.0, 0.0);
glVertex2iv(p4);
glVertex2iv(p3);
glVertex2iv(p5);
glVertex2iv(p6);
glEnd();
glFlush ();
}
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
Output:
Exercise:
1. Change the GL_QUADS parameter to GL_QUAD_STRIP and observe
the changes.
Week 5:
Procedure:
We draw a circle with GL_TRIANGLE_FAN command and experiment
with GL_LINE_LOOP.
Program:
#include <glut.h>
#include <math.h>
void DesignSegment(void);
void init (void);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
Output:
Exercise:
Week 6:
Procedure:
Name
glPushMatrix, glPopMatrix
push and pop the current matrix stack
C Specification
void glPushMatrix( void )
Description
There is a stack of matrices for each of the matrix modes.
In GL_MODELVIEW mode,the stack depth is at least 32.
Program:
#include <glut.h>
void PatternSegment(void);
void init (void);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400, 300);
glutInitWindowPosition (50, 100);
glutCreateWindow ("Muhammad");
init ();
glutDisplayFunc(PatternSegment);
glutMainLoop();
return 0;
}
void PatternSegment(void)
{
void Tri();
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f (1.0, 0.0, 0.0);
Tri();
glTranslatef(10.0,10.0,0.0);
glColor3f (0.0, 0.0, 1.0);
Tri();
glPopMatrix();
glutSwapBuffers();
glFlush ();
}
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void Tri()
{
glBegin(GL_TRIANGLES);
glVertex2i(10, 10);
glVertex2i(60, 10);
glVertex2i(30, 60);
glEnd();
}
Output:
glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(2,0);
glVertex2i(2,2);
glVertex2i(0,2);
glEnd();
glTranslate(0,1,0);
glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(2,0);
glVertex2i(2,2);
glVertex2i(0,2);
glEnd();
Output:
Exercise:
1. Change the above program to draw two polygons and get the input
from the user for translation value.
Week 7:
Objective:
Procedure:
Program:
#include <glut.h>
void PatternSegment(void);
void init (void);
void Tri();
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400, 300);
glutInitWindowPosition (50, 100);
glutCreateWindow ("Muhammad");
init ();
glutDisplayFunc(PatternSegment);
glutMainLoop();
return 0;
}
void PatternSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f (1.0, 0.0, 0.0);
Tri();
glScalef(10.0,10.0,0.0);
glColor3f (0.0, 1.0, 0.0);
Tri();
glPopMatrix();
glFlush ();
}
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
void Tri()
{
glBegin(GL_TRIANGLES);
glVertex2i(10, 10);
glVertex2i(60, 10);
glVertex2i(30, 60);
glEnd();
}
Output:
Exercise:
1. Change the above program to draw two Circles and get the input from
the user for scale value.
Example of Scaling
glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(4,0);
glVertex2i(4,4);
glVertex2i(0,4);
glEnd();
glScaled(2,2,1);
glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(4,0);
glVertex2i(4,4);
glVertex2i(0,4);
glEnd();
Output:
Week 8:
Two Dimensional Rotation
Objective:
Procedure:
Program:
#include <glut.h>
void PatternSegment(void);
void init (void);
void Tri();
void PatternSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f (1.0, 0.0, 0.0);
Tri();
glRotatef(10.0,1.0,1.0,0.0);
glColor3f (0.0, 0.0, 1.0);
Tri();
glPopMatrix();
glFlush ();
}
void Tri()
{
glBegin(GL_TRIANGLES);
glVertex2i(10, 10);
glVertex2i(60, 10);
glVertex2i(30, 60);
glEnd();
}
Output:
glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(4,0);
glVertex2i(4,4);
glVertex2i(0,4);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2i(0,0);
glVertex2i(4,0);
glVertex2i(4,4);
glVertex2i(0,4);
glEnd();
Output:
Exercise:
1. Change the above program to draw Hollow Square and get the input
from the user for angle and axis value.
2. How will you draw a Quadrilateral and then translate, rotate and scale
the same.
Week 9:
College of Computers and IT, Taif University 35
COMPUTER GRAPHICS Lab Manual
Procedure:
Name
gluPerspective - set up a perspective projection matrix
C Specification
void gluPerspective( GLdouble fovy,
GLdouble aspect,
GLdouble zNear,
GLdouble zFar )
Parameters
fovy Specifies the field of view angle, in degrees, in
the y direction.
Name
glLoadIdentity - replace the current matrix with the
identity matrix
C Specification
void glLoadIdentity( void )
Description
glLoadIdentity replaces the current matrix with the identity
matrix
Name
glEnable, glDisable - enable or disable server-side GL
capabilities
C Specification
void glEnable( GLenum cap )
Parameters
cap Specifies a symbolic constant indicating a GL
capability.
C Specification
void glDisable( GLenum cap )
Parameters
cap Specifies a symbolic constant indicating a GL
capability.
Description
glEnable and glDisable enable and disable various
capabilities.
Program:
#include <glut.h>
void PatternSegment(void);
void init (void);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
Output:
Exercise:
1. Change the above program to draw a 3D cube using glSolidCube(3).
2. Translate the cube to new location (0,-2,-5).
3. Change the above program to draw a 3D cube using glWireCube(3)
and glutTeapot(2.0) at different locations.
4. How you will use the program to draw the following.
WireTeapot
SolidCone
WireCone
SolidCube
WireCube
SolidDodecahedron
WireDodecahedron
SolidIcosahedron
WireIcosahedron
SolidOctahedron
WireOctahedron
WireSphere
SolidTetrahedron
WireTetrahedron
SolidTorus
WireTorus
WireTeapot glutWireTeapot(size)
Week 10:
Three Dimensional Transformations
College of Computers and IT, Taif University 39
COMPUTER GRAPHICS Lab Manual
Objective:
Procedure:
We can move the 3D objects to different locations , scale and rotate
them using the transformation commands.
Program:
#include <glut.h>
void PatternSegment(void);
void init (void);
Exercise:
Week 11:
Objective:
Procedure:
We can move the 3D objects to different locations using the keys of
the keyboard.
Program:
#include <glut.h>
int rot = 0;
void PatternSegment(void);
void init (void);
void Keyboard (unsigned char,int, int);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize (800, 600);
glutInitWindowPosition (50, 100);
glutCreateWindow ("Muhammad");
init ();
glutDisplayFunc(PatternSegment);
glutKeyboardFunc(Keyboard);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutMainLoop();
return 0;
}
void PatternSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rot,0,1,0);
glTranslatef(0,-3,-8);
glutSolidTeapot(1);
glFlush ();
}
void init (void)
{ glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void Keyboard(unsigned char Key, int x, int y)
{
if (Key==27) exit(1);
if(++rot>359) rot=0;
glutPostRedisplay();
Output:
Exercise:
1. Change the above program to rotate a 3D cube using key ‘A’ for
clockwise and key ‘B’ for anticlockwise
Week 12:
Procedure:
We can move the 3D objects to different locations using the buttons of
the mouse.
Program:
#include <glut.h>
int rot = 0;
void PatternSegment(void);
void init (void);
void Mouse(int,int,int,int);
}
void Mouse(int button, int state, int x, int y)
{ if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
{
if (++rot>359) rot=0;
glutPostRedisplay();
}
}
Output:
Week 13:
Operations on Three Dimensional Objects
Objective:
Procedure:
We can apply different colors to the objects by using glMaterialfv
command. First we have to create colors and then use those colors in
glMaterialfv command.
Program:
#include <glut.h>
int rot = 0;
float red[3] = {1.0, 0.0, 0.0};
float green[3] = {0.0, 1.0, 0.0};
float blue[3] = {0.0, 0.0, 1.0};
float white[3] = {1.0, 1.0, 1.0};
float brown[3] = {1.0, 0.6, 0.3};
float yellow[3] = {1.0, 1.0, 0.0};
float black[3] = {0.0, 0.0, 0.0};
void PatternSegment(void);
void init (void);
glutSolidCube(2);
glFlush ();
}
void init (void)
{ glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
Output:
Exercise:
Week 14:
Procedure:
We can apply different light effects to the objects by using glLightfv
command. First we have to define the position and the color of the diffused
light and then use the lights.
Program:
#include <glut.h>
void PatternSegment(void);
void init (void);
void PatternSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,-3,-8);
glutSolidTeapot(3);
glFlush ();
}
glLoadIdentity();
gluPerspective(90.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
Output:
Exercise:
References
1. www.google.com
College of Computers and IT, Taif University 49
COMPUTER GRAPHICS Lab Manual
2. www.opengl.org
3. Computer Graphics with Open GL, by Donald D. Hearn, M. Pauline Baker,
Warren Carithers.
4. www.opengl.org/resources/libraries/glut/glut37.zip
5. http://ww2.cs.mu.oz.au/380/project/glut/glut_installation.txt