Game Development Reference
In-Depth Information
Since we are planning on normalizing our basis vectors, we can drop the
leading constant fraction, and we are left with
u = t 2 q 1 − t 1 q 2 ,
v = −s 2 q 1 + s 1 q 2 .
This gives us basis vectors for each triangle. They are not guaranteed
to be perpendicular, but they are usable for our main purpose: determining
basis vectors at the vertex level. These can be calculated by using a trick
similar to computing vertex normals: for each vertex we take the average
of the basis vectors of the adjacent triangles. We also usually enforce an
orthonormal basis. This is done most simply via Gram-Schmidt orthogo-
nalization (Section 6.3.3). Also, if we are dropping one of the basis vectors,
then this is where we need to save the determinant of the basis. Listing 10.9
shows how we might compute vertex basis vectors.
s t r u c t
V e r t e x
{
V e c t o r 3
po s ;
f l o a t
u , v ;
V e c t o r 3
n o r m a l ;
V e c t o r 3
t a n g e n t ;
f l o a t
d e t ;
/ /
d e t e r m i n a n t
o f
t a n g e n t
t r a n s f o r m .
( 1
i f
m i r r o r e d )
} ;
s t r u c t
T r i a n g l e
{
i n t
v e r t e x I n d e x [ 3 ] ;
} ;
s t r u c t
T r i a n g l e M e s h
{
i n t
v e r t e x C o u n t ;
V e r t e x
v e r t e x L i s t ;
i n t
t r i a n g l e C o u n t ;
T r i a n g l e
t r i a n g l e L i s t ;
v o i d
c o m p u t e B a s i s V e c t o r s ( )
{
/ /
Note :
we assume
v e r t e x
n o r m a l s
a r e
v a l i d
V e c t o r 3
t e m p T a n g e n t
= new
V e c t o r 3 [ v e r t e x C o u n t ] ;
V e c t o r 3
t e m p B i n o r m a l
= new
V e c t o r 3 [ v e r t e x C o u n t ] ;
/ /
F i r s t
c l e a r
o u t
t h e
a c c u m u l a t o r s
f o r
( i n t
i
=
0
;
i < v e r t e x C o u n t
;
++ i )
{
t e m p T a n g e n t [ i ] . z e r o ( ) ;
t e m p B i n o r m a l [ i ] . z e r o ( ) ;
}
/ /
Average
i n
t h e
b a s i s
v e c t o r s
f o r
each
f a c e
/ /
i n t o
i t s
n e i g h b o r i n g
v e r t i c e s
f o r
( i n t
i
=
0
;
i < t r i a n g l e C o u n t
;
++ i )
{
/ /
Get
s h o r t c u t s
c o n s t
T r i a n g l e
& t r i
=
t r i a n g l e L i s t [ i ] ;
c o n s t
V e r t e x
&v0
=
v e r t e x L i s t [ t r i . v e r t e x I n d e x [ 0 ] ] ;
c o n s t
V e r t e x
&v1
=
v e r t e x L i s t [ t r i . v e r t e x I n d e x [ 1 ] ] ;
c o n s t
V e r t e x
&v2
=
v e r t e x L i s t [ t r i . v e r t e x I n d e x [ 2 ] ] ;
/ /
Compute
i n t e r m e d i a t e
v a l u e s
V e c t o r 3
q1
=
v1 . po s
v0 . po s ;
V e c t o r 3
q2
=
v2 . po s
v0 . po s ;
Search WWH ::




Custom Search