Reputation: 129
I found this ray-triangle intersection function in a GitHub repository for my ray tracing implementation. I know that is the "Möller and Trumbore" algorithm. i was wandering if someone can give me an hint about what actually are the out arguments of this function are(i mean mathematically ). code is below:
bool IntersectTriangle(Ray ray, vec3 p0, vec3 p1, vec3 p2,
out float hit, out vec3 barycentricCoord, out vec3 triangleNormal)
{
const vec3 e0 = p1 - p0;
const vec3 e1 = p0 - p2;
triangleNormal = cross( e1, e0 );
const vec3 e2 = ( 1.0 / dot( triangleNormal, ray.Direction ) ) * ( p0 - ray.Origin );
const vec3 i = cross( ray.Direction, e2 );
barycentricCoord.y = dot( i, e1 );
barycentricCoord.z = dot( i, e0 );
barycentricCoord.x = 1.0 - (barycentricCoord.z + barycentricCoord.y);
hit = dot( triangleNormal, e2 );
return /*(hit < ray.tmax) && */ (hit > INTERSECT_EPSILON) && all(greaterThanEqual(barycentricCoord, vec3(0.0)));
}
i know what are the barycentricCoords and the triangle normal but i dont understand mathematically the "hit" argument and the "e2" variable inside the function.
const vec3 e2 = ( 1.0 / dot( triangleNormal, ray.Direction ) ) * ( p0 - ray.Origin );
hit = dot( triangleNormal, e2 );
As far as my understanding, is the hit variable the hit position of the ray to the triangle?
link of the repository:
Upvotes: 2
Views: 1975
Reputation: 1136
I can answer part of your question. I believe the outputs here are as follows:
out vec3 barycentricCoord
The intersection point, P given in barycentric coordinates.
out vec3 triangleNormal
The triangle normal (the vector that is perpendicular to the triangle face).
out float hit
The distance from the intersection point to the ray.
This page has some really great information and is very clear and gives a much more in depth explanation:
It explains a lot about the calculations and what the mathematics represents. But from what I could see that is what I interpret those outputs to mean.
I'm still breaking this line down:
const vec3 e2 = ( 1.0 / dot( triangleNormal, ray.Direction ) ) * ( p0 - ray.Origin );
But this part of the line,
dot( triangleNormal, ray.Direction )
looks like it's doing a check to see if the ray and triangle normal are perpendicular. Which if they are it means that no intersection has occurred because the ray is parallel to the triangle.
Upvotes: 1