summaryrefslogtreecommitdiff
path: root/src/gfx/camera.rs
diff options
context:
space:
mode:
authorLizzy Fleckenstein <eliasfleckenstein@web.de>2023-05-16 15:20:39 +0200
committerLizzy Fleckenstein <eliasfleckenstein@web.de>2023-05-16 15:20:39 +0200
commitef8c75b9dc47142527fc1741d75cf34c66c3ac9d (patch)
tree836e541b516717f9c00d06e659d4cc7b238193b2 /src/gfx/camera.rs
parent0fc89aa3ec523d3d06546220bae5393232274e13 (diff)
downloadmt_client-ef8c75b9dc47142527fc1741d75cf34c66c3ac9d.tar.xz
Split state.rs and add debug menu
Diffstat (limited to 'src/gfx/camera.rs')
-rw-r--r--src/gfx/camera.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/gfx/camera.rs b/src/gfx/camera.rs
new file mode 100644
index 0000000..874c9fc
--- /dev/null
+++ b/src/gfx/camera.rs
@@ -0,0 +1,67 @@
+use super::{gpu::Gpu, util::MatrixUniform};
+use cgmath::{prelude::*, Deg, Matrix4, Rad};
+use collision::Frustum;
+use fps_camera::{FirstPerson, FirstPersonSettings};
+use std::time::Duration;
+
+pub struct Camera {
+ pub fov: Rad<f32>,
+ pub view: Matrix4<f32>,
+ pub proj: Matrix4<f32>,
+ pub frustum: Frustum<f32>,
+ pub first_person: FirstPerson,
+ pub uniform: MatrixUniform,
+ pub layout: wgpu::BindGroupLayout,
+}
+
+impl Camera {
+ pub fn new(gpu: &Gpu) -> Self {
+ let first_person = FirstPerson::new(
+ [0.0, 0.0, 0.0],
+ FirstPersonSettings {
+ speed_horizontal: 10.0,
+ speed_vertical: 10.0,
+ mouse_sensitivity_horizontal: 1.0,
+ mouse_sensitivity_vertical: 1.0,
+ },
+ );
+
+ let layout = MatrixUniform::layout(&gpu.device, "camera");
+ let uniform = MatrixUniform::new(&gpu.device, &layout, Matrix4::identity(), "camera", true);
+
+ Self {
+ fov: Deg(90.0).into(),
+ proj: Matrix4::identity(),
+ view: Matrix4::identity(),
+ frustum: Frustum::from_matrix4(Matrix4::identity()).unwrap(),
+ first_person,
+ uniform,
+ layout,
+ }
+ }
+
+ pub fn update(&mut self, gpu: &Gpu, dt: Duration) {
+ self.first_person.yaw += Rad::from(Deg(180.0)).0;
+ self.first_person.yaw *= -1.0;
+
+ let cam = self.first_person.camera(dt.as_secs_f32());
+
+ self.first_person.yaw *= -1.0;
+ self.first_person.yaw -= Rad::from(Deg(180.0)).0;
+
+ self.first_person.position = cam.position;
+
+ self.view = Matrix4::from(cam.orthogonal());
+ self.uniform.set(&gpu.queue, self.proj * self.view);
+ }
+
+ pub fn resize(&mut self, size: winit::dpi::PhysicalSize<u32>) {
+ self.proj = cgmath::perspective(
+ self.fov,
+ size.width as f32 / size.height as f32,
+ 0.1,
+ 100000.0,
+ );
+ self.frustum = Frustum::from_matrix4(self.proj).unwrap();
+ }
+}