From e938fed68e6f0ef2f2f3209f025830aa872cd7fc Mon Sep 17 00:00:00 2001 From: aiden Date: Sat, 13 May 2023 17:19:04 +0100 Subject: [PATCH] plane --- src/camera.rs | 12 +++++------- src/main.rs | 37 ++++++++++++++++++++++++++++++++++--- src/state.rs | 24 ++++++++---------------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 9ffcc0e..9dd4028 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -60,14 +60,12 @@ impl Camera { // Rotate self.yaw += Rad(input.rotate_horizontal) * input.sensitivity * dt; - self.pitch += Rad(-input.rotate_vertical) * input.sensitivity * dt; + println!("{:?}", Deg::from(self.yaw)); - // Keep the self's angle from going too high/low. - if self.pitch < -Rad(std::f32::consts::FRAC_PI_2 - 0.0001) { - self.pitch = -Rad(std::f32::consts::FRAC_PI_2 - 0.0001); - } else if self.pitch > Rad(std::f32::consts::FRAC_PI_2 - 0.0001) { - self.pitch = Rad(std::f32::consts::FRAC_PI_2 - 0.0001); - } + let pitch = (self.pitch + Rad(-input.rotate_vertical) * input.sensitivity * dt).0; + let frac = std::f32::consts::FRAC_PI_2 - 0.0001; + + self.pitch = Rad(pitch.clamp(-Rad(frac).0, Rad(frac).0)); } } diff --git a/src/main.rs b/src/main.rs index 5782378..e5f738f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ // based on https://sotrh.github.io/learn-wgpu/ -use {winit::{event as Event, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder}, std::process::ExitCode}; +use {winit::{event as Event, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder}, std::{cmp::Ordering, process::ExitCode}}; mod state; use state::State; @@ -82,16 +82,38 @@ fn real_main() -> Result<(), &'static str> { .with_title("game") .build(&(event_loop)) .map_err(|_| "failed to create window")?; + fn area(size: winit::dpi::PhysicalSize) -> u32 { + size.width * size.height + } + let video_modes = window.current_monitor().unwrap().video_modes(); + let video_mode = video_modes.max_by(|x, y| { + if area(x.size()) > area(y.size()) { + return Ordering::Greater; + } else if area(x.size()) < area(y.size()) { + return Ordering::Less; + } + if x.refresh_rate_millihertz() > y.refresh_rate_millihertz() { + return Ordering::Greater; + } else { + return Ordering::Less; + } + }).unwrap(); + window.set_fullscreen( + Some(winit::window::Fullscreen::Exclusive(video_mode)) + ); + window.set_cursor_visible(false); let mut state = State::new(window)?; - let mut input = Input::new(1.0, 8.0); + let mut input = Input::new(1.0, 0.088 * 31.4); let mut last_render_time = std::time::Instant::now(); + let mut timer = std::time::Instant::now(); + let mut frames = 0; + event_loop.run(move |event, _, flow| { *flow = ControlFlow::Poll; let window = state.window(); window.set_cursor_grab(winit::window::CursorGrabMode::Confined).expect("failed to lock cursor"); - match event { Event::Event::DeviceEvent { event: Event::DeviceEvent::MouseMotion { delta, }, @@ -134,6 +156,15 @@ fn real_main() -> Result<(), &'static str> { _ => (), }; + + frames += 1; + + let now = std::time::Instant::now(); + if now.duration_since(timer).as_millis() > 1000 { + timer = now; + println!("{:?}", frames); + frames = 0; + } } _ => (), }; diff --git a/src/state.rs b/src/state.rs index 2fe07bb..01cda01 100644 --- a/src/state.rs +++ b/src/state.rs @@ -85,27 +85,19 @@ impl State { &(wgpu::util::BufferInitDescriptor { label: Some("Vertex Buffer"), contents: bytemuck::cast_slice(&[ - Vertex { position: [0.0, 0.5, -0.5], color: [0.0, 0.0, 0.1] }, // top - Vertex { position: [-0.5, -0.5, -1.0], color: [0.0, 1.0, 0.0] }, // left - Vertex { position: [-0.5, -0.5, 0.0], color: [1.0, 0.0, 0.0] }, // right + Vertex { position: [0.0, 0.0, 0.0], color: [0.0, 1.0, 1.0] }, // top + Vertex { position: [5.0, 0.0, -5.0], color: [0.0, 1.0, 0.0] }, // left + Vertex { position: [0.0, 0.0, -5.0], color: [0.0, 1.0, 0.0] }, // right - Vertex { position: [0.0, 0.5, -0.5], color: [1.0, 0.0, 0.0] }, // top - Vertex { position: [-0.5, -0.5, 0.0], color: [0.0, 1.0, 0.0] }, // left - Vertex { position: [0.5, -0.5, 0.0], color: [0.0, 0.0, 1.0] }, // right - - Vertex { position: [0.0, 0.5, -0.5], color: [0.0, 1.0, 0.0] }, // top - Vertex { position: [0.5, -0.5, 0.0], color: [1.0, 0.0, 0.0] }, // left - Vertex { position: [0.5, -0.5, -1.0], color: [0.0, 0.0, 1.0] }, // right - - Vertex { position: [0.0, 0.5, -0.5], color: [1.0, 0.0, 0.0] }, // top - Vertex { position: [0.5, -0.5, -1.0], color: [0.0, 0.0, 1.0] }, // left - Vertex { position: [-0.5, -0.5, -1.0], color: [0.0, 1.0, 0.0] }, // right + Vertex { position: [0.0, 0.0, 0.0], color: [0.0, 1.0, 0.0] }, // top + Vertex { position: [5.0, 0.0, -0.0], color: [0.0, 1.0, 0.0] }, // left + Vertex { position: [5.0, 0.0, -5.0], color: [0.0, 1.0, 0.0] }, // right ]), usage: wgpu::BufferUsages::VERTEX, }) ); - let camera = Camera::new(size, (0.0, 0.0, 0.0), Deg(-90.0), Deg(-20.0)); + let camera = Camera::new(size, (0.25, 1.0, 0.0), Deg(0.0), Deg(0.0)); let camera_bind_group_layout = &(device.create_bind_group_layout(&(wgpu::BindGroupLayoutDescriptor { entries: &[ @@ -246,7 +238,7 @@ impl State { render_pass.set_bind_group(0, &(self.camera_bind_group), &[]); render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..)); - render_pass.draw(0..12, 0..1); + render_pass.draw(0..6, 0..1); } // submit will accept anything that implements IntoIter