diff --git a/Cargo.toml b/Cargo.toml index 0f1735a..3f68a36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ glium = "0.32.1" image = "0.24.4" glutin = "0.29.1" obj = "0.10.2" +phf = { version = "0.11.1", features = ["macros"]} load_file = "1.0.1" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6279ba2..aefb44e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ extern crate glium; extern crate load_file; use glium::texture::SrgbTexture2d; +use phf::phf_map; use std::io::Cursor; use std::time::Instant; @@ -20,6 +21,170 @@ struct PerInstance { } implement_vertex!(PerInstance, w_position, w_rotation); +const STRUCTURES_TEXTURES_NUMBER: usize = 78; +const STRUCTURES_TEXTURES: [&str; STRUCTURES_TEXTURES_NUMBER] = [ + "redNoise.jpg", + "cargoLightBlue.jpg", + "beigeNoise.jpg", + "wallStone.jpg", + "cargoWhite.jpg", + "b_ficusc2d_f.png", + "basketball court.jpg", + "zinc.jpg", + "solarPowerPlant.png", + "cargoSand.jpg", + "water.jpg", + "solarPowerPlantAccumulator.jpg", + "blueNoise.jpg", + "30kmh.jpg", + "white.jpg", + "blackYellow.jpg", + "stopText.jpg", + "cargoOrange.jpg", + "redWhite.jpg", + "grid.png", + "blueTile.jpg", + "blueWhiteNoise.jpg", + "reinforcedConcrete.jpg", + "log.jpg", + "green.jpg", + "fanWallGrey.jpg", + "cargoCyan.jpg", + "glass.png", + "wallGrey.jpg", + "greenGate.png", + "greyNoise.jpg", + "cattail.png", + "palmLayer.png", + "cargoLightGreen.jpg", + "palm.png", + "branch1.png", + "cargoYellow.jpg", + "blackNoise.jpg", + "pipes.jpg", + "whiteTile.jpg", + "darkBlueNoise.jpg", + "planks.png", + "trunk.jpg", + "greenNoise.jpg", + "wallBlue.jpg", + "stop.jpg", + "wallGreyATM.jpg", + "iron bars.png", + "tile.jpg", + "horizontalBrick.jpg", + "steel.jpg", + "yellowBars.png", + "whiteDoor.jpg", + "cargoBlue.jpg", + "barbed.png", + "woodBlue.jpg", + "bamboo.png", + "b_ficusc1s_f.png", + "whiteNoise.jpg", + "fanWall.jpg", + "wood.jpg", + "crossover.jpg", + "gridHexa.png", + "lightBlue.jpg", + "helipad.png", + "binBarrel.jpg", + "brick.jpg", + "wallBlueATM.jpg", + "yellow.jpg", + "blueDoor.jpg", + "parking.jpg", + "cargoGrey.jpg", + "haystack.jpg", + "cargoRed.jpg", + "yellowNoise.jpg", + "gridFull.jpg", + "rust.jpg", + "cargo.jpg", +]; + +// `phf` doesn't propose any `bimap`. +static STRUCTURES_TEXTURES_REVERSED: phf::Map<&'static str, usize> = phf_map! { + "redNoise.jpg" => 0, + "cargoLightBlue.jpg" => 1, + "beigeNoise.jpg" => 2, + "wallStone.jpg" => 3, + "cargoWhite.jpg" => 4, + "b_ficusc2d_f.png" => 5, + "basketball court.jpg" => 6, + "zinc.jpg" => 7, + "solarPowerPlant.png" => 8, + "cargoSand.jpg" => 9, + "water.jpg" => 10, + "solarPowerPlantAccumulator.jpg" => 11, + "blueNoise.jpg" => 12, + "30kmh.jpg" => 13, + "white.jpg" => 14, + "blackYellow.jpg" => 15, + "stopText.jpg" => 16, + "cargoOrange.jpg" => 17, + "redWhite.jpg" => 18, + "grid.png" => 19, + "blueTile.jpg" => 20, + "blueWhiteNoise.jpg" => 21, + "reinforcedConcrete.jpg" => 22, + "log.jpg" => 23, + "green.jpg" => 24, + "fanWallGrey.jpg" => 25, + "cargoCyan.jpg" => 26, + "glass.png" => 27, + "wallGrey.jpg" => 28, + "greenGate.png" => 29, + "greyNoise.jpg" => 30, + "cattail.png" => 31, + "palmLayer.png" => 32, + "cargoLightGreen.jpg" => 33, + "palm.png" => 34, + "branch1.png" => 35, + "cargoYellow.jpg" => 36, + "blackNoise.jpg" => 37, + "pipes.jpg" => 38, + "whiteTile.jpg" => 39, + "darkBlueNoise.jpg" => 40, + "planks.png" => 41, + "trunk.jpg" => 42, + "greenNoise.jpg" => 43, + "wallBlue.jpg" => 44, + "stop.jpg" => 45, + "wallGreyATM.jpg" => 46, + "iron bars.png" => 47, + "tile.jpg" => 48, + "horizontalBrick.jpg" => 49, + "steel.jpg" => 50, + "yellowBars.png" => 51, + "whiteDoor.jpg" => 52, + "cargoBlue.jpg" => 53, + "barbed.png" => 54, + "woodBlue.jpg" => 55, + "bamboo.png" => 56, + "b_ficusc1s_f.png" => 57, + "whiteNoise.jpg" => 58, + "fanWall.jpg" => 59, + "wood.jpg" => 60, + "crossover.jpg" => 61, + "gridHexa.png" => 62, + "lightBlue.jpg" => 63, + "helipad.png" => 64, + "binBarrel.jpg" => 65, + "brick.jpg" => 66, + "wallBlueATM.jpg" => 67, + "yellow.jpg" => 68, + "blueDoor.jpg" => 69, + "parking.jpg" => 70, + "cargoGrey.jpg" => 71, + "haystack.jpg" => 72, + "cargoRed.jpg" => 73, + "yellowNoise.jpg" => 74, + "gridFull.jpg" => 75, + "rust.jpg" => 76, + "cargo.jpg" => 77, +}; + const BIOMES_NUMBER: usize = 19; const BIOMES: [&str; BIOMES_NUMBER] = [ "dirt", @@ -55,7 +220,7 @@ fn main() { // building the vertex and index buffers let ground_vertex_buffer = support::load_ground(&display); - let textures: [SrgbTexture2d; BIOMES_NUMBER] = BIOMES.map(|biome| { + let ground_textures: [SrgbTexture2d; BIOMES_NUMBER] = BIOMES.map(|biome| { println!("{}", biome); let image = image::load( Cursor::new(&load_bytes!(&format!( @@ -72,6 +237,30 @@ fn main() { glium::texture::SrgbTexture2d::new(&display, image).unwrap() }); + let structures_textures: [SrgbTexture2d; STRUCTURES_TEXTURES_NUMBER] = + STRUCTURES_TEXTURES.map(|structure_texture| { + println!("{}", structure_texture); + let image = image::load( + Cursor::new(&load_bytes!(&format!( + "../Extensions/LemnosLife/Assets/Downloads/{}", + structure_texture + ))), + if structure_texture.ends_with(".png") { + image::ImageFormat::Png + } else { + image::ImageFormat::Jpeg + }, + ) + .unwrap() + .to_rgba8(); + let image_dimensions = image.dimensions(); + let image = glium::texture::RawImage2d::from_raw_rgba_reversed( + &image.into_raw(), + image_dimensions, + ); + glium::texture::SrgbTexture2d::new(&display, image).unwrap() + }); + let uniform_sampler_2d_str = (0..BIOMES_NUMBER) .map(|i| format!("tex{}", i)) .collect::>() @@ -198,11 +387,11 @@ fn main() { in vec3 w_position; in float w_rotation; in vec3 position; - in vec2 texture; - out vec2 v_texture; + in vec2 texture_coordinates; + out vec2 v_texture_coordinates; void main() { - v_texture = texture; + v_texture_coordinates = texture_coordinates; gl_Position = persp_matrix * view_matrix * vec4(position + w_position, 1.0); } ", @@ -210,13 +399,13 @@ fn main() { fragment: " #version 140 - uniform sampler2D tex; + uniform sampler2D tex5; - in vec2 v_texture; + in vec2 v_texture_coordinates; out vec4 f_color; void main() { - f_color = texture(tex, v_texture); + f_color = texture(tex5, v_texture_coordinates); } ", }, @@ -242,35 +431,112 @@ fn main() { let ground_uniforms = uniform! { persp_matrix: camera.get_perspective(), view_matrix: camera.get_view(), - tex0: &textures[0], - tex1: &textures[1], - tex2: &textures[2], - tex3: &textures[3], - tex4: &textures[4], - tex5: &textures[5], - tex6: &textures[6], - tex7: &textures[7], - tex8: &textures[8], - tex9: &textures[9], - tex10: &textures[10], - tex11: &textures[11], - tex12: &textures[12], - tex13: &textures[13], - tex14: &textures[14], - tex15: &textures[15], - tex16: &textures[16], - tex17: &textures[17], - tex18: &textures[18], + tex0: &ground_textures[0], + tex1: &ground_textures[1], + tex2: &ground_textures[2], + tex3: &ground_textures[3], + tex4: &ground_textures[4], + tex5: &ground_textures[5], + tex6: &ground_textures[6], + tex7: &ground_textures[7], + tex8: &ground_textures[8], + tex9: &ground_textures[9], + tex10: &ground_textures[10], + tex11: &ground_textures[11], + tex12: &ground_textures[12], + tex13: &ground_textures[13], + tex14: &ground_textures[14], + tex15: &ground_textures[15], + tex16: &ground_textures[16], + tex17: &ground_textures[17], + tex18: &ground_textures[18], }; let structures_uniforms = uniform! { persp_matrix: camera.get_perspective(), view_matrix: camera.get_view(), - tex: &textures[0], + tex0: &structures_textures[0], + tex1: &structures_textures[1], + tex2: &structures_textures[2], + tex3: &structures_textures[3], + tex4: &structures_textures[4], + tex5: &structures_textures[5], + tex6: &structures_textures[6], + tex7: &structures_textures[7], + tex8: &structures_textures[8], + tex9: &structures_textures[9], + tex10: &structures_textures[10], + tex11: &structures_textures[11], + tex12: &structures_textures[12], + tex13: &structures_textures[13], + tex14: &structures_textures[14], + tex15: &structures_textures[15], + tex16: &structures_textures[16], + tex17: &structures_textures[17], + tex18: &structures_textures[18], + tex19: &structures_textures[19], + tex20: &structures_textures[20], + tex21: &structures_textures[21], + tex22: &structures_textures[22], + tex23: &structures_textures[23], + tex24: &structures_textures[24], + tex25: &structures_textures[25], + tex26: &structures_textures[26], + tex27: &structures_textures[27], + tex28: &structures_textures[28], + tex29: &structures_textures[29], + tex30: &structures_textures[30], + tex31: &structures_textures[31], + tex32: &structures_textures[32], + tex33: &structures_textures[33], + tex34: &structures_textures[34], + tex35: &structures_textures[35], + tex36: &structures_textures[36], + tex37: &structures_textures[37], + tex38: &structures_textures[38], + tex39: &structures_textures[39], + tex40: &structures_textures[40], + tex41: &structures_textures[41], + tex42: &structures_textures[42], + tex43: &structures_textures[43], + tex44: &structures_textures[44], + tex45: &structures_textures[45], + tex46: &structures_textures[46], + tex47: &structures_textures[47], + tex48: &structures_textures[48], + tex49: &structures_textures[49], + tex50: &structures_textures[50], + tex51: &structures_textures[51], + tex52: &structures_textures[52], + tex53: &structures_textures[53], + tex54: &structures_textures[54], + tex55: &structures_textures[55], + tex56: &structures_textures[56], + tex57: &structures_textures[57], + tex58: &structures_textures[58], + tex59: &structures_textures[59], + tex60: &structures_textures[60], + tex61: &structures_textures[61], + tex62: &structures_textures[62], + tex63: &structures_textures[63], + tex64: &structures_textures[64], + tex65: &structures_textures[65], + tex66: &structures_textures[66], + tex67: &structures_textures[67], + tex68: &structures_textures[68], + tex69: &structures_textures[69], + tex70: &structures_textures[70], + tex71: &structures_textures[71], + tex72: &structures_textures[72], + tex73: &structures_textures[73], + tex74: &structures_textures[74], + tex75: &structures_textures[75], + tex76: &structures_textures[76], + tex77: &structures_textures[77], }; // draw parameters - let params = glium::DrawParameters { + let ground_params = glium::DrawParameters { depth: glium::Depth { test: glium::DepthTest::IfLess, write: true, @@ -281,6 +547,9 @@ fn main() { ..Default::default() }; + let mut structures_params = ground_params.clone(); + structures_params.blend = glium::draw_parameters::Blend::alpha_blending(); + let per_instance_buffer = glium::vertex::VertexBuffer::new(&display, &per_instance).unwrap(); @@ -294,7 +563,7 @@ fn main() { &glium::index::NoIndices(glium::index::PrimitiveType::TriangleStrip), &ground_program, &ground_uniforms, - ¶ms, + &ground_params, ) .unwrap(); @@ -304,7 +573,7 @@ fn main() { &glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList), &structures_program, &structures_uniforms, - ¶ms, + &structures_params, ) .unwrap(); diff --git a/src/support/mod.rs b/src/support/mod.rs index c3cf91f..4cd23c2 100644 --- a/src/support/mod.rs +++ b/src/support/mod.rs @@ -201,10 +201,11 @@ pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny { #[derive(Copy, Clone)] struct Vertex { position: [f32; 3], - texture: [f32; 2], + texture_index: u32, + texture_coordinates: [f32; 2], } - implement_vertex!(Vertex, position, texture); + implement_vertex!(Vertex, position, texture_index, texture_coordinates); let mut data = ::std::io::BufReader::new(data); let data = obj::ObjData::load_buf(&mut data).unwrap(); @@ -217,20 +218,30 @@ pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny { obj::SimplePolygon(indices) => { for v in indices.iter().take(3) { let position = data.position[v.0]; - let texture = v.1.map(|index| data.texture[index]); + let texture_coordinates = v.1.map(|index| data.texture[index]); - let texture = texture.unwrap_or([0.0, 0.0]); + let texture_coordinates = texture_coordinates.unwrap_or([0.0, 0.0]); + let texture_index = 5; - vertex_data.push(Vertex { position, texture }) + vertex_data.push(Vertex { + position, + texture_index, + texture_coordinates, + }) } for i in [0, 2, 3] { let v = indices[i]; let position = data.position[v.0]; - let texture = v.1.map(|index| data.texture[index]); + let texture_coordinates = v.1.map(|index| data.texture[index]); - let texture = texture.unwrap_or([0.0, 0.0]); + let texture_coordinates = texture_coordinates.unwrap_or([0.0, 0.0]); + let texture_index = 5; - vertex_data.push(Vertex { position, texture }) + vertex_data.push(Vertex { + position, + texture_index, + texture_coordinates, + }) } } }