From eedf0a1f8f297f438c1282c1dc689d3dfae037e4 Mon Sep 17 00:00:00 2001 From: Sakarias Johansson Date: Sun, 16 Apr 2023 18:50:30 +0200 Subject: =?UTF-8?q?=F0=9F=93=B8=20Add=20more=20keyboard=20input=20for=20ca?= =?UTF-8?q?mera?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Numpad +/- changes vfov - Numpad 8/2 to change aperture - Numpad 4/6 to change focus disance --- racer-tracer/src/camera.rs | 48 ++++++++++++++------- racer-tracer/src/scene_controller/interactive.rs | 54 ++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 15 deletions(-) diff --git a/racer-tracer/src/camera.rs b/racer-tracer/src/camera.rs index 438c18d..18b1d1a 100644 --- a/racer-tracer/src/camera.rs +++ b/racer-tracer/src/camera.rs @@ -6,19 +6,21 @@ use crate::vec3::Vec3; #[derive(Clone)] pub struct Camera { - pub viewport_height: f64, - pub viewport_width: f64, - pub origin: Vec3, - pub horizontal: Vec3, - pub vertical: Vec3, - pub upper_left_corner: Vec3, - pub forward: Vec3, - pub right: Vec3, - pub up: Vec3, - pub scene_up: Vec3, - pub lens_radius: f64, - pub focus_distance: f64, - pub aspect_ratio: f64, + viewport_height: f64, + viewport_width: f64, + origin: Vec3, + horizontal: Vec3, + vertical: Vec3, + upper_left_corner: Vec3, + forward: Vec3, + right: Vec3, + up: Vec3, + scene_up: Vec3, + lens_radius: f64, + focus_distance: f64, + aspect_ratio: f64, + vfov: f64, + aperture: f64, } impl Camera { @@ -57,6 +59,8 @@ impl Camera { lens_radius: aperture * 0.5, focus_distance, aspect_ratio: image.aspect_ratio, + vfov, + aperture, } } @@ -71,14 +75,24 @@ impl Camera { } pub fn set_fov(&mut self, vfov: f64) { - let h = (degrees_to_radians(vfov) / 2.0).tan(); + self.vfov = vfov; + let h = (degrees_to_radians(self.vfov) / 2.0).tan(); self.viewport_height = 2.0 * h; self.viewport_width = self.aspect_ratio * self.viewport_height; self.update_viewport(); } + pub fn get_vfov(&self) -> f64 { + self.vfov + } + pub fn set_aperture(&mut self, aperture: f64) { - self.lens_radius = aperture * 0.5; + self.aperture = aperture; + self.lens_radius = self.aperture * 0.5; + } + + pub fn get_aperture(&self) -> f64 { + self.aperture } pub fn set_focus_distance(&mut self, focus_distance: f64) { @@ -86,6 +100,10 @@ impl Camera { self.update_viewport(); } + pub fn get_focus_distance(&self) -> f64 { + self.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(); diff --git a/racer-tracer/src/scene_controller/interactive.rs b/racer-tracer/src/scene_controller/interactive.rs index 5fdecfc..bf0d50d 100644 --- a/racer-tracer/src/scene_controller/interactive.rs +++ b/racer-tracer/src/scene_controller/interactive.rs @@ -138,6 +138,60 @@ impl<'renderer, 'action> SceneController for InteractiveScene<'renderer, 'action cam.go_right(dt * self.camera_speed); }) }), + KeyInputs::input(KeyEvent::Release, Key::NumPadMinus, |_| { + self.camera + .write() + .map_err(|e| TracerError::KeyError(e.to_string())) + .map(|mut cam| { + let vfov = cam.get_vfov(); + cam.set_fov(vfov + 1.0); + }) + }), + KeyInputs::input(KeyEvent::Release, Key::NumPadPlus, |_| { + self.camera + .write() + .map_err(|e| TracerError::KeyError(e.to_string())) + .map(|mut cam| { + let vfov = cam.get_vfov(); + cam.set_fov(vfov - 1.0); + }) + }), + KeyInputs::input(KeyEvent::Release, Key::NumPad8, |_| { + self.camera + .write() + .map_err(|e| TracerError::KeyError(e.to_string())) + .map(|mut cam| { + let aperture = cam.get_aperture(); + cam.set_aperture(aperture + 0.01); + }) + }), + KeyInputs::input(KeyEvent::Release, Key::NumPad2, |_| { + self.camera + .write() + .map_err(|e| TracerError::KeyError(e.to_string())) + .map(|mut cam| { + let aperture = cam.get_aperture(); + cam.set_aperture(aperture - 0.01); + }) + }), + KeyInputs::input(KeyEvent::Release, Key::NumPad4, |_| { + self.camera + .write() + .map_err(|e| TracerError::KeyError(e.to_string())) + .map(|mut cam| { + let focus_distance = cam.get_focus_distance(); + cam.set_focus_distance(focus_distance + 1.0); + }) + }), + KeyInputs::input(KeyEvent::Release, Key::NumPad6, |_| { + self.camera + .write() + .map_err(|e| TracerError::KeyError(e.to_string())) + .map(|mut cam| { + let focus_distance = cam.get_focus_distance(); + cam.set_focus_distance(focus_distance - 1.0); + }) + }), ] } -- cgit v1.2.3