Thứ Năm, 23 tháng 1, 2014

Beginning OpenGL Game Programming (2004)

© 2004 by Premier Press, a division of Course Technology. 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
Course PTR, except for the inclusion of brief quotations in a review.
The Premier Press logo and related trade dress are trademarks of Premier Press
and may not be used without written permission.
OpenGL is a registered trademark of SGI.
glFont © 2004 Brad Fish, bhf5@email.byu.edu.
GLee © 2004 Ben Woodhouse, ben@elf-stone.com, with parts copyright by SGI.
All rights reserved.
All other trademarks are the property of their respective owners.
Important: Course PTR cannot provide software support. Please contact the
appropriate software manufacturer’s technical support line or Web site for
assistance.
Course PTR and the authors have attempted throughout this book to distin-
guish proprietary trademarks from descriptive terms by following the capital-
ization style used by the manufacturer.
Information contained in this book has been obtained by Course PTR from
sources believed to be reliable. However, because of the possibility of human or
mechanical error by our sources, Course 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 dis-
count information. Training manuals, CD-ROMs, and portions of this book are
also available individually or can be tailored for specific needs.
ISBN: 1-59200-369-9
Library of Congress Catalog Card Number: 2004090734
Printed in the United States of America
04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1
Course PTR, a division of Course Technology
25 Thomson Place
Boston, MA 02210
http://www.courseptr.com
Senior Vice President,
Course PTR Group:
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
Associate Marketing Manager:
Kristin Eisenzopf
Project Editor:
Sandy Doell
Technical Reviewer:
Ben Woodhouse
Retail Market Coordinator:
Sarah Dubois
Interior Layout Tech:
Marian Hartsough
Cover Designer:
Steve Deschene
CD-ROM Producer:
Brandon Penticuff
Indexer:
Katherine Stimson
Proofreader:
Gene Redding
00 BOGL_GP FM 3/1/04 2:14 PM Page iv
TLFeBOOK
For my family and friends
—Kevin
For my crash of rhinos
—Dave
00 BOGL_GP FM 3/1/04 2:14 PM Page v
TLFeBOOK
F
irst and foremost, I want to thank my wife Melissa and my kids, Rebi, Evan, Ellie,
Tyler, and Nate, for all of your support throughout this project, and for dragging
me away from the computer just often enough for me to retain most of my sanity.
I love you all.
I’d also like to thank Kevin, my partner and collaborator, without whom I never would
have done this. I can’t imagine finding a better teammate.
Big thanks to everyone at Premier Press/Course Technology. You’re a great group of peo-
ple to work with, and I genuinely appreciate the confidence you place in me.
Ben Woodhouse deserves special mention for his efforts as technical editor. He provided
valuable feedback that helped make this book much better than it would have been oth-
erwise. Thanks also to The Mighty Pete for allowing us to use his skybox images in many
of the example programs, and to Jeff Royle from ATI Technologies for providing us with
graphics hardware for testing purposes.
Finally, I want to thank everyone who has taught me in some way, including Chuck
Hansen, Robert Kessler and my other professors at the University of Utah, my coworkers
at Avalanche Software and Qualcomm, the denizens of the GameDev.net forums, and
everyone else who has taken the time to share their knowledge and experience via a Web
site or book.
— Dave Astle
vi
Acknowledgments
00 BOGL_GP FM 3/1/04 2:14 PM Page vi
TLFeBOOK
I’d like to thank Dave, for his work as a good teammate, motivator, and friend. Chances
are you would not be holding this book in your hands if he had not used a little friendly
coercion on me. I’m amazed at what we were able to accomplish with this project, and a
good deal of its success is due to our ability to work together as a team. I also want to
thank my family for their constant support for me in everything I do. Oftentimes they
don’t get as much credit as they should be getting.
My friends and coworkers also deserve a share of the thanks. Whether they know it or not,
I’ve learned from all of them in some form or another and value their friendships: Tucker,
Tom, Christie, Mike, Rael, Kristin, Vivian, JP, Andy, Greg R., Greg S., Bill, Kyle, Randall,
Jordan, Hack, Justin, Nate, Luke M., Mike M., Johnny Y., Nick M., and so many others that
we don’t have the space for here. Also, thank you to the Premier Press group for the oppor-
tunity to do this project and for maintaining a high degree of support and confidence in
both Dave and me, and in GameDev.net.
And finally, I want to thank everyone who has provided me with the ability and talent,
directly or indirectly, that has allowed me to create this book, including the professors at
Embry-Riddle, my baseball coaches and teammates, Chris Hargrove, Seth Robinson, Jeff
Molofee, Rich Benson, and a host of software engineering colleagues.
— Kevin Hawkins
Acknowledgments vii
00 BOGL_GP FM 3/1/04 2:14 PM Page vii
TLFeBOOK
D
AV E
A
STLE
has been programming games professionally for several years, working on
titles for the Xbox, PlayStation 2, GameCube, PC, and various wireless devices. Currently,
he is a lead engineer in the Gaming and Graphics group at Qualcomm, Inc. He is the
cofounder and executive producer of GameDev.net, the leading online community for
game developers. He has authored or contributed to several game development books and
has spoken at industry conferences, including the Game Developers’ Conference. He
received his bachelor’s degree in Computer Science from the University of Utah, where he
specialized in graphics, artificial intelligence, networking, software engineering, and com-
piler theory and design.
K
EVIN
H
AWKINS
is a lead software engineer at Raydon Corporation where he designs and
develops training simulations for a variety of customers, including the U.S. military. In
addition, Kevin is the cofounder and CEO of GameDev.net, the leading online commu-
nity for game developers. He holds a master’s degree in Software Engineering and a bach-
elor’s degree in Computer Science from Embry-Riddle University, where he also played
intercollegiate baseball and was drafted by the Cleveland Indians in the 2002 amateur
baseball draft.
viii
About the Authors
00 BOGL_GP FM 3/1/04 2:14 PM Page viii
TLFeBOOK
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii
Part I OpenGL Basics . . . . . . . . . . . . . . . . . . . . . . . .1
Chapter 1 The Exploration Begins . . . Again . . . . . . . . . . . . . . . . . . . . . . . . . .3
Chapter 2 Creating a Simple OpenGL Application . . . . . . . . . . . . . . . . . . . . .13
Chapter 3 OpenGL States and Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Chapter 4 Transformations and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . .67
Chapter 5 Colors, Lighting, Blending, and Fog . . . . . . . . . . . . . . . . . . . . . . .99
Chapter 6 Bitmaps and Images with OpenGL . . . . . . . . . . . . . . . . . . . . . . .133
Chapter 7 Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149
Part II Beyond the Basics . . . . . . . . . . . . . . . . . . .183
Chapter 8 OpenGL Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185
Chapter 9 More on Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197
Chapter 10 Up Your Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221
Chapter 11 Displaying Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249
Chapter 12 OpenGL Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
Chapter 13 The Endgame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277
Part III Appendices . . . . . . . . . . . . . . . . . . . . . . . . .283
Appendix A Answers to Review Questions and Exercises . . . . . . . . . . . . . . . .285
Appendix B Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295
Appendix C What’s on the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301
ix
Contents at a Glance
00 BOGL_GP FM 3/1/04 2:14 PM Page ix
TLFeBOOK
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii
Part I OpenGL Basics . . . . . . . . . . . . . . . . . . . . .1
Chapter 1 The Exploration Begins . . . Again . . . . . . . . . . . . . . . . . . . . .3
Why Make Games? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3
The World of 3D Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
The Elements of a Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
What Is OpenGL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
OpenGL History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
OpenGL Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7
Related Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
A Sneak Peek . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12
Chapter 2 Creating a Simple OpenGL Application . . . . . . . . . . . . . . . .13
Introduction to WGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
The Rendering Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Pixel Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
dwFlags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
iPixelType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18
cColorBits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
Setting the Pixel Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19
x
Contents
00 BOGL_GP FM 3/1/04 2:14 PM Page x
TLFeBOOK
An OpenGL Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
Full-Screen OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33
Chapter 3 OpenGL States and Primitives . . . . . . . . . . . . . . . . . . . . . . .35
State Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Querying Numeric States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Enabling and Disabling States . . . . . . . . . . . . . . . . . . . . . . . . . . .36
glIsEnabled() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Querying String Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Finding Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Giving OpenGL a Hint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
Handling Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
Drawing Points in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42
Drawing Lines in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Drawing Polygons in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .52
Using Primitives: Triangles and Quads Example . . . . . . . . . . . . . .60
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
Chapter 4 Transformations and Matrices . . . . . . . . . . . . . . . . . . . . . . .67
Understanding Coordinate Transformations . . . . . . . . . . . . . . . . . . . .67
Eye Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Viewing Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
Modeling Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
Projection Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
Viewport Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
OpenGL and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
The Modelview Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74
Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75
Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80
Matrix Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
The Robot Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .84
Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
Orthographic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87
Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Setting the Viewport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
Projection Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90
Contents xi
00 BOGL_GP FM 3/1/04 2:14 PM Page xi
TLFeBOOK
Manipulating the Viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91
Using gluLookAt() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
Using glRotate() and glTranslate() . . . . . . . . . . . . . . . . . . . . . . . .93
Creating Your Own Custom Routines . . . . . . . . . . . . . . . . . . . . . .94
Using Your Own Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
Loading Your Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
Multiplying Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
Transpose Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97
Chapter 5 Colors, Lighting, Blending, and Fog . . . . . . . . . . . . . . . . . .99
Using Colors in OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99
Setting the Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100
Secondary Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102
A Colorful Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104
Lighting in OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .104
OpenGL Lighting and the Real World . . . . . . . . . . . . . . . . . . . .104
Light Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106
Spotlights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109
Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .111
Normals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115
The Lighting Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118
Lighting in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120
Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
Separate Blend Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
The Blend Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
Constant Blend Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
Disk Blender . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
Fog 128
OpenGL Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128
Fog Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130
Fog in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131
Chapter 6 Bitmaps and Images with OpenGL . . . . . . . . . . . . . . . . . .133
The OpenGL Bitmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133
Positioning the Bitmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134
Drawing the Bitmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
An OpenGL Bitmap Example . . . . . . . . . . . . . . . . . . . . . . . . . . .136
Contentsxii
00 BOGL_GP FM 3/1/04 2:14 PM Page xii
TLFeBOOK
Using Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Drawing Image Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139
Reading from the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141
Copying Screen Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141
Magnification, Reduction, and Flipping . . . . . . . . . . . . . . . . . . .142
Managing Pixel Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Targa Image Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143
The Targa File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143
Loading Targa Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147
Chapter 7 Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .149
An Overview of Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . .150
Texture Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151
Using the Texture Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
Texture Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
Specifying Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155
Texture Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161
Basic Texture Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163
Mipmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166
Mipmaps and the OpenGL Utility Library . . . . . . . . . . . . . . . . . .167
Automatic Mipmap Generation . . . . . . . . . . . . . . . . . . . . . . . . .168
Texture Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169
Texture Wrap Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
Texture Level of Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173
Texture Environments and Texture Functions . . . . . . . . . . . . . . . . . .174
Specifying the Texture Environment . . . . . . . . . . . . . . . . . . . . . .175
Textured Terrain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178
Building the Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181
Part II Beyond the Basics . . . . . . . . . . . . . . . .183
Chapter 8 OpenGL Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185
Anatomy of an Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185
Extension Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186
Name Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .187
Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188
Contents xiii
00 BOGL_GP FM 3/1/04 2:14 PM Page xiii
TLFeBOOK

Không có nhận xét nào:

Đăng nhận xét