Skip to content

Commit

Permalink
Revert "simple frustum culling"
Browse files Browse the repository at this point in the history
This reverts commit e92ee62.
  • Loading branch information
SpeedyTurtle599 committed Aug 22, 2024
1 parent 2088b0a commit cf73267
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,17 @@ impl ApplicationHandler for Game<'_> {
self.world.spawn_chunk_updates();

if self.world.spawn_mesh_updates() {
let t = std::time::Instant::now();
//println!("Meshing...");
self.world.get_all_chunk_meshes(&renderer.device);
//println!("{:?}, {:?}", verts, indices);
println!("{:?}", t.elapsed());
//println!("Pushing meshes to GPU...");
//renderer.push_indices(indices, index_offsets);
//println!("Done!");
//self.world.need_mesh_update.lock().unwrap().clear();
println!("{:?}", t.elapsed());
}
//println!("through");


match renderer.render(&self.world) {
Expand Down
24 changes: 4 additions & 20 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,9 @@ impl<'a> Renderer<'a> {

println!("Using backend {}", adapter.get_info().backend.to_str().to_uppercase());

let required_features = wgpu::Features::default();//wgpu::Features::CONSERVATIVE_RASTERIZATION;

let (device, queue) = adapter.request_device(
&wgpu::DeviceDescriptor {
required_features,
required_features: wgpu::Features::PUSH_CONSTANTS,
required_limits: wgpu::Limits::default(),
..Default::default()
},
Expand Down Expand Up @@ -538,29 +536,15 @@ impl<'a> Renderer<'a> {

// SEND IT ALL IN

use glam::{Vec3, Mat3};
let player = world.entities.read_lock(world.player).unwrap();
let pos = player.pos;
let facing = player.facing;
// improved frustum culling can be done if the fov is taken into account and culling happens on the normals of the 4 planes of the camera's view
drop(player);
for handle in world.chunks.iter() {

// DO FRUSTUM CULLING
let chunk = world.chunks.read_lock(handle).unwrap();
if (Vec3::new(chunk.x, chunk.y, chunk.z) - pos).dot(facing) < -23.0 {
//println!("skipped {} {} {}", chunk.x, chunk.y, chunk.z);
continue;
}


// the .slice(..) will control frustum culling when the time comes
for (i, handle) in world.chunks.iter().enumerate() {
render_pass.set_pipeline(self.pipeline.as_ref().unwrap()); // 2.
render_pass.set_bind_group(0, &self.frame_data_bind_group, &[]);
for (i, texset) in self.texture_sets.iter().enumerate() {
render_pass.set_bind_group((i+1) as u32, &texset.bind_group, &[]);
}


let chunk = world.chunks.read_lock(handle).unwrap();
render_pass.set_vertex_buffer(0, chunk.vertex_buffer.as_ref().expect("A vertex buffer was never pushed to the GPU!").slice(..));
render_pass.set_index_buffer(chunk.index_buffer.as_ref().expect("An index buffer was never pushed to the GPU!").slice(..), wgpu::IndexFormat::Uint32); // 1.
render_pass.draw_indexed(0..chunk.index_count, 0, 0..1); // 2.
Expand Down
31 changes: 23 additions & 8 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ impl World {
let block_properties = &self.block_properties;
let tp = &self.thread_pool;

self.thread_pool.install(|| {
//self.thread_pool.install(|| {
let mut wlock = chunk.write().unwrap();
wlock.make_mesh(block_properties, tp);
wlock.ready_to_display = true;
});
//});
}
}
}
Expand All @@ -185,15 +185,30 @@ impl World {
}
}

pub fn get_all_chunk_meshes(&mut self, device: &wgpu::Device) {
pub fn get_all_chunk_meshes(&mut self, device: &impl wgpu::util::DeviceExt) {
// let mut vertices = Vec::<geometry::Vertex>::new();
// let mut indices = Vec::<u32>::new();
// let mut indices_offsets = Vec::<u32>::new();
// let mut index_offset = 0u32;

// for handle in self.need_mesh_update.lock().unwrap().iter() {
// println!("Updated {:?}", handle);
// self.chunks.write_lock(*handle).unwrap().make_mesh(&self.block_properties, &self.thread_pool);
// }

for handle in self.chunks.iter() {
let chunk = self.chunks.fetch_lock(handle).unwrap();
if chunk.read().unwrap().vertex_buffer.is_none() {
self.thread_pool.install(||{
chunk.write().unwrap().make_vertex_buffer(device);
});
let mut chunk = self.chunks.write_lock(handle).unwrap();
if chunk.vertex_buffer.is_none() {
chunk.make_vertex_buffer(device);
}
//let v = &chunk.mesh;
//vertices.extend(v);
//indices.extend(chunk.get_indices(index_offset));
//index_offset += chunk.mesh.len() as u32;
//indices_offsets.push(index_offset);
}

//(indices, indices_offsets)
}

fn do_physics(&self, dt: f32, e: ArenaHandle<Entity>) {
Expand Down

0 comments on commit cf73267

Please sign in to comment.