aboutsummaryrefslogtreecommitdiffstats
path: root/src/render.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/render.rs')
-rw-r--r--src/render.rs21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/render.rs b/src/render.rs
index 6e2abf8..740c3eb 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -3,24 +3,23 @@ use cgmath::{Vector3, vec3};
use rand::prelude::*;
use std::f64::INFINITY;
use crate::camera::Camera;
-use crate::colour::print_colour;
use crate::scene::Scene;
-use crate::random::random_in_unit_sphere;
+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) }
- match scene.is_hit(ray, 0.001, INFINITY) {
- None => {
- let t = 0.5 * (ray.dir.normalize().y + 1.0);
- (1.0 - t) * vec3(1.0, 1.0, 1.0) + t * vec3(0.5, 0.7, 1.0)
- }
- Some(t) => {
- let target = t.p + t.normal + random_in_unit_sphere(rng);
- 0.5 * ray_colour(rng, &Ray::new(t.p, target - t.p), scene, depth - 1)
+ 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)
+ } else {
+ vec3(0.0, 0.0, 0.0)
}
+ } else {
+ let t = 0.5 * (ray.dir.normalize().y + 1.0);
+ (1.0 - t) * vec3(1.0, 1.0, 1.0) + t * vec3(0.5, 0.7, 1.0)
}
}
@@ -35,7 +34,7 @@ pub fn render(world: &Scene, camera: Camera, image_height: i32, image_width: i32
let rv: f64 = rng.gen();
let u = (i as f64 + ru) / image_width as f64;
let v = ((image_height - 1 - j) as f64 + rv) / image_height as f64;
- let ray = camera.get_ray(u, v);
+ let ray = camera.get_ray(rng, u, v);
colour += ray_colour(rng, &ray, &world, max_depth);
}
print_colour(&colour, samples);