library: render: scene: use BVH

This commit is contained in:
Bruno BELANYI 2020-03-18 18:20:46 +01:00
parent eca5ec5c26
commit ca21cf06c2

View file

@ -6,18 +6,18 @@ use crate::{
texture::Texture, texture::Texture,
{Point, Point2D, Vector}, {Point, Point2D, Vector},
}; };
use bvh::ray::Ray; use bvh::{bvh::BVH, ray::Ray};
use image::RgbImage; use image::RgbImage;
use rand::prelude::thread_rng; use rand::prelude::thread_rng;
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
/// Represent the scene being rendered. /// Represent the scene being rendered.
#[derive(Debug, PartialEq)]
pub struct Scene { pub struct Scene {
camera: Camera, camera: Camera,
lights: LightAggregate, lights: LightAggregate,
objects: Vec<Object>, objects: Vec<Object>,
bvh: BVH,
aliasing_limit: u32, aliasing_limit: u32,
reflection_limit: u32, reflection_limit: u32,
} }
@ -26,14 +26,16 @@ impl Scene {
pub fn new( pub fn new(
camera: Camera, camera: Camera,
lights: LightAggregate, lights: LightAggregate,
objects: Vec<Object>, mut objects: Vec<Object>,
aliasing_limit: u32, aliasing_limit: u32,
reflection_limit: u32, reflection_limit: u32,
) -> Self { ) -> Self {
let bvh = BVH::build(&mut objects);
Scene { Scene {
camera, camera,
lights, lights,
objects, objects,
bvh,
aliasing_limit, aliasing_limit,
reflection_limit, reflection_limit,
} }
@ -82,7 +84,7 @@ impl Scene {
// NOTE(Bruno): should be written using iterators // NOTE(Bruno): should be written using iterators
let mut shot_obj: Option<&Object> = None; let mut shot_obj: Option<&Object> = None;
let mut t = std::f32::INFINITY; let mut t = std::f32::INFINITY;
for object in self.objects.iter() { for object in self.bvh.traverse(&ray, &self.objects).iter() {
match object.shape.intersect(&ray) { match object.shape.intersect(&ray) {
Some(dist) if dist < t => { Some(dist) if dist < t => {
t = dist; t = dist;