Home

Ray-Tracing: Rendering a Triangle

Distributed under the terms of the CC BY-NC-ND 4.0 License.

  1. Why Are Triangles Useful?
  2. Geometry of a Triangle
  3. Ray-Triangle Intersection: Geometric Solution
  4. Single vs Double Sided Triangle and Backface Culling
  5. Barycentric Coordinates
  6. Möller-Trumbore algorithm
  7. Source Code (external link GitHub)

Möller-Trumbore algorithm

Reading time: 24 mins.

Möller-Trumbore algorithm

The Möller-Trumbore (or MT for the remainder of this lesson) algorithm is a fast ray-triangle intersection algorithm that was introduced in 1997 by Tomas Möller and Ben Trumbore in a paper titled "Fast, Minimum Storage Ray/Triangle Intersection". It is still considered today a fast algorithm that is often used in benchmarks to compare the performances of other methods. However, a fair comparison of ray-triangle intersection algorithms is difficult because speed can depend on many factors, such as how algorithms are implemented, the type of test scene used, whether values are precomputed, etc.

The Möller-Trumbore algorithm takes advantage of the parameterization of P, the intersection point in barycentric coordinates (which we discussed in the previous chapter). We learned in the previous chapter to calculate P, the intersection point, using the following equation:

$$P = wA + uB + vC$$

We also learned that \(w=1-u-v\); thus, we can write:

$$P = (1 - u - v)A + uB + vC$$

Note that the paper by Möller-Trumbore uses the convention \( P = (1 - u - v)A + uB + vC \) instead of \( P = uA + vB + (1 - u - v)C \), which we've been using in the chapter on barycentric coordinates. As I mentioned in that chapter, the way you pair the barycentric coordinates and the vertices is entirely up to you, as long as the edges you use in the calculation of \( u \), \( v \), and \( w \) are adapted to the pairing convention you've chosen.

As explained in that chapter, the pairing convention chosen by Möller-Trumbore comes from the fact that you can write \( P = A + uAB + vAC \), which can be interpreted as: "start from \( A \), the origin, and then move along the line \( AB \) by \( u \), as well as along the line \( AC \) by \( v \), to find \( P \)." If you expand the vectors \( AB \) and \( AC \) as \( B - A \) and \( C - A \), respectively, you get:

$$ \begin{array}{l} P = A + u(B - A) + v(C - A) \\ P = A + uB - uA + vC - vA \\ P = (1 - u - v)A + uB + vC \end{array} $$

This is the formulation used by Möller-Trumbore, and this derivation explains why it is a convention commonly used in research papers and books, as opposed to the \( uA + vB + wC \) formulation that we've used in the chapter on barycentric coordinates, which is also frequently found in code.

I will stick to the convention used in the paper so that the lesson and the paper remain consistent. Bottom line: stay aware of this pairing issue. Let's move on with the rest of the demonstration.

If we develop, we get (equation 1):

$$ \begin{equation} P=A - uA - vA + uB + vC = A + u(B - A) + v(C - A) \end{equation} $$

Note that \( (B - A) \) and \( (C - A) \) are the edges \( AB \) and \( AC \) of the triangle \( \triangle ABC \). The intersection point \( P \) can also be written using the ray's parametric equation (Equation 2).

$$ \begin{equation} P=O+tD. \end{equation} $$

Where \(t\) is the distance from the ray's origin to the intersection \(P\). If we replace \(P\) in equation 1 with the ray's equation, we get (equation 3):

$$ \begin{equation} \begin{array}{l} O+tD & = & A + u(B - A) + v(C - A)\\ O-A & = & -tD+u(B-A)+v(C-A) \end{array} \end{equation} $$

On the right side of the equal sign, we have:

We can rearrange these terms and present equation 3 using the following notation (equation 4):

$$ \begin{equation} \begin{bmatrix} -D & (B-A) & (C-A) \end{bmatrix} \begin{bmatrix} t\\ u\\ v \end{bmatrix} =O-A \end{equation} $$

The left side of the equation has been rearranged into a row-column vector multiplication (1x3*3x1). This is the simplest possible form of matrix multiplication. You take the first element of the row matrix \(-D\), \(B-A\), \(C-A\) and multiply it by the first element of the column vector (\(t\), \(u\), \(v\)). Then you add the raw matrix's second element multiplied by the column vector's second element. Then finally, add the third element of the raw matrix multiplied by the third element of the column vector (which gives you equation 3 again).

In equation 3, the term \(O-A\) on the left side of the equal sign is a vector. \(B-A\), \(C-A\), and \(D\) are vectors as well, and \(t\), \(u\), and \(v\) (unknown) are scalars (floats or doubles if you prefer to think in terms of programming). This equation is about vectors. It combines three vectors in quantities defined by \(t\), \(u\), and \(v\). It gives another vector as a result of this operation (be aware that in their paper, Möller and Trumbore use the term x-, y- and z-axis instead of \(t\), \(u\) and \(v\) when they explain the geometrical meaning of this equation).

Figure 1: we can express the position of P in t, u, v space. t indicates the distance from P to the ray origin and is parallel to the y-axis. If P lies in the unit triangle, it means that 0 <= u <= 1, 0 <= v <= 1, and u+v <=1.

Let's say that \( P \) is where the ray intersects the triangle \( \triangle ABC \). You will (hopefully) agree that the Cartesian coordinates of point \( P \) (x, y, z) change as we move or rotate the triangle and the ray together. However, the barycentric coordinates of \( P \) are invariant. If the triangle is rotated, scaled, stretched, or translated, the coordinates \( u \) and \( v \), which define the position of \( P \) with respect to vertices \( A \), \( B \), and \( C \), will not change (see Figure 1).

The Möller-Trumbore algorithm takes advantage of this property. Instead of solving the ray-triangle intersection equation using the \(x\), \(y\), and \(z\) coordinates for the intersection point, it expresses the intersection point in terms of the triangle's barycentric coordinates \(u\) and \(v\). A point lying on the triangle (our intersection point, if it exists) can thus be expressed either in Cartesian coordinates \((x, y, z)\) or in barycentric coordinates \((u, v)\).

Finally, as we learned in the previous chapter, let's recall that the \(u\) and \(v\) coordinates of the point lying on the surface of a triangle can't be greater than one nor lower than 0. Their sum can't be greater than one either \(u + v <= 1\). They express coordinates of points defined inside a unit triangle (this is the triangle defined in \(u\), \(v\) space by the vertices (0, 0), (1, 0), (0, 1) as shown in Figure 1).

Let's summarize. What do we have so far? We have reinterpreted the three-dimensional \(x\), \(y\), \(z\) position of point \(P\) in terms of \(u\) and \(v\) barycentric coordinates, effectively moving from one parameter space to another: from xyz-space to uv-space. We have also learned that points defined in uv-space lie within a unit triangle.

Equation 3 (or 4, as they are identical) has three unknowns: \(u\), \(v\), and \(t\). Geometrically, we have already explained the meaning of \(u\) and \(v\). But what about \(t\)? Simply put, we will consider \(t\) as the third axis of the \(u\) and \(v\) coordinate system we just introduced. We now have a coordinate system defined by three axes: \(u\), \(v\), and \(t\).

Geometrically, let's explain what \(t\) represents. We know that \(t\) expresses the distance from the ray origin to \(P\), the intersection point. If you look at Figure 1, you can see that we have created a coordinate system that allows us to fully describe the position of the intersection point \(P\) in terms of barycentric coordinates and the distance from the ray origin to that point on the triangle.

Möller and Trumbore explain that the first part of Equation 4 (the term \( O - A \)) can be interpreted as a transformation that moves the triangle from its original world-space position to the origin (with the first vertex of the triangle coinciding with the origin). The other side of the equation transforms the intersection point from \(x, y, z\) space to "tuv-space," as explained above.

Cramer's Rule

What we need to do now is solve this equation, which has three unknowns: \(t\), \(u\), and \(v\). To solve Equation 4, Möller and Trumbore use a technique known in mathematics as Cramer's rule. Cramer's rule provides the solution to a system of linear equations in terms of determinants. It states that if the multiplication of a matrix \(M\) (a set of three numbers laid out horizontally) by a column vector \(X\) (three numbers laid out vertically) is equal to a column vector \(C\), then it is possible to find \(X_i\) (the \(i\)th element of the column vector \(X\)) by dividing the determinant of \(M_i\) by the determinant of \(M\). Here, \(M_i\) is the matrix formed by replacing the \(i\)th column of \(M\) with the column vector \(C\).

If you are unfamiliar with Cramer's rule, it is explained in the gray box below.

Imagine we need to solve for \(x\), \(y\), and \(z\) using the following set of linear equations:

$$ \begin{array}{l} 2x + 1y + 1z = 3 \\ 1x - 1y - 1z = 0 \\ 1x + 2y + 1z = 0 \end{array} $$

We can rewrite these equations using a matrix representation, where the coefficients on the left become the elements of matrix \(M\), and the numbers on the right of the equal sign become the elements of matrix \(C\):

$$ M = \left[ \begin{array}{rrr} 2 & 1 & 1\\ 1 & -1 & -1\\ 1 & 2 & 1 \end{array} \right] $$ $$ C = \left[ \begin{array}{c} \color{red}{3}\\ \color{red}{0}\\ \color{red}{0} \end{array} \right] $$

Thus, we can express this system of equations as:

$$ M \cdot \begin{bmatrix} x \\ y \\ z \end{bmatrix} = C $$

By replacing the first column in \(M\) with \(C\), we create matrix \(M_x\):

$$ M_x = \left[ \begin{array}{rrr} \color{red}{3} & 1 & 1 \\ \color{red}{0} & -1 & -1 \\ \color{red}{0} & 2 & 1 \end{array} \right] $$

Similarly, we can create \(M_y\) and \(M_z\):

$$ \begin{array}{l} M_y = \left[ \begin{array}{rrr} 2 & \color{red}{3} & 1 \\ 1 & \color{red}{0} & -1 \\ 1 & \color{red}{0} & 1 \end{array} \right] \\ M_z = \left[ \begin{array}{rrr} 2 & 1 & \color{red}{3} \\ 1 & -1 & \color{red}{0} \\ 1 & 2 & \color{red}{0} \end{array} \right] \end{array} $$

Now, let's evaluate each determinant:

$$ \begin{array}{l} \left| M \right| = \left| \begin{array}{rrr} 2 & 1 & 1\\ 1 & -1 & -1\\ 1 & 2 & 1 \end{array} \right| = (-2) + (-1) + (2) - (-1) - (-4) - (1) = 3\\ \left| M_x \right| = \left| \begin{array}{rrr} \color{red}{3} & 1 & 1 \\ \color{red}{0} & -1 & -1 \\ \color{red}{0} & 2 & 1 \end{array} \right| = (-3) + (0) + (0) - (0) - (-6) - (0) = 3\\ \left| M_y \right| = \left| \begin{array}{rrr} 2 & \color{red}{3} & 1 \\ 1 & \color{red}{0} & -1 \\ 1 & \color{red}{0} & 1 \end{array} \right| = (0) + (-3) + (0) - (0) - (0) - (3) = -6\\ \left| M_z \right| = \left| \begin{array}{rrr} 2 & 1 & \color{red}{3} \\ 1 & -1 & \color{red}{0} \\ 1 & 2 & \color{red}{0} \end{array} \right| = (0) + (0) + (6) - (-3) - (0) - (0) = 9 \end{array} $$

Remember that the determinant of a 3x3 matrix:

$$ \begin{vmatrix} a & b & c \\ d & e & f \\ g & h & i \end{vmatrix} $$

has the value:

$$ (aei + bfg + cdh) - (ceg + bdi + afh). $$

Cramer's rule states that \( x = \dfrac{M_x}{M} \), \( y = \dfrac{M_y}{M} \), and \( z = \dfrac{M_z}{M} \). Thus:

$$ \begin{array}{l} x = \dfrac{3}{3} = 1 \\ y = \dfrac{-6}{3} = -2 \\ z = \dfrac{9}{3} = 3 \end{array} $$

In linear algebra, the determinant is denoted by two vertical bars, and it reads: \( \begin{vmatrix} I & J & K \end{vmatrix} \) is the determinant of the matrix having \( I \), \( J \), and \( K \) as its columns. Following the conventions used in the gray box above, you can now see that matrix \( M \) is \( [-D, B - A, C - A] \), \( X \) is \( [t, u, v] \), and \( C \) is \( O - A \) in our system of linear equations. We are interested in finding values for \( t \), \( u \), and \( v \). To understand how we apply Cramer's rule let's recap:

Cramer's rule states that for a system of linear equations \( MX = C \), where \( M \) is a matrix of coefficients, \( X \) is a vector of unknowns, and \( C \) is the result vector, the solution for each component \( X_i \) can be found by replacing the \( i \)-th column of the matrix \( M \) with the vector \( C \), then dividing the determinant of the resulting matrix by the determinant of \( M \).

In this case, the matrix \( M \) is \( [-D, B - A, C - A] \), and the right-hand side vector \( C \) is \( O - A \). So, the solution using Cramer's rule for the unknowns \( t \), \( u \), and \( v \) is:

$$ \begin{bmatrix} t \\ u \\ v \end{bmatrix} = \frac{1}{\text{det}(M)} \begin{bmatrix} \text{det}(M_t) \\ \text{det}(M_u) \\ \text{det}(M_v) \end{bmatrix} $$

Where:

Applying this technique to our problem gives (Equation 5):

$$ \left[ \begin{array}{r} t \\ u \\ v \end{array} \right] = \dfrac{1}{\left| \begin{array}{r} -D & E_1 & E_2 \end{array} \right|} \left[ \begin{array}{c} \left| \begin{array}{c} T & E_1 & E_2 \end{array} \right| \\ \left| \begin{array}{c} -D & T & E_2 \end{array} \right| \\ \left| \begin{array}{c} -D & E_1 & T \end{array} \right| \\ \end{array} \right], $$

where, for brevity, we denote \( T = O - A \), \( E_1 = B - A \), and \( E_2 = C - A \). The equation for each unknown (\( t \), \( u \), and \( v \)) is expressed as the ratio of a determinant.

The next step is to find the values of these four determinants. In the lesson on Geometry, we explained that the determinant of a \(1 \times 3\) matrix \( \begin{vmatrix} A & B & C \end{vmatrix} \) can be calculated as follows (refer to the grey box below for more details on where these equations come from):

$$ |A \ B \ C| = -(A \times C) \cdot B = -(C \times B) \cdot A. $$

The determinant of a \(3 \times 3\) matrix is a scalar triple product, which combines a cross product and a dot product. Therefore, we can rewrite Equation 5 as:

$$ \left[\begin{array}{r}t\\u\\v\end{array}\right] = \frac{1}{(D \times E_2) \cdot E_1} \begin{bmatrix} (T \times E_1) \cdot E_2\\ (D \times E_2) \cdot T\\ (T \times E_1) \cdot D \end{bmatrix} = \frac{1}{P \cdot E_1} \begin{bmatrix} Q \cdot E_2\\ P \cdot T\\ Q \cdot D \end{bmatrix}, $$

where \( P = (D \times E_2) \) and \( Q = (T \times E_1) \).

It is now straightforward to find the values of \( t \), \( u \), and \( v \). We can obtain them using cross and dot products between known variables: the vertices of the triangle, the ray origin, and the ray direction. Very nice!

You may want to check the Wikipedia page on Triple Product for more information, but just in case you are in a hurry, let's recall some basic rules here that will help you understand the following equations:

  • Due to the cyclic permutation nature of the scalar triple product, all the following expressions evaluate to the same value:

    $$ \mathbf{a} \cdot (\mathbf{b} \times \mathbf{c}) = \mathbf{b} \cdot (\mathbf{c} \times \mathbf{a}) = \mathbf{c} \cdot (\mathbf{a} \times \mathbf{b}) $$
  • Swapping the positions of the operators without re-ordering the operands leaves the triple product unchanged. This follows from the preceding property and the commutative property of the dot product:

    $$ \mathbf{a} \cdot (\mathbf{b} \times \mathbf{c}) = (\mathbf{a} \times \mathbf{b}) \cdot \mathbf{c} $$
  • Swapping any two of the three operands negates the triple product. This follows from the anticommutativity property of the cross product:

    $$ \mathbf{a} \cdot (\mathbf{b} \times \mathbf{c}) = -\mathbf{a} \cdot (\mathbf{c} \times \mathbf{b}) = -\mathbf{b} \cdot (\mathbf{a} \times \mathbf{c}) = -\mathbf{c} \cdot (\mathbf{b} \times \mathbf{a}) $$

Using these rules, when you encounter the determinant \( \left| -D \ T \ E_2 \right| \), you can express it as the triple product \((E_2 \times -D) \cdot T\). To eliminate the negative sign in front of \( T \), we can swap \( E_2 \) and \(-D\), resulting in \( (D \times E_2) \cdot T \). The other triple products can be derived using the rules provided above.

The scalar triple product can also be understood as the determinant of a \(3 \times 3\) matrix with the three vectors as either its rows or its columns:

$$ \mathbf{a} \cdot (\mathbf{b} \times \mathbf{c}) = \det \begin{bmatrix} a_1 & a_2 & a_3 \\ b_1 & b_2 & b_3 \\ c_1 & c_2 & c_3 \\ \end{bmatrix}. $$

Implementation

Implementing the Möller-Trumbore algorithm is straightforward. The scalar triple product \( (A \times B) \cdot C \) is the same as \( A \cdot (B \times C) \). Therefore, the denominator \( (D \times E2) \cdot E1 \) in Equation 5 is the same as \( D \cdot (E1 \times E2) \). The cross product of \( E1 \) and \( E2 \) gives the normal of the triangle. We know that if the dot product of the ray direction \( D \) and the triangle normal is 0, then the triangle and the ray are parallel (and therefore, there is no intersection).

Remember that the user might want to cull (discard) back-facing triangles (the back side of the triangle, which typically will never face the camera). If the triangle is front-facing, the determinant is positive; otherwise, it is negative. In the code, we will first compute this determinant. There is no intersection if the result is negative and the triangle is single-sided, or if the determinant is close to 0. If the triangle is double-sided, we will still need to check if the determinant is close to 0, which is more complex than in the previous case, as the value is signed.

bool rayTriangleIntersect(
    const Vec3f &orig, const Vec3f &dir,
    const Vec3f &v0, const Vec3f &v1, const Vec3f &v2,
    float &t, float &u, float &v)
{
    Vec3f v0v1 = v1 - v0;
    Vec3f v0v2 = v2 - v0;
    Vec3f pvec = dir.crossProduct(v0v2);
    float det = v0v1.dotProduct(pvec);
#ifdef CULLING
    // if the determinant is negative, the triangle is 'back facing.'
    // if the determinant is close to 0, the ray misses the triangle
    if (det < kEpsilon) return false;
#else
    // ray and triangle are parallel if det is close to 0
    if (fabs(det) < kEpsilon) return false;
#endif
    ...
}

The following steps are simple. We first compute u and reject the triangle if u is either lower than 0 or greater than 1\. If we successfully pass the computation of \(u\), then we compute \(v\) and apply the same tests (there's no intersection if \(v\) is lower than 0 or greater than one and if u+v is greater than 1). We know the ray intersects the triangle at this point, and we can compute \(t\).

bool rayTriangleIntersect(
    const Vec3f &orig, const Vec3f &dir,
    const Vec3f &v0, const Vec3f &v1, const Vec3f &v2,
    float &t, float &u, float &v)
{
    ...
    float invDet = 1 / det;

    Vec3f tvec = orig - v0;
    u = tvec.dotProduct(pvec) * invDet;
    if (u < 0 || u > 1) return false;

    Vec3f qvec = tvec.crossProduct(v0v1);
    v = dir.dotProduct(qvec) * invDet;
    if (v < 0 || u + v > 1) return false;
    
    t = v0v2.dotProduct(qvec) * invDet;
    
    return true;
}

Note that when the ray and the normal of the triangle are facing each other (they go in opposite directions), the determinant is positive (greater than 0), as shown in the adjacent figure (top). On the other hand, when the ray hits the triangle from "behind" (the ray and the normal points in the same direction), the determinant is negative (adjacent figure, bottom). The figure on the side illustrates these two possible cases. The numbers are simple enough that cross and dot products between vectors are straightforward to compute. The coordinates of edge2 are (1,0,0), and the cross product between the ray direction (0,-1,0) and edge2 gives pvec. Remember that the cross product of two vectors is provided by:

$$ \begin{array}{l} x = v1.y v2.z - v1.z v2.y\\ y = v1.z v2.x - v1.x v2.z\\ z = v1.x v2.y - v1.y v2.x \end{array} $$

Thus:

$$ \begin{array}{l} pvec.z &=& dir.x * edge.y - dir.y * edge.x \\ &=& 0*0 -(-1 * 1) = 1, \end{array} $$

and pvec = (0,0,1). The dot product between this vector and edge1 (the determinant) is positive. When the ray has a direction (0,1,0), we get:

$$pvec.z = 0*0 - (1 * 1) = -1,$$

and pvec = (0,0,-1). The determinant is, in this case, negative.

In the original implementation of the algorithm (which you can find in the original paper, see reference below), the code can be either compiled to handle culling or ignored. When culling is active, rays intersecting triangles from behind will be discarded. This can easily be verified using the sign of the determinant. If culling is on and the determinant is lower than 0, then the ray doesn't intersect the triangle. However, when culling is off, we want to intersect triangles regardless of the normal orientation with resepct to the ray direction.

int intersect_triangle(...)
{
    ...
    det = DOT(edge1, pvec);
#ifdef CULLING        /* define CULLING if culling is desired */
    if (det < kEpsilon)
        return 0;
    ...
#else        /* the non-culling branch */
    if (det > -kEpsilon && det < kEpsilon)
        return 0;
    ...
#endif
}

The point we want to make here is that when culling is off and the determinant is negative, it becomes essential to "normalize \(u\)" by multiplying it by the inverse of the determinant. Because we later test if u is greater than 0 and lower than 1, when \(u\) is negative, but the determinant is also negative, then the sign of \(u\) when "normalized" (multiplied by invDet) is inverted (it becomes positive).

Source Code

The C++ implementation of the MT algorithm is straightforward (if you have an excellent vector library) and is also very compact, as you can see in this code snippet:

bool rayTriangleIntersect(
    const Vec3f &orig, const Vec3f &dir,
    const Vec3f &v0, const Vec3f &v1, const Vec3f &v2,
    float &t, float &u, float &v)
{
#ifdef MOLLER_TRUMBORE
    Vec3f v0v1 = v1 - v0;
    Vec3f v0v2 = v2 - v0;
    Vec3f pvec = dir.crossProduct(v0v2);
    float det = v0v1.dotProduct(pvec);
#ifdef CULLING
    // if the determinant is negative, the triangle is 'back facing'
    // if the determinant is close to 0, the ray misses the triangle
    if (det < kEpsilon) return false;
#else
    // ray and triangle are parallel if det is close to 0
    if (fabs(det) < kEpsilon) return false;
#endif
    float invDet = 1 / det;

    Vec3f tvec = orig - v0;
    u = tvec.dotProduct(pvec) * invDet;
    if (u < 0 || u > 1) return false;

    Vec3f qvec = tvec.crossProduct(v0v1);
    v = dir.dotProduct(qvec) * invDet;
    if (v < 0 || u + v > 1) return false;
    
    t = v0v2.dotProduct(qvec) * invDet;
    
    return true;
#else
    ...
#endif
}

Compile with:

c++ -o raytri raytri.cpp -O3 -std=c++11 -DMOLLER_TRUMBORE
The complete source code of this function can be found in the source code section at the end of this lesson. As in the previous chapter, the program renders a single triangle and uses the barycentric coordinates of the intersection point to interpolate three colors across the surface of that triangle.

Important note regarding the MT algorithm implementation: If you compile the code provided with this lesson, you might notice that the image produced when the MT algorithm is used is not the same as the image of the triangle displayed above. This can be confusing. That's because the image of the triangle shown above was rendered using the geometry solution. Not the MT algorithm. The image below shows what the two algorithms output. On the left is the Möller–Trumbore solution. On the right is the geometric solution.

Why are they different? Mathematically they are not. It's just that the two algorithms are not calculating u, v, and w in the same order. The coordinate u in the geometric solution is equal to w in the MT solution, etc., and u in the first solution is numerically similar to w in the second. So don't worry. It's not because they produce a different image that one or the other is incorrect. They are both correct. You'll need to potentially swap the coordinates when using them for texturing and shading.

Notes

One way of accelerating the ray triangle is to store the constants A, B, C, and D of the plane equation with the triangle's vertices (we precompute them once the mesh is loaded and store them in memory). However, storing variables in memory and accessing them can be more expansive than the time required to compute them on the fly. Therefore this is not necessarily proven to be a reliable optimization.

One way of accelerating the algorithm is to reduce the ray-triangle intersection to two dimensions. To do so, we determine the dominant axis of the triangle's normal and use this information to select the two coordinates we use for the projection. This technique is described in "Ray-Polygon Intersection. An Efficient Ray-Polygon Intersection" (see references section below).

Algorithms to ray-trace quads already exist.

Our code uses single precision floating numbers, which are generally enough, but the ray-triangle intersection test may suffer from precision errors in some conditions. For example, the intersection point might not lie on the triangle plane and be either slightly above or below the triangle's surface. We will learn more about this in the lesson on shading.

Many of these topics are or will be addressed in the advanced section.

Exercises

References

Fast, Minimum Storage Ray/Triangle Intersection. Möller & Trumbore. Journal of Graphics Tools, 1997.

Ray-Polygon Intersection. An Efficient Ray-Polygon Intersection. Didier Badouel from Graphics Gems I. 1990

previous-