Complete BVH building
This commit is contained in:
parent
556f50274d
commit
7d122d44b3
5 changed files with 207 additions and 17 deletions
|
|
@ -5,6 +5,7 @@ use ray_tracing_core::{
|
|||
|
||||
type Index = u32;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TriangleBVH<R: Rng> {
|
||||
vertices: Vec<Pos3>,
|
||||
triangles: Vec<Triangle>,
|
||||
|
|
@ -12,6 +13,7 @@ pub struct TriangleBVH<R: Rng> {
|
|||
bvh: Vec<Node>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct BVHMaterial<R: Rng> {
|
||||
material: Option<Box<dyn Material<R>>>,
|
||||
light: Option<Box<dyn Light<R>>>,
|
||||
|
|
@ -85,14 +87,100 @@ fn triangle_normal(v: [Pos3; 3]) -> Dir3 {
|
|||
Dir3::cross(e1, e2)
|
||||
}
|
||||
|
||||
fn calculate_aabb(vertices: &[Pos3], triangles: &[Triangle]) -> AABB {
|
||||
let mut aabb = AABB::new(
|
||||
vertices[triangles[0].vertices[0] as usize],
|
||||
vertices[triangles[0].vertices[1] as usize],
|
||||
);
|
||||
aabb = aabb.extend(vertices[triangles[0].vertices[2] as usize]);
|
||||
|
||||
for t in triangles.iter().skip(1) {
|
||||
for v in t.vertices {
|
||||
aabb = aabb.extend(vertices[v as usize]);
|
||||
}
|
||||
}
|
||||
|
||||
aabb
|
||||
}
|
||||
|
||||
fn build_bvh(
|
||||
vertices: &[Pos3],
|
||||
triangles: &mut [Triangle],
|
||||
bvh: &mut Vec<Node>,
|
||||
node: usize,
|
||||
aabb: AABB,
|
||||
) {
|
||||
let (start, count) = if let Node::Leaf { start, count } = bvh[node] {
|
||||
(start, count)
|
||||
} else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
if count < 8 {
|
||||
return;
|
||||
}
|
||||
|
||||
let size = aabb.size();
|
||||
|
||||
let dim = if size.x() > Float::max(size.y(), size.z()) {
|
||||
0
|
||||
} else if size.y() > Float::max(size.x(), size.z()) {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
};
|
||||
|
||||
let get_key = |t: &Triangle| {
|
||||
t.vertices
|
||||
.iter()
|
||||
.map(|&v| vertices[v as usize][dim])
|
||||
.sum::<Float>()
|
||||
/ 3.0
|
||||
};
|
||||
|
||||
triangles.sort_by(|a, b| get_key(a).partial_cmp(&get_key(b)).unwrap());
|
||||
|
||||
let (left_range, right_range) = triangles.split_at_mut((count / 2) as usize);
|
||||
|
||||
let left_aabb = calculate_aabb(vertices, left_range);
|
||||
let right_aabb = calculate_aabb(vertices, right_range);
|
||||
|
||||
let i = bvh.len() as Index;
|
||||
bvh[node] = Node::Inner {
|
||||
left: i,
|
||||
left_aabb,
|
||||
right: i + 1,
|
||||
right_aabb,
|
||||
};
|
||||
|
||||
bvh.push(Node::Leaf {
|
||||
start,
|
||||
count: left_range.len() as Index,
|
||||
});
|
||||
bvh.push(Node::Leaf {
|
||||
start: start + left_range.len() as Index,
|
||||
count: right_range.len() as Index,
|
||||
});
|
||||
|
||||
build_bvh(vertices, left_range, bvh, i as usize, left_aabb);
|
||||
build_bvh(vertices, right_range, bvh, i as usize + 1, right_aabb);
|
||||
}
|
||||
|
||||
impl<R: Rng> TriangleBVH<R> {
|
||||
fn new(vertices: Vec<Pos3>, triangles: Vec<Triangle>, materials: Vec<BVHMaterial<R>>) -> Self {
|
||||
fn new(
|
||||
vertices: Vec<Pos3>,
|
||||
mut triangles: Vec<Triangle>,
|
||||
materials: Vec<BVHMaterial<R>>,
|
||||
) -> Self {
|
||||
let mut bvh = vec![Node::Leaf {
|
||||
start: 0,
|
||||
count: triangles.len() as Index,
|
||||
}];
|
||||
let aabb = calculate_aabb(&vertices, &triangles);
|
||||
build_bvh(&vertices, &mut triangles, &mut bvh, 0, aabb);
|
||||
Self {
|
||||
vertices,
|
||||
bvh: vec![Node::Leaf {
|
||||
start: 0,
|
||||
count: triangles.len() as Index,
|
||||
}],
|
||||
bvh,
|
||||
triangles,
|
||||
materials,
|
||||
}
|
||||
|
|
@ -141,7 +229,7 @@ impl<R: Rng> TriangleBVH<R> {
|
|||
|
||||
if let Some(close_intersect) = self.intersect_bvh(close, ray, min, max) {
|
||||
if let Some(far_intersect) = self
|
||||
.intersect_bvh(far, ray, min, Float::min(min, close_intersect.1))
|
||||
.intersect_bvh(far, ray, min, Float::min(max, close_intersect.1))
|
||||
.filter(|far_intersect| far_intersect.1 < close_intersect.1)
|
||||
{
|
||||
Some(far_intersect)
|
||||
|
|
@ -198,13 +286,14 @@ pub mod examples {
|
|||
use super::{BVHMaterial, Triangle, TriangleBVH};
|
||||
use ray_tracing_core::{light::AreaLight, prelude::*};
|
||||
use ray_tracing_material::{diffuse::DiffuseMaterial, mirror::Mirror};
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub fn cornel<R: Rng>() -> TriangleBVH<R> {
|
||||
pub fn cornel<R: Rng + Debug>() -> TriangleBVH<R> {
|
||||
let side_length = 1.5;
|
||||
let light_size = 0.5;
|
||||
let light_offset = 0.01;
|
||||
let box_size = 0.3;
|
||||
TriangleBVH::new(
|
||||
dbg!(TriangleBVH::new(
|
||||
vec![
|
||||
Pos3::new(side_length, side_length, side_length),
|
||||
Pos3::new(side_length, side_length, -side_length),
|
||||
|
|
@ -264,6 +353,6 @@ pub mod examples {
|
|||
light: None,
|
||||
},
|
||||
],
|
||||
)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue