diff --git a/benches/default_benchmark.rs b/benches/default_benchmark.rs index bf25660..274101d 100644 --- a/benches/default_benchmark.rs +++ b/benches/default_benchmark.rs @@ -4,8 +4,8 @@ use rand_distr::Distribution; use rand_distr::Uniform; use raytracer::bvh::Aabb; -use raytracer::render::Interval; use raytracer::linalg::Vec3; +use raytracer::render::Interval; use raytracer::render::Ray; pub fn criterion_benchmark(c: &mut Criterion) { diff --git a/src/bvh/aabb.rs b/src/bvh/aabb.rs index f7d4de9..070e46e 100644 --- a/src/bvh/aabb.rs +++ b/src/bvh/aabb.rs @@ -1,6 +1,6 @@ use crate::elements::Hittable; -use crate::render::Interval; use crate::linalg::Vec3; +use crate::render::Interval; use crate::render::Ray; use std::fmt; @@ -171,8 +171,8 @@ impl Index for Aabb { #[cfg(test)] mod tests { use crate::bvh::aabb::Aabb; - use crate::render::Interval; use crate::linalg::Vec3; + use crate::render::Interval; use crate::render::Ray; use assert_approx_eq::assert_approx_eq; diff --git a/src/bvh/bvh_node.rs b/src/bvh/bvh_node.rs index 4744a2f..db62da3 100644 --- a/src/bvh/bvh_node.rs +++ b/src/bvh/bvh_node.rs @@ -3,8 +3,8 @@ use std::fmt::Debug; use super::aabb::Aabb; use crate::elements::*; -use crate::render::Interval; use crate::linalg::Vec3; +use crate::render::Interval; use crate::render::Ray; use rand::rngs::SmallRng; diff --git a/src/config.rs b/src/config.rs index ff45414..89a8003 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use crate::elements::*; -use crate::render::Interval; use crate::render::camera::Camera; +use crate::render::Interval; use crate::render::Ray; use crate::{color::Color, render::camera::JSONCamera}; diff --git a/src/elements.rs b/src/elements.rs index 41fcd61..404541f 100644 --- a/src/elements.rs +++ b/src/elements.rs @@ -11,10 +11,8 @@ use serde::{Deserialize, Serialize}; extern crate wavefront_obj; use wavefront_obj::obj; +use crate::linalg::{Mat4, Onb, Vec3, Vec4}; use crate::render::Interval; -use crate::linalg::{ - Onb, Vec3, Mat4, Vec4, -}; use crate::render::Ray; use crate::{bvh::Aabb, materials::*}; diff --git a/src/linalg/mod.rs b/src/linalg/mod.rs index 765b598..7b6b8ad 100644 --- a/src/linalg/mod.rs +++ b/src/linalg/mod.rs @@ -8,4 +8,4 @@ mod mat4; pub use mat4::Mat4; mod vec4; -pub use vec4::Vec4; \ No newline at end of file +pub use vec4::Vec4; diff --git a/src/linalg/vec4.rs b/src/linalg/vec4.rs index fc747f2..e4d2a97 100644 --- a/src/linalg/vec4.rs +++ b/src/linalg/vec4.rs @@ -28,4 +28,4 @@ impl Vec4 { pub fn to_vec3(self) -> Vec3 { Vec3::new(self.x, self.y, self.z) } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 521c697..3f74ade 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ use dotenv::dotenv; -use std::env; use raytracer::render::RenderIntegrator; +use std::env; // App main function // load the JSON files for the Scene and for the Configuration, and calls the main raytracer function render from lib.rs fn main() { dotenv().ok(); - + env_logger::init(); log::info!( @@ -14,12 +14,12 @@ fn main() { env::current_dir().unwrap() ); - let mut r: RenderIntegrator = RenderIntegrator::new_from_json("input/scene.json", "input/config.json"); + let mut r: RenderIntegrator = + RenderIntegrator::new_from_json("input/scene.json", "input/config.json"); // execute the main render match r.render() { Ok(()) => { - // success, so save to png match r.save_to_png("renders/render.png") { Ok(()) => { diff --git a/src/render/integrator.rs b/src/render/integrator.rs index db5e6a7..f0f9a8d 100644 --- a/src/render/integrator.rs +++ b/src/render/integrator.rs @@ -1,4 +1,10 @@ -use std::{error::Error, time::Instant, fs::File, io::{BufReader, BufWriter}, path::Path}; +use std::{ + error::Error, + fs::File, + io::{BufReader, BufWriter}, + path::Path, + time::Instant, +}; use indicatif::ProgressBar; use rand::{rngs::SmallRng, SeedableRng}; @@ -9,11 +15,11 @@ use crate::{ color::Color, config::{Config, JSONScene}, elements::{Element, Hittable, JSONElement}, - render::Interval, materials::{Emmits, Reflects, Refracts, Scatterable}, render::camera::Camera, render::pdf::{HittablePDF, MixedPDF, PDFTrait, Pdf}, render::ray::Ray, + render::Interval, }; #[derive(Debug, Clone)] @@ -25,25 +31,25 @@ pub struct RenderIntegrator { impl RenderIntegrator { pub fn new(json_scene: JSONScene, config: Config) -> Self { - // create a 1-d vector holding all the pixels, and - let pixels = vec![ - Color::new(0.0, 0.0, 0.0); - (config.img_width * config.img_height) as usize - ]; - - RenderIntegrator { json_scene, config, pixels } + // create a 1-d vector holding all the pixels, and + let pixels = + vec![Color::new(0.0, 0.0, 0.0); (config.img_width * config.img_height) as usize]; + + RenderIntegrator { + json_scene, + config, + pixels, + } } pub fn new_from_json(scene_path: &str, config_path: &str) -> Self { - // Open the Scene file - let scene_file = - File::open(scene_path).expect("Error reading input/scene.json, quitting"); + let scene_file = File::open(scene_path).expect("Error reading input/scene.json, quitting"); let scene_reader = BufReader::new(scene_file); // Read the JSON contents of the file as an instance of `Scene`. - let scene: JSONScene = - serde_json::from_reader(scene_reader).expect("Error parsing input/scene.json, quitting"); + let scene: JSONScene = serde_json::from_reader(scene_reader) + .expect("Error parsing input/scene.json, quitting"); // Open the Config file let config_file = @@ -51,9 +57,9 @@ impl RenderIntegrator { let config_reader = BufReader::new(config_file); // Read the JSON contents of the file as an instance of `Config`. - let config: Config = - serde_json::from_reader(config_reader).expect("Error parsing input/config.json, quitting"); - + let config: Config = serde_json::from_reader(config_reader) + .expect("Error parsing input/config.json, quitting"); + return RenderIntegrator::new(scene, config); } @@ -71,8 +77,11 @@ impl RenderIntegrator { let file = File::create(path).unwrap(); let w = &mut BufWriter::new(file); - let mut encoder = - png::Encoder::new(w, self.config.img_width as u32, self.config.img_height as u32); // Width x heigth + let mut encoder = png::Encoder::new( + w, + self.config.img_width as u32, + self.config.img_height as u32, + ); // Width x heigth encoder.set_color(png::ColorType::Rgb); encoder.set_depth(png::BitDepth::Eight); encoder.set_source_gamma(png::ScaledFloat::new(1.0 / 2.2)); // 1.0 / 2.2, unscaled, but rounded @@ -88,7 +97,7 @@ impl RenderIntegrator { let data = raw_pixels; // An array containing a RGB sequence writer.write_image_data(&data).unwrap(); // Save - + Ok(()) } @@ -124,7 +133,8 @@ impl RenderIntegrator { // split into bands for parallel rendering - let bands: Vec<(usize, &mut [Color])> = self.pixels + let bands: Vec<(usize, &mut [Color])> = self + .pixels .chunks_mut(self.config.img_width as usize) .enumerate() .collect();