camera: fix camera movement
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 8m11s

This commit is contained in:
Florian RICHER 2025-05-29 13:54:00 +02:00
parent f835941432
commit 998aa68da1
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
10 changed files with 83 additions and 103 deletions

View file

@ -65,6 +65,7 @@ impl ApplicationHandler for App {
renderer.swapchain_format(),
GuiConfig {
is_overlay: true,
allow_srgb_render_target: true,
..Default::default()
},
)
@ -132,7 +133,9 @@ impl ApplicationHandler for App {
self.scene_manager.load_scene_if_not_loaded(&scene_context);
if let Some(scene) = self.scene_manager.current_scene_mut() {
scene.update(&scene_context);
if let Err(e) = scene.update(&scene_context) {
log::error!("Error updating scene: {}", e);
}
let acquire_future = renderer.acquire(None, |_| {}).unwrap();
let acquire_future = scene.render(

View file

@ -1,6 +1,6 @@
use std::{f32::consts::PI, sync::Arc};
use std::{f32::consts::FRAC_PI_2, sync::Arc};
use glam::{Mat4, Quat, Vec3};
use glam::{Mat4, Vec3, Vec4};
use vulkano::{
Validated,
buffer::{AllocateBufferError, Subbuffer},
@ -13,7 +13,6 @@ use super::mvp::MVP;
#[derive(Default)]
pub struct Camera {
view: Mat4,
projection: Mat4,
position: Vec3,
@ -21,9 +20,8 @@ pub struct Camera {
}
impl Camera {
pub fn new(view: Mat4, projection: Mat4) -> Self {
pub fn new(projection: Mat4) -> Self {
Self {
view,
projection,
position: Vec3::ZERO,
rotation: Vec3::ZERO,
@ -45,43 +43,57 @@ impl Camera {
0.0,
);
if self.rotation.x > 90.0 {
self.rotation = Vec3::new(90.0, self.rotation.y, 0.0);
if self.rotation.x > FRAC_PI_2 {
self.rotation = Vec3::new(FRAC_PI_2, self.rotation.y, 0.0);
}
if self.rotation.x < -90.0 {
self.rotation = Vec3::new(-90.0, self.rotation.y, 0.0);
if self.rotation.x < -FRAC_PI_2 {
self.rotation = Vec3::new(-FRAC_PI_2, self.rotation.y, 0.0);
}
let movement_delta = movement_speed * timer.delta_time();
let (yaw_sin, yaw_cos) = self.rotation.y.sin_cos();
let forward = Vec3::new(yaw_cos, 0.0, yaw_sin).normalize();
let right = Vec3::new(-yaw_sin, 0.0, yaw_cos).normalize();
let tx = input_manager.get_virtual_input_state("move_right") * movement_delta;
self.position += tx * right;
let tz = input_manager.get_virtual_input_state("move_forward") * movement_delta;
self.position.x += tx * (self.rotation.y).cos();
self.position.z += tx * (self.rotation.y).sin();
self.position.x += tz * (self.rotation.y + PI / 2.0).cos();
self.position.z += tz * (self.rotation.y + PI / 2.0).sin();
self.position += tz * forward;
}
pub fn set_projection(&mut self, projection: Mat4) {
self.projection = projection;
}
pub fn get_position(&self) -> Vec3 {
self.position
}
pub fn get_rotation(&self) -> Vec3 {
self.rotation
}
pub fn create_buffer(
&self,
memory_allocator: &Arc<StandardMemoryAllocator>,
) -> Result<Subbuffer<[MVP]>, Validated<AllocateBufferError>> {
let mut world_matrix = Mat4::IDENTITY;
world_matrix *= Mat4::from_quat(Quat::from_euler(
glam::EulerRot::XYX,
self.rotation.x,
self.rotation.y,
self.rotation.z,
));
world_matrix *= Mat4::from_translation(self.position);
let (sin_pitch, cos_pitch) = self.rotation.x.sin_cos();
let (sin_yaw, cos_yaw) = self.rotation.y.sin_cos();
MVP::new(&world_matrix, &self.view, &self.projection).into_buffer(memory_allocator)
let view_matrix = Mat4::look_to_rh(
self.position,
Vec3::new(cos_pitch * cos_yaw, sin_pitch, cos_pitch * sin_yaw).normalize(),
Vec3::Y,
);
MVP {
model: Mat4::IDENTITY.to_cols_array_2d(),
view: view_matrix.to_cols_array_2d(),
projection: self.projection.to_cols_array_2d(),
}
.into_buffer(memory_allocator)
}
}

View file

@ -10,20 +10,12 @@ use vulkano::memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, Standar
#[derive(BufferContents, Clone, Copy)]
#[repr(C)]
pub struct MVP {
world: [[f32; 4]; 4],
view: [[f32; 4]; 4],
projection: [[f32; 4]; 4],
pub model: [[f32; 4]; 4],
pub view: [[f32; 4]; 4],
pub projection: [[f32; 4]; 4],
}
impl MVP {
pub fn new(world: &Mat4, view: &Mat4, projection: &Mat4) -> Self {
Self {
world: world.to_cols_array_2d(),
view: view.to_cols_array_2d(),
projection: projection.to_cols_array_2d(),
}
}
pub fn into_buffer(
self,
memory_allocator: &Arc<StandardMemoryAllocator>,

View file

@ -1,9 +1,4 @@
use std::sync::Arc;
use vulkano::Validated;
use vulkano::buffer::{
AllocateBufferError, Buffer, BufferContents, BufferCreateInfo, BufferUsage, Subbuffer,
};
use vulkano::memory::allocator::{AllocationCreateInfo, MemoryTypeFilter, StandardMemoryAllocator};
use vulkano::buffer::BufferContents;
use vulkano::pipeline::graphics::vertex_input::Vertex;
#[derive(BufferContents, Vertex)]
@ -15,24 +10,3 @@ pub struct Vertex2D {
#[format(R32G32_SFLOAT)]
pub uv: [f32; 2],
}
impl Vertex2D {
pub fn create_buffer(
vertices: Vec<Vertex2D>,
memory_allocator: &Arc<StandardMemoryAllocator>,
) -> Result<Subbuffer<[Vertex2D]>, Validated<AllocateBufferError>> {
Buffer::from_iter(
memory_allocator.clone(),
BufferCreateInfo {
usage: BufferUsage::VERTEX_BUFFER,
..Default::default()
},
AllocationCreateInfo {
memory_type_filter: MemoryTypeFilter::PREFER_DEVICE
| MemoryTypeFilter::HOST_SEQUENTIAL_WRITE,
..Default::default()
},
vertices,
)
}
}

View file

@ -14,7 +14,9 @@ impl SceneManager {
pub fn load_scene_if_not_loaded(&mut self, scene_context: &SceneContext) {
if let Some(current_scene) = self.current_scene.as_mut() {
if !current_scene.loaded() {
current_scene.load(scene_context);
if let Err(e) = current_scene.load(scene_context) {
log::error!("Error loading scene: {}", e);
}
}
}
}

View file

@ -11,8 +11,8 @@ pub use manager::SceneManager;
pub trait Scene {
fn loaded(&self) -> bool;
fn load(&mut self, scene_context: &SceneContext);
fn update(&mut self, scene_context: &SceneContext);
fn load(&mut self, scene_context: &SceneContext) -> Result<(), Box<dyn Error>>;
fn update(&mut self, scene_context: &SceneContext) -> Result<(), Box<dyn Error>>;
fn render(
&mut self,
image_view: &Arc<ImageView>,