Game Development Reference
In-Depth Information
/ /
Model −> view
t r a n s f o r m
m a t r i x .
( The
” modelview ”
m a t r i x )
uniform
f l o a t 4 x 4
modelToView ;
/ /
C l i p
m a t r i x .
( The
” p r o j e c t i o n ”
m a t r i x ) .
uniform
f l o a t 4 x 4
v i e w T o C l i p ;
/ /
Omni
l i g h t
p o s i t i o n ,
i n
VIEW
space ,
and
r e c i p r o c a l
o f
/ /
f a l l o f f
i n
t h e w component
uniform
f l o a t 4
omniPosAndInvRad ;
/ /
U n a t t e n u a t e d
omni
l i g h t
c o l o r
uniform
f l o a t 3
o m n i C o l o r ;
/ /
The
body
o f
our
v e r t e x
s h a d e r
{
O u t p u t
main ( I n p u t
i n p u t )
O u t p u t
o u t p u t ;
/ /
T r a n s f o r m
v e r t e x
p o s i t i o n
t o
view
space .
f l o a t 4
v P o s
= mul ( i n p u t . pos ,
modelToView ) ;
/ /
And
i n t o
c l i p
space .
Note
t h a t
t h e
c l i p
m a t r i x
/ /
o f t e n
has
a
s i m p l e
s t r u c t u r e
which
can
be
e x p l o i t e d
/ /
and
t h e
number
o f
v e c t o r
o p e r a t i o n s
can
be
reduced .
o u t p u t . c l i p P o s
= mul ( vPos ,
v i e w T o C l i p ) ;
/ /
T r a n s f o r m
normal
t o
camera
space .
We ” promote ”
t h e
normal
/ /
t o
f l o a t 4
by
s e t t i n g
w t o
0 ,
so
i t
w i l l
r e c e i v e
any
t r a n s l a t i o n
o u t p u t . n o r m a l
= mul ( f l o a t 4 ( i n p u t . normal , 0 ) ,
modelToView ) ;
/ /
Compute
v e c t o r
t o
l i g h t
f l o a t 3
L
=
omniPosAndInvRad . x y z
v 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 .
/ /
Note
t h a t
t h e
view
p o s i t i o n
i s
t h e
o r i g i n ,
/ /
i n
view
space ,
by
d e f i n i t i o n
f l o a t 3
V
=
n o r m a l i z e ( v 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 .
1
d i s t omniPosAndInvRad .w;
f l o a t
a t t e n F a c t o r
=
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 UV' 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.17
Vertex shader for per-pixel lighting of a single omni plus ambient, calculated in camera space
World space (“upright space”) is an attractive option for lighting calcu-
lations in many circumstances because shadow cube maps or lighting probes
are usually rendered in this orientation; it also has the advantage that we
Search WWH ::




Custom Search