2020-03-17 19:16:25 +01:00
|
|
|
use super::Material;
|
2020-03-17 20:30:50 +01:00
|
|
|
use crate::core::color::LinearColor;
|
|
|
|
use crate::Point2D;
|
2020-03-18 14:38:17 +01:00
|
|
|
use serde::Deserialize;
|
2020-03-17 19:16:25 +01:00
|
|
|
|
|
|
|
/// A material with the same characteristics on all points.
|
2020-03-18 15:24:09 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize)]
|
2020-03-17 19:16:25 +01:00
|
|
|
pub struct UniformMaterial {
|
|
|
|
diffuse: LinearColor,
|
|
|
|
specular: LinearColor,
|
2020-03-18 00:06:08 +01:00
|
|
|
reflectivity: f32,
|
2020-03-17 19:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UniformMaterial {
|
2020-03-18 00:06:08 +01:00
|
|
|
pub fn new(diffuse: LinearColor, specular: LinearColor, reflectivity: f32) -> Self {
|
|
|
|
UniformMaterial {
|
|
|
|
diffuse,
|
|
|
|
specular,
|
|
|
|
reflectivity,
|
|
|
|
}
|
2020-03-17 19:16:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Material for UniformMaterial {
|
|
|
|
fn diffuse(&self, _: Point2D) -> LinearColor {
|
|
|
|
self.diffuse.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn specular(&self, _: Point2D) -> LinearColor {
|
|
|
|
self.specular.clone()
|
|
|
|
}
|
2020-03-18 00:06:08 +01:00
|
|
|
|
|
|
|
fn reflectivity(&self, _: Point2D) -> f32 {
|
|
|
|
self.reflectivity
|
|
|
|
}
|
2020-03-17 19:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_works() {
|
|
|
|
let diffuse = LinearColor::new(0., 0.5, 0.);
|
|
|
|
let specular = LinearColor::new(1., 1., 1.);
|
2020-03-18 00:06:08 +01:00
|
|
|
let reflectivity = 0.5;
|
|
|
|
let mat = UniformMaterial::new(diffuse.clone(), specular.clone(), reflectivity);
|
|
|
|
assert_eq!(
|
|
|
|
mat,
|
|
|
|
UniformMaterial {
|
|
|
|
diffuse,
|
|
|
|
specular,
|
|
|
|
reflectivity
|
|
|
|
}
|
|
|
|
)
|
2020-03-17 19:16:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn simple_material() -> impl Material {
|
|
|
|
UniformMaterial::new(
|
|
|
|
LinearColor::new(0.5, 0.5, 0.5),
|
|
|
|
LinearColor::new(1., 1., 1.),
|
2020-03-18 00:06:08 +01:00
|
|
|
0.5,
|
2020-03-17 19:16:25 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn diffuse_works() {
|
|
|
|
let mat = simple_material();
|
|
|
|
assert_eq!(
|
|
|
|
mat.diffuse(Point2D::origin()),
|
|
|
|
LinearColor::new(0.5, 0.5, 0.5)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn specular_works() {
|
|
|
|
let mat = simple_material();
|
|
|
|
assert_eq!(
|
|
|
|
mat.specular(Point2D::origin()),
|
|
|
|
LinearColor::new(1., 1., 1.)
|
|
|
|
)
|
|
|
|
}
|
2020-03-18 00:06:08 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reflectivity_works() {
|
|
|
|
let mat = simple_material();
|
2020-03-18 14:34:37 +01:00
|
|
|
assert!(mat.reflectivity(Point2D::origin()) - 0.5 < std::f32::EPSILON)
|
2020-03-18 00:06:08 +01:00
|
|
|
}
|
2020-03-18 14:38:17 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialization_works() {
|
|
|
|
let yaml = r#"
|
|
|
|
diffuse: {r: 1.0, g: 0.5, b: 0.25}
|
|
|
|
specular: {r: 0.25, g: 0.125, b: 0.75}
|
|
|
|
reflectivity: 0.25
|
|
|
|
"#;
|
|
|
|
let material: UniformMaterial = serde_yaml::from_str(yaml).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
material,
|
|
|
|
UniformMaterial::new(
|
|
|
|
LinearColor::new(1., 0.5, 0.25),
|
|
|
|
LinearColor::new(0.25, 0.125, 0.75),
|
|
|
|
0.25
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2020-03-17 19:16:25 +01:00
|
|
|
}
|