From f19c8cc40c5caf8abb4f04aaf9f91ec3a8c1ccbc Mon Sep 17 00:00:00 2001 From: Sakarias Johansson Date: Mon, 13 Mar 2023 22:00:44 +0100 Subject: =?UTF-8?q?=F0=9F=93=B8=20Add=20Camera=20defocus=20blur=20+=20Othe?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just wanted to add defocus blur but ended up changing a bunch of other this as well. - Moved scenes to a separate folder. - Updated readme with more pretty images. - Add interface for loading scenes. There is currently one for yaml and another if you want a slightly random scene. - Add image action to decide what to do with the final image once its rendered. Currently supports just showing the buffer until you press the render buffer again and saving the image as `png`. - When you use nix shell you will be dropped in the proper folder so you can just do cargo build etc without having to do `cd`. --- racer-tracer/src/camera.rs | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'racer-tracer/src/camera.rs') diff --git a/racer-tracer/src/camera.rs b/racer-tracer/src/camera.rs index 31c2391..e6396e8 100644 --- a/racer-tracer/src/camera.rs +++ b/racer-tracer/src/camera.rs @@ -1,13 +1,12 @@ use crate::image::Image; use crate::ray::Ray; -use crate::util::degrees_to_radians; +use crate::util::{degrees_to_radians, random_in_unit_disk}; use crate::vec3::Vec3; #[derive(Clone)] pub struct Camera { pub viewport_height: f64, pub viewport_width: f64, - pub focal_length: f64, pub origin: Vec3, pub horizontal: Vec3, pub vertical: Vec3, @@ -15,6 +14,8 @@ pub struct Camera { pub forward: Vec3, pub right: Vec3, pub up: Vec3, + pub lens_radius: f64, + pub focus_distance: f64, } impl Camera { @@ -24,7 +25,8 @@ impl Camera { up: Vec3, vfov: f64, image: &Image, - focal_length: f64, + aperture: f64, + focus_distance: f64, ) -> Camera { let h = (degrees_to_radians(vfov) / 2.0).tan(); let viewport_height = 2.0 * h; @@ -34,45 +36,47 @@ impl Camera { let right = up.cross(&forward).unit_vector(); let up = forward.cross(&right); - let horizontal = viewport_width * right; - let vertical = viewport_height * up; - + let horizontal = focus_distance * viewport_width * right; + let vertical = focus_distance * viewport_height * up; Camera { viewport_height, viewport_width, - focal_length, origin: look_from, horizontal, vertical, - upper_left_corner: look_from + vertical / 2.0 - horizontal / 2.0 - forward, + upper_left_corner: look_from + vertical / 2.0 + - horizontal / 2.0 + - focus_distance * forward, forward, right, up, + lens_radius: aperture * 0.5, + focus_distance, } } pub fn get_ray(&self, u: f64, v: f64) -> Ray { + let ray_direction = self.lens_radius * random_in_unit_disk(); + let offset = self.right * ray_direction.x() + self.up * ray_direction.y(); Ray::new( - self.origin, - self.upper_left_corner + u * self.horizontal - v * self.vertical - self.origin, + self.origin + offset, + self.upper_left_corner + u * self.horizontal - v * self.vertical - self.origin - offset, ) } - // TODO: Add support for rotation - - // TODO: Use forward facing vector pub fn go_forward(&mut self, go: f64) { self.origin += self.forward * go; - self.upper_left_corner = - self.origin + self.vertical / 2.0 - self.horizontal / 2.0 - self.forward; + self.upper_left_corner = self.origin + self.vertical / 2.0 + - self.horizontal / 2.0 + - self.focus_distance * self.forward; } - // TODO: Use right facing vector pub fn go_right(&mut self, go: f64) { self.origin += self.right * go; - self.upper_left_corner = - self.origin + self.vertical / 2.0 - self.horizontal / 2.0 - self.forward; + self.upper_left_corner = self.origin + self.vertical / 2.0 + - self.horizontal / 2.0 + - self.focus_distance * self.forward; } } -- cgit v1.2.3