My Homework:An Interaction 3D Terrain Game

1. Design outline

This project is based on Visual Studio 2005 with C++ language. The library of OpenGL is GLUT 3.7 version.

1.1 The expansion of source files

The source files of this project are shown below:

clip_image001 clip_image002

Source Files

The first, from the aspect of source, it composes of four classes and designs in independent .cpp files, the main functions of them are as follows:

●class Menu for constructing and interaction of menu in this game.

●class Model for viewing and texturing of car which can move on terrain.

●class Terrain for viewing and texturing of terrain, trees and balls.

●class SkyBox for building the sky in this game.

The second, there are also two function library are need in the project, they are bmpLoader and Vector. The main functions of them are as follows:

●bmpLoader.cpp for loading the source what need by this game, such as bmp files, and so on.

●Vector.cpp for vector calculate, such as calculate the triangle of two vector.

The third, the last but the most important is Project.cpp file which is including main function, initail OpenGL platform, Desplay function, simple collision and keyboard & mouse interactions.

1.2 The explansion of main flowchart

The big map of this project is shown as below flowchart:

Y0J`))~)VTO`$M6H_HNG]9T

Big Map of flowchart

To explansion of flowchart, first we must focus on the main() function in Project.cpp.

●Make use of GLUT library to initial display mode and create window form.

●Load files including *.bmp and *.obj into PC memory.

●Rigister display, mouse and keyboard interaction functions.

●Call glutMainLoop() fucntion.

If all of this fucntion has been called, a window will appear and user can use keyboard and mouse to contral the car moving on terrain.

Except of main fucntion, the second impotant function is display() fucntion. From the function requirement, there are four states.

●Initial of menu which is define as state 0.

●Show menus which is define as state 1.

●Initial of Game which is define as state 2.

●Show Game which is define as state 3.

Those states can contral by click menu to forward and press ESC buttion in keyboard to backward. According to this two kinds of interaction, the state could contraled by game player.

The last should descript fucntion is Gamedisplay() function which is third importance.

●Clear buffer of frame.

●Setting projection.

●Setting gluLookAt() fucntion.

●Update and calculate the position of car.

●Call simple collision detection function.

●Draw terrain, tree, ball.

●Draw car.

●Draw sky box.

Graphic knowledge is briefly used in this function and its sub-function. The detail will indicate as follow chapter.

From this chapter we have known the outline of programme on source architecture and flowchart. And then the next chapter will show more detail of algorithms and code explanation.

2. Explanation of key code

There are several aspect is define as key code, including menu construction, viewing & texturing, navigation & simple collision detection and illumination & shading. most of them are involve Graphic field.

2.1 Menu Construction

To construct menu, there are composed of two parts which is to show the menu and interaction by mouse.

●To show the menu

The key approach is to call glRasterPos2i() and glDrawPixels() with reasonably parameters. The .bmp which need to construct menus has been load in main() function. Take show ‘New Game’ item in window as an example. The other items are the same as ‘New Game’ item.

We can clearly see the source shown below:

clip_image004

To show ‘New Game’ item source

From the source I make use of glDrawPixels() function to indicate the position of NewGame387x638.bmp or NewGameSelected387x638.bmp. To draw NewGame387x638.bmp or NewGameSelected387x638.bmp is controlled by the ‘if’ sentence which parameter is the position of mouse and the size of .bmp file.

●Interaction by mouse

There are three different function of menu items, they are ‘New Game’, ‘Resume Game’ and ‘Exit’ functions.

The first is ‘New Game’, source shown below:

clip_image005

Source of ‘New Game’ function

There are two main jobs, which are ‘re-initial all of parameter’ and ‘convert to next state’. ‘re-initial all of parameter’ is easy to understand. But the ‘convert to next state’, as I have expansion before, what is the four states in chapter 1.2.

The second, ‘Resume Game’ item, It is simple than ‘New Game” item. It only has ‘convert to next state’. So that means your Game Score is still in there, did not clear by Game.

The third, The most easy function, just call exit() function to quit Game.

2.2 Viewing and Texturing

There are three objects which are Model, Terrain and Skybox in this Game. And all of them have viewing and texturing. Before expansion of those objects, we should remind the setting of projection and setting of gluLookAt() function.

●Setting of projection

clip_image006

Setting projection

●Setting of gluLookAt() function

There are two kinds of viewing parameters, they are the car driver’s point of view and chase camera’s point of view. And the source below is chase camera’s point of view as example:

clip_image007

There is more detail of three objects:

●Model

Because the state of the car is decided by the control of Game Player and the environment of Terrain. So when draw the car it will transform, scale and rotate.

clip_image008

Transform, scale and rotate of car

Consequently, the most important is how to get the parameters. So I add a Update() function to update the parameter when player control the car.

●Terrain with trees and balls

There was loaded Terrain.bmp into memory. And now we make use of it and (x,y) to calculate the height of terrain. And then using GL_TRIANGLE_STRIP as parameter of glBegin() to draw Game’s Terrain. The code of below is to draw the model.

clip_image009

Draw the model of terrain

To draw trees on terrain, I draw two triangles as for a tree with Tree.bmp to texturing. The balls is more simple than that just call gluSphere() function. But the position of them are randomness which is firstly generated by rand() function..

●Skybox

The sky of Game, we must clearly known that the sky is a box. And the terrain is located in the center of the box. I draw it with GL_QUADS as parameter of glBegin.

As for texturing, there are the key code from my source:

clip_image010

Build a texturing

2.3 Navigation and Simple Collision Detection

As for navigation, in my Game, you have two ways which is keyboard and mouse to control the car. The mouse is simple and then I expansion more of keyboard control function. The code quote shown below:

clip_image011

Keyboard control key code

The key function GetAsyncKeyState() is the library of windows.h rather than OpenGL. Although there is not involve OpenGL, but Player could control by keyboard is easier than mouse. So I think it also is key code.

Simple collision detection, my idea is that when Player control the car, and then I will get the position vector (x,y,z) of car. And as mentioned above I save the position of trees and balls in arrays which value is got from rand() function. So I just compare this two vector and if this two point is approach same, I say they are collision.

2.4 Illumination and Shading

To add illumination function into the Game, we must define the normal vectors of Terrain, Trees and Car when draw them. Take draw tree as example:

clip_image012

Draw a tree

We can see glNormal3f() between glTexCoord2f() and glVertex3f(). After define the normal vector, the next is to create a sun. The code is shown below:

clip_image013

To define a sun

3. User manual of Game

After compile and run, that is one picture capture from the game.

clip_image015

Have a first look of my Game (chase camera)

clip_image017

Have a first look of my Game (driver’s view)

●The rules of My Game

The rules of my Game are controlling the car to hit the red ball, and then Player will get 5 points and the red ball turn into blue. But if Player hit the tree, and then the Score will decrease minus 5 point and the tree will fall.

There is also having a sun up and down every 60 seconds as for one day. So Player will not seem anything when the sun is down. It is 30 seconds as day and 30 seconds as night.

●Hot key function

I have defined some hot keys represent for some functions. The list as follows:

Ø Key ‘f’: to convert window to full.

Ø Key ‘g’: to convert full to window.

Ø Key ‘1’: to drivers view.

Ø Key ‘2’: to chase camera.

Ø Key UP: to increase the speed.

Ø Key DOWN: to decrease the speed.

Ø Key LEFT: to turn the car to left.

Ø Key RIGHT: to turn the car to right.

●Menu function

clip_image019

The GUI of menu

When the mouse move over the item of the menu, and then the menu will change, take ‘New Game’ as an example:

clip_image021 Turn into clip_image023

Mouse move over

When click then left button, it will trigger the function hidden in the Graphic User Interface. The function is shown below:

Ø New Game: that mean the Score will turn to 0 as begin a new game.

Ø Resume Game: if interrupt in the middle of playing game, using this to return game. The Score will still there.

Ø Exit: to quit the game

l Control by Keyboard & Mouse

In my Game, navigation using mouse and keyboard. The keyboard to control is simple way, so I will tell more detail of control by mouse.

The car should move forward in the direction it is facing as long as the left mouse button is pressed. The heading of the car will rotated by pressing the left mouse button and moving the mouse horizontally. Just as in a real car, the direction of rotation will reverse when the car is moving backwards.

Actually, you can control the car using keyboard and mouse at the same time.

4. Experimental results

After compile and run my Game. Everything is right. All of the function has implemented. And I found the most worse is the head of car’s Up and Down. I have captured a picture shown below:

clip_image025

A problem of my Game

Why will arouse this fault? The half of car has lost. There is because of calculate the normal vector of car, is not so precise. And then from the picture shown, the car should rotate Up and Down. Actually, it did. But Up and Down is not so precise, just a little.If want to solve this problem, the Game should more calculate.

Another problem is that this project is not composing the shadow mechanism.

5. Summary of this project

After this project I know how to build a simple 3D Game with OpenGL platform. And know more detail about the Graphic theory in implement.

It includes several key points:

●Menu construction

●Viewing and Texturing

●Navigation by keyboard and mouse

●Simple collision detection

●Illumination and shading

Except involve OpenGL, there are also including make use of vector in Graphic interaction software.

I am a graduate student in macau university of science and technology. If you want the source of this project, please contact me by e-mail:mr.chan.yuen#gmail.com

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值