aboutsummaryrefslogtreecommitdiff
path: root/src/trace/mod.rs
blob: 7f02f6fac15b522632737c92ec43d76e755c91eb (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
use crate::scene::Scene;
use crate::core::{Spectrum, Ray, Vector3f};
use crate::sample::Sampler;

mod pathtrace;
pub use pathtrace::PathTracer;

/// Simple surface normal tracer
///
/// This ray tracer bases color values on the hit surface normals
pub struct NormTracer<'a> {
    scn: &'a Scene,
}

/// Alias for chosen trace implementation.
///
/// This is swiched at compile time to save alot of time.
pub type DefaultTracer<'a> = PathTracer<'a>;

pub trait Tracer {
    fn trace(&self, sampler: &mut dyn Sampler, ray: &Ray) -> Spectrum;
}

impl NormTracer<'_> {
    pub fn new(scn: &Scene) -> NormTracer {
        NormTracer {scn}
    }
}

impl Tracer for NormTracer<'_> {
    fn trace(&self, sampler: &mut dyn Sampler, ray: &Ray) -> Spectrum {
        // Trace ray
        if let Some(i) = self.scn.intersect(ray) {
            let norm = i.n * 0.5 + Vector3f::new(0.5);

            return Spectrum::new_rgb(norm.x, norm.y, norm.z);
        }

        Spectrum::new_rgb(0.0, 0.0, 0.0)
    }
}