use enum for bvh node content + wrap every model in aabb

This commit is contained in:
Quaternions 2023-10-30 18:48:51 -07:00
parent 9fc7884270
commit a3340143db

View File

@ -9,22 +9,30 @@ use crate::aabb::Aabb;
//start with bisection into octrees because a bad bvh is still 1000x better than no bvh //start with bisection into octrees because a bad bvh is still 1000x better than no bvh
//sort the centerpoints on each axis (3 lists) //sort the centerpoints on each axis (3 lists)
//bv is put into octant based on whether it is upper or lower in each list //bv is put into octant based on whether it is upper or lower in each list
enum BvhNodeContent{
Branch(Vec<BvhNode>),
Leaf(usize),
}
impl Default for BvhNodeContent{
fn default()->Self{
Self::Branch(Vec::new())
}
}
#[derive(Default)] #[derive(Default)]
pub struct BvhNode{ pub struct BvhNode{
children:Vec<Self>, content:BvhNodeContent,
models:Vec<usize>,
aabb:Aabb, aabb:Aabb,
} }
impl BvhNode{ impl BvhNode{
pub fn the_tester<F:FnMut(usize)>(&self,aabb:&Aabb,f:&mut F){ pub fn the_tester<F:FnMut(usize)>(&self,aabb:&Aabb,f:&mut F){
for &model in &self.models{ match &self.content{
f(model); &BvhNodeContent::Leaf(model)=>f(model),
} BvhNodeContent::Branch(children)=>for child in children{
for child in &self.children{ if aabb.intersects(&child.aabb){
if aabb.intersects(&child.aabb){ child.the_tester(aabb,f);
child.the_tester(aabb,f); }
} },
} }
} }
} }
@ -37,10 +45,15 @@ fn generate_bvh_node(boxen:Vec<(usize,Aabb)>)->BvhNode{
let n=boxen.len(); let n=boxen.len();
if n<20{ if n<20{
let mut aabb=Aabb::default(); let mut aabb=Aabb::default();
let models=boxen.into_iter().map(|b|{aabb.join(&b.1);b.0}).collect(); let nodes=boxen.into_iter().map(|b|{
aabb.join(&b.1);
BvhNode{
content:BvhNodeContent::Leaf(b.0),
aabb:b.1,
}
}).collect();
BvhNode{ BvhNode{
children:Vec::new(), content:BvhNodeContent::Branch(nodes),
models,
aabb, aabb,
} }
}else{ }else{
@ -99,8 +112,7 @@ fn generate_bvh_node(boxen:Vec<(usize,Aabb)>)->BvhNode{
node node
}).collect(); }).collect();
BvhNode{ BvhNode{
children, content:BvhNodeContent::Branch(children),
models:Vec::new(),
aabb, aabb,
} }
} }