Some better code organization for xy movement and camera reset key
parent
c2f7effc71
commit
ceeeb6e268
|
@ -4,7 +4,7 @@ use ashpd::desktop::screenshot::Screenshot;
|
|||
use pollster::FutureExt;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::{MouseScrollDelta, WindowEvent},
|
||||
event::WindowEvent,
|
||||
keyboard::{Key, NamedKey},
|
||||
window::{Fullscreen, Window},
|
||||
};
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use cgmath::Point3;
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
#[rustfmt::skip]
|
||||
|
@ -8,6 +9,8 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
|
|||
0.0, 0.0, 0.0, 1.0,
|
||||
);
|
||||
|
||||
pub const DEFAULT_CAMERA_ZOOM: f32 = 1.7;
|
||||
|
||||
pub struct Camera {
|
||||
pub eye: cgmath::Point3<f32>,
|
||||
pub target: cgmath::Point3<f32>,
|
||||
|
@ -28,7 +31,7 @@ pub struct CameraInfo {
|
|||
impl Camera {
|
||||
pub fn new(config: &wgpu::SurfaceConfiguration, device: &wgpu::Device) -> (Self, CameraInfo) {
|
||||
let camera = Self {
|
||||
eye: (0.0, 0.0, 1.7).into(),
|
||||
eye: (0.0, 0.0, DEFAULT_CAMERA_ZOOM).into(),
|
||||
target: (0.0, 0.0, 0.0).into(),
|
||||
up: cgmath::Vector3::unit_y(),
|
||||
aspect: config.width as f32 / config.height as f32,
|
||||
|
@ -84,6 +87,20 @@ impl Camera {
|
|||
let proj = cgmath::perspective(cgmath::Deg(self.fovy), 1.0, self.znear, self.zfar);
|
||||
OPENGL_TO_WGPU_MATRIX * proj * view
|
||||
}
|
||||
|
||||
pub fn reset_camera(&mut self) {
|
||||
self.eye = Point3 {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
z: DEFAULT_CAMERA_ZOOM,
|
||||
};
|
||||
|
||||
self.target = Point3 {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
z: 0.0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
use std::sync::Arc;
|
||||
use cgmath::Point3;
|
||||
use winit::event::{MouseButton, MouseScrollDelta, WindowEvent};
|
||||
|
||||
use winit::{
|
||||
dpi::PhysicalSize,
|
||||
event::{MouseButton, MouseScrollDelta, WindowEvent},
|
||||
keyboard::{KeyCode, PhysicalKey},
|
||||
};
|
||||
|
||||
use crate::camera::Camera;
|
||||
use crate::camera::{Camera, DEFAULT_CAMERA_ZOOM};
|
||||
|
||||
const MAX_ZOOM_LEVEL: f32 = 15.0;
|
||||
|
||||
|
@ -45,32 +40,34 @@ impl CameraController {
|
|||
false
|
||||
}
|
||||
WindowEvent::CursorMoved { position, .. } => {
|
||||
let mut x = 0.0;
|
||||
let mut y = 0.0;
|
||||
|
||||
if !self.is_moving {
|
||||
self.cursor_start = (position.x as f32, position.y as f32);
|
||||
}
|
||||
|
||||
x = match position.x {
|
||||
self.movement = match self.is_moving {
|
||||
true => {
|
||||
let x = -self.cursor_start.0 + position.x as f32;
|
||||
let x = x * 0.000001;
|
||||
let y = self.cursor_start.1 - position.y as f32;
|
||||
let y = y * 0.000001;
|
||||
(x, y)
|
||||
}
|
||||
false => {
|
||||
let x = match position.x {
|
||||
x if x < 100.0 => -0.0005,
|
||||
x if x > window_size.0 - 100.0 => 0.0005,
|
||||
_ => 0.0,
|
||||
};
|
||||
|
||||
y = match position.y {
|
||||
let y = match position.y {
|
||||
y if y < 100.0 => 0.0005,
|
||||
y if y > window_size.1 - 100.0 => -0.0005,
|
||||
_ => 0.0,
|
||||
};
|
||||
}
|
||||
else {
|
||||
x = -self.cursor_start.0 + position.x as f32;
|
||||
x *= 0.000001;
|
||||
y = self.cursor_start.1 - position.y as f32;
|
||||
y *= 0.000001;
|
||||
}
|
||||
|
||||
self.movement = (x, y);
|
||||
(x, y)
|
||||
}
|
||||
};
|
||||
|
||||
true
|
||||
}
|
||||
|
@ -110,4 +107,9 @@ impl CameraController {
|
|||
|
||||
self.forward = 0.0;
|
||||
}
|
||||
|
||||
pub fn reset_camera(&mut self, camera: &mut Camera) {
|
||||
camera.reset_camera();
|
||||
self.zoom_level = 1.0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,5 +36,5 @@ var s_diffuse: sampler;
|
|||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
return textureSample(t_diffuse, s_diffuse, in.tex_coords);;
|
||||
return textureSample(t_diffuse, s_diffuse, in.tex_coords);
|
||||
}
|
||||
|
|
23
src/state.rs
23
src/state.rs
|
@ -2,7 +2,12 @@ use std::sync::Arc;
|
|||
|
||||
use pollster::FutureExt;
|
||||
use wgpu::util::DeviceExt;
|
||||
use winit::window::Window;
|
||||
use winit::{
|
||||
event::{ElementState, WindowEvent},
|
||||
keyboard::{Key, KeyCode, NamedKey},
|
||||
platform::modifier_supplement::KeyEventExtModifierSupplement,
|
||||
window::Window,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
camera::{Camera, CameraInfo},
|
||||
|
@ -205,8 +210,24 @@ impl<'a> State<'a> {
|
|||
|
||||
pub fn input(&mut self, event: &winit::event::WindowEvent) -> bool {
|
||||
self.camera_controller.process_events(self.wsize, event);
|
||||
match event {
|
||||
WindowEvent::KeyboardInput {
|
||||
event: key_event, ..
|
||||
} => {
|
||||
if key_event.state == ElementState::Pressed {
|
||||
let key = &key_event.logical_key;
|
||||
if let Key::Character(char) = key {
|
||||
if char == "r" {
|
||||
self.camera_controller.reset_camera(&mut self.camera);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
self.camera_controller.update_camera(&mut self.camera);
|
||||
|
|
Loading…
Reference in New Issue