Game Development Reference
In-Depth Information
/ / Get s h o r t c u 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 ] ;
/ /
Compute
t r i a n g l e
normal .
V e c t o r 3
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 ] ] . p os ;
V e c t o r 3
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 ] ] . p os ;
V e c t o r 3
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 ] ] . p os ;
t r i . n o r m a l
=
c r o s s ( v1 v0 ,
v2 v1 ) ;
t r i . n o r m a l . n o r m a l i z e ( ) ;
/ /
Sum
i t
i n t o
t h e
a d j a c e n t
v e r t i c e s
f o r
( i n t j = 0 ; j < 3 ; ++ j ) {
v e r t e x L i s t [ t r i . v e r t e x I n d e x [ j ] ] . n o r m a l
+=
t r i . n o r m a l ;
}
}
/ /
F i n a l l y ,
a v e r a g e
and
n o r m a l i z e
t h e
r e s u l t s .
/ /
Note
t h a t
t h i s
can
blow
up
i f
a
v e r t e x
i s
i s o l a t e d
/ /
( n o t
used
by
any
t r i a n g l e s ) ,
and
i n
some
o t h e r
c a s 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 L i s t [ i ] . n o r m a l . n o r m a l i z e ( ) ;
}
}
} ;
Listing 10.6
Simple method for calculating vertex normals as the average of adjacent face normals
Averaging face normals to compute vertex normals is a tried-and-true
technique that works well in most cases. However, there are a few things
to watch out for. The first is that sometimes the mesh is supposed to
have a discontinuity, and if we're not careful, this discontinuity will get
“smoothed out.” Take the very simple example of a box. There should
be a sharp lighting discontinuity at its edges. However, if we use vertex
normals computed from the average of the surface normals, then there is
no lighting discontinuity, as shown in Figure 10.14.
Figure 10.14
On the right, the box edges
are not visible because there is
only one normal at each
corner
Search WWH ::




Custom Search