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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::f32;
use enum_set::EnumSet;
use linalg::{self, Vector};
use film::Colorf;
use bxdf::{self, BxDF, BxDFType};
#[derive(Copy, Clone)]
pub struct Merl<'a> {
brdf: &'a [f32],
n_theta_h: usize,
n_theta_d: usize,
n_phi_d: usize,
}
impl<'a> Merl<'a> {
pub fn new(brdf: &'a [f32], n_theta_h: usize, n_theta_d: usize, n_phi_d: usize) -> Merl<'a> {
Merl { brdf: brdf, n_theta_h: n_theta_h, n_theta_d: n_theta_d, n_phi_d: n_phi_d }
}
fn map_index(val: f32, max: f32, n_vals: usize) -> usize {
linalg::clamp((val / max * n_vals as f32) as usize, 0, n_vals - 1)
}
}
impl<'a> BxDF for Merl<'a> {
fn bxdf_type(&self) -> EnumSet<BxDFType> {
let mut e = EnumSet::new();
e.insert(BxDFType::Glossy);
e.insert(BxDFType::Reflection);
e
}
fn eval(&self, w_oi: &Vector, w_ii: &Vector) -> Colorf {
let mut w_i = *w_ii;
let mut w_h = *w_oi + w_i;
if w_h.z < 0.0 {
w_i = -w_i;
w_h = -w_h;
}
if w_h.length_sqr() == 0.0 {
return Colorf::black();
}
let w_h = w_h.normalized();
let theta_h = linalg::spherical_theta(&w_h);
let cos_phi_h = bxdf::cos_phi(&w_h);
let sin_phi_h = bxdf::sin_phi(&w_h);
let cos_theta_h = bxdf::cos_theta(&w_h);
let sin_theta_h = bxdf::sin_theta(&w_h);
let w_hx = Vector::new(cos_phi_h * cos_theta_h, sin_phi_h * cos_theta_h, -sin_theta_h);
let w_hy = Vector::new(-sin_phi_h, cos_phi_h, 0.0);
let w_d = Vector::new(linalg::dot(&w_i, &w_hx), linalg::dot(&w_i, &w_hy), linalg::dot(&w_i, &w_h));
let theta_d = linalg::spherical_theta(&w_d);
let phi_d = match linalg::spherical_phi(&w_d) {
d if d > f32::consts::PI => d - f32::consts::PI,
d => d,
};
let theta_h_idx = Merl::map_index(f32::sqrt(f32::max(0.0, 2.0 * theta_h / f32::consts::PI)), 1.0, self.n_theta_h);
let theta_d_idx = Merl::map_index(theta_d, f32::consts::PI / 2.0, self.n_theta_d);
let phi_d_idx = Merl::map_index(phi_d, f32::consts::PI, self.n_phi_d);
let i = phi_d_idx + self.n_phi_d * (theta_d_idx + theta_h_idx * self.n_theta_d);
assert!(i < self.brdf.len());
Colorf::new(self.brdf[3 * i], self.brdf[3 * i + 1], self.brdf[3 * i + 2])
}
}