Skip to content

Commit

Permalink
Merge pull request #193 from Walther/2023-11-02
Browse files Browse the repository at this point in the history
2023 11 02 work
  • Loading branch information
Walther authored Nov 4, 2023
2 parents 4167c96 + 8e5bd71 commit 8b97384
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
8 changes: 8 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Rust",
"image": "mcr.microsoft.com/devcontainers/rust:1-1-bookworm",
"features": {
"ghcr.io/lee-orr/rusty-dev-containers/cargo-binstall": {},
"ghcr.io/guiyomh/features/just": {}
}
}
6 changes: 3 additions & 3 deletions clovers-cli/src/json_scene.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::Opts;
use clovers::scenes::{self, Scene, SceneFile};
use std::error::Error;
use std::fs::File;
Expand All @@ -9,15 +8,16 @@ use tracing::info;

pub(crate) fn initialize<'scene>(
path: &Path,
opts: &Opts,
width: u32,
height: u32,
) -> Result<Scene<'scene>, Box<dyn Error>> {
let mut file = File::open(path)?;
let mut contents: String = String::new();
file.read_to_string(&mut contents)?;
info!("Parsing the scene file");
let scene_file: SceneFile = serde_json::from_str(&contents)?;
info!("Initializing the scene");
let scene: Scene = scenes::initialize(scene_file, opts.width, opts.height);
let scene: Scene = scenes::initialize(scene_file, width, height);
info!("Count of nodes in the BVH tree: {}", scene.hitables.count());
Ok(scene)
}
64 changes: 34 additions & 30 deletions clovers-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

// External imports
use clap::Parser;
use human_format::Formatter;
use humantime::format_duration;
use image::{ImageBuffer, ImageOutputFormat, Rgb, RgbImage};
use img_parts::png::{Png, PngChunk};
Expand Down Expand Up @@ -60,9 +59,20 @@ struct Opts {
}

fn main() -> Result<(), Box<dyn Error>> {
let opts: Opts = Opts::parse();

if opts.debug {
let Opts {
input,
output,
width,
height,
samples,
max_depth,
quiet,
gpu,
debug,
normalmap,
} = Opts::parse();

if debug {
tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
.with_timer(UtcTime::rfc_3339())
Expand All @@ -76,50 +86,45 @@ fn main() -> Result<(), Box<dyn Error>> {
}

// Pretty printing output, unless in quiet mode
if !opts.quiet {
println!("clovers 🍀 ray tracing in rust 🦀");
println!("width: {}", opts.width);
println!("height: {}", opts.height);
println!("samples: {}", opts.samples);
println!("max depth: {}", opts.max_depth);
let rays: u64 =
opts.width as u64 * opts.height as u64 * opts.samples as u64 * opts.max_depth as u64;
println!("max total rays: {}", Formatter::new().format(rays as f64));
if !quiet {
println!("clovers 🍀 path tracing renderer");
println!();
println!("{width}x{height} resolution");
println!("{samples} samples per pixel");
println!("{max_depth} max bounce depth");
println!(); // Empty line before progress bar
}

let renderopts: RenderOpts = RenderOpts {
width: opts.width,
height: opts.height,
samples: opts.samples,
max_depth: opts.max_depth,
quiet: opts.quiet,
normalmap: opts.normalmap,
width,
height,
samples,
max_depth,
quiet,
normalmap,
};
let threads = std::thread::available_parallelism()?;

info!("Reading the scene file");
let path = Path::new(&opts.input);
let path = Path::new(&input);
let scene = match path.extension() {
Some(ext) => match &ext.to_str() {
Some("json") => json_scene::initialize(path, &opts),
Some("json") => json_scene::initialize(path, width, height),
_ => panic!("Unknown file type"),
},
None => panic!("Unknown file type"),
}?;

info!("Calling draw()");
let start = Instant::now();
let pixelbuffer = match opts.gpu {
let pixelbuffer = match gpu {
// Note: live progress bar printed within draw_cpu::draw
false => draw_cpu::draw(renderopts, &scene),
true => unimplemented!("GPU accelerated rendering is currently unimplemented"),
};
info!("Drawing a pixelbuffer finished");

info!("Converting pixelbuffer to an image");
let width = opts.width;
let height = opts.height;
let mut img: RgbImage = ImageBuffer::new(width, height);
img.enumerate_pixels_mut().for_each(|(x, y, pixel)| {
let index = y * width + x;
Expand All @@ -135,8 +140,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let formatted_duration = format_duration(duration);
info!("Finished render in {}", formatted_duration);

if !opts.quiet {
println!(); // Empty line after progress bar
if !quiet {
println!("Finished render in {}", formatted_duration);
}

Expand All @@ -146,10 +150,10 @@ fn main() -> Result<(), Box<dyn Error>> {
img.write_to(&mut Cursor::new(&mut bytes), ImageOutputFormat::Png)?;
let mut png = Png::from_bytes(bytes.into())?;

let comment = if opts.normalmap {
format!("Comment\0{} rendered with the clovers raytracing engine at {}x{} in normalmap mode. finished render in {}, using {} threads", opts.input, opts.width, opts.height, formatted_duration, threads)
let comment = if normalmap {
format!("Comment\0{input} rendered with the clovers raytracing engine at {width}x{height} in normalmap mode. finished render in {formatted_duration}, using {threads} threads")
} else {
format!("Comment\0{} rendered with the clovers raytracing engine at {}x{}, {} samples per pixel, {} max ray bounce depth. finished render in {}, using {} threads", opts.input, opts.width, opts.height, opts.samples, opts.max_depth, formatted_duration, threads)
format!("Comment\0{input} rendered with the clovers raytracing engine at {width}x{height}, {samples} samples per pixel, {max_depth} max ray bounce depth. finished render in {formatted_duration}, using {threads} threads")
};
let software = "Software\0https://github.com/walther/clovers".to_string();

Expand All @@ -159,7 +163,7 @@ fn main() -> Result<(), Box<dyn Error>> {
png.chunks_mut().push(chunk);
}

let target = match opts.output {
let target = match output {
Some(filename) => filename,
None => {
// Default to using a timestamp & `renders/` directory
Expand Down

0 comments on commit 8b97384

Please sign in to comment.