diff --git a/src/render/scene.rs b/src/render/scene.rs index 51d5d13..8514d81 100644 --- a/src/render/scene.rs +++ b/src/render/scene.rs @@ -10,10 +10,10 @@ use bvh::ray::Ray; use image::RgbImage; use rand::prelude::thread_rng; use rand::Rng; -use serde::Deserialize; +use serde::{Deserialize, Deserializer}; /// Represent the scene being rendered. -#[derive(Debug, PartialEq, Deserialize)] +#[derive(Debug, PartialEq)] pub struct Scene { camera: Camera, lights: LightAggregate, @@ -182,6 +182,37 @@ fn reflected(incident: Vector, normal: Vector) -> Vector { incident - delt } +#[derive(Debug, PartialEq, Deserialize)] +struct SerializedScene { + camera: Camera, + lights: LightAggregate, + objects: Vec, + aliasing_limit: u32, + reflection_limit: u32, +} + +impl From 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(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let cam: SerializedScene = Deserialize::deserialize(deserializer)?; + Ok(cam.into()) + } +} + #[cfg(test)] mod test { use super::*;