Skip to content

Commit

Permalink
test: Draw spheres and triangles
Browse files Browse the repository at this point in the history
Add a sphere to the cube over plane test scene, and fix bounding boxes
spheres by transformationing their center point from model to world.
  • Loading branch information
Fahien committed Nov 15, 2024
1 parent 64497e6 commit 22ed3e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/bvh/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,24 @@ impl<'m> BvhPrimitive<'m> {
}
}

pub fn centroid(&self) -> &Point3 {
pub fn centroid(&self) -> Point3 {
match &self.geometry {
BvhGeometry::Triangle(triangle) => &triangle.centroid,
BvhGeometry::Sphere(sphere) => &sphere.center,
BvhGeometry::Triangle(triangle) => triangle.centroid,
BvhGeometry::Sphere(sphere) => self.trs * sphere.center,
}
}

pub fn min(&self) -> Point3 {
match &self.geometry {
BvhGeometry::Triangle(triangle) => triangle.min(),
BvhGeometry::Sphere(sphere) => sphere.min(),
BvhGeometry::Sphere(sphere) => self.trs * sphere.min(),
}
}

pub fn max(&self) -> Point3 {
match &self.geometry {
BvhGeometry::Triangle(triangle) => triangle.max(),
BvhGeometry::Sphere(sphere) => sphere.max(),
BvhGeometry::Sphere(sphere) => self.trs * sphere.max(),
}
}

Expand Down
17 changes: 9 additions & 8 deletions src/bvh/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ impl AABB {
self.grow(&triangle.vertices[2].pos);
}

fn grow_sphere(&mut self, sphere: &BvhSphere) {
fn grow_sphere(&mut self, sphere: &BvhSphere, trs: &Trs) {
let radius = sphere.get_radius();
self.grow(&(sphere.center + Vec3::new(-radius, 0.0, 0.0)));
self.grow(&(sphere.center + Vec3::new(radius, 0.0, 0.0)));
self.grow(&(sphere.center + Vec3::new(0.0, -radius, 0.0)));
self.grow(&(sphere.center + Vec3::new(0.0, radius, 0.0)));
self.grow(&(sphere.center + Vec3::new(0.0, 0.0, -radius)));
self.grow(&(sphere.center + Vec3::new(0.0, 0.0, radius)));
let center = trs * sphere.center;
self.grow(&(center + Vec3::new(-radius, 0.0, 0.0)));
self.grow(&(center + Vec3::new(radius, 0.0, 0.0)));
self.grow(&(center + Vec3::new(0.0, -radius, 0.0)));
self.grow(&(center + Vec3::new(0.0, radius, 0.0)));
self.grow(&(center + Vec3::new(0.0, 0.0, -radius)));
self.grow(&(center + Vec3::new(0.0, 0.0, radius)));
}

fn grow_primitive(&mut self, primitive: &BvhPrimitive) {
Expand All @@ -47,7 +48,7 @@ impl AABB {
self.grow_triangle(triangle);
}
BvhGeometry::Sphere(sphere) => {
self.grow_sphere(sphere);
self.grow_sphere(sphere, primitive.trs);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ fn cube_over_plane() {
scene.load("tests/model/box/box.gltf").unwrap();
scene.load("tests/model/box/box.gltf").unwrap();

let mut model = Model::default();
let sphere_prim = Primitive::unit_sphere();
let prim_handle = model.primitives.push(sphere_prim);
let sphere_mesh = model.meshes.push(Mesh::new(vec![prim_handle]));
let sphere_node = model.nodes.push(
Node::builder()
.translation(Vec3::new(-0.5, 2.0, -3.0))
.mesh(sphere_mesh)
.build(),
);
model.root.children.push(sphere_node);
scene.models.push(model);


scene.models[0].root.trs.scale = Vec3::new(16.0, 0.125, 16.0);
scene.models[0].root.trs.translation += Vec3::new(0.0, -1.0, 0.0);
scene.models[0]
Expand Down

0 comments on commit 22ed3e0

Please sign in to comment.