aboutsummaryrefslogtreecommitdiff
path: root/src/trace/pathtrace.rs
blob: 47c68352449f7db2370639327d0c413aa10d4906 (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
use crate::world::Scene;
use crate::core::{Ray, Spectrum};
use crate::sample::Sampler;
use super::Tracer;

pub struct PathTracer<'a> {
    depth: i32,
    scn: &'a Scene,
}

impl PathTracer<'_> {
    pub fn new(scn: &Scene, depth: Option<i32>) -> PathTracer {
        let depth = depth.unwrap_or(-1);

        PathTracer {
            depth,
            scn,
        }
    }

    pub fn trace_recur(&self, sampler: &mut dyn Sampler, ray: &Ray, depth: i32) -> Spectrum {

        if depth == 0 {
            return Spectrum::ZERO;
        }

        if let Some(si) = self.scn.intersect(ray) {
            if let Some((scalar, nray)) = si.mat.scatter(ray, &si.i, sampler) {
                return self.trace_recur(sampler, &nray, depth-1) * scalar;
            } else {
                return Spectrum::ZERO;
            }
        }

        // Simulates a sky
        let t = (ray.direction.norm().y + 1.0) * 0.5;
        Spectrum::new_rgb(1.0, 1.0, 1.0) * (1.0-t) + Spectrum::new_rgb(0.5, 0.7, 1.0) * t

    }
}

impl Tracer for PathTracer<'_> {
    fn trace(&self, sampler: &mut dyn Sampler, ray: &Ray) -> Spectrum {
        self.trace_recur(sampler, ray, self.depth)
    }
}