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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Defines the BxDF interface implemented by BRDF/BTDFs to describe
//! material properties. Also provides the BSDF type which composes
//! various BRDF/BTDFs to describe materials

use std::mem;
use std::f32;
use enum_set::{EnumSet, CLike};

use linalg::{self, Vector};
use film::Colorf;
use mc;

pub use self::bsdf::BSDF;
pub use self::lambertian::Lambertian;
pub use self::oren_nayar::OrenNayar;
pub use self::specular_reflection::SpecularReflection;
pub use self::specular_transmission::SpecularTransmission;
pub use self::merl::Merl;
pub use self::torrance_sparrow::TorranceSparrow;
pub use self::microfacet_transmission::MicrofacetTransmission;

pub mod bsdf;
pub mod lambertian;
pub mod oren_nayar;
pub mod fresnel;
pub mod specular_reflection;
pub mod specular_transmission;
pub mod merl;
pub mod microfacet;
pub mod torrance_sparrow;
pub mod microfacet_transmission;

/// Various types of BxDFs that can be selected to specify which
/// types of surface functions should be evaluated
#[repr(u32)]
#[derive(Clone, Copy, Debug)]
pub enum BxDFType {
    Reflection, Transmission, Diffuse, Glossy, Specular,
}

impl BxDFType {
    /// Get an EnumSet containing all flags for the different types of
    /// BxDFs: Diffuse, Glossy, Specular
    pub fn all_types() -> EnumSet<BxDFType> {
        let mut e = EnumSet::new();
        e.insert(BxDFType::Diffuse);
        e.insert(BxDFType::Glossy);
        e.insert(BxDFType::Specular);
        e
    }
    /// Get an EnumSet containing all flags for reflective BxDFs (eg. BRDFs)
    pub fn all_brdf() -> EnumSet<BxDFType> {
        let mut e = BxDFType::all_types();
        e.insert(BxDFType::Reflection);
        e
    }
    /// Get an EnumSet containing all flags for transmissive BxDFs (eg. BTDFs)
    pub fn all_btdf() -> EnumSet<BxDFType> {
        let mut e = BxDFType::all_types();
        e.insert(BxDFType::Transmission);
        e
    }
    /// Get an EnumSet containing all flags for all BxDFs. This would be all
    /// types of BRDFs and BTDFs
    pub fn all() -> EnumSet<BxDFType> { BxDFType::all_brdf().union(BxDFType::all_btdf()) }
    /// Get an EnumSet containing flags for all types of specular BxDFs
    pub fn specular() -> EnumSet<BxDFType> {
        let mut e = EnumSet::new();
        e.insert(BxDFType::Specular);
        e.insert(BxDFType::Reflection);
        e.insert(BxDFType::Transmission);
        e
    }
    /// Get an EnumSet containing flags for all non-specular BxDFs
    pub fn non_specular() -> EnumSet<BxDFType> {
        let mut e = EnumSet::new();
        e.insert(BxDFType::Diffuse);
        e.insert(BxDFType::Glossy);
        e.insert(BxDFType::Reflection);
        e.insert(BxDFType::Transmission);
        e
    }
}

impl CLike for BxDFType {
    fn to_u32(&self) -> u32 { *self as u32 }
    unsafe fn from_u32(v: u32) -> BxDFType { mem::transmute(v) }
}

/// Trait implemented by BRDF/BTDFs in `tray_rust`. Provides methods for
/// evaluating and sampling the function
pub trait BxDF {
    /// Get the type of this BxDF
    fn bxdf_type(&self) -> EnumSet<BxDFType>;
    /// Evaluate the BxDF for the pair of incident and outgoing light directions,
    /// `w_i` and `w_o`.
    fn eval(&self, w_o: &Vector, w_i: &Vector) -> Colorf;
    /// Sample an incident light direction for an outgoing light direction `w_o`.
    /// `samples` will be used to randomly sample a direction for the outgoing light
    /// Returns the color of the material for the pair of directions, the incident
    /// light direction and pdf
    fn sample(&self, w_o: &Vector, samples: &(f32, f32)) -> (Colorf, Vector, f32) {
        let mut w_i = mc::cos_sample_hemisphere(samples);
        // We may need to flip the sampled direction to be on the same hemisphere as w_o
        if w_o.z < 0.0 {
            w_i.z *= -1.0;
        }
        (self.eval(w_o, &w_i), w_i, self.pdf(w_o, &w_i))
    }
    /// Check if this BxDF matches the type flags passed
    fn matches(&self, flags: EnumSet<BxDFType>) -> bool {
        self.bxdf_type().is_subset(&flags)
    }
    /// Compute the pdf of sampling the pair of directions passed for this BxDF
    fn pdf(&self, w_o: &Vector, w_i: &Vector) -> f32 {
        if same_hemisphere(w_o, w_i) {
            f32::abs(cos_theta(w_i)) * f32::consts::FRAC_1_PI
        } else {
            0.0
        }
    }
}

/// Compute the value of cosine theta for a vector in shading space
pub fn cos_theta(v: &Vector) -> f32 { v.z }
/// Compute the value of cosine^2 theta for a vector in shading space
pub fn cos_theta_sqr(v: &Vector) -> f32 { v.z * v.z }
/// Compute the value of (sine theta)^2  for a vector in shading space
pub fn sin_theta_sqr(v: &Vector) -> f32 { f32::max(0.0, 1.0 - v.z * v.z) }
/// Compute the value of sine theta for a vector in shading space
pub fn sin_theta(v: &Vector) -> f32 { f32::sqrt(sin_theta_sqr(v)) }
/// Compute the value of tan theta for a vector in shading space
pub fn tan_theta(v: &Vector) -> f32 {
    let sin_theta_2 = sin_theta_sqr(v);
    if sin_theta_2 <= 0.0 {
        0.0
    } else {
        f32::sqrt(sin_theta_2) / cos_theta(v)
    }
}
/// Compute the value of tan theta^2 for a vector in shading space
pub fn tan_theta_sqr(v: &Vector) -> f32 {
    sin_theta_sqr(v) / cos_theta_sqr(v)
}
/// Compute the value of arctan theta for a vector in shading space
pub fn arctan_theta(v: &Vector) -> f32 { cos_theta(v) / sin_theta(v) }
/// Compute the value of cosine phi for a vector in shading space
pub fn cos_phi(v: &Vector) -> f32 {
    let sin_theta = sin_theta(v);
    if sin_theta == 0.0 {
        1.0
    } else {
        linalg::clamp(v.x / sin_theta, -1.0, 1.0)
    }
}
/// Compute the value of sine phi for a vector in shading space
pub fn sin_phi(v: &Vector) -> f32 {
    let sin_theta = sin_theta(v);
    if sin_theta == 0.0 {
        0.0
    } else {
        linalg::clamp(v.y / sin_theta, -1.0, 1.0)
    }
}
/// Check if two vectors are in the same hemisphere in shading space
pub fn same_hemisphere(a: &Vector, b: &Vector) -> bool { a.z * b.z > 0.0 }