library: light: add AmbientLight implementation

This commit is contained in:
Bruno BELANYI 2020-03-16 17:54:44 +01:00
parent d759c16f5d
commit 3a487d956e
2 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,41 @@
use super::super::core::LinearColor;
use super::super::Point;
use super::Light;
/// Represent an ambient lighting which is equal in all points of the scene.
#[derive(Debug, PartialEq)]
pub struct AmbientLight {
color: LinearColor,
}
impl AmbientLight {
pub fn new(color: LinearColor) -> Self {
AmbientLight { color }
}
}
impl Light for AmbientLight {
fn illumination(&self, _: &Point) -> LinearColor {
self.color.clone()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn new_works() {
let color = LinearColor::new(1., 1., 1.);
let light = AmbientLight::new(color.clone());
let res = AmbientLight { color };
assert_eq!(light, res)
}
#[test]
fn illumination_is_correct() {
let light = AmbientLight::new(LinearColor::new(1., 1., 1.));
let lum = light.illumination(&Point::new(1., 1., 1.));
assert_eq!(lum, LinearColor::new(1., 1., 1.))
}
}

View file

@ -13,6 +13,9 @@ pub trait SpatialLight: Light {
fn to_source(&self, origin: &Point) -> (Vector, f32); fn to_source(&self, origin: &Point) -> (Vector, f32);
} }
pub mod ambient_light;
pub use ambient_light::*;
pub mod directional_light; pub mod directional_light;
pub use directional_light::*; pub use directional_light::*;