some mouse stuff i don't remember
This commit is contained in:
@@ -17,6 +17,7 @@ serde_derive = "1.0.114"
|
|||||||
toml = "0.5.6"
|
toml = "0.5.6"
|
||||||
gilrs = "0.7.4"
|
gilrs = "0.7.4"
|
||||||
gltf = "0.15.2"
|
gltf = "0.15.2"
|
||||||
|
mgf = "1.4.0"
|
||||||
|
|
||||||
[profile.dev.package."*"]
|
[profile.dev.package."*"]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
@@ -43,6 +43,10 @@ scan_code = 63
|
|||||||
name = "quickload"
|
name = "quickload"
|
||||||
scan_code = 64
|
scan_code = 64
|
||||||
|
|
||||||
|
[[button]]
|
||||||
|
name = "select"
|
||||||
|
mouse = "left"
|
||||||
|
|
||||||
[[axis]]
|
[[axis]]
|
||||||
name = "move_forward"
|
name = "move_forward"
|
||||||
positive_button = "button_forward"
|
positive_button = "button_forward"
|
||||||
|
|||||||
@@ -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};
|
use crate::game::player::PlayerMovementMode::{FirstPerson, Flying};
|
||||||
|
|
||||||
@@ -47,6 +47,15 @@ impl Camera {
|
|||||||
pub fn transform_vector(&self, vec: Vector3<f32>) -> Vector3<f32> {
|
pub fn transform_vector(&self, vec: Vector3<f32>) -> Vector3<f32> {
|
||||||
self.rotation.invert().rotate_vector(vec)
|
self.rotation.invert().rotate_vector(vec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn viewport_pos_to_ray(&self, viewport_pos: [f64; 2], viewport_dimensions: [u32; 2]) -> Option<Vector3<f32>> {
|
||||||
|
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)]
|
#[derive(PartialEq)]
|
||||||
@@ -79,10 +88,7 @@ impl Player {
|
|||||||
|
|
||||||
impl Updatable for Player {
|
impl Updatable for Player {
|
||||||
fn update(&mut self, delta_time: f32, input: &InputState, renderer: &mut VulkanRenderer) {
|
fn update(&mut self, delta_time: f32, input: &InputState, renderer: &mut VulkanRenderer) {
|
||||||
// Rotation
|
// Edit mode
|
||||||
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;
|
|
||||||
|
|
||||||
if input.button_just_pressed("toggle_edit") {
|
if input.button_just_pressed("toggle_edit") {
|
||||||
if self.movement_mode == FirstPerson {
|
if self.movement_mode == FirstPerson {
|
||||||
self.movement_mode = Flying;
|
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
|
// Movement
|
||||||
if self.movement_mode == FirstPerson {
|
if self.movement_mode == FirstPerson {
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ pub struct InputState {
|
|||||||
pub virtual_axes: HashMap<String, VirtualAxis>,
|
pub virtual_axes: HashMap<String, VirtualAxis>,
|
||||||
pub mouse_delta_x: f64,
|
pub mouse_delta_x: f64,
|
||||||
pub mouse_delta_y: f64,
|
pub mouse_delta_y: f64,
|
||||||
|
pub mouse_position: [f64; 2],
|
||||||
input_events: HashSet<DigitalInputEvent>,
|
input_events: HashSet<DigitalInputEvent>,
|
||||||
pressed_scan_codes: HashSet<ScanCode>,
|
pressed_scan_codes: HashSet<ScanCode>,
|
||||||
pressed_mouse_buttons: HashSet<MouseButton>,
|
pressed_mouse_buttons: HashSet<MouseButton>,
|
||||||
@@ -106,7 +107,7 @@ impl InputState {
|
|||||||
let mut state = InputState { virtual_buttons: HashMap::new(), virtual_axes: HashMap::new(),
|
let mut state = InputState { virtual_buttons: HashMap::new(), virtual_axes: HashMap::new(),
|
||||||
input_events: HashSet::new(), pressed_scan_codes: HashSet::new(),
|
input_events: HashSet::new(), pressed_scan_codes: HashSet::new(),
|
||||||
pressed_mouse_buttons: HashSet::new(), analog_wheel_state: 0.0,
|
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() };
|
controller_input: Gilrs::new().unwrap() };
|
||||||
|
|
||||||
config.button.iter().for_each(|bn| {
|
config.button.iter().for_each(|bn| {
|
||||||
@@ -229,7 +230,10 @@ impl InputState {
|
|||||||
}
|
}
|
||||||
self.mouse_delta_x += *delta_x;
|
self.mouse_delta_x += *delta_x;
|
||||||
self.mouse_delta_y += *delta_y;
|
self.mouse_delta_y += *delta_y;
|
||||||
}
|
},
|
||||||
|
Event::WindowEvent { event: WindowEvent::CursorMoved { position, .. }, .. } => {
|
||||||
|
self.mouse_position = [position.x, position.y];
|
||||||
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ pub struct Mesh {
|
|||||||
pub vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
|
pub vertex_buffer: Arc<CpuAccessibleBuffer<[Vertex]>>,
|
||||||
pub index_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
|
pub index_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
|
||||||
pub original_path: String,
|
pub original_path: String,
|
||||||
|
pub collision_mesh: mgf::Mesh,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -416,9 +417,18 @@ impl VulkanRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload_mesh(self: &mut Self, mesh: CPUMesh, original_path: String) -> usize {
|
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 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();
|
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
|
self.game_data.meshes.len() - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user