diff --git a/src/game/mod.rs b/src/game/mod.rs index c1a6b05..c102c0d 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -67,7 +67,10 @@ impl Game { // Get positions let pos = camera.get_position(); let pos = Vector3{x: pos.x as i32, y: pos.y as i32, z: pos.z as i32}; - let chunk_pos = pos / world::chunk::SIZE as i32; + let mut chunk_pos = pos / world::chunk::SIZE as i32; + if pos.x < 0 { chunk_pos.x -= 1; } + if pos.y < 0 { chunk_pos.y -= 1; } + if pos.z < 0 { chunk_pos.z -= 1; } // Block position if self.prev_pos == None || self.prev_pos != Some(pos) { diff --git a/src/game/world/generator.rs b/src/game/world/generator.rs index b5489a3..408db8e 100644 --- a/src/game/world/generator.rs +++ b/src/game/world/generator.rs @@ -1,3 +1,4 @@ +use std::time::Instant; use cgmath::Vector3; use noise::{Perlin, NoiseFn}; use rand::{Rng, prelude::ThreadRng}; @@ -16,21 +17,22 @@ impl WorldGen { /* * Generate height map for given block position */ - fn gen_height_map(x: usize, z: usize, rand: &mut ThreadRng, noise: &Perlin) -> ([[usize;block::SIZE]; block::SIZE], usize) { - let x = x * block::SIZE; - let z = z * block::SIZE; + fn gen_height_map(x: i32, z: i32, rand: &mut ThreadRng, noise: &Perlin) -> ([[usize;block::SIZE]; block::SIZE], usize) { + let size = block::SIZE as i32; + let x = x * size; + let z = z * size; // Generate map let mut max: usize = 0; let mut map = [[0_usize; block::SIZE]; block::SIZE]; - for lx in 0..block::SIZE { - for lz in 0..block::SIZE { + for lx in 0..size { + for lz in 0..size { let value = (noise.get([(x + lx) as f64 / SCALE, (z + lz) as f64 /SCALE]) + 1.0) / 2.0; let mut h = (value * 30.0) as usize; h += (rand.gen::() * 2.0) as usize; if h == 0 { h = 1; } if h > max { max = h; } - map[lx][lz] = h; + map[lx as usize][lz as usize] = h; } } (map, max) @@ -44,8 +46,8 @@ impl WorldGen { let noise = Perlin::new(); // Generate height map - let (map, _max) = WorldGen::gen_height_map(block_pos.x as usize, block_pos.z as usize, &mut rng, &noise); - + let (map, _max) = WorldGen::gen_height_map(block_pos.x, block_pos.z, &mut rng, &noise); + // Fill for x in 0..block::SIZE { for z in 0..block::SIZE { diff --git a/src/game/world/mod.rs b/src/game/world/mod.rs index 04e233e..5986190 100644 --- a/src/game/world/mod.rs +++ b/src/game/world/mod.rs @@ -2,6 +2,7 @@ mod generator; mod data; pub mod chunk; pub mod block; +use std::sync::Arc; use std::time::Instant; use std::path::PathBuf; use std::io::Write; diff --git a/src/renderer/buffers/content.rs b/src/renderer/buffers/content.rs index 493301e..bbe78c4 100644 --- a/src/renderer/buffers/content.rs +++ b/src/renderer/buffers/content.rs @@ -103,6 +103,10 @@ impl Content { pub fn write(&mut self, queue: &wgpu::Queue, block_pos: &Vector3, block: &WorldBlock) { + // 0,0 at node buffer center + let half = (NODE_TEX_SIZE / 2); + let block_pos = block_pos + &Vector3{x: half as i32, y: half as i32, z: half as i32}; + // Get node let index = (block_pos.x + NODE_TEX_SIZE as i32 * (block_pos.y + NODE_TEX_SIZE as i32 * block_pos.z)) as usize; let mut value = self.node_buffer[index] as usize; @@ -167,7 +171,7 @@ impl Content { self.node_buffer = result; self.node_dirty = true; - println!("Shifted in {} ms", timer.elapsed().as_millis()); + println!("Shifted node buffer by {:?} in {} ms", offset, timer.elapsed().as_millis()); } fn write_nodes(&mut self, queue: &wgpu::Queue, z: usize) { diff --git a/src/renderer/buffers/mod.rs b/src/renderer/buffers/mod.rs index c9e415d..d49943e 100644 --- a/src/renderer/buffers/mod.rs +++ b/src/renderer/buffers/mod.rs @@ -1,7 +1,7 @@ mod uniform; pub use uniform::Uniform; pub use uniform::UniformValues; -mod content; +pub mod content; pub use content::Content; mod materials; pub use materials::Materials; diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 7616e72..bf51f83 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -117,7 +117,9 @@ impl Renderer { pub fn render(&mut self) -> Result<(), wgpu::SwapChainError> { // Update uniform buffer - let offset = Vector3{x: self.world_offset.x as f32, y: self.world_offset.y as f32, z: self.world_offset.z as f32 }; + let half = (buffers::content::NODE_TEX_SIZE / 2) as i32; + let offset = Vector3{x: (self.world_offset.x + half) as f32, y: (self.world_offset.y + half) as f32, z: (self.world_offset.z + half) as f32 }; + self.buffers.uniforms.values.update(&self.camera, offset, self.start.elapsed().as_secs_f32()); self.queue.write_buffer(&self.buffers.uniforms.buffer, 0, bytemuck::cast_slice(&[self.buffers.uniforms.values]));