8 Commits

3 changed files with 254 additions and 131 deletions
+105 -51
View File
@@ -6,6 +6,7 @@ extern crate load_file;
use glium::texture::SrgbTexture2d;
use std::io::Cursor;
use std::time::Instant;
#[allow(unused_imports)]
use glium::{glutin, Surface};
@@ -38,12 +39,12 @@ const BIOMES: [&str; BIOMES_NUMBER] = [
fn main() {
// building the display, ie. the main object
let event_loop = glutin::event_loop::EventLoop::new();
let wb = glutin::window::WindowBuilder::new();
let wb = glutin::window::WindowBuilder::new().with_title("LemnosLife");
let cb = glutin::ContextBuilder::new().with_depth_buffer(24);
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
// building the vertex and index buffers
let (vertex_buffer, chunks) = support::load_ground(&display);
let vertex_buffer = support::load_ground(&display);
let textures: [SrgbTexture2d; BIOMES_NUMBER] = BIOMES.map(|biome| {
println!("{}", biome);
@@ -73,70 +74,121 @@ fn main() {
in uint index;
in float altitude;
in uint biome;
in uvec2 chunk;
out vec2 v_tex_coords;
flat out uint v_biome;
const uint TILE_SIZE = 4u;
void main() {
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++;
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 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 % 4u) {
case 0u:
v_tex_coords = vec2(index_div_502_mod_2, 0);
break;
case 1u:
v_tex_coords = vec2(index_div_502_mod_2, 1);
break;
case 2u:
v_tex_coords = vec2(1u - index_div_502_mod_2, 0);
break;
default: // case 3u
v_tex_coords = vec2(1u - index_div_502_mod_2, 1);
break;
}
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);
v_biome = biome;
x *= TILE_SIZE;
z *= TILE_SIZE;
x += chunk.x;
z += chunk.y;
gl_Position = persp_matrix * view_matrix * vec4(vec3(x, altitude, z), 1.0);
}
",
fragment: "
#version 140
flat in vec3 normal;
in vec2 v_tex_coords;
flat in uint v_biome;
out vec4 f_color;
flat in uint biome;
uniform sampler2D tex0, tex1, tex2, tex3, tex4, tex5, tex6, tex7, tex8, tex9, tex10, tex11, tex12, tex13, tex14, tex15, tex16, tex17, tex18;
const vec3 LIGHT = vec3(0.0, -1.0, 0.0);
void main() {
float lum = max(dot(normalize(normal), normalize(LIGHT)), 0.0);
if(biome == 0u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex0, v_tex_coords);
else if(biome == 1u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex1, v_tex_coords);
else if(biome == 2u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex2, v_tex_coords);
else if(biome == 3u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex3, v_tex_coords);
else if(biome == 4u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex4, v_tex_coords);
else if(biome == 5u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex5, v_tex_coords);
else if(biome == 6u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex6, v_tex_coords);
else if(biome == 7u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex7, v_tex_coords);
else if(biome == 8u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex8, v_tex_coords);
else if(biome == 9u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex9, v_tex_coords);
else if(biome == 10u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex10, v_tex_coords);
else if(biome == 11u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex11, v_tex_coords);
else if(biome == 12u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex12, v_tex_coords);
else if(biome == 13u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex13, v_tex_coords);
else if(biome == 14u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex14, v_tex_coords);
else if(biome == 15u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex15, v_tex_coords);
else if(biome == 16u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex16, v_tex_coords);
else if(biome == 17u) f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex17, v_tex_coords);
else /*if(biome == 18u)*/ f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex18, v_tex_coords);
switch (v_biome) {
case 0u:
f_color = texture(tex0, v_tex_coords);
break;
case 1u:
f_color = texture(tex1, v_tex_coords);
break;
case 2u:
f_color = texture(tex2, v_tex_coords);
break;
case 3u:
f_color = texture(tex3, v_tex_coords);
break;
case 4u:
f_color = texture(tex4, v_tex_coords);
break;
case 5u:
f_color = texture(tex5, v_tex_coords);
break;
case 6u:
f_color = texture(tex6, v_tex_coords);
break;
case 7u:
f_color = texture(tex7, v_tex_coords);
break;
case 8u:
f_color = texture(tex8, v_tex_coords);
break;
case 9u:
f_color = texture(tex9, v_tex_coords);
break;
case 10u:
f_color = texture(tex10, v_tex_coords);
break;
case 11u:
f_color = texture(tex11, v_tex_coords);
break;
case 12u:
f_color = texture(tex12, v_tex_coords);
break;
case 13u:
f_color = texture(tex13, v_tex_coords);
break;
case 14u:
f_color = texture(tex14, v_tex_coords);
break;
case 15u:
f_color = texture(tex15, v_tex_coords);
break;
case 16u:
f_color = texture(tex16, v_tex_coords);
break;
case 17u:
f_color = texture(tex17, v_tex_coords);
break;
case 18u:
f_color = texture(tex18, v_tex_coords);
break;
default: // case 19u:
f_color = vec4(0.0, 0.0, 0.0, 1.0);
}
}
"
},
@@ -172,7 +224,6 @@ fn main() {
tex16: &textures[16],
tex17: &textures[17],
tex18: &textures[18],
chunks: &chunks,
};
// draw parameters
@@ -188,18 +239,21 @@ fn main() {
};
// drawing a frame
let start = Instant::now();
let mut target = display.draw();
target.clear_color_and_depth((0.0, 0.0, 0.0, 0.0), 1.0);
target
.draw(
&vertex_buffer,
&glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
&glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip),
&program,
&uniforms,
&params,
)
.unwrap();
target.finish().unwrap();
let duration = start.elapsed();
println!("Time elapsed is: {:?}", duration);
let mut action = support::Action::Continue;
+24 -22
View File
@@ -16,9 +16,9 @@ pub struct CameraState {
impl CameraState {
pub fn new() -> CameraState {
CameraState {
aspect_ratio: 1024.0 / 768.0,
position: (0.1, 0.1, 1.0),
aspect_ratio: 1_024.0 / 768.0,
// The second coordinate is for the altitude.
position: (3_646.41, 12.3622, 13_113.7),
direction: (0.0, 0.0, -1.0),
moving_up: false,
moving_left: false,
@@ -33,8 +33,8 @@ impl CameraState {
pub fn get_perspective(&self) -> [[f32; 4]; 4] {
let fov: f32 = 3.141592 / 2.0;
let zfar = 8000.0;
let znear = 0.01;
let zfar = 53_209.0;
let znear = 1.0; //0.01;
let f = 1.0 / (fov / 2.0).tan();
@@ -118,40 +118,42 @@ impl CameraState {
s.0 * f.1 - s.1 * f.0,
);
const SPEED: f32 = 5.0; //1.0;
if self.moving_up {
self.position.0 += u.0 * 0.01;
self.position.1 += u.1 * 0.01;
self.position.2 += u.2 * 0.01;
self.position.0 += u.0 * SPEED;
self.position.1 += u.1 * SPEED;
self.position.2 += u.2 * SPEED;
}
if self.moving_left {
self.position.0 -= s.0 * 0.01;
self.position.1 -= s.1 * 0.01;
self.position.2 -= s.2 * 0.01;
self.position.0 -= s.0 * SPEED;
self.position.1 -= s.1 * SPEED;
self.position.2 -= s.2 * SPEED;
}
if self.moving_down {
self.position.0 -= u.0 * 0.01;
self.position.1 -= u.1 * 0.01;
self.position.2 -= u.2 * 0.01;
self.position.0 -= u.0 * SPEED;
self.position.1 -= u.1 * SPEED;
self.position.2 -= u.2 * SPEED;
}
if self.moving_right {
self.position.0 += s.0 * 0.01;
self.position.1 += s.1 * 0.01;
self.position.2 += s.2 * 0.01;
self.position.0 += s.0 * SPEED;
self.position.1 += s.1 * SPEED;
self.position.2 += s.2 * SPEED;
}
if self.moving_forward {
self.position.0 += f.0 * 0.01;
self.position.1 += f.1 * 0.01;
self.position.2 += f.2 * 0.01;
self.position.0 += f.0 * SPEED;
self.position.1 += f.1 * SPEED;
self.position.2 += f.2 * SPEED;
}
if self.moving_backward {
self.position.0 -= f.0 * 0.01;
self.position.1 -= f.1 * 0.01;
self.position.2 -= f.2 * 0.01;
self.position.0 -= f.0 * SPEED;
self.position.1 -= f.1 * SPEED;
self.position.2 -= f.2 * SPEED;
}
if self.rotate_left {
+125 -58
View File
@@ -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,47 +68,55 @@ 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::<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 = 0;
/// Returns a vertex buffer that should be rendered as `TrianglesList`.
pub fn load_ground(display: &Display) -> (VertexBufferAny, Vec<[u32; 2]>) {
pub fn load_ground(display: &Display) -> VertexBufferAny {
#[derive(Copy, Clone)]
struct Vertex {
altitude: f32,
normal: [f32; 3],
index: u32,
biome: u32,
chunk: [u32; 2],
}
implement_vertex!(Vertex, altitude, normal, index, biome);
implement_vertex!(Vertex, altitude, index, biome, chunk);
let mut vertex_data: Vec<Vertex> = Vec::new();
let mut chunks: Vec<[u32; 2]> = Vec::new();
let ground_folder = "Extensions/LemnosLife/Map/Altis/Ground/";
let paths = fs::read_dir(ground_folder).unwrap();
let mut i: u32 = 0;
for path in paths {
/*for path in paths {
let file_name = path
.unwrap()
.path()
.to_str()
.unwrap()
.replace("Extensions/LemnosLife/Map/Altis/Ground/", "")
.replace(".height", "");
.replace(".height", "");*/
for file_name in ["3 13", "4 13"] {
println!("{}", file_name);
let file_name_parts: Vec<&str> = file_name.split(' ').collect();
chunks.push([file_name_parts[0].parse().unwrap(), file_name_parts[1].parse().unwrap()]);
const CHUNK_SIZE: u32 = 1_000;
let chunk = [
file_name_parts[0].parse::<u32>().unwrap() * CHUNK_SIZE,
file_name_parts[1].parse::<u32>().unwrap() * CHUNK_SIZE,
];
// have deleted a few height files to make it work
// TODO: use triangle strip
let height_contents = fs::read_to_string(&format!(
"Extensions/LemnosLife/Map/Altis/Ground/{}.height",
file_name
@@ -119,95 +128,153 @@ pub fn load_ground(display: &Display) -> (VertexBufferAny, Vec<[u32; 2]>) {
file_name
))
.expect("Unable to load biomes file!");
println!("alpha");
let height_lines: Vec<&str> = height_contents.split('\n').collect();
let height_lines: Vec<&str> = height_contents.split('\n').rev().collect();
let biomes_lines: Vec<&str> = biomes_contents.split('\n').collect();
println!("beta");
let mut index: u32 = 0;
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<char> = height_line.split(' ');
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];
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);
// TODO: compute normals if necessary.
// First triangle.
if lines_index == 0 && columns_index == 0 {
// This vertex defines the end of the line below the ground.
vertex_data.push(Vertex {
altitude: ALTITUDE_BETWEEN_CHUNKS,
index,
biome,
chunk,
});
vertex_data.push(Vertex {
altitude: ALTITUDE_BETWEEN_CHUNKS,
index,
biome,
chunk,
});
vertex_data.push(Vertex {
altitude: ALTITUDE_BETWEEN_CHUNKS,
index,
biome,
chunk,
});
// This vertex defines the cursor ready to be used.
vertex_data.push(Vertex {
altitude: column,
index,
biome,
chunk,
});
vertex_data.push(Vertex {
altitude: column,
index,
biome,
chunk,
});
vertex_data.push(Vertex {
altitude: column,
index,
biome,
chunk,
});
}
vertex_data.push(Vertex {
altitude: column,
normal: [0.0, 0.0, 1.0],
index,
biome,
});
index += 1;
vertex_data.push(Vertex {
altitude: column_right,
normal: [0.0, 0.0, 1.0],
index,
biome,
chunk,
});
index += 1;
vertex_data.push(Vertex {
altitude: column_below,
normal: [0.0, 0.0, 1.0],
index,
biome,
chunk,
});
index += 1;
// Second triangle.
if lines_index == height_lines.len() - 1 && columns_index == height_columns.len() {
// This vertex starts the triangle going vertically down under the first chunk.
vertex_data.push(Vertex {
altitude: column_below,
index,
biome,
chunk,
});
vertex_data.push(Vertex {
altitude: column_below,
index,
biome,
chunk,
});
vertex_data.push(Vertex {
altitude: column_below,
index,
biome,
chunk,
});
// This vertex doesn't give width to the triangle going vertically down under the first chunk.
vertex_data.push(Vertex {
altitude: ALTITUDE_BETWEEN_CHUNKS,
index,
biome,
chunk,
});
vertex_data.push(Vertex {
altitude: ALTITUDE_BETWEEN_CHUNKS,
index,
biome,
chunk,
});
vertex_data.push(Vertex {
altitude: ALTITUDE_BETWEEN_CHUNKS,
index,
biome,
chunk,
});
}
vertex_data.push(Vertex {
altitude: column_below,
normal: [0.0, 0.0, 1.0],
index,
biome,
});
index += 1;
vertex_data.push(Vertex {
altitude: column_right,
normal: [0.0, 0.0, 1.0],
index,
biome,
});
index += 1;
vertex_data.push(Vertex {
altitude: column_below_right,
normal: [0.0, 0.0, 1.0],
index,
biome,
});
index += 1;
}
}
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 == 200 {
if i == 2 {
break;
}
}
println!("charlie");
(glium::vertex::VertexBuffer::new(display, &vertex_data)
glium::vertex::VertexBuffer::new(display, &vertex_data)
.unwrap()
.into(), chunks)
.into()
}