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

This commit is contained in:
Florian RICHER 2025-05-27 23:31:23 +02:00
parent a0fce9c08e
commit 09bfe6fb48
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
2 changed files with 79 additions and 34 deletions

View file

@ -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<StandardMemoryAllocator>,
) -> Result<Subbuffer<[MVP]>, Validated<AllocateBufferError>> {
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)
}
}

View file

@ -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,