Visvesvaraya Technological University: Computer Graphics Laboratory With Mini Project 18CSL67
Visvesvaraya Technological University: Computer Graphics Laboratory With Mini Project 18CSL67
Visvesvaraya Technological University: Computer Graphics Laboratory With Mini Project 18CSL67
BACHELOR OF ENGINEERING
IN
COMPUTER SCIENCE AND ENGINEERING
For the academic year
2020-2021
Submitted by:
CERTIFICATE
Certified that the mini project entitled “HELICOPTER GAME” carried out by DIWAKAR KARNA
(1KN18CS030) & MUKUNDA PRASAD UPADHYAYA (1KN18CS067) , bonafide students of KNS
Institute of Technology, Bengaluru, in partial fulfillment for the award of Bachelor of Engineering in
Computer Science & Engineering of the Visvesvaraya Technological University, Belagavi
during the year 2021– 2022.
EXTERNAL VIVA
2
ACKNOWLEDGEMENT
The satisfaction and euphoria that accompanies the successful completion of any task would be
incomplete without mentioning the people who made it possible. With deep gratitude, I
acknowledge all those guidance and encouragement, which served as bacon of light and crowned
our efforts with success. I thank each one of them for their valuable support.
I express my sincere thanks to Dr. S.M Prakash, Principal, KNS Institute of Technology,
Bangalore, for providing necessary facilities and motivation to carry out internship work
successfully.
I express heartfelt gratitude and humble thanks to Prof Mohammed Shakir, HOD, Dept. of CSE,
KNS Institute of Technology, for the constant encouragement, inspiration and help to carry out
internship work successfully.
I would like to express my sincere gratitude towards my internal guide Mrs. Ammu Bhuvana D,
Asst. Prof, Dept of CSE for providing encouragement and inspiration throughout the Internship.
I am thankful to all the teaching and non-teaching staff members of Computer Science & Engineering
Department for their help and needed support rendered throughout the Internship.
ABSTRACT
A 2D graphics based game helicopter is a great start for a student who starts learning
computer graphics & visualization. The development of the game has large scope to
learn computer graphics from scratch. We used OpenGL utility toolkit to implement
the algorithm, written it in C++ language.
There is still scope left in the development of project like, after “game
over” a list should show top ten scorers, a need to embed a button “play again”.
Welcome screen need more modification there is scope of embedding buttons like
“about”, “how to play”, “configuration”, “profiles”, etc. In future we hope we would
implement it in source code for better experience of playing this game.
Finally, we could say by developing the game we have learnt the basics of
computer graphics and in future by developing it further we shall learn more. It will
be our pleasure if we could develop in 3d graphics package.
i
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
CONTENTS
INDEX PAGE NO
INTRODUCTION ....................................................................................................5
1.32DHelicopter ............................................................................................9
iii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
SNAPSHOT.............................................................................................................24
3. Game over...............................................................................................25
CONCLUSION .......................................................................................................26
BIBLIOGRAPHY...................................................................................................27
iv
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Chapter 1
INTRODUCTION
There is a lot of development in hardware and software required to generate images, and
nowadays the Cost of hardware and software is dropping rapidly. Due to this, interactive
computer graphics is becoming available to more and more people.
Computer graphics started with the display of data on hardcopy plotters and cathode ray tube
(CRT) screens soon after the introduction of computers themselves. It has grown to include the
creation, storage and manipulation of models and manipulation of models and images of
objects. These models come from a diverse and expanding set of fields, and include physical,
mathematical, engineering, architectural, and even conceptual structures, natural phenomena,
and so on.
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. The handling of such devices is included in the
study of computer graphics, because of the close relationship between the input devices and the
display.
Digital to
HOST Analogy
v
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
To reduce the burden of basic systems we use the following support applications.
1. Display processor
2. Pipeline architecture
3. Graphics pipeline
4. Vertex processing
5. Clipping and primitive assembly
6. Rasterization
7. Fragment processing
Display
HOST Processor
Display List
1. Display of information
Classical graphics techniques are used as a medium to convey information among people. In
ancient times Greeks were able to convey their architectural ideas graphically even though the
relevant mathematics was not developed.
Today the same type of information is generated by architectures mechanical designers and
drafts people using computer based drafting system.
2. Design
Professionals such as engineering and architecture are concerned with design. Starting with a
set of specifications, engineers and architects seek cost effective and esthetic solution that
satisfies the specifications. Design is an iterative process.
Once graphics system evolved to be capable of generating sophisticated images in real time,
engineers and researchers began to use them as simulators. Graphical flight simulator as
proved to increase safety and reduce training expenses, Use of graphics for animation in
television, motion pictures and advertising industry.
vi
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
4. User interfaces
Our interaction with computers has become dominated by visual paradigm that includes
windows, icons, menus and a pointing device such as a mouse. More recently millions of
people have become internet users. Their access is through graphical network browsers such as
firefox and internet explorer.
vii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
1.2 OpenGL
OpenGL is a software interface to graphics hardware. This interface consists of about
150 distinct commands that you use to specify the objects and operations needed to
produce interactive three-dimensional applications.
With OpenGL, you must build up your desired model from a small set of geometric
primitives - points, lines, and polygons.
A sophisticated library that provides these features could certainly be built on top of
OpenGL.
The OpenGL Utility Library (GLU) provides many of the modeling features, such as
quadric surfaces and NURBS curves and surfaces. GLU is a standard part of every
OpenGL implementation. Also, there is a higher-level, object-oriented toolkit, Open
Inventor, which is built atop OpenGL, and is available separately for many
implementations of OpenGL.
OpenGL was developed by Silicon Graphics Inc. (SGI) in 1992 and is widely used in
CAD, virtual reality, scientific visualization, information visualization, and flight
simulation. It is also used in video games, where it competes with Direct3D on
Microsoft Windows platforms. OpenGL is managed by the non-profit technology
consortium, the Khronos Group.
viii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
1.3 2DHelicopter
This mini project under Computer Graphics & Visualization Laboratory is an implementation
of a kind popular helicopter game using the OpenGL Graphics Library and GLUT Toolkit.
Scope
The player input his/her name before starting the game & can control it either using keyboard
or mouse at a time and at the end of game it will show how much distance you covered as
his/her score.
Objective
The objective of the game is to fly a helicopter in space with restricted upward and downward
motion using either mouse or keyboard, meanwhile walls will move towards player’s copter
and player have to avoid a collision between them. The game will enter into next level as soon
as player crosses next 50 unit distance and speed of wall will increase by a certain amount each
time player enter next level. The game will be over if a collision occurring there.
Mouse function: -
On right click down, copter moves upward,
On releasing it moves downward.
Keyboard function: -
Pressing UP key move copter up and down if press DOWN key.
ix
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Chapter 2
2.1 Commands
void glBegin(GLenum mode)
Marks the beginning of a vertex-data list that describes a geometric primitive. The
type of primitive is indicated by mode, which can be any of the values shown in
void glEnd(void);
2.2 Viewing
2.2.1 The viewing transformation
void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,
GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz);
Defines a viewing matrix and multiplies it to the right of the current matrix. The
desired viewpoint is specified by eyex, eyey, and eyez. The centerx, centery, and
centerz arguments specify any point along the desired line of sight, but typically
they're some point in the center of the scene being looked at. The upx, upy, and upz
arguments indicate which direction is up (that is, the direction from the bottom to the
top of the viewing volume).
Translate
x
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Multiplies the current matrix by a matrix that moves (translates) an object by the
given x, y, and z values (or moves the local coordinate system by the same amounts).
Rotate
Multiplies the current matrix by a matrix that rotates an object (or the local coordinate
system) 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.
Scale
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. With the local coordinate system approach,
the local coordinate axes are stretched, shrunk, or reflected by the x, y, and z factors,
and the associated object is transformed with them.
Creates a matrix for a perspective-view frustum and multiplies the current matrix by
it. The frustum's viewing volume is defined by the parameters: (left, bottom, -near)
and (right, top, -near) specify the (x, y, z) coordinates of the lower-left and upper-right
corners of the near clipping plane; near and far give the distances from the viewpoint
to the near and far clipping planes. They should always be positive.
Creates a matrix for a symmetric perspective-view frustum and multiplies the current
matrix by it. fovy is the angle of the field of view in the x-z plane; its value must be in
the range [0.0,180.0]. aspect is the aspect ratio of the frustum, its width divided by its
height. near and far values the distances between the viewpoint and the clipping
planes, along the negative z-axis. They should always be positive.
Orthographic projection
xi
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Creates a matrix for an orthographic parallel viewing volume and multiplies the
current matrix by it. (left, bottom, -near) and (right, top, -near) are points on the near
clipping plane that are mapped to the lower-left and upper-right corners of the
viewport window, respectively. (left, bottom, -far) and (right, top, -far) are points on
the far clipping plane that are mapped to the same respective corners of the viewport.
Both near and far can be positive or negative.
Defines a pixel rectangle in the window into which the final image is mapped. The (x,
y) parameter specifies the lower-left corner of the viewport, and width and height are
the size of the viewport rectangle. By default, the initial viewport values are (0, 0,
winWidth, winHeight), where winWidth and winHeight are the size of the window.
Pushes all matrices in the current stack down one level. The current stack is
determined by glMatrixMode(). The topmost matrix is copied, so its contents are
duplicated in both the top and second-from-the-top matrix. If too many matrices are
pushed, an error is generated.
void glPopMatrix(void);
Pops the top matrix off the stack, destroying the contents of the popped matrix. What
was the second-from-the-top matrix becomes the top matrix. The current stack is
determined by glMatrixMode(). If the stack contains a single matrix, calling
glPopMatrix() generates an error.
A depth buffer works by associating a depth, or distance, from the view plane (usually
the near clipping plane), with each pixel on the window. Initially, the depth values for
all pixels are set to the largest possible distance (usually the far clipping plane) using
the glClear() command with GL_DEPTH_BUFFER_BIT. Then the objects in the
scene are drawn in any order.
To use depth buffering, you need to enable depth buffering. This has to be done only
once. Before drawing, each time you draw the scene, you need to clear the depth
buffer and then draw the objects in the scene in any order.
xii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
ambient illumination is light that's been scattered so much by the environment that its
direction is impossible to determine - it seems to come from all directions.
The diffuse component is the light that comes from one direction, so it's brighter if it
comes squarely down on a surface than if it barely glances off the surface. Once it hits
a surface, however, it's scattered equally in all directions, so it appears equally bright,
no matter where the eye is located.
Specular light comes from a particular direction, and it tends to bounce off the
surface in a preferred direction. A well-collimated laser beam bouncing off a high-
quality mirror produces almost 100 percent specular reflection. Shiny metal or plastic
has a high specular component, and chalk or carpet has almost none. You can think of
specularity as shininess.
examples: -
xiii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Enabling lighting
"Antialiasing" explains this relatively subtle technique that alters colors so that the
edges of points,
lines, and polygons appear smooth rather than angular and jagged.
"Fog" describes how to create the illusion of depth by computing the color values of
an object
based on its distance from the viewpoint. Thus, objects that are far away appear to
fade into the background, just as they do in real life.
Examples: -
glRasterPos2i(20, 20);
xiv
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Draws the bitmap specified by bitmap, which is a pointer to the bitmap image. The
origin of the bitmap is placed at the current raster position. If the current raster
position is invalid, nothing is drawn, and the raster position remains invalid. The
width and height arguments indicate the width and height, in pixels, of the bitmap.
The width need not be a multiple of 8, although the data is stored in unsigned
characters of 8 bits each.
xv
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Chapter 3
This section describes a subset of Mark Kilgard's OpenGL Utility Toolkit (GLUT), which
is fully documented in his book, OpenGL Programming for the X Window System
(Reading, MA: Addison-Wesley Developers Press, 1996). GLUT has become a popular
library for OpenGL programmers, because it standardizes and simplifies window and
event management. GLUT has been ported atop a variety of OpenGL implementations,
including both the X Window System and Microsoft Windows NT.
xvi
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
xvii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Specifies the function, func, that's called when a mouse button is pressed or released. The
button callback parameter is one of GLUT_LEFT_BUTTON,
GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON. The state callback parameter
is either GLUT_UP or GLUT_DOWN, depending upon whether the mouse has been
released or pressed. The x and y callback parameters indicate the location (in window-
relative coordinates) of the mouse when the event occurred.
void glutPostRedisplay(void);
Marks the current window as needing to be redrawn. At the next opportunity, the callback
function registered by glutDisplayFunc() will be called.
xviii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
void glutWireOctahedron(void);
void glutSolidOctahedron(void);
void glutWireTetrahedron(void);
void glutSolidTetrahedron(void);
void glutWireDodecahedron(GLdouble radius);
void glutSolidDodecahedron(GLdouble radius);
void glutWireCone(GLdouble radius, GLdouble height, GLint slices, GLint stacks);
void glutSolidCone(GLdouble radius, GLdouble height, GLint slices, GLint stacks);
void glutWireTeapot(GLdouble size);
void glutSolidTeapot(GLdouble size);
void glutMainLoop(void);
Enters the GLUT processing loop, never to return. Registered callback functions will be
called when the corresponding events instigate them.
xix
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Chapter 4
SYSTEM REQUIREMENTS
4.1 Software Requirements
● Language: C++
● Tool: Eclipse
● Processor
Intel Processor 100 MHz / Pentium Processor 100 MHz /AMD
Processor 100 MHz
● RAM
256 MB or more
xx
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Chapter 5
5.1 Analysis
The objective of the game is to fly a helicopter in space with restricted upward and
downward motion using either mouse or keyboard, meanwhile walls will move
towards player’s copter and player have to avoid a collision between them. The game
will enter into next level as soon as player crosses next certain fixed unit distance and
speed of wall will increase by a certain fixed amount each time player enter next
level.
4. A game over window which will show score and level of player and also a button
“PLAY AGAIN” clicking on which game will start again.
5. If player score manage among top ten scorer, after game over it should prompt for
entering his name so it can store and display player’s name among top ten scorer with
player’s name in the list.
xxi
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
5.2 Design
Algorithm of display_function()
// start
{
// Checking collision condition
if ( ( top & bottom check ) ORed ( propeller front check ) ORed ( lower
body
check ) ORed ( lower body check ) )
else
{
// increase in level by 1 and block_speed by certain amount
if( (score multiple of 50) ANDed level_flag is set to 1)
{
reset level_flag to zero
increase level by 1
increase block_speed by a fixed small amount
}
xxii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
// drawining bloack
glPopMatrix();
glutSwapBuffers();
glFluash();
}
// end of external if…else statement
}
// end of display_function
xxiii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Chapter 6
SNAPSHOT
xxiv
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
3. Game over
You scored : 50
xxv
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
Chapter 7
CONCLUSION
xxvi
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
BIBLIOGRAPHY
1. Computer Graphics – Principals And Practice (Foley, Van Dam, Fenier and
Hughes) helped me to understand graphics generation algorithms, user interface and
dialogue design
xxvii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
char name[25];
char scs[20],slevel[20];
//to store score_string using itoa() and level as well
void init(void)
{
srand(time(0));
b1y=(rand()%45)+10;//b/w 10 to 44
void drawcopter()
{
glColor3f(0.7,1.0,1.0);
glRectf(10,49.8,19.8,44.8);//body
glRectf(2,46,10,48);//tail
glRectf(2,46,4,51);//tail up
glRectf(14,49.8,15.8,52.2);//propeller stand
glRectf(7,53.6,22.8,52.2);//propeller*/
}
char *c;
glRasterPos3f(x, y,z);
xxviii
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//GameOver Checking
if(
(i==730||i==-700)
//top and bottom checking
||
( ((int)b1x==10||(int)b1x==7||(int)b1x==4||(int)b1x==1)
&&(int)b1y<53+(int)hm&&(int)b1y+35>53+(int)hm)
// propeller front checking
||
( ((int)b1x==9||(int)b1x==3||(int)b1x==6)
&&(int)b1y<45+(int)hm&&(int)b1y+35>45+(int)hm)
//lower body checking
||
( ((int)b1x==0) && (int)b1y<46+(int)hm&&(int)b1y+35>46+(int)hm))
// lower tail checking
glColor3f(0.0,0.0,1.0);
glRectf(0.0,0.0,100.0,100.0);
glColor3f(1.0,0.0,0.0);
renderBitmapString(40,70,0,GLUT_BITMAP_HELVETICA_18,"GAME OVER!!!");
glColor3f(1.0,1.0,1.0);
renderBitmapString(25,58,0,GLUT_BITMAP_TIMES_ROMAN_24,"You");
renderBitmapString(45,58,0,GLUT_BITMAP_TIMES_ROMAN_24,"scored:");
renderBitmapString(70,58,0,GLUT_BITMAP_TIMES_ROMAN_24,scs);
glutSwapBuffers();
glFlush();
printf("\nGAME OVER\n\n");
printf("%s\You scored %s" ,name,scs);
printf("\n\nClose the console window to exit...\n");
//getch();
exit(0);
}
wflag=0;
glColor3f(0.0,0.5,0.7);
glRectf(0.0,0.0,100.0,10.0);//ceil
glRectf(0.0,100.0,100.0,90.0);//floor
glColor3f(1.0,1.0,1.0);
renderBitmapString(35,85,0,GLUT_BITMAP_HELVETICA_18,"CITY ENGINEERING
COLLEGE");
renderBitmapString(41,80,0,GLUT_BITMAP_HELVETICA_12,"Bangalore,
Karnataka-560 062");
glColor3f(1.0,1.0,0.0);
xxix
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
glColor3f(1.0,0.0,0.0);
renderBitmapString(40,45,0,GLUT_BITMAP_TIMES_ROMAN_24,"Welcome");
renderBitmapString(53,45,0,GLUT_BITMAP_TIMES_ROMAN_24,name);
renderBitmapString(43,30,0,GLUT_BITMAP_TIMES_ROMAN_24,"Click To
Start");
renderBitmapString(17,24,0,GLUT_BITMAP_9_BY_15,"CLICK AND HOLD LEFT
MOUSE BUTTON TO GO UP RELEASE TO GO DOWN");
glColor3f(0.0,0.0,0.0);
drawcopter();
glutSwapBuffers();
glFlush();
else
{
glPushMatrix();
glColor3f(0.0,0.5,0.7);
glRectf(0.0,0.0,100.0,10.0); //ceil
glRectf(0.0,100.0,100.0,90.0); //floor
glColor3f(0.0,0.0,0.0); //score
renderBitmapString(1,3,0,GLUT_BITMAP_TIMES_ROMAN_24,"Distance:");
//glColor3f(0.7,0.7,0.7);
sprintf(slevel,"%d",level); //level
renderBitmapString(80,3,0,GLUT_BITMAP_TIMES_ROMAN_24,"Level:");
renderBitmapString(93,3,0,GLUT_BITMAP_TIMES_ROMAN_24,slevel);
renderBitmapString(20,3,0,GLUT_BITMAP_TIMES_ROMAN_24,scs);
glTranslatef(0.0,hm,0.0);
// hm(=dy) changes occur by mouse func
xxx
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
drawcopter();
//code for helicopter
//if wall move towards left & get out of projection volume
if(b1x<-10)
{
else
b1x-=bspd;
//within the projection volume dec its x value by block_speed
glTranslatef(b1x,-hm,0.0);
glColor3f(1.0,0.0,0.0);
glRectf(b1x,b1y,b1x+5,b1y+35);//block 1
glPopMatrix();
glutSwapBuffers();
glFlush();
void moveHeliU(void)
{
hm+=0.05;
i++;
glutPostRedisplay();
void moveHeliD()
{
hm-=0.05;
i--;
glutPostRedisplay();
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
xxxi
Dept. OF CSE, KNSIT 2020-21
Computer Garphics LAB
glutIdleFunc(moveHeliU);
default: break;
if(key=='w') glutIdleFunc(moveHeliU);
if(key=='m') glutIdleFunc(moveHeliD);
helicopter
xxxii
Dept. OF CSE, KNSIT 2020-21