Using convert for Lambertian material

This commit is contained in:
max.nuding 2022-07-08 13:59:28 +02:00
parent 429b87e912
commit c2d16937ff
Failed to extract signature
2 changed files with 12 additions and 9 deletions

View File

@ -35,7 +35,7 @@ mod rect;
const ASPECT_RATIO: f64 = 3.0 / 2.0; const ASPECT_RATIO: f64 = 3.0 / 2.0;
const IMAGE_WIDTH: usize = 600; const IMAGE_WIDTH: usize = 600;
const IMAGE_HEIGHT: usize = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as usize; 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; const MAX_DEPTH: i32 = 50;
struct Scene { struct Scene {
@ -47,11 +47,11 @@ struct Scene {
fn cornell_box() -> Scene { fn cornell_box() -> Scene {
let mut world:HittableList = Vec::new(); let mut world:HittableList = Vec::new();
let red = Material::Lambertian( 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( 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( 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( let light = Material::DiffuseLight(
DiffuseLight::from(Color::new(15.0, 15.0, 15.0))); DiffuseLight::from(Color::new(15.0, 15.0, 15.0)));
world.push(Arc::new(Rect2D::new( world.push(Arc::new(Rect2D::new(
@ -352,7 +352,7 @@ fn random_scene() -> Scene {
continue; continue;
} }
let material = match choose_material { 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)))), Color::random(0.0, 1.0) * Color::random(0.0, 1.0)))),
_ if choose_material < 0.95 => Arc::new(Material::Metal(Metal::new( _ if choose_material < 0.95 => Arc::new(Material::Metal(Metal::new(
Color::random(0.5, 1.0), Color::random(0.5, 1.0),
@ -387,7 +387,7 @@ fn random_scene() -> Scene {
radius: 1.0, radius: 1.0,
material: material1 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 { world.push(Arc::new(Sphere {
center: Point3::new(-4.0, 1.0, 0.0), center: Point3::new(-4.0, 1.0, 0.0),
radius: 1.0, radius: 1.0,

View File

@ -18,7 +18,7 @@ pub enum Material {
impl Default for Material { impl Default for Material {
fn default() -> Self { 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 struct Lambertian {
pub albedo: Arc<dyn Texture> pub albedo: Arc<dyn Texture>
} }
impl Lambertian {
pub fn new(albedo: Color) -> Self { impl From<Color> for Lambertian {
fn from(albedo: Color) -> Self {
let texture = SolidColor::from(albedo); let texture = SolidColor::from(albedo);
Lambertian { albedo: Arc::new(texture) } Lambertian { albedo: Arc::new(texture) }
} }
}
impl Lambertian {
pub fn textured(albedo: Arc<dyn Texture>) -> Self { pub fn textured(albedo: Arc<dyn Texture>) -> Self {
Lambertian { albedo } Lambertian { albedo }
} }