library: render: scene: deserialize w/ indirection

This commit is contained in:
Bruno BELANYI 2020-03-18 18:20:20 +01:00
parent be4da14313
commit eca5ec5c26

View file

@ -10,10 +10,10 @@ use bvh::ray::Ray;
use image::RgbImage; use image::RgbImage;
use rand::prelude::thread_rng; use rand::prelude::thread_rng;
use rand::Rng; use rand::Rng;
use serde::Deserialize; use serde::{Deserialize, Deserializer};
/// Represent the scene being rendered. /// Represent the scene being rendered.
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq)]
pub struct Scene { pub struct Scene {
camera: Camera, camera: Camera,
lights: LightAggregate, lights: LightAggregate,
@ -182,6 +182,37 @@ fn reflected(incident: Vector, normal: Vector) -> Vector {
incident - delt incident - delt
} }
#[derive(Debug, PartialEq, Deserialize)]
struct SerializedScene {
camera: Camera,
lights: LightAggregate,
objects: Vec<Object>,
aliasing_limit: u32,
reflection_limit: u32,
}
impl From<SerializedScene> for Scene {
fn from(scene: SerializedScene) -> Self {
Scene::new(
scene.camera,
scene.lights,
scene.objects,
scene.aliasing_limit,
scene.reflection_limit,
)
}
}
impl<'de> Deserialize<'de> for Scene {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let cam: SerializedScene = Deserialize::deserialize(deserializer)?;
Ok(cam.into())
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;