From 53f5c8bac54c1a96391143e28a76d1035382d9fa Mon Sep 17 00:00:00 2001 From: bijan2005 Date: Thu, 10 Dec 2020 09:23:45 -0500 Subject: [PATCH] Redefine Scene struct --- src/main.rs | 23 +++++++++++++---------- src/object.rs | 6 +++++- src/object/triangle.rs | 1 - 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 26e6761..8393f05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,20 +11,20 @@ mod camera; use camera::*; mod types; use types::*; mod object; use object::*; -fn trace(ray: Ray, scene: &Scene) -> Option<(&Object, f32)> { - scene.iter() - .filter_map(|obj| obj.intersect(ray) - .map(|x| (obj, x))) - .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal)) +fn trace(ray: Ray, objects: &Vec) -> Option<(&Object, f32)> { + objects.iter() + .filter_map(|obj| obj.intersect(ray) + .map(|x| (obj, x))) + .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal)) } fn cast_ray(ray: Ray, scene: &Scene) -> Color { - if let Some((obj, dist)) = trace(ray, scene) { + if let Some((obj, dist)) = trace(ray, &scene.objects) { let point = ray.project(dist); obj.getcolor(point) } - else { Color::black() } + else { scene.background } } fn render(camera: &Camera, scene: &Scene, filename: &str) -> std::io::Result<()> { @@ -54,9 +54,12 @@ fn main() -> std::io::Result<()> { let camera = Camera::new(Point3::new(0.0,0.0,0.0), Vector3::new(0.0,0.0,1.0), 1.0, 16.0 / 9.0, 2.0, 480); - let scene = vec![ - Object::new_boundless(TriangleMesh::singleton(Point3::new(-1.0, -1.0, 2.0), Point3::new(0.0, 1.0, 2.0), Point3::new(1.0, -1.0, 2.0), |t, u, v| Color::new(t, u, v))) - ]; + let scene = Scene { + objects: vec![ + Object::new(TriangleMesh::singleton(Point3::new(-1.0, -1.0, 2.0), Point3::new(0.0, 1.0, 2.0), Point3::new(1.0, -1.0, 2.0), |t, u, v| Color::new(t, u, v))) + ], + background: Color::black() + }; render(&camera, &scene, "out.ppm") } diff --git a/src/object.rs b/src/object.rs index 50bf2fb..0e82a52 100644 --- a/src/object.rs +++ b/src/object.rs @@ -70,7 +70,11 @@ impl Object { pub fn getcolor(&self, point: Point3) -> Color { self.surface.getcolor(point) } } -pub type Scene = Vec; +pub struct Scene { + pub objects: Vec, + + pub background: Color +} #[cfg(test)] mod tests { diff --git a/src/object/triangle.rs b/src/object/triangle.rs index 06376b1..bface56 100644 --- a/src/object/triangle.rs +++ b/src/object/triangle.rs @@ -205,7 +205,6 @@ impl Surface for TriangleMesh { } fn smallest_sphere(points: Vec<&Point3>, boundary: Vec<&Point3>) -> (Point3, f32) { - println!("{:?}\n{:?}\n", points, boundary); if points.len() == 0 || boundary.len() == 4 { match boundary.len() { 0 => (Point3::new(0.0, 0.0, 0.0), 0.0),