library: use beevee's Intersected trait

This commit is contained in:
Bruno BELANYI 2020-03-24 21:37:00 +01:00
parent 21c7aea1c0
commit 6df2d857d4
3 changed files with 19 additions and 4 deletions

View file

@ -4,7 +4,11 @@ use crate::material::MaterialEnum;
use crate::shape::{Shape, ShapeEnum}; use crate::shape::{Shape, ShapeEnum};
use crate::texture::TextureEnum; use crate::texture::TextureEnum;
use crate::Point; use crate::Point;
use beevee::aabb::{Bounded, AABB}; use beevee::{
aabb::{Bounded, AABB},
bvh::Intersected,
ray::Ray,
};
use serde::Deserialize; use serde::Deserialize;
/// An object being rendered in the scene. /// An object being rendered in the scene.
@ -66,6 +70,12 @@ impl Bounded for Object {
} }
} }
impl Intersected for Object {
fn intersect(&self, ray: &Ray) -> Option<f32> {
self.shape.intersect(ray)
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View file

@ -149,9 +149,7 @@ impl Scene {
} }
fn cast_ray(&self, ray: Ray) -> Option<(f32, &Object)> { fn cast_ray(&self, ray: Ray) -> Option<(f32, &Object)> {
self.bvh self.bvh.walk(&ray, &self.objects)
.walk(&ray, &self.objects)
.and_then(|o| o.shape.intersect(&ray).map(|t| (t, o)))
} }
fn color_at( fn color_at(

View file

@ -3,6 +3,7 @@
use super::{Point, Point2D, Vector}; use super::{Point, Point2D, Vector};
use beevee::{ use beevee::{
aabb::{Bounded, AABB}, aabb::{Bounded, AABB},
bvh::Intersected,
ray::Ray, ray::Ray,
}; };
use nalgebra::Unit; use nalgebra::Unit;
@ -44,6 +45,12 @@ impl Bounded for dyn Shape {
} }
} }
impl Intersected for dyn Shape {
fn intersect(&self, ray: &Ray) -> Option<f32> {
self.intersect(ray)
}
}
mod sphere; mod sphere;
pub use sphere::*; pub use sphere::*;