ft: deserialization

This commit is contained in:
2026-04-26 23:49:03 +02:00
parent 430bdf63bc
commit 076bcc7155
5 changed files with 55 additions and 27 deletions

View File

@@ -0,0 +1,22 @@
{
"camera": {
"image_width": 1920,
"image_height": 1080,
"anti_alias_rate": 1,
"max_depth": 10,
"fov": 90.0,
"look_from": { "x": -10, "y": 5, "z": 10 },
"look_at": { "x": 0.0, "y": 0.0, "z": -1.0 },
"vup": { "x": 0.0, "y": 1.0, "z": 0.0 }
},
"materials": [
{ "type": "metal", "albedo": { "x": 0.2, "y": 0.4, "z": 0.8 }, "prob": 1.0, "fuzz": 0.1 }
],
"objects": [
{ "type": "sphere", "center": { "x": 0, "y": 0.7, "z": -0.4 }, "radius": 0.2, "material": 1}
]
}

View File

@@ -2,10 +2,10 @@
"camera": { "camera": {
"image_width": 1920, "image_width": 1920,
"image_height": 1080, "image_height": 1080,
"anti_alias_rate": 3, "anti_alias_rate": 23,
"max_depth": 50, "max_depth": 100,
"fov": 90.0, "fov": 40.0,
"look_from": { "x": 0, "y": 0, "z": 0 }, "look_from": { "x": -10, "y": 5, "z": 10 },
"look_at": { "x": 0.0, "y": 0.0, "z": -1.0 }, "look_at": { "x": 0.0, "y": 0.0, "z": -1.0 },
"vup": { "x": 0.0, "y": 1.0, "z": 0.0 } "vup": { "x": 0.0, "y": 1.0, "z": 0.0 }
}, },
@@ -25,7 +25,8 @@
{ "type": "sphere", "center": { "x": 0.0, "y": -100.5, "z": -1.0 }, "radius": 100.0, "material": 2}, { "type": "sphere", "center": { "x": 0.0, "y": -100.5, "z": -1.0 }, "radius": 100.0, "material": 2},
{ "type": "sphere", "center": { "x": 0.0, "y": 0.0, "z": -1.2 }, "radius": 0.5, "material": 3}, { "type": "sphere", "center": { "x": 0.0, "y": 0.0, "z": -1.2 }, "radius": 0.5, "material": 3},
{ "type": "sphere", "center": { "x": -1, "y": 0, "z": -1 }, "radius": 0.4, "material": 5}, { "type": "sphere", "center": { "x": -1, "y": 0, "z": -1 }, "radius": 0.4, "material": 5},
{ "type": "sphere", "center": { "x": 1, "y": 0, "z": -1 }, "radius": 0.5, "material": 6} { "type": "sphere", "center": { "x": 1, "y": 0, "z": -1 }, "radius": 0.5, "material": 6},
{ "type": "sphere", "center": { "x": 20, "y": 7, "z": -15 }, "radius": 10.5, "material": 0}
] ]
} }

View File

@@ -72,14 +72,15 @@ fn random_sphere_on_floor<T: Hittable>(
sphere sphere
} }
// FIXME: bunch of unwraps/expects in deserialization code
// TODO: implement scene serialization // TODO: implement scene serialization
fn main() { fn main() {
dotenv().ok(); dotenv().ok();
pretty_env_logger::init(); pretty_env_logger::init();
// TODO: use cli arg for scenefile // TODO: use cli arg for scenefile
let json_str = fs::read_to_string("./scenes/scene.json").expect("waddehell!"); let json_file = "./scenes/scenes.json";
// let json_file = "./scenes/failsMatBounds.json";
let json_str = fs::read_to_string(json_file).expect("Reading specified scene file failed!");
let mut scene: Scene = serde_json::from_str(&json_str).unwrap(); let mut scene: Scene = serde_json::from_str(&json_str).unwrap();
scene.render(); scene.render();

View File

@@ -41,11 +41,9 @@ impl Default for RawCamera {
} }
} }
impl TryFrom<RawCamera> for Camera { impl From<RawCamera> for Camera {
type Error = String; fn from(raw: RawCamera) -> Self {
Camera::new_full(
fn try_from(raw: RawCamera) -> Result<Self, Self::Error> {
let c = Camera::new_full(
raw.image_width, raw.image_width,
raw.image_height, raw.image_height,
raw.anti_alias_rate, raw.anti_alias_rate,
@@ -54,8 +52,6 @@ impl TryFrom<RawCamera> for Camera {
raw.look_from, raw.look_from,
raw.look_at, raw.look_at,
raw.vup, raw.vup,
); )
Ok(c)
} }
} }

View File

@@ -1,8 +1,6 @@
use std::{ use std::sync::Arc;
fmt::{self, Debug},
sync::Arc,
};
use log::warn;
use serde::Deserialize; use serde::Deserialize;
use crate::{ use crate::{
@@ -53,10 +51,10 @@ impl<'de> Deserialize<'de> for Scene {
let objs: Vec<Arc<dyn Hittable>> = conc let objs: Vec<Arc<dyn Hittable>> = conc
.objects .objects
.into_iter() .into_iter()
.map(|h| h.into_arc(&mats)) .filter_map(|h| h.into_arc(&mats))
.collect(); .collect();
Ok(Self { Ok(Self {
camera: Camera::try_from(conc.camera).unwrap(), camera: Camera::from(conc.camera),
materials: mats, materials: mats,
objects: objs, objects: objs,
}) })
@@ -77,13 +75,23 @@ enum HittableDef {
} }
impl HittableDef { impl HittableDef {
fn into_arc(self, materials: &Vec<Arc<dyn Material>>) -> Arc<dyn Hittable> { fn into_arc(self, materials: &Vec<Arc<dyn Material>>) -> Option<Arc<dyn Hittable>> {
// THOUGHT: i think this can be done better; in the map/filter call up there?
match self { match self {
HittableDef::Sphere(s) => Arc::new(Sphere::new( HittableDef::Sphere(s) => {
s.center, if s.material as usize >= materials.len() {
s.radius, warn!(
materials.get(s.material as usize).unwrap().clone(), "Sphere specified nonexistent material {}; skipping...",
)), s.material
);
return None;
}
Some(Arc::new(Sphere::new(
s.center,
s.radius,
materials.get(s.material as usize).unwrap().clone(),
)))
}
} }
} }
} }