From c2d16937ffb07f7ea9d9c64ce48ee7c050f04960 Mon Sep 17 00:00:00 2001 From: "max.nuding" Date: Fri, 8 Jul 2022 13:59:28 +0200 Subject: [PATCH] Using convert for Lambertian material --- src/main.rs | 12 ++++++------ src/material.rs | 9 ++++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6ab287c..57135db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ mod rect; const ASPECT_RATIO: f64 = 3.0 / 2.0; const IMAGE_WIDTH: usize = 600; const IMAGE_HEIGHT: usize = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as usize; -const SAMPLES_PER_PIXEL: i32 = 800; +const SAMPLES_PER_PIXEL: i32 = 100; const MAX_DEPTH: i32 = 50; struct Scene { @@ -47,11 +47,11 @@ struct Scene { fn cornell_box() -> Scene { let mut world:HittableList = Vec::new(); let red = Material::Lambertian( - Lambertian::new(Color::new(0.65, 0.05, 0.05))); + Lambertian::from(Color::new(0.65, 0.05, 0.05))); let white = Arc::new(Material::Lambertian( - Lambertian::new(Color::new(0.73, 0.73, 0.73)))); + Lambertian::from(Color::new(0.73, 0.73, 0.73)))); let green = Material::Lambertian( - Lambertian::new(Color::new(0.12, 0.45, 0.15))); + Lambertian::from(Color::new(0.12, 0.45, 0.15))); let light = Material::DiffuseLight( DiffuseLight::from(Color::new(15.0, 15.0, 15.0))); world.push(Arc::new(Rect2D::new( @@ -352,7 +352,7 @@ fn random_scene() -> Scene { continue; } let material = match choose_material { - _ if choose_material < 0.8 => Arc::new(Material::Lambertian(Lambertian::new( + _ if choose_material < 0.8 => Arc::new(Material::Lambertian(Lambertian::from( Color::random(0.0, 1.0) * Color::random(0.0, 1.0)))), _ if choose_material < 0.95 => Arc::new(Material::Metal(Metal::new( Color::random(0.5, 1.0), @@ -387,7 +387,7 @@ fn random_scene() -> Scene { radius: 1.0, material: material1 })); - let material2 = Arc::new(Material::Lambertian(Lambertian::new(Color::new(0.4, 0.2, 0.1)))); + let material2 = Arc::new(Material::Lambertian(Lambertian::from(Color::new(0.4, 0.2, 0.1)))); world.push(Arc::new(Sphere { center: Point3::new(-4.0, 1.0, 0.0), radius: 1.0, diff --git a/src/material.rs b/src/material.rs index 709cd73..09981f8 100644 --- a/src/material.rs +++ b/src/material.rs @@ -18,7 +18,7 @@ pub enum Material { impl Default for Material { fn default() -> Self { - Material::Lambertian(Lambertian::new(Color::default())) + Material::Lambertian(Lambertian::from(Color::default())) } } @@ -63,11 +63,14 @@ impl DiffuseLight { pub struct Lambertian { pub albedo: Arc } -impl Lambertian { - pub fn new(albedo: Color) -> Self { + +impl From for Lambertian { + fn from(albedo: Color) -> Self { let texture = SolidColor::from(albedo); Lambertian { albedo: Arc::new(texture) } } +} +impl Lambertian { pub fn textured(albedo: Arc) -> Self { Lambertian { albedo } }