Compare commits

...

2 Commits

Author SHA1 Message Date
7179f20e67 add zoom 2023-08-31 22:48:17 -07:00
e25a2d7a7a use slope fov 2023-08-31 22:47:55 -07:00

View File

@ -1,5 +1,5 @@
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use std::{borrow::Cow, f32::consts,time::Instant}; use std::{borrow::Cow, time::Instant};
use wgpu::{util::DeviceExt, AstcBlock, AstcChannel}; use wgpu::{util::DeviceExt, AstcBlock, AstcChannel};
const IMAGE_SIZE: u32 = 128; const IMAGE_SIZE: u32 = 128;
@ -25,6 +25,7 @@ struct Camera {
friction: f32, friction: f32,
screen_size: (u32, u32), screen_size: (u32, u32),
offset: glam::Vec3, offset: glam::Vec3,
fov: f32,
yaw: f32, yaw: f32,
pitch: f32, pitch: f32,
controls: u32, controls: u32,
@ -40,7 +41,7 @@ const CONTROL_MOVELEFT:u32 = 0b00001000;
const CONTROL_MOVEUP:u32 = 0b00010000; const CONTROL_MOVEUP:u32 = 0b00010000;
const CONTROL_MOVEDOWN:u32 = 0b00100000; const CONTROL_MOVEDOWN:u32 = 0b00100000;
const CONTROL_JUMP:u32 = 0b01000000; const CONTROL_JUMP:u32 = 0b01000000;
//const CONTROL_ZOOM:u32 = 0b10000000; const CONTROL_ZOOM:u32 = 0b10000000;
const FORWARD_DIR:glam::Vec3 = glam::Vec3::new(0.0,0.0,-1.0); const FORWARD_DIR:glam::Vec3 = glam::Vec3::new(0.0,0.0,-1.0);
const RIGHT_DIR:glam::Vec3 = glam::Vec3::new(1.0,0.0,0.0); const RIGHT_DIR:glam::Vec3 = glam::Vec3::new(1.0,0.0,0.0);
@ -70,10 +71,27 @@ fn get_control_dir(controls: u32) -> glam::Vec3{
return control_dir return control_dir
} }
#[inline]
fn perspective_rh(fov_y_slope: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> glam::Mat4 {
//glam_assert!(z_near > 0.0 && z_far > 0.0);
let r = z_far / (z_near - z_far);
glam::Mat4::from_cols(
glam::Vec4::new(1.0/(fov_y_slope * aspect_ratio), 0.0, 0.0, 0.0),
glam::Vec4::new(0.0, 1.0/fov_y_slope, 0.0, 0.0),
glam::Vec4::new(0.0, 0.0, r, -1.0),
glam::Vec4::new(0.0, 0.0, r * z_near, 0.0),
)
}
impl Camera { impl Camera {
fn to_uniform_data(&self) -> [f32; 16 * 3 + 4] { fn to_uniform_data(&self) -> [f32; 16 * 3 + 4] {
let aspect = self.screen_size.0 as f32 / self.screen_size.1 as f32; let aspect = self.screen_size.0 as f32 / self.screen_size.1 as f32;
let proj = glam::Mat4::perspective_rh(consts::FRAC_PI_2, aspect, 1.0, 200.0); let fov = if self.controls&CONTROL_ZOOM==0 {
self.fov
}else{
self.fov/5.0
};
let proj = perspective_rh(fov, aspect, 1.0, 200.0);
let view = (glam::Mat4::from_translation(self.pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32)).inverse(); let view = (glam::Mat4::from_translation(self.pos+self.offset) * glam::Mat4::from_euler(glam::EulerRot::YXZ, self.yaw, self.pitch, 0f32)).inverse();
let proj_inv = proj.inverse(); let proj_inv = proj.inverse();
@ -217,6 +235,7 @@ impl strafe_client::framework::Example for Skybox {
friction: 90.0, friction: 90.0,
screen_size: (config.width, config.height), screen_size: (config.width, config.height),
offset: glam::Vec3::new(0.0,4.5,0.0), offset: glam::Vec3::new(0.0,4.5,0.0),
fov: 1.0, //fov_slope = tan(fov_y/2)
pitch: 0.0, pitch: 0.0,
yaw: 0.0, yaw: 0.0,
mv: 2.7, mv: 2.7,
@ -482,6 +501,10 @@ impl strafe_client::framework::Example for Skybox {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_JUMP, winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_JUMP,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_JUMP, winit::event::ElementState::Released => self.camera.controls&=!CONTROL_JUMP,
} }
(k,winit::event::VirtualKeyCode::Z) => match k {
winit::event::ElementState::Pressed => self.camera.controls|=CONTROL_ZOOM,
winit::event::ElementState::Released => self.camera.controls&=!CONTROL_ZOOM,
}
_ => (), _ => (),
} }
} }