1
0

Fixed a bug in BVH implementation

This commit is contained in:
Ishan Jain 2021-03-02 12:43:57 +05:30
parent 18bb6a250b
commit e873ddac40
No known key found for this signature in database
GPG Key ID: F261A0E73038D89D
2 changed files with 5 additions and 6 deletions

View File

@ -20,8 +20,6 @@ impl<T: Hitable + Clone> BvhNode<T> {
.choose(rng) .choose(rng)
.unwrap(); .unwrap();
objects.sort_by(comparator);
let (left, right) = match objects.len() { let (left, right) = match objects.len() {
1 => ( 1 => (
HitNode::Direct(objects[0].clone()), HitNode::Direct(objects[0].clone()),
@ -39,6 +37,7 @@ impl<T: Hitable + Clone> BvhNode<T> {
}, },
n => { n => {
objects.sort_by(comparator);
let (l, r) = objects.split_at_mut(n / 2); let (l, r) = objects.split_at_mut(n / 2);
( (
HitNode::Bvh(Box::new(BvhNode::new(rng, l, t0, t1))), HitNode::Bvh(Box::new(BvhNode::new(rng, l, t0, t1))),
@ -94,7 +93,9 @@ impl<T: Hitable + Clone> BvhNode<T> {
impl<T: Hitable> Hitable for BvhNode<T> { impl<T: Hitable> Hitable for BvhNode<T> {
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> { fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
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); let hbox_left = self.left.hit(ray, t_min, t_max);

View File

@ -1,12 +1,10 @@
use crate::BvhNode;
use { use {
crate::{ crate::{
types::{ types::{
material::{Dielectric, Lambertian, Metal}, material::{Dielectric, Lambertian, Metal},
MovingSphere, Ray, Sphere, Vec3, MovingSphere, Ray, Sphere, Vec3,
}, },
Camera, Hitable, HORIZONTAL_PARTITION, VERTICAL_PARTITION, BvhNode, Camera, Hitable, HORIZONTAL_PARTITION, VERTICAL_PARTITION,
}, },
rand::{rngs::SmallRng, Rng, SeedableRng}, rand::{rngs::SmallRng, Rng, SeedableRng},
rayon::prelude::*, rayon::prelude::*,