Dept. of CS&E, CIT, Ponnampet
Dept. of CS&E, CIT, Ponnampet
Dept. of CS&E, CIT, Ponnampet
CHAPTER 1
INTRODUCTION
Computer graphics are graphics created using computers and more generally, the
representation and manipulation of image data by a computer.
The development of computer graphics has made computers easier to interact with,
and better for understanding and interpreting many types of data. Developments in computer
graphics have a profound impact on many types of media and have revolutionized animation,
movies and the video game industry.
that provide facility for designing object motion. Ex: cartoons decision is an example of
computer art which uses CG.
1.5 Objectives
To implement the concepts of Computer Graphics we have learnt.
CHAPTER 2
INTRODUCTION TO OPENGL
As a software interface for graphics hardware, OpenGL's main purpose is to render
two- and three-dimensional objects into a frame buffer. These objects are described as
sequences vertices (which define geometric objects) or pixels (which define images).
OpenGL performs several processing steps on this data to convert it to pixels to form the final
desired image in the frame buffer.
Rasterization produces a series of frame buffer addresses and associated values using a
is fed into the last stage, per-fragment operations, which perform the final operations on the
data before it's stored as pixels in the frame buffer. These operations include conditional
updates to the frame buffer based on incoming and previously stored z-values (for z-
buffering) and blending of incoming pixel colors with stored colors, as well as masking and
other logical operations on pixel values.
Input data can be in the form of pixels rather than vertices. Such data, which might
describe an image for use in texture mapping, skips the first stage of processing described
above and instead is processed as pixels, in the pixel operations stage. The result of this stage
is either stored as texture memory, for use in the rasterization stage, or rasterized and the
resulting fragments merged into the frame buffer just as if they were generated from
geometric data.
All elements of OpenGL state, including the contents of the texture memory and even
of the frame buffer, can be obtained by an OpenGL application.
glutSwapBuffers (void):
Swap the front and back buffers (used in double buffering).
Clears one or more of the existing buffers. The mask is a logical or ("|") of any of the
following: GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT.
glFlush (void):
OpenGL normally saves or "buffers" drawing commands for greater efficiency. This forces
the image to be redrawn, in case the next update will be far in the future.
glVertex*(...):
Specify the coordinates of a vertex.
glNormal*(...)
Specify the surface normal for subsequent vertices. The normal should be of unit length after
the modelview transformation is applied. Call glEnable (GL_NORMALIZE) to have
OpenGL do normalization automatically.
glColor*(...):
Specify the color of subsequent vertices. This is normally used if lighting
is not enabled. Otherwise, colors are defined by glMaterial*(), defined below under lighting.
glLineWidth (GLfloat width):
Sets the line width for subsequent line drawing.
character is drawn, the raster position is advanced. So to draw a string, initialize the raster
position (using glRasterPos*()) and then call this on each character of the string.
CHAPTER 3
PROJECT DESCRIPTION
Some of the basic requirements for the development of this project are as follows:
System functions
1. Void glBegin(glEnum mode);
Initiates a new primitive of type mode and starts the collection of vertices.Values
include GL_POINTS and GL_LINE.
2. Void glEnd();
Terminates a list of vertices.
3. void glColor3f [ i f d ] (TYPE r, TYPE g, TYPE b) ;
Sets the present RGB colors.Valid types are int , float and double.
4. void glClearColor (GLclampf r, GLclampf g, GLclampf b, GLclampf a);
Sets the present RGBA clear color used when clearing color buffer.
5. intglutCreateWindow (char *title) ;
Creates a window on the display. The string title can be used to label the window. The
return value provides a reference to the window that can be used where there are
multiple windows.
6. Flush ( ) ;
Forces and buffers any OpenGL commands to execute.
3.2User-Defined Functions
1.voidinit();
This function is used to set the initial co-ordinates of the objects on the screen.
3. void idle();
This function sets the global idle call back to be functioned so a GLUT
program can perform background processing tasks or continuous animation when window
system events are not being received.
This occurs when one of the mouse button is either pressed or released.The state of the
button after the event(up or down),and the position of the cursor tracking the mouse in
window coordinates.In this program when the right button is pressed it displays the option.
3.3 Implementation
Source code:
#include<GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
char cost[10][10];
int nodes,co,count,i,j
int ak1,ak2
int pa,for41,for42,for43,for51,for52,for53,for54,for55,for56;
char akey1,akey2;
void *currentfont;
GLfloat thickness=3.0;
void drawstring(float x,float y,float z,char *string)
{
char *c;
glRasterPos3f(x,y,z);
for(c=string;*c!='\0';c++)
{
glutBitmapCharacter(currentfont,*c);
}
}
void myInit()
{
glClearColor(0.0,0.0,0.0,0.0);
gluOrtho2D(0.0,500.0,0.0,500.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
delay();
delay();
draw();
text();
}
void draw(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1.0f,0.0f,0.0f);
glVertex2i(345,260);
glVertex2i(135,260);
glVertex2i(135,230);
glVertex2i(345,230);
glEnd();
glFlush();
}
void text(void)
{
setFont(GLUT_BITMAP_TIMES_ROMAN_24);
glColor3f(0.0,1.0,1.0);
drawstring(120.0,455.0,1.0,"*THE BELLAMAN-FORD ALGORITHM ");
setFont(GLUT_BITMAP_HELVETICA_18);
glColor3f(0.0,1.0,0.0);
drawstring(145.0,240.0,1.0,"ENTER NODES B/W 2 TO 5");
glFlush();
}
void calc2(int ae1,int ae2)
{
float i,temp=0.3,temp1=0.3;
int j=0;
if(ae1==1 && ae2==2){
glColor3f(1.0,1.0,1.0);
setFont(GLUT_BITMAP_HELVETICA_18);
while(j<10)
{
for(i=0;i<156;i=i+temp)
{
glColor3f(0,0,1);
}
j++;
}
}
else if(ae1==2 && ae2==1){
glColor3f(1.0,1.0,1.0);
setFont(GLUT_BITMAP_HELVETICA_18);
drawstring(305.5,403.5,1.0,"2----->source");
glFlush();
glColor3f(1.0,1.0,1.0);
setFont(GLUT_BITMAP_HELVETICA_18);
drawstring(305.5,385.5,1.0,"1----->destination");
glFlush();
j++;
}
}
void mykeyboard (unsigned char key,int x,int y)
{
char a1[2] = {' ',' '};
char akey1;
char akey2;
char b1[2]= {' ',' '};
char b2[2]= {' ',' '};
char b3[2]= {' ',' '};
int i,j;
b1[0]=0;
b2[0]=0;
b3[0]=0;
b1[2]='\0';
b2[2]='\0';
b3[2]='\0';
count=count+1;
if(count==1){
switch((char)key){
case '5':printf("the number of nodes is 5\n");
co=shape5();
naming5();
break;
default:glColor3f(1.0,1.0,1.0);
drawstring(10.0,10.0,1.0,"INVALID INPUT ENTER BETWEEN 2 AND 5");
glFlush();
delay();
printf("inavlid input\n");
exit(0);
}
displayText();
}
else if(count>1 && count<=(co*co+1))
{
if(co==2)
{
if (count==2)
{
cost[1][1]=' ';
cost[1][1]=(char)key;
if(cost[1][1]!='0'){
printf("\nDiagonal element should be 0 ie cost[1][1]\n");
exit(0);
}
}
else if(count==3){
cost[1][2]=(char)key;
if(cost[1][2]=='0' || cost[1][2]=='9'){
printf("\ncost[1][2] should be between 0 and 9\n");
exit(0);
}
}
else if(count==4){
cost[2][1]=(char)key;
if(cost[2][1]!=cost[1][2]){
printf("\ncost[2][1] should have the same value as
cost[1][2]\n");
exit(0);
}
}
else if(count==5){
cost[2][2]=(char)key;
if(cost[2][2]!='0'){
printf("\nDiagonal element should be 0 ie cost[2][2]\n");
exit(0);
}}
}
if(c5[0]>0 && c5[0]<'9'){
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2f(115,200.0);
glVertex2f(200.0,30.0);
glEnd();
glFlush();
glColor3f(0.0,1.0,0.0);
setFont(GLUT_BITMAP_HELVETICA_18);
drawstring(143.5,120.0,1.0,c5);
glFlush();
}
if(c6[0]>0 && c6[0]<'9'){
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2f(300.0,407.5);
glVertex2f(215.0,30.0);
glEnd();
glFlush();
glColor3f(0.0,1.0,0.0);
setFont(GLUT_BITMAP_HELVETICA_18);
drawstring(265.5,230.5,1.0,c6);
glFlush();
}
}
void delaypacket()
{
j=1000;
while(j!=0)
{
j--;
i=1000;
while(i!=0)
{
i--;
}
}
}
void naming2()
{
glColor3f(1.0,1.0,1.0);
drawstring(105.5,403.5,1.0,"1");
glColor3f(1.0,1.0,1.0);
drawstring(105.5,203.5,1.0,"2");
glFlush();
}
void naming3()
{
glColor3f(1.0,1.0,1.0);
drawstring(105.5,403.5,1.0,"1");
glColor3f(1.0,1.0,1.0);
drawstring(105.5,203.5,1.0,"2");
glColor3f(1.0,1.0,1.0);
drawstring(305.5,403.0,1.0,"3");
glFlush();
}
int shape2()
{
int t=2;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.3,0.5);
glLineWidth(thickness);
glBegin(GL_LINES);
{
// square for 1
glColor3f(1.0,0.0,0.0);
glVertex2f(100.0,415.0);
glVertex2f(115.0,415.0);
glVertex2f(115.0,415.0);
glVertex2f(115.0,400.0);
glVertex2f(115.0,400.0);
glVertex2f(100.0,400.0);
glVertex2f(100.0,400.0);
glVertex2f(100.0,415.0);
glVertex2f(100.0,415.0);//to form tower
glVertex2f(107.5,440.2);//to form tower
//square for 2
glColor3f(0.0,1.0,0.0);
glVertex2f(100.0,215.0);
glVertex2f(115.0,215.0);
glVertex2f(115.0,215.0);
glVertex2f(115.0,200.0);
glVertex2f(115.0,200.0);
glVertex2f(100.0,200.0);
glVertex2f(100.0,200.0);
glVertex2f(100.0,215.0);
glVertex2f(100.0,215.0);//to form tower
glVertex2f(107.5,240.0);//to form tower
}
glEnd();
glLineWidth(1.0);
glFlush();
return t;
}
int shape3()
{
int t=3;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.6,0.0,0.4);
glLineWidth(thickness);
glBegin(GL_LINES);
{
// square for 1
glVertex2f(100.0,415.0);
glVertex2f(115.0,415.0);
glVertex2f(115.0,415.0);
glVertex2f(115.0,400.0);
glVertex2f(115.0,400.0);
glVertex2f(100.0,400.0);
glVertex2f(100.0,400.0);
glVertex2f(100.0,415.0);
glVertex2f(100.0,415.0);//to form tower
glVertex2f(107.5,440.2);//to form tower
glColor3f(0.5,0.5,0.0);
//square for 2
glVertex2f(100.0,215.0);
glVertex2f(115.0,215.0);
glVertex2f(115.0,215.0);
glVertex2f(115.0,200.0);
glVertex2f(115.0,200.0);
glVertex2f(100.0,200.0);
glVertex2f(100.0,200.0);
glVertex2f(100.0,215.0);
glVertex2f(100.0,215.0);//to form tower
glVertex2f(107.5,240.0);//to form tower
glColor3f(0.0,0.5,0.5);
}
}
displayText()
{
glColor3f(1.0,1.0,1.0);
drawstring(3.0,60.0,1.0,"->Enter the cost adjacency matrix");
glFlush();
glColor3f(1.0,1.0,1.0);
drawstring(3.0,45.0,1.0,"->value you enter should be between (0<value<9)");
glFlush();
glColor3f(1.0,1.0,1.0);
drawstring(3.0,25.0,1.0,"->if entered 9 there wont be direct edge between ");
glFlush();
glColor3f(1.0,1.0,1.0);
drawstring(3.0,10.0,1.0,"node,in case if u enter no of nodes as 4,5,6");
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(1365,768);
glutCreateWindow("Bellaman-Ford Algorithm");
glClearColor(0.0,0.0,0.0,0.0);
myInit();
glutDisplayFunc(display);
glutKeyboardFunc(mykeyboard);
glutMainLoop();
}
CHAPTER 4
SNAPSHOTS
CHAPTER 5
CONCLUSION AND FUTURE SCOPE
Conclusion
Here we conclude that, we can produce any form of images, design and
animations using OpenGL. OpenGL provides smooth surface to draw the objects, it is also
possible to give realistic effects like texture and lighting to the object. We have used many
built-in functions that are user friendly. We have tried to use graphical concepts in OpenGL
such as translation, motion etc.
Future Scope
This shows the graphical representation of the sinking ship using OpenGL package.
It can also be implemented in 2D format giving it a virtual appeal.
In fractal image compression,you can change the dimensions of the image and calculate the
time of compression and decompression.
BIBLIOGRAPHY
TEXT BOOKS
[1] Edward Angel, Interactive Computer Graphics A Top-Down Approach Using
OpenGL, Pearson Education Asia, Fifth Edition, 2008
[2] Computer Graphics Using OpenGL – F.S Hill,Jr. Second Edition, Pearson Education
2001
[3] Computer Graphics- James D Foley, Andries Van Dam, Steven K Feiner, John F
Hughes, Addison-wesley 1997
[4] Computer Graphics – OpenGL Version – Donald Hearn and Pauline Baker, Second
Edition, Pearson Education, 2
Website
www.OpenGL.org