src/3D/BVH.cpp
// returns true if something hit (the return value is only used in recursion)
//
// populates outparam with all triangle hits in depth-first order
returns true if something hit (the return value is only used in recursion)
populates outparam with all triangle hits in depth-first order
returns true if something hit ( the return value is only used in recursion ) populates outparam with all triangle hits in depth - first order
static bool BVH_GetRayTriangleCollisionsRecursive(
        BVH const& bvh,
        glm::vec3 const* vs,
        size_t n,
        Line const& ray,
        int nodeidx,
        std::vector<BVHCollision>& out) {
    BVHNode const& node = bvh.nodes[nodeidx];
    RayCollision res = GetRayCollisionAABB(ray, node.bounds);
    if (!res.hit) {
        return false;  
    }
    if (node.nlhs == -1) {
        bool hit = false;
        for (int i = node.firstPrimOffset, end = node.firstPrimOffset + node.nPrims; i < end; ++i) {
            BVHPrim const& p = bvh.prims[i];
            RayCollision rayrtri = GetRayCollisionTriangle(ray, vs + p.id);
            if (rayrtri.hit) {
                out.push_back(BVHCollision{p.id, rayrtri.distance});
                hit = true;
            }
        }
        return hit;
    } else {
        bool lhs = BVH_GetRayTriangleCollisionsRecursive(bvh, vs, n, ray, nodeidx+1, out);
        bool rhs = BVH_GetRayTriangleCollisionsRecursive(bvh, vs, n, ray, nodeidx+node.nlhs+1, out);
        return lhs || rhs;
    }
}
static bool BVH_GetRayTriangleCollisionsRecursive ( BVH const & bvh , glm :: vec3 const * vs , size_t n , Line const & ray , int nodeidx , std :: vector < BVHCollision > & out ) { BVHNode const & node = bvh . nodes [ nodeidx ] ; RayCollision res = GetRayCollisionAABB ( ray , node . bounds ) ; if ( ! res . hit ) { return false ; } if ( node . nlhs == -1 ) { bool hit = false ; for ( int i = node . firstPrimOffset , end = node . firstPrimOffset + node . nPrims ; i < end ; ++ i ) { BVHPrim const & p = bvh . prims [ i ] ; RayCollision rayrtri = GetRayCollisionTriangle ( ray , vs + p . id ) ; if ( rayrtri . hit ) { out . push_back ( BVHCollision { p . id , rayrtri . distance } ) ; hit = true ; } } return hit ; } else { bool lhs = BVH_GetRayTriangleCollisionsRecursive ( bvh , vs , n , ray , nodeidx + 1 , out ) ; bool rhs = BVH_GetRayTriangleCollisionsRecursive ( bvh , vs , n , ray , nodeidx + node . nlhs + 1 , out ) ; return lhs || rhs ; } }
returns true if something hit (the return value is only used in recursion)
populates outparam with all triangle hits in depth-first order
returns true if something hit ( the return value is only used in recursion ) populates outparam with all triangle hits in depth - first order
// check ray-AABB intersection with the BVH node // no intersection with this node at all // leaf node: check ray-triangle intersection // else: internal node: check intersection with direct children