pathtracer/src/light/ambient_light.rs
2020-03-18 14:35:19 +01:00

50 lines
1.2 KiB
Rust

use super::Light;
use crate::core::LinearColor;
use crate::Point;
use serde::Deserialize;
/// Represent an ambient lighting which is equal in all points of the scene.
#[derive(Debug, PartialEq, Deserialize)]
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.))
}
#[test]
fn deserialization_works() {
let yaml = "color: {r: 1.0, g: 0.5, b: 0.2}";
let light: AmbientLight = serde_yaml::from_str(yaml).unwrap();
assert_eq!(light, AmbientLight::new(LinearColor::new(1., 0.5, 0.2)))
}
}