diff --git a/src/main.rs b/src/main.rs index aae2dfa..38d7ad4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,8 @@ extern crate glium; #[macro_use] extern crate load_file; -use std::io::Cursor; use glium::texture::SrgbTexture2d; +use std::io::Cursor; #[allow(unused_imports)] use glium::{glutin, Surface}; @@ -13,7 +13,27 @@ use glium::{glutin, Surface}; mod support; const BIOMES_NUMBER: usize = 19; -const BIOMES: [&str; BIOMES_NUMBER] = ["dirt", "grassDry", "grassGreen", "concrete", "soil", "beach", "seabed", "thorn", "wildField", "rock", "grassWild", "stony", "forestPine", "mud", "stonyThistle", "marsh", "dead", "desert", "weed"]; +const BIOMES: [&str; BIOMES_NUMBER] = [ + "dirt", + "grassDry", + "grassGreen", + "concrete", + "soil", + "beach", + "seabed", + "thorn", + "wildField", + "rock", + "grassWild", + "stony", + "forestPine", + "mud", + "stonyThistle", + "marsh", + "dead", + "desert", + "weed", +]; fn main() { // building the display, ie. the main object @@ -28,13 +48,14 @@ fn main() { let textures: [SrgbTexture2d; BIOMES_NUMBER] = BIOMES.map(|biome| { println!("{}", biome); let image = image::load( - Cursor::new(&load_bytes!( - &format!("../Extensions/LemnosLife/Assets/Pictures/Map/Ground/{}.jpg", biome) - )), + Cursor::new(&load_bytes!(&format!( + "../Extensions/LemnosLife/Assets/Pictures/Map/Ground/{}.jpg", + biome + ))), image::ImageFormat::Jpeg, ) - .unwrap() - .to_rgba8(); + .unwrap() + .to_rgba8(); let image_dimensions = image.dimensions(); let image = glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions); diff --git a/src/support/mod.rs b/src/support/mod.rs index eeaa588..38d9e46 100644 --- a/src/support/mod.rs +++ b/src/support/mod.rs @@ -61,7 +61,16 @@ fn get_biome(line: &str, index: usize) -> u32 { biome_char.to_digit(10).unwrap() } else { (10 + (biome_char as u8 - 'A' as u8)) as 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 + } else { + height_str.parse::().unwrap() + }; } /// Returns a vertex buffer that should be rendered as `TrianglesList`. @@ -78,91 +87,120 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { let mut vertex_data: Vec = Vec::new(); - // TODO: load "all" ground/biomes files - // TODO: use triangle strip - let height_contents = fs::read_to_string("Extensions/LemnosLife/Map/Altis/Ground/13 13.height") + 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 { + let file_name = path + .unwrap() + .path() + .to_str() + .unwrap() + .replace("Extensions/LemnosLife/Map/Altis/Ground/", "") + .replace(".height", ""); + println!("{}", file_name); + // 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 + )) .expect("Unable to load ground file!"); - let biomes_contents = fs::read_to_string("Extensions/LemnosLife/Map/Altis/Biomes/13 13.biomes") + let biomes_contents = fs::read_to_string(&format!( + "Extensions/LemnosLife/Map/Altis/Biomes/{}.biomes", + 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').collect(); - let biomes_lines: Vec<&str> = biomes_contents.split('\n').collect(); + let biomes_lines: Vec<&str> = biomes_contents.split('\n').collect(); + println!("beta"); - let mut index: u32 = 0; + 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(); + 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 biomes_line = biomes_lines[lines_index / 2]; + let biomes_line = biomes_lines[lines_index / 2]; - for columns_index in 0..height_columns.len() - 1 { - let column = height_columns[columns_index].parse::().unwrap(); - let column_right = height_columns[columns_index + 1].parse::().unwrap(); - let column_below = height_next_columns[columns_index].parse::().unwrap(); - let column_below_right = height_next_columns[columns_index + 1].parse::().unwrap(); + for columns_index in 0..height_columns.len() - 1 { + 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 = get_biome(biomes_line, columns_index / 2); - // TODO: compute normals if necessary. + // TODO: compute normals if necessary. - // First triangle. + // First triangle. - vertex_data.push(Vertex { - altitude: column, - normal: [0.0, 0.0, 1.0], - index, - biome, - }); - index += 1; + 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 - }); - 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, - normal: [0.0, 0.0, 1.0], - index, - biome - }); - index += 1; + vertex_data.push(Vertex { + altitude: column_below, + normal: [0.0, 0.0, 1.0], + index, + biome, + }); + index += 1; - // Second triangle. + // Second triangle. - vertex_data.push(Vertex { - altitude: column_below, - normal: [0.0, 0.0, 1.0], - index, - biome - }); - index += 1; + 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_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; + 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 { + break; } } + println!("charlie"); glium::vertex::VertexBuffer::new(display, &vertex_data) .unwrap()