Ray-Tracing – Ray-Object Intersection
•
Ray-Triangle Intersection: Möller–Trumbore algorithm
bool Ray-Triangle(Vector3 rayOrigin, Vector3 rayDir,
Triangle* inTriangle, Vector3& outIntersect){
const float EPSILON = 0.0000001;
Vector3 vertex0 = inTriangle->vertex0;
Vector3 vertex1 = inTriangle->vertex1;
Vector3 vertex2 = inTriangle->vertex2;
Vector3 edge1 = vertex1 - vertex0;
Vector3 edge2 = vertex2 - vertex0;
Vector3 h = rayDir.crossProduct(edge2);
float a = edge1.dotProduct(h);
if (a > -EPSILON && a < EPSILON)
return false;
float f = 1/a;
Vector3 s = rayOrigin - vertex0;
float u = f * (s.dotProduct(h));
if (u < 0.0 || u > 1.0)
return false;
...