Intersection Test Utility Functions

Ray AABB Intersection (Slab)

bool RayAABB(const glm::vec3 &orig, const glm::vec3 &dir, const glm::vec3 &minB, const glm::vec3 &maxB, float &tmin)
{
    float t1 = (minB.x - orig.x) / dir.x;
    float t2 = (maxB.x - orig.x) / dir.x;
    float t3 = (minB.y - orig.y) / dir.y;
    float t4 = (maxB.y - orig.y) / dir.y;
    float t5 = (minB.z - orig.z) / dir.z;
    float t6 = (maxB.z - orig.z) / dir.z;

    float tminCandidate = std::max({std::min(t1, t2), std::min(t3, t4), std::min(t5, t6)});
    float tmaxCandidate = std::min({std::max(t1, t2), std::max(t3, t4), std::max(t5, t6)});

    if (tmaxCandidate < 0 || tminCandidate > tmaxCandidate)
        return false;
    tmin = tminCandidate;
    return true;
}

Ray Triangle Intersection

Last updated