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
//! Defines a BRDF that describes specular reflection

use std::f32;
use enum_set::EnumSet;

use linalg::Vector;
use film::Colorf;
use bxdf::{self, BxDF, BxDFType};
use bxdf::fresnel::Fresnel;

/// Specular reflection BRDF that implements a specularly reflective material model
#[derive(Copy, Clone)]
pub struct SpecularReflection<'a> {
    /// Color of the reflective material
    reflectance: Colorf,
    /// Fresnel term for the reflection model
    fresnel: &'a Fresnel,
}

impl<'a> SpecularReflection<'a> {
    /// Create a specularly reflective BRDF with the reflective color and Fresnel term
    pub fn new(c: &Colorf, fresnel: &'a Fresnel) -> SpecularReflection<'a> {
        SpecularReflection { reflectance: *c, fresnel: fresnel }
    }
}

impl<'a> BxDF for SpecularReflection<'a> {
    fn bxdf_type(&self) -> EnumSet<BxDFType> {
        let mut e = EnumSet::new();
        e.insert(BxDFType::Specular);
        e.insert(BxDFType::Reflection);
        e
    }
    /// We'll never exactly hit the specular reflection direction with some pair
    /// so this just returns black. Use `sample` instead
    fn eval(&self, _: &Vector, _: &Vector) -> Colorf { Colorf::broadcast(0.0) }
    /// Sampling the specular BRDF just returns the specular reflection direction
    /// for the light leaving along `w_o`
    fn sample(&self, w_o: &Vector, _: &(f32, f32)) -> (Colorf, Vector, f32) {
        let w_i = Vector::new(-w_o.x, -w_o.y, w_o.z);
        // TODO: is this an expected but super rare case? or does it imply some error
        // in the sphere intersection? Such a glancing angle shouldn't really be counted right?
        if w_i.z != 0.0 {
            let c = self.fresnel.fresnel(bxdf::cos_theta(w_o)) * self.reflectance
                / f32::abs(bxdf::cos_theta(&w_i));
            (c, w_i, 1.0)
        } else {
            (Colorf::black(), w_i, 0.0)
        }
    }
}