diff options
author | HampusM <hampus@hampusmat.com> | 2023-11-27 22:57:34 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-11-27 22:57:34 +0100 |
commit | c9a08e77dded7dd6c1013f14f19316f755b6a5e8 (patch) | |
tree | d39e9afd019444ecd5cd336e980cad4759233c35 /src/main.rs | |
parent | 7360e3086d5f3365813e8169bd17bcf347fa30c7 (diff) |
chore: make compile again
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 79 |
1 files changed, 71 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs index e7486b2..321009e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ use std::error::Error; use std::path::Path; +use engine::camera::Camera; use engine::lighting::LightSourceBuilder; use engine::material::Builder as MaterialBuilder; use engine::object::Id as ObjectId; use engine::texture::{Id as TextureId, Texture}; use engine::vector::{Vec2, Vec3}; -use engine::{Engine, Key, WindowSize}; +use engine::{Engine, Key, WindowSettingsBuilder, WindowSize}; use tracing::Level; use tracing_subscriber::FmtSubscriber; @@ -24,7 +25,10 @@ fn main() -> Result<(), Box<dyn Error>> tracing::subscriber::set_global_default(subscriber)?; - let mut engine = Engine::new(&WINDOW_SIZE, "Yaaay lmao")?; + let mut engine = Engine::<LookAtCamera>::new( + &WindowSettingsBuilder::new().build(WINDOW_SIZE, "Yaaay lmao"), + LookAtCamera::new(), + )?; let texture = Texture::open(Path::new("vent.png"))?; @@ -66,13 +70,13 @@ fn main() -> Result<(), Box<dyn Error>> if engine.is_key_pressed(Key::W).unwrap() { let cam_target_direction = engine.camera().target_direction().clone(); - *engine.camera_mut().position_mut() += + engine.camera_mut().position += cam_target_direction * cam_speed * delta_time.as_secs_f32(); } if engine.is_key_pressed(Key::S).unwrap() { let rev_cam_target_direction = -engine.camera().target_direction().clone(); - *engine.camera_mut().position_mut() += + engine.camera_mut().position += rev_cam_target_direction * cam_speed * delta_time.as_secs_f32(); } if engine.is_key_pressed(Key::A).unwrap() { @@ -83,7 +87,7 @@ fn main() -> Result<(), Box<dyn Error>> let cam_speed_dist_adj = cam_speed * (engine.camera().position() - engine.camera().target()).length(); - *engine.camera_mut().position_mut() += + engine.camera_mut().position += cam_left * cam_speed_dist_adj * delta_time.as_secs_f32(); } if engine.is_key_pressed(Key::D).unwrap() { @@ -94,7 +98,7 @@ fn main() -> Result<(), Box<dyn Error>> let cam_speed_dist_adj = cam_speed * (engine.camera().position() - engine.camera().target()).length(); - *engine.camera_mut().position_mut() += + engine.camera_mut().position += cam_right * cam_speed_dist_adj * delta_time.as_secs_f32(); } if engine.is_key_pressed(Key::K).unwrap() { @@ -105,7 +109,7 @@ fn main() -> Result<(), Box<dyn Error>> let cam_speed_dist_adj = cam_speed * (engine.camera().position() - engine.camera().target()).length(); - *engine.camera_mut().position_mut() += + engine.camera_mut().position += cam_up * cam_speed_dist_adj * delta_time.as_secs_f32(); } if engine.is_key_pressed(Key::J).unwrap() { @@ -116,7 +120,7 @@ fn main() -> Result<(), Box<dyn Error>> let cam_speed_dist_adj = cam_speed * (engine.camera().position() - engine.camera().target()).length(); - *engine.camera_mut().position_mut() += + engine.camera_mut().position += cam_up * cam_speed_dist_adj * delta_time.as_secs_f32(); } @@ -141,3 +145,62 @@ fn main() -> Result<(), Box<dyn Error>> Ok(()) } + +#[derive(Debug)] +pub struct LookAtCamera +{ + position: Vec3<f32>, + target: Vec3<f32>, +} + +impl LookAtCamera +{ + fn new() -> Self + { + let position = Vec3 { x: 0.0, y: 0.0, z: 3.0 }; + + Self { position, target: Vec3::default() } + } + + fn target_direction(&self) -> Vec3<f32> + { + -(&self.position - &self.target).normalize() + } + + fn right(&self) -> Vec3<f32> + { + let rev_target_direction = (&self.position - &self.target).normalize(); + + Vec3::UP.cross(&rev_target_direction).normalize() + } + + fn left(&self) -> Vec3<f32> + { + -self.right() + } + + fn up(&self) -> Vec3<f32> + { + let rev_target_direction = (&self.position - &self.target).normalize(); + + rev_target_direction.cross(&self.right()) + } + + fn down(&self) -> Vec3<f32> + { + -self.up() + } +} + +impl Camera for LookAtCamera +{ + fn position(&self) -> Vec3<f32> + { + self.position.clone() + } + + fn target(&self) -> Vec3<f32> + { + self.target.clone() + } +} |