From decae9d00de6e54ce7bf85ba880757e6f79a56ff Mon Sep 17 00:00:00 2001 From: JonOfUs Date: Wed, 1 Jun 2022 17:01:16 +0200 Subject: [PATCH] add different diffuse renderer --- src/main.rs | 4 ++-- src/vec3.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a9bbc3..e525991 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ fn ray_color(r: &Ray, world: &HittableList, depth: u32) -> Color { } if world.hit(r, 0.0, f64::INFINITY, &mut rec) { - let target = rec.p + rec.normal + Vec3::random_in_unit_sphere(); + let target = rec.p + rec.normal + Vec3::random_unit_vector(); // rec.p + rec.normal.random_in_hemisphere(); return 0.5 * ray_color(&Ray::new(rec.p, target - rec.p), world, depth-1); } let unit_direction = r.direction(); @@ -43,7 +43,7 @@ fn main() { let aspect_ratio = 16.0 / 9.0; let image_width = 1000; let image_height = (image_width as f64 / aspect_ratio) as u32; - let samples_per_pixel = 10_u32; + let samples_per_pixel = 50_u32; let max_depth = 50; // World diff --git a/src/vec3.rs b/src/vec3.rs index f694aaf..67c3c86 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -54,6 +54,19 @@ impl Vec3 { } } + pub fn random_unit_vector() -> Self { + Self::unit_vector(Self::random_in_unit_sphere()) + } + + pub fn random_in_hemisphere(&self) -> Self { + let in_unit_sphere = Self::random_in_unit_sphere(); + if Self::dot(in_unit_sphere, *self) > 0.0 { + return in_unit_sphere; + } else { + return -1.0*in_unit_sphere; + } + } + pub fn x(&self) -> f64 { self.e[0] }