Ray-triangle intersection

From vegard.wiki
Revision as of 08:13, 8 December 2021 by Vegard (talk | contribs) (new page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
// https://twitter.com/brunolevy01/status/1229664999179726848
// "(Moller-Tumblore). A, B, C is the triangle, O, D is the ray."
// gives barycentric coordinates and normal for free.
bool ray_triangle_intersection(vec3 A, vec3 B, vec3 C, vec3 O, vec3 D)
{
    vec3 E1 = B - A;
    vec3 E2 = C - A;
    vec3 N = cross(E1, E2);
    float det = -dot(D, N);
    vec3 AO = O - A;
    vec3 DAO = cross(AO, D);
    float u =  dot(E2, DAO) / det;
    float v = -dot(E1, DAO) / det;
    float t =  dot(AO, N) / det;
    return (t > 0. && u > 0. && v > 0. && (u + v) < 1.);
}

See also