0% found this document useful (0 votes)
73 views30 pages

1.1 About Computer Graphics

The document provides an overview of computer graphics and OpenGL, and describes a project to simulate the Tower of Hanoi problem. It includes 3 chapters: Introduction, Requirement Specification, and Design. The Introduction defines key computer graphics concepts and describes OpenGL. The Requirement Specification outlines the necessary software and hardware. The Design section explains the recursive solution algorithm and overall project design.

Uploaded by

kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views30 pages

1.1 About Computer Graphics

The document provides an overview of computer graphics and OpenGL, and describes a project to simulate the Tower of Hanoi problem. It includes 3 chapters: Introduction, Requirement Specification, and Design. The Introduction defines key computer graphics concepts and describes OpenGL. The Requirement Specification outlines the necessary software and hardware. The Design section explains the recursive solution algorithm and overall project design.

Uploaded by

kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 1

INTRODUCTION
1.1 About Computer Graphics
The Computer Graphics is one of the most effective and commonly used methods to
communicate the processed information to the user. It displays the information in the form of
graphics objects such as pictures, charts, graphs and diagram instead of simple text.

In computer graphics, pictures or graphics objects are presented as a collection of discrete


picture elements called pixels. The pixel is the smallest addressable screen element

Computer graphics today is largely interactive: The user controls the contents structure,
and appearance of objects and their displayed images by using input devices, such as a keyboard,
mouse, or touch-sensitive panel on the screen.
Computer Graphics relies on an internal model of the scene, that is, mathematical
representation suitable for graphical computations. The model describes the 3D shapes, layout
and materials of the scene. This 3D representation then has to be projected to compute a 2D
image from a given viewpoint, this is rendering step. Rendering involves projecting the objects,
handling visibility (which parts of objects are hidden) and computing their appearance and
lighting interactions. Finally, for animated sequence, the motion of objects has to be specified.

1.2 About OpenGL


OpenGL (Open Graphics Library) is a standard specification defining a cross-language,
cross-platform API for writing applications that produce 2D and 3D computer graphics. The
interface consists of over 250 different function calls which can be used to draw complex three-
dimensional scenes from simple primitives. OpenGL was developed by Silicon Graphics Inc.

OpenGL's basic operation is to accept primitives such as points, lines and polygons, and
convert them into pixels. This is done by a graphics pipeline known as the OpenGL state
machine.
Features of OpenGL:

 Geometric Primitives allow you to construct mathematical descriptions of objects.


 Viewing and Modeling permits arranging objects in a 3-dimensional scene, move our
camera around space and select the desired vantage point for viewing the scene to be
rendered.
 Materials lighting OpenGL provides commands to compute the color of any point given
the properties of the material and the sources of light in the room.
 Transformations: rotation, scaling, translations, perspectives in 3D, etc.

1.3 About Project


The project simulates the optimal solution of the Tower of Hanoi problem given a
number of disk. The project implements various geometric transformations like Translation,
Rotation, and Scaling. The basic model consists of three poles, source, auxiliary and destination.
The disks initially resting on the source pole reaches the destination. The poles are drawn using
the GLUT library function glutSolidCone(), and the disks are drawn using
glutSolidTorus().Initially the user is presented with an introduction scene which has a menu to
choose the number of disks. On pressing the Enter key the actual simulation scene is loaded. The
user can use the mouse wheel to advance through the simulation. An optional animation has been
implemented, which shows the movement of the disks from pole to pole. The viewing model
used is an Orthogonal Projection. The moves to be performed as per the optimal solution are
displayed on the top left of the screen is a raster text using the function glutBitmapCharacter().
Lighting has been implemented by the inbuilt OPENGL lighting functions. Menus have been
provided to modify various features such as Lighting, Move camera, animation, change
background color, to automatically simulate the complete solution, restart the simulation and to
exit the program. The movement of the camera is only along the Y axis, and is implemented
using the glutLookAt() function.
Chapter 2

REQUIREMENT SPECIFICATION
The requirement specification is a comprehensive description of the software and the
hardware requirements required to run the project successfully.

2.1 Software Requirements


 Operating System- WINDOWS 7/WINDOWS 8/ WINDOWS XP/ WINDOWS 10
 Language Used- OpenGL and C
 Graphic card
 Code blocks IDE

2.2 Hardware Requirements:


 Processor : Intel/AMD processor.
 RAM : 512MB.
 Input : Keyboard/Mouse.
 Display : Monitor.
 Memory : 4GB.
Chapter 3

DESIGN
The design specifies the design of various aspects and different stages of the project.
Design is the ultimate frame work of the project. In essence the design is a plan or a mindful
blueprint of the project to be developed.

3.1 Recursive Solution:

A key to solving this puzzle is to recognize that it can be solved by breaking the problem
down into a collection of smaller problems and further breaking those problems down into even
smaller problems until a solution is reached. For example:

 label the pegs A, B, C.

 let n be the total number of discs.

 number the discs from 1 (smallest, topmost) to n (largest, bottommost).

To move n discs from peg A to peg C:

1. Move n−1 discs from A to B. This leaves disc n alone on peg A.

2. Move disc n from A to C.

3. Move n−1 discs from B to C so they sit on disc n.

The above is a recursive algorithm, to carry out steps 1 and 3, apply the same algorithm
again for n−1. The entire procedure is a finite number of steps, since at some point the algorithm
will be required for n = 1. This step, moving a single disc from peg A to peg C, is trivial. This
approach can be given a rigorous mathematical formalism with the theory of dynamic
programming and is often used as an example of recursion when teaching programming.
Chapter 4
IMPLEMENTATION
Implementation is the stage where all planned activities are put into action. Before the
implementation of a project, the implementers (spearheaded by the project committee or
executive) should identify their strength and weaknesses (internal forces), opportunities and
threats (external forces).

4.1 Complete Source Code of the Package:

#include<freeglut.h>

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#include<string.h>

#include<unistd.h>

#define LIGHT_ON 0

#define LIGHT_OFF 1

int pos[16] = {10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85};

int peg[3] = {50,150,250};

int moves[10000][3];

int max_moves;

int POLES[3][10];

int top[3]={-1,-1,-1};

int NUM_DISKS=3;

int cnt,counter,speed=2;

int line1=90,line2=85;

float ycoordinate;
int lightflag=1,animationFlag=1,randomColorFlag=0;

void push(int p,int disk)

POLES[p][++top[p]] = disk;

void pop(int p)

top[p]--;

void tower(int n,int src,int temp,int dst)

if(n>0)

tower(n-1,src,dst,temp);

moves[cnt][0] = n;

moves[cnt][1] = src;

moves[cnt][2] = dst;

cnt++;

tower(n-1,temp,src,dst);

void drawPegs()

int i;

glColor3f(0.5,0.0,0.1);
for(i=0;i<3;i++)

glPushMatrix();

glTranslatef(peg[i],5,0);

glRotatef(-90,1,0,0);

glutSolidCone(2,70,20,20);

glutSolidTorus(2,45, 20, 20);

glPopMatrix();

void printString(char *text)

int len=strlen(text),i;

for(i=0;i<len;i++)

glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,text[i]);

void drawText()

glColor3f(1,1,1);

glRasterPos3f(-70,line1,0);

printString("Move :");

char str[5];

sprintf(str, "%d", counter);

glRasterPos3f(-40,line1,0);

printString(str);
glRasterPos3f(-70,line2,0);

printString("Disk");

char str1[10];

sprintf(str1, "%d", moves[counter][0]);

glRasterPos3f(-50,line2,0);

printString(str1);

glRasterPos3f(-40,line2,0);

printString("from");

char src[2];

if(moves[counter][1]==0)strcpy(src,"A");

else if(moves[counter][1]==1)strcpy(src,"B");

else strcpy(src,"C");

glRasterPos3f(-20,line2,0);

printString(src);

glRasterPos3f(-10,line2,0);

printString("to");

char dst[2];

if(moves[counter][2]==0)strcpy(dst,"A");

else if(moves[counter][2]==1)strcpy(dst,"B");

else strcpy(dst,"C");

glRasterPos3f(0,line2,0);

printString(dst);

glColor3f(0.6,0.3,0.5);

glBegin(GL_POLYGON);

glVertex3f(-75,93,-5);
glVertex3f(-75,83,-5);

glVertex3f(10,83,-5);

glVertex3f(10,93,-5);

glEnd();

glColor3f(1,0,0);

glRasterPos3f(peg[0],70,0);

glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'A');

glRasterPos3f(peg[1],70,0);

glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'B');

glRasterPos3f(peg[2],70,0);

glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'C');

void drawSolved()

glColor3f(1,1,0);

glRasterPos3f(-60,87,0);

printString("Solved !!");

glColor3f(0.6,0.3,0.5);

glBegin(GL_POLYGON);

glVertex3f(-75,93,-5);

glVertex3f(-75,83,-5);

glVertex3f(10,83,-5);

glVertex3f(10,93,-5);

glEnd();
glColor3f(1,0,0);

glRasterPos3f(peg[0],70,0);

glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'A');

glRasterPos3f(peg[1],70,0);

glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'B');

glRasterPos3f(peg[2],70,0);

glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,'C');

void display()

int i,j,k;

if(randomColorFlag)

glClearColor((rand()%100)/100.0,(rand()%100)/100.0,(rand()%100)/100.0,0);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

if(lightflag)glEnable(GL_LIGHTING);

glPushMatrix();

gluLookAt(0,ycoordinate,0,0,0,-1,0,1,0);

drawPegs();

for(i=0;i<3;i++)

k=0;

for(j=0;j<=top[i];j++)

glPushMatrix();
glTranslatef(peg[i],pos[k++],0);

glRotatef(90,1,0,0);

glColor3f(0.1*POLES[i][j],0.2*POLES[i][j],0);

glutSolidTorus(2.0, 4*POLES[i][j], 20, 20);

glPopMatrix();

glPopMatrix();

glDisable(GL_LIGHTING);

if(counter==max_moves)

drawSolved();

else

drawText();

if(lightflag)glEnable(GL_LIGHTING);

glutSwapBuffers();

void lighting()

GLfloat shininess[] = {50};

GLfloat white[] = {0.6,0.6,0.6,1};

glEnable(GL_COLOR_MATERIAL);

glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);

GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };

GLfloat light_position[] = {100,60, 10, 0.0 };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);


glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glMaterialfv(GL_FRONT, GL_DIFFUSE, white);

glMaterialfv(GL_FRONT, GL_SPECULAR, white);

glMaterialfv(GL_FRONT, GL_SHININESS, shininess);

glEnable(GL_LIGHT0);

void init()

glClearColor(0.0,0.0,0.0,0);

glColor3f(1,0,0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-80,350,-10,100,-100,100);

glMatrixMode(GL_MODELVIEW);

glEnable(GL_DEPTH_TEST);

lighting();

void animate(int n,int src,int dest)

int i;

if(speed<=0)speed=1;

for(i=pos[top[src]+1];i<90;i+=speed)

glPushMatrix();
glTranslatef(peg[src],i,0);

glRotatef(85,1,0,0);

glColor3f(0.1*n,0.2*n,0);

glutSolidTorus(2.0, 4*n, 20, 20);

glPopMatrix();

glutSwapBuffers();

display();

if(peg[src]<peg[dest])

for(i=peg[src];i<=peg[dest];i+=speed)

glPushMatrix();

glTranslatef(i,90,0);

glRotatef(85,1,0,0);

glColor3f(0.1*n,0.2*n,0);

glutSolidTorus(2.0, 4*n, 20, 20);

glPopMatrix();

glutSwapBuffers();

display();

else

for(i=peg[src];i>=peg[dest];i-=speed)

glPushMatrix();

glTranslatef(i,90,0);
glRotatef(85,1,0,0);

glColor3f(0.1*n,0.2*n,0);

glutSolidTorus(2.0, 4*n, 20, 20);

glPopMatrix();

glutSwapBuffers();

display();

for(i=70;i>pos[top[dest]+1];i-=speed)

glPushMatrix();

glTranslatef(peg[dest],i,0);

glRotatef(85,1,0,0);

glColor3f(0.1*n,0.2*n,0);

glutSolidTorus(2.0, 4*n, 20, 20);

glPopMatrix();

glutSwapBuffers();

display();

void mouse(int btn,int mode,int x,int y)

if(btn == 4 && mode == GLUT_DOWN)

if(counter<max_moves)
{

pop(moves[counter][1]);

if(animationFlag)

animate(moves[counter][0],moves[counter][1],moves[counter][2]);

push(moves[counter][2],moves[counter][0]);

counter++;

if(btn == 3 && mode == GLUT_DOWN)

if(counter>0)

counter--;

pop(moves[counter][2]);

if(animationFlag)

animate(moves[counter][0],moves[counter][2],moves[counter][1]);

push(moves[counter][1],moves[counter][0]);

glutPostRedisplay();

void restart()

int i;
memset(POLES,0,sizeof(POLES));

memset(moves,0,sizeof(POLES));

memset(top,-1,sizeof(top));

cnt=0,counter=0;

ycoordinate=0.1;

max_moves = pow(2,NUM_DISKS)-1;

for(i=NUM_DISKS;i>0;i--)

push(0,i);

tower(NUM_DISKS,0,1,2);

void processMenuLighting(int option)

switch(option)

case LIGHT_OFF:

glDisable(GL_LIGHTING);

lightflag=0;

break;

case LIGHT_ON:

glEnable(GL_LIGHTING);

lightflag=1;

break;
}

glutPostRedisplay();

void processMenuMain2(int option)

void processMenuCamera(int option)

switch(option)

case 0:ycoordinate+=0.1;break;

case 1:ycoordinate-=0.1;break;

glutPostRedisplay();

void processMenuRestart(int option)

if(option==0)

restart();

glutPostRedisplay();

}
void processMenuExit(int option)

if(option==0)exit(0);

void processMenuAnimate(int option)

switch(option)

case 0:

animationFlag=1;

break;

case 1:

animationFlag=0;

void processMenuSolveCompletely(int option)

int temp=animationFlag;

animationFlag=0;

int i,j;

while(counter<max_moves)

mouse(4,GLUT_DOWN,0,0);

display();

for(i=0;i<100000;i++)
for(j=0;j<100;j++);

animationFlag=temp;

void processMenuBgColor(int option)

switch(option)

case 0:glClearColor(0,0,0,0);randomColorFlag=0;break;

case 1:glClearColor(1,1,1,0);randomColorFlag=0;break;

case 2:glClearColor(1,0,0,0);randomColorFlag=0;break;

case 3:glClearColor(0,1,0,0);randomColorFlag=0;break;

case 4:glClearColor(0,0,1,0);randomColorFlag=0;break;

case 5:randomColorFlag=1;break;

glutPostRedisplay();

void createGLUTMenus2()

int menu = glutCreateMenu(processMenuLighting);

glutAddMenuEntry("On",LIGHT_ON);

glutAddMenuEntry("Off",LIGHT_OFF);

int menuExit = glutCreateMenu(processMenuExit);

glutAddMenuEntry("Yes",0);
glutAddMenuEntry("No",1);

int menuCamera = glutCreateMenu(processMenuCamera);

glutAddMenuEntry("+0.1",0);

glutAddMenuEntry("-0.1",1);

int menuRestart = glutCreateMenu(processMenuRestart);

glutAddMenuEntry("Yes",0);

glutAddMenuEntry("No",1);

int menuAnimate = glutCreateMenu(processMenuAnimate);

glutAddMenuEntry("On",0);

glutAddMenuEntry("Off",1);

int menuBgColor = glutCreateMenu(processMenuBgColor);

glutAddMenuEntry("Black",0);

glutAddMenuEntry("White",1);

glutAddMenuEntry("Red",2);

glutAddMenuEntry("Green",3);

glutAddMenuEntry("Blue",4);

glutAddMenuEntry("Random",5);

int menuSolveCompletely = glutCreateMenu(processMenuSolveCompletely);

glutAddMenuEntry("Start",0);

glutCreateMenu(processMenuMain2);

glutAddSubMenu("Lighting",menu);

glutAddSubMenu("Move Camera",menuCamera);

glutAddSubMenu("Animation",menuAnimate);

glutAddSubMenu("Background Color",menuBgColor);

glutAddSubMenu("Solve Completely",menuSolveCompletely);
glutAddSubMenu("Restart",menuRestart);

glutAddSubMenu("Exit",menuExit);

glutAttachMenu(GLUT_RIGHT_BUTTON);

void processMenuMain1(int option)

void processMenuNumDisks(int option)

NUM_DISKS=option;

restart();

glutPostRedisplay();

void createGLUTMenus1()

int menu = glutCreateMenu(processMenuNumDisks);

glutAddMenuEntry("3",3);

glutAddMenuEntry("4",4);

glutAddMenuEntry("5",5);

glutAddMenuEntry("6",6);

glutAddMenuEntry("7",7);

glutAddMenuEntry("8",8);
glutAddMenuEntry("9",9);

glutAddMenuEntry("10",10);

int menuExit = glutCreateMenu(processMenuExit);

glutAddMenuEntry("Yes",0);

glutAddMenuEntry("No",1);

glutCreateMenu(processMenuMain1);

glutAddSubMenu("Number of Disks",menu);

glutAddSubMenu("Exit",menuExit);

glutAttachMenu(GLUT_RIGHT_BUTTON);

void strokeString(float x,float y,float sx,float sy,char *string,int width)

char *c;

glLineWidth(width);

glPushMatrix();

glTranslatef(x,y,0);

glScalef(sx,sy,0);

for(c=string; *c != '\0'; c++)

glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);

glPopMatrix();

}
void initfirst()

glClearColor(0,0,0,0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0,1000,0,1000,-1,1);

glMatrixMode(GL_MODELVIEW);

void first()

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1,0,0);

strokeString(50,850,0.15,0.15,"ATME COLLEGE OF ENGINEERING",2);

strokeString(100,750,0.3,0.3,"DEPARTMENT OF COMPUTER SCIENCE",4);

strokeString(300,670,0.3,0.3,"AND ENGINEERING",4);

strokeString(200,500,0.3,0.3,"AN OPENGL MINI PROJECT ON",2);

glColor3f(0,1,1);

strokeString(250,420,0.4,0.4,"TOWER OF HANOI",6);

strokeString(150,220,0.2,0.2,"NUMBER OF DISKS:",3);

glColor3f(1,1,0);

char str[5];

sprintf(str, "%d", NUM_DISKS);

strokeString(450,220,0.2,0.2,str,3);

glColor3f(1,0,0);
strokeString(50,100,0.17,0.17,"1 . Set the number of disks using the menu",2);

strokeString(50,50,0.17,0.17,"2 . Press (Enter) to start the simulation",2);

strokeString(650,200,0.15,0.15,"By:",2);

glColor3f(1,1,0);

strokeString(650,160,0.18,0.18,"Kiran Hadapad - 4AD19CS410",2);

strokeString(650,30,0.2,0.2,"Mrs.Keerthana M M(Asst Professor)",2);

glColor3f(1,0,0);

strokeString(650,120,0.18,0.18,"Kishore P - 4AD19CS411",2);

strokeString(650,60,0.15,0.15,"Under the guidance of.",2);

glutSwapBuffers();

void keyboard2(unsigned char c, int x, int y){}

void keyboard(unsigned char c, int x, int y)

switch(c)

case 13:

restart();

init();

glutDisplayFunc(display);

createGLUTMenus2();

glutKeyboardFunc(keyboard2);

glutMouseFunc(mouse);

break;
}

glutPostRedisplay();

int main(int argc,char** argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);

glutInitWindowSize(1024,720);

glutInitWindowPosition(100,100);

glutCreateWindow("Tower of Hanoi");

initfirst();

glutDisplayFunc(first);

createGLUTMenus1();

glutKeyboardFunc(keyboard);

glutMainLoop();

return 0;

}
Chapter 5

SNAPSHOT
A snapshot is the state of the system at a particular point in time. It can refer to the actual
copy of a state of a system or to a capability provide by systems.

Fig 1: Home Page


Fig 1.1: Disks (Click on Start to Solve).

Fig 1.2: Moving Disks.


Fig 1.3: Solving Criteria.

Fig1.4: Final result.


Conclusion

It was a wonderful learning experience for me working on this project. This


project took me through various phases of project development and gave me real insight into the
world of software engineering. The joy of working and the thrill involved while tackling the
various problems and challenges gave me a feel of developers industry.

It was due to project that I came to know how professional software’s are designed.

I enjoyed each and every bit of work I had to put into this project.
Reference

 Interactive Computer Graphics – Edward Angel


 Official OPENGL Documentation at

https://www.opengl.org/documentation/

 OpenGL Programming Guide: The Official Guide to Learning OpenGLDave Shreiner.

You might also like