From 00dae425d9adfd56c97b7ff5fb2b2c4aa141187d Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Wed, 8 Apr 2020 18:05:11 +0200 Subject: [PATCH] library: render: write passes in steps --- pathtracer/src/render/pathtrace/pathtracer.rs | 19 ++++++++++++++----- pathtracer/src/render/utils.rs | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pathtracer/src/render/pathtrace/pathtracer.rs b/pathtracer/src/render/pathtrace/pathtracer.rs index 193024b..b052642 100644 --- a/pathtracer/src/render/pathtrace/pathtracer.rs +++ b/pathtracer/src/render/pathtrace/pathtracer.rs @@ -33,6 +33,7 @@ impl Pathtracer { /// /// [`Scene`]: ../scene/scene/struct.Scene.html pub fn render(&self) -> RgbImage { + let steps = vec![1, 5, 50]; let (width, height) = ( self.scene.camera.film().width(), self.scene.camera.film().height(), @@ -42,7 +43,7 @@ impl Pathtracer { let p = super::super::progress::get_passes_progressbar(self.scene.shot_rays); // Ensure at least one round of shots - let img_buf = (0..self.scene.shot_rays.max(1)) + let (img_buf, _) = (0..self.scene.shot_rays.max(1)) .progress_with(p) .map(|_| { let mut buffer: Vec = Vec::new(); @@ -72,18 +73,26 @@ impl Pathtracer { { let mut vec = Vec::new(); vec.resize_with(total as usize, LinearColor::black); - vec + let count = 0usize; + (vec, count) }, - |mut acc, buf| { + |(mut acc, count), buf| { + if steps.contains(&count) { + let image = buffer_to_image(&acc, count as u32, width, height); + image + .save(format!("{}_passes.png", count)) + .expect("writing image failed!"); + } + for (i, pixel) in buf.into_iter().enumerate() { acc[i] += pixel; } - acc + (acc, count + 1) }, ); - buffer_to_image(img_buf, self.scene.shot_rays, width, height) + buffer_to_image(&img_buf, self.scene.shot_rays, width, height) } fn pixel_ray(&self, x: f32, y: f32) -> LinearColor { diff --git a/pathtracer/src/render/utils.rs b/pathtracer/src/render/utils.rs index 086a31e..18c5755 100644 --- a/pathtracer/src/render/utils.rs +++ b/pathtracer/src/render/utils.rs @@ -104,7 +104,7 @@ pub fn sample_hemisphere(normal: Unit) -> (Unit, f32) { (scattered, 1. / scattered.dot(&normal)) } -pub fn buffer_to_image(buffer: Vec, passes: u32, width: u32, height: u32) -> RgbImage { +pub fn buffer_to_image(buffer: &[LinearColor], passes: u32, width: u32, height: u32) -> RgbImage { let mut image = RgbImage::new(width, height); for (x, y, pixel) in image.enumerate_pixels_mut() {