diff --git a/src/main.rs b/src/main.rs index 0e3ee25..6c6428b 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,17 +21,21 @@ fn main() { let m = Lambertian::new(Vec3::new(0.5,0.5,0.5)); - let world_m: Arc = Arc::new(m); + let b: Box = Box::new(m); + let world_m = std::rc::Rc::new(b); let m = Lambertian::new(Vec3::new(0.1, 0.2, 0.5)); - let center_m: Arc = Arc::new(m); + let b: Box = Box::new(m); + let center_m = std::rc::Rc::new(b); let m = Metal::new(Vec3::new(0.8, 0.8, 0.8), 0.3); - let left_m: Arc = Arc::new(m); + let b: Box = Box::new(m); + let left_m = std::rc::Rc::new(b); let m = Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.5); - let right_m: Arc = Arc::new(m); + let b: Box = Box::new(m); + let right_m = std::rc::Rc::new(b); let mut camera = Camera::new(aspect_ratio,image_width,focal_length,viewport_height,camera_center); let mut world = World::new(); diff --git a/src/material.rs b/src/material.rs index 4ce924c..c7d0efb 100644 --- a/src/material.rs +++ b/src/material.rs @@ -9,7 +9,7 @@ pub struct Scatter { pub ray: Ray, } -pub trait Material: Send + Sync { +pub trait Material { fn scatter(&self, r_in: &Ray, rec: &HitRecord, rng: &mut ThreadRng) -> Option; } diff --git a/src/object.rs b/src/object.rs index 3eea3ff..b3c5852 100644 --- a/src/object.rs +++ b/src/object.rs @@ -2,10 +2,9 @@ use crate::vec3::{Point3, Ray, Vec3, Interval}; use crate::material::{Material,Scatter}; use rand::prelude::*; -use std::sync::Arc; -pub trait Hittable: Send + Sync{ +pub trait Hittable { fn hit(&self, ray: Ray, interval:Interval) -> Option; } @@ -14,7 +13,7 @@ pub struct HitRecord { normal: Vec3, root: f64, front_face: bool, - material: Arc, + material: std::rc::Rc>, } impl HitRecord { @@ -44,11 +43,11 @@ impl HitRecord { pub struct Sphere { radius: f64, center: Point3, - material: Arc + material: std::rc::Rc> } impl Sphere { - pub fn new(radius: f64, center: Point3, material: Arc) -> Self { + pub fn new(radius: f64, center: Point3, material:std::rc::Rc>) -> Self { Sphere { radius, center , material} } } diff --git a/src/render.rs b/src/render.rs index 91763e3..c28d22c 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,13 +1,11 @@ use crate::object::{HitRecord, Hittable}; use crate::vec3::{Interval, Ray, Vec3, Point3}; use crate::image::Image; -use std::sync::Arc; -use std::thread; use rand::prelude::*; pub struct World { - pub objects: Vec>, + pub objects: Vec>, } impl World { @@ -18,7 +16,7 @@ impl World { } pub fn add(&mut self, object: impl Hittable + 'static) { - self.objects.push(Arc::new(object)); + self.objects.push(Box::new(object)); } pub fn hit(&self, ray: Ray, interval:Interval) -> Option { @@ -71,53 +69,29 @@ impl Camera { pub fn render(&mut self, world: &World){ - let thread_count = thread::available_parallelism().unwrap().get() as i32; - let height = self.img.height; - let width = self.img.width; - let total_pixels = height*width; - let num_samples = 5; + let mut rng:ThreadRng = rand::rng(); + for y in 0..self.img.height { + for x in 0..self.img.width { + let (mut r,mut g,mut b) = (0.0,0.0,0.0); + let num_samples=10.0; + let depth=10; + for _ in 0..num_samples as i32{ + let ray = self.get_ray(x,y,&mut rng); + let (tr,tg,tb)= trace(ray, &world,depth,&mut rng); + r+= tr; + g+= tg; + b+= tb; + } + r /= num_samples; + g /= num_samples; + b /= num_samples; - //cast to const ref - let const_self = &*self; - - let threads:Vec> = thread::scope(|s|{ - (0..thread_count).map(|i|{ - s.spawn(move ||{ - let mut rng:ThreadRng = rand::rng(); - let start = (i*total_pixels)/thread_count; - let end = ((i+1)*total_pixels)/thread_count; - (start..end).map(|i|{ - let x = i%width; - let y = i/width; - let (mut r,mut g,mut b) = (0.0,0.0,0.0); - let ray = const_self.get_ray(x,y,&mut rng); - for _ in 0..num_samples{ - let (tr,tg,tb) = trace(ray, &world,10,&mut rng); - r+=tr; - g+=tg; - b+=tb; - } - - r/=num_samples as f64; - g/=num_samples as f64; - b/=num_samples as f64; - - r = r.clamp(0.0,1.0).sqrt(); - g = g.clamp(0.0,1.0).sqrt(); - b = b.clamp(0.0,1.0).sqrt(); - - ((x,y),(r,g,b)) - }).collect::>() - }) - }).collect::>().into_iter().map(|t|{t.join().unwrap()}).collect() - }); - - for t in threads { - for c in t { - let ((x,y),rgb) = c; - let px = self.img.get_pixel(x,y).unwrap(); - let (r,g,b)= rgb; - px.set_color(r,g,b); + r = r.clamp(0.0,1.0).sqrt(); + g = g.clamp(0.0,1.0).sqrt(); + b = b.clamp(0.0,1.0).sqrt(); + if let Some(px) = self.img.get_pixel(x,y){ + px.set_color(r,g,b); + } else{/*ignore*/} } } self.img.save("./foo.ppm").unwrap();