From def2ebb3df0e4aee106e8ec242c994c7bcdd429f Mon Sep 17 00:00:00 2001 From: Micha White Date: Sun, 25 Sep 2022 22:55:35 -0400 Subject: Code simplification + fixed bug --- src/camera.rs | 43 +++++++++++++++++++++++++++---------------- src/renderer.rs | 2 +- 2 files changed, 28 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/camera.rs b/src/camera.rs index 2eb1730..b594b0f 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,4 +1,4 @@ -use cgmath::{Matrix4, Vector3}; +use cgmath::{Matrix4, Vector2}; #[derive(Clone, Copy, Debug, PartialEq)] pub struct Camera { @@ -46,10 +46,20 @@ impl Camera { /// Set the position of the camera pub fn set_position(&mut self, x: f32, y: f32) { + debug_assert!( + x <= 1000.0 && y <= 1000.0, + "The x-position of the camera is ({}, {}), which is too large. \ + Please keep both the x and y positions below 1000 units. \ + Otherwise, everything will look crazy. \ + For an explanation, see https://www.youtube.com/watch?v=Q2OGwnRik24", + x, + y + ); + self.position = (x, y); } - /// Set the aspect ratio of the camera + /// Set the zoom of the camera pub fn set_zoom(&mut self, zoom: f32) { self.zoom = zoom; } @@ -60,36 +70,37 @@ impl Camera { } #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_matrix(&mut self) -> [[f32; 4]; 4] { - let cos_theta = self.rotation.cos(); - let sin_theta = self.rotation.sin(); + pub(crate) fn to_matrix(&mut self) -> CameraUniform { + let cos = self.rotation.cos(); + let sin = self.rotation.sin(); - let x_axis = Vector3::new(cos_theta, -sin_theta, 0.0); - let y_axis = Vector3::new(sin_theta, cos_theta, 0.0); - let z_axis = Vector3::new(0.0, 0.0, 1.0); + let x_axis = Vector2::new(cos, -sin); + let y_axis = Vector2::new(sin, cos); - let eye = Vector3::new(self.position.0, self.position.1, 0.0); + let eye = Vector2::new(self.position.0, self.position.1); let x_dot = -cgmath::dot(x_axis, eye); let y_dot = -cgmath::dot(y_axis, eye); - let z_dot = -cgmath::dot(z_axis, eye); #[rustfmt::skip] let view_matrix = Matrix4::new( - x_axis.x, y_axis.x, z_axis.x, 0.0, - x_axis.y, y_axis.y, z_axis.y, 0.0, - x_axis.x, y_axis.y, z_axis.z, 0.0, - x_dot, y_dot, z_dot, 1.0 + x_axis.x, y_axis.x, 0.0, 0.0, + x_axis.y, y_axis.y, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + x_dot, y_dot, 0.0, 1.0 ); #[rustfmt::skip] // TODO implement more scaling coordinate systems let projection_matrix = Matrix4::new( - self.inverse_aspect_ratio, 0.0, 0.0, 0.0, + self.inverse_aspect_ratio * self.zoom, 0.0, 0.0, 0.0, 0.0, self.zoom, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 ); - (projection_matrix * view_matrix).into() + self.rotation += 0.01; + + let transform = projection_matrix * view_matrix; + transform.into() } } diff --git a/src/renderer.rs b/src/renderer.rs index 5820f1c..4ac521f 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -74,7 +74,7 @@ impl Renderer { let adapter = adapter.or_else(|| { instance - .enumerate_adapters(wgpu::Backends::all()) + .enumerate_adapters(wgpu::Backends::PRIMARY) .find(|adapter| !surface.get_supported_formats(adapter).is_empty()) }); -- cgit v1.2.3