Graphics Reference
In-Depth Information
R 1
R 2
Figure 5.22 Ray R 1 does not intersect the box because its intersections with the x slab and
the y slab do not overlap. Ray R 2 does intersect the box because the slab intersections overlap.
A test for intersecting a ray against a box therefore only needs to compute the
intersection intervals of the ray with the planes of the slabs and perform some simple
comparison operations to maintain the logical intersection of all intersection intervals
along the ray. All that is required is to keep track of the farthest of all entries into a
slab, and the nearest of all exits out of a slab. If the farthest entry ever becomes farther
than the nearest exit, the ray cannot be intersecting the slab intersection volume and
the test can exit early (with a result of nonintersection).
The intersection intervals of the ray with the slabs are obtained by inserting the
parametric equation of the ray, R ( t )
=
+
P
t d , into the plane equations of the slab
n i ) ( d
·
=
=
·
·
planes, X
n i
d i , and solving for t , giving t
( d
P
n i ). For an AABB, two
components of the normal are zero, and thus given P
=
( p x , p y , p z ) and d
=
( d x , d y , d z )
p x ) d x for a plane perpendicular to
the x axis, where d simply corresponds to the position of the plane along the x axis.
To avoid a division by zero when the ray is parallel to a slab, this case is best handled
separately by substituting a test for the ray origin being contained in the slab. The
following code is an implementation of the test of a ray against an AABB.
the expression simplifies to, for example, t
=
( d
// Intersect ray R(t)=p+t*dagainst AABB a. When intersecting,
// return intersection distance tmin and point q of intersection
int IntersectRayAABB(Point p, Vector d, AABB a, float &tmin, Point &q)
{
tmin = 0.0f;
// set to -FLT_MAX to get first hit on line
float tmax = FLT_MAX;
// set to max distance ray can travel (for segment )
 
Search WWH ::




Custom Search