From 862cf9215e203f805d96d5f1900e2354ef458ca5 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Fri, 24 Feb 2023 04:46:05 -0600 Subject: [PATCH] Divide by max --- assignment-1c/src/image.rs | 7 ++++++- assignment-1c/src/scene/input_file.rs | 23 +++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/assignment-1c/src/image.rs b/assignment-1c/src/image.rs index 21b4f01..3bafc31 100644 --- a/assignment-1c/src/image.rs +++ b/assignment-1c/src/image.rs @@ -43,6 +43,7 @@ impl Image { let width = parts[1].parse::()?; let height = parts[2].parse::()?; + let max_value = parts[3].parse::()?; let numbers = Gn::<()>::new_scoped(move |mut s| { macro_rules! gen_try { @@ -77,7 +78,11 @@ impl Image { None => bail!("Not enough elements"), }; - let color = Color::new(r?, g?, b?); + let r = r? / max_value as f64; + let g = g? / max_value as f64; + let b = b? / max_value as f64; + + let color = Color::new(r, g, b); data.push(color); } diff --git a/assignment-1c/src/scene/input_file.rs b/assignment-1c/src/scene/input_file.rs index 29b78b8..512b578 100644 --- a/assignment-1c/src/scene/input_file.rs +++ b/assignment-1c/src/scene/input_file.rs @@ -1,11 +1,15 @@ -use std::{fs::File, io::Read, path::Path}; +use std::{ + fs::File, + io::Read, + path::{Path, PathBuf}, +}; use anyhow::Result; use itertools::Itertools; use nalgebra::Vector3; use crate::{ - image::Color, + image::{Color, Image}, scene::{ cylinder::Cylinder, data::{Attenuation, Light, LightKind, Material, Object}, @@ -21,11 +25,13 @@ use super::data::{DepthCueing, ObjectKind}; impl Scene { /// Parse the input file into a scene pub fn from_input_file(path: impl AsRef) -> Result { + let path = path.as_ref(); + // Scope the read so the file is dropped and closed immediately after the // contents have been read to memory let contents = { let mut contents = String::new(); - let mut file = File::open(path.as_ref())?; + let mut file = File::open(path)?; file.read_to_string(&mut contents)?; contents }; @@ -281,7 +287,16 @@ impl Scene { } } - "texture" => {} + "texture" => { + let input_parent = path.parent().unwrap().to_path_buf(); + let path = match parts.next() { + Some(s) => input_parent.join(s), + None => bail!("Did not provide path."), + }; + + let image = Image::from_file(path)?; + scene.textures.push(image); + } _ => bail!("Unknown keyword {keyword}"), }