GAME ENGINE BLACK BOOK WOLFENSTEIN 3D FABIEN SANGLARD 1 Copyright In order to illustrate how the Wolfenstein 3D game engine works, a few screenshots, im- ages,sprites,andtexturesbelongingtoandcopyrightedbyidSoftwarearereproducedin thisbook. Thefollowingitemsareusedunderthe"fairuse"doctrine: 1. Allin-gamescreenshots,titlescreen,signonscreen,totalcarnagescreen. 2. Allin-gamemenuscreenshots(mainmenu,soundmenu). 3. All3Dsequencetextures(bluewall,wood,darkwood,gridwall). 4. All3Dsequencesprites(brownguard,deadguard,deaddog). 5. All3DsequenceimagesusedfortheHUD. 6. AllscreenshotsofSpearofDestiny,Catacomb3-D,andHovertankOne. 3 Acknowledgments ThankstoJohnCarmack,JohnRomero,RomainGuy,VictoriaHo,andAurelienSanglard forgenerouslyhelping. Thisprojectwouldhavenevermaterializedwithoutthem. Thanks to Jim Leonard and Foone Turing who volunteered their fleet of 286s, 386s, and VGAcardstoaccuratelybenchmarkWolfenstein3D. ThankstoJimLeonardforsharinghisencyclopedicknowledgeofPCsystemarchitecture and programming. His patience in explaining sound systems and extended memory sys- temshelpedthisbooktoshipwithaccurateinformation. Thanks to Chet Haase, Daniel Thornburgh, Xiao Yu, and Chris Forbes for proofreading andcatchingmistakes. Thankstoreaderswhokindlyreportederrorsinthefirstedition:ChristopherVanDerWest- huizen,pinterk,BryanStillwell,EliseyShemyakin,oneveu,IgorNikolaev,MikhailNaganov, ghosttie, elieb, Amro, Thizz (@codyvasy), Olivier Cahagne, tronster, Cyril Mottier, Ted Marynicz,EluanCostaMiranda,andJustinMeiners. Thankstoreaderswhokindlyreportederrorsinthesecondedition: A.Piquet,Amro,Ben Terris, Chris Chokan, David Snyder, Dennis Duda, Dmitry Minsky, Graeme McCutcheon, JamesFMcMahon,JonathanJayet,LFaria,TimGarlick,andEliseyShemyakin. –FabienSanglard [email protected] 5 How To Send Feedback This book strives to be as accurate and as clear as possible. If you find factual errors, spellingmistakes,ormerelyambiguities,pleasetakeafewminutestoreportthemonthe GameEngineBlackBook: Wolfenstein3D companionwebpagelocatedat: http://fabiensanglard.net/gebbwolf3d Thanks:) ! 7 Foreword by John Carmack Fabien’s commentary on the classic game engine codebases have been a wonderful re- sourceontheweb,soIwasthrilledthathedecidedtostartexpandingthemallthewayto booklength. WhileoftenovershadowedbyDoom,Wolfenstein3Ddoesholdasignificant placeinvideogamehistory,anditremainsfuntorunaroundintoday,justlikedroppinga quarterinaPacManmachine. Despite being open source, the 16-bit code and assembly language is not easy to build orexperimentwith, sofarfewerpeoplehavelookedintoitthanthelatercodebases. The mostremarkablethingabouttheprojectfromtoday’sperspectiveisjusthowsmallitwas: onelittledirectoryofcodefileswithnoexternaldependencies. BackthenIbarelytrusted (with some reason!) the C standard library implementations that we had to work with, so almosteverythingwasdoneinthosefewsourcefiles. Iwas21yearsoldwhenIwrotemostofthecode,andIhadonlybeenprogramminginC forayear,soitisfarfromamasterpieceofcodingstyle,buttherearestillsomethingsthat weredonewell. Compiledscalersareacaseofcodespecializationtakentotheextreme, which, combined with the low level VGA trick of multi-column writes and the progressive performance characteristics of ray-casting make it much more even in framerate than a conventional approach. The choice to use ray casting was also an important pragmatic decision. I wasn’t experienced enough yet to do a solid implementation of a polygon, or evenline-based,engine. Ray-castinggotmewhereIneededtobeatanacceptablecost. So,setthewaybackmachinefor1992. ACHTUNG! –JohnCarmack 9