From 94432b0c0d9218488a656ea0135019766f53ddcd Mon Sep 17 00:00:00 2001 From: zack Date: Sat, 28 Dec 2024 22:21:15 -0500 Subject: [PATCH] fix builds, improve movement --- Cargo.lock | 1 + crates/vk-rs/Cargo.toml | 4 +- crates/vk-rs/src/main.rs | 71 +++++++++++++++++++++++++++--------- crates/vk-rs/src/renderer.rs | 9 +++-- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d0b52d..cc05c08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -875,6 +875,7 @@ dependencies = [ [[package]] name = "gpu-profiler" version = "0.1.0" +source = "git+https://github.com/zackartz/gpu-profiler#cb5b9d74a9ea25d1f27e4be7949403296e351370" dependencies = [ "ash", "once_cell", diff --git a/crates/vk-rs/Cargo.toml b/crates/vk-rs/Cargo.toml index b23dad3..2691d6d 100644 --- a/crates/vk-rs/Cargo.toml +++ b/crates/vk-rs/Cargo.toml @@ -19,7 +19,9 @@ ash-window = "0.12.0" shaders-shared = { path = "../shaders-shared" } puffin = { git = "https://github.com/EmbarkStudios/puffin", rev = "5ac4e54164ee89bd68c29f288c2b5613afc2c929" } puffin_egui = { git = "https://github.com/EmbarkStudios/puffin", rev = "5ac4e54164ee89bd68c29f288c2b5613afc2c929" } -gpu-profiler = { path = "../../../gpu-profiler", features = ["use-ash"] } +gpu-profiler = { git = "https://github.com/zackartz/gpu-profiler", features = [ + "use-ash", +] } [build-dependencies] spirv-builder.workspace = true diff --git a/crates/vk-rs/src/main.rs b/crates/vk-rs/src/main.rs index dd37e92..5a55e99 100644 --- a/crates/vk-rs/src/main.rs +++ b/crates/vk-rs/src/main.rs @@ -45,6 +45,7 @@ struct Game { camera_position: Vec3, camera_yaw: f32, camera_pitch: f32, + camera_fov: f32, right_mouse_pressed: bool, last_mouse_pos: Option<(f32, f32)>, @@ -105,6 +106,12 @@ impl App for Game { ui.add(egui::DragValue::new(&mut self.camera_position.z).speed(0.1)); }); ui.separator(); + ui.label("FOV"); + ui.add(egui::widgets::Slider::new( + &mut self.camera_fov, + 10.0..=150.0, + )); + ui.separator(); if ui.button("Show Profiler").clicked() { self.show_profiler = !self.show_profiler; } @@ -114,12 +121,19 @@ impl App for Game { if !ctx.wants_keyboard_input() { let movement_speed = 0.1; - let forward = Vec3::new(self.camera_yaw.sin(), 0.0, self.camera_yaw.cos()).normalize(); + // Calculate forward direction using yaw + let forward = Vec3::new( + self.camera_yaw.sin(), + self.camera_pitch.sin(), + self.camera_yaw.cos(), + ) + .normalize(); + // Calculate right direction let right = Vec3::new( - (self.camera_yaw + 90.0_f32.to_radians()).sin(), + (self.camera_yaw + std::f32::consts::FRAC_PI_2).sin(), 0.0, - (self.camera_yaw + 90.0_f32.to_radians()).cos(), + (self.camera_yaw + std::f32::consts::FRAC_PI_2).cos(), ) .normalize(); @@ -141,6 +155,7 @@ impl App for Game { // Handle mouse input for camera rotation let is_right_mouse_down = ctx.input(|i| i.pointer.secondary_down()); + let is_middle_mouse_down = ctx.input(|i| i.pointer.middle_down()); let hover_pos = ctx.input(|i| i.pointer.hover_pos()); // Set cursor visibility based on right mouse button @@ -154,22 +169,40 @@ impl App for Game { self.right_mouse_pressed = is_right_mouse_down; - if self.right_mouse_pressed { - if let Some(pos) = hover_pos { - if let Some((last_x, last_y)) = self.last_mouse_pos { - let delta_x = pos.x - last_x; - let delta_y = pos.y - last_y; + match (self.right_mouse_pressed, is_middle_mouse_down) { + (true, false) => { + if let Some(pos) = hover_pos { + if let Some((last_x, last_y)) = self.last_mouse_pos { + let delta_x = pos.x - last_x; + let delta_y = pos.y - last_y; - // Update camera rotation - let rotation_speed = 0.002; - self.camera_yaw -= delta_x * rotation_speed; - self.camera_pitch = (self.camera_pitch + delta_y * rotation_speed) - .clamp(-89.0_f32.to_radians(), 89.0_f32.to_radians()); + // Update camera rotation + let rotation_speed = 0.002; + self.camera_yaw -= delta_x * rotation_speed; + self.camera_pitch = (self.camera_pitch + delta_y * rotation_speed) + .clamp(-89.0_f32.to_radians(), 89.0_f32.to_radians()); + } + self.last_mouse_pos = Some((pos.x, pos.y)); } - self.last_mouse_pos = Some((pos.x, pos.y)); } - } else { - self.last_mouse_pos = None; + (false, true) => { + if let Some(pos) = hover_pos { + if let Some((last_x, last_y)) = self.last_mouse_pos { + let delta_x = pos.x - last_x; + let delta_y = pos.y - last_y; + + let move_speed = 0.005; + + self.camera_position.x -= delta_x * move_speed; + self.camera_position.y += delta_y * move_speed; + } + + self.last_mouse_pos = Some((pos.x, pos.y)) + } + } + _ => { + self.last_mouse_pos = None; + } } match self.theme { @@ -186,9 +219,10 @@ impl App for Game { let camera_position = self.camera_position; let camera_yaw = self.camera_yaw; let camera_pitch = self.camera_pitch; + let camera_fov = self.camera_fov; move |size, egui_cmd| { let mut renderer = renderer.inner.lock().unwrap(); - renderer.update_camera(camera_position, camera_yaw, camera_pitch); + renderer.update_camera(camera_position, camera_yaw, camera_pitch, camera_fov); renderer.render(size.width, size.height, egui_cmd, rotate_y) } })) @@ -521,7 +555,7 @@ impl AppCreator>> for MyAppCreator { } let ( physical_device, - physical_device_properties, + _physical_device_properties, _physical_device_memory_properties, queue_family_index, ) = Self::create_physical_device(&instance, &surface_loader, surface, &req_ext); @@ -591,6 +625,7 @@ impl AppCreator>> for MyAppCreator { camera_position: Vec3::new(0.0, 0.0, -5.0), camera_pitch: 0., camera_yaw: 0., + camera_fov: 45., last_mouse_pos: None, right_mouse_pressed: false, last_fps_update: std::time::Instant::now(), diff --git a/crates/vk-rs/src/renderer.rs b/crates/vk-rs/src/renderer.rs index a9d7cd4..732ca02 100644 --- a/crates/vk-rs/src/renderer.rs +++ b/crates/vk-rs/src/renderer.rs @@ -3,7 +3,6 @@ use ash::{ vk::{self, Buffer}, Device, }; -use cgmath::num_traits::Float; use egui_ash::EguiCommand; use glam::{Mat4, Vec3}; use gpu_allocator::vulkan::{Allocation, Allocator}; @@ -102,6 +101,7 @@ pub struct RendererInner { frame_counter: u32, camera_position: Vec3, + camera_fov: f32, camera_yaw: f32, camera_pitch: f32, accumulation_reset_needed: bool, @@ -1082,18 +1082,21 @@ impl RendererInner { camera_position: Vec3::new(0.0, 0.0, -5.0), camera_yaw: 0., camera_pitch: 0., + camera_fov: 45., accumulation_reset_needed: true, } } - pub fn update_camera(&mut self, new_position: Vec3, yaw: f32, pitch: f32) { + pub fn update_camera(&mut self, new_position: Vec3, yaw: f32, pitch: f32, fov: f32) { if (new_position - self.camera_position).length() > 0.0001 || (yaw - self.camera_yaw).abs() > 0.001 || (pitch - self.camera_pitch).abs() > 0.001 + || (fov - self.camera_fov).abs() > 0.001 { self.camera_position = new_position; self.camera_yaw = yaw; self.camera_pitch = pitch; + self.camera_fov = fov; self.accumulation_reset_needed = true; } } @@ -1172,7 +1175,7 @@ impl RendererInner { model: Mat4::from_rotation_y(rotate_y.to_radians()), view, proj: Mat4::perspective_rh( - 45.0_f32.to_radians(), + self.camera_fov.to_radians(), width as f32 / height as f32, 0.1, 10.0,