aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorlamp2024-01-21 17:03:07 +0000
committerlamp2024-01-21 17:03:07 +0000
commit45fafbcd4b41a5388ece377c4e051b5846407288 (patch)
tree2192c471ca3b47671f0906e27f6f6088f40b3d8a /src/main.rs
parent89a5c9a8a0cdf627cda0e31da454f83ca21315ce (diff)
fmt and tidy
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs106
1 files changed, 85 insertions, 21 deletions
diff --git a/src/main.rs b/src/main.rs
index 0fe2df9..1e889e6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,21 +1,24 @@
mod camera;
+mod display;
mod hittable;
mod material;
mod ray;
+mod scenes;
+mod texture;
mod util;
mod vec3;
-mod display;
-mod texture;
-mod scenes;
-use std::{sync::{Arc, mpsc}, thread};
+use std::{
+ sync::{mpsc, Arc},
+ thread,
+};
use camera::Camera;
-use hittable::Hittable;
use display::{Display, Image, Pixelflut};
+use hittable::Hittable;
use ray::Ray;
-use vec3::{Vec3, Color, Point3};
use scenes::get_scene;
+use vec3::{Color, Vec3};
struct PixelUpdate {
color: Color,
@@ -33,7 +36,7 @@ fn ray_color(ray: &Ray, background: &Color, world: &dyn Hittable, depth: u32) ->
}
match world.hit(ray, 0.001, f64::INFINITY) {
None => background.clone(),
- Some(mut rec) => {
+ Some(rec) => {
let mut scattered = Ray::new();
let mut attenuation = Color::new();
if let Some(material) = &rec.material {
@@ -44,13 +47,26 @@ fn ray_color(ray: &Ray, background: &Color, world: &dyn Hittable, depth: u32) ->
emitted + attenuation * ray_color(&scattered, background, world, depth - 1)
}
} else {
- Color { x: 0.0, y: 0.0, z: 0.0 }
+ Color {
+ x: 0.0,
+ y: 0.0,
+ z: 0.0,
+ }
}
- },
+ }
}
}
-fn render(image_width: u32, image_height: u32, samples_per_pixel: u32, max_depth: u32, world: Arc<dyn Hittable>, background: Color, camera: Arc<Camera>, tx: mpsc::Sender<PixelUpdate>) {
+fn render(
+ image_width: u32,
+ image_height: u32,
+ samples_per_pixel: u32,
+ max_depth: u32,
+ world: Arc<dyn Hittable>,
+ background: Color,
+ camera: Arc<Camera>,
+ tx: mpsc::Sender<PixelUpdate>,
+) {
for j in (0..image_height).rev() {
for i in 0..image_width {
for _ in 0..samples_per_pixel {
@@ -58,7 +74,12 @@ fn render(image_width: u32, image_height: u32, samples_per_pixel: u32, max_depth
let v = ((j as f64) + rand::random::<f64>()) / ((image_height - 1) as f64);
let ray = camera.get_ray(u, v);
- tx.send(PixelUpdate { color: ray_color(&ray, &background, world.as_ref(), max_depth), x: i as usize, y: j as usize}).unwrap();
+ tx.send(PixelUpdate {
+ color: ray_color(&ray, &background, world.as_ref(), max_depth),
+ x: i as usize,
+ y: j as usize,
+ })
+ .unwrap();
}
}
}
@@ -76,27 +97,67 @@ fn main() {
const TIME_START: f64 = 0.0;
const TIME_END: f64 = 1.0;
// World
- let (world, lookfrom, lookat, vfov, aperture, background) = get_scene(std::env::args().nth(1).unwrap_or("0".to_string()).trim().parse().unwrap_or(0));
+ let (world, lookfrom, lookat, vfov, aperture, background) = get_scene(
+ std::env::args()
+ .nth(1)
+ .unwrap_or("0".to_string())
+ .trim()
+ .parse()
+ .unwrap_or(0),
+ );
// Camera
- let vup = Vec3 { x: 0.0, y: 1.0, z: 0.0 };
+ let vup = Vec3 {
+ x: 0.0,
+ y: 1.0,
+ z: 0.0,
+ };
let dist_to_focus = 10.0;
- let cam = Arc::new(Camera::new(lookfrom, lookat, vup, vfov, ASPECT_RATIO, aperture, dist_to_focus, TIME_START, TIME_END));
+ let cam = Arc::new(Camera::new(
+ lookfrom,
+ lookat,
+ vup,
+ vfov,
+ ASPECT_RATIO,
+ aperture,
+ dist_to_focus,
+ TIME_START,
+ TIME_END,
+ ));
// Render
// let mut final_image = Image::new(IMAGE_WIDTH as usize, IMAGE_HEIGHT as usize);
- let mut final_image = Pixelflut::new("192.168.0.38:1337", 0, 0, IMAGE_WIDTH as usize, IMAGE_HEIGHT as usize);
+ let mut final_image = Pixelflut::new(
+ "192.168.0.38:1337",
+ 0,
+ 0,
+ IMAGE_WIDTH as usize,
+ IMAGE_HEIGHT as usize,
+ );
let (tx, rx) = mpsc::channel::<PixelUpdate>();
for _ in 0..THREAD_COUNT {
let sender = tx.clone();
let world_ref = world.clone();
let camera_ref = cam.clone();
let background_clone = background.clone();
- thread::spawn( || {
- render(IMAGE_WIDTH, IMAGE_HEIGHT, SAMPLES_PER_PIXEL / THREAD_COUNT, MAX_DEPTH, world_ref, background_clone, camera_ref, sender);
+ thread::spawn(|| {
+ render(
+ IMAGE_WIDTH,
+ IMAGE_HEIGHT,
+ SAMPLES_PER_PIXEL / THREAD_COUNT,
+ MAX_DEPTH,
+ world_ref,
+ background_clone,
+ camera_ref,
+ sender,
+ );
});
}
- let expected_updates: u64 = (SAMPLES_PER_PIXEL / THREAD_COUNT) as u64 * THREAD_COUNT as u64 * IMAGE_HEIGHT as u64 * IMAGE_WIDTH as u64;
- let print_frequency: u64 = (SAMPLES_PER_PIXEL / THREAD_COUNT) as u64 * THREAD_COUNT as u64 * IMAGE_WIDTH as u64;
+ let expected_updates: u64 = (SAMPLES_PER_PIXEL / THREAD_COUNT) as u64
+ * THREAD_COUNT as u64
+ * IMAGE_HEIGHT as u64
+ * IMAGE_WIDTH as u64;
+ let print_frequency: u64 =
+ (SAMPLES_PER_PIXEL / THREAD_COUNT) as u64 * THREAD_COUNT as u64 * IMAGE_WIDTH as u64;
let print_frequency = print_frequency * 3;
let mut update_count: u64 = 0;
loop {
@@ -105,11 +166,14 @@ fn main() {
final_image.add_sample(update.x, update.y, update.color);
if update_count % print_frequency == 0 {
final_image.maybe_update();
- eprint!("\rCurrent completion: {:.2}%", (update_count as f64 / expected_updates as f64) * 100.0)
+ eprint!(
+ "\rCurrent completion: {:.2}%",
+ (update_count as f64 / expected_updates as f64) * 100.0
+ )
}
} else {
if Arc::strong_count(&world) == 1 {
- break
+ break;
}
}
}