diff --git a/src/main.rs b/src/main.rs index e465ed1..436ce82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,21 +147,22 @@ pub struct RenderContext{ camera: Camera, } -fn render_line(y: i32, small_rng: &mut SmallRng, context: RenderContext ) -> Vec { +fn render_line(y: i32, small_rng: &mut SmallRng, context: RenderContext) -> Vec { + //TODO: Ensure that the compiler hoists the distribution's out as constants + // else, do so manually let distrib_zero_one = Uniform::new(0.0, 1.0); let distrib_plusminus_one = Uniform::new(-1.0, 1.0); - let mut line = Vec::::new(); - for x in 0..context.image.0 { - let mut color = Vec3::zero(); - for _ in 0..context.samples_per_pixel { - let u = ((x as f32) + small_rng.sample(distrib_zero_one)) / ((context.image.0 - 1) as f32); - let v = ((y as f32) + small_rng.sample(distrib_zero_one)) / ((context.image.1 - 1) as f32); - let ray = context.camera.get_ray(u, v, small_rng); - color+= ray_color(ray, &context.world, context.max_depth, small_rng, distrib_plusminus_one); - } - line.push(color); - } - return line; + (0..context.image.0).map(|x| { + (0..context.samples_per_pixel).into_iter().fold( + Vec3::zero(), + |color, _sample| { + let u = ((x as f32) + small_rng.sample(distrib_zero_one)) / ((context.image.0 - 1) as f32); + let v = ((y as f32) + small_rng.sample(distrib_zero_one)) / ((context.image.1 - 1) as f32); + let ray = context.camera.get_ray(u, v, small_rng); + color + ray_color(ray, &context.world, context.max_depth, small_rng, distrib_plusminus_one) + } + ) + }).collect() } fn ray_color(r: Ray, world: &Hittable, depth: u32, srng: &mut SmallRng, distrib: Uniform ) -> Vec3 {