library: render: light_aggregate: add deserialization

This commit is contained in:
Bruno BELANYI 2020-03-18 14:52:50 +01:00
parent ccc9eaf423
commit 4112a933ed

View file

@ -1,7 +1,8 @@
use crate::light::*;
use serde::Deserialize;
use std::iter::Iterator;
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Deserialize)]
pub struct LightAggregate {
ambients: Vec<AmbientLight>,
directionals: Vec<DirectionalLight>,
@ -70,4 +71,44 @@ mod test {
let lights = <LightAggregate as Default>::default();
assert_eq!(lights, LightAggregate::empty())
}
#[test]
fn deserialization_works() {
use crate::{core::LinearColor, Point, Vector};
let yaml = r#"
ambients:
- color: {r: 1.0, g: 0.5, b: 0.2}
directionals:
- direction: [1.0, 0.0, 0.0]
color: {r: 1.0, g: 0.5, b: 0.2}
points:
- position: [1.0, 1.0, 1.0]
color: {r: 1.0, g: 0.5, b: 0.2}
spots:
- position: [0.0, 0.0, 0.0]
direction: [1.0, 0.0, 0.0]
fov: 90.0
color: {r: 1.0, g: 0.5, b: 0.2}
"#;
let expected = LightAggregate::new(
vec![AmbientLight::new(LinearColor::new(1., 0.5, 0.2))],
vec![DirectionalLight::new(
Vector::new(1., 0., 0.),
LinearColor::new(1., 0.5, 0.2),
)],
vec![PointLight::new(
Point::new(1., 1., 1.),
LinearColor::new(1., 0.5, 0.2),
)],
vec![SpotLight::degrees_new(
Point::origin(),
Vector::new(1., 0., 0.),
90.,
LinearColor::new(1., 0.5, 0.2),
)],
);
let lights: LightAggregate = serde_yaml::from_str(yaml).unwrap();
assert_eq!(lights, expected)
}
}