Sebastien St-Laurent
Thomson Course Technology PTR
The Premier Press and Thomson Course Technology PTR logo and related
trade dress are trademarks of Thomson Course Technology PTR and may not
be used without written permission.
NVIDIA® is a registered trademark of NVIDIA Corporation.
RenderMonkey™ is a trademark of ATI Technologies, Inc.
DirectX® is a registered trademark of Microsoft Corporation.
All other trademarks are the property of their respective owners.
trade dress are trademarks of Thomson Course Technology PTR and may not
be used without written permission.
NVIDIA® is a registered trademark of NVIDIA Corporation.
RenderMonkey™ is a trademark of ATI Technologies, Inc.
DirectX® is a registered trademark of Microsoft Corporation.
All other trademarks are the property of their respective owners.
First and foremost, I want to thank my wife Nicole for all of her support throughout
this project.Writing a book can be a major undertaking, and without her help
and love, I would never have completed this one or might have lost my sanity doing so. I love you!
I also want to extend a big thanks to the Thomson Course Technology PTR team, first for
giving me the opportunity to write this book, but also for all your help and support in making it come true.
Mathieu Mazerolle also deserves special mention for his efforts as a longtime friend and
technical editor. His help proved invaluable in making sure I was in line and ensuring this
book was the best possible book it could be. I also want to send my thanks to the kind people
at NVIDIA and ATI Technologies for their technical information, which helped
immensely with this production.
Finally, I want to thank everyone who has taught me in some way, including the awesome
teachers at Sherbrooke University and, more importantly, Larry Landry and Glen Eagan
for offering me an internship as part of the video game industry; thus launching my career.
About the Author
SEBASTIEN ST-LAURENT has been programming games professionally for several years,
working on titles for the Xbox, PlayStation 2, GameCube, and PC. He started in the video
game industry while studying computer engineering at Sherbrooke University in Sherbrooke,
Quebec. By interning in a small company called Future Endeavors during his college
years, he got into the industry and stood out in the line of graphics engineering.
After graduating from college, he moved to California to work full time with Z-Axis as
lead Xbox engineer, where he worked on several titles including the Dave Mirra Freestyle
BMX series. He is a graphics engineer in the ACES group at Microsoft, Inc, where he is
currently working on the next incarnation of Microsoft’s Flight Simulator product.
About the Series Editor
ANDRÉ LAMOTHE, CEO, Xtreme Games LLC, has been involved in the computing
industry 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
Technology PTR Game Development series.
Introduction
During the summer of 2003, I was approached by André LaMothe to write a book
on the topic of shaders. My experience on the PC and Xbox and writing game
engines and shader architectures made me a great candidate for such an
endeavor. Having always wanted to write a technical book, I simply could not resist and
jumped into this great adventure.
My first task was to determine the approach I would take in writing this book. At that
time, there were already several books available on the topic of shaders, and I felt the need
to innovate and explore this topic in a form not done before. One of my gripes with many
of the books already in print was that they all spent so much time explaining how to use
rendering APIs, such as DirectX, and little time making shaders. This is where the idea of
using ATI’s RenderMonkey came into being. This new tool offered a rich set of features,
allowing the quick, easy, and intuitive development of shaders.
I set off to do a brain dump of all my shader knowledge, taking advantage of
RenderMonkey to make the learning process even easier. Throughout this book, you
can expect to spend most of your time learning about shaders and how to create them. I
do not just focus on the basics; several useful techniques, from basic to advanced, are presented
in a straightforward manner aimed at allowing you to quickly absorb and apply the
knowledge you gain from this book.
Who Should Read This Book
The topic of Shaders for Game Programmers and Artists is shader development; therefore,
the book is written for anybody who has some interest in the topic. Because the topics and
techniques covered throughout this book are so varied, it is bound to be of interest to
everybody from hobbyist programmers to professional shader developers.
The approach I take in this book, using RenderMonkey, allows the content to be distanced
from rendering APIs, such as DirectX or OpenGL. This allows you, the reader, to focus
essentially on shader development and not on the development of framework applications.
My approach to this book has the added advantage of making shader development
available not only to engineers but also to technically minded artists.
Finally, with the approach taken throughout this book and the extensive exercises at the
end of each chapter, Shaders for Game Programmers and Artists can also be a valuable asset
in the classroom where real-time graphics have taken an even more important place in the
computer science curriculum.
What Will Be Covered (And What Won’t)
The topic of Shaders for Game Programmers and Artists is shaders, and it is all I will focus
on. I will explain a variety of techniques that cover a wide range of topics, from image filtering
to advanced lighting techniques. The following list summarizes some of the topics
covered in this book:
■ Introduction to several basic shader-related topics, including shaders, their history,
and extensive documentation on how to use RenderMonkey and develop shaders
using the HLSL shader language.
■ An extensive set of screen-based techniques that can be used to enhance existing
scenes. This book covers simple techniques, including everything from basic color
filters to more advanced topics such as depth of field, heat shimmer, and highdynamic
range rendering.
■ Lighting techniques ranging from simple per-vertex and per-pixel lighting
approaches to more advanced topics such as bumpmapping, spherical harmonics,
and polynomial texture maps.
■ The rendering of varying materials is also covered through several techniques
ranging from bi-directional refractance functions to procedural materials.
With this in mind, all shaders are developed making use of the RenderMonkey platform.
This tool, developed by ATI Technologies, provides an easy-to-use framework for shader
development. This approach allows you to focus solely on shaders and not on any specific
APIs or the writing of framework code.
The preceding paragraph implies what we will not cover in this book. Because I want to
focus solely on shader development, I will not go into any detail regarding general C/C++
programming; nor will I go into detail about how any of the rendering APIs work.
In simple words, this book covers shaders, using both RenderMonkey and the HLSL shader language.
Exercises
To facilitate the learning process throughout your reading of this book, I have included
several exercises at the end of each chapter in a section called “It’s Your Turn.” These exercises
invite you to expand upon the shaders developed throughout the chapters and
increase your understanding of shaders. Extensive solutions to each exercise are to be
found in Appendix D, “Exercise Solutions.”
Support
Finally, a Web site is maintained at http://www.courseptr.com that provides support for
this book. This site will be updated regularly to post errata and updates to the example
programs as needed. Be sure to check it if you have any problems.
And if you have any questions or comments about this book, feel free to contact me,
Sebastien St-Laurent, at sebastien_st_laurent@hotmail.com.
Product details
Price
|
|
---|---|
File Size
| 9,908 KB |
Pages
|
513 p |
File Type
|
PDF format |
ISBN
| 1-59200-092-4 |
Copyright
| 2004 by Thomson Course Technology PTR |
Contents at a Glance
Introduction
Part I From the Ground Up
Chapter 1 Welcome to the World of Shaders
Chapter 2 The Art of 3D
Chapter 3 RenderMonkey Version 1.5
Chapter 4 Getting Started, Your First Shaders
Part II Screen Effects
Chapter 5 Looking Through a Filter
Chapter 6 Blurring Things Up
Chapter 7 It’s Getting Hot in Here
Chapter 8 Making Your Day Brighter
Part III Making It Look Real
Chapter 9 May There Be Light
Chapter 10 Shiny Little Pixels
Chapter 11 Mirror, Mirror, On the Wall
Chapter 12 Not All Materials Are the Same
Chapter 13 Building Materials from Scratch
Chapter 14 Why Does It Always Need to Look Real?
Part IV Advanced Topics
Chapter 15 Watch Out for That Morning Fog
Chapter 16 Moving Objects Around
Chapter 17 Advanced Lighting
Chapter 18 Shadowing
Chapter 19 Geometry Tricks
Part V Appendixes
Appendix A High-Level Shader Language Reference
Appendix B Render Monkey 1.5 User Manual
Appendix C What’s on the CD
Appendix D Exercise Solutions
Appendix E Shader Library
Index
Table of Contents
Introduction . . . .. . . . . . xxi
Part I From the Ground Up . . . . .. . . . . 1
Chapter 1 Welcome to the World of Shaders . . . . .. . . . . . . . . 3
Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Vertex and Pixel Shader Pipelines and Capabilities . . . . . . . . . . . . . . . 6
Tool Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
RenderMonkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Microsoft Texture Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
NVIDIA Photoshop Plug-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3D Studio Max . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Microsoft Effect Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
NVIDIA’s Cg Toolkit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Chapter 2 The Art of 3D. . . . . . . . . . . . . . . . . . . 19
From the Ground Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Looking at Our Universe. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Translation Matrix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Scale Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Rotation Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Viewing It from a Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Under the Hood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3D APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
OpenGL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
DirectX and Direct3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Which One Is Better? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Hardware Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Chapter 3 RenderMonkey Version 1.5 . . . . . .. . . . . . . . 37
Introduction to RenderMonkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Our First Look at RenderMonkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Autopsy of a Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Chapter 4 Getting Started, Your First Shaders. . . . . . . . . 51
Your First Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Texturing Your Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Seeing Double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Exercise 1: ANIMATING A TEXTURE . . . . . . . . . . . . . . . . . . . . . . . 64
Exercise 2: BLENDING TWO TEXTURES . . . . . . . . . . . . . . . . . . . . . 64
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Part II Screen Effects. . . . .. . . . . 65
Chapter 5 Looking Through a Filter . . . . . . . . . . . . . . . 67
Rendering to a Sketchpad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Texture Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Finally Rendering Your Render Target . . . . . . . . . . . . . . . . . . . . . 74
Don’t Adjust Your TV! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Black and White, Like in the Old Times . . . . . . . . . . . . . . . . . . . . 75
Generalizations Are Good! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Things Are Not Always Linear . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Blurring Things Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Bring on the Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Motion Blur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Building the Motion Blur Shader . . . . . . . . . . . . . . . . . . . . . . . . . 86
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Exercise 1: OLD TIME MOVIE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Exercise 2: GAUSS FILTER . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Chapter 6 Blurring Things Up. . . . . .. . . . . . . . . . . 89
What Is Depth of Field? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
It’s All About Faking It! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Blurring Things, Take Two . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Depth Impostors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
A Note About Z-Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Using the Alpha Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
A Note About Multiple Render Targets . . . . . . . . . . . . . . . . . . . 106
Doing It Twice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
What About the Z-Buffer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Special Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Exercise 1: MULTIPLE IMPOSTORS. . . . . . . . . . . . . . . . . . . . . . . . 113
Exercise 2: USING A LOOKUP TEXTURE. . . . . . . . . . . . . . . . . . . . 113
Exercise 3: USING INTERMEDIATE BLUR TEXTURES
TO CREATE A SMOOTHER TRANSITION . . . . . . . . . . . . . . . . . . . 114
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Chapter 7 It’s Getting Hot in Here . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
What Is Heat Haze?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Uses for Heat Haze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
It’s All About Distortion Maps . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Putting a Background to Your Shader . . . . . . . . . . . . . . . . . . . . 120
Hitting the Pavement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Looking Above the Flame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Exercise 1: YOUR OWN REFRACTION SHADER . . . . . . . . . . . . . . 132
Exercise 2: MAKING IT MORE LIVELY . . . . . . . . . . . . . . . . . . . . . 132
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Chapter 8 Making Your Day Brighter. . . . . . . . . . . . . . . . . . . . . . . . . 133
What Is High Dynamic Range? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Glare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Streaks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Lens Flares. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
A Few HDR Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
What About Floating-Point Textures? . . . . . . . . . . . . . . . . . . . . 136
Exposure Control: The First Step. . . . . . . . . . . . . . . . . . . . . . . . . 136
A Note on Automatic Exposure Control . . . . . . . . . . . . . . . . . . . 139
Time for Some High Dynamic Range Effects . . . . . . . . . . . . . . . . . . . 139
Your First HDR Shader: The Glare! . . . . . . . . . . . . . . . . . . . . . . . 139
Time for Some Streaking! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Lens Flare Free-for-All. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Solutions for Today’s Hardware. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Exercise 1: USING A BIG FILTER . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Exercise 2: STREAKING ON TODAY’S HARDWARE . . . . . . . . . . . 152
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Part III Making It Look Real . . . . . . . . . . . . . . . . 153
Chapter 9 May There Be Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Of Light and Magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
What Makes Light in the First Place . . . . . . . . . . . . . . . . . . . . . . 156
Types of Lights. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Directional Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Point Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Spot Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Area Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Let’s Get Shading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Ambient Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Diffuse Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Specular Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Putting It Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Exercise 1: DIRECTION LIGHTS. . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Exercise 2: ANIMATING LIGHTS . . . . . . . . . . . . . . . . . . . . . . . . . . 177
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Chapter 10 Shiny Little Pixels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Why Isn’t Vertex Lighting Enough?. . . . . . . . . . . . . . . . . . . . . . . . . . 179
Basic Pixel Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Diffuse Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Specular Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Giving You Goose Bumps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Bumpmapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Tangent Space. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Normal Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Exercise 1: DIRECTION LIGHTS. . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Exercise 2: MULTIPLE LIGHTS. . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Chapter 11 Mirror, Mirror, On the Wall . . . . . . . . . . . . . . . . . . . . . . . . 199
From Reflections to Refractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Reflections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Refraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Walking Hand in Hand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Building Dynamic Environment Maps . . . . . . . . . . . . . . . . . . . . . . . . 212
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Exercise 1: DOING IT ALL PER-PIXEL . . . . . . . . . . . . . . . . . . . . . . 213
Exercise 2: COLOR-BASED REFRACTION . . . . . . . . . . . . . . . . . . . 214
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Chapter 12 Not All Materials Are the Same. . . . . . . . . . . . . . . . . . . . . 215
BRDFs Are Your Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Soft and Velvety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Determining BRDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Oren-Nayer Velvet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Exercise 1: USING LOOKUP TEXTURES . . . . . . . . . . . . . . . . . . . . 227
Exercise 2: MULTIPLE BRDFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Chapter 13 Building Materials from Scratch . . . . . . . . . . . . . . . . . . . . 229
Turning Up the Noise! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Clouds, Clouds in the Sky . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Wood and Marble. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Using Noise to Move Things Around . . . . . . . . . . . . . . . . . . . . . 240
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Exercise 1: ANIMATING CLOUDS . . . . . . . . . . . . . . . . . . . . . . . . . 242
Exercise 2: RENDERING STRATA . . . . . . . . . . . . . . . . . . . . . . . . . 242
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Chapter 14 Why Does It Always Need to Look Real? . . . . . . . . . . . . . 245
Just Like a Television Cartoon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Outline Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Other Outlining Ideas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Toon Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Real-Time Hatching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Exercise 1: DEPTH-BASED OUTLINE. . . . . . . . . . . . . . . . . . . . . . . 259
Exercise 2: SILHOUETTE AND TOON SHADING . . . . . . . . . . . . . . 260
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Part IV Advanced Topics. . . . . . . . . . . . . . . . . . 261
Chapter 15 Watch Out for That Morning Fog . . . . . . . . . . . . . . . . . . . 263
The Basics of Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Hardware Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Not Just Your Everyday Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Giving Your Fog a Little Depth . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Rendering the Atmosphere. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Exercise 1: ROUND FOG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Chapter 16 Moving Objects Around . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Light, Camera, Action! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Object Metamorphosis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Of Skin and Bones. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Chapter 17 Advanced Lighting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Outdoor Scene Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Some General Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Hemisphere Lighting Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Polynomial Texture Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Combining BRDF and Bumpmapping . . . . . . . . . . . . . . . . . . . . . 297
Building the Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Spherical Harmonics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
The Basic Idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Lighting with Spherical Harmonics . . . . . . . . . . . . . . . . . . . . . . . 304
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Exercise 1: PER-PIXEL SPHERICAL HARMONICS . . . . . . . . . . . . . . 306
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Chapter 18 Shadowing. . . . . . . . .. . . . . . . . . . . . 309
The Basics of Shadows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Shadow Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Shadow Volumes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Taking Advantage of the Hardware . . . . . . . . . . . . . . . . . . . . . . 323
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Exercise 1: SOFT SHADOW MAPPING . . . . . . . . . . . . . . . . . . . . . 325
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Chapter 19 Geometry Tricks .. . . . . . . . . . . . . . 327
Level of Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Static LOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Progressive LOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Re-Creating Lost Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Displacement Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
It’s Your Turn! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Part V Appendixes . . . . . . . . . . . . . . . . . . . . . . 341
Appendix A High-Level Shader Language Reference . . . . . . . . . . . . . . 343
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Scalar Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Vector Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Matrix Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Structure Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Predefined Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Typecasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
User-Defined Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Appendix B RenderMonkey Version 1.5 User Manual . . . . . . . . . . . . . 379
Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Installing RenderMonkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Using RenderMonkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Application Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Application Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Workspace View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Application Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Where Do We Go from Here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Appendix C What’s on the CD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Source Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
RenderMonkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
High Resolution Illustrations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
DirectX 9.0 SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
NVIDIA Texture Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
NVIDIA Photoshop Plug-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Appendix D Exercise Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Appendix E Shader Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Basic Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Object Transformation and Projection . . . . . . . . . . . . . . . . . . . . 451
Basic Texturing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Color Modulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Depth Encoding and Decoding. . . . . . . . . . . . . . . . . . . . . . . . . . 452
Screen Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Rendering to a Full Screen Quad . . . . . . . . . . . . . . . . . . . . . . . . 453
Color Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Basic Filtering Pixel Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Box Blur Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Gauss Blur Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Edge Detection Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Diffuse Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Specular Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Tangent Space Lighting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Per-Pixel Bumpmapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Polynomial Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Spherical Harmonics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Reflection and Refraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Refraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Velvet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Oren-Nayer Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Basic Perlin Noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Marble and Wood Noise Materials . . . . . . . . . . . . . . . . . . . . . . . 465
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●
●❂●