Fix #2: Optimize ground rendering using triangle strip
This commit is contained in:
+17
-18
@@ -83,30 +83,26 @@ fn main() {
|
|||||||
const uint TILE_SIZE = 4u;
|
const uint TILE_SIZE = 4u;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
uint index_mod = index % 6u;
|
uint index_mod_502 = index % 502u;
|
||||||
uint index_div = index / 6u;
|
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_502_mod_2 == 0u ? index_mod_502_div_2 : 250u - index_mod_502_div_2;
|
||||||
uint x = index_div % 250u;
|
uint z = index_div_502 + index % 2u;
|
||||||
|
|
||||||
switch (index_mod) {
|
switch (index % 4u) {
|
||||||
case 0u:
|
case 0u:
|
||||||
v_tex_coords = vec2(0, 0);
|
v_tex_coords = vec2(index_div_502_mod_2, 0);
|
||||||
break;
|
break;
|
||||||
case 1u:
|
case 1u:
|
||||||
case 4u:
|
v_tex_coords = vec2(index_div_502_mod_2, 1);
|
||||||
v_tex_coords = vec2(0, 1);
|
|
||||||
x++;
|
|
||||||
break;
|
break;
|
||||||
case 2u:
|
case 2u:
|
||||||
case 3u:
|
v_tex_coords = vec2(1u - index_div_502_mod_2, 0);
|
||||||
v_tex_coords = vec2(1, 0);
|
|
||||||
z++;
|
|
||||||
break;
|
break;
|
||||||
default: // case 5u
|
default: // case 3u
|
||||||
v_tex_coords = vec2(1, 1);
|
v_tex_coords = vec2(1u - index_div_502_mod_2, 1);
|
||||||
x++;
|
|
||||||
z++;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
v_biome = biome;
|
v_biome = biome;
|
||||||
@@ -187,8 +183,11 @@ fn main() {
|
|||||||
case 17u:
|
case 17u:
|
||||||
f_color = texture(tex17, v_tex_coords);
|
f_color = texture(tex17, v_tex_coords);
|
||||||
break;
|
break;
|
||||||
default: // case 18u:
|
case 18u:
|
||||||
f_color = texture(tex18, v_tex_coords);
|
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
|
target
|
||||||
.draw(
|
.draw(
|
||||||
&vertex_buffer,
|
&vertex_buffer,
|
||||||
&glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
|
&glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip),
|
||||||
&program,
|
&program,
|
||||||
&uniforms,
|
&uniforms,
|
||||||
¶ms,
|
¶ms,
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ impl CameraState {
|
|||||||
pub fn get_perspective(&self) -> [[f32; 4]; 4] {
|
pub fn get_perspective(&self) -> [[f32; 4]; 4] {
|
||||||
let fov: f32 = 3.141592 / 2.0;
|
let fov: f32 = 3.141592 / 2.0;
|
||||||
let zfar = 53_209.0;
|
let zfar = 53_209.0;
|
||||||
let znear = 0.01;
|
let znear = 1.0; //0.01;
|
||||||
|
|
||||||
let f = 1.0 / (fov / 2.0).tan();
|
let f = 1.0 / (fov / 2.0).tan();
|
||||||
|
|
||||||
@@ -118,7 +118,7 @@ impl CameraState {
|
|||||||
s.0 * f.1 - s.1 * f.0,
|
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 {
|
if self.moving_up {
|
||||||
self.position.0 += u.0 * SPEED;
|
self.position.0 += u.0 * SPEED;
|
||||||
|
|||||||
+62
-44
@@ -4,6 +4,7 @@ use glium::glutin::event_loop::{ControlFlow, EventLoop};
|
|||||||
use glium::vertex::VertexBufferAny;
|
use glium::vertex::VertexBufferAny;
|
||||||
use glium::{self, Display};
|
use glium::{self, Display};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::str::Split;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
pub mod camera;
|
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 {
|
fn get_altitude(height_columns: &Vec<&str>, columns_index: usize) -> f32 {
|
||||||
let height_str = height_columns[columns_index];
|
let height_str = height_columns[columns_index];
|
||||||
return if height_str == "N" {
|
return if height_str == "N" {
|
||||||
-100.0
|
SEABED_ALTITUDE
|
||||||
} else {
|
} else {
|
||||||
height_str.parse::<f32>().unwrap()
|
height_str.parse::<f32>().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`.
|
/// Returns a vertex buffer that should be rendered as `TrianglesList`.
|
||||||
pub fn load_ground(display: &Display) -> VertexBufferAny {
|
pub fn load_ground(display: &Display) -> VertexBufferAny {
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
@@ -132,20 +138,59 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
|
|||||||
for lines_index in 0..height_lines.len() - 1 {
|
for lines_index in 0..height_lines.len() - 1 {
|
||||||
let height_line = height_lines[lines_index];
|
let height_line = height_lines[lines_index];
|
||||||
let height_next_line = height_lines[lines_index + 1];
|
let height_next_line = height_lines[lines_index + 1];
|
||||||
let height_columns: Vec<&str> = height_line.split(' ').collect();
|
let height_columns_split: Split<char> = height_line.split(' ');
|
||||||
let height_next_columns: Vec<&str> = height_next_line.split(' ').collect();
|
let height_next_columns_split: Split<char> = 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];
|
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 = 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 = 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 {
|
vertex_data.push(Vertex {
|
||||||
altitude: column,
|
altitude: column,
|
||||||
@@ -155,14 +200,6 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
|
|||||||
});
|
});
|
||||||
index += 1;
|
index += 1;
|
||||||
|
|
||||||
vertex_data.push(Vertex {
|
|
||||||
altitude: column_right,
|
|
||||||
index,
|
|
||||||
biome,
|
|
||||||
chunk,
|
|
||||||
});
|
|
||||||
index += 1;
|
|
||||||
|
|
||||||
vertex_data.push(Vertex {
|
vertex_data.push(Vertex {
|
||||||
altitude: column_below,
|
altitude: column_below,
|
||||||
index,
|
index,
|
||||||
@@ -170,39 +207,20 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
|
|||||||
chunk,
|
chunk,
|
||||||
});
|
});
|
||||||
index += 1;
|
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 += 1;
|
||||||
// i == 250 thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: BufferCreationError(OutOfMemory)', src/support/mod.rs:203:10
|
// 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;
|
break;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
println!("charlie");
|
println!("charlie");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user