From d6e92fa2ba7e4f92826a31df3174e5e775b63f76 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Wed, 8 Jun 2022 19:53:37 +0200 Subject: [PATCH] Add some materials, move some spheres --- src/main.rs | 32 ++++++++++++++++++-------------- src/material.rs | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9e92e40..484e2a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ use std::env; use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::Arc; use vec3::{Color, Point3, Vec3}; -use material::{Material, Lambertian, Metal}; +use material::{Material, Lambertian, Metal, Mirror}; fn ray_color(r: &Ray, world: &HittableList, depth: u32) -> Color { let mut rec = HitRecord::empty(); @@ -50,7 +50,7 @@ fn main() { // Image let aspect_ratio = 16.0 / 9.0; - let image_width = 1920; + let image_width = 1000; let image_height = (image_width as f64 / aspect_ratio) as u32; let samples_per_pixel = 100_u32; let max_depth = 50; @@ -60,9 +60,9 @@ fn main() { let material_ground = Arc::new(Lambertian::new(&Color::new(0.8, 0.8, 0.0))); let material_center = Arc::new(Lambertian::new(&Color::new(0.7, 0.1, 0.2))); - let material_left = Arc::new(Metal::new(&Color::new(0.8, 0.8, 0.8))); - let material_right = Arc::new(Metal::new(&Color::new(0.8, 0.6, 0.2))); - let material_mirror = Arc::new(Metal::new(&Color::new(0.9, 0.9, 0.9))); + let material_blue = Arc::new(Lambertian::new(&Color::new(0.2, 0.1, 0.7))); + let material_metal = Arc::new(Metal::new(&Color::new(0.8, 0.8, 0.8))); + let _material_mirror = Arc::new(Mirror::new(&Color::new(0.99, 0.99, 0.99))); @@ -72,26 +72,30 @@ fn main() { material_ground.clone(), ))); world.add(Box::::new(Sphere::new( - Point3::new(0.0, 0.0, -1.0), - 0.5, + Point3::new(0.0, -0.1, -1.0), + 0.4, material_center.clone(), ))); world.add(Box::::new(Sphere::new( Point3::new(-1.0, 0.0, -1.0), 0.5, - material_mirror.clone(), + material_metal.clone(), ))); world.add(Box::::new(Sphere::new( - Point3::new(1.0, 0.0, -1.0), - 0.5, - material_mirror.clone(), + Point3::new(1.3, 0.3, -1.5), + 0.8, + material_metal.clone(), ))); world.add(Box::::new(Sphere::new( - Point3::new(-1.0, 1.1, -1.2), + Point3::new(-1.5, 1.3, -1.7), 0.4, - material_left.clone(), + material_metal.clone(), + ))); + world.add(Box::::new(Sphere::new( + Point3::new(-0.5, 0.5, 1.0), + 1.0, + material_blue.clone(), ))); - /* for i in -15..15 { for j in -15..15 { diff --git a/src/material.rs b/src/material.rs index 0f003ea..e2f0e93 100644 --- a/src/material.rs +++ b/src/material.rs @@ -3,6 +3,7 @@ use super::{ HitRecord, Color, Vec3, + utility, }; pub struct Lambertian { @@ -13,6 +14,11 @@ pub struct Metal { albedo: Color, } +pub struct Mirror { + albedo: Color, +} + + pub trait Material: Sync + Send { fn scatter(&self,r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool; } @@ -45,10 +51,6 @@ impl Metal { albedo: *a, } } - - pub fn clone(&self) -> Self { - Self::new(&self.albedo.clone()) - } } impl Material for Metal { @@ -60,4 +62,32 @@ impl Material for Metal { return Vec3::dot(scattered.direction(), rec.normal) > 0.0; } +} + +impl Mirror { + pub fn new(a: &Color) -> Self { + Mirror { + albedo: *a, + } + } +} + +impl Material for Mirror { + fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool { + if utility::random_f64() > 0.8 { // Reflektiert + let reflected = Vec3::reflect(&Vec3::unit_vector(r_in.direction()), &rec.normal); + *scattered = Ray::new(rec.p, reflected); + *attenuation = self.albedo.clone(); + + return Vec3::dot(scattered.direction(), rec.normal) > 0.0; + + } else { // Geht geradeaus durch + let reflected = r_in.direction().clone(); + *scattered = Ray::new(rec.p, reflected); + + *attenuation = self.albedo.clone(); + + return true; + }; + } } \ No newline at end of file