From 169f068f75a5e4cd65cb2615ac4100cb4c775324 Mon Sep 17 00:00:00 2001 From: Thinkofname Date: Tue, 22 Mar 2016 11:47:02 +0000 Subject: [PATCH] Add frustum culling --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/main.rs | 1 + src/render/mod.rs | 13 ++++++++++--- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 805ed3b..9d0ebf3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.0.1" dependencies = [ "byteorder 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "cgmath 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "collision 0.5.1 (git+https://github.com/csherratt/collision-rs?rev=f80825e)", "flate2 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "glutin 0.4.9 (git+https://github.com/Thinkofname/glutin?branch=hide-cursor-state-x11)", "hyper 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -94,6 +95,15 @@ dependencies = [ "objc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "collision" +version = "0.5.1" +source = "git+https://github.com/csherratt/collision-rs?rev=f80825e#f80825eca687ff1053ff492e54fa782944c9cf6b" +dependencies = [ + "cgmath 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "color_quant" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 1758af1..1a6c350 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ rustc-serialize = "0.3.18" log = "0.3.5" cgmath = "0.7.0" lazy_static = "0.1.15" +collision = {git = "https://github.com/csherratt/collision-rs", rev = "f80825e"} [dependencies.steven_gl] path = "./gl" diff --git a/src/main.rs b/src/main.rs index 516c579..65fc4aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ extern crate cgmath; extern crate log; #[macro_use] extern crate lazy_static; +extern crate collision; #[macro_use] pub mod macros; diff --git a/src/render/mod.rs b/src/render/mod.rs index 1d472b3..0301f0c 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -29,6 +29,7 @@ use byteorder::{WriteBytesExt, NativeEndian}; use serde_json; use cgmath::{self, Vector, Point}; use world; +use collision; const ATLAS_SIZE: usize = 1024; @@ -287,11 +288,17 @@ impl Renderer { self.chunk_shader.light_level.set_float(LIGHT_LEVEL); self.chunk_shader.sky_offset.set_float(SKY_OFFSET); + let frustum = collision::Frustum::from_matrix4(self.perspective_matrix * camera_matrix).unwrap(); + for (pos, info) in &self.chunks { if let Some(solid) = info.solid.as_ref() { - self.chunk_shader.offset.set_int3(pos.0, pos.1 * 4096, pos.2); - solid.array.bind(); - gl::draw_elements(gl::TRIANGLES, solid.count, self.element_buffer_type, 0); + let min = cgmath::Point3::new(pos.0 as f32 * 16.0, -pos.1 as f32 * 16.0, pos.2 as f32 * 16.0); + let bounds = collision::Aabb3::new(min, min + cgmath::Vector3::new(16.0, -16.0, 16.0)); + if frustum.contains(bounds) != collision::Relation::Out { + self.chunk_shader.offset.set_int3(pos.0, pos.1 * 4096, pos.2); + solid.array.bind(); + gl::draw_elements(gl::TRIANGLES, solid.count, self.element_buffer_type, 0); + } } }