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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::f32;
use enum_set::EnumSet;
use linalg::{self, Vector};
use film::Colorf;
use bxdf::{self, BxDF, BxDFType};
use bxdf::fresnel::{Dielectric, Fresnel};
use bxdf::microfacet::{MicrofacetDistribution};
#[derive(Copy, Clone)]
pub struct MicrofacetTransmission<'a> {
reflectance: Colorf,
fresnel: &'a Dielectric,
microfacet: &'a MicrofacetDistribution,
}
impl<'a> MicrofacetTransmission<'a> {
pub fn new(c: &Colorf, fresnel: &'a Dielectric, microfacet: &'a MicrofacetDistribution)
-> MicrofacetTransmission<'a> {
MicrofacetTransmission { reflectance: *c, fresnel: fresnel, microfacet: microfacet }
}
fn eta_for_interaction(&self, w_o: &Vector) -> (f32, f32) {
if bxdf::cos_theta(w_o) > 0.0 {
(self.fresnel.eta_i, self.fresnel.eta_t)
} else {
(self.fresnel.eta_t, self.fresnel.eta_i)
}
}
fn jacobian(w_o: &Vector, w_i: &Vector, w_h: &Vector, eta: (f32, f32)) -> f32 {
let wi_dot_h = linalg::dot(w_i, w_h);
let wo_dot_h = linalg::dot(w_o, w_h);
let denom = f32::powf(eta.1 * wi_dot_h + eta.0 * wo_dot_h, 2.0);
if denom != 0.0 {
f32::abs(f32::powf(eta.0, 2.0) * f32::abs(wo_dot_h) / denom)
} else {
0.0
}
}
fn half_vector(w_o: &Vector, w_i: &Vector, eta: (f32, f32)) -> Vector {
(-eta.1 * *w_i - eta.0 * *w_o).normalized()
}
}
impl<'a> BxDF for MicrofacetTransmission<'a> {
fn bxdf_type(&self) -> EnumSet<BxDFType> {
let mut e = EnumSet::new();
e.insert(BxDFType::Glossy);
e.insert(BxDFType::Transmission);
e
}
fn eval(&self, w_o: &Vector, w_i: &Vector) -> Colorf {
if bxdf::same_hemisphere(w_o, w_i) {
return Colorf::black();
}
let cos_to = bxdf::cos_theta(w_o);
let cos_ti = bxdf::cos_theta(w_i);
if cos_to == 0.0 || cos_ti == 0.0 {
return Colorf::black();
}
let eta = self.eta_for_interaction(w_o);
let w_h = MicrofacetTransmission::half_vector(w_o, w_i, eta);
let d = self.microfacet.normal_distribution(&w_h);
let f = Colorf::broadcast(1.0) - self.fresnel.fresnel(linalg::dot(w_i, &w_h));
let g = self.microfacet.shadowing_masking(w_i, w_o, &w_h);
let wi_dot_h = linalg::dot(w_i, &w_h);
let jacobian = MicrofacetTransmission::jacobian(w_o, w_i, &w_h, eta);
self.reflectance * (f32::abs(wi_dot_h) / (f32::abs(w_i.z) * f32::abs(w_o.z)))
* (f * g * d) * jacobian
}
fn sample(&self, w_o: &Vector, samples: &(f32, f32)) -> (Colorf, Vector, f32) {
let mut w_h = self.microfacet.sample(w_o, samples);
if !bxdf::same_hemisphere(w_o, &w_h) {
w_h = -w_h;
}
let eta = self.eta_for_interaction(w_o);
if let Some(w_i) = linalg::refract(w_o, &w_h, eta.0 / eta.1) {
if bxdf::same_hemisphere(w_o, &w_i) {
(Colorf::black(), Vector::broadcast(0.0), 0.0)
} else {
(self.eval(w_o, &w_i), w_i, self.pdf(w_o, &w_i))
}
} else {
(Colorf::black(), Vector::broadcast(0.0), 0.0)
}
}
fn pdf(&self, w_o: &Vector, w_i: &Vector) -> f32 {
if bxdf::same_hemisphere(w_o, w_i) {
0.0
} else {
let eta = self.eta_for_interaction(w_o);
let w_h = MicrofacetTransmission::half_vector(w_o, w_i, eta);
self.microfacet.pdf(&w_h) * MicrofacetTransmission::jacobian(w_o, w_i, &w_h, eta)
}
}
}