library: render: write passes in steps

This commit is contained in:
Antoine Martin 2020-04-08 18:05:11 +02:00
parent be1f400b34
commit 00dae425d9
2 changed files with 15 additions and 6 deletions

View file

@ -33,6 +33,7 @@ impl Pathtracer {
/// ///
/// [`Scene`]: ../scene/scene/struct.Scene.html /// [`Scene`]: ../scene/scene/struct.Scene.html
pub fn render(&self) -> RgbImage { pub fn render(&self) -> RgbImage {
let steps = vec![1, 5, 50];
let (width, height) = ( let (width, height) = (
self.scene.camera.film().width(), self.scene.camera.film().width(),
self.scene.camera.film().height(), self.scene.camera.film().height(),
@ -42,7 +43,7 @@ impl Pathtracer {
let p = super::super::progress::get_passes_progressbar(self.scene.shot_rays); let p = super::super::progress::get_passes_progressbar(self.scene.shot_rays);
// Ensure at least one round of shots // 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) .progress_with(p)
.map(|_| { .map(|_| {
let mut buffer: Vec<LinearColor> = Vec::new(); let mut buffer: Vec<LinearColor> = Vec::new();
@ -72,18 +73,26 @@ impl Pathtracer {
{ {
let mut vec = Vec::new(); let mut vec = Vec::new();
vec.resize_with(total as usize, LinearColor::black); 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() { for (i, pixel) in buf.into_iter().enumerate() {
acc[i] += pixel; 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 { fn pixel_ray(&self, x: f32, y: f32) -> LinearColor {

View file

@ -104,7 +104,7 @@ pub fn sample_hemisphere(normal: Unit<Vector>) -> (Unit<Vector>, f32) {
(scattered, 1. / scattered.dot(&normal)) (scattered, 1. / scattered.dot(&normal))
} }
pub fn buffer_to_image(buffer: Vec<LinearColor>, 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); let mut image = RgbImage::new(width, height);
for (x, y, pixel) in image.enumerate_pixels_mut() { for (x, y, pixel) in image.enumerate_pixels_mut() {