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 pollster::FutureExt;
|
||||||
use winit::{
|
use winit::{
|
||||||
application::ApplicationHandler,
|
application::ApplicationHandler,
|
||||||
event::{MouseScrollDelta, WindowEvent},
|
event::WindowEvent,
|
||||||
keyboard::{Key, NamedKey},
|
keyboard::{Key, NamedKey},
|
||||||
window::{Fullscreen, Window},
|
window::{Fullscreen, Window},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use cgmath::Point3;
|
||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[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,
|
0.0, 0.0, 0.0, 1.0,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
pub const DEFAULT_CAMERA_ZOOM: f32 = 1.7;
|
||||||
|
|
||||||
pub struct Camera {
|
pub struct Camera {
|
||||||
pub eye: cgmath::Point3<f32>,
|
pub eye: cgmath::Point3<f32>,
|
||||||
pub target: cgmath::Point3<f32>,
|
pub target: cgmath::Point3<f32>,
|
||||||
|
@ -28,7 +31,7 @@ pub struct CameraInfo {
|
||||||
impl Camera {
|
impl Camera {
|
||||||
pub fn new(config: &wgpu::SurfaceConfiguration, device: &wgpu::Device) -> (Self, CameraInfo) {
|
pub fn new(config: &wgpu::SurfaceConfiguration, device: &wgpu::Device) -> (Self, CameraInfo) {
|
||||||
let camera = Self {
|
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(),
|
target: (0.0, 0.0, 0.0).into(),
|
||||||
up: cgmath::Vector3::unit_y(),
|
up: cgmath::Vector3::unit_y(),
|
||||||
aspect: config.width as f32 / config.height as f32,
|
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);
|
let proj = cgmath::perspective(cgmath::Deg(self.fovy), 1.0, self.znear, self.zfar);
|
||||||
OPENGL_TO_WGPU_MATRIX * proj * view
|
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)]
|
#[repr(C)]
|
||||||
|
|
|
@ -1,12 +1,7 @@
|
||||||
use std::sync::Arc;
|
use cgmath::Point3;
|
||||||
|
use winit::event::{MouseButton, MouseScrollDelta, WindowEvent};
|
||||||
|
|
||||||
use winit::{
|
use crate::camera::{Camera, DEFAULT_CAMERA_ZOOM};
|
||||||
dpi::PhysicalSize,
|
|
||||||
event::{MouseButton, MouseScrollDelta, WindowEvent},
|
|
||||||
keyboard::{KeyCode, PhysicalKey},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::camera::Camera;
|
|
||||||
|
|
||||||
const MAX_ZOOM_LEVEL: f32 = 15.0;
|
const MAX_ZOOM_LEVEL: f32 = 15.0;
|
||||||
|
|
||||||
|
@ -45,32 +40,34 @@ impl CameraController {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
WindowEvent::CursorMoved { position, .. } => {
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
let mut x = 0.0;
|
|
||||||
let mut y = 0.0;
|
|
||||||
|
|
||||||
if !self.is_moving {
|
if !self.is_moving {
|
||||||
self.cursor_start = (position.x as f32, position.y as f32);
|
self.cursor_start = (position.x as f32, position.y as f32);
|
||||||
|
|
||||||
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 {
|
|
||||||
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);
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
let y = match position.y {
|
||||||
|
y if y < 100.0 => 0.0005,
|
||||||
|
y if y > window_size.1 - 100.0 => -0.0005,
|
||||||
|
_ => 0.0,
|
||||||
|
};
|
||||||
|
|
||||||
|
(x, y)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
@ -110,4 +107,9 @@ impl CameraController {
|
||||||
|
|
||||||
self.forward = 0.0;
|
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
|
@fragment
|
||||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
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);
|
||||||
}
|
}
|
||||||
|
|
25
src/state.rs
25
src/state.rs
|
@ -2,7 +2,12 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use pollster::FutureExt;
|
use pollster::FutureExt;
|
||||||
use wgpu::util::DeviceExt;
|
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::{
|
use crate::{
|
||||||
camera::{Camera, CameraInfo},
|
camera::{Camera, CameraInfo},
|
||||||
|
@ -205,7 +210,23 @@ impl<'a> State<'a> {
|
||||||
|
|
||||||
pub fn input(&mut self, event: &winit::event::WindowEvent) -> bool {
|
pub fn input(&mut self, event: &winit::event::WindowEvent) -> bool {
|
||||||
self.camera_controller.process_events(self.wsize, event);
|
self.camera_controller.process_events(self.wsize, event);
|
||||||
false
|
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) {
|
pub fn update(&mut self) {
|
||||||
|
|
Loading…
Reference in New Issue