Ray-triangle intersection: Difference between revisions

From vegard.wiki
Jump to navigation Jump to search
Content added Content deleted
(new page)
 
(add link)
 
Line 21: Line 21:


* [[Barycentric coordinates]]
* [[Barycentric coordinates]]
* https://twitter.com/BrunoLevy01/status/1229665598273150979
* https://stackoverflow.com/questions/42740765/intersection-between-line-and-triangle-in-3d/42752998#42752998
* https://stackoverflow.com/questions/42740765/intersection-between-line-and-triangle-in-3d/42752998#42752998



Latest revision as of 08:13, 8 December 2021

// 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