Game Development Reference
In-Depth Information
/ /
T h i s
s t r u c t
d e c l a r e s
t h e
i n p u t s
we
r e c e i v e
from
t h e
mesh .
/ /
Note
t h a t
t h e
o r d e r
h e r e
i s
n o t
i m p o r t a n t .
I n p u t s
a r e
i d e n t i f i e d
/ /
by
t h e i r
” s e m a n t i c ” ,
which
i s
t h e
t h i n g
on
t h e
r i g h t
a f t e r
t h e
/ /
c o l o n .
When
s e n d i n g
t h e
v e r t e x
l i s t
t o
t h e
r e n d e r e r ,
we must
/ /
s p e c i f y
t h e
” s e m a n t i c ”
o f
each
v e r t e x
e l e m e n t .
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
modeling
space
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d i n a t e s
} ;
/ /
T h i s
i s
t h e
d a t a
we
w i l l
o u t p u t
from
our
v e r t e x
s h a d e r .
These
/ /
a r e
matched
up
w i t h
t h e
p i x e l
s h a d e r
i n p u t s
based
on
t h e
s e m a n t i c
s t r u c t
O u t p u t
{
f l o a t 4
p o s
:
POSITION ;
/ /
p o s i t i o n
i n
CLIP
space
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d i n a t e
} ;
/ /
Here we
d e c l a r e
a
g l o b a l
v a r i a b l e ,
which
i s
a
s h a d e r
c o n s t a n t
/ /
t h a t
h o l d s
t h e
model −> c l i p
t r a n s f o r m
m a t r i x .
uniform
f l o a t 4 x 4
m o d e l T o C l i p ;
/ /
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
c l i p
space .
The
mul ( )
i n t r i n s i c
/ /
f u n c t i o n
p e r f o r m s
m a t r i x
m u l t i p l i c a t i o n .
Note
t h a t
mul ( )
/ /
t r e a t s
any
v e c t o r
p a s s e d
a s
t h e
f i r s t
operand
a s
a
row
v e c t o r .
o u t p u t . po s
= mul ( i n p u t . pos ,
m o d e l T o C l i p ) ;
/ /
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.11
Vertex shader for decal rendering
A vertex shader like this could be be used with the pixel shader of
Listing 10.12, which actually does the decal shading. However, to make
things interesting and demonstrate that pixel shader constants work the
same as vertex shader constants, we've added a global constant color, which
we consider to be part of the global render context. We have found it very
useful to have a constant such as this, which modulates the color and opacity
of every rendered primitive.
/ /
T h i s
s t r u c t
d e c l a r e s
t h e
i n t e r p o l a t e d
i n p u t s
we
r e c e i v e
from
t h e
/ /
r a s t e r i z e r .
They
w i l l
u s u a l l y
match
t h e
v e r t e x
o u t p u t s
e x a c t l y ,
/ /
e x c e p t
t h a t
we
o f t e n
l e a v e
o f f
t h e
c l i p space
p o s i t i o n .
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 i n a t e s
} ;
/ /
Here ,
j u s t
t o
show how a
p i x e l
s h a d e r
c o n s t a n t
works ,
we
d e c l a r e
/ /
a
g l o b a l
c o n s t a n t
c o l o r .
The
o u t p u t
o f
our
s h a d e r
i s
m u l t i p l i e d
/ /
by
t h i s
RGBA v a l u e .
One
o f
t h e
most common
r e a s o n s
t o
have
such
a
/ /
c o n s t a n t
i s
t o
add
an
o p a c i t y
s e t t i n g
i n t o
t h e
r e n d e r
c o n t e x t ,
/ /
b u t
i t ' s
v e r y
handy
t o
have
a
f u l l
RGBA
c o n s t a n t
c o l o r .
Search WWH ::




Custom Search