diff --git a/src/main.rs b/src/main.rs index 73e125f..4c9838b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,6 @@ use render::{World,Camera}; use vec3::{Point3,Vec3}; use material::{Metal,Lambertian,Material}; -use std::sync::Arc; fn main() { @@ -19,16 +18,16 @@ fn main() { let viewport_height = 2.0; let m = Lambertian::new(Vec3::new(0.5,0.5,0.5)); - let world_m: Arc = Arc::new(m); + let world_m: Box = Box::new(m); let m = Lambertian::new(Vec3::new(0.1, 0.2, 0.5)); - let center_m: Arc = Arc::new(m); + let center_m: Box = Box::new(m); let m = Metal::new(Vec3::new(0.8, 0.8, 0.8), 0.3); - let left_m: Arc = Arc::new(m); + let left_m: Box = Box::new(m); let m = Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.5); - let right_m: Arc = Arc::new(m); + let right_m: Box = Box::new(m); let mut camera = Camera::new(aspect_ratio,image_width,focal_length,Point3::new(-1.0,1.0,1.0),Point3::new(0.0,0.0,-1.0),90.0); let mut world = World::new(); diff --git a/src/object.rs b/src/object.rs index 3eea3ff..5ebe938 100644 --- a/src/object.rs +++ b/src/object.rs @@ -2,22 +2,21 @@ use crate::vec3::{Point3, Ray, Vec3, Interval}; use crate::material::{Material,Scatter}; use rand::prelude::*; -use std::sync::Arc; pub trait Hittable: Send + Sync{ - fn hit(&self, ray: Ray, interval:Interval) -> Option; + fn hit(&self, ray: Ray, interval:Interval) -> Option>; } -pub struct HitRecord { +pub struct HitRecord<'a> { point: Point3, normal: Vec3, root: f64, front_face: bool, - material: Arc, + material: &'a dyn Material, } -impl HitRecord { +impl HitRecord <'_> { pub fn normal(&self) -> Vec3 { self.normal } @@ -44,17 +43,17 @@ impl HitRecord { pub struct Sphere { radius: f64, center: Point3, - material: Arc + material: Box } impl Sphere { - pub fn new(radius: f64, center: Point3, material: Arc) -> Self { + pub fn new(radius: f64, center: Point3, material: Box) -> Self { Sphere { radius, center , material} } } impl Hittable for Sphere { - fn hit(&self, ray: Ray, interval:Interval) -> Option { + fn hit(&self, ray: Ray, interval:Interval) -> Option> { let oc = self.center - ray.origin(); let a = ray.direction().length_squared(); let h = Vec3::dot(ray.direction(), oc.to_vec()); @@ -80,7 +79,7 @@ impl Hittable for Sphere { point, normal, front_face: false, - material: self.material.clone() + material: self.material.as_ref(), }; tmp.set_face_normal(ray, normal); Some(tmp) diff --git a/src/render.rs b/src/render.rs index f4477e5..142fcf4 100644 --- a/src/render.rs +++ b/src/render.rs @@ -21,7 +21,7 @@ impl World { self.objects.push(Arc::new(object)); } - pub fn hit(&self, ray: Ray, interval:Interval) -> Option { + pub fn hit(&self, ray: Ray, interval:Interval) -> Option> { let mut hit_anything = None; let mut closest_so_far = interval.max();