Render frame function does the iterator thing!

The many, many nested for loops don't feel right in a language that lets
you `let x = for...` to assign the results of the loop directly to a
variable. The logic hasn't changed (and I'm pretty sure the compiler
emits the same code), but it feels better now.

I'm now equipped to go over the rest of the project and rewrite the
loops. Hopefully a more ergonomic way to dispatch to the threads arises
as a result. I shall see.
This commit is contained in:
2023-08-18 18:08:37 -05:00
parent adaf277cba
commit 4430b7c0bf

View File

@@ -148,20 +148,21 @@ pub struct RenderContext{
}
fn render_line(y: i32, small_rng: &mut SmallRng, context: RenderContext) -> Vec<Vec3> {
//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::<Vec3>::new();
for x in 0..context.image.0 {
let mut color = Vec3::zero();
for _ in 0..context.samples_per_pixel {
(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);
color + ray_color(ray, &context.world, context.max_depth, small_rng, distrib_plusminus_one)
}
line.push(color);
}
return line;
)
}).collect()
}
fn ray_color(r: Ray, world: &Hittable, depth: u32, srng: &mut SmallRng, distrib: Uniform<f32> ) -> Vec3 {