diff --git a/src/main.rs b/src/main.rs index 6c6428b..0e3ee25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use render::{World,Camera}; use vec3::{Point3,Vec3}; use material::{Metal,Lambertian,Material}; - +use std::sync::Arc; fn main() { @@ -21,21 +21,17 @@ fn main() { let m = Lambertian::new(Vec3::new(0.5,0.5,0.5)); - let b: Box = Box::new(m); - let world_m = std::rc::Rc::new(b); + let world_m: Arc = Arc::new(m); let m = Lambertian::new(Vec3::new(0.1, 0.2, 0.5)); - let b: Box = Box::new(m); - let center_m = std::rc::Rc::new(b); + let center_m: Arc = Arc::new(m); let m = Metal::new(Vec3::new(0.8, 0.8, 0.8), 0.3); - let b: Box = Box::new(m); - let left_m = std::rc::Rc::new(b); + let left_m: Arc = Arc::new(m); let m = Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.5); - let b: Box = Box::new(m); - let right_m = std::rc::Rc::new(b); + let right_m: Arc = Arc::new(m); let mut camera = Camera::new(aspect_ratio,image_width,focal_length,viewport_height,camera_center); let mut world = World::new(); diff --git a/src/object.rs b/src/object.rs index b3c5852..c465e72 100644 --- a/src/object.rs +++ b/src/object.rs @@ -2,6 +2,7 @@ use crate::vec3::{Point3, Ray, Vec3, Interval}; use crate::material::{Material,Scatter}; use rand::prelude::*; +use std::sync::Arc; pub trait Hittable { @@ -13,7 +14,7 @@ pub struct HitRecord { normal: Vec3, root: f64, front_face: bool, - material: std::rc::Rc>, + material: Arc, } impl HitRecord { @@ -43,11 +44,11 @@ impl HitRecord { pub struct Sphere { radius: f64, center: Point3, - material: std::rc::Rc> + material: Arc } impl Sphere { - pub fn new(radius: f64, center: Point3, material:std::rc::Rc>) -> Self { + pub fn new(radius: f64, center: Point3, material: Arc) -> Self { Sphere { radius, center , material} } } diff --git a/src/render.rs b/src/render.rs index c28d22c..a13581f 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,11 +1,12 @@ use crate::object::{HitRecord, Hittable}; use crate::vec3::{Interval, Ray, Vec3, Point3}; use crate::image::Image; +use std::sync::Arc; use rand::prelude::*; pub struct World { - pub objects: Vec>, + pub objects: Vec>, } impl World { @@ -16,7 +17,7 @@ impl World { } pub fn add(&mut self, object: impl Hittable + 'static) { - self.objects.push(Box::new(object)); + self.objects.push(Arc::new(object)); } pub fn hit(&self, ray: Ray, interval:Interval) -> Option {