-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
252 additions
and
50,427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::*; | ||
|
||
#[derive(Copy, Clone, Default)] | ||
pub struct Aabb { | ||
minimum: point3, | ||
maximum: point3, | ||
} | ||
pub fn surrounding_box(box0: &Aabb, box1: &Aabb) -> Aabb { | ||
let small = point3::from( | ||
min(box0.min().x(), box1.min().x()), | ||
min(box0.min().y(), box1.min().y()), | ||
min(box0.min().z(), box1.min().z()), | ||
); | ||
|
||
let big = point3::from( | ||
max(box0.min().x(), box1.min().x()), | ||
max(box0.min().y(), box1.min().y()), | ||
max(box0.min().z(), box1.min().z()), | ||
); | ||
|
||
Aabb::from(&small, &big) | ||
} | ||
impl Aabb { | ||
pub fn new() -> Self { | ||
Self { | ||
minimum: point3::new(), | ||
maximum: point3::new(), | ||
} | ||
} | ||
pub fn from(a: &point3, b: &point3) -> Self { | ||
Self { | ||
minimum: *a, | ||
maximum: *b, | ||
} | ||
} | ||
pub fn min(&self) -> point3 { | ||
self.minimum | ||
} | ||
pub fn max(&self) -> point3 { | ||
self.maximum | ||
} | ||
pub fn hit(&self, r: &Ray, mut t_min: f32, mut t_max: f32) -> bool { | ||
for i in 0..3 { | ||
let invD = 1.0 / r.direction()[i]; | ||
|
||
let mut t0 = (self.min()[i] - r.origin()[i]) * invD; | ||
let mut t1 = (self.max()[i] - r.origin()[i]) * invD; | ||
|
||
if invD < 0.0 { | ||
let prev_t0 = t0; | ||
t0 = t1; | ||
t1 = prev_t0; | ||
} | ||
t_min = if t0 > t_min { t0 } else { t_min }; | ||
t_max = if t1 < t_max { t1 } else { t_max }; | ||
|
||
if t_max <= t_min { | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
} | ||
|
||
fn min(a: f32, b: f32) -> f32 { | ||
if a == std::f32::NAN { | ||
return b; | ||
} else if b == std::f32::NAN { | ||
return a; | ||
} | ||
if a < b { | ||
return a; | ||
} else { | ||
return b; | ||
} | ||
} | ||
fn max(a: f32, b: f32) -> f32 { | ||
if a == std::f32::NAN { | ||
return b; | ||
} else if b == std::f32::NAN { | ||
return a; | ||
} | ||
if a > b { | ||
return a; | ||
} else { | ||
return b; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::{aabb::*, objects::hittable::*, objects::hittable_list::*, ray::*, utils::*}; | ||
use std::sync::Arc; | ||
|
||
pub struct BvhNode { | ||
left: Arc<dyn Hittable>, | ||
right: Arc<dyn Hittable>, | ||
boxx: Aabb, | ||
} | ||
|
||
impl BvhNode { | ||
pub fn from(list: &HittableList, time0: f32, time1: f32) -> Self { | ||
Self::fromvec(&list.objects, 0, list.objects.len(), time0, time1) | ||
} | ||
pub fn fromvec( | ||
src_objects: &Vec<Arc<dyn Hittable>>, | ||
start: usize, | ||
end: usize, | ||
time0: f32, | ||
time1: f32, | ||
) -> Self { | ||
let mut objects = src_objects; | ||
let axis = random_int(0, 2); | ||
|
||
let mut left = objects[start]; | ||
let mut right = objects[start]; | ||
|
||
let comparator = if axis == 0 { | ||
box_x_compare | ||
} else if axis == 1 { | ||
box_y_compare | ||
} else { | ||
box_z_compare | ||
}; | ||
let object_span = end - start; | ||
|
||
if object_span == 1 { | ||
} else if object_span == 1 { | ||
if comparator(objects[start], objects[start + 1]) { | ||
left = objects[start]; | ||
right = objects[start + 1]; | ||
} else { | ||
right = objects[start]; | ||
left = objects[start + 1]; | ||
} | ||
} else { | ||
} | ||
} | ||
} | ||
|
||
impl Hittable for BvhNode { | ||
fn bounding_box<'a>(&'a self, time0: f32, time1: f32, output_box: &mut Aabb) -> bool { | ||
*output_box = self.boxx; | ||
true | ||
} | ||
fn hit<'a>(&'a self, r: &Ray, t_min: f32, t_max: f32, rec: &mut HitRecord<'a>) -> bool { | ||
if !self.boxx.hit(r, t_min, t_max) { | ||
return false; | ||
} | ||
|
||
let hit_left = self.left.hit(r, t_min, t_max, rec); | ||
let hit_right = self.right.hit( | ||
r, | ||
t_min, | ||
{ | ||
if hit_left { | ||
rec.t | ||
} else { | ||
t_max | ||
} | ||
}, | ||
rec, | ||
); | ||
|
||
hit_left || hit_right | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters