ebook img

Tricks of the 3D Game Programming Gurus. Advanced 3D Graphics and Rasterization PDF

1691 Pages·2003·47.591 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Tricks of the 3D Game Programming Gurus. Advanced 3D Graphics and Rasterization

00 0672318350 fm 05•02•2003 2:31 PM Page i Tricks of the 3D Game Programming Gurus Advanced 3D Graphics and Rasterization André LaMothe 201 West 103rd Street, Indianapolis, Indiana 46290 00 0672318350 fm 05•02•2003 2:31 PM Page ii Tricks of the 3D Game Programming Gurus: Associate Publisher Advanced 3D Graphics and Rasterization Michael Stephens Copyright © 2003 by Sams Publishing Acquisitions Editor All rights reserved. No part of this book shall be reproduced, stored in Kim Spilker a retrieval system, or transmitted by any means, electronic, mechani- Development Editor cal, photocopying, recording, or otherwise, without written permission Mark Renfrow from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution Managing Editor has been taken in the preparation of this book, the publisher and Charlotte Clapp author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the informa- Project Editor tion contained herein. George E. Nedeff International Standard Book Number: 0-672-31835-0 Copy Editors Library of Congress Catalog Card Number: 99-067392 Seth Kerney Printed in the United States of America Mike Henry First Printing: June 2003 Indexer 06 05 04 03 4 3 2 1 Erika Millen Sams Publishing offers excellent discounts on this book when ordered Proofreaders in quantity for bulk purchases or special sales. For more information, Kevin Ober please contact Suzanne Thomas U.S. Corporate and Government Sales Technical Editor 1-800-382-3419 David Franson [email protected] Team Coordinator For sales outside of the U.S., please contact Cindy Teeters International Sales 1-317-581-3793 Multimedia Developer Dan Scherf [email protected] Graphic Designer Trademarks Gary Adair All terms mentioned in this book that are known to be trademarks or Cover Illustrator service marks have been appropriately capitalized. Sams Publishing Alex Varanese cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The infor- mation provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the CD or programs accom- panying it. 00 0672318350 fm 05•02•2003 2:31 PM Page iii Contents at a Glance Foreword.....................................................................................xix Introduction...................................................................................1 Part I Introduction to 3D Game Programming 1 Getting Started with 3D Game Programming........................................9 2 A Crash Course in Windows and DirectX...........................................69 3 3D Game Programming with a Virtual Computer..............................115 Part II 3D Math and Transformation 4 It’s a Math, Math, Math World—Trigonometry, Vectors, Matrices, and Quaternions................................................................................241 5 Building a Math Engine.................................................................363 6 Introduction to 3D Graphics..........................................................493 7 Rendering Wireframe Worlds in 3D.................................................621 Part III Basic 3D Rendering 8 Basic Lighting and Solid Modeling..................................................743 9 Interpolative Shading Techniques and Affine Texture Mapping............869 10 Clipping in 3D++........................................................................1005 11 Depth Buffering and Visibility ......................................................1087 Part IV Advanced 3D Rendering 12 Advanced Texturing Techniques....................................................1147 13 Spatial Partitioning and Visible Algorithms.....................................1307 14 Lighting, Shadows, and Secrets of the id.........................................1439 Part V Advanced Animation and Physics Modeling and Optimization 15 Character Animation, Motion, and Collision Detection.....................1493 16 Optimization Technologies...........................................................1549 00 0672318350 fm 05•02•2003 2:31 PM Page iv Part VI CD-ROM Appendixes A About The CD-ROM B Installing DirectX and Using Visual C/C++ C Trigonometry and Vector Reference D C++ Primer E Game Programming Resources F ASCII Tables Index.........................................................................................000 00 0672318350 fm 05•02•2003 2:31 PM Page v Table of Contents Introduction 1 Part I Introduction to 3D Game Programming 1 Getting Started with 3D Game Programming 9 A Brief Introduction.........................................................................9 The Elements of a 2D/3D Game........................................................11 Section 1: Initialization...........................................................12 Section 2: Enter Game Loop.....................................................12 Section 3: Retrieve Player Input................................................12 Section 4: Perform AI and Game Logic.......................................12 Section 5: Render Next Frame...................................................13 Section 6: Synchronize Display.................................................13 Section 7: Loop......................................................................13 Section 8: Shutdown...............................................................13 General Game Programming Guidelines............................................17 Using Tools...................................................................................22 3D Level Editors.....................................................................26 Setting Up to Get Down—Using The Compiler............................28 A Sample 3D Game: Raiders 3D.......................................................32 The Event Loop......................................................................61 The Core 3D Game Logic.........................................................61 3D Projections.......................................................................63 The Star Field........................................................................66 The Laser Cannons and Collision Detection................................66 The Explosions......................................................................67 Playing Raiders3D..................................................................67 Summary......................................................................................67 2 A Crash Course in Windows and DirectX 69 The Win32 Programming Model.......................................................69 The Bare Minimum for a Windows Program.......................................71 It All Begins with WinMain().....................................................71 A Basic Windows Application...........................................................77 The Windows Class................................................................78 Registering the Windows Class.................................................84 Creating the Window..............................................................85 00 0672318350 fm 05•02•2003 2:31 PM Page vi vi Tricks of the 3D Game Programming Gurus: Advanced 3D Graphics and Rasterization The Event Handler.................................................................87 The Main Event Loop..............................................................94 Making a Real-Time Event Loop................................................99 DirectX and COM Crash Course.....................................................101 The HEL and HAL.................................................................103 The DirectX Foundation Classes in Depth.................................104 Flash Introduction to COM............................................................106 What Exactly Is a COM Object?..............................................107 Creating and Using DirectX COM Interfaces.............................110 Querying for Interfaces..........................................................111 Summary....................................................................................114 3 3D Game Programming with a Virtual Computer 115 Introduction to the Virtual Computer Interface.................................115 Building the Virtual Computer Interface...........................................118 The Frame Buffer and Video System.........................................118 Working with Color..............................................................123 Animating the Display...........................................................125 The Complete Virtual Graphics System.....................................128 I/O, Sound, and Music...........................................................129 The T3DLIBGame Console.............................................................130 T3DLIBSystem Overview........................................................131 The Basic Game Console........................................................131 The T3DLIB1Library......................................................................139 The DirectX Graphics Engine Architecture................................139 Basic Definitions ..................................................................140 Working Macros...................................................................142 Data Types and Structures......................................................143 Function Prototypes..............................................................147 Global Domination...............................................................153 The DirectDraw Interface.......................................................155 2D Polygon Functions...........................................................160 Math and Error Functions......................................................169 Bitmap Functions.................................................................171 8-Bit Palette Functions...........................................................177 Utility Functions..................................................................181 The BOB (Blitter Object) Engine..............................................183 The T3DLIB2DirectX Input System..................................................193 The T3DLIB3Sound and Music Library.............................................201 The Header..........................................................................201 The Types............................................................................202 00 0672318350 fm 05•02•2003 2:31 PM Page vii Contents vii Global Domination...............................................................203 The DirectSound API Wrapper................................................204 The DirectMusic API Rapper...........................................................210 Building the Final T3D Game Console.............................................214 Mapping the Real Graphics to the Unreal Graphics of the Virtual Interface.................................................................214 The Final T3DLIBGame Console..............................................218 Sample T3LIBApplications.............................................................231 Windowed Applications.........................................................231 Full-Screen Applications.........................................................232 Sound and Music..................................................................233 Working with Input..............................................................234 Summary....................................................................................238 Part II 3D Math and Transformation 4 It’s a Math, Math, Math World—Trigonometry, Vectors, Matrices, and Quaternions 241 Mathematical Notation.................................................................242 2D Coordinate Systems.................................................................243 2D Cartesian Coordinates......................................................243 2D Polar Coordinates............................................................246 3D Coordinate Systems.................................................................249 3D Cartesian Coordinates......................................................249 3D Cylindrical Coordinates............................................................252 Converting Between 3D Cartesian and Polar Coordinates............253 3D Spherical Coordinates...............................................................253 Trigonometry...............................................................................256 The Right Triangle................................................................256 Inverse Trigonometric Functions.............................................259 Trigonometric Identities.........................................................259 Vectors.......................................................................................261 Vector Length......................................................................262 Normalization......................................................................263 Scalar Multiplication.............................................................263 Vector Addition....................................................................265 Vector Subtraction................................................................265 The Inner Product, or the “Dot” Product..................................266 The Cross Product.................................................................270 The Zero Vector....................................................................272 00 0672318350 fm 05•02•2003 2:31 PM Page viii viii Tricks of the 3D Game Programming Gurus: Advanced 3D Graphics and Rasterization Position and Displacement Vectors..........................................272 Vectors as Linear Combinations..............................................273 Matrices and Linear Algebra...........................................................273 The Identity Matrix...............................................................275 Matrix Addition...................................................................276 Transpose of a Matrix............................................................276 Matrix Multiplication............................................................277 Laws for Matrix Operations....................................................279 Computing the Inverse and Solving Systems.....................................279 Cramer’s Rule.......................................................................282 Transformations Using Matrices..............................................285 Homogeneous Coordinates ....................................................286 Applied Matrix Transformations..............................................287 Basic Geometric Entities................................................................296 Points.................................................................................297 Lines..................................................................................297 Lines in 3D Space.........................................................................300 Planes.................................................................................302 Using Parametric Equations............................................................307 2D and 3D Parametric Lines...................................................307 Parametric Line Segments with Standard Direction Vector v.........308 Parametric Line Segments with Unit Direction Vector |v| = 1........309 Parametric Lines in 3D..........................................................311 Introduction to Quaternions..........................................................317 Complex Number Theory......................................................318 Hyper-Complex Numbers.......................................................325 Applications of Quaternions...................................................333 Basic Calculus..............................................................................337 The Concept of Infinity.........................................................337 Limits.................................................................................340 Summation and Finite Series..................................................341 Infinite Series.......................................................................344 Derivatives..........................................................................345 Integrals..............................................................................355 Summary....................................................................................361 5 Building a Math Engine 363 Brief Overview of the Math Engine..................................................364 File Structure of the Math Engine............................................364 Naming Conventions............................................................365 00 0672318350 fm 05•02•2003 2:31 PM Page ix Contents ix Error Handling.....................................................................366 My Final Word on C++..........................................................366 Data Structures and Types..............................................................367 Vectors and Points................................................................367 Parametric Lines...................................................................369 3D Planes............................................................................371 Matrices..............................................................................372 Quaternions.........................................................................376 Angular Coordinate System Support........................................377 Polar 2D..............................................................................378 Cylindrical 3D .....................................................................379 Spherical 3D........................................................................380 Fixed-Point Numbers.............................................................381 Math Constants...........................................................................382 Macros and Inline Functions..........................................................384 General Utility and Conversion...............................................390 Points and Vectors................................................................390 Matrices..............................................................................392 Quaternions.........................................................................394 Fixed-Point Math..................................................................395 Prototypes...................................................................................395 Globals.......................................................................................400 Math Engine API Listing................................................................401 Trigonometric Functions........................................................401 Coordinate System Support Functions......................................403 Vector Support Functions.......................................................407 Matrix Support Functions......................................................418 2D and 3D Parametric Line Support Functions..........................434 3D Plane Support Functions...................................................440 Quaternion Support Functions................................................446 Fixed-Point Math Support Functions........................................458 Equation Solving Support.......................................................464 Floating-Point Unit Math Primer.....................................................468 The FPU Architecture............................................................469 The FPU Stack......................................................................470 The FPU Instruction Set.........................................................472 Classical Instruction Format...................................................475 Memory Instruction Format...................................................475 Register Instruction Format....................................................476 Register Pop Instruction Format..............................................477 FPU Examples......................................................................477

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.