From 09bfe6fb488fc35ef70a1293af94d02b44f1ee7c Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 27 May 2025 23:31:23 +0200 Subject: [PATCH] camera: Try fix camera --- src/core/render/primitives/camera.rs | 45 ++++++++++++++---- src/game/main_scene.rs | 68 ++++++++++++++++++---------- 2 files changed, 79 insertions(+), 34 deletions(-) diff --git a/src/core/render/primitives/camera.rs b/src/core/render/primitives/camera.rs index efd61e5..16ae79b 100644 --- a/src/core/render/primitives/camera.rs +++ b/src/core/render/primitives/camera.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use glam::Mat4; +use glam::{EulerRot, Mat4, Quat, Vec3}; use vulkano::{ Validated, buffer::{AllocateBufferError, Subbuffer}, @@ -14,7 +14,8 @@ pub struct Camera { view: Mat4, projection: Mat4, - transform: Transform, + position: Vec3, + rotation: Vec3, } impl Camera { @@ -22,27 +23,53 @@ impl Camera { Self { view, projection, - transform: Transform::default(), + position: Vec3::ZERO, + rotation: Vec3::ZERO, } } - pub fn get_transform(&self) -> &Transform { - &self.transform + pub fn rotate(&mut self, rotation: Vec3) { + self.rotation += rotation; } - pub fn get_transform_mut(&mut self) -> &mut Transform { - &mut self.transform + pub fn translate(&mut self, translation: Vec3) { + self.position += translation; + } + + pub fn set_view(&mut self, view: Mat4) { + self.view = view; } pub fn set_projection(&mut self, projection: Mat4) { self.projection = projection; } + pub fn get_rotation(&self) -> Vec3 { + self.rotation + } + + pub fn set_rotation(&mut self, rotation: Vec3) { + self.rotation = rotation; + } + + pub fn get_position(&self) -> Vec3 { + self.position + } + + pub fn set_position(&mut self, position: Vec3) { + self.position = position; + } + pub fn create_buffer( &self, memory_allocator: &Arc, ) -> Result, Validated> { - MVP::new(&self.transform.get_mat4(), &self.view, &self.projection) - .into_buffer(memory_allocator) + let mut world_matrix = Mat4::IDENTITY; + world_matrix *= Mat4::from_quat(Quat::from_rotation_z(self.rotation.z)); + world_matrix *= Mat4::from_quat(Quat::from_rotation_y(self.rotation.y)); + world_matrix *= Mat4::from_quat(Quat::from_rotation_x(self.rotation.x)); + world_matrix *= Mat4::from_translation(-self.position); + + MVP::new(&world_matrix, &self.view, &self.projection).into_buffer(memory_allocator) } } diff --git a/src/game/main_scene.rs b/src/game/main_scene.rs index c5a56a0..3b0ed93 100644 --- a/src/game/main_scene.rs +++ b/src/game/main_scene.rs @@ -6,7 +6,7 @@ use crate::core::render::render_context::RenderContext; use crate::core::render::texture::Texture; use crate::core::scene::Scene; use crate::core::timer::Timer; -use glam::{Mat4, Quat, Vec3}; +use glam::{Mat4, Vec3}; use std::sync::Arc; use vulkano::buffer::Subbuffer; use vulkano::command_buffer::{ @@ -18,20 +18,20 @@ use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint}; const VERTICES: [Vertex2D; 4] = [ Vertex2D { - position: [-0.5, -0.5], + position: [0.0, 0.0], uv: [0.0, 0.0], }, Vertex2D { - position: [-0.5, 0.5], - uv: [0.0, 1.0], + position: [0.0, 5.0], + uv: [0.0, 0.5], }, Vertex2D { - position: [0.5, -0.5], + position: [10.0, 0.0], uv: [1.0, 0.0], }, Vertex2D { - position: [0.5, 0.5], - uv: [1.0, 1.0], + position: [10.0, 5.0], + uv: [1.0, 0.5], }, ]; @@ -40,6 +40,7 @@ pub struct MainSceneState { vertex_buffer: Subbuffer<[Vertex2D]>, camera: Camera, texture: Texture, + speed: f32, } #[derive(Default)] @@ -60,7 +61,7 @@ impl Scene for MainScene { Vertex2D::create_buffer(Vec::from_iter(VERTICES), render_context.memory_allocator()) .unwrap(); - let camera = Camera::new( + let mut camera = Camera::new( Mat4::look_at_rh( Vec3::new(0.3, 0.3, 1.0), Vec3::new(0.0, 0.0, 0.0), @@ -73,6 +74,7 @@ impl Scene for MainScene { 100.0, ), ); + camera.set_position(Vec3::new(-10.0, 0.0, -10.0)); let mut uploads = AutoCommandBufferBuilder::primary( render_context.command_buffer_allocator().clone(), @@ -100,6 +102,7 @@ impl Scene for MainScene { vertex_buffer, camera, texture, + speed: 50.0, }); } @@ -111,27 +114,42 @@ impl Scene for MainScene { ) { let state = self.state.as_mut().unwrap(); - let speed = 50.0 * timer.delta_time(); + state.speed += input_manager.get_virtual_input_state("mouse_wheel") * 10.0; - let mut rot = Quat::default(); - rot *= Quat::from_rotation_y( - input_manager.get_virtual_input_state("mouse_x") * speed.to_radians(), - ); - rot *= Quat::from_rotation_x( - input_manager.get_virtual_input_state("mouse_y") * speed.to_radians(), - ); - state.camera.get_transform_mut().rotate(rot); + let speed = state.speed * timer.delta_time(); - let translation_x = - input_manager.get_virtual_input_state("move_right") * timer.delta_time() * speed; + state.camera.rotate(Vec3::new( + (input_manager.get_virtual_input_state("mouse_y") * 50.0 * timer.delta_time()) + .to_radians(), + (input_manager.get_virtual_input_state("mouse_x") * 50.0 * timer.delta_time()) + .to_radians(), + 0.0, + )); - let translation_z = - input_manager.get_virtual_input_state("move_forward") * timer.delta_time() * speed; + if state.camera.get_rotation().x > 89.0 { + state + .camera + .set_rotation(Vec3::new(89.0, state.camera.get_rotation().y, 0.0)); + } - state - .camera - .get_transform_mut() - .translate(Vec3::new(translation_x, 0.0, translation_z)); + if state.camera.get_rotation().x < -89.0 { + state + .camera + .set_rotation(Vec3::new(-89.0, state.camera.get_rotation().y, 0.0)); + } + + let rotation = state.camera.get_rotation(); + let mut translation = Vec3::ZERO; + + let tx = input_manager.get_virtual_input_state("move_right") * timer.delta_time() * speed; + translation.x += tx * (rotation.y).to_radians().cos(); + translation.z += tx * (rotation.y).to_radians().sin(); + + let ty = input_manager.get_virtual_input_state("move_forward") * timer.delta_time() * speed; + translation.x += ty * (rotation.y + 90.0).to_radians().cos(); + translation.z += ty * (rotation.y + 90.0).to_radians().sin(); + + state.camera.translate(translation); state.camera.set_projection(Mat4::perspective_rh_gl( std::f32::consts::FRAC_PI_2,