©2004 by Thomson Course Technology PTR.All rights reserved.No SVP,Thomson Course Technology part ofthis book may be reproduced or transmitted in any form or by PTR:Andy Shafran any means,electronic or mechanical,including photocopying,record- Publisher: ing,or by any information storage or retrieval system without written Stacy L.Hiquet permission from Thomson Course Technology PTR, except for the inclusion ofbriefquotations in a review. Senior Marketing Manager: Sarah O’Donnell The Premier Press and Thomson Course Technology PTR logo and related trade dress are trademarks ofThomson Course Technology PTR Marketing Manager: and may not be used without written permission. Heather Hurley Maple 9.5 is a registered trademark ofMaplesoft.All other trademarks Manager ofEditorial Services: are the property oftheir respective owners. Heather Talbot Important:Thomson Course Technology PTR cannot provide software Acquisitions Editor: support.Please contact the appropriate software manufacturer’s techni- Mitzi Koontz cal support line or Web site for assistance. Senior Editor: Thomson Course Technology PTR and the author have attempted Mark Garvey throughout this book to distinguish proprietary trademarks from Associate Marketing Managers: descriptive terms by following the capitalization style used by the Kristin Eisenzopfand Sarah Dubois manufacturer. Project Editor: Information contained in this book has been obtained by Thomson Kate Shoup Welsh Course Technology PTR from sources believed to be reliable.However, because ofthe possibility ofhuman or mechanical error by our sources, Technical Reviewers: Thomson Course Technology PTR, or others, the Publisher does not Kelly Dempski and Dave Astle guarantee the accuracy,adequacy,or completeness of any information Thomson Course Technology PTR and is not responsible for any errors or omissions or the results obtained Market Coordinator: from use ofsuch information.Readers should be particularly aware of Amanda Weaver the fact that the Internet is an ever-changing entity.Some facts may have changed since this book went to press. Copy Editors: Kate Shoup Welsh and Sean Medlock Educational facilities,companies,and organizations interested in multi- ple copies or licensing of this book should contact the publisher for Interior Layout Tech: quantity discount information.Training manuals,CD-ROMs,and por- Susan Honeywell tions of this book are also available individually or can be tailored for specific needs. Cover Designer: Mike Tanamachi ISBN:1-59200-038-X Library ofCongress Catalog Card Number:2003105366 CD-ROM Producer: Printed in the United States ofAmerica Brandon Penticuff 04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1 Indexer: Sharon Shock Proofreader: Sean Medlock Thomson Course Technology PTR,a division of Thomson Course Technology 25 Thomson Place Boston,MA 02210 http://www.courseptr.com To the only things that truly matter: family and friends Acknowledgments So many people were involved in bringing this book to life, thanking each person individually would be a book in itself.Instead ofsubjecting you to that,I’ll break the deserving into groups. From the early-on mathematicians to the old-school demo programmer gurus who taught me the secrets ofthe PC,I thank you all for the invaluable technical knowledge. Special thanks to the University of Ottawa professors who could actually teach,a rarity in an educator (especially in the engineering field).Thanks to my personal stress relievers (that is,the Quake3knobs on XO server),which were like caffeine when the hours were late.Special thanks to everyone who shepherded this book through production;I think the end result speaks for itself.Finally,I would like to thank every sin- gle reader out there,without whom none ofthis would be possible.After you’ve mastered the material here,I call on you to create some kick-ass demo to make us all proud! iv About the Author CHRISTOPHER TREMBLAY lives in the California Bay Area, where he works for Motorola building a 3D graphics engine for cell phones to power next-generation games.He holds a degree in Software Engineering from the University ofOttawa,Canada,and is currently completing final courses for a mathematics degree. His work in the game industry includes game AI, core-networking, software rendering algorithms, 3D geometry algo- rithms,and optimization.Although most ofhis work is PC-based,a fair amount ofit was done on embedded devices ranging from bottom-line TI-calculators to Z80 and 68K Palm processors to speedy PocketPC strong-arm processors,with games such as LemmingZ. About the Series Editor ANDRÉLAMOTHE,CEO,Xtreme Games LLC,has been involved in the computing indus- try 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 Thomson Course Technol- ogy PTR Game Developmentseries. v Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Part I: The Basics 1 Chapter 1 Equation Manipulation and Representation. . . . . . . . . . . . . 3 Choosing a Coordinate System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Equation Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 Using Polynomial Roots to Solve Equations . . . . . . . . . . . . . . . . . . . .18 Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 Chapter 2 Baby Steps: Introduction to Vectors . . . . . . . . . . . . . . . . . . 29 O Vector, What Art Thou? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 Basic Operations and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 Advanced Operations and Properties . . . . . . . . . . . . . . . . . . . . . . . .36 Vector Spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Chapter 3 Meet the Matrices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Meat, the Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 Basic Operations and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 Advanced Operations and Properties . . . . . . . . . . . . . . . . . . . . . . . .64 Matrix Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89 vi Contents vii Diagonalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Vector Revision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 Chapter 4 Basic Geometric Elements. . . . . . . . . . . . . . . . . . . . . . . . . . 95 Creating Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Generating Planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Constructing Spheres . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 When Elements Collide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 Know Your Distances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 3D File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123 Chapter 5 Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 It’s All About the Viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Linear Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Multiple Linear Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144 Non-Linear Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151 Chapter 6 Moving to Hyperspace Vectors: Quaternions. . . . . . . . . . 153 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Basic Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Advanced Operations on Quaternions . . . . . . . . . . . . . . . . . . . . . . .161 Part II: Physics 167 Chapter 7 Accelerated Vector Calculus for the Uninitiated. . . . . . . . 169 The Concept of Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 Derivatives (The Result of Differentiation) . . . . . . . . . . . . . . . . . . .175 Reversing: Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 Chapter 8 Gravitating Around Basic Physics . . . . . . . . . . . . . . . . . . . 209 Move That Body... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210 Physical Force . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215 Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 Chapter 9 Advanced Physics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Angular Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236 viii Contents Oscillations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Center of Mass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257 Angular Forces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 Chapter 10 And Then It Hits You: You Need Collision Detection . . . . 263 Environmental Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264 Collisions of Objects Among Themselves . . . . . . . . . . . . . . . . . . . . .269 Picking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287 Part III: Playing with Numbers 293 Chapter 11 Educated Guessing with Statistics and Probability. . . . . . 295 Basic Statistics Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296 Random Number Generation (Uniform Deviates) . . . . . . . . . . . . . .302 Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310 Intelligence Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 Chapter 12 Closing the Gap for Numerical Approximation. . . . . . . . . 323 Solution Approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324 2D Function Approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327 Multivariable Function Approximation . . . . . . . . . . . . . . . . . . . . . .358 Chapter 13 Exploring Curvy Bodies. . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363 Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .385 Part IV: Rendering and Lighting 389 Chapter 14 Graphics-Generation Engines . . . . . . . . . . . . . . . . . . . . . . 391 Decals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .392 Billboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .402 Chapter 15 Visibility Determination: The World of the Invisible . . . . 405 GPU-Level Visibility Determination . . . . . . . . . . . . . . . . . . . . . . . . .406 CPU Culling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409 Grid Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427 Contents ix Chapter 16 Space Partitioning: Cleaning Your Room . . . . . . . . . . . . . 427 Quadtrees and Octrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .430 k-D Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .431 Binary Space Partitioned (BSP) Trees . . . . . . . . . . . . . . . . . . . . . . . .437 Portal Subdivision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .441 Chapter 17 Filling the Gaps with Rendering Techniques. . . . . . . . . . . 447 Studying the 3D Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .448 Texture Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450 Cube Map Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .463 Cel Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466 Shadows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468 Chapter 18 Building a Light Show. . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Studying the Rasterization Pipeline . . . . . . . . . . . . . . . . . . . . . . . . .476 Light a Ray Tracing Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .483 Light Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489 Lighting Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492 Part V: Optimizations 501 Chapter 19 The Quick Mind: Computational Optimizations . . . . . . . . 503 Fixed Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504 IEEE Floating Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .517 Condition Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524 Chapter 20 Using SIMD to Move Linear Algebra Functions onto the Highway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537 Matrix Storage Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .538 Matrix Transposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .538 Dot Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .540 Vector Length and Normalization . . . . . . . . . . . . . . . . . . . . . . . . . .542 Matrix-Vector Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544 Matrix-Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .547 Matrix Determinant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .551 Cross Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .555 Matrix Division (Inverse) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .556
Description: