Step 2: Is P Inside or Outside the Triangle? If \(t\) is greater than 0, the triangle is "visible" to that ray, and we can proceed to the next step. There is no intersection, and we can return false again. If \(t\) is lower than 0, the triangle is behind the ray's origin (with regards to the ray's direction) and is not visible. Therefore we need to check the sign of \(t\) before deciding whether the intersection is valid. If we don't catch this "error," 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? Usually, 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 ray's origin and should be discarded. The Triangle is "Behind" the Ray Figure 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 no need to compute \(t\) anyway. When this term equals 0, the ray is parallel to the triangle. 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 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. If the triangle and the ray are parallel, then the triangle's normal and the ray's direction should be perpendicular. For robustness, we need to handle that case if it happens. If the ray and the plane are parallel, they won't intersect (Figure 2). There are two very important cases that we need to look at before we check if the point is inside the triangle. We now have computed \(t\), which we can use to calculate the position of P: Where A, B, C can be seen as the components (or coordinates) of the normal to the plane (\N_įloat t = - (dot(N, orig) + D) / dot(N, dir) 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 from the previous lesson for more information on this topic): To find P, we must find \(t\) (Figure 1). Where \(t\) is the 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. 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. Ray-triangle intersection: geometric solution Figure 1: the intersection of a ray and a triangle. Ray-Triangle Intersection: Geometric Solution Reading time: 13 mins. Single vs Double Sided Triangle and Backface Culling.Ray-Triangle Intersection: Geometric Solution.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |