View on GitHub

Cardinal3D

Stanford CS248A (Assignments 2 and 3)

Ray Triangle Intersection

Given a triangle defined by points p0, p1 and p2 and a ray given by its origin o and direction d, the barycentrics of the hit point as well as the t-value of the hit can be obtained by solving the system:

triangle_eq1

Where:

triangle_eq2

This system can be solved by Cramer’s Rule, yielding:

triangle_eq3

In the above, |a b c| denotes the determinant of the 3x3 with column vectors a, b, c. Note that since the determinant is given by:triangle_eq4 you can rewrite the above as: triangle_eq5

Of which you should notice a few common subexpressions that, if exploited in an implementation, make computation of t, u, and v substantially more efficient.

A few final notes and thoughts:

If the denominator dot((e1 x d), e2) is zero, what does that mean about the relationship of the ray and the triangle? Can a triangle with this area be hit by a ray? Given u and v, how do you know if the ray hits the triangle? Don’t forget that the intersection point on the ray should be within the ray’s dist_bounds.