the tools to get the job done

This commit is contained in:
Quaternions 2023-10-05 22:50:16 -07:00
parent a942e10554
commit 2b47827383
2 changed files with 27 additions and 1 deletions

View File

@ -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.),

View File

@ -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<Self>,
models:Vec<u32>,
aabb:Aabb,
}
impl BvhNode{
pub fn the_tester<F:FnMut(u32)>(&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<Aabb>)->BvhNode{
generate_bvh_node(boxen.into_iter().enumerate().collect())
}