Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

TAIF UNIVERSITY

COLLEGE OF COMPUTERS & IT


Kingdom of Saudi Arabia

COMPUTER GRAPHICS (501354-3)


Lab Manual

Prepared By:
Mohamed Rafi
Fayas Asharindavida

2012
COMPUTER GRAPHICS Lab Manual

College of computers & IT, Taif University, Saudi Arabia

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

Week 1 : Introduction to OpenGL ............................................................................. 6

Week 2 : OpenGL functions ...................................................................................... 10

Week 3 : Drawing Primitives .................................................................................... 13

Week 4 : Fill Area Functions .................................................................................... 18

Week 5 : Circle Drawing Algorithms ..................................................................... 24

Week 6 : Two Dimensional Translation ............................................................... 26

Week 7 : Two Dimensional Scaling ....................................................................... 29

Week 8 : Two Dimensional Rotation .................................................................... 32

Week 9 : Three Dimensional Drawing .................................................................. 35

Week 10 : Three Dimensional Transformation ................................................ 39

Week 11 : Three Dimensional Transformation with keyboard ................. 41

Week 12 : Three Dimensional Transformation with Mouse ....................... 43

Week 13 : Operations on Three Dimensional Objects .................................. 45

Week 14 : Lighting effects on Three Dimensional Objects ........................ 47

References ......................................................................................................................... 49

College of Computers and IT, Taif University 3


COMPUTER GRAPHICS Lab Manual

Class Learning Outcomes

Apply mathematics, physics and computer programming to computer graphics


CLO1 applications and problem solutions.
Imagine how CRT, raster, and plasma displays work.
CLO2
Draw most geometric primitives such as pixel, line, circle, and ellipse
CLO3 and to able to fill color and draw the shapes.
Apply the transformations and deformations on 2D/3D object
CLO4
Implement graphics problem using OpenGl.
CLO5
Introduction to OpenGL CLO5
1
OpenGL functions CLO5
2
Drawing Primitives CLO3,CLO5
3
Fill Area Functions CLO3,CLO5
4
Circle Drawing Algorithms CLO3,CLO5
5
Two Dimensional Translation CLO4,CLO5
6
Two Dimensional Scaling CLO4,CLO5
7
Two Dimensional Rotation CLO4,CLO5
8
Three Dimensional Drawing CLO4,CLO5
9
Three Dimensional Transformation CLO4,CLO5
10

College of Computers and IT, Taif University 4


COMPUTER GRAPHICS Lab Manual

Course Outline [Computer Graphics Lab -501354-3 ]


Week 1: Introduction to OpenGL
• Objectives.
• What is OpenGL and how does work.
• Basic OpenGL Syntax.
• OpenGL Related Libraries.
• Library installation.

Week 2 : OpenGL functions


• Display-Window Management.
• Writing a simple displaying function.
• Writing a complete simple OpenGL program.

Week 3: Drawing Primitives


• Line drawing primitives
• Pattern drawing
• Writing a simple line drawing program for implementing
Bresenham’s Algorithm

Week 4: Fill Area Functions


• Polygon Drawing
• Filling Triangle, Quadrilaterals, Polygons

Week 5: Circle Drawing Algorithms


• Circle Drawing using polar coordinates
• Midpoint Algorithm

Week 6: Two Dimensional Translation


• Two Dimensional Scaling using glTranslatef command

Week 7: Two Dimensional Scaling


• Two Dimensional Scaling using glScalef command

Week 8: Two Dimensional Rotation


• Two Dimensional Scaling using glRotatef command

Week 9: Three Dimensional Drawing


• Three dimensional drawing using glPerspective command

Week 10: Three Dimensional Transformation


• To implement three dimensional transformation using glRotatef, glTranslatef
and glScalef commands.

Week 11: Three Dimensional Transformation with Keyboard


• Invoking different keyboard functions
• Manipulating the transformation using keys

College of Computers and IT, Taif University 5


COMPUTER GRAPHICS Lab Manual

Week 12: Three Dimensional Transformation with Mouse


• Writing code for Mouse functions
• Handling transformation using Mouse events

Week 13: Operations on Three Dimensional Objects


• Mapping of materials to the 3D objects
• Shading objects

Week 14: Lighting effects on Three Dimensional Objects


• Implement lighting effects on 3D objects
• To create different effects

College of Computers and IT, Taif University 6


COMPUTER GRAPHICS Lab Manual

Week 1
Introduction to OpenGL
Objective:

This Week introduces the student to the main concept of OpenGL


explaining how graphics are generated through the OpenGL library by
describing the structure of OpenGL, in this Week the student should get to
understand the following:

1. How to install the OpenGL/GLUT library files into the system.


2. Understand the basic syntax of OpenGL functions.
3. Understand the purpose of each function invoked.
4. Writing a simple routine to create line segment.
5. Understanding the displaying callback function.

By the end of this Week the student must be able to write a complete
OpenGL program using C++.

Procedure:

Installing OpenGL Libraries:

Installation of OpenGL differs from operating system to another, and


from compiler to another , because each system like Linux , Win , or Mac
has different way of sorting system files , the same issue with compilers ,
but since we are using Microsoft Visual Studio 98 C++ then we are only
going to demonstrate how to install OpenGL on Windows system.

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.

By extracting the compressed files you will find the following:

• Glut32.dll.
• Glut32.LIB
• Glut.h

College of Computers and IT, Taif University 7


COMPUTER GRAPHICS Lab Manual

Now copy each file to the following indicated folder:


Name of File Where it should be copied?
Glut32.dll c:/Windows/System32
Glut32.LIB c:/Program files/Microsoft visual 98 /VC98/Lib
Glut.h c:/Program files/Microsoft visual 98 /VC98/include

Program 1:

/* Program to create a Window titled Muhammad */

#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)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
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);
}

College of Computers and IT, Taif University 8


COMPUTER GRAPHICS Lab Manual

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.

College of Computers and IT, Taif University 9


COMPUTER GRAPHICS Lab Manual

Solution for question 5.

/* Program to create a Window with input from user */


#include <glut.h>
#include <iostream.h>
float winWidth,winHeight;
void PatternSegment(void);
void init (void);
void inputData();

int main(int argc, char** argv)


{

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;
}

College of Computers and IT, Taif University 10


COMPUTER GRAPHICS Lab Manual

Week 2
OpenGL functions
Objective:

To understand the working of the coordinate system, the gluOrtho2D


command with its different parameters, and to draw points at different
locations.

Procedure:

We can use the program to create window and modify it to draw a


single point at a particular coordinate.

We will change the parameters of the gluOrtho2D to understand its


working by drawing points at different coordinate positions.

Program 2:

/* Program to draw a point at vertices 100,50 */

#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)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 1.0);
glPointSize(15.0);
glBegin(GL_POINTS );
glVertex2i(100,50);
glEnd();
glFlush ();
}

College of Computers and IT, Taif University 11


COMPUTER GRAPHICS Lab Manual

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:

College of Computers and IT, Taif University 12


COMPUTER GRAPHICS Lab Manual

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.

College of Computers and IT, Taif University 13


COMPUTER GRAPHICS Lab Manual

Week 3
Drawing Primitives
Part 1:

Objective:

To understand the working of GL_LINES command and its different


versions such as GL_LINE_LOOP and GL_LINE_STRIP.
To draw different line patterns with these commands.
To write a simple program that would implement Bresenham’s line
drawing Algorithm.

Procedure:

We start with the program to draw a point and convert it with


GL_LINES command for drawing a basic line.

We experiment with GL_LINE_LOOP and GL_LINE_STRIP by changing


the GL_LINES command.

Use the For… loop command along with GL_POINTS to create a line
generating program.

Program:

#include <glut.h>

void pointSegment ( void );


void init ( void );

int main ( int argc, char** argv )

{
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize ( 400, 300);
glutInitWindowPosition ( 50, 50 );
glutCreateWindow ( "Muhammad" );
init ();
glutDisplayFunc ( pointSegment );
glutMainLoop();
return 0;
}

void pointSegment ( void )


{
glClear (GL_COLOR_BUFFER_BIT);

College of Computers and IT, Taif University 14


COMPUTER GRAPHICS Lab Manual

glColor3f ( 1.0, 0.0, 0.0 );


glPointSize ( 5.0 );
glBegin ( GL_POINTS );

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.

College of Computers and IT, Taif University 15


COMPUTER GRAPHICS Lab Manual

Part 2:

College of Computers and IT, Taif University 16


COMPUTER GRAPHICS Lab Manual

Program:
/* Program to draw lines using glVertex2iv */

#include <glut.h>

void LineSegment (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(LineSegment);
glutMainLoop();
return 0;
}
void LineSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
glLineWidth(5.0);
int p1[] = {10,50};
int p2[] = {50,10};
int p3[] = {10,10};
int p4[] = {50,50};
glBegin(GL_LINES);
glVertex2iv(p1);
glVertex2iv(p2); /* line 1 with end points p1 and p2 */
glVertex2iv(p3);
glVertex2iv(p4); /* line 2 with end points p3 and p4 */
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);
}

College of Computers and IT, Taif University 17


COMPUTER GRAPHICS Lab Manual

Output:

Exercise:

1.Write a program to create the following design.

College of Computers and IT, Taif University 18


COMPUTER GRAPHICS Lab Manual

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.

We experiment with GL_TRIANGLES and its different versions

Program:

/* Program to draw a simple polygon using glVertex2iv */

#include <glut.h>

void LineSegment (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(LineSegment);
glutMainLoop();
return 0;
}
void LineSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
int p1[] = {10,25};
int p2[] = {15,10};
int p3[] = {60,10};
int p4[] = {65,25};
int p5[] = {60,40};
int p6[] = {15,40};

College of Computers and IT, Taif University 19


COMPUTER GRAPHICS Lab Manual

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.

College of Computers and IT, Taif University 20


COMPUTER GRAPHICS Lab Manual

Part 2:

College of Computers and IT, Taif University 21


COMPUTER GRAPHICS Lab Manual

Procedure:

GL_QUADS and GL_QUAD_STRIP commands

Program:

College of Computers and IT, Taif University 22


COMPUTER GRAPHICS Lab Manual

/* Program to draw simple Quadrilateral using glVertex2iv */


#include <glut.h>

void LineSegment (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(LineSegment);
glutMainLoop();
return 0;
}
void LineSegment(void)
{ glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 0.0);
int p1[] = {10,80};
int p2[] = {15,10};
int p3[] = {60,10};
int p4[] = {60,80};
int p5[] = {100,10};
int p6[] = {110,80};

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:

College of Computers and IT, Taif University 23


COMPUTER GRAPHICS Lab Manual

Exercise:
1. Change the GL_QUADS parameter to GL_QUAD_STRIP and observe
the changes.

Week 5:

College of Computers and IT, Taif University 24


COMPUTER GRAPHICS Lab Manual

Circle Drawing Algorithms


Objective:

To implement circle drawing algorithm. To draw a shaded circle using


polar coordinates.

Procedure:
We draw a circle with GL_TRIANGLE_FAN command and experiment
with GL_LINE_LOOP.

Program:

// Opengl program to draw a shaded circle

#include <glut.h>
#include <math.h>

float angle, radius, xx1,yy1;

void DesignSegment(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(DesignSegment);
glutMainLoop();
return 0;
}
void DesignSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
xx1 = 100; yy1 = 75; radius =25;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(xx1, yy1);
for (angle=0;angle<=360;angle++)
glVertex2f(xx1+sin(angle)*radius,yy1+cos(angle)*radius);
glEnd();
glFlush ();
}
void init (void)
{ glClearColor (1.0, 1.0, 1.0, 0.0);
College of Computers and IT, Taif University 25
COMPUTER GRAPHICS Lab Manual

glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}

Output:

Exercise:

1.Change GL_TRIANGLE _FAN to GL_LINE_LOOP to draw a hollow circle.


2.Change the above program to get input the value of circle and radius.

Week 6:

College of Computers and IT, Taif University 26


COMPUTER GRAPHICS Lab Manual

Two Dimensional Translation


Objective:

To implement two dimensional translation using glTranslatef


command.

Procedure:

glTranslatef command helps us to move objects from one location to


another. This command has three parameters one for each axis.

Name
glPushMatrix, glPopMatrix
push and pop the current matrix stack
C Specification
void glPushMatrix( void )

void glPopMatrix( 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.

glPushMatrix pushes the current matrix stack down by one,


duplicating the current matrix. That is, after a glPushMatrix call,
the matrix on top of the stack is identical to the one below it.

glPopMatrix pops the current matrix stack, replacing the


current matrix with the one below it on the stack.

Initially, each of the stacks contains one matrix, an identity matrix.

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;

College of Computers and IT, Taif University 27


COMPUTER GRAPHICS Lab Manual

}
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:

Example for Translation


College of Computers and IT, Taif University 28
COMPUTER GRAPHICS Lab Manual

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:

College of Computers and IT, Taif University 29


COMPUTER GRAPHICS Lab Manual

Two Dimensional Scaling

Objective:

To implement two dimensional scaling using glScalef command.

Procedure:

glScalef command helps us to scale objects from one size to another.


This command has three parameters one for each axis.

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()

College of Computers and IT, Taif University 30


COMPUTER GRAPHICS Lab Manual

{
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.

College of Computers and IT, Taif University 31


COMPUTER GRAPHICS Lab Manual

Example of Scaling

glScale[fd](Sx, Sy, Sz)

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:

College of Computers and IT, Taif University 32


COMPUTER GRAPHICS Lab Manual

Week 8:
Two Dimensional Rotation
Objective:

To implement two dimensional rotation using glRotatef command.

Procedure:

glRotatef command helps us to rotate objects from one angle to


another. This command has four parameters one angle and rest for each
axis.

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();
glRotatef(10.0,1.0,1.0,0.0);
glColor3f (0.0, 0.0, 1.0);
Tri();
glPopMatrix();
glFlush ();
}

College of Computers and IT, Taif University 33


COMPUTER GRAPHICS Lab Manual

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:

Example for Rotation


College of Computers and IT, Taif University 34
COMPUTER GRAPHICS Lab Manual

glBegin(GL_LINE_LOOP);

glVertex2i(0,0);

glVertex2i(4,0);

glVertex2i(4,4);

glVertex2i(0,4);

glEnd();

glRotatef(45, 0.0, 0.0, 1.0);

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

Three Dimensional Drawing


Objective:

To implement three dimensional drawing using glPerspective


command.

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.

aspect Specifies the aspect ratio that determines the field


of view in the x direction. The aspect ratio is the
ratio of x (width) to y (height).

zNear Specifies the distance from the viewer to the near


clipping plane (always positive).

zFar Specifies the distance from the viewer to the far


clipping plane (always positive).
Description
gluPerspective specifies a viewing frustum into the world
coordinate system. In general, the aspect ratio in
gluPerspective should match the aspect ratio of the
associated viewport. For example, aspect=2.0 means the
viewer's angle of view is twice as wide in x as it is in y.
If the viewport is twice as wide as it is tall, it displays
the image without distortion.

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

College of Computers and IT, Taif University 36


COMPUTER GRAPHICS Lab Manual

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);

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);
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();
glTranslatef(0,-3,-8);
glutSolidTeapot(1.5);
glFlush ();
}

void init (void)


{ glClearColor (1.0, 1.0, 1.0, 0.0);

College of Computers and IT, Taif University 37


COMPUTER GRAPHICS Lab Manual

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)

College of Computers and IT, Taif University 38


COMPUTER GRAPHICS Lab Manual

SolidCone glutSolidCone(base , height , slices , stacks )


WireCone glutWireCone(base , height , slices , stacks )
SolidCube glutSolidCube(size )
WireCube glutWireCube(size )
SolidDodecahedron 12 glutSolidDodecahedron()
Sided
WireDodecahedron ‘’ glutWireDodecahedron()
SolidIcosahedron 20 glutSolidIcosahedron()
Sided
WireIcosahedron glutWireIcosahedron()
SolidOctahedron 8 glutSolidOctahedron()
Sided
WireOctahedron glutWireOctahedron()
WireSphere glutWireSphere(radius , slices , stacks )
SolidTetrahedron 4 glutSolidTetrahedron()
Sided
WireTetrahedron glutWireTetrahedron()
SolidTorus glutSolidTorus ( innerRadius , outerRadius , nsides , rings )

WireTorus glutWireTorus ( innerRadius , outerRadius , nsides , rings )

Week 10:
Three Dimensional Transformations
College of Computers and IT, Taif University 39
COMPUTER GRAPHICS Lab Manual

Objective:

To implement three dimensional transformation using glRotatef,


glTranslatef and glScalef commands.

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);

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);
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();
glTranslatef(0,-3,-8);
glRotatef(60,0,1,0);
glutSolidTeapot(3);
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:

College of Computers and IT, Taif University 40


COMPUTER GRAPHICS Lab Manual

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.

Week 11:

College of Computers and IT, Taif University 41


COMPUTER GRAPHICS Lab Manual

Three Dimensional Transformations with Keyboard

Objective:

To implement three dimensional transformation with keyboard


interaction.

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);

College of Computers and IT, Taif University 42


COMPUTER GRAPHICS Lab Manual

}
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:

College of Computers and IT, Taif University 43


COMPUTER GRAPHICS Lab Manual

Three Dimensional Transformations with Mouse


Objective:

To implement three dimensional transformation with mouse


interaction.

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);

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);
glutMouseFunc(Mouse);
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);

College of Computers and IT, Taif University 44


COMPUTER GRAPHICS Lab Manual

}
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

College of Computers and IT, Taif University 45


COMPUTER GRAPHICS Lab Manual

Objective:

To implement mapping of materials to the 3D objects and to create 3D


objects with different colors.

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);

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);
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);
glMaterialfv(GL_FRONT, GL_DIFFUSE, green);
glTranslatef(0,-3,-8);
glutSolidTeapot(1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
glTranslatef(0,-8,-8);

College of Computers and IT, Taif University 46


COMPUTER GRAPHICS Lab Manual

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:

1. Change the above program to draw a 3D Wire-Icosahedron in blue


color and Solid Torus in magenta.

Week 14:

College of Computers and IT, Taif University 47


COMPUTER GRAPHICS Lab Manual

Lighting Effects on Three Dimensional Objects


Objective:
To implement lighting effects on 3D objects and to create different
effects.

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);

int main(int argc, char** argv)


{
GLfloat light_position[] = {50.0, 50.0, 50.0, 10.0};
GLfloat diffuse_light[] = {0.0, 0.0, 1.0, 0.0};
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize (800, 600);
glutInitWindowPosition (50, 100);
glutCreateWindow ("Muhammad");
init ();
glutDisplayFunc(PatternSegment);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv (GL_LIGHT0, GL_POSITION, light_position);
glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light);
glutMainLoop();
return 0;
}

void PatternSegment(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,-3,-8);
glutSolidTeapot(3);
glFlush ();
}

void init (void)


{ glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);

College of Computers and IT, Taif University 48


COMPUTER GRAPHICS Lab Manual

glLoadIdentity();
gluPerspective(90.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}

Output:

Exercise:

1. Change the above program to draw a 3D Wire Tetrahedron in green


light.

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

College of Computers and IT, Taif University 50

You might also like