From 76d64b66606da6c0783926ca2f73e6adec622279 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Sun, 23 Oct 2022 01:52:24 +0200 Subject: [PATCH] Fix #2: Optimize ground rendering using triangle strip --- src/main.rs | 35 +++++++------- src/support/camera.rs | 4 +- src/support/mod.rs | 106 ++++++++++++++++++++++++------------------ 3 files changed, 81 insertions(+), 64 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3825d9c..036dd3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,30 +83,26 @@ fn main() { const uint TILE_SIZE = 4u; void main() { - uint index_mod = index % 6u; - uint index_div = index / 6u; + uint index_mod_502 = index % 502u; + uint index_div_502 = index / 502u; + uint index_div_502_mod_2 = index_div_502 % 2u; + uint index_mod_502_div_2 = index_mod_502 / 2u; - uint z = index_div / 250u; - uint x = index_div % 250u; + uint x = index_div_502_mod_2 == 0u ? index_mod_502_div_2 : 250u - index_mod_502_div_2; + uint z = index_div_502 + index % 2u; - switch (index_mod) { + switch (index % 4u) { case 0u: - v_tex_coords = vec2(0, 0); + v_tex_coords = vec2(index_div_502_mod_2, 0); break; case 1u: - case 4u: - v_tex_coords = vec2(0, 1); - x++; + v_tex_coords = vec2(index_div_502_mod_2, 1); break; case 2u: - case 3u: - v_tex_coords = vec2(1, 0); - z++; + v_tex_coords = vec2(1u - index_div_502_mod_2, 0); break; - default: // case 5u - v_tex_coords = vec2(1, 1); - x++; - z++; + default: // case 3u + v_tex_coords = vec2(1u - index_div_502_mod_2, 1); break; } v_biome = biome; @@ -187,8 +183,11 @@ fn main() { case 17u: f_color = texture(tex17, v_tex_coords); break; - default: // case 18u: + case 18u: f_color = texture(tex18, v_tex_coords); + break; + default: // case 19u: + f_color = vec4(0.0, 0.0, 0.0, 1.0); } } " @@ -246,7 +245,7 @@ fn main() { target .draw( &vertex_buffer, - &glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList), + &glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip), &program, &uniforms, ¶ms, diff --git a/src/support/camera.rs b/src/support/camera.rs index 3abdd90..6820b2f 100644 --- a/src/support/camera.rs +++ b/src/support/camera.rs @@ -34,7 +34,7 @@ impl CameraState { pub fn get_perspective(&self) -> [[f32; 4]; 4] { let fov: f32 = 3.141592 / 2.0; let zfar = 53_209.0; - let znear = 0.01; + let znear = 1.0; //0.01; let f = 1.0 / (fov / 2.0).tan(); @@ -118,7 +118,7 @@ impl CameraState { s.0 * f.1 - s.1 * f.0, ); - const SPEED: f32 = 1.0; //0.01; + const SPEED: f32 = 100.0; //1.0; if self.moving_up { self.position.0 += u.0 * SPEED; diff --git a/src/support/mod.rs b/src/support/mod.rs index c3a3c0c..ec8fe7e 100644 --- a/src/support/mod.rs +++ b/src/support/mod.rs @@ -4,6 +4,7 @@ use glium::glutin::event_loop::{ControlFlow, EventLoop}; use glium::vertex::VertexBufferAny; use glium::{self, Display}; use std::fs; +use std::str::Split; use std::time::{Duration, Instant}; pub mod camera; @@ -67,12 +68,17 @@ fn get_biome(line: &str, index: usize) -> u32 { fn get_altitude(height_columns: &Vec<&str>, columns_index: usize) -> f32 { let height_str = height_columns[columns_index]; return if height_str == "N" { - -100.0 + SEABED_ALTITUDE } else { height_str.parse::().unwrap() }; } +const SEABED_ALTITUDE: f32 = -100.0; +// seabed altitude - biggest drop * tiles in a chunk * (chunks in a row - 1) +const ALTITUDE_BETWEEN_CHUNKS: f32 = SEABED_ALTITUDE - 16.17 * 250.0 * 30.0; +const BELOW_MAP_BIOME: u32 = 19; + /// Returns a vertex buffer that should be rendered as `TrianglesList`. pub fn load_ground(display: &Display) -> VertexBufferAny { #[derive(Copy, Clone)] @@ -132,20 +138,59 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { for lines_index in 0..height_lines.len() - 1 { let height_line = height_lines[lines_index]; let height_next_line = height_lines[lines_index + 1]; - let height_columns: Vec<&str> = height_line.split(' ').collect(); - let height_next_columns: Vec<&str> = height_next_line.split(' ').collect(); + let height_columns_split: Split = height_line.split(' '); + let height_next_columns_split: Split = height_next_line.split(' '); + let (height_columns, height_next_columns): (Vec<&str>, Vec<&str>) = + if lines_index % 2 == 0 { + ( + height_columns_split.collect(), + height_next_columns_split.collect(), + ) + } else { + ( + height_columns_split.rev().collect(), + height_next_columns_split.rev().collect(), + ) + }; let biomes_line = biomes_lines[lines_index / 2]; - for columns_index in 0..height_columns.len() - 1 { + for columns_index in 0..height_columns.len() { let column = get_altitude(&height_columns, columns_index); - let column_right = get_altitude(&height_columns, columns_index + 1); let column_below = get_altitude(&height_next_columns, columns_index); - let column_below_right = get_altitude(&height_next_columns, columns_index + 1); - let biome = get_biome(biomes_line, columns_index / 2); + let biome_index = if lines_index % 2 == 0 { + columns_index / 2 + } else { + height_columns.len() / 2 - columns_index / 2 + }; + let biome = get_biome(biomes_line, biome_index); - // First triangle. + if lines_index == 0 && columns_index == 0 { + // This vertex defines the end of the wall below the ground. + vertex_data.push(Vertex { + altitude: ALTITUDE_BETWEEN_CHUNKS, + index, + biome: BELOW_MAP_BIOME, + chunk, + }); + // This vertex ends correctly the across chunks triangle under the ground. + // It is useful to keep a specific color for the triangle under the ground. + vertex_data.push(Vertex { + altitude: column, + index, + biome: BELOW_MAP_BIOME, + chunk, + }); + // This vertex defines the cursor ready to be used. + // It resets the specific color for the triangle under the ground. + vertex_data.push(Vertex { + altitude: column, + index, + biome, + chunk, + }); + } vertex_data.push(Vertex { altitude: column, @@ -155,14 +200,6 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { }); index += 1; - vertex_data.push(Vertex { - altitude: column_right, - index, - biome, - chunk, - }); - index += 1; - vertex_data.push(Vertex { altitude: column_below, index, @@ -170,39 +207,20 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { chunk, }); index += 1; - - // Second triangle. - - vertex_data.push(Vertex { - altitude: column_below, - index, - biome, - chunk, - }); - index += 1; - - vertex_data.push(Vertex { - altitude: column_right, - index, - biome, - chunk, - }); - index += 1; - - vertex_data.push(Vertex { - altitude: column_below_right, - index, - biome, - chunk, - }); - index += 1; } } + // This vertex remove the wall across the chunks above the ground. + vertex_data.push(Vertex { + altitude: ALTITUDE_BETWEEN_CHUNKS, + index, + biome: BELOW_MAP_BIOME, + chunk, + }); i += 1; // i == 250 thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: BufferCreationError(OutOfMemory)', src/support/mod.rs:203:10 - if i == 286 { + /*if i == 1 { break; - } + }*/ } println!("charlie");