Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu đang bị lỗi
File tài liệu này hiện đang bị hỏng, chúng tôi đang cố gắng khắc phục.
3D game engine programming
Nội dung xem thử
Mô tả chi tiết
TEAM LinG - Live, Informative, Non-cost and Genuine!
TEAM LinG - Live, Informative, Non-cost and Genuine!
This page intentionally left blank
TEAM LinG - Live, Informative, Non-cost and Genuine!
TEAM LinG - Live, Informative, Non-cost and Genuine!
SVP, Thomson Course Technology
PTR:
Andy Shafran
Publisher:
Stacy L. Hiquet
Senior Marketing Manager:
Sarah O’Donnell
Marketing Manager:
Heather Hurley
Manager of Editorial Services:
Heather Talbot
Senior Acquisitions Editor:
Emi Smith
Senior Editor:
Mark Garvey
Associate Marketing Managers:
Kristin Eisenzopf and Sarah Dubois
Developmental Editor:
Ginny Bess
Project Editor:
Kevin Sullivan, Argosy Publishing
Technical Reviewer:
Richard Fine
Thomson Course Technology PTR
Market Coordinator:
Amanda Weaver
Interior Layout Tech:
Sharon Burkhardt, Argosy Publishing
Cover Designer:
Steve Deschene
CD-ROM Producer:
Brandon Penticuff
Indexer:
Maureen Shepherd
Proofreader:
Sherri Dietrich
© 2004 by Thomson Course Technology PTR. All rights reserved. No part of this
book 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 written permission from Thomson Course Technology PTR, except for the inclusion of brief quotations in a review.
The Premier Press and Thomson Course Technology PTR logo and related trade
dress are trademarks of Thomson Course Technology PTR and may not be used
without written permission.
Microsoft DirectX 9 SDK is Copyright Microsoft Corporation, 2002. All rights
reserved. Images from DOOM and QUAKE © 1994, 1996 id Software, Inc. All
rights reserved. Images from Return to Castle Wolfenstein © 2002 id Software, Inc.
All rights reserved. Special thanks to id Software for their support and help with
this project.
All other trademarks are the property of their respective owners.
Important: Thomson Course Technology PTR cannot provide software support.
Please contact the appropriate software manufacturer’s technical support line or
Web site for assistance.
Thomson Course Technology PTR and the author have attempted throughout this
book to distinguish proprietary trademarks from descriptive terms by following the
capitalization style used by the manufacturer.
Information contained in this book has been obtained by Thomson Course
Technology PTR from sources believed to be reliable. However, because of the
possibility of human or mechanical error by our sources, Thomson Course
Technology PTR, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors
or omissions or the results obtained from use of such information. Readers
should be particularly aware of the fact that the Internet is an ever-changing
entity. Some facts may have changed since this book went to press.
Educational facilities, companies, and organizations interested in multiple copies or
licensing of this book should contact the publisher for quantity discount information. Training manuals, CD-ROMs, and portions of this book are also available individually or can be tailored for specific needs.
ISBN: 1-59200-351-6
Library of Congress Catalog Card Number: 2004090737
Printed in the United States of America
04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1
Thomson Course Technology PTR, a division of
Thomson Course Technology
25 Thomson Place
Boston, MA 02210
http://www.courseptr.com
TEAM LinG - Live, Informative, Non-cost and Genuine!
To my beloved wife Rike and our son Tim
TEAM LinG - Live, Informative, Non-cost and Genuine!
In a world designed by the men in gray who decide how we live in brief, there’s a
master plan for the company men from the cradle to the company grave.
“Return of Alex,” Die Toten Hosen
Writing about the things I like is pretty much one of my favorite hobbies. After
all, it is a good way to pull even more people over to the dark side. This book
is about the dark side of programming 3D engines for video game applications, and I hope to pull as many of you into the fun side of programming computer
graphics stuff as I can.
It took some time to write the original version of this book as well as complete the English
translation for it. A lot of things happened during that time, but the most important event
was my little son Tim’s arrival into this world. Even if doing 3D graphics is a lot of fun,
Tim taught me that a baby’s smile is way more beautiful than the most perfect 3D scene
with per-pixel lighting, real-time shadows, and glow effects.
Besides my baby Tim, a lot of other people took their share in bringing this book about,
and I would like to thank all of those people. I first want to mention Oli (Oliver Düvel),
who wrote the chapter on character animation and a large portion of the network device
chapter. For the original version of this book, Marcus Beck and Boris Karnikowski looked
after me and helped me get the book done. Working with them was a pleasure, as they
were friendly and motivated me. The same applies for Emi Smith, who organized the
English version of the book and who tolerated my tardiness on due dates with a smile on
her face and in her friendly e-mails. Thanks, Emi. And thanks to André LaMothe for
establishing the contact. vi
Acknowledgments
TEAM LinG - Live, Informative, Non-cost and Genuine!
Marco Kögler was kind enough to help me out with tips and tricks and listened carefully
when I described scene management and lighting ideas to him. He pointed out weaknesses in the approaches and helped me with per-pixel lighting topics. Finally, I would like
to thank the band Toten Hosen for the great music that kept me going and Jacobs for the
instant coffee that kept me running.
Last, but not least, thanks to Rike, who makes all this possible.
Stefan Zerbst
Diligence is neither an ability nor a talent. It is a matter of habit.
Peter F. Drucker
These days, it is important to create video games that fascinate the player. To create
this fascination, a video game needs very basic elements, such as network modes
and character animation. The goal of these elements is to increase the level of
interactivity the game is design for.
Personally, I am always astonished by the development of the character animation techniques employed in video games. Starting with plain, animated image files called sprites
in the 80s and ending with the highly precise animations of characters with facial expressions moving through virtual game worlds, animations techniques are developing at
amazing speeds.
I’ve had a lot of fun contributing to this book, and I hope you will have as much fun reading it.
Oliver Düvel
Acknowledgments vii
TEAM LinG - Live, Informative, Non-cost and Genuine!
Stefan Zerbst started programming on the ancient C16 and now holds a German Diplom
degree in Computer Science in Economics. During his studies he founded the German
hobby game development community ZFX (www.zfx.info), which is the biggest and most
active development community in Germany. He has already published two best-selling
books about game programming and also shares his comprehensive experience in this
field by holding lectures about game programming at a German university and the online
academy Gameversity (ww.gameversity.com).
Oliver Düvel works as a project manager in the field of support for a medium-sized software company in Germany. He started programming in the early 1980s and did his first
vector and matrix programs using assembler to visualize three-dimensional objects.
About the Authors
viii
TEAM LinG - Live, Informative, Non-cost and Genuine!
André LaMothe, CEO, Xtreme Games LLC, has been involved in the computer industry
for more than 25 years. He wrote his first game for the TRS-80 and has been hooked ever
since! His experience includes 2D/3D graphics, AI research at NASA, compiler design,
robotics, virtual reality, and telecommunications. His books are top sellers in the game
programming genre, and his experience is echoed in the Premier Press Game Development
books.
ix
About the Series Editor
TEAM LinG - Live, Informative, Non-cost and Genuine!
x
Letter from the Series Editor
Today there are many 3D graphics and game programming books to choose from.
However, the development of a 3D game engine is usually left as an exercise for the
reader because the majority of 3D engine work involves the implementation of the
3D rendering pipeline itself. Unfortunately, many readers still have problems putting
theory into practice and creating a professional quality 3D game engine that can be
used to develop actual production-ready games. Fortunately for us, Stefan Zerbst has
stepped up to the plate and taken the opposite approach for 3D Game Engine Programming. Instead of assuming you know nothing about math or 3D graphics, he
assumes that you are familiar with these areas and comfortable with C++ and the
DirectX/3D API. Given this assumption, for the first time really, an entire book is
dedicated to creating the framework for and implementing an entire 3D game
engine, aptly named the “ZFXEngine.” (I will let you learn what it stands for.)
With that in mind, this book targets the intermediate or advanced 2D/3D game programmer who wants to see in detail the construction of an entire 3D engine ready to
develop commercial games and not just demos. The ZFXEngine is a fully featured
3D engine supporting advanced 3D graphics, shading, model loading, spatial partitioning, networking, audio, scene management, and more. If you can read and
understand everything in this book, not only will you be in a superior position to
develop your own 3D game engine, but you will also have the background to take the
lead position at a studio, defining and developing an engine for them.
And as if that wasn’t enough, the book ends with the implementation of a deathmatch shooter to show you just a sample of what the ZFXEngine and the concepts
herein can do. In conclusion, this book will round out even the most wicked 3D
graphics programmer, or give the newbie 3D graphics programmer a roadmap to
where he is going. I highly recommend it!
Sincerely,
André LaMothe
Series Editor, Premier Game Development series
ceo@nurve.net
TEAM LinG - Live, Informative, Non-cost and Genuine!
xi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv
Part I Introduction to the Topics 1
Chapter 1
3D Engines and Game Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Chapter 2
Designing the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Chapter 3
Engineering the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Chapter 4
Fast 3D Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91
Part II Rendering Graphics 187
Chapter 5
Materials, Textures, and Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .189
Chapter 6
The Render Device of the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235
Chapter 7
3D Pipelines and Shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335
Contents at a Glance
TEAM LinG - Live, Informative, Non-cost and Genuine!
xii Contents at a Glance
Chapter 8
Loading and Animating 3D Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .387
Part III Support Modules for the Engine 445
Chapter 9
The Input Interface of the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .447
Chapter 10
The Audio Interface of the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .483
Chapter 11
The Network Interface of the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .503
Chapter 12
Timing and Movement in the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .569
Chapter 13
Scene Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .585
Part IV Black Art of Game Programming 665
Chapter 14
Computer-Aided Design (CAD) Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .667
Chapter 15
Deathmatch Shooter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .787
Epilogue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .843
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .845
TEAM LinG - Live, Informative, Non-cost and Genuine!
xiii
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv
Part I Introduction to the Topics 1
Chapter 1 3D Engines and Game Programming . . . . . . . . . . . . . . . . . .3
Buzzword Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
Engines versus APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
What Exactly Is an Engine? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5
A Kind of Magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
Nostalgia and Modern Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
The Problems of Modern Times . . . . . . . . . . . . . . . . . . . . . . . . . . . .8
Wizardry and Art: Creating the Magic . . . . . . . . . . . . . . . . . . . . . .9
The Development Process of a Video Game . . . . . . . . . . . . . . . . . . .10
Professional Perspectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Game Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Lead Programmer / Technical Director . . . . . . . . . . . . . . . . . . . . . .14
Game Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Game Proposal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Publisher, Publishing, and Revenues . . . . . . . . . . . . . . . . . . . . . . .23
Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Direct3D versus OpenGL Wars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26
Flame Wars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
Drivers and Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
TEAM LinG - Live, Informative, Non-cost and Genuine!
xiv Contents
What Are We Going to Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
Why Do It the Hard Way If There Is an Easy Way? . . . . . . . . . . . . . .28
D3DX Helper Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28
Knowing Instead of Copying . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Get Ready to Rock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Chapter 2 Designing the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Requirements for the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Naming the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Is the Game Code Autonomous? . . . . . . . . . . . . . . . . . . . . . . . . . .32
Other Video Game Engine Requirements . . . . . . . . . . . . . . . . . . .33
API Independence through Interface Definitions . . . . . . . . . . . . . . .34
The Structure of the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Interfaces and Dynamic Link Libraries . . . . . . . . . . . . . . . . . . . . . .36
Static Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Components of the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
ZFXRenderDevice Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
ZFXInputDevice Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
ZFXAudioDevice Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
ZFXNetworkDevice Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
ZFX3D Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
ZFXGeneral Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
One Look Back, Two Steps Forward . . . . . . . . . . . . . . . . . . . . . . . . . .39
Chapter 3 Engineering the Engine . . . . . . . . . . . . . . . . . . . . . . . . . . .41
What Is an Interface? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42
Virtual Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Defining the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
About DLLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
More About the Renderer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Workspace for the Implementations . . . . . . . . . . . . . . . . . . . . . . . . .45
Loading DLLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Exporting Classes from DLLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
ZFXRenderer: A Static Library As Manager . . . . . . . . . . . . . . . . . .47
ZFXD3D: A DLL As Render Device . . . . . . . . . . . . . . . . . . . . . . . . .48
ZFXRenderDevice: An Abstract Class As Interface . . . . . . . . . . . . .50
Implementing the Static Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
Loading DLLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
TEAM LinG - Live, Informative, Non-cost and Genuine!