Game Development Reference
In-Depth Information
f l o a t
s 1
=
v1 . u
v0 . u ;
f l o a t
s 2
=
v2 . u
v0 . u ;
f l o a t
t 1
=
v1 . v
v0 . v ;
f l o a t
t 2
=
v2 . v
v0 . v ;
/ /
Compute
b a s i s
v e c t o r s
f o r
t h i s
t r i a n g l e
V e c t o r 3
t a n g e n t
=
t 2 q1
t 1 q2 ;
t a n g e n t . n o r m a l i z e ( ) ;
V e c t o r 3
b i n o r m a l
= s 2 q1
+
s 1 q2 ;
b i n o r m a l . n o r m a l i z e ( ) ;
/ /
Add them
i n t o
t h e
r u n n i n g
t o t a l s
f o r
n e i g h b o r i n g
v e r t s
f o r
( i n t j = 0 ; j < 3 ; ++ j ) {
t e m p T a n g e n t [ t r i . v e r t e x I n d e x [ j ] ]
+=
t a n g e n t ;
t e m p B i n o r m a l [ t r i . v e r t e x I n d e x [ j ] ]
+=
b i n o r m a l ;
}
}
/ / Now
f i l l
i n
t h e
v a l u e s
i n t o
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 )
{
V e r t e x
&v
=
v e r t e x L i s t [ i ] ;
V e c t o r 3
t
=
t e m p T a n g e n t [ i ] ;
/ /
E n s u r e
t a n g e n t
i s
p e r p e n d i c u l a r
t o
t h e
normal .
/ /
( Gram S c h m i t ) ,
t h e n
keep
n o r m a l i z e d
v e r s i o n
t
=
v . n o r m a l
d o t ( t ,
v . n o r m a l ) ;
t . n o r m a l i z e ( ) ;
v . t a n g e n t
=
t ;
/ /
F i g u r e
o u t
i f
we ' r e
m i r r o r e d
i f
( d o t ( c r o s s ( v . normal ,
t ) ,
t e m p B i n o r m a l [ i ] ) < 0 . 0 f )
{
v . d e t
=
1.0 f ;
/ /
we ' r e
m i r r o r e d
}
e l s e {
v . d e t
=
+ 1 . 0 f ;
/ /
n o t
m i r r o r e d
}
}
/ /
Clean
up
d e l e t e [ ]
t e m p T a n g e n t ;
d e l e t e [ ]
t e m p B i n o r m a l ;
}
} ;
Listing 10.9
Simple method for calculating basis vectors as the average of adjacent triangle normals
One irritating complication that Listing 10.9 doesn't address is that
there may be a discontinuity in the mapping, where the basis vectors should
not be averaged together, and the basis vectors must be different across a
shared edge. Most of the time, the faces will have already be detached
from each other (the vertices will be duplicated) along such an edge, since
the UV coordinates or normals will not match. Unfortunately, there is
one particularly common case where this is not true: mirrored textures on
symmetric objects. For example, it is common for character models and
other symmetric meshes to have a line down their center, across which the
texture has been mirrored. The vertices along this seam very often require
identical UVs but an opposite u or v . These vertices must be detached in
order to avoid producing invalid basis vectors along this seam.
Search WWH ::




Custom Search