BA3D Front Matter 11/15/05 1:13 PM Page i ™ WAITE GROUP PRESS™ CORTE MADERA, CA BA3D Front Matter 11/15/05 1:13 PM Page ii PUBLISHER Mitchell Waite EDITOR-IN-CHIEF Charles Drucker ACQUISITIONS EDITOR Jill Pisoni EDITORIAL DIRECTOR John Crudo MANAGING EDITOR Kurt Stephan CONTENT EDITOR Heidi Brumbaugh TECHNICAL REVIEWER Jeff Bankston COPY EDITOR Judith Brown PRODUCTION DIRECTOR Julianne Ososke PRODUCTION MANAGER Cecile Kaufman PRODUCTION TRAFFIC COORDINATOR Ingrid Owen DESIGN Sestina Quarequio, Christi Fryday PRODUCTION Jude Levinson ILLUSTRATIONS Kristin Peterson, Pat Rogondino, Larry Wilson COVER ILLUSTRATION James Dowlen APPENDIX A ILLUSTRATION Barclay Shaw © 1995 by The Waite Group, Inc.® Published by Waite Group Press™, 200 Tamal Plaza, Corte Madera, CA 94925 Waite Group Press™ is distributed to bookstores and book wholesalers by Publishers Group West, Box 8843, Emeryville, CA 94662, 1-800-788-3123 (in California 1-510-658-3453). All rights reserved. No part of this manual shall be reproduced, stored in a retrieval system, or transmitted by any means, elec- tronic, mechanical, photocopying, desktop publishing, recording, or otherwise, without written permission from the publish- er. No patent liability is assumed with respect to the use of the information contained herein. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. All terms mentioned in this book that are known to be trademarks or service marks are listed below. In addition, terms sus- pected of being trademarks or service marks have been appropriately capitalized. Waite Group Press 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. The Waite Group is a registered trademark of The Waite Group, Inc. Waite Group Press and The Waite Group logo are trademarks of The Waite Group, Inc. America Online is a registered service mark of America Online, Inc. Apple and Macintosh are registered trademarks of Apple Computer, Inc. All Borland products mentioned are trademarks or registered trademarks of Borland International, Inc. CompuServe is a registered trademark of CompuServe, Inc. DOS Extender is a trademark of Tenberry Software. All IBM products mentioned are trademarks or registered trademarks of International Business Machines, Inc. All Microsoft products mentioned are trademarks or registered trademarks of Microsoft Corporation. NorthCAD-3D is a trademark of CyberDesign Productions. Pentium is a trademark of Intel, Inc. Sound Blaster, Sound Blaster Pro, and Video Blaster are trademarks of Creative Labs, Inc. All other product names are trademarks, registered trademarks, or service marks of their respective owners. Printed in the United States of America 95 96 97 98 • 10 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication Data LaMothe, André. Black art of 3D game programming / André LaMothe. p. cm. Includes index. ISBN 1-57169-004-2 1. Computer games--Programming. 2. Three-dimensional display systems. I. Title. QA76.76.C672L36 1995 794.8’16765--dc20 95-32976 CIP BA3D Front Matter 11/15/05 1:13 PM Page iii Dedication I dedicate this book to all the children of the world; may you take us boldly into the 21st century and beyond... BA3D Front Matter 11/15/05 1:13 PM Page iv Message from the Publisher WELCOME TO OUR NERVOUS SYSTEM Some people say that the World Wide Web is a graphical extension of the information superhighway, just a network of humans and machines sending each other long lists of the equivalent of digital junk mail. I think it is much more than that. To me the Web is nothing less than the nervous system of the entire planet—not just a collection of computer brains connected togeth- er, but more like a billion silicon neurons entangled and recirculating electro-chemical signals of information and data, each contributing to the birth of another CPU and another Web site. Think of each person’s hard disk connected at once to every other hard disk on earth, driven by human navigators searching like Columbus for the New World. Seen this way the Web is more of a super entity, a growing, living thing, controlled by the universal human will to expand, to be more. Yet unlike a purposeful business plan with rigid rules, the Web expands in a nonlinear, unpredictable, creative way that echoes natural evolution. We created our Web site not just to extend the reach of our computer book products but to be part of this synaptic neural network, to experience, like a nerve in the body, the flow of ideas and then to pass those ideas up the food chain of the mind. Your mind. Even more, we wanted to pump some of our own creative juices into this rich wine of technology. TASTE OUR DIGITAL WINE And so we ask you to taste our wine by visiting the body of our business. Begin by understanding the metaphor we have created for our Web site—a universal learning center, situated in outer space in the form of a space station. A place where you can journey to study any topic from the convenience of your own screen. Right now we are focusing on computer topics, but the stars are the limit on the Web. If you are interested in discussing this Web site, or finding out more about the Waite Group, please send 200 Tamal Plaza me email with your comments and I will be happy to Corte Madera CA 94925 415 924 2575 respond. Being a programmer myself, I love to talk 415 924 2576 fax about technology and find out what our readers are Internet email: looking for. [email protected] Sincerely, CompuServe email: 75146,3515 Website: Mitchell Waite, C.E.O. and Publisher http://www.waite.com/waite CREATING THE HIGHEST QUALITY COMPUTER BOOKS IN THE INDUSTRY Waite Group Press Waite Group New Media BA3D Front Matter 11/15/05 1:13 PM Page v Come Visit WAITE.COM Waite Group Press World Wide Web Site Now find all the latest information on Waite Group books at our new Web site, http://www.waite.com/waite. You’ll find an online catalog where you can examine and order any title, review upcoming books, and send email to our authors and editors. Our ftp site has all you need to update your book: the latest program listings, errata sheets, most recent versions of Fractint, POVRay, Polyray, DMorph, and all the programs featured in our books. So down- load, talk to us, ask questions, on http://www.waite.com/waite. The New Arrivals Room has all our new books listed by month. Just click for a description, Index, Table of Contents, and links to authors. The Backlist Room has all our books listed alphabetically. The People Room is where you’ll interact with Waite Group employees. Links to Cyberspace get you in touch with other computer book publishers and other interesting Web sites. The FTPsite contains all The Subject Room contains typical book program listings, errata pages which show description, Index, Table sheets, etc. The Order Room is where of Contents, and links to authors. you can order any of our books online. World Wide Web: COME SURF OUR TURF—THE WAITE GROUP WEB http://www.waite.com/waite Gopher: gopher.waite.com FTP: ftp.waite.com ™ BA3D Front Matter 11/15/05 1:13 PM Page vi André LaMothe has worked in many areas of the computing industry, including neural networks, 3D graphics, virtual reality, and robotics. He holds degrees in Math, Computer Science, and Electrical Engineering. You’ll find articles by André published in Computer Language, Circuit Cellar, and Game Developer. André has also written two best-selling books previous to Black Art of 3D Game Programming—Tricks of the Game Programming Gurus and Teach Yourself Game Programming in 21 Days. vi BA3D Front Matter 11/15/05 1:13 PM Page vii Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviii Part I: Incantations 1 Chapter 1: The Genesis of 3D Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Elements of the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Part II: Alchemy 45 Chapter 3: The Mysterious VGA Card. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Chapter 4: Waking the Dead with Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Chapter 5: Communicating with the Outside World . . . . . . . . . . . . . . . . . . . . . . . 205 Chapter 6: Dancing with Cyberdemons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Chapter 7: The Magick of Thought . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Chapter 8: The Art of Possession. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 Chapter 9: Multiplayer Game Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 Chapter 10: 3D Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 Chapter 11: Building a 3D Graphics Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 Chapter 12: Solid Modeling and Shading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701 Chapter 13: Universal Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783 Chapter 14: Hidden Surface and Object Removal . . . . . . . . . . . . . . . . . . . . . . . . . 803 Chapter 15: Clipping and Rendering the Final View. . . . . . . . . . . . . . . . . . . . . . . 827 Chapter 16: Voxel Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933 Part III Spells 991 : Chapter 17: Optimizing the 3D Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993 Chapter 18: Kill or Be Killed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1115 Appendix A: Cybersorcerer and Cyberwizard Contests. . . . . . . . . . . . . . . . . . . . 1155 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1159 BA3D Front Matter 11/15/05 1:13 PM Page viii Part I: Incantations 1 Chapter 1: The Genesis of 3D Games . . . . . . . . . . . . . . . . . . . . . . . . . 3 Of Cyberspace and 3D Games . . . . . . . . . . . . . . . . . . . . . . . . 5 Evolving from 2D to 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Vector Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Raster Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Parallax Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Wireframe Polygon Engines. . . . . . . . . . . . . . . . . . . . . . . 12 Trends in Sorcery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Solid Polygon Rendering . . . . . . . . . . . . . . . . . . . . . . . . 13 Ray Casting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Voxel Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Ellipsoidal Technology . . . . . . . . . . . . . . . . . . . . . . . . . 16 Journeys into the Mind’s Eye . . . . . . . . . . . . . . . . . . . . . . . . 18 The Black Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Chapter 2: Elements of the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 The Sequence of a Game . . . . . . . . . . . . . . . . . . . . . . . . . . 23 The Installation and Setup Phase . . . . . . . . . . . . . . . . . . . . 24 Introducing the Player to the Game . . . . . . . . . . . . . . . . . . . 25 Run Time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The Concluding Sequence . . . . . . . . . . . . . . . . . . . . . . . 26 The Input Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Event Driven Programming . . . . . . . . . . . . . . . . . . . . . . . . . 30 Input Driven Event Loops. . . . . . . . . . . . . . . . . . . . . . . . 30 Real-Time Event Loops . . . . . . . . . . . . . . . . . . . . . . . . . 32 Typical 3D Game Loops. . . . . . . . . . . . . . . . . . . . . . . . . 34 Autonomous Functions and Auto-Logic. . . . . . . . . . . . . . . . . . . 40 viii BA3D Front Matter 11/15/05 1:13 PM Page ix (cid:1)(cid:1)(cid:1)(cid:1)(cid:1) Black Art of3D Game Programming ix Temporal and Video Synchronization . . . . . . . . . . . . . . . . . . . . 41 The Sound System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Part II: Alchemy 45 Chapter 3: The Mysterious VGA Card . . . . . . . . . . . . . . . . . . . . . . . . 47 Raster Graphics Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 VGA Internals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Graphics Controller. . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Display Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Serializer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Attribute Controller. . . . . . . . . . . . . . . . . . . . . . . . . . . 56 CRT Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Sequencer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Using BIOS and the C Library Functions. . . . . . . . . . . . . . . . . . . 57 Screen Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 The Lay of the Land. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Summoning Mode 13h . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Clearing the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Rendering Images on the Screen. . . . . . . . . . . . . . . . . . . . . . . 65 Plotting Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Drawing Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Horizontal Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Vertical Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Filling Rectangles. . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Restoring the Video Mode. . . . . . . . . . . . . . . . . . . . . . . . . . 74 Optimizing Screen Access . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The Color Lookup Table . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Writing to a Color Register . . . . . . . . . . . . . . . . . . . . . . . 77 Reading a Color Register . . . . . . . . . . . . . . . . . . . . . . . . 79 Saving and Restoring the Palette . . . . . . . . . . . . . . . . . . . . 80 Bitmapped Graphics Fundamentals . . . . . . . . . . . . . . . . . . . . . 82 Drawing Text with Dots. . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Mode 13h in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 The Dark Secrets of the X-tendedGraphics Modes . . . . . . . . . . . . . . 91 Introduction to Mode Z . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 The Memory Configuration . . . . . . . . . . . . . . . . . . . . . . . . . 92 Switching to Mode Z . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Clearing the Mode Z Screen. . . . . . . . . . . . . . . . . . . . . . . . . 98 Plotting Z Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Page Flipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Mode Z in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103