From fd535654369b31e1283a609ba8e8fcf0df42100b Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 25 Oct 2023 21:15:41 +0200 Subject: feat(engine): make camera look at a target position --- engine/src/camera.rs | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) (limited to 'engine/src/camera.rs') diff --git a/engine/src/camera.rs b/engine/src/camera.rs index b98f3f4..1546960 100644 --- a/engine/src/camera.rs +++ b/engine/src/camera.rs @@ -1,34 +1,57 @@ -use crate::transform::Transform; +use crate::matrix::Matrix; use crate::vector::Vec3; #[derive(Debug)] pub struct Camera { - transform: Transform, + position: Vec3, + target: Vec3, } impl Camera { - pub fn translate(&mut self, translation: Vec3) + pub fn set_position(&mut self, position: Vec3) { - self.transform.set_translation(translation); + self.position = position; } - pub(crate) fn new() -> Self + #[must_use] + pub fn position(&self) -> &Vec3 + { + &self.position + } + + pub fn set_target(&mut self, target: Vec3) + { + self.target = target; + } + + #[must_use] + pub fn target(&self) -> &Vec3 { - let mut transform = Transform::new(); + &self.target + } - transform.set_translation(Vec3 { + pub(crate) fn new() -> Self + { + let position = Vec3 { x: 0.0, y: 0.0, - z: -3.0, - }); + z: 3.0, + }; - Self { transform } + Self { + position, + target: Vec3::default(), + } } - pub(crate) fn transform(&self) -> &Transform + pub(crate) fn as_matrix(&self) -> Matrix { - &self.transform + let mut matrix = Matrix::new(); + + matrix.look_at(&self.position, &self.target, &Vec3::UP); + + matrix } } -- cgit v1.2.3-18-g5258