the tools to get the job done
This commit is contained in:
parent
a942e10554
commit
2b47827383
13
src/aabb.rs
13
src/aabb.rs
@ -13,6 +13,12 @@ pub struct Aabb {
|
|||||||
pub max: glam::Vec3,
|
pub max: glam::Vec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for Aabb {
|
||||||
|
fn default() -> Self {
|
||||||
|
Aabb::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Aabb {
|
impl Aabb {
|
||||||
const VERTEX_DATA: [glam::Vec3; 8] = [
|
const VERTEX_DATA: [glam::Vec3; 8] = [
|
||||||
glam::vec3(1., -1., -1.),
|
glam::vec3(1., -1., -1.),
|
||||||
@ -37,6 +43,13 @@ impl Aabb {
|
|||||||
self.min=self.min.min(aabb.min);
|
self.min=self.min.min(aabb.min);
|
||||||
self.max=self.max.max(aabb.max);
|
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 {
|
pub fn normal(face:AabbFace) -> glam::Vec3 {
|
||||||
match face {
|
match face {
|
||||||
AabbFace::Right => glam::vec3(1.,0.,0.),
|
AabbFace::Right => glam::vec3(1.,0.,0.),
|
||||||
|
15
src/bvh.rs
15
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
|
//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
|
||||||
|
#[derive(Default)]
|
||||||
pub struct BvhNode{
|
pub struct BvhNode{
|
||||||
children:Vec<Self>,
|
children:Vec<Self>,
|
||||||
models:Vec<u32>,
|
models:Vec<u32>,
|
||||||
aabb:Aabb,
|
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{
|
pub fn generate_bvh(boxen:Vec<Aabb>)->BvhNode{
|
||||||
generate_bvh_node(boxen.into_iter().enumerate().collect())
|
generate_bvh_node(boxen.into_iter().enumerate().collect())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user