aboutsummaryrefslogtreecommitdiffstats
path: root/src/render.rs
diff options
context:
space:
mode:
authorYann Herklotz <git@yannherklotz.com>2020-10-10 00:12:12 +0100
committerYann Herklotz <git@yannherklotz.com>2020-10-10 00:12:12 +0100
commit495d1c9c113098f24767b595c7e830f0fa8bc991 (patch)
tree95eac21eacecadc58f23a59216e764ddd6928979 /src/render.rs
parentdd5ac7c5fe336e9cac794cfa72d139613a99b466 (diff)
downloadleela-495d1c9c113098f24767b595c7e830f0fa8bc991.tar.gz
leela-495d1c9c113098f24767b595c7e830f0fa8bc991.zip
Format all the files and add moving sphere
Diffstat (limited to 'src/render.rs')
-rw-r--r--src/render.rs72
1 files changed, 48 insertions, 24 deletions
diff --git a/src/render.rs b/src/render.rs
index 47c6698..c3d132a 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -1,20 +1,22 @@
+use crate::camera::Camera;
+use crate::hittable::Hittable;
+use crate::ray::Ray;
+use crate::scene::Scene;
+use crate::utils::print_colour;
use cgmath::prelude::*;
-use cgmath::{Vector3, vec3};
+use cgmath::{vec3, Vector3};
use rand::prelude::*;
use rayon::prelude::*;
use std::f64::INFINITY;
-use crate::camera::Camera;
-use crate::scene::Scene;
-use crate::utils::{print_colour};
-use crate::ray::Ray;
-use crate::hittable::Hittable;
fn ray_colour(rng: &mut ThreadRng, ray: &Ray, scene: &Scene, depth: i32) -> Vector3<f64> {
- if depth <= 0 { return vec3(0.0, 0.0, 0.0) }
+ if depth <= 0 {
+ return vec3(0.0, 0.0, 0.0);
+ }
if let Some(t) = scene.is_hit(ray, 0.001, INFINITY) {
if let Some(nray) = t.material.scatter(rng, ray, &t) {
- ray_colour(rng, &nray.ray, scene, depth-1).mul_element_wise(nray.attenuation)
+ ray_colour(rng, &nray.ray, scene, depth - 1).mul_element_wise(nray.attenuation)
} else {
vec3(0.0, 0.0, 0.0)
}
@@ -24,10 +26,19 @@ fn ray_colour(rng: &mut ThreadRng, ray: &Ray, scene: &Scene, depth: i32) -> Vect
}
}
-pub fn thread_render(world: &Scene, camera: &Camera, rng: &mut ThreadRng, image_height: i32,
- image_width: i32, samples: i32, max_depth: i32, i: i32, j: i32) -> Vector3<f64> {
+pub fn thread_render(
+ world: &Scene,
+ camera: &Camera,
+ rng: &mut ThreadRng,
+ image_height: i32,
+ image_width: i32,
+ samples: i32,
+ max_depth: i32,
+ i: i32,
+ j: i32,
+) -> Vector3<f64> {
let mut colour = vec3(0.0, 0.0, 0.0);
- for _ in 0 .. samples {
+ for _ in 0..samples {
let ru: f64 = rng.gen();
let rv: f64 = rng.gen();
let u = (i as f64 + ru) / image_width as f64;
@@ -38,19 +49,32 @@ pub fn thread_render(world: &Scene, camera: &Camera, rng: &mut ThreadRng, image_
colour
}
-pub fn render(world: &Scene, camera: &Camera, image_height: i32, image_width: i32,
- samples: i32, max_depth: i32, threads: i32) {
- for j in 0 .. image_height {
- eprint!("\rScanlines: {:3} / {:3}", j+1, image_height);
- for i in 0 .. image_width {
- let colours = (0 .. threads)
- .into_par_iter()
- .map(|_| {
- let mut trng = thread_rng();
- thread_render(
- world, camera, &mut trng, image_height, image_width,
- samples / threads, max_depth, i, j)
- });
+pub fn render(
+ world: &Scene,
+ camera: &Camera,
+ image_height: i32,
+ image_width: i32,
+ samples: i32,
+ max_depth: i32,
+ threads: i32,
+) {
+ for j in 0..image_height {
+ eprint!("\rScanlines: {:3} / {:3}", j + 1, image_height);
+ for i in 0..image_width {
+ let colours = (0..threads).into_par_iter().map(|_| {
+ let mut trng = thread_rng();
+ thread_render(
+ world,
+ camera,
+ &mut trng,
+ image_height,
+ image_width,
+ samples / threads,
+ max_depth,
+ i,
+ j,
+ )
+ });
let colour = colours.sum::<Vector3<f64>>();
print_colour(&colour, samples);
}