Tải bản đầy đủ (.pdf) (513 trang)

shaders for game programming and artists

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.68 MB, 513 trang )

TEAM LinG - Live, Informative, Non-cost and Genuine !
TEAM LinG - Live, Informative, Non-cost and Genuine !
This page intentionally left blank
TEAM LinG - Live, Informative, Non-cost and Genuine !
TEAM LinG - Live, Informative, Non-cost and Genuine !
© 2004 by Thomson Course Technology PTR. All rights reserved. No part of this
book may be reproduced or transmitted in any form or by any means, electronic
or mechanical, including photocopying, recording, or by any information stor-
age or retrieval system without written permission from Thomson Course Tech-
nology PTR, except for the inclusion of brief quotations in a review.
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.
Important: Thomson Course Technology PTR cannot provide software sup-
port. Please contact the appropriate software manufacturer’s technical support
line or Web site for assistance.
Thomson Course Technology PTR and the author have attempted throughout
this book to distinguish proprietary trademarks from descriptive terms by fol-
lowing the capitalization style used by the manufacturer.
Information contained in this book has been obtained by Thomson Course
Technology PTR from sources believed to be reliable. However, because of the
possibility of human or mechanical error by our sources, Thomson Course
Technology PTR, or others, the Publisher does not guarantee the accuracy, ade-
quacy, or completeness of any information and is not responsible for any errors
or omissions or the results obtained from use of such information. Readers
should be particularly aware of the fact that the Internet is an ever-changing
entity. Some facts may have changed since this book went to press.


Educational facilities, companies, and organizations interested in multiple
copies or licensing of this book should contact the publisher for quantity dis-
count information. Training manuals, CD-ROMs, and portions of this book are
also available individually or can be tailored for specific needs.
ISBN: 1-59200-092-4
Library of Congress Catalog Card Number: 2004105651
Printed in the United States of America
04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1
Thomson Course Technology PTR,
a division of Thomson Course Technology
25 Thomson Place
Boston, MA 02210

SVP, Thomson Course
Technology PTR:
Andy Shafran
Publisher:
Stacy L. Hiquet
Senior Marketing Manager:
Sarah O’Donnell
Marketing Manager:
Heather Hurley
Manager of Editorial Services:
Heather Talbot
Acquisitions Editor:
Mitzi Foster
Senior Editor:
Mark Garvey
Associate Marketing Managers:
Kristin Eisenzopf and

Sarah Dubois
Project Editor:
Sandy Doell
Technical Reviewer:
Mathieu Mazerolle
Thomson Course Technology
PTR Market Coordinator:
Amanda Weaver
Interior Layout Tech:
Marian Hartsough
Cover Designer:
Mike Tanamachi
CD-ROM Producer:
Brandon Penticuff
Indexer:
Kelly Talbot
Proofreader:
Sean Medlock
TEAM LinG - Live, Informative, Non-cost and Genuine !
To my wife, Nicole,
for all her love and support
while I wrote this book.
TEAM LinG - Live, Informative, Non-cost and Genuine !
F
irst and foremost, I want to thank my wife Nicole for all of her support through-
out 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 peo-
ple 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.
vi
Acknowledgments
TEAM LinG - Live, Informative, Non-cost and Genuine !
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 Sher-
brooke, Quebec. By interning in a small company called Future Endeavors during his col-
lege 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É LAM
OTHE
, 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, com-

piler 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.
vii
About the Author
TEAM LinG - Live, Informative, Non-cost and Genuine !
Letter from the Series Editor
You may have noticed that the Thomson Course Technology PTR Game Develop-
ment series has not published a book on shaders until this one. This is no mistake.
We were waiting for a number of things to occur: first and foremost, for the tech-
nology to mature. If you recall the initial release of DirectX, you know that the soft-
ware was revised almost on a quarterly basis, and worse yet, everything you learned
was nearly useless until DirectX 5.0 stabilized a number of the systems. Shader pro-
gramming is a similar animal; it’s been changing very quickly; however, both
NVIDIA and ATI seem to have the hardware down, and Microsoft has stepped up to
take a leadership role in the development of HLSL (High Level Shader Language) to
make programming shaders as effortless as possible.
The second, and probably most important, reason we have held off on a book in this
area is that, as the series editor, I wanted to have a book that was the quintessential
guide to beginning to intermediate shader programming. Finding the right author
to do that has taken a long time, but the wait was well worth it. Sebastien St-Laurent
is expert at shader programming, but even more important is his ability to make the
topic interesting and engaging.
Moreover, the information you read in this book will not be out of date in six
months; this is core material, and 90 percent of it will be applicable three to five years
from now, so you are going to get an incredible return on your time investment.
There are a lot of shader books on the market. I have read all of them. When
Sebastien and I developed the outline and table of contents for this book, we both
wanted to make sure to cover the important material that others had covered while
filling the holes and gaps that other books have repeatedly left out.

In the final analysis, this is one of my favorite Thomson Course Technology PTR
Game Development books. Not only does the book move at a fast (although not a
blinding) pace, the writing style is fun, and the author continually gives examples
and suggestions of how to use the technology. In addition, because the book relies
heavily on ATI’s RenderMonkey shader tool, non-programmers and artists can learn
a lot as well.
On a technical note, the progress of graphics technology over the last 25 years is
rather cyclic. If you recall, the first 3D games were software-based with software ras-
terizers: DOOM, QUAKE, and related games. Then, as 3D fixed pipeline hardware
matured, games started taking advantage and became hardware-based, and the
pipeline moved to the hardware with the result that a huge loss of control ensued.
TEAM LinG - Live, Informative, Non-cost and Genuine !
Now, however, we can run software on a per pixel basis, and that’s a mind-blowing
concept. So shaders bring us full circle; we have the speed of hardware with the flex-
ibility of software. I suppose the next step will be for the hardware to be completely
reconfigurable via reprogrammable logic cores embedded in the GPUs we
will see.
In conclusion, if you had to pick a single book on pixel and vertex shader program-
ming, this is the complete solution. You will learn everything from the tools, the
technology, and actual implementation details. And of course this is all fresh mater-
ial, not regurgitated, updated material from articles or other books. Hence, without
hesitation, I recommend this book if you are interested at all in shader
technology.
Sincerely,
André LaMothe
Thomson Course Technology PTR Game Development Series Editor
2004
TEAM LinG - Live, Informative, Non-cost and Genuine !
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Part I From the Ground Up . . . . . . . . . . . . . . . . . 1

Chapter 1 Welcome to the World of Shaders . . . . . . . . . . . . . . . . . . . . 3
Chapter 2 The Art of 3D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chapter 3 RenderMonkey Version 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . 37
Chapter 4 Getting Started, Your First Shaders . . . . . . . . . . . . . . . . . . . 51
Part II Screen Effects. . . . . . . . . . . . . . . . . . . . 65
Chapter 5 Looking Through a Filter. . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Chapter 6 Blurring Things Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Chapter 7 It’s Getting Hot in Here . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Chapter 8 Making Your Day Brighter. . . . . . . . . . . . . . . . . . . . . . . . . 133
x
Contents at a Glance
TEAM LinG - Live, Informative, Non-cost and Genuine !
Part III Making It Look Real . . . . . . . . . . . . . . 153
Chapter 9 May There Be Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Chapter 10 Shiny Little Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Chapter 11 Mirror, Mirror, On the Wall . . . . . . . . . . . . . . . . . . . . . . . . 199
Chapter 12 Not All Materials Are the Same. . . . . . . . . . . . . . . . . . . . . 215
Chapter 13 Building Materials from Scratch . . . . . . . . . . . . . . . . . . . . 229
Chapter 14 Why Does It Always Need to Look Real?. . . . . . . . . . . . . . 245
Part IV Advanced Topics . . . . . . . . . . . . . . . . . 261
Chapter 15 Watch Out for That Morning Fog . . . . . . . . . . . . . . . . . . . 263
Chapter 16 Moving Objects Around. . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Chapter 17 Advanced Lighting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Chapter 18 Shadowing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Chapter 19 Geometry Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Part V Appendixes . . . . . . . . . . . . . . . . . . . . . . 341
Appendix A High-Level Shader Language Reference . . . . . . . . . . . . . . 343
Appendix B Render Monkey 1.5 User Manual . . . . . . . . . . . . . . . . . . . 379
Appendix C What’s on the CD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Appendix D Exercise Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

Appendix E Shader Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Contents at a Glance xi
TEAM LinG - Live, Informative, Non-cost and Genuine !
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
xii
Contents
TEAM LinG - Live, Informative, Non-cost and Genuine !
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

Contents xiii
TEAM LinG - Live, Informative, Non-cost and Genuine !
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
Contentsxiv
TEAM LinG - Live, Informative, Non-cost and Genuine !
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
Contents xv
TEAM LinG - Live, Informative, Non-cost and Genuine !
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
Contentsxvi
TEAM LinG - Live, Informative, Non-cost and Genuine !
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
Contents xvii

TEAM LinG - Live, Informative, Non-cost and Genuine !
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
Contentsxviii
TEAM LinG - Live, Informative, Non-cost and Genuine !
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
Contents xix
TEAM LinG - Live, Informative, Non-cost and Genuine !
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
Contentsxx
TEAM LinG - Live, Informative, Non-cost and Genuine !
D
uring 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 pre-
sented 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.
xxi
Introduction
TEAM LinG - Live, Informative, Non-cost and Genuine !
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 applica-
tions. 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 fil-
tering 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 high-
dynamic 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.
Introductionxxii
TEAM LinG - Live, Informative, Non-cost and Genuine !
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 exer-
cises 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 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

Introduction xxiii
TEAM LinG - Live, Informative, Non-cost and Genuine !
This page intentionally left blank
TEAM LinG - Live, Informative, Non-cost and Genuine !

×