aboutsummaryrefslogtreecommitdiff
path: root/src/render.rs
blob: 23b80ae98f3dfa4a7a7c94d155d4d1c7554112ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! Implements the main render loop
//!
//! This is not a final design
use crate::camera::film::FilmTile;
use crate::camera::Camera;
use crate::trace::{DefaultTracer, Tracer};
use crate::sample::Sampler;

use crate::core::{Vector2f};
use crate::Float;

pub struct RenderTask {
    pub tile: Box<FilmTile>,
    samples: u32,
}

pub struct RenderContext<'a> {
    pub cam: &'a Camera,
    pub trc: &'a DefaultTracer<'a>,
}

impl RenderTask {
    pub fn new(tile: Box<FilmTile>, samples: u32) -> Self {
        Self { tile, samples }
    }

    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);

        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(sampler, &r));
        }
    }

    pub fn render(&mut self, ctx: &RenderContext, sampler: &mut dyn Sampler) {
        let b = self.tile.bounds.clone();
        for y in b.min.y .. b.max.y {
            for x in b.min.x .. b.max.x {
                self.render_at(ctx, x, y, sampler);
            }
        }
    }
}