From 637abb06ad8afe0b63aa98ef746260aca25a2421 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Tue, 20 Sep 2022 17:47:34 +0200 Subject: [PATCH] Some tries to fix normal directions --- src/color.rs | 8 ++++---- src/hittable_list.rs | 3 ++- src/main.rs | 16 ++++++++-------- src/material.rs | 8 +++++++- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/color.rs b/src/color.rs index 8ed9059..7a87000 100644 --- a/src/color.rs +++ b/src/color.rs @@ -5,10 +5,10 @@ pub fn put_color(img: &mut RgbImage, pixel_color: &Color, x: u32, y: u32, sample let mut g = pixel_color.y(); let mut b = pixel_color.z(); - //let scale = 1.0 / samples_per_pixel as f64; - //r = r/samples_per_pixel as f64; //(scale * r).sqrt(); - //g = g/samples_per_pixel as f64; //(scale * g).sqrt(); - //b = b/samples_per_pixel as f64; //(scale * b).sqrt(); + let scale = 1.0;// / samples_per_pixel as f64; + r = (scale * r).sqrt(); + g = (scale * g).sqrt(); + b = (scale * b).sqrt(); img.put_pixel( x, diff --git a/src/hittable_list.rs b/src/hittable_list.rs index ebbbcc9..e181a54 100644 --- a/src/hittable_list.rs +++ b/src/hittable_list.rs @@ -29,7 +29,8 @@ impl HittableList { for obj in &self.objects { let mut temp_rec = HitRecord::empty(); - if obj.hit(&r, t_min, closest_so_far, &mut temp_rec) { + if obj.hit(&r, t_min, closest_so_far, &mut temp_rec) + && closest_so_far > temp_rec.t { hit_anything = true; closest_so_far = temp_rec.t; *rec = temp_rec; diff --git a/src/main.rs b/src/main.rs index 641503b..6836cc6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,9 +67,9 @@ fn main() { // Image let aspect_ratio = 10.0 / 7.5; //16.0 / 9.0; - let image_width = 600; + let image_width = 300; let image_height = (image_width as f64 / aspect_ratio) as u32; - let samples_per_pixel = 50_u32; + let samples_per_pixel = 1_u32; let max_depth = 50; let antialiasing_threshold = 0.2; // at what diff between two colors will a pixel be antialiased @@ -287,7 +287,7 @@ fn from_obj(path: &str) -> HittableList { // 118.0 / 255.0, //))); //let material = Arc::new(Dielectric::new(2.0)); - let material = Arc::new(Metal::new(&Color::new(0.7, 0.7, 0.8), 0.0)); + let material = Arc::new(Metal::new(&Color::new(0.7, 0.6, 0.5), 0.0)); //let material = Arc::new(Rainbow::new()); let cornell_box = tobj::load_obj(path, &tobj::OFFLINE_RENDERING_LOAD_OPTIONS); @@ -318,8 +318,8 @@ fn from_obj(path: &str) -> HittableList { for v in 0..mesh.indices.len() / 3 { let index_a = mesh.indices[3 * v] as usize; - let index_b = mesh.indices[3 * v + 1] as usize; - let index_c = mesh.indices[3 * v + 2] as usize; + let index_b = mesh.indices[3 * v + 2] as usize; + let index_c = mesh.indices[3 * v + 1] as usize; let index_normal_a = mesh.normal_indices[3 * v] as usize; let index_normal_b = mesh.normal_indices[3 * v + 1] as usize; let index_normal_c = mesh.normal_indices[3 * v + 2] as usize; @@ -327,16 +327,16 @@ fn from_obj(path: &str) -> HittableList { let normal_avg = Vec3::unit_vector( Vec3::new( mesh.positions[3 * index_normal_a] as f64, - mesh.positions[3 * index_normal_a + 1] as f64, mesh.positions[3 * index_normal_a + 2] as f64, + mesh.positions[3 * index_normal_a + 1] as f64, ) + Vec3::new( mesh.positions[3 * index_normal_b] as f64, - mesh.positions[3 * index_normal_b + 1] as f64, mesh.positions[3 * index_normal_b + 2] as f64, + mesh.positions[3 * index_normal_b + 1] as f64, ) + Vec3::new( mesh.positions[3 * index_normal_c] as f64, - mesh.positions[3 * index_normal_c + 1] as f64, mesh.positions[3 * index_normal_c + 2] as f64, + mesh.positions[3 * index_normal_c + 1] as f64, ), ); diff --git a/src/material.rs b/src/material.rs index 409e928..5004352 100644 --- a/src/material.rs +++ b/src/material.rs @@ -77,7 +77,13 @@ impl Material for Metal { *scattered = Ray::new(rec.p, reflected + self.fuzz * Vec3::random_in_unit_sphere()); *attenuation = self.albedo.clone(); - return true;//Vec3::dot(scattered.direction(), rec.normal) > 0.0; + let reflect = Vec3::dot(scattered.direction(), rec.normal); + //dbg!(reflect); + + /*if reflect > 0.0 { + *attenuation = Color::new(100.0, 0.0, 0.0); + }*/ + return reflect > 0.0; } }