From 22ed3e0dd36fa0b34a2cce759a4a65b725ef45d2 Mon Sep 17 00:00:00 2001 From: Antonio Caggiano Date: Sat, 24 Dec 2022 15:11:24 +0100 Subject: [PATCH] test: Draw spheres and triangles Add a sphere to the cube over plane test scene, and fix bounding boxes spheres by transformationing their center point from model to world. --- src/bvh/primitive.rs | 10 +++++----- src/bvh/structure.rs | 17 +++++++++-------- tests/test.rs | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/bvh/primitive.rs b/src/bvh/primitive.rs index cf2ec6e0..af9ec07a 100644 --- a/src/bvh/primitive.rs +++ b/src/bvh/primitive.rs @@ -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(), } } diff --git a/src/bvh/structure.rs b/src/bvh/structure.rs index 45d67a30..e8fb6574 100644 --- a/src/bvh/structure.rs +++ b/src/bvh/structure.rs @@ -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) { @@ -47,7 +48,7 @@ impl AABB { self.grow_triangle(triangle); } BvhGeometry::Sphere(sphere) => { - self.grow_sphere(sphere); + self.grow_sphere(sphere, primitive.trs); } } } diff --git a/tests/test.rs b/tests/test.rs index 764f0eea..69620bc0 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -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]