-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_tracer_mats.rs
60 lines (51 loc) · 1.81 KB
/
path_tracer_mats.rs
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
49
50
51
52
53
54
55
56
57
58
59
60
use nalgebra_glm::Vec3;
use rand::Rng;
use crate::core::ray::Ray;
use crate::core::scene::Scene;
use crate::integrators::Integrator;
use crate::materials::Material;
use crate::samplers::{Sampler, SamplerType};
#[derive(Debug, Clone)]
pub struct PathTracerMatsIntegrator {
max_bounces: i32,
}
impl PathTracerMatsIntegrator {
pub fn new(max_bounces: i32) -> PathTracerMatsIntegrator {
PathTracerMatsIntegrator { max_bounces }
}
}
// iterative version
impl Integrator for PathTracerMatsIntegrator {
fn li(&self, scene: &Scene, sampler: &mut SamplerType, rng: &mut impl Rng, ray: &Ray) -> Vec3 {
let mut radiance = Vec3::zeros();
let mut attenuation = Vec3::new(1.0, 1.0, 1.0);
let mut ray = ray.clone();
for _ in 0..=self.max_bounces {
// find next intersection
let Some(hit) = scene.intersect(&ray) else {
return radiance + scene.background.component_mul(&attenuation);
};
// sample next direction
let rv = sampler.next2f(rng);
let Some(srec) = hit.mat.sample(&ray.direction, &hit, rv) else {
break;
};
// add emitted light contribution
if let Some(emitted) = hit.mat.emmitted(&ray, &hit) {
radiance += emitted.component_mul(&attenuation);
}
// update attenuation
let a = if srec.is_specular {
srec.attenuation
} else {
hit.mat.eval(&ray.direction, &srec.wo, &hit)
/ hit.mat.pdf(&ray.direction, &srec.wo, &hit)
};
attenuation = attenuation.component_mul(&a);
// update the ray for the next bounce
ray.origin = hit.p;
ray.direction = srec.wo;
}
radiance
}
}