//! Camera abstraction for creating 2D projections from a 3D scene. use std::f32::consts::PI; use ombre::prelude::*; /// Camera input sensitivty settings. #[derive(Debug)] pub struct CameraSensitivity { /// Orbit sensitivty. pub orbit: f32, /// Dolly sensitivty. pub dolly: f32, /// Pan sensitivty. pub pan: f32, } impl CameraSensitivity { pub fn fov(angle: f32) -> Self { Self { orbit: 0.0015 * angle / 90., dolly: 0.0020 * angle / 90., pan: 0.0015 * angle / 90., } } } impl Default for CameraSensitivity { fn default() -> Self { Self { orbit: 0.0025, dolly: 0.0050, pan: 0.0025, } } } /// Camera object. #[derive(Debug)] pub struct Camera { /// Camera "eye" position. pub eye: Vector3D, /// Where the camera is looking at. pub target: Vector3D, /// An optional object position that is in focus. /// When resetting the camera, we set the target to this object. pub object: Option>, /// Up vector (roll). pub up: Vector3D, /// Controls the sensitivty to user input. pub sensitivity: CameraSensitivity, /// Near Z-plane. pub near: f32, /// Far Z-plane. pub far: f32, /// Field of view (degrees). pub fov: f32, } impl Camera { /// Reset the camera so that it looks at its object straight on. pub fn reset(&mut self) { self.target = self.object.unwrap_or_default(); self.eye = self.target + Vector3D::new(0., 0., 5.); // TODO: Compute distance. } /// Pan the camera. pub fn pan(&mut self, delta: Vector2D) { let scale = self.sensitivity.pan * (self.target - self.eye).magnitude(); let forward = (self.target - self.eye).normalize(); let right = Vector3D::cross(forward, self.up).normalize(); let up = Vector3D::cross(right, forward).normalize(); let horizontal = right * -delta.x * scale; let vertical = up * delta.y * scale; self.eye = self.eye + horizontal + vertical; self.target = self.target + horizontal + vertical; } /// Dolly the camera. pub fn dolly(&mut self, delta: Vector2D) { let mut sph = self.eye.to_spherical(self.target); // Scale sensitivty based on distance from object. sph.x -= (delta.y * self.sensitivity.dolly) * sph.x; sph.x = sph.x.max(0.1); // Convert back to cartesian coordinates. self.eye = sph.to_world() + self.target; } /// Orbit the camera around its target. pub fn orbit(&mut self, delta: impl Into>) { let delta = delta.into(); let mut sph = self.eye.to_spherical(self.target); let max = self.sensitivity.orbit * 10.; sph.y += (delta.x * self.sensitivity.orbit * sph.x).clamp(-max, max); sph.z -= (delta.y * self.sensitivity.orbit * sph.x).clamp(-max, max); // Clamp phi to avoid going over the poles. sph.z = sph.z.clamp(0.01, PI - 0.01); // Convert back to cartesian coordinates. self.eye = sph.to_world() + self.target; } /// Return a projection transform based on the camera, ie. a perspective matrix. pub fn projection(&self, size: Size) -> Transform3D { Transform3D::::perspective( self.fov, size.w / size.h, self.near, self.far, ) * Transform3D::::from_nonuniform_scale(1.0, -1.0, 1.0) } /// Return a view transform based on the camera, ie. a "look at" matrix. pub fn view(&self) -> Transform3D { Transform3D::::look_at(self.eye, self.target, self.up) } }