Divide by max

This commit is contained in:
Michael Zhang 2023-02-24 04:46:05 -06:00
parent 021ba892f8
commit 862cf9215e
2 changed files with 25 additions and 5 deletions

View file

@ -43,6 +43,7 @@ impl Image {
let width = parts[1].parse::<usize>()?; let width = parts[1].parse::<usize>()?;
let height = parts[2].parse::<usize>()?; let height = parts[2].parse::<usize>()?;
let max_value = parts[3].parse::<usize>()?;
let numbers = Gn::<()>::new_scoped(move |mut s| { let numbers = Gn::<()>::new_scoped(move |mut s| {
macro_rules! gen_try { macro_rules! gen_try {
@ -77,7 +78,11 @@ impl Image {
None => bail!("Not enough elements"), 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); data.push(color);
} }

View file

@ -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 anyhow::Result;
use itertools::Itertools; use itertools::Itertools;
use nalgebra::Vector3; use nalgebra::Vector3;
use crate::{ use crate::{
image::Color, image::{Color, Image},
scene::{ scene::{
cylinder::Cylinder, cylinder::Cylinder,
data::{Attenuation, Light, LightKind, Material, Object}, data::{Attenuation, Light, LightKind, Material, Object},
@ -21,11 +25,13 @@ use super::data::{DepthCueing, ObjectKind};
impl Scene { impl Scene {
/// Parse the input file into a scene /// Parse the input file into a scene
pub fn from_input_file(path: impl AsRef<Path>) -> Result<Self> { pub fn from_input_file(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
// Scope the read so the file is dropped and closed immediately after the // Scope the read so the file is dropped and closed immediately after the
// contents have been read to memory // contents have been read to memory
let contents = { let contents = {
let mut contents = String::new(); 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)?; file.read_to_string(&mut contents)?;
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}"), _ => bail!("Unknown keyword {keyword}"),
} }