From d06c0b2a96b29c55b4b7cc8a8e9b8b488b820d98 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Wed, 18 Jan 2023 02:46:15 +0100 Subject: [PATCH] upd --- src/game.rs | 14 ++++++-- src/game/camera.rs | 55 ++++++++++++++++++++++++++++-- src/game/controller.rs | 2 +- src/game/world/thread/world_gen.rs | 2 +- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/game.rs b/src/game.rs index 68a7cdb..42c4ac5 100644 --- a/src/game.rs +++ b/src/game.rs @@ -26,12 +26,14 @@ use options::GameOptions; struct State { pub camera: Camera, + pub first_draw: bool, pub controls: Controls, pub world: World } impl State { pub fn init() -> Self { Self { + first_draw: true, camera: Camera::default(), controls: Controls::default(), world: World::new(), @@ -90,6 +92,9 @@ pub fn run() { *control_flow = ControlFlow::Exit; return }, + WindowEvent::Resized(size) => { + state.camera.update_perspective_matrix((size.width, size.height)); + }, _ => return } }, @@ -118,8 +123,11 @@ pub fn run() { target.clear_color_and_depth((0.5, 0.5, 1., 1.), 1.); //Compute camera - let target_dimensions = target.get_dimensions(); - let perspective = state.camera.perspective_matrix(target_dimensions); + if state.first_draw { + let target_dimensions = target.get_dimensions(); + state.camera.update_perspective_matrix(target_dimensions); + } + let perspective = state.camera.perspective_matrix; let view = state.camera.view_matrix(); //Draw chunks @@ -146,5 +154,7 @@ pub fn run() { //Finish drawing target.finish().unwrap(); + + state.first_draw = false; }); } diff --git a/src/game/camera.rs b/src/game/camera.rs index e6c32e4..9394241 100644 --- a/src/game/camera.rs +++ b/src/game/camera.rs @@ -22,6 +22,7 @@ pub struct Camera { pub fov: f32, pub znear: f32, pub zfar: f32, + pub perspective_matrix: [[f32; 4]; 4], } impl Camera { /// Update camera direction based on yaw/pitch @@ -67,20 +68,67 @@ impl Camera { ] } - pub fn perspective_matrix(&self, target_dimensions: (u32, u32)) -> [[f32; 4]; 4] { + pub fn update_perspective_matrix(&mut self, target_dimensions: (u32, u32)) { let znear = self.znear; let zfar = self.zfar; let fov = self.fov; let (width, height) = target_dimensions; let aspect_ratio = height as f32 / width as f32; let f = 1.0 / (fov / 2.0).tan(); - [ + self.perspective_matrix = [ [f*aspect_ratio, 0.0, 0.0, 0.0], [0.0, f, 0.0, 0.0], [0.0, 0.0, (zfar+znear)/(zfar-znear), 1.0], [0.0, 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0], - ] + ]; } + + // https://web.archive.org/web/20070226173353/https://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf + pub fn frustum_planes(&self, normalized: bool) -> [[f32; 4]; 6] { + let mut p_planes = [[0.0_f32; 4]; 6]; + let matrix = self.perspective_matrix; + fn normalize_plane(mut plane: [f32; 4]) -> [f32; 4] { + let mag = (plane[0] * plane[0] + plane[1] * plane[1] + plane[2] * plane[2]).sqrt(); + plane[0] = plane[0] / mag; + plane[1] = plane[1] / mag; + plane[2] = plane[2] / mag; + plane[3] = plane[3] / mag; + plane + } + // Left clipping plane + p_planes[0][0] = matrix[3][0] + matrix[0][0]; + p_planes[0][1] = matrix[3][1] + matrix[0][1]; + p_planes[0][2] = matrix[3][2] + matrix[0][2]; + p_planes[0][3] = matrix[3][3] + matrix[0][3]; + // Right clipping plane + p_planes[1][0] = matrix[3][0] - matrix[0][0]; + p_planes[1][1] = matrix[3][1] - matrix[0][1]; + p_planes[1][2] = matrix[3][2] - matrix[0][2]; + p_planes[1][3] = matrix[3][3] - matrix[0][3]; + // Top clipping plane + p_planes[2][0] = matrix[3][0] - matrix[1][0]; + p_planes[2][1] = matrix[3][1] - matrix[1][1]; + p_planes[2][2] = matrix[3][2] - matrix[1][2]; + p_planes[2][3] = matrix[3][3] - matrix[1][3]; + // Bottom clipping plane + p_planes[3][0] = matrix[3][0] + matrix[1][0]; + p_planes[3][1] = matrix[3][1] + matrix[1][1]; + p_planes[3][2] = matrix[3][2] + matrix[1][2]; + p_planes[3][3] = matrix[3][3] + matrix[1][3]; + // Near clipping plane + p_planes[4][0] = matrix[3][0] + matrix[3][0]; + p_planes[4][1] = matrix[3][1] + matrix[3][1]; + p_planes[4][2] = matrix[3][2] + matrix[3][2]; + p_planes[4][3] = matrix[3][3] + matrix[3][3]; + // Far clipping plane + p_planes[5][0] = matrix[3][0] - matrix[3][0]; + p_planes[5][1] = matrix[3][1] - matrix[3][1]; + p_planes[5][2] = matrix[3][2] - matrix[3][2]; + p_planes[5][3] = matrix[3][3] - matrix[3][3]; + + p_planes + } + } impl Default for Camera { fn default() -> Self { @@ -93,6 +141,7 @@ impl Default for Camera { znear: 0.1, yaw: 0., pitch: 0., + perspective_matrix: [[0.; 4]; 4] } } } diff --git a/src/game/controller.rs b/src/game/controller.rs index cb029a9..58b5b29 100644 --- a/src/game/controller.rs +++ b/src/game/controller.rs @@ -110,7 +110,7 @@ impl Default for Controls { Self { inputs: Default::default(), speed: 40., - sensitivity: 2., + sensitivity: 1.24, } } } diff --git a/src/game/world/thread/world_gen.rs b/src/game/world/thread/world_gen.rs index 40d81ad..faac7b2 100644 --- a/src/game/world/thread/world_gen.rs +++ b/src/game/world/thread/world_gen.rs @@ -14,7 +14,7 @@ pub fn generate_chunk(position: IVec2, seed: u32) -> ChunkData { //generate noises let height_noise = NoiseBuilder::fbm_2d_offset(world_xz.x, CHUNK_SIZE, world_xz.y, CHUNK_SIZE) - .with_freq(0.01) + .with_freq(0.025) .with_octaves(4) .with_seed(seed as i32) .generate_scaled(TERRAIN_HEIGHT_MIN, TERRAIN_HEIGHT_MAX);