From d33bceeab822d3ca8ee6da6f8842ac9e97790526 Mon Sep 17 00:00:00 2001 From: Asuro Date: Thu, 18 Feb 2021 11:47:31 +0100 Subject: [PATCH] some mouse stuff i don't remember --- Cargo.toml | 1 + config/input.toml | 4 ++++ src/game/player.rs | 31 ++++++++++++++++++++++++++----- src/input.rs | 8 ++++++-- src/vulkan/mod.rs | 12 +++++++++++- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dc761cb..f719773 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ serde_derive = "1.0.114" toml = "0.5.6" gilrs = "0.7.4" gltf = "0.15.2" +mgf = "1.4.0" [profile.dev.package."*"] opt-level = 3 \ No newline at end of file diff --git a/config/input.toml b/config/input.toml index 51f156d..c2867d4 100644 --- a/config/input.toml +++ b/config/input.toml @@ -43,6 +43,10 @@ scan_code = 63 name = "quickload" scan_code = 64 +[[button]] +name = "select" +mouse = "left" + [[axis]] name = "move_forward" positive_button = "button_forward" diff --git a/src/game/player.rs b/src/game/player.rs index 5627407..1248457 100644 --- a/src/game/player.rs +++ b/src/game/player.rs @@ -1,4 +1,4 @@ -use cgmath::{Deg, InnerSpace, Matrix4, One, Quaternion, Rad, Rotation, Rotation3, SquareMatrix, vec3, Vector3}; +use cgmath::{Deg, InnerSpace, Matrix4, One, Quaternion, Rad, Rotation, Rotation3, SquareMatrix, vec3, Vector3, Transform}; use crate::game::player::PlayerMovementMode::{FirstPerson, Flying}; @@ -47,6 +47,15 @@ impl Camera { pub fn transform_vector(&self, vec: Vector3) -> Vector3 { self.rotation.invert().rotate_vector(vec) } + + pub fn viewport_pos_to_ray(&self, viewport_pos: [f64; 2], viewport_dimensions: [u32; 2]) -> Option> { + let normalized_x = (2. * viewport_pos[0]) / viewport_dimensions[0] as f64 - 1.; + let normalized_y = (2. * viewport_pos[1]) / viewport_dimensions[1] as f64 - 1.; + + let camera_ray = self.proj.inverse_transform_vector(vec3(normalized_x as f32, normalized_y as f32, -1.))?; + let world_ray = self.view.inverse_transform_vector(camera_ray)?.normalize(); + Some(world_ray) + } } #[derive(PartialEq)] @@ -79,10 +88,7 @@ impl Player { impl Updatable for Player { fn update(&mut self, delta_time: f32, input: &InputState, renderer: &mut VulkanRenderer) { - // Rotation - self.camera.rotation = self.camera.rotation * Quaternion::from_angle_y(Deg(input.get_axis("look_horizontal") * delta_time * self.look_sensitivity)); - self.camera.rotation = Quaternion::from_angle_x(Deg(input.get_axis("look_vertical") * delta_time * self.look_sensitivity)) * self.camera.rotation; - + // Edit mode if input.button_just_pressed("toggle_edit") { if self.movement_mode == FirstPerson { self.movement_mode = Flying; @@ -91,6 +97,21 @@ impl Updatable for Player { } } + if self.movement_mode == Flying && input.button_just_pressed("select") { + let ray_direction = self.camera.viewport_pos_to_ray(input.mouse_position, renderer.game_data.dimensions).unwrap(); + let ray_origin: [f32; 3] = self.camera.position.into(); + let ray = mgf::Ray::new(ray_origin.into(), ray_direction); + println!("Ray: {:?}", ray); + let collision_mesh = &renderer.game_data.meshes[renderer.game_data.game_objects[0].mesh_index].collision_mesh; + collision_mesh.bvh.raytrace(&ray, |v, is| { + println!("v: {:?}, is: {:?}", v, is); + }); + } + + // Rotation + self.camera.rotation = self.camera.rotation * Quaternion::from_angle_y(Deg(input.get_axis("look_horizontal") * delta_time * self.look_sensitivity)); + self.camera.rotation = Quaternion::from_angle_x(Deg(input.get_axis("look_vertical") * delta_time * self.look_sensitivity)) * self.camera.rotation; + // Movement if self.movement_mode == FirstPerson { diff --git a/src/input.rs b/src/input.rs index 2260a34..4558faf 100644 --- a/src/input.rs +++ b/src/input.rs @@ -71,6 +71,7 @@ pub struct InputState { pub virtual_axes: HashMap, pub mouse_delta_x: f64, pub mouse_delta_y: f64, + pub mouse_position: [f64; 2], input_events: HashSet, pressed_scan_codes: HashSet, pressed_mouse_buttons: HashSet, @@ -106,7 +107,7 @@ impl InputState { let mut state = InputState { virtual_buttons: HashMap::new(), virtual_axes: HashMap::new(), input_events: HashSet::new(), pressed_scan_codes: HashSet::new(), pressed_mouse_buttons: HashSet::new(), analog_wheel_state: 0.0, - config: config.config, mouse_delta_x: 0.0, mouse_delta_y: 0.0, log_config, + config: config.config, mouse_delta_x: 0.0, mouse_delta_y: 0.0, mouse_position: [0., 0.], log_config, controller_input: Gilrs::new().unwrap() }; config.button.iter().for_each(|bn| { @@ -229,7 +230,10 @@ impl InputState { } self.mouse_delta_x += *delta_x; self.mouse_delta_y += *delta_y; - } + }, + Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, .. } => { + self.mouse_position = [position.x, position.y]; + }, _ => {} } } diff --git a/src/vulkan/mod.rs b/src/vulkan/mod.rs index 8474df1..eeb9c56 100644 --- a/src/vulkan/mod.rs +++ b/src/vulkan/mod.rs @@ -71,6 +71,7 @@ pub struct Mesh { pub vertex_buffer: Arc>, pub index_buffer: Arc>, pub original_path: String, + pub collision_mesh: mgf::Mesh, } #[derive(Debug, Clone)] @@ -416,9 +417,18 @@ impl VulkanRenderer { } pub fn upload_mesh(self: &mut Self, mesh: CPUMesh, original_path: String) -> usize { + let mut collision_mesh = mgf::Mesh::new(); + mesh.vertices.iter().for_each(|v| { + collision_mesh.push_vert(v.position.into()); + }); // TODO: convert vert pos to world space + for i in (0..mesh.indices.len()).step_by(3) { + collision_mesh.push_face((mesh.indices[i] as usize, mesh.indices[i + 1] as usize, mesh.indices[i + 2] as usize)); + } + let vertex_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::vertex_buffer(), false, mesh.vertices.into_iter()).unwrap(); let index_buffer = CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::index_buffer(), false, mesh.indices.into_iter()).unwrap(); - self.game_data.meshes.push(Mesh { vertex_buffer, index_buffer, original_path }); + + self.game_data.meshes.push(Mesh { vertex_buffer, index_buffer, original_path, collision_mesh }); self.game_data.meshes.len() - 1 }