camera: Try fix camera
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 8m5s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 8m5s
This commit is contained in:
parent
a0fce9c08e
commit
09bfe6fb48
2 changed files with 79 additions and 34 deletions
|
@ -1,6 +1,6 @@
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use glam::Mat4;
|
use glam::{EulerRot, Mat4, Quat, Vec3};
|
||||||
use vulkano::{
|
use vulkano::{
|
||||||
Validated,
|
Validated,
|
||||||
buffer::{AllocateBufferError, Subbuffer},
|
buffer::{AllocateBufferError, Subbuffer},
|
||||||
|
@ -14,7 +14,8 @@ pub struct Camera {
|
||||||
view: Mat4,
|
view: Mat4,
|
||||||
projection: Mat4,
|
projection: Mat4,
|
||||||
|
|
||||||
transform: Transform,
|
position: Vec3,
|
||||||
|
rotation: Vec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Camera {
|
impl Camera {
|
||||||
|
@ -22,27 +23,53 @@ impl Camera {
|
||||||
Self {
|
Self {
|
||||||
view,
|
view,
|
||||||
projection,
|
projection,
|
||||||
transform: Transform::default(),
|
position: Vec3::ZERO,
|
||||||
|
rotation: Vec3::ZERO,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_transform(&self) -> &Transform {
|
pub fn rotate(&mut self, rotation: Vec3) {
|
||||||
&self.transform
|
self.rotation += rotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_transform_mut(&mut self) -> &mut Transform {
|
pub fn translate(&mut self, translation: Vec3) {
|
||||||
&mut self.transform
|
self.position += translation;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_view(&mut self, view: Mat4) {
|
||||||
|
self.view = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_projection(&mut self, projection: Mat4) {
|
pub fn set_projection(&mut self, projection: Mat4) {
|
||||||
self.projection = projection;
|
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(
|
pub fn create_buffer(
|
||||||
&self,
|
&self,
|
||||||
memory_allocator: &Arc<StandardMemoryAllocator>,
|
memory_allocator: &Arc<StandardMemoryAllocator>,
|
||||||
) -> Result<Subbuffer<[MVP]>, Validated<AllocateBufferError>> {
|
) -> Result<Subbuffer<[MVP]>, Validated<AllocateBufferError>> {
|
||||||
MVP::new(&self.transform.get_mat4(), &self.view, &self.projection)
|
let mut world_matrix = Mat4::IDENTITY;
|
||||||
.into_buffer(memory_allocator)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ use crate::core::render::render_context::RenderContext;
|
||||||
use crate::core::render::texture::Texture;
|
use crate::core::render::texture::Texture;
|
||||||
use crate::core::scene::Scene;
|
use crate::core::scene::Scene;
|
||||||
use crate::core::timer::Timer;
|
use crate::core::timer::Timer;
|
||||||
use glam::{Mat4, Quat, Vec3};
|
use glam::{Mat4, Vec3};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use vulkano::buffer::Subbuffer;
|
use vulkano::buffer::Subbuffer;
|
||||||
use vulkano::command_buffer::{
|
use vulkano::command_buffer::{
|
||||||
|
@ -18,20 +18,20 @@ use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint};
|
||||||
|
|
||||||
const VERTICES: [Vertex2D; 4] = [
|
const VERTICES: [Vertex2D; 4] = [
|
||||||
Vertex2D {
|
Vertex2D {
|
||||||
position: [-0.5, -0.5],
|
position: [0.0, 0.0],
|
||||||
uv: [0.0, 0.0],
|
uv: [0.0, 0.0],
|
||||||
},
|
},
|
||||||
Vertex2D {
|
Vertex2D {
|
||||||
position: [-0.5, 0.5],
|
position: [0.0, 5.0],
|
||||||
uv: [0.0, 1.0],
|
uv: [0.0, 0.5],
|
||||||
},
|
},
|
||||||
Vertex2D {
|
Vertex2D {
|
||||||
position: [0.5, -0.5],
|
position: [10.0, 0.0],
|
||||||
uv: [1.0, 0.0],
|
uv: [1.0, 0.0],
|
||||||
},
|
},
|
||||||
Vertex2D {
|
Vertex2D {
|
||||||
position: [0.5, 0.5],
|
position: [10.0, 5.0],
|
||||||
uv: [1.0, 1.0],
|
uv: [1.0, 0.5],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -40,6 +40,7 @@ pub struct MainSceneState {
|
||||||
vertex_buffer: Subbuffer<[Vertex2D]>,
|
vertex_buffer: Subbuffer<[Vertex2D]>,
|
||||||
camera: Camera,
|
camera: Camera,
|
||||||
texture: Texture,
|
texture: Texture,
|
||||||
|
speed: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -60,7 +61,7 @@ impl Scene for MainScene {
|
||||||
Vertex2D::create_buffer(Vec::from_iter(VERTICES), render_context.memory_allocator())
|
Vertex2D::create_buffer(Vec::from_iter(VERTICES), render_context.memory_allocator())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let camera = Camera::new(
|
let mut camera = Camera::new(
|
||||||
Mat4::look_at_rh(
|
Mat4::look_at_rh(
|
||||||
Vec3::new(0.3, 0.3, 1.0),
|
Vec3::new(0.3, 0.3, 1.0),
|
||||||
Vec3::new(0.0, 0.0, 0.0),
|
Vec3::new(0.0, 0.0, 0.0),
|
||||||
|
@ -73,6 +74,7 @@ impl Scene for MainScene {
|
||||||
100.0,
|
100.0,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
camera.set_position(Vec3::new(-10.0, 0.0, -10.0));
|
||||||
|
|
||||||
let mut uploads = AutoCommandBufferBuilder::primary(
|
let mut uploads = AutoCommandBufferBuilder::primary(
|
||||||
render_context.command_buffer_allocator().clone(),
|
render_context.command_buffer_allocator().clone(),
|
||||||
|
@ -100,6 +102,7 @@ impl Scene for MainScene {
|
||||||
vertex_buffer,
|
vertex_buffer,
|
||||||
camera,
|
camera,
|
||||||
texture,
|
texture,
|
||||||
|
speed: 50.0,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,27 +114,42 @@ impl Scene for MainScene {
|
||||||
) {
|
) {
|
||||||
let state = self.state.as_mut().unwrap();
|
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();
|
let speed = state.speed * timer.delta_time();
|
||||||
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 translation_x =
|
state.camera.rotate(Vec3::new(
|
||||||
input_manager.get_virtual_input_state("move_right") * timer.delta_time() * speed;
|
(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 =
|
if state.camera.get_rotation().x > 89.0 {
|
||||||
input_manager.get_virtual_input_state("move_forward") * timer.delta_time() * speed;
|
state
|
||||||
|
.camera
|
||||||
|
.set_rotation(Vec3::new(89.0, state.camera.get_rotation().y, 0.0));
|
||||||
|
}
|
||||||
|
|
||||||
state
|
if state.camera.get_rotation().x < -89.0 {
|
||||||
.camera
|
state
|
||||||
.get_transform_mut()
|
.camera
|
||||||
.translate(Vec3::new(translation_x, 0.0, translation_z));
|
.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(
|
state.camera.set_projection(Mat4::perspective_rh_gl(
|
||||||
std::f32::consts::FRAC_PI_2,
|
std::f32::consts::FRAC_PI_2,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue