Skip to content

Commit

Permalink
simplified main function
Browse files Browse the repository at this point in the history
  • Loading branch information
scott223 committed Nov 13, 2023
1 parent c8163ab commit 1d7e0ae
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 35 deletions.
2 changes: 1 addition & 1 deletion benches/default_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/bvh/aabb.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -171,8 +171,8 @@ impl Index<usize> 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;

Expand Down
2 changes: 1 addition & 1 deletion src/bvh/bvh_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
4 changes: 1 addition & 3 deletions src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*};

Expand Down
2 changes: 1 addition & 1 deletion src/linalg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ mod mat4;
pub use mat4::Mat4;

mod vec4;
pub use vec4::Vec4;
pub use vec4::Vec4;
2 changes: 1 addition & 1 deletion src/linalg/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ impl Vec4 {
pub fn to_vec3(self) -> Vec3 {
Vec3::new(self.x, self.y, self.z)
}
}
}
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
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!(
"Program started: loading scene and config from JSON files located in /input and render will be located in /render. Base directory is {:?}",
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(()) => {
Expand Down
52 changes: 31 additions & 21 deletions src/render/integrator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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)]
Expand All @@ -25,35 +31,35 @@ 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 =
File::open(config_path).expect("Error reading input/config.json, quitting");
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);
}

Expand All @@ -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
Expand All @@ -88,7 +97,7 @@ impl RenderIntegrator {

let data = raw_pixels; // An array containing a RGB sequence
writer.write_image_data(&data).unwrap(); // Save

Ok(())
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1d7e0ae

Please sign in to comment.