From d3f8b55aa971d1ef00345e80bc40365927bf08e5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 27 Nov 2023 20:51:26 +0100 Subject: feat(engine): add support for custom camera implementations --- engine/src/camera.rs | 102 ++++++--------------------------------------------- 1 file changed, 11 insertions(+), 91 deletions(-) (limited to 'engine/src/camera.rs') diff --git a/engine/src/camera.rs b/engine/src/camera.rs index fab92f5..1bae5bb 100644 --- a/engine/src/camera.rs +++ b/engine/src/camera.rs @@ -1,99 +1,19 @@ -use crate::matrix::Matrix; use crate::vector::Vec3; -#[derive(Debug)] -pub struct Camera +pub trait Camera { - position: Vec3, - target: Vec3, -} - -impl Camera -{ - pub fn set_position(&mut self, position: Vec3) - { - self.position = position; - } - - #[must_use] - pub fn position(&self) -> &Vec3 - { - &self.position - } - - #[must_use] - pub fn position_mut(&mut self) -> &mut Vec3 - { - &mut self.position - } - - pub fn set_target(&mut self, target: Vec3) - { - self.target = target; - } - - #[must_use] - pub fn target(&self) -> &Vec3 - { - &self.target - } + /// Returns the current camera position. + fn position(&self) -> Vec3; - /// Returns the normalized direction to the current target. - #[must_use] - pub fn target_direction(&self) -> Vec3 - { - -(&self.position - &self.target).normalize() - } + /// Returns the position of the camera target. + fn target(&self) -> Vec3; - /// Returns the right direction (normalized) relative from where the camera is - /// currently looking. - #[must_use] - pub fn right(&self) -> Vec3 + /// Returns the global direction upwards. + /// + /// The default implementation which returns [`Vec3::UP`] should be fine in most + /// cases. + fn up(&self) -> Vec3 { - let rev_target_direction = (&self.position - &self.target).normalize(); - - Vec3::UP.cross(&rev_target_direction).normalize() - } - - /// Returns the left direction (normalized) relative from where the camera is - /// currently looking. - #[must_use] - pub fn left(&self) -> Vec3 - { - -self.right() - } - - /// Returns the up direction (normalized) relative from where the camera is currently - /// looking. - #[must_use] - pub fn up(&self) -> Vec3 - { - let rev_target_direction = (&self.position - &self.target).normalize(); - - rev_target_direction.cross(&self.right()) - } - - /// Returns the down direction (normalized) relative from where the camera is - /// currently looking. - #[must_use] - pub fn down(&self) -> Vec3 - { - -self.up() - } - - pub(crate) fn new() -> Self - { - let position = Vec3 { x: 0.0, y: 0.0, z: 3.0 }; - - Self { position, target: Vec3::default() } - } - - pub(crate) fn as_matrix(&self) -> Matrix - { - let mut matrix = Matrix::new(); - - matrix.look_at(&self.position, &self.target, &Vec3::UP); - - matrix + Vec3::UP } } -- cgit v1.2.3-18-g5258