diff --git a/example.png b/example.png index 91701cc..979ba45 100644 Binary files a/example.png and b/example.png differ diff --git a/src/main.rs b/src/main.rs index f4aa9e9..d6d7d29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,15 +28,15 @@ use crate::{ fn random_scene() -> HittableList { let mut world = HittableList::new_empty(); - static GROUND_MATERIAL: Lambertian = Lambertian::new(Color::new(0.5, 0.5, 0.5)); + let ground_material: Lambertian = Lambertian::new(Color::new(0.5, 0.5, 0.5)); world.add(Box::new(Sphere::new( Point3::new(0.0, -1000.0, 0.0), 1000.0, - &GROUND_MATERIAL, + Box::new(ground_material), ))); - for a in 0..12 { - for b in 0..12 { + for a in -12..12 { + for b in -12..12 { let choose_mat = random_f64(); let center = Point3::new( a as f64 + 0.9 * random_f64(), @@ -47,38 +47,38 @@ fn random_scene() -> HittableList { if (center - Point3::new(4.0, 0.2, 0.0)).length() > 0.9 { if choose_mat < 0.8 { //let albedo = Color::new(0.3, 0.8, 0.4); - static SPHERE_MATERIAL: Lambertian = Lambertian::new(Color::new(0.3, 0.8, 0.4)); - world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL))); + let sphere_material: Lambertian = Lambertian::new(Color::random()); + world.add(Box::new(Sphere::new(center, 0.2, Box::new(sphere_material)))); } else if choose_mat < 0.95 { - static SPHERE_MATERIAL: Metal = Metal::new(Color::new(0.3, 0.6, 0.2), 0.2); - world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL))); + let sphere_material: Metal = Metal::new(Color::random(), 0.2); + world.add(Box::new(Sphere::new(center, 0.2, Box::new(sphere_material)))); } else { - static SPHERE_MATERIAL: Dielectric = Dielectric::new(1.5); - world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL))); + let sphere_material: Dielectric = Dielectric::new(1.5); + world.add(Box::new(Sphere::new(center, 0.2, Box::new(sphere_material)))); } } } } - static MATERIAL1: Dielectric = Dielectric::new(1.5); + let material1: Dielectric = Dielectric::new(1.5); world.add(Box::new(Sphere::new( Point3::new(0.0, 1.0, 0.0), 1.0, - &MATERIAL1, + Box::new(material1), ))); - static MATERIAL2: Lambertian = Lambertian::new(Color::new(0.4, 0.2, 0.1)); + let material2: Lambertian = Lambertian::new(Color::new(0.4, 0.2, 0.1)); world.add(Box::new(Sphere::new( Point3::new(-4.0, 1.0, 0.0), 1.0, - &MATERIAL2, + Box::new(material2), ))); - static MATERIAL3: Metal = Metal::new(Color::new(0.7, 0.6, 0.5), 0.0); + let material3: Metal = Metal::new(Color::new(0.7, 0.6, 0.5), 0.0); world.add(Box::new(Sphere::new( Point3::new(4.0, 1.0, 0.0), 1.0, - &MATERIAL3, + Box::new(material3), ))); world @@ -112,10 +112,10 @@ fn ray_color(r: Ray, world: &dyn Hittable, depth: i32) -> Color { fn main() { // Image let aspect_ratio: f64 = 16. / 9.; - let image_width: i32 = 2560; + let image_width: i32 = 1920; let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32; - let samples_per_pixel: i32 = 5; - let max_depth: i32 = 4; + let samples_per_pixel: i32 = 200; + let max_depth: i32 = 5; // World let world = random_scene(); diff --git a/src/sphere.rs b/src/sphere.rs index 702e72b..6c9e9e3 100644 --- a/src/sphere.rs +++ b/src/sphere.rs @@ -1,21 +1,24 @@ +use std::ops::Deref; + use crate::hittable::*; use crate::material::Material; use crate::vec3::*; +use crate::Ray; -pub struct Sphere<'a> { +pub struct Sphere { center: Point3, radius: f64, - mat_ptr: &'a dyn Material, + mat_ptr: Box, } -impl Sphere<'_> { - pub fn new(center: Point3, radius: f64, mat_ptr: &'static T) -> Self { +impl Sphere { + pub fn new(center: Point3, radius: f64, mat_ptr: Box) -> Self { Sphere {center, radius, mat_ptr} } } -impl Hittable for Sphere<'_> { - fn hit(&self, r: &crate::ray::Ray, t_min: f64, t_max: f64) -> Option> { +impl Hittable for Sphere { + fn hit(&self, r: &Ray, t_min: f64, t_max: f64) -> Option> { let oc = r.origin() - self.center; let a = r.direction().length_squared(); let half_b = oc.dot(&r.direction()); @@ -51,7 +54,7 @@ impl Hittable for Sphere<'_> { p: p, normal: outward_normal, front_face, - mat_ptr: self.mat_ptr, + mat_ptr: self.mat_ptr.deref(), }) } } \ No newline at end of file