Add upwards movement, make camera pitch not shit itself

This commit is contained in:
Sijmen 2021-05-29 01:05:04 +02:00
parent eee0287d7f
commit 5d1aa6ce96
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE
3 changed files with 37 additions and 24 deletions

View file

@ -1,7 +1,7 @@
use cgmath::InnerSpace;
use cgmath::{Matrix4, Point3, Rad, Vector3};
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
pub const OPENGL_TO_WGPU_MATRIX: Matrix4<f32> = Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
@ -9,17 +9,13 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
);
pub struct Camera {
pub position: cgmath::Point3<f32>,
pub yaw: cgmath::Rad<f32>,
pub pitch: cgmath::Rad<f32>,
pub position: Point3<f32>,
pub yaw: Rad<f32>,
pub pitch: Rad<f32>,
}
impl Camera {
pub fn new(
position: cgmath::Point3<f32>,
yaw: cgmath::Rad<f32>,
pitch: cgmath::Rad<f32>,
) -> Self {
pub fn new(position: Point3<f32>, yaw: Rad<f32>, pitch: Rad<f32>) -> Self {
Self {
position,
yaw,
@ -27,25 +23,26 @@ impl Camera {
}
}
pub fn calculate_matrix(&self) -> cgmath::Matrix4<f32> {
cgmath::Matrix4::look_to_rh(
self.position,
cgmath::Vector3::new(self.yaw.0.cos(), self.pitch.0.sin(), self.yaw.0.sin())
.normalize(),
cgmath::Vector3::unit_y(),
)
pub fn calculate_matrix(&self) -> Matrix4<f32> {
let direction = Vector3::new(
self.yaw.0.cos() * self.pitch.0.cos(),
self.pitch.0.sin(),
self.yaw.0.sin() * self.pitch.0.cos(),
);
Matrix4::look_to_rh(self.position, direction, Vector3::unit_y())
}
}
pub struct Projection {
pub aspect_ratio: f32,
pub fov_y: cgmath::Rad<f32>,
pub fov_y: Rad<f32>,
pub z_near: f32,
pub z_far: f32,
}
impl Projection {
pub fn new<Fov: Into<cgmath::Rad<f32>>>(
pub fn new<Fov: Into<Rad<f32>>>(
width: u32,
height: u32,
fov_y: Fov,
@ -64,7 +61,7 @@ impl Projection {
self.aspect_ratio = width as f32 / height as f32;
}
pub fn calculate_matrix(&self) -> cgmath::Matrix4<f32> {
pub fn calculate_matrix(&self) -> Matrix4<f32> {
OPENGL_TO_WGPU_MATRIX
* cgmath::perspective(self.fov_y, self.aspect_ratio, self.z_near, self.z_far)
}

View file

@ -59,14 +59,15 @@ fn main(model: VertexInput, instance: InstanceInput) -> VertexOutput {
}
[[group(0), binding(0)]]
var t_diffuse: texture_2d<f32>;
var texture_diffuse: texture_2d<f32>;
[[group(0), binding(1)]]
var s_diffuse: sampler;
var sampler_diffuse: sampler;
[[stage(fragment)]]
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
let object_color: vec4<f32> = textureSample(t_diffuse, s_diffuse, in.texture_coordinates);
let object_color: vec4<f32> =
textureSample(texture_diffuse, sampler_diffuse, in.texture_coordinates);
let ambient_strength = 0.2;
let ambient_color = light.color * ambient_strength;

View file

@ -39,6 +39,7 @@ pub struct State {
light_bind_group: wgpu::BindGroup,
right_speed: f32,
forward_speed: f32,
up_speed: f32,
camera: Camera,
uniforms: Uniforms,
projection: Projection,
@ -428,6 +429,7 @@ impl State {
right_speed: 0.0,
forward_speed: 0.0,
up_speed: 0.0,
}
}
@ -462,6 +464,8 @@ impl State {
VirtualKeyCode::S => self.forward_speed -= amount,
VirtualKeyCode::A => self.right_speed -= amount,
VirtualKeyCode::D => self.right_speed += amount,
VirtualKeyCode::LControl => self.up_speed -= amount,
VirtualKeyCode::Space => self.up_speed += amount,
_ => (),
}
}
@ -470,6 +474,12 @@ impl State {
if self.mouse_grabbed {
self.camera.yaw += cgmath::Rad(dx as f32 * 0.005);
self.camera.pitch -= cgmath::Rad(dy as f32 * 0.005);
if self.camera.pitch < cgmath::Rad::from(cgmath::Deg(-80.0)) {
self.camera.pitch = cgmath::Rad::from(cgmath::Deg(-80.0));
} else if self.camera.pitch > cgmath::Rad::from(cgmath::Deg(89.0)) {
self.camera.pitch = cgmath::Rad::from(cgmath::Deg(89.0));
}
}
}
@ -490,11 +500,16 @@ impl State {
// Move forward/backward and left/right
let (yaw_sin, yaw_cos) = self.camera.yaw.0.sin_cos();
let forward = cgmath::Vector3::new(yaw_cos, 0.0, yaw_sin).normalize();
let right = cgmath::Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize();
self.camera.position += forward * self.forward_speed * 6.0 * dt_secs;
let right = cgmath::Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize();
self.camera.position += right * self.right_speed * 6.0 * dt_secs;
let up = cgmath::Vector3::new(0.0, 1.0, 0.0).normalize();
self.camera.position += up * self.up_speed * 6.0 * dt_secs;
self.uniforms
.update_view_projection(&self.camera, &self.projection);
self.queue.write_buffer(