This commit is contained in:
parent
09bfe6fb48
commit
fbb1493b45
5 changed files with 68 additions and 76 deletions
|
@ -19,7 +19,7 @@ use vulkano_util::context::VulkanoContext;
|
|||
use vulkano_util::renderer::VulkanoWindowRenderer;
|
||||
use vulkano_util::window::{VulkanoWindows, WindowDescriptor};
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event::{DeviceEvent, WindowEvent};
|
||||
use winit::event_loop::ActiveEventLoop;
|
||||
use winit::window::WindowId;
|
||||
|
||||
|
@ -76,6 +76,8 @@ impl ApplicationHandler for App {
|
|||
width: 800.0,
|
||||
height: 600.0,
|
||||
present_mode: PresentMode::Fifo,
|
||||
cursor_visible: false,
|
||||
cursor_locked: true,
|
||||
..Default::default()
|
||||
},
|
||||
|_| {},
|
||||
|
@ -101,6 +103,15 @@ impl ApplicationHandler for App {
|
|||
.load_scene(Box::new(MainScene::default()));
|
||||
}
|
||||
|
||||
fn device_event(
|
||||
&mut self,
|
||||
_event_loop: &ActiveEventLoop,
|
||||
_device_id: winit::event::DeviceId,
|
||||
event: winit::event::DeviceEvent,
|
||||
) {
|
||||
self.input_manager.process_device_event(&event);
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
|
||||
let renderer = Arc::get_mut(&mut self.vulkano_windows)
|
||||
.unwrap()
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
|||
use cache::{CachedElementState, CachedMovement};
|
||||
use virtual_input::VirtualInput;
|
||||
use winit::{
|
||||
event::{MouseButton, MouseScrollDelta, WindowEvent},
|
||||
event::{DeviceEvent, MouseButton, MouseScrollDelta, WindowEvent},
|
||||
keyboard::PhysicalKey,
|
||||
};
|
||||
|
||||
|
@ -39,6 +39,15 @@ impl InputManager {
|
|||
input_manager
|
||||
}
|
||||
|
||||
pub fn process_device_event(&mut self, event: &DeviceEvent) {
|
||||
match event {
|
||||
DeviceEvent::MouseMotion { delta, .. } => {
|
||||
self.mouse_position_delta += glam::Vec2::new(delta.0 as f32, delta.1 as f32);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_window_event(&mut self, event: &WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::AxisMotion { axis, value, .. } => {
|
||||
|
@ -53,10 +62,6 @@ impl InputManager {
|
|||
.update_key_binding(event.physical_key, new_key_state);
|
||||
}
|
||||
}
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
self.mouse_position_delta
|
||||
.set_value(glam::Vec2::new(position.x as f32, position.y as f32));
|
||||
}
|
||||
WindowEvent::MouseInput { button, state, .. } => {
|
||||
let new_mouse_button_state =
|
||||
self.mouse_buttons_state.set_key_state(*button, *state);
|
||||
|
|
|
@ -46,6 +46,7 @@ pub fn create_triangle_pipeline(
|
|||
) -> Result<Arc<GraphicsPipeline>, Box<dyn Error>> {
|
||||
let (vs, fs) = load_shaders(device)?;
|
||||
let vertex_input_state = Vertex2D::per_vertex().definition(&vs)?;
|
||||
log::trace!("vertex_input_state: {:#?}", vertex_input_state);
|
||||
|
||||
let stages = [
|
||||
PipelineShaderStageCreateInfo::new(vs),
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
use std::sync::Arc;
|
||||
use std::{f32::consts::PI, sync::Arc};
|
||||
|
||||
use glam::{EulerRot, Mat4, Quat, Vec3};
|
||||
use glam::{Mat4, Quat, Vec3};
|
||||
use vulkano::{
|
||||
Validated,
|
||||
buffer::{AllocateBufferError, Subbuffer},
|
||||
memory::allocator::StandardMemoryAllocator,
|
||||
};
|
||||
|
||||
use super::{mvp::MVP, transform::Transform};
|
||||
use crate::core::{input::InputManager, timer::Timer};
|
||||
|
||||
use super::mvp::MVP;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Camera {
|
||||
|
@ -28,47 +30,57 @@ impl Camera {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn rotate(&mut self, rotation: Vec3) {
|
||||
self.rotation += rotation;
|
||||
}
|
||||
pub fn update(
|
||||
&mut self,
|
||||
input_manager: &InputManager,
|
||||
timer: &Timer,
|
||||
movement_speed: f32,
|
||||
camera_sensitivity: f32,
|
||||
) {
|
||||
// Process camera rotation
|
||||
let camera_delta = camera_sensitivity * timer.delta_time();
|
||||
self.rotation += Vec3::new(
|
||||
-(input_manager.get_virtual_input_state("mouse_y") * camera_delta).to_radians(),
|
||||
(input_manager.get_virtual_input_state("mouse_x") * camera_delta).to_radians(),
|
||||
0.0,
|
||||
);
|
||||
|
||||
pub fn translate(&mut self, translation: Vec3) {
|
||||
self.position += translation;
|
||||
}
|
||||
if self.rotation.x > 90.0 {
|
||||
self.rotation = Vec3::new(90.0, self.rotation.y, 0.0);
|
||||
}
|
||||
|
||||
pub fn set_view(&mut self, view: Mat4) {
|
||||
self.view = view;
|
||||
if self.rotation.x < -90.0 {
|
||||
self.rotation = Vec3::new(-90.0, self.rotation.y, 0.0);
|
||||
}
|
||||
|
||||
let movement_delta = movement_speed * timer.delta_time();
|
||||
|
||||
let tx = input_manager.get_virtual_input_state("move_right") * movement_delta;
|
||||
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();
|
||||
}
|
||||
|
||||
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>> {
|
||||
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);
|
||||
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);
|
||||
|
||||
MVP::new(&world_matrix, &self.view, &self.projection).into_buffer(memory_allocator)
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ impl Scene for MainScene {
|
|||
Vertex2D::create_buffer(Vec::from_iter(VERTICES), render_context.memory_allocator())
|
||||
.unwrap();
|
||||
|
||||
let mut camera = Camera::new(
|
||||
let camera = Camera::new(
|
||||
Mat4::look_at_rh(
|
||||
Vec3::new(0.3, 0.3, 1.0),
|
||||
Vec3::new(0.0, 0.0, 0.0),
|
||||
|
@ -74,7 +74,6 @@ 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(),
|
||||
|
@ -113,43 +112,7 @@ impl Scene for MainScene {
|
|||
timer: &Timer,
|
||||
) {
|
||||
let state = self.state.as_mut().unwrap();
|
||||
|
||||
state.speed += input_manager.get_virtual_input_state("mouse_wheel") * 10.0;
|
||||
|
||||
let speed = state.speed * timer.delta_time();
|
||||
|
||||
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,
|
||||
));
|
||||
|
||||
if state.camera.get_rotation().x > 89.0 {
|
||||
state
|
||||
.camera
|
||||
.set_rotation(Vec3::new(89.0, state.camera.get_rotation().y, 0.0));
|
||||
}
|
||||
|
||||
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.update(input_manager, timer, state.speed, 10.0);
|
||||
|
||||
state.camera.set_projection(Mat4::perspective_rh_gl(
|
||||
std::f32::consts::FRAC_PI_2,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue