svenstaro / bvh

A fast BVH using SAH in rust
https://docs.rs/bvh
MIT License
227 stars 38 forks source link

why won't my rays intersect? #110

Open TirushOne opened 3 months ago

TirushOne commented 3 months ago

So I have a problem, obvious ray intersections just arn't happening, like, at all, I can't get any rays (from the origin, but anywere else as well) to hit my custom mesh. this is part of a small personal project so it's only a couple hundred lines and I have already tested everything else and the problem seems to stem from this code here:

fn closest_hit(&self, ray: &Ray<f32, 3>) -> Option<(f32, usize)> {
       let mut closest_dist = f32::INFINITY;
       let mut closest_idx = 0;

    for tri in self.bvh.traverse_iterator(&ray, &self.tris) {
        let p1 = Point3::from(self.verts[tri.indices[0]]);
        let p2 = Point3::from(self.verts[tri.indices[1]]);
        let p3 = Point3::from(self.verts[tri.indices[2]]);

        let res = ray.intersects_triangle(&p1, &p2, &p3);

        if res.distance < closest_dist {
            closest_dist = res.distance;
            closest_idx = tri.index;
        }
    }

    if closest_dist == f32::INFINITY {
        None
    } else {
        Some((closest_dist, closest_idx))
    }
}

this code always return None no matter where the ray is, it's durection or the normals of the triangles being tested. The iterator always returns no items. And like I said I already validated everything else and it's driving me insane. Does anyone know what could possibley be causing this issue? Here is some debug info on the bvh:

child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {-2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {-2.050189, -0.990136, 2.050189} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {-2.050189, -0.990136, 2.050189} shape 14 child_r Min bound: {-2.050189, -2.050189, -0.990136}; Max bound: {-2.050189, -0.990136, 2.050189} shape 5 child_r Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {-2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {-2.050189, 2.050189, -0.990136} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {-2.050189, 0.990136, -0.990136} shape 7 child_r Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {-2.050189, 2.050189, -0.990136} shape 16 child_r Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {-2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, 0.990136, -2.050189}; Max bound: {-2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, 0.990136, -2.050189}; Max bound: {-2.050189, 2.050189, 0.990136} shape 8 child_r Min bound: {-2.050189, 0.990136, -2.050189}; Max bound: {-2.050189, 2.050189, 2.050189} shape 17 child_r Min bound: {-2.050189, -2.050189, 0.990136}; Max bound: {-2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, 0.990136}; Max bound: {-2.050189, 2.050189, 2.050189} shape 15 child_r Min bound: {-2.050189, -0.990136, 0.990136}; Max bound: {-2.050189, 2.050189, 2.050189} shape 6 child_r Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, -2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, -2.050189, 2.050189} shape 2 child_r Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, -2.050189, 2.050189} shape 11 child_r Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, -2.050189} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, -2.050189} shape 4 child_r Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, -2.050189} shape 13 child_r Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, 2.050189}; Max bound: {2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, -2.050189, 2.050189}; Max bound: {2.050189, 2.050189, 2.050189} shape 1 child_r Min bound: {-2.050189, -2.050189, 2.050189}; Max bound: {2.050189, 2.050189, 2.050189} shape 10 child_r Min bound: {-2.050189, 2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} child_l Min bound: {-2.050189, 2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} shape 0 child_r Min bound: {-2.050189, 2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} shape 9 child_r Min bound: {2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} child_l Min bound: {2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} shape 3 child_r Min bound: {2.050189, -2.050189, -2.050189}; Max bound: {2.050189, 2.050189, 2.050189} shape 12

svenstaro commented 3 months ago

Can you look at the example and check whether that gives you results? Also, running the tests should work.