Game Development Reference
In-Depth Information
Computing barycentric coordinates for an arbitrary point p in 3D is
more complicated than in 2D. We cannot solve a system of equations as we
did before, since we have three unknowns and four equations (one equation
for each coordinate of p , plus the normalization constraint on the barycen-
tric coordinates). Another complication is that p may not lie in the plane
that contains the triangle, in which case the barycentric coordinates are
undefined. For now, let's assume that p lies in the plane containing the
triangle.
One trick that works is to turn the 3D problem into a 2D problem simply
by discarding one of x, y, or z. This has the effect of projecting the triangle
onto one of the three cardinal planes. Intuitively, this works because the
projected areas are proportional to the original areas.
But which coordinate should we discard? We can't just always discard
the same one, since the projected points will be collinear if the triangle is
perpendicular to the projection plane. If our triangle is nearly perpendic-
ular to the plane of projection, we will have problems with floating point
accuracy. A solution to this dilemma is to choose the plane of projection
so as to maximize the area of the projected triangle. This can be done
by examining the plane normal, and whichever coordinate has the largest
absolute value is the coordinate that we will discard. For example, if the
normal is [0.267,−0.802,0.535] then we would discard the y values of the
vertices and p , projecting onto the xz-plane. The code snippet in List-
ing 9.6 shows how to compute the barycentric coordinates for an arbitrary
3D point.
bool
c o m p u t e B a r y c e n t r i c C o o r d s 3 d (
c o n s t
V e c t o r 3
v [ 3 ] ,
/ /
v e r t i c e s
o f
t h e
t r i a n g l e
c o n s t
V e c t o r 3
&p ,
/ /
p o i n t
t h a t
we wish
t o
compute
c o o r d s
f o r
f l o a t
b [ 3 ]
/ /
b a r y c e n t r i c
c o o r d s
r e t u r n e d
h e r e
)
{
/ /
F i r s t ,
compute
two
c l o c k w i s e
edge
v e c t o r s
V e c t o r 3
d1
=
v [ 1 ]
v [ 0 ] ;
V e c t o r 3
d2
=
v [ 2 ]
v [ 1 ] ;
/ /
Compute
s u r f a c e
normal
u s i n g
c r o s s
p r o d u c t .
I n
many
c a s e s
/ /
t h i s
s t e p
c o u l d
be
s k i p p e d ,
s i n c e
we would
have
t h e
s u r f a c e
/ /
normal
precomputed .
We do
n o t
need
t o
n o r m a l i z e
i t ,
a l t h o u g h
/ /
i f
a
precomputed
normal
was
n o r m a l i z e d ,
i t
would
be OK.
V e c t o r 3
n
=
c r o s s P r o d u c t ( d1 ,
d2 ) ;
/ /
L o c a t e
dominant
a x i s
o f
normal ,
and
s e l e c t
p l a n e
o f
p r o j e c t i o n
f l o a t
u1 ,
u2 ,
u3 ,
u4 ;
f l o a t
v1 ,
v2 ,
v3 ,
v4 ;
i f
( ( f a b s ( n . x ) > =
f a b s ( n . y ) )
&&
( f a b s ( n . x ) > =
f a b s ( n . z ) ) )
{
/ /
D i s c a r d
x ,
p r o j e c t
onto
yz
p l a n e
u1
=
v [ 0 ] . y
v [ 2 ] . y ;
u2
=
v [ 1 ] . y
v [ 2 ] . y ;
u3
=
p . y
v [ 0 ] . y ;
u4
=
p . y
v [ 2 ] . y ;
Search WWH ::




Custom Search