Non-Confidential | PDF version | ARM 100140_0201_00_en | ||

| ||||

Home > Advanced Graphics Techniques > Implementing reflections with a local cubemap > Ray-box intersection algorithm |

This section describes the ray-box intersection algorithm.

The following figure shows a graph with line equations:

Equation of a line

y = mx + b

The vector form of this equation is:

r = O + t*D

Where:

`O`

is the origin point`D`

is the direction vector`t`

is the parameterThe following figure shows an axis aligned bounding box:

An axis aligned bounding box

`AABB`

can
be defined by its min and max points `A`

and `B`

`AABB`

defines a set of lines parallel to
coordinate axis. Each component of line can be defined by the following equation: x = A_{x}; y = A_{y}; z = A_{z}x = B_{x}; y = B_{y}; z = B_{z}

To find where a ray intersects one of those lines, equal both equations.
For example:

O_{x}+ t_{x}*D_{x}= A_{x}

You can write the solution as:

t_{Ax}= (A_{x}– O_{x}) / D_{x}

Obtain the solution for all components of both intersection points in
the same manner:

t_{Ax}= (Ax – Ox) / Dx t_{Ay}= (Ay – Oy) / Dy t_{Az}= (Az – Oz) / Dz t_{Bx}= (Bx – Ox) / Dx t_{By}= (By – Oy) / Dy t_{Bz}= (Bz – Oz) / Dz

In vector form these are:

t_{A}= (A – O) / D t_{B}= (B – O) / D

This finds where the line intersects the planes defined by the faces of
the cube but it does not guarantee that the intersections lie on the cube.

The following figure shows a 2D representation of ray-box intersection:

To find what solution is really an intersection with the box, you
require the greater value of the

`t`

parameter for the
intersection at the min plane.t_{min}= (t_{Ax}> t_{Ay}) ? t_{Ax}: t_{Ay}

You require the smaller value of the parameter

`t`

for the intersection at the max plane.t_{min}= (t_{Ax}> t_{Ay}) ? t_{Ax}: t_{Ay}

You also must consider those cases when you get no intersections.

The following figure shows a ray-box with no intersection:

If you guarantee that the reflective surface is enclosed by the BBox,
that is, the origin of the reflected ray is inside the BBox, then there are always two
intersections with the box, and the handling of different cases is simplified.

The following figure shows a ray-box intersection in BBox: