chapter 17 creating an interactive 3d environment © 2008 cengage learning emea

17
CHAPTER 17 CHAPTER 17 Creating an Interactive 3D Creating an Interactive 3D Environment Environment © 2008 Cengage Learning EM

Upload: nora-tamsin-davis

Post on 22-Dec-2015

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

CHAPTER 17CHAPTER 17

Creating an Interactive 3DCreating an Interactive 3D

EnvironmentEnvironment

© 2008 Cengage Learning EMEA

Page 2: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

LEARNING OBJECTIVESLEARNING OBJECTIVES In this chapter you will learn about:

– Game engine architecture– Game initialization and shutdown– The game loop– Creating an interactive DirectX 10 3D

environment– Creating an interactive OpenGL 3D

environment

Page 3: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

GAME ENGINE GAME ENGINE ARCHITECTUREARCHITECTURE

A game engine is the central unit of any computer game and it can be described as a collection of technologies such as a sound engine, AI subsystem, physics engine, networking subsystem, 3D renderer, and input control system.

The number of subsystems provided is highly dependent on the developer’s requirements and the implementation platform of choice.

Page 4: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

GAME ENGINE GAME ENGINE ARCHITECTUREARCHITECTURE

Game engines, built upon various APIs such as DirectX and OpenGL, are normally designed with software componentry in mind. – This allows for decomposition of the engine,

resulting in numerous functional units. By designing component-based engines, we

are able to replace provided technologies with other third-party or in-house developed units as needed. – For example, a game engine’s renderer, physics

engine or sound system can easily be replaced by an improved or alternate version in a plug-and-play fashion.

Page 5: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

GAME ENGINE GAME ENGINE ARCHITECTUREARCHITECTURE

The term ‘game engine’ existed for some time, but only became truly common in the mid-1990s when developers started licensing the core code of other games for their own titles.

This reuse led to the development of high-end commercial game engines and middleware providing game developers with a number of game creation tools and technical components – i.e. accelerating the game development process.

Page 6: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

GAME ENGINE GAME ENGINE ARCHITECTUREARCHITECTURE

We can divide the source code of a game into two units, namely, the game-engine code and the game-specific code.– The game-specific code deals exclusively with in-

game play elements, for instance, the behaviour of non-player characters, mission-based events and logic, and the main menu.

– Game-specific code is not intended for future reuse and thus excluded from the game engine code.

– Game-engine code forms the core of the entire game implementation with the game-specific code being executed on top of it.

– The game engine is separate from the game being developed in the sense that it provides all the technological components without any hard coded information about the actual gameplay.

Page 7: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

GAME ENGINE GAME ENGINE ARCHITECTUREARCHITECTURE

Game-engine code and game-specific code can be designed and integrated using one of the following architectures: ad-hoc, modular, or the directed acyclic graph architecture (DAG).

Page 8: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

GAME ENGINE GAME ENGINE ARCHITECTUREARCHITECTURE Once we have chosen the preferred overall architecture, we have to

summarize all possible states our game will go through from initialization to shutdown.

Possible states (with associated events) are listed here:– Initialization.– Enter the main game loop:

Additional initialization and memory allocation. Load intro video. Initialize and display in-game menu:

– Event monitoring.– Process user input.

Start game. In-game loop:

– Input monitoring.– Execution of artificial intelligence (AI)– Execution of physics routines.– Sound and music output.– Execution of game logic.– Rendering of the scene based on the input from the user and

other subsystems.– Display synchronization.– Update game state.

Exit the game and return to the in-game menu.– Shutdown of the game if the user wishes to terminate the program.

Page 9: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

Initialization and ShutdownInitialization and Shutdown The first step invoked whenever a game is executed,

is initialization. This step deals with resource and device acquisition,

memory allocation, initialization of the game’s GUI, and loading of art assets such as an intro video from file.

The first initialization phase is commonly referred to as the ‘front-end initialization step’ to distinguish it from the level and actual game play initialization phases. – Front-end initialization occurs prior to the game

loop and is required for setting up the environment by assigning resources and loading game data and assets

Page 10: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

Initialization and ShutdownInitialization and Shutdownvoid FrontEndInit()void FrontEndInit(){{

AcquireResources();AcquireResources();AllocMem();AllocMem();LoadAssets();LoadAssets();InitGUI();InitGUI();LoadPlayerPreferences();LoadPlayerPreferences();

}} All devices and resources are released and final

program cleanup is done during the exit state. The exit state has to release all resources and

devices acquired, memory allocated and data loaded in the reverse order of the initial front-end acquisition.

Page 11: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

Initialization and ShutdownInitialization and Shutdown

void Cleanup()void Cleanup(){{

SavePlayerPreferences();SavePlayerPreferences();ShutdownGUI();ShutdownGUI();ShutdownAssetAccess();ShutdownAssetAccess();FreeMem();FreeMem();ReleaseResources();ReleaseResources();

}}

Page 12: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

The Game LoopThe Game Loop The game loop allows uninterrupted

execution of the game. It enables us to execute a series of tasks

such as input monitoring, execution of artificial intelligence and physics routines, sound and music processing, execution of game logic, display synchronization and so forth for every frame rendered.

All these tasks are processed on a per-frame basis, thus resulting in a living world where everything happens in a seemingly concurrent manner, especially so where the computer game runs at 40 frames per second or more.

Page 13: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

The Game LoopThe Game Loop Timing allows a game to execute at a speed

independent of the frame rate or processor’s clock speed.

Most games released today make use of variable frame timing.

Another key element of any game is the processing of player input.

Other tasks performed during the game loop include the execution of AI code so that NPCs can decide where to go next or what action to take, object updates, the execution of game code and scripts, the execution of physics code to ensure correct inter-object and object-entity interaction, updating the camera according to player input, animating objects and updating particle effects, etc.

Page 14: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

The Game LoopThe Game Loop

while(!ExitGame())while(!ExitGame()){{

UpdateTiming();UpdateTiming();InputHandling();InputHandling();UpdateNetworking();UpdateNetworking();ExecuteScripts();ExecuteScripts();UpdateAI();UpdateAI();UpdatePhysics();UpdatePhysics();UpdateSound();UpdateSound();UpdateEntities();UpdateEntities();UpdateCamera();UpdateCamera();CollisionDetection();CollisionDetection();CollisionResponse();CollisionResponse();RenderFrame();RenderFrame();UpdateGameState();UpdateGameState();

}}

Page 15: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

The Game LoopThe Game Loop The following code sample illustrates the possible structure

of a decoupled game loop:

while(!ExitGame()){

UpdateTiming();InputHandling();if(UpdateWorld()){

UpdateNetworking();ExecuteScripts();UpdateAI();UpdatePhysics();UpdateSound();UpdateEntities();UpdateCamera();CollisionDetection();CollisionResponse();]

}InterpolateObjectStates();RenderFrame();UpdateGameState();

}

Page 16: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

CREATING AN INTERACTIVE CREATING AN INTERACTIVE DIRECTX 10 3D ENVIRONMENTDIRECTX 10 3D ENVIRONMENT

[see the textbook and book website for the source code example and detailed discussion].

Page 17: CHAPTER 17 Creating an Interactive 3D Environment © 2008 Cengage Learning EMEA

CREATING AN INTERACTIVE CREATING AN INTERACTIVE OPENGL 3D ENVIRONMENTOPENGL 3D ENVIRONMENT

[see the textbook and book website for the source code example and detailed discussion].