Add BVH bounding boxes
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
use crate::{Point3, Ray, Vec3};
|
||||
use crate::material::{Material};
|
||||
use crate::aabb::Aabb;
|
||||
|
||||
pub type HittableList = Vec<Box<dyn Hittable + Sync>>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct HitRecord<'material> {
|
||||
@ -12,7 +15,10 @@ pub struct HitRecord<'material> {
|
||||
|
||||
pub trait Hittable {
|
||||
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord>;
|
||||
fn bounding_box(&self, time0: f64, time1: f64) -> Option<Aabb>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Sphere {
|
||||
pub center: Point3,
|
||||
pub radius: f64,
|
||||
@ -32,7 +38,6 @@ impl Default for Sphere {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hittable for Sphere {
|
||||
fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
|
||||
let oc = &ray.origin() - &self.center;
|
||||
@ -61,8 +66,16 @@ impl Hittable for Sphere {
|
||||
material: &self.material
|
||||
})
|
||||
}
|
||||
fn bounding_box(&self, _time0: f64, _time1: f64) -> Option<Aabb> {
|
||||
let s = Vec3::new(self.radius, self.radius, self.radius);
|
||||
Some(Aabb {
|
||||
minimum: self.center - s,
|
||||
maximum: self.center + s,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MovableSphere {
|
||||
pub center0: Point3,
|
||||
pub center1: Point3,
|
||||
@ -126,4 +139,16 @@ impl Hittable for MovableSphere {
|
||||
material: &self.material
|
||||
})
|
||||
}
|
||||
}
|
||||
fn bounding_box(&self, time0: f64, time1: f64) -> Option<Aabb> {
|
||||
let s = Vec3::new(self.radius, self.radius, self.radius);
|
||||
let center0 = self.center(time0);
|
||||
let center1 = self.center(time1);
|
||||
Some(Aabb {
|
||||
minimum: center0 - s,
|
||||
maximum: center0 + s,
|
||||
}.surrounding(&Aabb {
|
||||
minimum: center1 - s,
|
||||
maximum: center1 + s
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user