library: material: add reflectivity

This necessitates adding it to the trait, and its implementations.
This commit is contained in:
Bruno BELANYI 2020-03-18 00:06:08 +01:00
parent 6a829a96fd
commit d8f5f880ec
3 changed files with 33 additions and 4 deletions

View file

@ -7,6 +7,8 @@ pub trait Material: std::fmt::Debug {
fn diffuse(&self, point: Point2D) -> LinearColor;
/// The specular component on a texel point.
fn specular(&self, point: Point2D) -> LinearColor;
/// The reflectivity coefficient on a texel point.
fn reflectivity(&self, point: Point2D) -> f32;
}
pub mod uniform;

View file

@ -7,11 +7,16 @@ use crate::Point2D;
pub struct UniformMaterial {
diffuse: LinearColor,
specular: LinearColor,
reflectivity: f32,
}
impl UniformMaterial {
pub fn new(diffuse: LinearColor, specular: LinearColor) -> Self {
UniformMaterial { diffuse, specular }
pub fn new(diffuse: LinearColor, specular: LinearColor, reflectivity: f32) -> Self {
UniformMaterial {
diffuse,
specular,
reflectivity,
}
}
}
@ -23,6 +28,10 @@ impl Material for UniformMaterial {
fn specular(&self, _: Point2D) -> LinearColor {
self.specular.clone()
}
fn reflectivity(&self, _: Point2D) -> f32 {
self.reflectivity
}
}
#[cfg(test)]
@ -33,14 +42,23 @@ mod test {
fn new_works() {
let diffuse = LinearColor::new(0., 0.5, 0.);
let specular = LinearColor::new(1., 1., 1.);
let mat = UniformMaterial::new(diffuse.clone(), specular.clone());
assert_eq!(mat, UniformMaterial { diffuse, specular })
let reflectivity = 0.5;
let mat = UniformMaterial::new(diffuse.clone(), specular.clone(), reflectivity);
assert_eq!(
mat,
UniformMaterial {
diffuse,
specular,
reflectivity
}
)
}
fn simple_material() -> impl Material {
UniformMaterial::new(
LinearColor::new(0.5, 0.5, 0.5),
LinearColor::new(1., 1., 1.),
0.5,
)
}
@ -61,4 +79,10 @@ mod test {
LinearColor::new(1., 1., 1.)
)
}
#[test]
fn reflectivity_works() {
let mat = simple_material();
assert_eq!(mat.reflectivity(Point2D::origin()), 0.5)
}
}

View file

@ -68,6 +68,9 @@ mod test {
fn specular(&self, _: Point2D) -> LinearColor {
todo!()
}
fn reflectivity(&self, _: Point2D) -> f32 {
todo!()
}
}
#[derive(Debug)]