Game Development Reference
In-Depth Information
/ /
Compute
v e c t o r
t o
l i g h t
f l o a t 3
L
=
omniPos
i n p u t . p o 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 ) ;
o u t p u t . 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 ) ;
o u t p u t .H =
n o r m a l i z e ( V
+
o u t p u t . L ) ;
/ /
Compute
a t t e n u a t i o n
f a c t o r .
Note
t h a t
we do NOT clamp
/ /
t o
z e r o
here ,
we
w i l l
do
t h a t
i n
t h e
p i x e l
s h a d e r .
T h i s
/ /
i s
i m p o r t a n t
i n
c a s e
t h e
f a l l o f f
r e a c h e s
z e r o
i n
t h e
middle
/ /
o f
a
l a r g e
polygon .
f l o a t
a t t e n F a c t o r
=
1
d i s t invOmniRad ;
o u t p u t . l i g h t C o l o r
=
o m n i C o l o r
a t t e n F a c t o r ;
/ /
P a s s
t h r o u g h
o t h e r
v e r t e x
i n p u t 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 . n o r m a l
=
i n p u t . n o r m a l ;
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.15
Alternate vertex shader for per-pixel lighting of a single omni plus ambient
Now the pixel shader has less work to do. According to the DirectX 10
FXC compiler, the pixel shader in Listing 10.16 compiles to approximately
25 instruction slots, compared to 33 instruction slots for Listing 10.14.
/ /
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
n o r m a l
:
TEXCOORD1 ;
/ /
v e r t e x
normal
i n
model
space
f l o a t 3
L
:
TEXCOORD2 ;
/ /
v e c t o r
t o
l i g h t
f l o a t 3 H
:
TEXCOORD3 ;
/ /
h a l f w a y
v e c t o r
f l o a t 3
l i g h t C o l o r
:
TEXCOORD4 ;
/ /
l i g h t
c o l o r
+
a t t e n u a t i o n
f a c t o r
} ;
/ /
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 ;
/ /
C o n s t a n t
ambient
l i g h t
c o l o r
uniform
f l o a t 3
a m b i e n t L i g h t C o l o r ;
/ /
M a t e r i a l
g l o s s i n e s s
( phong
exponent )
uniform
f l o a t
s p e c E x p o n e n t ;
/ /
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 we assume
t h a t
d i f f u s e
/ /
and
spec
maps
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 D i f f
= tex2D ( d i f f u s e M a p ,
i n p u t . uv ) ;
Search WWH ::




Custom Search