mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-25 16:28:42 -06:00
upd
This commit is contained in:
parent
716a5fe641
commit
d06c0b2a96
12
src/game.rs
12
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
|
||||
if state.first_draw {
|
||||
let target_dimensions = target.get_dimensions();
|
||||
let perspective = state.camera.perspective_matrix(target_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;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -110,7 +110,7 @@ impl Default for Controls {
|
|||
Self {
|
||||
inputs: Default::default(),
|
||||
speed: 40.,
|
||||
sensitivity: 2.,
|
||||
sensitivity: 1.24,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue