Some tries to fix normal directions

This commit is contained in:
Jonathan Flueren 2022-09-20 17:47:34 +02:00
parent e3cb37817c
commit 637abb06ad
4 changed files with 21 additions and 14 deletions

View file

@ -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 g = pixel_color.y();
let mut b = pixel_color.z(); let mut b = pixel_color.z();
//let scale = 1.0 / samples_per_pixel as f64; let scale = 1.0;// / samples_per_pixel as f64;
//r = r/samples_per_pixel as f64; //(scale * r).sqrt(); r = (scale * r).sqrt();
//g = g/samples_per_pixel as f64; //(scale * g).sqrt(); g = (scale * g).sqrt();
//b = b/samples_per_pixel as f64; //(scale * b).sqrt(); b = (scale * b).sqrt();
img.put_pixel( img.put_pixel(
x, x,

View file

@ -29,7 +29,8 @@ impl HittableList {
for obj in &self.objects { for obj in &self.objects {
let mut temp_rec = HitRecord::empty(); 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; hit_anything = true;
closest_so_far = temp_rec.t; closest_so_far = temp_rec.t;
*rec = temp_rec; *rec = temp_rec;

View file

@ -67,9 +67,9 @@ fn main() {
// Image // Image
let aspect_ratio = 10.0 / 7.5; //16.0 / 9.0; 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 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 max_depth = 50;
let antialiasing_threshold = 0.2; // at what diff between two colors will a pixel be antialiased 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, // 118.0 / 255.0,
//))); //)));
//let material = Arc::new(Dielectric::new(2.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 material = Arc::new(Rainbow::new());
let cornell_box = tobj::load_obj(path, &tobj::OFFLINE_RENDERING_LOAD_OPTIONS); 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 { for v in 0..mesh.indices.len() / 3 {
let index_a = mesh.indices[3 * v] as usize; let index_a = mesh.indices[3 * v] as usize;
let index_b = mesh.indices[3 * v + 1] as usize; let index_b = mesh.indices[3 * v + 2] as usize;
let index_c = 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_a = mesh.normal_indices[3 * v] as usize;
let index_normal_b = mesh.normal_indices[3 * v + 1] 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; 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( let normal_avg = Vec3::unit_vector(
Vec3::new( Vec3::new(
mesh.positions[3 * index_normal_a] as f64, 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 + 2] as f64,
mesh.positions[3 * index_normal_a + 1] as f64,
) + Vec3::new( ) + Vec3::new(
mesh.positions[3 * index_normal_b] as f64, 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 + 2] as f64,
mesh.positions[3 * index_normal_b + 1] as f64,
) + Vec3::new( ) + Vec3::new(
mesh.positions[3 * index_normal_c] as f64, 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 + 2] as f64,
mesh.positions[3 * index_normal_c + 1] as f64,
), ),
); );

View file

@ -77,7 +77,13 @@ impl Material for Metal {
*scattered = Ray::new(rec.p, reflected + self.fuzz * Vec3::random_in_unit_sphere()); *scattered = Ray::new(rec.p, reflected + self.fuzz * Vec3::random_in_unit_sphere());
*attenuation = self.albedo.clone(); *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;
} }
} }