Skip to content

Commit

Permalink
Add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
grishy committed Feb 14, 2024
1 parent 383bf6c commit 9428b26
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nalgebra = "0.32.3"
nalgebra = "0.32.3"
indicatif = "0.17.8"
rayon = "1.8.1"
itertools = "0.12.1"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Rust Ray Tracer

## Source
https://raytracing.github.io/

## Development

### Start
Expand Down
44 changes: 25 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
use itertools::Itertools;
use rayon::prelude::*;
use std::fs::File;
use std::io::prelude::*;

Expand Down Expand Up @@ -62,10 +65,8 @@ struct hittable_list {
}

impl hittable_list {
fn new()-> hittable_list {
hittable_list{
objects: vec![],
}
fn new() -> hittable_list {
hittable_list { objects: vec![] }
}
fn add(&mut self, object: Box<dyn hittable>) {
self.objects.push(object);
Expand All @@ -89,7 +90,7 @@ struct Sphere {
}
impl Sphere {
fn new(center: Point3, radius: f64) -> Sphere {
Sphere{
Sphere {
center: center,
radius: radius,
}
Expand Down Expand Up @@ -176,13 +177,13 @@ fn main() {
// Params
let aspect_ratio = 16.0 / 9.0;

let image_width = 400;
let image_width = 600;
let image_height = (image_width as f64 / aspect_ratio) as i32;

// World
let mut world = Box::new(hittable_list::new());
world.add(Box::new(Sphere::new(Point3::new(0.0,0.0,-1.0), 0.5)));
world.add(Box::new(Sphere::new(Point3::new(0.0,-100.5,-1.0), 100.0)));
world.add(Box::new(Sphere::new(Point3::new(0.0, 0.1, -1.0), 0.5)));
world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));

// Camera
let focal_length = 1.0;
Expand Down Expand Up @@ -210,18 +211,23 @@ fn main() {
let mut image_file = File::create("target/image.ppm").unwrap();
writeln!(image_file, "P3\n{} {}\n255", image_width, image_height).unwrap();

for j in 0..image_height {
println!("\rScanlines remaining: {}", (image_height - j));
for i in 0..image_width {
let pixel_center =
pixel00_loc + (i as f64 * pixel_delta_u) + (j as f64 * pixel_delta_v);
let ray_direction = pixel_center - camera_center;
let pb = ProgressBar::new(image_width as u64 * image_height as u64);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {pos}/{len} ({eta})")
.unwrap()
.progress_chars("#>-"));

let r = Ray::new(camera_center, ray_direction);

let pixel_color = ray_color(&r, &world);
write_color(&mut image_file, pixel_color);
}
for (y, x) in (0..image_height).cartesian_product(0..image_width) {
let pixel_center =
pixel00_loc + (x as f64 * pixel_delta_u) + (y as f64 * pixel_delta_v);
let ray_direction = pixel_center - camera_center;

let r = Ray::new(camera_center, ray_direction);

let pixel_color = ray_color(&r, &world);
write_color(&mut image_file, pixel_color);
pb.inc(1);
}
println!("Done");

pb.finish();
}

0 comments on commit 9428b26

Please sign in to comment.