From e873ddac4036ce2fcc9d58b7b78bd992d90b74a3 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Tue, 2 Mar 2021 12:43:57 +0530 Subject: [PATCH] Fixed a bug in BVH implementation --- src/bvh.rs | 7 ++++--- src/demo.rs | 4 +--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/bvh.rs b/src/bvh.rs index a7bd00b..5db3983 100644 --- a/src/bvh.rs +++ b/src/bvh.rs @@ -20,8 +20,6 @@ impl BvhNode { .choose(rng) .unwrap(); - objects.sort_by(comparator); - let (left, right) = match objects.len() { 1 => ( HitNode::Direct(objects[0].clone()), @@ -39,6 +37,7 @@ impl BvhNode { }, n => { + objects.sort_by(comparator); let (l, r) = objects.split_at_mut(n / 2); ( HitNode::Bvh(Box::new(BvhNode::new(rng, l, t0, t1))), @@ -94,7 +93,9 @@ impl BvhNode { impl Hitable for BvhNode { fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option { - self.bounding_box(t_min, t_max)?; + if !self.bounding_box.hit(ray, t_min, t_max) { + return None; + } let hbox_left = self.left.hit(ray, t_min, t_max); diff --git a/src/demo.rs b/src/demo.rs index 6154d62..7e51370 100644 --- a/src/demo.rs +++ b/src/demo.rs @@ -1,12 +1,10 @@ -use crate::BvhNode; - use { crate::{ types::{ material::{Dielectric, Lambertian, Metal}, MovingSphere, Ray, Sphere, Vec3, }, - Camera, Hitable, HORIZONTAL_PARTITION, VERTICAL_PARTITION, + BvhNode, Camera, Hitable, HORIZONTAL_PARTITION, VERTICAL_PARTITION, }, rand::{rngs::SmallRng, Rng, SeedableRng}, rayon::prelude::*,