From 2b478273832aa9e563e612cbc68b4115c3c6c11a Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 5 Oct 2023 22:50:16 -0700 Subject: [PATCH] the tools to get the job done --- src/aabb.rs | 13 +++++++++++++ src/bvh.rs | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) 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()) }