Introduction to 3d game engine design using directx 9 and c

608 118 0
Introduction to 3d game engine design using directx 9 and c

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Introduction to 3D Game Engine Design Using DirectX 9 and C# ISBN:1590590813 by Lynn Thomas Harrison Apress © 2003 (422 pages) This text shows how to develop a complete 3D game engine, write games using C#, DirectX 9, and the NET Framework, and go beyond simple graphics to explore audio, user input, artificial intelligence, and multiplayer design Table of Contents Introduction to 3D Game Engine Design Using DirectX 9 and C# Introduction Chapter 1 - Overview Chapter 2 - User Interface Hanging Ten—A Ride Through the Chapter 3 Rendering Pipeline Chapter 4 - Basic 3D Objects Chapter 5 - Complex 3D Objects Chapter 6 - Camera—The Player’s View of the World Adding Some Atmosphere—Lighting and Chapter 7 Fog Artificial Intelligence—Adding the Chapter 8 Competition Chapter 9 - Game Audio—Let’s Make Some Noise Chapter 10 - Game Physics—Keeping It Real Chapter 11 - Tools of the Trade Index List of Figures List of Tables List of Examples Back Cover Introduction to 3D Game Engine Design Using DirectX 9 and C# illustrates the process of creating a simple 3D game engine During this process, author Lynn Harrison demonstrates many facets of the DirectX 9 software through clear-cut explanations and examples Throughout the course of the book, you'll develop an off-road driving game that brings such features as management of large scenes, environmental effects, and physics into play To write the game, you'll use cutting-edge technologies—C# and DirectX, and the NET Framework—and you'll go beyond simple graphics to explore audio, user input, artificial intelligence, and multiplayer design About the Author Lynn Thomas Harrison is both a Microsoft Certified Systems Engineer (MCSE) and Microsoft Certified Solutions Developer (MCSD), and is currently employed as a senior systems engineer for Diamond Visionics Company (a visualization engineering company) Lynn has been active in the simulation and graphics industries for over 22 years Introduction to 3D Game Engine Design Using DirectX 9 and C# LYNN T HARRISON Apress™ Copyright © 2003 by Lynn T Harrison All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher ISBN (pbk): 1590590813 Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark Technical Reviewer: Patrick DeNardo Editorial Board: Dan Appleman, Craig Berry, Gary Cornell, Tony Davis, Steven Rycroft, Julian Skinner, Martin Streicher, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski Assistant Publisher: Grace Wong Project Manager: Tracy Brown Collins Copy Editor: Ami Knox Production Manager: Kari Brooks Compositor and Proofreader: Kinetic Publishing Services, LLC Indexer: Michael Brinkman Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co KG, Tiergartenstr 17, 69112 Heidelberg, Germany In the United States: phone 1–800-SPRINGER, email , or visit http://www.springer-ny.com Outside the United States: fax +49 6221 345229, email , or visit http://www.springer.de For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710 Phone 510–549–5930, fax 510–549–5939, email , or visit http://www.apress.com The information in this book is distributed on an “as is” basis, without warranty Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work The source code for this book is available to readers at http://www.apress.com in the Downloads section Dedication To my wife, Gerri, for her support and patience during the process of writing this book I couldn’t have done it without you And also to my son, Mike, who inherited my love of games of all types About the Author Lynn Thomas Harrison is both a Microsoft Certified Systems Engineer (MCSE) and Microsoft Certified Solutions Developer (MCSD) and is currently employed as a senior systems engineer for Diamond Visionics Company (a visualization engineering company) He lives in Binghamton, New York, with his wife, Gerri, and son, Michael Lynn has been active in the simulation and graphics industries for over 22 years About the Technical Reviewer Patrick Denardo is a project director and software technical lead at Universal Instruments, Inc., where he architects and develops clientserver internationalized software for the Windows platform He is a Microsoft Certified Professional and has a Bachelor of Science degree in computer science from Binghamton University Patrick enjoys reading computer books, playing golf, and spending time with his wife, Jennifer, and their dog, Abby Acknowledgments I would like to thank all of the great people at Apress for their guidance along the way I really had no idea when I started this book about everything that went into the process First, thanks to Gary Cornell and Dan Appleman for their enthusiasm in this project and for getting me started Also thanks to Sofia Marchant and Tracy Brown Collins for keeping everything organized and on track Big thanks to my wife, Gerri, and to my copy editor, Ami Knox, who helped translate my techie ramblings into English I would also like to thank my technical reviewer, Pat DeNardo Pat not only made sure that the information presented in this book was technically correct, but also that the information was understandable to those new to C# and Managed DirectX Lastly, I would like to thank my family for their support over the last 14 months Too many weekends were spent with my computer rather than with them Gerri and Mike, thanks for understanding my need to do this I would also like to thank my parents for always believing me and raising me to believe in myself Introduction I have been educating myself about game design and 3D visualization for many years My bookshelves are full of books on programming, game development, and the various graphical application programming interfaces (APIs) Many books have been written that explain every part of DirectX or OpenGL Quite a few books are also dedicated to creating portions of a game using one or both of these technologies The book that I could never find was one on game engine design I wanted an object-oriented, reusable package of software that was not tightly integrated to one particular game I knew that I wasn’t the only person interested in game engine design I decided that, as I learned C# and managed DirectX 9 and ported my game engine to this new technology, I would document the process The fact that you are reading this indicates you share my interest in 3D game engines I have kept this book at an introductory level When I initially began planning the book, I considered including more advanced topics such as animation, networking for multiplayer capability, and the programmable pipeline (shaders) I quickly came to the conclusion that to cover that much material in any depth at all was too much for a single book I am hoping that at some point in the future I will have the time to do a followup volume that includes these and other more advanced areas No one book can answer all questions I encourage you to extend your research to other books and the Internet as you hone your development capabilities The best resources for getting your questions answered are the Microsoft DirectX newsgroups There are a number of newsgroups of interest that are prefixed with microsoft.public.win32.programmer.directx The newsgroup dedicated to Managed DirectX is microsoft.public.win32.programmer.directx.managed I often monitor this newsgroup, and I will answer any questions that I can Chapter 1: Overview This chapter looks at several types of game engines as well as the distinction between a game and a game engine Game engine design requires more thought than do hard-coding rendering and game logic within the game itself The benefit is greater reuse of the underlying technology and a cleaner overall design Chapter 2: User Interface A game’s user interface provides the means of giving the player information and obtaining the player’s commands This chapter investigates the presentation of splash screens, option screens, and a console screen It also looks at the use of Directlnput to obtain player inputs from the keyboard, mouse, and joysticks Chapter 3: Hanging Ten: A Ride Through the Rendering Pipeline Before diving into the actual rendering of three-dimensional objects, it is good to have a basic understanding of the rendering pipeline This chapter walks you through the typical steps involved in the fixed-function rendering pipeline This includes the manipulation of cameras to provide the viewpoint in the game It also describes a base class that will be used for all rendered objects The process of culling objects and other techniques for improving performance is also investigated The actual illustration of these steps appears in Chapters 4 through 8 Chapter 4: Basic 3D Objects This chapter is the first of two chapters dealing with rendering the various types of objects that are used within a game These objects include a skybox for providing a view into the distance, terrain rendering to provide a surface to move upon, billboards for simple symmetrical objects, and particle systems Particle systems are an extremely powerful tool that may be used for dynamic systems of small objects These include flowing water, fire, fireworks, and blowing sand Chapter 5: Complex 3D Objects Chapter 5 is the second chapter dedicated to the rendering of objects for games The objects described in this chapter are much more complex, with many polygons per object These are referred to as mesh objects and are used as the primary moving models within a game The class used to encapsulate meshes includes the capability to adjust the complexity of the mesh as a function of range from the camera to improve performance Chapter 6: Camera: The Player’s View of the World To control the view of the action, we need something akin to the cameras used to film a movie This chapter illustrates the design and use of a camera class that may be employed in a variety of ways Cameras may be positioned at static locations within the scene and follow objects as they move past or they may be attached to the moving objects themselves Employing multiple cameras and including logic within a game can provide a cinematic flair to the game Chapter 7: Adding Some Atmosphere: Lighting and Fog All of the rendering performed by the game engine up until this point has been under fully lit and crystal-clear conditions—conditions that are often hard to find in the real world This chapter explores the four types of lights that may be used to illuminate a scene as well as how to configure fog to improve the realism of a game Fog may also be used to disguise shortcomings in the ability to render objects at a large distance from the camera Chapter 8: Artificial Intelligence: Adding the Competition Few games are much fun without opponents Although it is possible to write games that are strictly multiplayer, in which all of the opponents are human, networking is out of the scope of this book Instead, we will look at the different types of artificial intelligence techniques that can be used within a game One of these methods will be developed as part of the sample game engine for the book Chapter 9: Game Audio: Let’s Make Some Noise Another method to add character to a game is through the use of audio Background music, if chosen properly, adds mood to the play This chapter shows you how to develop classes for the game engine that facilitate playing songs It also includes support for sound effects within a game Players expect certain sounds to coincide with events within a game If a car hits another car or a tree, they expect to hear the crash as well as see a reaction between the objects involved in the collision Chapter 10: Game Physics: Keeping It Real As I mentioned when talking about audio, players not only expect to hear noise in a collision, but also to see a more physical reaction Chapter 10 concentrates on the physics involved primarily with cars The basic mathematics applied to cars may also be applied to many types of moving objects This chapter also covers the physics used for cloth dynamics This type of physics is used for not only cloth, but also for many types of flexible objects such as hair or rope Chapter 11: Tools of the Trade The final chapter of the book looks at a cross-section of tools that are used during game development If you are just starting out in game development (or just starting out with C#), you may find it useful to check out this chapter before diving into the rest of the book The first portion of this chapter concentrates on the development environment used to compile and test your software Other topics covered in the chapter include the manipulation of the artistic content for the game The artistic content includes the audio files, two-dimensional images, and threedimensional models Example 4.17b: Terrain Class Constructor Example 4.17c: Terrain Class Constructor Example 4.17d: Terrain Class Constructor Example 4.17e: Terrain Class Constructor Example 4.18: Terrain Class HeightOfTerrain and TerrainHeight Methods Example 4.19: Terrain Class HeightAboveTerrain Method Example 4.20: Terrain Class InLineOfSight Method Example 4.21: Terrain Class GetSlope Method Example 4.22: Terrain Class Render Method Example 4.23: Terrain Class Dispose Method Example 4.24: BillBoard Class Definition Example 4.25: BillBoard SetupMatrix Method Example 4.26: BillBoard Add Method Example 4.27: BillBoard Class Copy Constructor Example 4.28: BillBoard Class Constructor Example 4.29: BillBoard Copy Method Example 4.30: BillBoard Render All Method Example 4.31: BillBoard Render and RenderChildren Methods Example 4.32: BillBoard Render All Method Example 4.33: BillBoard Render All Method Example 4.34: Example ParticleUpdateMethod Example 4.35: Particle Structure Definition Example 4.36: ParticleGenerator Definition Example 4.37: ParticleGenerator Copy Constructor Example 4.38: ParticleGenerator Constructor Example 4.39: ParticleGenerator Copy Method Example 4.40: ParticleGenerator Update Method Example 4.41: ParticleGenerator Render Method Example 4.42: ParticleGenerator InRect Method Example 4.43: ParticleGenerator Dispose Method Chapter 5: Complex 3D Objects Example 5.1: Mesh Class FromFile Method Signatures Example 5.2: Model Class Declaration Example 5.3a: Model Class Constructor Example 5.3b: Model Class Constructor Example 5.3c: Model Class Constructor Example 5.3d: Model Class Constructor (Conclusion) Example 5.3e: Model Class Constructor Example 5.4: Model Class SetLOD Method Example 5.5: Model Class CreateLod Method Example 5.6: Model Class InRect Method Example 5.7: Model Class GetCorner Method Example 5.8a: Model Class Collide Method Example 5.8b: Model Class Collide Method Example 5.8c: Model Class Collide Method (Conclusion) Example 5.9: Model Class Render Method Example 5.10: Model Class Update Method Example 5.11: Model Class Dispose Method Example 5.12: Instantiating Models Example 5.13: Sample Update Method 1 Example 5.14: Sample Update Method 2 Chapter 6: Camera—The Player’s View of the World Example 6.1: Camera Culling Enumeration Example 6.2a: Camera Class Definition (Attributes) Example 6.2b: Camera Class Definition (Properties) Example 6.3: Camera Constructors Example 6.4: Camera AdjustHeading Method Example 6.5: Camera AdjustPitch Method Example 6.6: Camera MoveCamera Method Example 6.7: Camera Attach Method Example 6.8: Camera LookAt Method Example 6.9: Camera CheckFrustum Method 1 Example 6.10: Camera CheckFrustum Method 2 Example 6.11: Camera GetDistance Method Example 6.12a: Camera Render Method Example 6.12b: Camera Render Method Example 6.12c: Camera Render Method Example 6.12d: Camera Render Method (Conclusion) Example 6.13: Camera Reset Method Example 6.14: Camera AddVisibleObject Method Chapter 7: Adding Some Atmosphere—Lighting and Fog Example 7.1: GameLights Attributes Example 7.2: GameLights Properties Example 7.3: GameLights Constructor Example 7.4: GameLights CompareTo Method Example 7.5: GameLights GetLight Method Example 7.6: GameLights AddDirectionalLight Method Example 7.7: GameLights AddPointLight Method Example 7.8: GameLights AddSpotLight Method Example 7.9: GameLights InitializeLights Method Example 7.10: GameLights DeactivateLights Method Example 7.11: GameLights SetupLights Method Example 7.12: GameLights CheckCulling Method Example 7.13: GameLights Update Method Example 7.14: GameLights Example Example 7.15: Fog Properties Example 7.16: Using Fog Chapter 8: Artificial Intelligence—Adding the Competition Example 8.1: Operator Enumeration Example 8.2: Fact Attributes Example 8.3: Fact Properties Example 8.4: Fact Constructor Example 8.5: Fact Write Method Example 8.6: Logic Attributes Example 8.7: Logic Properties Example 8.8: Logic Default Constructor Example 8.9: Logic Constructor Example 8.10: Logic Evaluate Method Example 8.11: Logic Write Method Example 8.12: Logic Read Method Example 8.13: Expression Attributes Example 8.14: Expression Property Example 8.15: Expression Constructor Example 8.16: Expression Clear Method Example 8.17: Expression AddLogic Method Example 8.18: Expression Evaluate Method Example 8.19: Expression Write Method Example 8.20: Expression Read Method Example 8.21: Transitioner Attributes Example 8.22: Transitioner Constructor Example 8.23: Transitioner Evaluate Method Example 8.24: Transitioner Write Method Example 8.25: Transitioner Read Method Example 8.26: AIState Attributes Example 8.27: AIState Properties Example 8.28: AIState Constructor Example 8.29: AIState AddAction Method Example 8.30: AIState DoActions Method Example 8.31: AIState Think Method Example 8.32: AIState AddTransitioner Method Example 8.33: AIState WriteStateName Method Example 8.34: AIState WriteFullState Method Example 8.35: AIState Read Method Example 8.36: Thinker Delegates Example 8.37: Thinker Attributes Example 8.38: Thinker Properties Example 8.39: Thinker Constructor Example 8.40: Thinker AddAction Method Example 8.41: Thinker GetAction Method Example 8.42: Thinker GetActionName Method Example 8.43: Thinker Execute Method Example 8.44: Thinker Dispose Method Example 8.45: Thinker GetState Method Example 8.46: Thinker GetFact Method Example 8.47: Thinker SetFact Method Example 8.48: Thinker AddSensorMethod Method Example 8.49: Thinker AddState Method Example 8.50: Thinker Write Method Example 8.51: Thinker Read Method Example 8.52: Example XML Data File Example 8.53: DriverView Method Example 8.54: Action Methods Example 8.55: Opponent Class Chapter 9: Game Audio—Let’s Make Some Noise Example 9.1: Music Class Attributes and Properties Example 9.2: Music Constructor Example 9.3: Music Class ClipEnded Event Handler Example 9.4: Jukebox Attributes and Properties Example 9.5: Jukebox Constructor Example 9.6: Jukebox AddSong Method Example 9.7: Jukebox Play Method Example 9.8: Jukebox Stop Method Example 9.9: Jukebox Next Method Example 9.10: Jukebox ClipEnded Event Handler Example 9.11: Jukebox Dispose Method Example 9.12: Listener Class Attributes and Properties Example 9.13: Listener Class Constructor Example 9.14: Listener Update Method Example 9.15: Listener Dispose Method Example 9.16: SoundEffect Attributes Example 9.17: SoundEffect Properties Example 9.18: SoundEffect Constructor Example 9.19: SoundEffect LoadSoundFile Method Example 9.20: SoundEffect RestoreBuffer Method Example 9.21: SoundEffect PlaySound Method Example 9.22: SoundEffect StopSound Method Example 9.23: SoundEffect Update Method Example 9.24: SoundEffect Dispose Method Example 9.25: Ownship Class Constructor Excerpt Example 9.26: Ownship Class Update Method Excerpt Chapter 10: Game Physics—Keeping It Real Example 10.1: Vector Class Example 10.2: Euler Class Example 10.3: LFI Class Example 10.4: Wheel Class Attributes Example 10.5: Wheel Class Properties Example 10.6: Wheel Class Example 10.7a: Wheel Class Process Method Example 10.7b: Wheel Class Process Method Example 10.7c: Wheel Class Process Method Example 10.7d: Wheel Class Process Method Example 10.7e: Wheel Class Process Method (Conclusion) Example 10.8a: CarDynamics Enumerations and Attributes Example 10.8b: CarDynamics Enumerations and Attributes (Conclusion) Example 10.9: CarDynamics Properties Example 10.10: CarDynamics Constructor Example 10.11: CarDynamics Thread Process Method Example 10.12: CarDynamics Reset Method Example 10.13: CarDynamics IntegratePosition Method Example 10.14: CarDynamics CalcWeightTransfer Method Example 10.15: CarDynamics SetFriction Method Example 10.16: CarDynamics SetGearRatio Method Example 10.17: CarDynamics WheelNorth Method Example 10.18: CarDynamics WheelEast Method Example 10.19: CarDynamics SetWheelAltitude Method Example 10.20: CarDynamics SetWheelOffset Method Example 10.21: CarDynamics Dispose Method Example 10.22a: CarDynamics Process Method Example 10.22b: CarDynamics Process Method Example 10.22c: CarDynamics Process Method Example 10.22d: CarDynamics Process Method Example 10.22e: CarDynamics Process Method Example 10.23: CarDynamics SetAttitude Method Example 10.24: CarDynamics SetPosition and SetVelocity Methods Example 10.25: CarDynamics SetGroundHeight and SetAllGroundHeights Methods Example 10.26: CarDynamics WheelAngle Method Example 10.27: CarDynamics GetPitch and GetRoll Methods Example 10.28: CarDynamics IsTireSquealing Method Example 10.29: CarDynamics IsTireLoose Method Example 10.30: CarDynamics SetTireStiction Method Example 10.31: CarDynamics ProcessWheels Method Example 10.32: CarDynamics ProcessAttitude Method Example 10.33: CarDynamics MinorCollision Method Example 10.34: CarDynamics Major Collision Method Example 10.35: Cloth Class Node Structure Attributes Example 10.36: Cloth Class Node Constructor Example 10.37: Cloth Class NodeIndex Structure Example 10.38: Cloth Class Spring Structure Example 10.39: Cloth Class Attributes and Constants Example 10.40: Cloth Class Properties Example 10.41a: Cloth Class Constructor Example 10.41b: Cloth Class Constructor Example 10.41c: Cloth Class Constructor Example 10.41d: Cloth Class Constructor Example 10.41e: Cloth Class Constructor (Conclusion) Example 10.42: Cloth Class PopulateBuffer Method Example 10.43: Cloth Class PopulateIndexBuffer Method Example 10.44: Cloth Class Render Method Example 10.45: Cloth Class Update Method Example 10.46a: Cloth Class DoPhysics Method Example 10.46b: Cloth Class DoPhysics Method Example 10.46c: Cloth Class DoPhysics Method Example 10.46d: Cloth Class DoPhysics Method Example 10.47: Cloth Class Dispose Method ... List of Tables List of Examples Back Cover Introduction to 3D Game Engine Design Using DirectX 9 and C# illustrates the process of creating a simple 3D game engine During this process, author Lynn Harrison demonstrates many facets of the DirectX 9. .. game engine while demonstrating how to build an example game engine over the course of the book You may expand the example game engine to create the game of your choice I have chosen to concentrate on rendering using DirectX 9 If you prefer OpenGL, I leave it to you as a... I decided that, as I learned C# and managed DirectX 9 and ported my game engine to this new technology, I would document the process The fact that you are reading this indicates you share my interest in 3D game engines

Ngày đăng: 25/03/2019, 17:14

Mục lục

  • Table of Contents

  • BackCover

  • Introduction to 3D Game Engine Design Using DirectX 9 and C#

  • Introduction

  • Chapter 1: Overview

    • How This Book's Game Engine Project Differs

    • Starting at the Beginning-Defining a Few Primitives

    • Interfaces: The Integration Contract

    • Process Flow Overview

    • Developer Splash Screen

    • Game Splash Screen

    • Presenting the Options

    • Playing the Game

    • After Action Review: Player Scoring

    • Looking at the C# Code

    • Summary

    • Chapter 2: User Interface

      • Getting Keyboard Input

      • Getting Mouse Input

      • Getting Joystick Input

      • Unified Input Interface

      • Drawing Splash Screens

Tài liệu cùng người dùng

Tài liệu liên quan