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
use std::f32;
use enum_set::EnumSet;
use linalg::{self, Vector};
use film::Colorf;
use bxdf::{self, BxDF, BxDFType};
#[derive(Clone, Copy, Debug)]
pub struct OrenNayar {
albedo: Colorf,
a: f32,
b: f32,
}
impl OrenNayar {
pub fn new(c: &Colorf, roughness: f32) -> OrenNayar {
let mut sigma = linalg::to_radians(roughness);
sigma *= sigma;
OrenNayar { albedo: *c,
a: 1.0 - 0.5 * sigma / (sigma + 0.33),
b: 0.45 * sigma / (sigma + 0.09),
}
}
}
impl BxDF for OrenNayar {
fn bxdf_type(&self) -> EnumSet<BxDFType> {
let mut e = EnumSet::new();
e.insert(BxDFType::Diffuse);
e.insert(BxDFType::Reflection);
e
}
fn eval(&self, w_o: &Vector, w_i: &Vector) -> Colorf {
let sin_theta_o = bxdf::sin_theta(w_o);
let sin_theta_i = bxdf::sin_theta(w_i);
let max_cos =
if sin_theta_i > 1e-4 && sin_theta_o > 1e-4 {
f32::max(0.0, bxdf::cos_phi(w_i) * bxdf::cos_phi(w_o)
+ bxdf::sin_phi(w_i) * bxdf::sin_phi(w_o))
} else {
0.0
};
let (sin_alpha, tan_beta) =
if f32::abs(bxdf::cos_theta(w_i)) > f32::abs(bxdf::cos_theta(w_o)) {
(sin_theta_o, sin_theta_i / f32::abs(bxdf::cos_theta(w_i)))
} else {
(sin_theta_i, sin_theta_o / f32::abs(bxdf::cos_theta(w_o)))
};
self.albedo * f32::consts::FRAC_1_PI * (self.a + self.b * max_cos * sin_alpha * tan_beta)
}
}