Вы находитесь на странице: 1из 44

ACKNOWLEDGEMENT

The satisfaction and euphoria that accompany the successful completion of any task would be incomplete without the mention of the people who made it possible, whose constant guidance and encouragement crowned my efforts with success. I would like to express my deep sense of gratitude to our institution MSEC, for providing me the means of attaining my most cherished goal. We express our sincere thanks to our principal, Dr.RANAPRATAP REDDY for his kind encouragement and support. I also extend my sincere thanks to Mr.VIJAYA KUMAR H.O.D. of the department of CS&E, who encouraged me throughout this case study. We feel delighted to have this page to express our sincere thanks and deep appreciation to our guide, Mr.ASHWIN KUMAR for valuable guidance, and keen interest and constant encouragement through out the entire period of this project work. We also convey my regards and deep sense of gratitude to all the lecturers of CS&E department, my parents and my friends for their constant motivation. Finally I consider my privilege to express my voice of gratitude and respect to all those who guided and inspired me in the completion of this study.

Chapter 1:

INTRODUCTION

About the Copter Game


Copter game is an OpenGL game for one player where you fly a helicopter through a maze of obstacles. This version is the most popular of helicopter game, being very easy to operate and required almost no skills at all. Copter game graphics are very simple, one might say primitive. It's not a fancy game, but rest assured that it is very enjoyable and people became addicted to this game playing it over and over for hours. Copter game is played using just two keys W and S on keyboard. You can only navigate your copter up and down; it is a 3 dimensional game. By default the copter will hit an obstacle n crash if you don't touch anything, so basically during the game you have the press and release the button the way that fits the runway. The goal of the game is to get the copter through the runway as far as you can go without colliding with obstacles. You cannot touch the upper wall or the ground or you'll be crashed, and there are also obstacles along the way that you must avoid. Copter game is tough, there are no lifes, no second changes, the second your helicopter touches anything it will crash and you will have to start the game all over, with zero score. Copter game, as simple as it may look, is really a challenge for all players of all ages around the world. Even young players, which are playing sophisticated games with amazing 3D graphics seems to enjoy playing the helicopter game.

Chapter 2:

LITERATURE SUMMARY

2.1 What is OpenGL?


OpenGL provides a set of commands to render a three dimensional scene. That means

you provide the data in an OpenGL-useable form and OpenGL will show this data on the screen (render it). It is developed by many companies and it is free to use. You can develop OpenGL-applications without licencing. OpenGL is a hardware- and system-independent interface. An OpenGL-application will work on every platform, as long as there is an installed implementation. Because it is system independent, there are no functions to create windows etc., but there are helper functions for each platform. A very useful thing is GLUT.

2.2 What is GLUT?


GLUT is a complete API written by Mark Kilgard which lets you create windows and handle the messages. It exists for several platforms, that means that a program which uses GLUT can be compiled on many platforms without (or at least with very few) changes in the code.

2.3 How does OpenGL work?


OpenGL bases on the state variables. There are many values, for example the color, that remain after being specified. That means, you can specify a color once and draw several polygons, lines or what ever with this color then. There are no classes like in DirectX. However, it is logically structured. Before we come to the commands themselves, here is another thing: To be hardware independent, OpenGL provides its own data types. They all begin with "GL". For example GLfloat, GLint and so on. There are also many symbolic constants, they all begin with "GL_", like GL_POINTS, GL_POLYGON. Finally the commands have the prefix "gl" like glVertex3f(). There is a utility library called GLU, here the prefixes are "GLU_" and "glu". GLUT commands begin with "glut", it is the same for every library. A very important thing is to know, that there are two important matrices, which affect the transformation from the 3d-world to the 2d-screen: The projection matrix and the modelview matrix. The projection matrix contains information, how a vertex let's say a

"point" in space shall be mapped to the screen. This contains, whether the projection shall be isometric or from a perspective, how wide the field of view is and so on. Into the other matrix you put information, how the objects are moved, where the viewer is and so on. 2.4 How can I use GLUT? GLUT provides some routines for the initialization and creating the window (or fullscreen mode, if you want to). Those functions are called first in a GLUT application: In your first line you always write glutInit(&argc, argv). After this, you must tell GLUT, which display mode you want. This is done by calling glutInitDisplayMode(). The symbolic constants are connected by a logical OR, so you could use glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE). In later tutorials we will use some more constants here. After the initialization you call glCreateWindow() with the window name as parameter. Then you can (and should) pass some methods for certain events. The most important ones are "reshape" and "display". Display should clear the so called color buffer let's say this is the sheet of paper and draw our objects. You pass the methods by glut*Func(), for example glutDisplayFunc(). At the end of the main function you call glutMainLoop(). This function doesn't return, but calls the several functions passed by glut*Func.

Chapter 3:

SYSTEM REQUIREMENTS

3.1 HARDWARE REQUIREMENTS


Minimum hardware specification

Processor : Pentium processors

Main memory : 512 MB RAM Hard Disk : 40 GB

3.2 SOFTWARE REQUIREMENTS


Minimum software specification

Operating system : Windows Visual C++ OPENGL Library

Chapter4:

FUNCTIONAL DESCRIPTION

Function Name

Application

Void initLight(); int LoadBitmap()

Hidden surface removal. Setup and enable light0 To load the bitmap texture. To calculate the score To display GAME OVER. To display all body parts of copter To display success and failure status in background. Keyboard interface. To move the copter up and down. To reset the score to 0. Display the popup menu.

void timer(); void display3(); void display1(); void display2(); Void keyboard(); Void reset(); Void levelMenu();

Chapter5:

SYSTEM DESIGN

5.1 OPERATING SYSTEM:


An important program that runs on computer is the OS softwares. Every generalpurpose computer must have an operating system to run other programs. Operating system performs basic tasks, such as recognizing output from the keyboard, sending output to the display screen, keeping track of files and directories on the disk, and

controlling peripheral devices such as disk drivers and printers. For large systems, the OS has an even greater responsibilities and powers. It is like a traffic cop- it makes sure that different program and user running at same time do not interfere with each other. The OS is also responsible for security, ensuring that unauthorized users do not access the system.

5.2 CLASSIFICATION OF OPERATING SYSTEM:


Operating system can be classified as follows: 1. Multi-user:- Allows two or more users to run programs at the same time. Some OS permit hundreds or even thousands of current users. 2. Multi processing: - Supports running a program on more than one CPU. 3. Multitasking: - Allows more than one program to run concurrently. 4. Multithreading: - Allows different parts of a single program to run concurrently. 5. Real time: - Respond to inputs instantly. General-Purpose OS, such as DOS and UNIX are not real time. Operating system provides a software platform on the top of which other program can run. The application program must be written to run on the top of a particular OS your choice of OS, therefore, determines to a great extend the application you can run. For PCs the most popular OS are DOS, OS/2 and windows, but others are available, such as LINUX. Os can be explored from two points of view; user and the system.

5.3 USER VIEW:


Some users sit at a terminal connected to a mainframe or a minicomputer. Other users are accessing the same computer though other terminals. These users share resources and may exchange information the OS is designed to maximize resources utilization to assure that all available CPU time, memory and I/O are used efficiently and that no individual user takes more than their fare share. Other users sit work station connected to networks

of other workstations and servers. These users dedicated resources of their disposal, but they also share resources such as networking and servers file, compute and Print servers. Therefore, their OS is designed to compromise between individual usability and resources utilization. Some computers have a little or no user views. For example, embedded computers in home devices and automobiles may have numeric key pad and many turn indicator lights on or off to show status, but mostly they are their OS and designed to run without user intervention. As user normally interact with the OS through a set of commands. For example, the commands such as COPY and RENAME for copying and changing the names of files, respectively. The commands are accepted and executed by a part of the OS called the command processor command line interpreter. Graphical user interfaces allow you to enter commands by pointing and clicking at objects that appear on the screen.

5.4SYSTEM VIEW:
From the computers point of view, the OS is the program that is most intimate with the hardware. We can view the OS as a resource allocator. A computer system has many resources-hardware and software that may be required to solve a problem CPU time, memory space, file storage space, I/O devices are and so on. The OS acts as a manager of these resources. Facing numerous and possibly conflicting resources, the OS must decide how to specify programs and users so that it can operate the computer system efficiently and fairly. An OS is a control program. A control program manages the execution of user programs to prevent errors and improper use of the computer. It especially concerns with the operation and control of input and output devices. In general, however we have no complete adequate definition of an OS. OS exist because it is a responsible way to solve the problem of creating a usable computer system. The fundamental goal of computer systems is to execute user programs and make solving user problems easier. Towards this goal, computer hardware is constructed. Since bare hardware alone is not particularly easy to use, application programs are developed.

These programs require certain common operations such as those controlling and allocating resources is then brought together into piece of software.

5.5 DESIGN:
We have no universally accepted definition of which is part of the OS. A simple viewpoint is that it includes everything a vendor ships when you order The Operating System. The storage (memory, disks and tapes) requirements and features included, however, vary greatly across systems. A more common definition is that OS the one program running at all times on the computer (usually called Kernel), with all else being application programs. The matter of what constitutes an OS is becoming important. In 1998 the United States department of Justice filed suit against Microsoft in essence claiming that Microsoft included too much functionality in its OS and thus prevented competition and application vendors. The primary goal of OS is convenience the users. This view is particularly clear when you look at OS for small PCs. The primary goal of other OS is efficient operation of computer system. This is the case for the large, shared, multi-user system. These systems are expensive, so it is desirable to make them. This view is particularly clear when you look at OS for small PCs.

Chapter6:

DATAFLOW DIAGRAM

Chapter7:
7.1 HEADER FILES
#include<stdlib.h> #include<stdio.h> #include<conio.h> #include<GL/glut.h> #include <windows.h> #include <string.h>

IMPLEMENTATION

#include <math.h> #include <GL/gl.h>

7.2 FUNCTIONS
7.2.1 glLightfv Function The glLightfv function returns light source parameter values. SYNTAX: void glLightfv(GLenum light, GLenum pname, GLfloat *params); PARAMETERS: 1. light: They are identified by symbolic names of the form GL_LIGHTi where i is a value: 0 to GL_MAX_LIGHTS - 1. 2. pname: A light source parameter for light. The following symbolic names are accepted: 1.GL_DIFFUSE 2.GL_SPECULAR 7.2.2 glPushMatrix, glPopMatrix Function The glPushMatrix and glPopMatrix functions push and pop the current matrix stack. SYNTAX: void glPushMatrix, glPopMatrix(void); PARAMETERS: This function has no parameters. 3. glPushMatrix(); 4. glPopMatrix(); 7.2.3 glEnable, glDisable Function The glEnable and glDisable functions enable or disable OpenGL capabilities. SYNTAX: void glEnable, glDisable(GLenum cap); PARAMETERS: 5. cap: A symbolic constant indicating an OpenGL capability. 6. glEnable(GL_CULL_FACE); 7. glDisable(GL_CULL_FACE); 7.2.4 glColor3f Function Sets the current color.

SYNTAX: void glColor3f(GLfloat red, GLfloat green, GLfloat blue); 7.2.5 glBegin, glEnd Function The glBegin and glEnd functions delimit the vertices of a primitive or a group of like primitives. SYNTAX: void glBegin, glEnd(GLenum mode); PARAMETERS: 8. mode: its possible values are: 7.2.6 glNormal3f Function Sets the current normal vector. SYNTAX: void glNormal3b(GLfloat12 nx, GLfloat ny, GLfloat nz); 9. glNormal3f(1.0,0.0,0.0); 7.2.7 glVertex3f Function Specifies a vertex. SYNTAX: void glVertex3f(GLfloat x, GLfloat y, GLfloat z); 10. glVertex3f(-3.0,3.0,4.0); 7.2.8 glRotatef Function The glRotated and glRotatef functions multiply the current matrix by a rotation matrix. SYNTAX: void glRotate( GLfloat angle, GLfloat x, GLfloat y, GLfloat z); 11. glRotatef(xrot,1.0,0.0,0.0); 7.2.9 glTranslate Function The glTranslated and glTranslatef functions multiply the current matrix by a translation matrix. SYNTAX: void glTranslate( x, y, z); 1.GL_LINES 2. GL_LINE_STRIP 3.GL_LINE_LOOP 4. GL_TRIANGLES 5. GL_QUADS

12. glTranslatef(0.0,0.0,-1.0); 7.2.10 glScalef Function The glScaled and glScalef functions multiply the current matrix by a general scaling matrix. SYNTAX: void glScalef(GLfloat x, GLfloat y, GLfloat z); glScalef(1.0,1.0,-1.0);

7.3 FUNCTIONS USED TO DISPLAY


7.3.1 glClear Function The glClear function clears buffers to preset values. SYNTAX: glClear(GLbitfield mask); PARAMETERS: 13. mask: the possible used for this are: 1. GL_COLOR_BUFFER_BIT 14. GL_DEPTH_BUFFER_BIT GL_DEPTH_BUFFER_BIT); 7.3.2. glMatrixMode Function The glMatrixMode function specifies which matrix is the current matrix. SYNTAX: void glMatrixMode(GLenum mode); PARAMETERS: 15. Mode: it can take GL_MODELVIEW as its value. glMatrixMode(GL_MODELVIEW); 7.3.3 glLoadIdentity Function The glLoadIdentity function replaces the current matrix with the identity matrix. SYNTAX: void glLoadIdentity(void); PARAMETERS: This function has no parameters. glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT|

7.3.4 glPushAttrib, glPopAttrib Pushes or pops the attribute stack. glPushAttrib glPopAttrib Pushes the attribute stack. Pops the attribute stack. glPushAttrib(0xffffffff); glPopAttrib();

7.3.5 glColor4f Function Sets the current color. SYNTAX: void glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); 16. glColor4f(0.0,0.0,0.0,0.05); 7.3.6 glutSwapBuffers glutSwapBuffers swaps the buffers of the current window if double buffered. SYNTAX: void glutSwapBuffers(void); 17. glutSwapBuffers();

7.4 FUNCTIONS USED TO RESHAPE


7.4.1 glViewport Function The glViewport function sets the viewport. SYNTAX: void glViewport(x, y,width, height); 18. glViewport(0,0,w,h); 7.4.2 gluPerspective Function set up a perspective projection matrix SYNTAX: void gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar ); PARAMETERS: 19. fovy: Specifies the field of view angle, in degrees, in the y-direction. 20. 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). 21. zNear: Specifies the distance from the viewer to the near clipping plane (always positive). 22. zFar: Specifies the distance from the viewer to the far clipping plane (always 23. gluPerspective(50.0,(float)w/(float)h,1.0,20.0); 7.4.3 glutTimerFunc PARAMETERS: 24. msecs: Number of milliseconds to pass before calling the callback. 25. func: The timer callback function. 26. value: Integer value to pass to the timer callback. 27. glutTimerFunc(TIMER,timer,0); 7.4.4 glMaterialfv Function The glMaterialfv function specifies material parameters for the lighting model. SYNTAX: void glMaterialfv(GLenum face, GLenum pname, const GLfloat params); PARAMETERS: 28. face: GL_FRONT, GL_BACK, or GL_FRONT and GL_BACK. 29. pname: 1.GL_DIFFUSE 2.GL_SPECULAR 30. glMaterialfv(GL_FRONT,GL_SPECULAR,white); 7.4.2 glColorMaterial Function The glColorMaterial function causes a material color to track the current color. SYNTAX: void glColorMaterial(GLenum face, GLenum mode); PARAMETERS: 31. face: Accepted values are GL_FRONT, GL_BACK, and GL_FRONT_AND_BACK. The default value is GL_FRONT_AND_BACK. 32. mode: Accepted values are GL_EMISSION, GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, and GL_AMBIENT_AND_DIFFUSE. The default value is GL_AMBIENT_AND_DIFFUSE. 33. glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); positive).

7.4.3 glutDisplayFunc Function glutDisplayFunc sets the display callback for the current window. SYNTAX: void glutDisplayFunc(void (*func)(void)); PARAMETERS: 34. func; The new display callback function. 35. glutDisplayFunc(display); 7.4.4 glutReshapeFunc Function glutReshapeFunc sets the reshape callback for the current window. SYNTAX: void glutReshapeFunc(void (*func)(int width, int height)); PARAMETERS: 36. func; The new reshape callback function. 37. glutReshapeFunc(reshape);

7.5 MAIN FUNCTION


7.5.1. glutInit Function glutInit is used to initialize the GLUT library. SYNTAX: glutInit(int *argcp, char **argv); PARAMETERS: 38. argcp; A pointer to the program's unmodified argc variable from main. Upon return, the value pointed to by argcp will be updated. 39. argv: The program's unmodified argv variable from main. Like argcp, the data for argv will be updated. 40. glutInit(&argc,argv); 7.5.2. glutInitDisplayMode Function glutInitDisplayMode sets the initial display mode. SYNTAX: void glutInitDisplayMode(unsigned int mode); PARAMETERS:

41. mode:

1.

GLUT_RGB

2.GLUT_DOUBLE

3.GLUT_SINGLE

4.GLUT_DEPTH 42. glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE); 7.5.3. glutInitWindowPosition, glutInitWindowSize Functions glutInitWindowPosition and glutInitWindowSize set the initial window position and size respectively. SYNTAX: void glutInitWindowSize(int width, int height); void glutInitWindowPosition(int x, int y); 43. glutInitWindowSize(300,300); 7.5.4. glutCreateWindow Function glutCreateWindow creates a top-level window. SYNTAX: int glutCreateWindow(char *name); 44. glutCreateWindow("two pass mirror"); 7.5.5. glutMainLoop Function glutMainLoop enters the GLUT event processing loop. SYNTAX: void glutMainLoop(void); 45. glutMainLoop();

Chapter8:

TESTING

Software testing can be stated as the process of validating and verifying that a software program/application : 1. meets the requirements that guided its design and development; 2. works as expected; and 3. can be implemented with the same characteristics anywhere Here most of the test effort has occurred after the requirements have been defined and the coding process has been completed. We carried out this test for the following reasons:

46. Examination of code as well as execution of that code in various environments and conditions as well as examining the aspects of code: does it do what it is supposed to do and do what it needs to do. 47. Functional testing was carried out in order to find "can the user do this" or "does this particular feature work".

Chapter9:

SCREEN SHOTS

Level- submenu, to choose the level

Game in progress

Display of GAME OVER when obstacle hits the copter

Chapter 10:

CONCLUSION

We started with modest aim with no prior experience in any programming projects as this, but ended up in learning many things, fine tuning the programming skills and getting into the real world of software development with an exposure to corporate environment. During the development of any software of significant utility, we are faced with the trade-off between speed of execution and amount of memory consumed. This is simple interactive application . It is extremely user friendly and has the features, which makes simple graphics project. It has an open source code and no security features has been included. The user is free to alter the code for feature enhancement. Checking and verification of all possible types of the functions are taken care. Care was taken to avoid bugs. Bugs may be reported to creator as the need may be .So, we conclude on note that we are looking forward to develop more such projects with an appetite to learn more in computer graphics.

Chapter11:

FUTURE SCOPE

In this project we have obstacles in form of rectangles. We can have obstacles of different shapes and sizes. A touch of animation can be given to the obstacles also. When the copter comes in contact with the obstacle, instead of assuming that the game is over, we can reduce the score with respect to what kind of obstacle it hits. Currently, there is only one life for the copter, if desired the life count can be extended in the enhanced model. We can also increase the levels, if desired and the complexity of game increases with each level, like the number of obstacles should increase as the level of game is increased. The above is beyond the scope of our study.

Chapter12:

REFERENCES

48. Interactive Computer Graphics: A Top Down Approach with OpenGL- Edward Angel, 5th Edition, Addison-Wellesley,2008

2. OpenGL SuperBible: Comprehensive Tutorial and ReferenceRichard S.Wright, Jr. and others, 4th Edition, Addison

Chapter 13:
#pragma once #ifndef _WIN32_WINNT

SOURCE CODE

#define _WIN32_WINNT 0x0600 #endif extern int num_texture; extern int LoadBitmap(char *filename); #include <stdio.h> #include <tchar.h> #include<stdlib.h> #include<conio.h> #include<GL/glut.h> #include <windows.h> #include <string.h> #include <math.h> #include <GL/gl.h> int num_texture=0; char *s; void *font = GLUT_BITMAP_TIMES_ROMAN_24; void *font1=GLUT_BITMAP_8_BY_13 ; int id_rock,id_dummy,id_top,id_btm; int score=0,level=1,start=0,flag=1; GLfloat globalAmbient[] = { 0.1,0.1,0.1 }; GLfloat ambientLight[]={0.4,0.4,0.4}; GLfloat diffuseLight[] = { 0.8,0.8,0.8 }; GLfloat specularLight[]={0.5,0.5,0.5};

GLfloat lightPos[] = { 0,25,0 }; float mcolor[] = { 1, 0, 1, 1}; void initLight() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glLightModelfv(GL_LIGHT_MODEL_AMBIENT,globalAmbient); glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight); glLightfv(GL_LIGHT0,GL_DIFFUSE,specularLight); glLightfv(GL_LIGHT0,GL_POSITION,lightPos); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor); glMateriali(GL_FRONT, GL_SHININESS, 100); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); } float a[4][3]={7.0,-1.5,0, 7.2,-1.5,0, 7.2,-2.5,0, 7,-2.5,0}; float a1[4][3]={14.0,-1.5,0, 14.2,-1.5,0, 14.2,-2.5,0, 14,-2.5,0}; float a2[4][3]={10.0,-1.5,0, 10.2,-1.5,0, 10.2,-2.5,0, 10,-2.5,0};

float b[4][3]={-6,3.88,0,6,3.88,0,6,5,0,-6,5,0}; float c[4][3]={-6,-5,0,6,-5,0,6,-3.88,0,-6,-3.88,0}; float d[4][3]={2.5,-4.2,1,4,-4.2,1,4,-3.6,1,2.5,-3.6,1}; float e[4][3]={-2,0,0,4,0,0,2,3,0,-2,3,0}; float left_mov=0; float left_mov1=0; float left_mov2=0; int rand_y=2; int rand_y1=3; int rand_y2=0; void drawcube(); int LoadBitmap(char *filename) { int i, j=0; //Index variables FILE *l_file; //File pointer unsigned char *l_texture; BITMAPFILEHEADER fileheader; BITMAPINFOHEADER infoheader; RGBTRIPLE rgb; num_texture++; if( (l_file = fopen(filename, "rb"))==NULL) return (-1); fread(&fileheader, sizeof(fileheader), 1, l_file); fseek(l_file, sizeof(fileheader), SEEK_SET); fread(&infoheader, sizeof(infoheader), 1, l_file); l_texture = (byte *) malloc(infoheader.biWidth * infoheader.biHeight * 4); memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4); for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++) { fread(&rgb, sizeof(rgb), 1, l_file); l_texture[j+0] = rgb.rgbtRed; l_texture[j+1] = rgb.rgbtGreen;

l_texture[j+2] = rgb.rgbtBlue; l_texture[j+3] = 255; j += 4; } fclose(l_file); glBindTexture(GL_TEXTURE_2D, num_texture); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture); free(l_texture); return (num_texture); } GLUquadricObj *body,*back,*sidepipe; float wx=-4,wy=0,wz=-6; float gx=wx+2,gy=wy,gz=wz; int fanangle=0; int i=0; float backpoint=0; int angle=180; void init() { glClearColor(0,0,0,0); glEnable(GL_DEPTH_TEST);

glEnable(GL_TEXTURE_2D); body=gluNewQuadric(); back=gluNewQuadric(); sidepipe=gluNewQuadric(); glEnable(GL_TEXTURE_2D); id_rock=LoadBitmap("wood.bmp"); id_dummy=LoadBitmap("dummy.bmp"); id_top=id_btm=LoadBitmap("bot.bmp"); initLight(); } void display2(); void output(float x, float y, float z,void *font, char *string) { int len=0, i; glRasterPos3f(x, y, z); printf("%d",len); len = (int) strlen(string); for (i = 0; i < len; i++) { glutBitmapCharacter(font, string[i]); } } void drawPoly1(float a[4][3]) { glPushMatrix(); glColor3f(1.0,1.0,1.0); glBegin(GL_QUADS); glVertex3fv(a[0]); glVertex3fv(a[1]); glVertex3fv(a[2]); glVertex3fv(a[3]); glEnd();

glFlush(); glPopMatrix(); } void drawPoly(float a[4][3]) { glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3fv(a[0]); glTexCoord2f(0,1); glVertex3fv(a[1]); glTexCoord2f(1,1); glVertex3fv(a[2]); glTexCoord2f(1,0); glVertex3fv(a[3]); glEnd(); glFlush(); } void timer(int val) { score=score+level; glutTimerFunc(200,timer,1); } void display3() { printf("asjdkajdsa"); output(2,1,-2,font,"GAME OVER kahkjahdkj hkashdkajshdkjad asdasd"); } void display1() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); gluLookAt(0,5,10,0,0,-2,0,1,0); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glColor3f(0.7,0,1.0); output(-1.2,4,4,font,"COPTER GAME"); output(-6,-7,-10,font1,"Right Click to Enter into the Game"); if(start==1) display2(); else { glBindTexture(GL_TEXTURE_2D, id_dummy); glPushMatrix(); wx=-1; wy=-2; glTranslatef(wx,wy,wz); glRotatef(angle,0,1,0); glTranslatef(-wx,-wy,-wz); glPushMatrix(); glScalef(.5,.5,.5); glColor3f(0.3,0.3,0.5); glPushMatrix(); glTranslatef(wx-3,wy,wz); glScalef(1,1,.7); gluSphere(body,1.5,30,30); glScalef(1,1,1.3); glTranslatef(wx+0.6-wx,wy-wy,wz-wz); glScalef(1,1,.7); gluSphere(body,1.5,30,30); glPopMatrix(); glColor3f(0,0,1);

glPushMatrix(); glTranslatef(wx-2.5,wy+1.5,wz); glRotatef(180,0,1,0); glScalef(.5,5,.5); glutSolidCube(0.3); glPopMatrix(); glColor3f(1,0,1); glPushMatrix(); glTranslatef(wx-2.5,wy+2.1,wz); glRotatef(fanangle,0,1,0); glScalef(14,.5,.5); glutSolidCube(.3); glPopMatrix(); glPushMatrix(); glTranslatef(wx-2.5,wy+2.1,wz); glRotatef(fanangle,0,1,0); glScalef(.5,.5,14); glutSolidCube(.3); glPopMatrix(); glPushMatrix(); glColor3f(0.1,0.1,0.2); glTranslatef(wx-2,wy+0.4,wz); glScalef(1,0.6,.8); glRotatef(90,0,1,0); gluCylinder(back,1.2,0.5,4,30,30); glPopMatrix(); glPushMatrix(); glColor3f(0.2,0.2,0.2); glTranslatef(wx-2,wy+0.4,wz); glRotatef(90,0,1,0); gluCylinder(back,1.0,0.5,4,30,30);

glPopMatrix(); glPushMatrix(); glTranslatef(wx+2,wy+0.4,wz); glRotatef(90,0,1,0); gluCylinder(back,0.5,0.3,1,30,30); glPopMatrix(); glPushMatrix(); glColor3f(0,0,1); glTranslatef(wx+3,wy+0.4,wz); gluSphere(back,0.3,30,30); glPopMatrix(); glColor3f(1,0,1); glPushMatrix(); glTranslatef(wx+3,wy+0.3,wz); glRotatef(fanangle,1,0,0); glScalef(0.5,13,.5); glutSolidCube(.3); glPopMatrix(); glPushMatrix(); glTranslatef(wx+3,wy+0.3,wz); glRotatef(fanangle,1,0,0); glScalef(0.5,0.5,14); glutSolidCube(.3); glPopMatrix(); glPushMatrix(); glPopMatrix(); glPopMatrix(); glPopMatrix(); fanangle=(fanangle+5)%360; glutSwapBuffers(); glutPostRedisplay();

} } void display2() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0,0,10,0,0,-1,0,1,0); wx=-4; output(1.01,-4.05,1 ,font,"SCORE"); char * b1= new char [50]; itoa(score, b1, 10); //b if(score<100) output(2.8,-3.99,1.1,font,"0"); output(2.99,-3.99,1.1,font,b1); float c[4][3]={-6,-5,0,6,-5,0,6,-3.88,0,-6,-3.88,0}; glPushMatrix(); glBindTexture(GL_TEXTURE_2D, id_rock); if(flag==1) { drawPoly(a); a[0][0]=7-left_mov; a[1][0]=7.2-left_mov; a[2][0]=7.2-left_mov; a[3][0]=7-left_mov; a[0][1]=-1.5+rand_y; a[1][1]=-1.5+rand_y; a[2][1]=-2.5+rand_y; a[3][1]=-2.5+rand_y; drawPoly(a1); a1[0][0]=14-left_mov1; a1[1][0]=14.2-left_mov1;

a1[2][0]=14.2-left_mov1; a1[3][0]=14-left_mov1; a1[0][1]=-1.5+rand_y1; a1[1][1]=-1.5+rand_y1; a1[2][1]=-2.5+rand_y1; a1[3][1]=-2.5+rand_y1; drawPoly(a2); a2[0][0]=10-left_mov2; a2[1][0]=10.2-left_mov2; a2[2][0]=10.2-left_mov2; a2[3][0]=10-left_mov2; a2[0][1]=-1.5+rand_y2; a2[1][1]=-1.5+rand_y2; a2[2][1]=-2.5+rand_y2; a2[3][1]=-2.5+rand_y2; glFlush(); } drawPoly1(d); glFlush(); float cent=(a[0][1]+a[2][1])/2; float cent1=(a1[0][1]+a1[2][1])/2; float cent2=(a2[0][1]+a2[2][1])/2; printf("gy=%f\n",gy/2); printf("cent=%f\n",cent); printf("a[0][0]=%f\n",a[0][0]); printf("gx=%f\n\n",gx); printf("gy=%f\n",gy/2); printf("cent1=%f\n",cent1); printf("a1[0][0]=%f\n",a1[0][0]); printf("gx=%f\n\n",gx); if(gy/2==cent && (int)a[0][0]<=(int)gx && (int)a[0][0]>=(int)wx)

{ printf("Success00000000000000000"); flag=0; } if(gy/2==cent1 && (int)a1[0][0]<=(int)gx && (int)a1[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0;; } if(gy/2==cent2 && (int)a2[0][0]<=(int)gx && (int)a2[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)-0.25==cent && (int)a[0][0]<=(int)gx && (int)a[0][0]>=(int)wx) { printf("Success00000000000000000"); flag=0; } if((gy/2)-0.25==cent1 && (int)a1[0][0]<=(int)gx && (int)a1[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)-0.25==cent2 && (int)a2[0][0]<=(int)gx && (int)a2[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+0.25==cent && (int)a[0][0]<=(int)gx && (int)a[0][0]>=(int)wx) {

printf("Success00000000000000000"); flag=0; } if((gy/2)+0.25==cent1 && (int)a1[0][0]<=(int)gx && (int)a1[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+0.25==cent2 && (int)a2[0][0]<=(int)gx && (int)a2[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)-0.50==cent && (int)a[0][0]<=(int)gx && (int)a[0][0]>=(int)wx) { printf("Success00000000000000000"); flag=0; } if((gy/2)-0.50==cent1 && (int)a1[0][0]<=(int)gx && (int)a1[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)-0.50==cent2 && (int)a2[0][0]<=(int)gx && (int)a2[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+0.50==cent && (int)a[0][0]<=(int)gx && (int)a[0][0]>=(int)wx) {

printf("Success00000000000000000"); flag=0; } if((gy/2)+0.50==cent1 && (int)a1[0][0]<=(int)gx && (int)a1[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+0.50==cent2 && (int)a2[0][0]<=(int)gx && (int)a2[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+0.75==cent && (int)a[0][0]<=(int)gx && (int)a[0][0]>=(int)wx) { printf("Success00000000000000000"); flag=0; } if((gy/2)+0.75==cent1 && (int)a1[0][0]<=(int)gx && (int)a1[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+0.75==cent2 && (int)a2[0][0]<=(int)gx && (int)a2[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)-0.75==cent && (int)a[0][0]<=(int)gx && (int)a[0][0]>=(int)wx) { printf("Success00000000000000000");

flag=0; } if((gy/2)-0.75==cent1 && (int)a1[0][0]<=(int)gx && (int)a1[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)-0.75==cent2 && (int)a2[0][0]<=(int)gx && (int)a2[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+1==cent && (int)a[0][0]<=(int)gx && (int)a[0][0]>=(int)wx) { printf("Success00000000000000000"); flag=0; } if((gy/2)+1==cent1 && (int)a1[0][0]<=(int)gx && (int)a1[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+1==cent2 && (int)a2[0][0]<=(int)gx && (int)a2[0][0]>=(int)wx) { printf("Success1111111111111111"); flag=0; } if((gy/2)+1==4) { flag=0; }

if((gy/2)-.75==-4) { flag=0; } glPushMatrix(); glBindTexture(GL_TEXTURE_2D, id_top); drawPoly(b); glBindTexture(GL_TEXTURE_2D, id_btm); drawPoly(c); glPopMatrix(); glPopMatrix(); if (level==1) { left_mov+=0.04; left_mov1+=0.04; left_mov2+=0.03; } if (level==2) { left_mov+=0.07; left_mov1+=0.03; left_mov2+=0.04; } if(flag==1) { if(left_mov>16) { left_mov=0; rand_y=(rand()*rand())%6; a[0][0]=7; a[1][0]=7.2;

a[2][0]=7.2; a[3][0]=7; a[0][1]=-1.5; a[1][1]=-1.5; a[2][1]=-2.5; a[3][1]=-2.5; } if(left_mov1>20) { left_mov1=0; rand_y1=(rand()*rand())%6; a1[0][0]=14; a1[1][0]=14.2; a1[2][0]=14.2; a1[3][0]=14; a1[0][1]=-1.5; a1[1][1]=-1.5; a1[2][1]=-2.5; a1[3][1]=-2.5; } if(left_mov2>18) { left_mov2=0; rand_y2=(rand()*rand())%6; a2[0][0]=10; a2[1][0]=10.2; a2[2][0]=10.2; a2[3][0]=10; a2[0][1]=-1.5; a2[1][1]=-1.5; a2[2][1]=-2.5;

a2[3][1]=-2.5; } } glBindTexture(GL_TEXTURE_2D, id_dummy); glPushMatrix(); glTranslatef(wx,wy,wz); glRotatef(angle,0,1,0); glTranslatef(-wx,-wy,-wz); glPushMatrix(); glScalef(.5,.5,.5); glColor3f(0.3,0.3,0.5); glPushMatrix(); glTranslatef(wx-3,wy,wz); glScalef(1,1,.7); gluSphere(body,1.5,30,30); glScalef(1,1,1.3); glTranslatef(wx+0.6-wx,wy-wy,wz-wz); glScalef(1,1,.7); gluSphere(body,1.5,30,30); glPopMatrix(); glColor3f(0,0,1); glPushMatrix(); glTranslatef(wx-2.5,wy+1.5,wz); glRotatef(180,0,1,0); glScalef(.5,5,.5); glutSolidCube(0.3); glPopMatrix(); glColor3f(1,0,1); glPushMatrix(); glTranslatef(wx-2.5,wy+2.1,wz); glRotatef(fanangle,0,1,0);

glScalef(14,.5,.5); glutSolidCube(.3); glPopMatrix(); glPushMatrix(); glTranslatef(wx-2.5,wy+2.1,wz); glRotatef(fanangle,0,1,0); glScalef(.5,.5,14); glutSolidCube(.3); glPopMatrix(); glPushMatrix(); glColor3f(0.1,0.1,0.2); glTranslatef(wx-2,wy+0.4,wz); glScalef(1,0.6,.8); glRotatef(90,0,1,0); gluCylinder(back,1.2,0.5,4,30,30); glPopMatrix(); glPushMatrix(); glColor3f(0.2,0.2,0.2); glTranslatef(wx-2,wy+0.4,wz); glRotatef(90,0,1,0); gluCylinder(back,1.0,0.5,4,30,30); glPopMatrix(); glPushMatrix(); glTranslatef(wx+2,wy+0.4,wz); glRotatef(90,0,1,0); gluCylinder(back,0.5,0.3,1,30,30); glPopMatrix(); glPushMatrix(); glColor3f(0,0,1); glTranslatef(wx+3,wy+0.4,wz); gluSphere(back,0.3,30,30);

glPopMatrix(); glColor3f(1,0,1); glPushMatrix(); glTranslatef(wx+3,wy+0.3,wz); glRotatef(fanangle,1,0,0); glScalef(0.5,13,.5); glutSolidCube(.3); glPopMatrix(); glPushMatrix(); glTranslatef(wx+3,wy+0.3,wz); glRotatef(fanangle,1,0,0); glScalef(0.5,0.5,14); glutSolidCube(.3); glPopMatrix(); glPushMatrix(); glPopMatrix(); glPopMatrix(); glPopMatrix(); if(flag==0) output(-1.8,2,3,font,"GAME OVER"); fanangle=(fanangle+5)%360; glutSwapBuffers(); if(flag==1) { glutPostRedisplay(); } } void reshape(int w,int h) { if(h==0) h=1;

glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(52.0f,w/h,1,1000); glMatrixMode(GL_MODELVIEW); } void keyboard(unsigned char key,int x,int y) { switch(key) { case 27: exit(0); break; case 'w': if(wy<12) wy++; gy=wy/2; break; case 's': if(wy>-13) wy--; gy=wy/2; break; } } void reset() { score=0; } void Copter(int id) {

switch(id) { case 1:exit(0); break; case 2:start=1; break; default :break; } glutPostRedisplay(); } void levelMenu(int id) { switch(id) { case 4:level=1; reset(); break; case 5:level=2; reset(); break; default :break; } glutPostRedisplay(); } int main() { glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB); glutInitWindowSize(700,600); glutInitWindowPosition(0,0); glutCreateWindow("copter game"); init();

glutDisplayFunc(display1); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutTimerFunc(200,timer,1); int sub_menu=glutCreateMenu(levelMenu); glutAddMenuEntry("Level 1",4); glutAddMenuEntry("Level 2",5); glutCreateMenu(Copter); glutAddMenuEntry("Start Game",2); glutAddSubMenu("Level",sub_menu); glutAddMenuEntry("Quit",1); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop(); return 0; }

Вам также может понравиться