diff --git a/src/aabb.rs b/src/aabb.rs index ef728e9f..c027d93e 100644 --- a/src/aabb.rs +++ b/src/aabb.rs @@ -13,6 +13,12 @@ pub struct Aabb { pub max: glam::Vec3, } +impl Default for Aabb { + fn default() -> Self { + Aabb::new() + } +} + impl Aabb { const VERTEX_DATA: [glam::Vec3; 8] = [ glam::vec3(1., -1., -1.), @@ -37,6 +43,13 @@ impl Aabb { self.min=self.min.min(aabb.min); self.max=self.max.max(aabb.max); } + pub fn inflate(&mut self, hs:glam::Vec3){ + self.min-=hs; + self.max+=hs; + } + pub fn intersects(&self,aabb:&Aabb)->bool{ + (self.min.cmplt(aabb.max)&aabb.min.cmplt(self.max)).all() + } pub fn normal(face:AabbFace) -> glam::Vec3 { match face { AabbFace::Right => glam::vec3(1.,0.,0.), diff --git a/src/bvh.rs b/src/bvh.rs index 62eadc9f..b79ecc0e 100644 --- a/src/bvh.rs +++ b/src/bvh.rs @@ -9,13 +9,26 @@ use crate::aabb::Aabb; //start with bisection into octrees because a bad bvh is still 1000x better than no bvh //sort the centerpoints on each axis (3 lists) //bv is put into octant based on whether it is upper or lower in each list - +#[derive(Default)] pub struct BvhNode{ children:Vec, models:Vec, aabb:Aabb, } +impl BvhNode{ + pub fn the_tester(&self,aabb:&Aabb,f:&mut F){ + for &model in &self.models{ + f(model); + } + for child in &self.children{ + if aabb.intersects(&child.aabb){ + child.the_tester(aabb,f); + } + } + } +} + pub fn generate_bvh(boxen:Vec)->BvhNode{ generate_bvh_node(boxen.into_iter().enumerate().collect()) }