Real-Time 3D Rendering With DirectX And HLSL: A Practical .

Transcription

Praise forReal-Time 3D Renderingwith DirectX and HLSL“I designed and taught the technical curriculum at UCF’s FIEA graduate program and was neversatisfied with textbooks available for graphics programming. I wish I had Paul Varcholik’s bookthen; it would make the list now.”—Michael Gourlay, Principal Development Lead, Microsoft“Modern 3D rendering is a surprisingly deep topic; one that spans several different areas. Manybooks only focus on one specific aspect of rendering, such as shaders, but leave other aspectswith little or no discussion. Real-Time 3D Rendering with DirectX and HLSL takes the approach ofgiving you a full understanding of what a modern rendering application consists of, from oneend of the pipeline to the other.”—Joel Martinez, Software Engineer, Xamarin“This practical book will take you on a journey of developing a modern 3D rendering enginethrough step-by-step code examples. I highly recommend this well-written book for anyonewho wants to learn the necessary graphics techniques involved in developing a 3D renderingengine using the latest Direct3D.”—Budirijanto Purnomo, GPU Developer Tools Lead, Advanced Micro Devices, Inc.“A great tour of the modern DirectX landscape, with a heavy emphasis on authoring HLSLshaders for common game rendering techniques for C developers.”—Chuck Walbourn, Senior Design Engineer, Microsoft

This page intentionally left blank

Real-Time 3D RenderingwithDirectX and HLSL

The Addison-WesleyGame Design and Development SeriesVisit informit.com/series/gamedesign for a complete list of available publications.Essential References for Game Designers and Developershese practical guides, written by distinguished professors and industry gurus,cover basic tenets of game design and development using a straightforward,common-sense approach. The books encourage readers to try things on their ownand think for themselves, making it easier for anyone to learn how to design anddevelop digital games for both computers and mobile devices.TMake sure to connect with us!informit .com/socialconnect

Real-Time 3D RenderingwithDirectX and HLSL A Practical Guide to Graphics ProgrammingPaul VarcholikUpper Saddle River, NJ Boston Indianapolis San FranciscoNew York Toronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico City

Many of the designations used by manufacturers and sellers to distinguish their productsare claimed as trademarks. Where those designations appear in this book, and thepublisher was aware of a trademark claim, the designations have been printed with initialcapital letters or in all capitals.The author and publisher have taken care in the preparation of this book, but make noexpressed or implied warranty of any kind and assume no responsibility for errors oromissions. No liability is assumed for incidental or consequential damages in connectionwith or arising out of the use of the information or programs contained herein.For information about buying this title in bulk quantities, or for special sales opportunities(which may include electronic versions; custom cover designs; and content particular toyour business, training goals, marketing focus, or branding interests), please contact ourcorporate sales department at corpsales@pearsoned.com or (800) 382-3419.For government sales inquiries, please contact governmentsales@pearsoned.com.For questions about sales outside the U.S., please contact international@pearsoned.com.Visit us on the Web: informit.com/awLibrary of Congress Control Number: 2014933263Copyright 2014 Pearson Education, Inc.Editor-in-ChiefMark TaubExecutive EditorLaura LewinDevelopment EditorSonglin QiuManaging EditorKristy HartSenior Project EditorLori LyonsCopy EditorKrista Hansing EditorialServices, Inc.IndexerTim WrightAll rights reserved. Printed in the United States of America. This publication is protectedby copyright, and permission must be obtained from the publisher prior to any prohibitedreproduction, storage in a retrieval system, or transmission in any form or by any means,electronic, mechanical, photocopying, recording, or likewise. To obtain permission touse material from this work, please submit a written request to Pearson Education, Inc.,Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or youmay fax your request to (201) 236-3290.ProofreaderDebbie WilliamsDirectX, Direct3D, MS-DOS, MSDN, Visual Studio, Windows, Windows Phone, WindowsVista, Xbox, and Xbox 360 are registered trademarks of Microsoft Corporation in theUnited States and/or other countries.Editorial AssistantOlivia BasegioNVIDIA, GeForce, Nsight, and FX Composer are registered trademarks of NVIDIACorporation in the United States and/or other countries.Autodesk, Maya and 3ds Max are registered trademarks of Autodesk, Inc. in the UnitedStates and/or other countries.Rendermonkey is a trademark of Advanced Micro Devices, Inc.OpenGL is a registered trademark of Silicon Graphics, Inc. in the United States and/or othercountries worldwide.StarCraft and Blizzard Entertainment are trademarks or registered trademarks of BlizzardEntertainment, Inc. in the United States and/or other countries.COLLADA is a trademark of the Khronos Group Inc.Photoshop is a registered trademark of Adobe Systems Incorporated in the United Statesand/or other countries.Steam is a registered trademark of Valve Corporation.Terragen is a trademark of Planetside Software.Unreal Development Kit and UDK are trademarks or registered trademarks of Epic Games,Inc. in the United States and elswhere.Unity Software is a copyright of Unity Technologies.All other trademarks are the property of their respective owners.ISBN-13: 978-0-321-96272-0ISBN-10: 0-321-96272-9Text printed in the United States on recycled paper at R.R. Donnelley inCrawfordsville, Indiana.First printing: April 2014Technical ReviewersMichael GourlayJoel MartinezBudirijanto PurnomoCover DesignerChuti PrasertsithSenior CompositorGloria Schurick

Contents-at-a-GlanceIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Part I An Introduction to 3D Rendering . . . . . . . . . . . . . . . . . . 51Introducing DirectX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72A 3D/Math Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233Tools of the Trade. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Part II Shader Authoring with HLSL . . . . . . . . . . . . . . . . . . . . .574 Hello, Shaders!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736Lighting Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917Additional Lighting Models . . . . . . . . . . . . . . . . . . . . . . .1158Gleaming the Cube. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1419Normal Mapping and Displacement Mapping . . . . . . . . . . 169Part III Rendering with DirectX . . . . . . . . . . . . . . . . . . . . . . . 18310Project Setup and Window Initialization . . . . . . . . . . . . . . 18511Direct3D Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . 20512Supporting Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . 23313Cameras. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26914 Hello, Rendering! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28315Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31516Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34117Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371

viiiCONTENTS-AT-A-GLANCEPart IV Intermediate-Level Rendering Topics . . . . . . . . . . . . . . 38918 Post-Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39119Shadow Mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43520Skeletal Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46921Geometry and Tessellation Shaders . . . . . . . . . . . . . . . . . 49722 Additional Topics in Modern Rendering . . . . . . . . . . . . . . 529Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555

ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Part I An Introduction to 3D Rendering . . . . . . . . . . . . . . . . . . 51Introducing DirectX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7A Bit of History. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8The Direct3D 11 Graphics Pipeline . . . . . . . . . . . . . . . . . . . . 9Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212A 3D/Math Primer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31DirectXMath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Tools of the Trade. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Microsoft Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . 44NVIDIA FX Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Visual Studio Graphics Debugger. . . . . . . . . . . . . . . . . . . . 53Graphics Debugging Alternatives . . . . . . . . . . . . . . . . . . . 55Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Part II Shader Authoring with HLSL . . . . . . . . . . . . . . . . . . . . .574Hello, Shaders!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Your First Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Hello, Structs! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

xCONTENTS5Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73An Introduction to Texture Mapping . . . . . . . . . . . . . . . . . 74A Texture Mapping Effect. . . . . . . . . . . . . . . . . . . . . . . . . 75Texture Filtering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Texture Addressing Modes . . . . . . . . . . . . . . . . . . . . . . . . 86Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896Lighting Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Ambient Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Diffuse Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97Specular Highlights . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1147Additional Lighting Models . . . . . . . . . . . . . . . . . . . . . . .115Point Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116Spotlights. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Multiple Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1398Gleaming the Cube. . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Texture Cubes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Skyboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145Environment Mapping . . . . . . . . . . . . . . . . . . . . . . . . . 149Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Color Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

CONTENTS9xiNormal Mapping and Displacement Mapping . . . . . . . . . . 169Normal Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170Displacement Mapping . . . . . . . . . . . . . . . . . . . . . . . . . 178Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181Part III Rendering with DirectX . . . . . . . . . . . . . . . . . . . . . . . 18310Project Setup and Window Initialization . . . . . . . . . . . . . . 185A New Beginning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186Project Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186The Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Window Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . 199Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20411Direct3D Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . 205Initializing Direct3D . . . . . . . . . . . . . . . . . . . . . . . . . . . 206Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . 219Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23212Supporting Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . 233Game Components . . . . . . . . . . . . . . . . . . . . . . . . . . . 234Device Input . . . .

Real-Time 3D Rendering with DirectX and HLSL takes the approach of giving you a full understanding of what a modern rendering application consists of, from one end of the pipeline to the other.” —Joel Martinez, Software Engineer, Xamarin “This practical book will take you on a journey of developing a modern 3D rendering engine