Game Development Reference
In-Depth Information
v1
=
v [ 0 ] . z
v [ 2 ] . z ;
v2
=
v [ 1 ] . z
v [ 2 ] . z ;
v3
=
p . z
v [ 0 ] . z ;
v4
=
p . z
v [ 2 ] . z ;
}
e l s e
i f
( f a b s ( n . y ) > =
f a b s ( n . z ) )
{
/ /
D i s c a r d
y ,
p r o j e c t
onto
xz
p l a n e
u1
=
v [ 0 ] . z
v [ 2 ] . z ;
u2
=
v [ 1 ] . z
v [ 2 ] . z ;
u3
=
p . z
v [ 0 ] . z ;
u4
=
p . z
v [ 2 ] . z ;
v [ 0 ] . x
v1
=
v [ 2 ] . x ;
v [ 1 ] . x
v2
=
v [ 2 ] . x ;
v3
=
p . x
v [ 0 ] . x ;
v4
=
p . x
v [ 2 ] . x ;
}
e l s e
{
/ /
D i s c a r d
z ,
p r o j e c t
onto
xy
p l a n e
u1
=
v [ 0 ] . x
v [ 2 ] . x ;
u2
=
v [ 1 ] . x
v [ 2 ] . x ;
u3
=
p . x
v [ 0 ] . x ;
u4
=
p . x
v [ 2 ] . x ;
v1
=
v [ 0 ] . y
v [ 2 ] . y ;
v2
=
v [ 1 ] . y
v [ 2 ] . y ;
v3
=
p . y
v [ 0 ] . y ;
v4
=
p . y
v [ 2 ] . y ;
}
/ /
Compute
denominator ,
check
f o r
i n v a l i d
f l o a t
denom
=
v1 u2
v2 u1 ;
i f
( denom
==
0 . 0 f )
{
/ /
Bogus
t r i a n g l e
p r o b a b l y
t r i a n g l e
has
z e r o
a r e a
r e t u r n
f a l s e ;
}
/ /
Compute
b a r y c e n t r i c
c o o r d i n a t e s
f l o a t
oneOverDenom
=
1 . 0 f
/
denom ;
b [ 0 ]
=
( v4 u2
v2 u4 )
oneOverDenom ;
b [ 1 ]
=
( v1 u3
v3 u1 )
oneOverDenom ;
b [ 2 ]
=
1 . 0 f
b [ 0 ]
b [ 1 ] ;
/ / OK
r e t u r n
t r u e ;
}
Listing 9.6
Computing barycentric coordinates in 3D
Another technique for computing barycentric coordinates in 3D is based
on the method for computing the area of a 3D triangle using the cross
product, which is discussed in Section 9.6.2. Recall that given two edge
vectors e 1 and e 2 of a triangle, we can compute the area of the triangle as
/2. Once we have the area of the entire triangle and the areas of
the three subtriangles, we can compute the barycentric coordinates.
e 1
×
e 2
Search WWH ::




Custom Search