Use altitude: f32 and index: u32 instead of respectively position: vec3 and tex_coords: vec2

It reduces GPU memory usage from 20-21MiB to 16-17MiB.
This commit is contained in:
2022-10-17 01:38:18 +02:00
parent e6e30712b0
commit 02e28b2bc8
2 changed files with 48 additions and 19 deletions
+25 -4
View File
@@ -38,17 +38,38 @@ fn main() {
uniform mat4 persp_matrix;
uniform mat4 view_matrix;
in vec3 position;
in vec3 normal;
in vec2 tex_coords;
in uint index;
in float altitude;
out vec3 v_normal;
out vec2 v_tex_coords;
void main() {
v_normal = normal;
v_tex_coords = tex_coords;
gl_Position = persp_matrix * view_matrix * vec4(position * 0.005, 1.0);
uint index_mod = index % 6u;
uint index_div = index / 6u;
uint z = index_div / 250u;
uint x = index_div % 250u;
if(index_mod == 0u)
v_tex_coords = vec2(0, 0);
else if(index_mod == 1u || index_mod == 4u)
{
v_tex_coords = vec2(0, 1);
x++;
}
else if(index_mod == 2u || index_mod == 3u)
{
v_tex_coords = vec2(1, 0);
z++;
}
else //if(index_mod == 5u)
{
v_tex_coords = vec2(1, 1);
x++;
z++;
}
gl_Position = persp_matrix * view_matrix * vec4(vec3(x, altitude, z) * 0.005, 1.0);
}
",
+23 -15
View File
@@ -59,12 +59,12 @@ where
pub fn load_ground(display: &Display) -> VertexBufferAny {
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 3],
altitude: f32,
normal: [f32; 3],
tex_coords: [f32; 2],
index: u32,
}
implement_vertex!(Vertex, position, normal, tex_coords);
implement_vertex!(Vertex, altitude, normal, index);
let mut vertex_data: Vec<Vertex> = Vec::new();
@@ -72,6 +72,8 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
let lines: Vec<&str> = contents.split('\n').collect();
let mut index: u32 = 0;
for lines_index in 0..lines.len() - 1 {
let line = lines[lines_index];
let next_line = lines[lines_index + 1];
@@ -88,42 +90,48 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
// First triangle.
vertex_data.push(Vertex {
position: [lines_index as f32, column, columns_index as f32],
altitude: column,
normal: [0.0, 0.0, 1.0],
tex_coords: [0.0, 0.0],
index,
});
index += 1;
vertex_data.push(Vertex {
position: [lines_index as f32, column_right, (columns_index + 1) as f32],
altitude: column_right,
normal: [0.0, 0.0, 1.0],
tex_coords: [0.0, 1.0],
index,
});
index += 1;
vertex_data.push(Vertex {
position: [(lines_index + 1) as f32, column_below, columns_index as f32],
altitude: column_below,
normal: [0.0, 0.0, 1.0],
tex_coords: [1.0, 0.0],
index,
});
index += 1;
// Second triangle.
vertex_data.push(Vertex {
position: [(lines_index + 1) as f32, column_below, columns_index as f32],
altitude: column_below,
normal: [0.0, 0.0, 1.0],
tex_coords: [1.0, 0.0],
index,
});
index += 1;
vertex_data.push(Vertex {
position: [lines_index as f32, column_right, (columns_index + 1) as f32],
altitude: column_right,
normal: [0.0, 0.0, 1.0],
tex_coords: [0.0, 1.0],
index,
});
index += 1;
vertex_data.push(Vertex {
position: [(lines_index + 1) as f32, column_below_right, (columns_index + 1) as f32],
altitude: column_below_right,
normal: [0.0, 0.0, 1.0],
tex_coords: [1.0, 1.0],
index,
});
index += 1;
}
}