Game Development Reference
In-Depth Information
/ /
Compute
t h e
V
v e c t o r
f l o a t 3
V
=
n o r m a l i z e ( v i e w P o s
i n p u t . p o s ) ;
/ / C l e a r a c c u m u l a t o r s .
o u t p u t . d i f f C o l o r
=
a m b i e n t L i g h t C o l o r ;
o u t p u t . s p e c C o l o r
=
0 ;
/ /
Sum up
l i g h t s .
Note
t h a t
t h e
c o m p i l e r
i s
u s u a l l y
p r e t t y
/ /
good
a t
u n r o l l i n g
s m a l l
l o o p s
l i k e
t h i s ,
b u t
t o
e n s u r e
/ /
t h e
f a s t e s t
code ,
i t ' s
b e s t
n o t
t o
depend
on
t h e
c o m p i l e r ,
/ /
and
u n r o l l
t h e
l o o p
y o u r s e l f
f o r
( i n t
i
=
0
;
i < 3
;
++ i )
{
/ /
Compute
l a m b e r t
term
and sum
d i f f u s e
c o n t r i b
f l o a t
nDotL
=
dot ( i n p u t . normal ,
l i g h t D i r [ i ] ) ;
o u t p u t . d i f f C o l o r
+= max ( nDotL , 0 )
l i g h t C o l o r [ i ] ;
/ /
Compute
h a l f w a y
v e c t o r
f l o a t 3 H =
n o r m a l i z e ( V
+
l i g h t D i r [ i ] ) ;
/ /
Sum
s p e c u l a r
c o n t r i b
f l o a t
nDotH
=
dot ( i n p u t . normal ,H ) ;
f l o a t
s
= pow ( max ( nDotH , 0 ) ,
s p e c E x p o n e n t ) ;
o u t p u t . s p e c C o l o r
+=
s l i g h t C o l o r [ i ] ;
}
/ /
P a s s
t h r o u g h
t h e
s u p p l i e d UV
c o o r d i n a t e s
w i t h o u t
m o d i f i c a t i o n
o u t p u t . uv
=
i n p u t . uv ;
r e t u r n
o u t p u t ;
}
Listing 10.20
Vertex shader for Gouraud shading, using constant ambient plus three directional lights
10.11.4 Bump Mapping
Next, let's look at an example of normal mapping. We will be performing
the lighting in tangent space, and we'll stick with the lighting environment
of a single omni light plus constant ambient to make the examples easier to
compare. In the vertex shader (Listing 10.21), we synthesize the binormal
from the normal and tangent. Then, we use the three basis vectors to rotate
L and H into tangent space, after first computing them as usual in model
space. Notice the use of the three dot products, which is equivalent to mul-
tiplication by the transpose of the matrix. We also perform the attenuation
calculations in the vertex shader, passing the unclamped attenuated light
color, as we have done in previous examples.
/ /
Mesh
i n p u t s .
s t r u c t
I n p u t
{
f l o a t 4
p o s
:
POSITION ;
/ /
p o s i t i o n
i n
model
space
f l o a t 3
n o r m a l
: NORMAL ;
/ /
v e r t e x
normal
i n
model
space
f l o a t 4
t a n g e n t D e t
:
TANGENT ;
/ /
t a n g e n t
i n
model
space ,
d e t
i n w
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d s
f o r
d i f f u s e ,
spec
maps
} ;
/ /
V e r t e x
s h a d e r
o u t p u t
s t r u c t
O u t p u t
{
f l o a t 4
c l i p P o s
:
POSITION ;
/ /
c l i p space
p o s i t i o n
 
Search WWH ::




Custom Search