Game Development Reference
In-Depth Information
10.11.5
Skinned Mesh
Now for some examples of skeletal rendering. All of the skinning happens in
the vertex shaders, and so we will not need to show any pixel shaders here;
the vertex shaders here can be used with the pixel shaders given previously.
This is not unusual: skinned and unskinned geometry can usually share the
same pixel shader. We give two examples. The first example (Listing 10.23)
illustrates per-pixel lighting of our omni + ambient lighting rig. We will do
all the lighting in the pixel shader (Listing 10.14), so that we can focus on
the skinning, which is what is new.
/ /
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 ;
/ /
model
space
p o s i t i o n
( b i n d i n g
pose )
f l o a t 3
n o r m a l
: NORMAL ;
/ /
model
space
v e r t e x
normal
( d i t t o )
by te 4
bones
:
BLENDINDICES ;
/ /
Bone
i n d i c e s .
Unused
e n t r i e s
a r e
0
f l o a t 4
w e i g h t
:
BLENDWEIGHT ;
/ /
Blend
w e i g h t s .
Unused
e n t r i e s
a r e
0
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
( f o r
r a s t e r i z a t i o n )
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
modelPos
:
TEXCOORD2 ;
/ /
p o s i t i o n
i n
model
space
( f o r
l i g h t i n g )
} ;
/ /
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 ;
/ /
D e c l a r e
an
a r b i t r a r y
max number
o f
bones .
# d e f i n e
MAX BONES
40
/ /
A r r a y
o f
' ' b i n d i n g
pose −> c u r r e n t ' '
pose
m a t r i c e s
f o r
each
bone .
/ /
These
a r e
4x3
m a t r i c e s ,
which we
i t e r p r e t
a s
4x4
m a t r i c e s
w i t h
t h e
/ /
r i g h t m o s t
column
assumed
t o
be
[ 0 , 0 , 0 , 1 ] .
Note we
a r e
a s su m i n g
/ /
t h a t
column major
i s
t h e
d e f a u l t
s t o r a g e
−−−
meaning
each
column
/ /
i s
s t o r e d
i n
a
4D
r e g i s t e r .
Thus
each
m a t r i x
t a k e s
3
r e g i s t e r s .
uniform
f l o a t 4 x 3
b o n e M a t r i x [ MAX BONES ] ;
/ /
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 ;
/ /
G e n e r a t e
a
blended
m a t r i x .
N o t i c e
t h a t
we
a l w a y s
b le nd
4
bones ,
/ /
even
though
most
v e r t i c e s
w i l l
use
f e w e r
bones .
Whether
i t s
/ /
f a s t e r
t o
use
c o n d i t i o n a l
l o g i c
t o
t r y
t o
b y p a s s
t h i s
e x t r a
l o g i c ,
/ /
o r
i f
i t ' s
b e t t e r
t o
j u s t
t o
a l l
o f
c a l c u l a t i o n s
( which
can
be
/ /
e a s i l y
s c h e d u l e d
by
t h e
a s s e m b l e r
t o
h i d e
any
i n s t r u c t i o n
/ /
l a t e n c y )
w i l l
depend
on
t h e
hardware .
f l o a t 4 x 3 b l e n d e d M a t =
b o n e M a t r i x [ i n p u t . bones . x ] i n p u t . w e i g h t . x
+
b o n e M a t r i x [ i n p u t . bones . y ] i n p u t . w e i g h t . y
+
b o n e M a t r i x [ i n p u t . bones . z ] i n p u t . w e i g h t . z
+
b o n e M a t r i x [ i n p u t . bones .w] i n p u t . w e i g h t .w;
/ /
P e r f o r m
s k i n n i n g
t o
t r a n s f o r m
p o s i t i o n
and
normal
/ /
from
t h e i r
b i n d i n g
pose
p o s i t i o n
i n t o
t h e
p o s i t i o n
 
Search WWH ::




Custom Search