Game Development Reference
In-Depth Information
f l o a t
L
=
omniPosAndInvRad . x y z
i n p u t . po s ;
/ /
Normalize
i t ,
and
s a v e
o f f
d i s t a n c e
t o
use
l a t e r
/ /
f o r
a t t e n u a t i o n
f l o a t
d i s t
=
l e n g t h ( L ) ;
L
/ =
d i s t ;
/ /
Compute
view
and
h a l f w a y
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 ) ;
f l o a t 3 H =
n o r m a l i z e ( V
+
L ) ;
/ /
Compute
a t t e n u a t e d
l i g h t
c o l o r .
f l o a t 3
l i g h t C o l o r
=
o m n i C o l o r
max ( 1
d i s t omniPosAndInvRad . w , 0 ) ;
/ /
Compute
d i f f u s e
and
s p e c u l a r
f a c t o r s
f l o a t
d i f f F a c t o r
= max ( dot ( i n p u t . normal ,
L ) , 0 ) ;
f l o a t
s p e c F a c t o r
= pow ( max ( dot ( i n p u t . normal ,H ) , 0 ) ,
s p e c E x p o n e n t ) ;
/ /
Compute
e f f e c t i v e
l i g h t
c o l o r s
o u t p u t . d i f f C o l o r
=
l i g h t C o l o r d i f f F a c t 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
=
l i g h t C o l o r s p e c F a c t o r ;
/ /
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.18
Vertex shader for Gouraud shading of a single omni plus ambient
Now the pixel shader (Listing 10.19) simply takes the lighting results
and modulates by the material diffuse and specular colors, from the texture
maps.
/ /
I n t e r p o l a t e d
i n p u t s
from
t h e
v e r t e x
s h a d e r .
s t r u c t
I n p u t
{
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
f l o a t 3
d i f f C o l o r
:
TEXCOORD1 ;
/ /
d i f f u s e
l i g h t i n g
RGB
f l o a t 3
s p e c C o l o r
:
TEXCOORD2 ;
/ /
s p e c u l a r
l i g h t i n g
RGB
} ;
/ /
A
g l o b a l
c o n s t a n t
RGB and
o p a c i t y
uniform
f l o a t 4
c o n s t a n t C o l o r ;
/ /
D i f f u s e
and
s p e c u l a r
map
s a m p l e r s .
Note
t h a t
we assume
t h a t
d i f f u s e
/ /
and
spec
maps
a r e
mapped
t h e
same ,
and
so
t h e y
use
t h e
same UV c o o r d s
sampler2D
d i f f u s e M a p ;
sampler2D
s p e c u l a r M a p ;
/ /
P i x e l
s h a d e r
body
f l o a t 4
main ( I n p u t
i n p u t ) :
COLOR
{
/ /
F e t c h
t h e
t e x e l s
t o
g e t
t h e
m a t e r i a l
c o l o r s
f l o a t 4
m a t e r i a l D i f f
= tex2D ( d i f f u s e M a p ,
i n p u t . uv ) ;
f l o a t 4
m a t e r i a l S p e c
= tex2D ( s p e c u l a r M a p ,
i n p u t . uv ) ;
/ /
Sum up
c o l o r s .
Note
t h a t
HLSL
has
a
v e r y
f l e x i b l e
s w i z z l i n g
s y s t e m
/ /
which
a l l o w s
us
t o
a c c e s s
a
p o r t i o n
o f
a
v e c t o r
a s
i f
were
a
/ /
”member”
o f
t h e
v e c t o r
f l o a t 4
r e s u l t
=
m a t e r i a l D i f f ;
/ /
RGB
and
o p a c i t y
from
d i f f u s e
map
Search WWH ::




Custom Search