
If we don't catch this "error" though, the triangle will show up in the final image which we don't want. In that case, \(t\) is negative which causes the intersection point to be in the opposite direction to the ray direction. Equation 3 returns a valid result, even when the triangle is "behind" the ray. But what happens if the triangle is behind the ray with the ray still looking in the same direction? Normally the triangle shouldn't be visible. So far we have assumed that the triangle was always in front of the ray. Whenever the value of t computed with equation 3 is lower than 0, the intersection point is located behind the origin of the ray and should be discarded. The triangle is "behind" the rayįigure 3: If a triangle is "behind" the ray, it shouldn't be visible. Conclusion: before we calculate \(t\), we will calculate the result of the term \(N \cdot R\) first and if the result is 0, the function will return false (no intersection). Since they don't intersect in that case there's, therefore, no need to compute \(t\) anyway. It turns out that when this term is equal to 0 the ray is parallel to the triangle.
#Ray geometry code#
Our code is not very robust because this term can potentially be 0 and we should always catch a possible division by 0. If you look at the denominator of equation 3 (the term below the line) we do already compute the dot product between the triangle's normal N and the ray direction \(D\). We have learned that the dot product of two perpendicular vectors is 0. This situation occurs when the normal of the triangle and the ray direction is perpendicular (and the dot product of these two vectors is 0). If the ray is parallel to the triangle there is no possible intersection. The ray can intersect the triangle or miss it. What else do we know? We know the plane's normal which we have already computed and the plane equation (2) which is (check the chapter on the ray-plane intersection in previous lesson for more information on this topic):įigure 2: several situations can occur. To find P we need to find \(t\) (figure 1). Where \(t\) is distance from the ray origin \(O\) to P. The ray parametric equation is (equation 1): We used \(D\) in the previous lesson but we will use the term \(R\) in this lesson to avoid confusion with the term \(D\) from the plane equation. We know that P is somewhere on the ray defined by its origin \(O\) and its direction \(R\). Next, we need to find out the position of point P (for some illustrations we also used Phit), the point where the ray intersects the plane.
#Ray geometry how to#
In the previous paragraphs we learned how to calculate a plane's normal. The value \(t\) is the distance from the ray origin to the intersection point. Figure 1: intersection of a ray and a triangle.
