rtiow: implement EnvMap in parser.

This commit is contained in:
2023-02-15 15:46:23 -08:00
parent 6fbdb49ce1
commit 5f0e7a26dd
3 changed files with 53 additions and 29 deletions

View File

@@ -8,7 +8,8 @@ use crate::{
texture::{EnvMap, Texture},
};
use serde::Deserialize;
use std::path::PathBuf;
use thiserror::Error;
use vec3::Vec3;
#[derive(Debug, Deserialize)]
@@ -16,38 +17,40 @@ pub struct Config {
scene: SceneConfig,
camera: CameraConfig,
spheres: Vec<SphereConfig>,
envmap: Option<EnvMapConfig>,
}
impl From<Config> for Scene {
fn from(c: Config) -> Scene {
#[derive(Error, Debug)]
pub enum ConfigError {
#[error("failed to load image")]
ImageError(#[from] image::ImageError),
}
impl TryFrom<Config> for Scene {
type Error = ConfigError;
fn try_from(c: Config) -> Result<Scene, Self::Error> {
// TODO(wathiede): make this something that is loaded from the config and referenced by
// name in the object's config.
let material = Lambertian::new([1., 0., 0.]);
let objects: Vec<Box<dyn Hit>> = c
let spheres: Vec<Box<dyn Hit>> = c
.spheres
.iter()
.map(|sc| -> Box<dyn Hit> {
Box::new(Sphere::new(sc.center, sc.radius, material.clone()))
})
.collect();
let world: Box<dyn Hit> = Box::new(HitableList::new(objects));
let env_map: Option<EnvMap> = None;
let lookfrom = Vec3::new(0., 80., 80.);
let lookat = Vec3::new(0., 0., 0.);
let dist_to_focus = 10.0;
let aperture = 0.0;
let time_min = 0.;
let time_max = 1.;
let camera = Camera::new(
lookfrom,
lookat,
Vec3::new(0., 1., 0.),
45.,
c.scene.width as f32 / c.scene.height as f32,
aperture,
dist_to_focus,
time_min,
time_max,
);
let world: Box<dyn Hit> = Box::new(HitableList::new(spheres));
let mut env_map: Option<EnvMap> = None;
if let Some(em) = c.envmap {
let im = image::open(em.path)?.into_rgb();
env_map = Some(EnvMap::new(im));
};
let camera = make_camera(&c.camera, c.scene.width, c.scene.height);
let scene = Scene {
world,
@@ -60,11 +63,24 @@ impl From<Config> for Scene {
height: c.scene.height,
global_illumination: c.scene.global_illumination.unwrap_or(true),
};
dbg!(&scene);
scene
Ok(scene)
}
}
fn make_camera(cfg: &CameraConfig, width: usize, height: usize) -> Camera {
Camera::new(
cfg.lookfrom.into(),
cfg.lookat.into(),
Vec3::new(0., 1., 0.),
cfg.fov,
width as f32 / height as f32,
cfg.aperture,
cfg.focus_dist,
cfg.time_min,
cfg.time_max,
)
}
#[derive(Debug, Deserialize)]
struct SceneConfig {
subsamples: Option<usize>,
@@ -92,3 +108,8 @@ pub struct CameraConfig {
time_min: f32,
time_max: f32,
}
#[derive(Debug, Deserialize)]
struct EnvMapConfig {
path: PathBuf,
}