From d97b4ced834bc54548307733d0ea3b2161ed4971 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Fri, 23 Jun 2023 19:48:22 -0500 Subject: [PATCH] Feat: Single-line rendering function Laying the groundwork for setting up threading. Gotta be able to run parts of the render to get multiple things working on the whole. --- src/main.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index fbba0e6..d8eed8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,8 +33,8 @@ fn main() { // random generator let mut small_rng = SmallRng::seed_from_u64(0); - let distrib_zero_one = Uniform::new(0.0, 1.0); - let distrib_plusminus_one = Uniform::new(-1.0, 1.0); + + // world let world = random_scene(&mut small_rng); @@ -60,20 +60,31 @@ fn main() { println!("P3\n{} {}\n255", image.0, image.1); for y in (0..image.1).rev() { eprintln!("Scanlines remaining: {}", y); - for x in 0..image.0 { - let mut color = Vec3::zero(); - for _ in 0..samples_per_pixel { - let u = ((x as f32) + small_rng.sample(distrib_zero_one)) / ((image.0 - 1) as f32); - let v = ((y as f32) + small_rng.sample(distrib_zero_one)) / ((image.1 - 1) as f32); - let ray = cam.get_ray(u, v, &mut small_rng); - color+= ray_color(ray, &world, max_depth, &mut small_rng, distrib_plusminus_one); - } + let line = render_line(y, image, samples_per_pixel, &world, max_depth, &mut small_rng, &cam); + for color in line { println!("{}", color.print_ppm(samples_per_pixel)); } } eprintln!("Done!"); } +fn render_line(y: i32, image: (i32, i32), samples_per_pixel: u32, world: &dyn Hittable, max_depth: u32, small_rng: &mut SmallRng, cam: &camera::Camera ) -> Vec { + 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..image.0 { + let mut color = Vec3::zero(); + for _ in 0..samples_per_pixel { + let u = ((x as f32) + small_rng.sample(distrib_zero_one)) / ((image.0 - 1) as f32); + let v = ((y as f32) + small_rng.sample(distrib_zero_one)) / ((image.1 - 1) as f32); + let ray = cam.get_ray(u, v, small_rng); + color+= ray_color(ray, world, max_depth, small_rng, distrib_plusminus_one); + } + line.push(color); + } + return line; +} + fn ray_color(r: Ray, world: &dyn Hittable, depth: u32, srng: &mut SmallRng, distrib: Uniform ) -> Vec3 { // recursion depth guard if depth == 0 {