From 1e83ea211055eb234b89c69b5d03602e3fcb98fb Mon Sep 17 00:00:00 2001 From: Julian T Date: Fri, 5 Feb 2021 20:17:20 +0100 Subject: Achieve anti aliasing with multi pixel sampling --- src/render.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/render.rs') diff --git a/src/render.rs b/src/render.rs index 7d05aba..c8a75da 100644 --- a/src/render.rs +++ b/src/render.rs @@ -5,8 +5,9 @@ use crate::camera::film::FilmTile; use crate::camera::Camera; use crate::scene::Scene; use crate::trace::Tracer; +use crate::sample::Sampler; -use crate::core::{Vector2f, Vector3f, Spectrum}; +use crate::core::{Vector2f, Spectrum}; use crate::Float; pub struct RenderTask { @@ -25,20 +26,24 @@ impl RenderTask { Self { tile, samples } } - fn render_at(&self, ctx: &RenderContext, x: i32, y: i32) -> Spectrum { - // Create a ray - let (r, _) = ctx.cam.generate_ray(&Vector2f::new_xy(x as Float, y as Float)); + fn render_at(&mut self, ctx: &RenderContext, x: i32, y: i32, sampler: &mut dyn Sampler) { + let corner = Vector2f::new_xy(x as Float, y as Float); - ctx.trc.trace(ctx.scn, &r) + for _ in 0..self.samples { + let p = corner + sampler.get_sample_2d(); + + // Create a ray + let (r, _) = ctx.cam.generate_ray(&p); + + self.tile.add_sample(&p, ctx.trc.trace(ctx.scn, &r)); + } } - pub fn render(&mut self, ctx: &RenderContext) { + pub fn render(&mut self, ctx: &RenderContext, sampler: &mut dyn Sampler) { let b = self.tile.bounds.clone(); for x in b.min.x .. b.max.x { for y in b.min.y .. b.max.y { - let p = Vector2f::new_xy(x as Float, y as Float); - - self.tile.add_sample(&p, self.render_at(ctx, x, y)) + self.render_at(ctx, x, y, sampler); } } } -- cgit v1.2.3