Game Development Reference
In-Depth Information
Listing 10.7 illustrates this basic technique. Note that we are also plan-
ning ahead by including vertex normals. These are handled in the same
way as vertex positions, only we discard the translation portion of the ma-
trix. In theory, the same matrix should not be used to transform positions
and normals. Remember that if nonuniform scale or skew is included in the
matrix, we really should use the inverse transpose matrix, as was discussed
in Section 10.4.2. In practice, however, computing and sending two sets
of matrices to the GPU is too expensive, so for the sake of e ciency this
error is ignored, or nonuniform scale is simply avoided. (Uniform scale is
typically OK because the normals have to be renormalized anyway.) Basis
vectors for bump mapping are also commonly part of the process, but they
are handled in a manner very similar to normals, so we will leave those out
for now.
/ /
S e t
a
l i m i t
on
t h e
max number
o f
bones
t h a t
can
i n f l u e n c e
one
v e r t e x
c o n s t
i n t
k M a x B o n e s P e r V e r t e x
=
4 ;
/ /
D e s c r i b e s
a
v e r t e x
i n
a
s k e l e t a l
model
s t r u c t
S k i n n e d V e r t e x
{
/ /
Number
o f
bones
t h a t
i n f l u e n c e
t h i s
v e r t e x
i n t
boneCount ;
/ /
Which
bones
i n f l u e n c e
t h e
v e r t e x ?
These
a r e
i n d i c e s
/ /
i n t o
a
l i s t
o f
bones .
i n t
b o n e I n d e x [ k M a x B o n e s P e r V e r t e x ] ;
/ /
Bone
w e i g h t s .
These
must
sum
t o
1
f l o a t
boneWeight [ k M a x B o n e s P e r V e r t e x ] ;
/ /
V e r t e x
p o s i t i o n
and
normal ,
i n
bone
space
V e c t o r 3
p o s I n B o n e S p a c e [ k M a x B o n e s P e r V e r t e x ] ;
V e c t o r 3
n o r m a l I n B o n e S p a c e [ k M a x B o n e s P e r V e r t e x ] ;
} ;
/ /
D e s c r i b e s
a
v e r t e x
a s
we
w i l l
use
i t
f o r
r e n d e r i n g
s t r u c t
V e r t e x
{
V e c t o r 3
po s ;
V e c t o r 3
n o r m a l ;
} ;
/ /
Compute
s k i n n e d
v e r t e x
p o s i t i o n s
and
n o r m a l s .
v o i d
c o m p u t e S k i n n e d V e r t i c e s (
i n t
v e r t e x C o u n t ,
/ /
number
o f
v e r t s
t o
s k i n
c o n s t
S k i n n e d V e r t e x
i n S k i n V e r t L i s t ,
/ /
i n p u t
v e r t
l i s t
c o n s t
M a t r i x 4 x 3
b o n e T o M o d e l L i s t ,
/ /
Pos / o r i e n t
o f
each
bone
V e r t e x
o u t V e r t L i s t
/ /
o u t p u t
goes
h e r e
)
{
/ /
I t e r a t e
o v e r
a l l
t h e
v e r t i c e s
f o r
( i n t
i
=
0
;
i < v e r t e x C o u n t
;
++ i )
{
c o n s t
S k i n n e d V e r t e x
&s
=
i n S k i n V e r t L i s t [ i ] ;
V e r t e x
&d
=
o u t V e r t L i s t [ i ] ;
/ /
Loop
o v e r
a l l
bones
t h a t
i n f l u e n c e
t h i s
v e r t e x ,
and
/ /
compute
w e i g h t e d
a v e r a g e
Search WWH ::




Custom Search