From 87c9093a4e6aee31d9e62ef4f543c46dd36c054d Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Tue, 1 Nov 2022 01:15:35 +0100 Subject: [PATCH] Set the correct texture --- src/main.rs | 225 ++++++++++++++++++++++++++------------------- src/support/mod.rs | 61 +++++++----- 2 files changed, 168 insertions(+), 118 deletions(-) diff --git a/src/main.rs b/src/main.rs index aefb44e..e0c0aaf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,86 +103,87 @@ const STRUCTURES_TEXTURES: [&str; STRUCTURES_TEXTURES_NUMBER] = [ "cargo.jpg", ]; +// This approach works as there isn't file names having several file extensions. // `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, +static STRUCTURES_TEXTURES_REVERSED: phf::Map<&'static str, u32> = phf_map! { + "redNoise" => 0, + "cargoLightBlue" => 1, + "beigeNoise" => 2, + "wallStone" => 3, + "cargoWhite" => 4, + "b_ficusc2d_f" => 5, + "basketball court" => 6, + "zinc" => 7, + "solarPowerPlant" => 8, + "cargoSand" => 9, + "water" => 10, + "solarPowerPlantAccumulator" => 11, + "blueNoise" => 12, + "30kmh" => 13, + "white" => 14, + "blackYellow" => 15, + "stopText" => 16, + "cargoOrange" => 17, + "redWhite" => 18, + "grid" => 19, + "blueTile" => 20, + "blueWhiteNoise" => 21, + "reinforcedConcrete" => 22, + "log" => 23, + "green" => 24, + "fanWallGrey" => 25, + "cargoCyan" => 26, + "glass" => 27, + "wallGrey" => 28, + "greenGate" => 29, + "greyNoise" => 30, + "cattail" => 31, + "palmLayer" => 32, + "cargoLightGreen" => 33, + "palm" => 34, + "branch1" => 35, + "cargoYellow" => 36, + "blackNoise" => 37, + "pipes" => 38, + "whiteTile" => 39, + "darkBlueNoise" => 40, + "planks" => 41, + "trunk" => 42, + "greenNoise" => 43, + "wallBlue" => 44, + "stop" => 45, + "wallGreyATM" => 46, + "iron bars" => 47, + "tile" => 48, + "horizontalBrick" => 49, + "steel" => 50, + "yellowBars" => 51, + "whiteDoor" => 52, + "cargoBlue" => 53, + "barbed" => 54, + "woodBlue" => 55, + "bamboo" => 56, + "b_ficusc1s_f" => 57, + "whiteNoise" => 58, + "fanWall" => 59, + "wood" => 60, + "crossover" => 61, + "gridHexa" => 62, + "lightBlue" => 63, + "helipad" => 64, + "binBarrel" => 65, + "brick" => 66, + "wallBlueATM" => 67, + "yellow" => 68, + "blueDoor" => 69, + "parking" => 70, + "cargoGrey" => 71, + "haystack" => 72, + "cargoRed" => 73, + "yellowNoise" => 74, + "gridFull" => 75, + "rust" => 76, + "cargo" => 77, }; const BIOMES_NUMBER: usize = 19; @@ -288,7 +289,7 @@ fn main() { BIOMES_NUMBER - 1 ); - let fragment = &format!( + let ground_fragment = &format!( " #version 140 @@ -308,6 +309,52 @@ fn main() { uniform_sampler_2d_str, switch_v_biome_str ); + let uniform_sampler_2d_str = (0..STRUCTURES_TEXTURES_NUMBER) + .map(|i| format!("tex{}", i)) + .collect::>() + .join(", "); + + let switch_v_texture_index_str = (0..STRUCTURES_TEXTURES_NUMBER) + .map(|i| { + format!( + " + case {}u: + f_color = texture(tex{}, v_texture_coordinates); + break; + ", + i, i + ) + }) + .collect::>() + .join("") + + &format!( + " + default: // case {}u: + f_color = texture(tex{}, v_texture_coordinates); + ", + STRUCTURES_TEXTURES_NUMBER - 1, + STRUCTURES_TEXTURES_NUMBER - 1 + ); + + let structures_fragment = &format!( + " + #version 140 + + in vec2 v_texture_coordinates; + flat in uint v_texture_index; + out vec4 f_color; + + uniform sampler2D {}; + + void main() {{ + switch (v_texture_index) {{ + {} + }} + }} + ", + uniform_sampler_2d_str, switch_v_texture_index_str + ); + // the program let ground_program = program!(&display, 140 => { @@ -364,7 +411,7 @@ fn main() { } ", - fragment: fragment, + fragment: ground_fragment, }, ) .unwrap(); @@ -387,27 +434,19 @@ fn main() { in vec3 w_position; in float w_rotation; in vec3 position; + in uint texture_index; in vec2 texture_coordinates; + flat out uint v_texture_index; out vec2 v_texture_coordinates; void main() { + v_texture_index = texture_index; v_texture_coordinates = texture_coordinates; gl_Position = persp_matrix * view_matrix * vec4(position + w_position, 1.0); } ", - fragment: " - #version 140 - - uniform sampler2D tex5; - - in vec2 v_texture_coordinates; - out vec4 f_color; - - void main() { - f_color = texture(tex5, v_texture_coordinates); - } - ", + fragment: structures_fragment, }, ) .unwrap(); diff --git a/src/support/mod.rs b/src/support/mod.rs index 4cd23c2..d5f6874 100644 --- a/src/support/mod.rs +++ b/src/support/mod.rs @@ -1,4 +1,5 @@ #![allow(dead_code)] +use crate::STRUCTURES_TEXTURES_REVERSED; use glium::glutin::event::{Event, StartCause}; use glium::glutin::event_loop::{ControlFlow, EventLoop}; use glium::vertex::VertexBufferAny; @@ -213,35 +214,45 @@ pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny { let mut vertex_data = Vec::new(); for object in data.objects.iter() { - for polygon in object.groups.iter().flat_map(|g| g.polys.iter()) { - match polygon { - obj::SimplePolygon(indices) => { - for v in indices.iter().take(3) { - let position = data.position[v.0]; - let texture_coordinates = v.1.map(|index| data.texture[index]); + for group in object.groups.iter() { + for poly in &group.polys { + match poly { + obj::SimplePolygon(indices) => { + for v in indices.iter().take(3) { + let position = data.position[v.0]; + let texture_coordinates = data.texture[v.1.unwrap()]; - let texture_coordinates = texture_coordinates.unwrap_or([0.0, 0.0]); - let texture_index = 5; + let texture_index = if let obj::ObjMaterial::Ref(texture) = group.material.as_ref().unwrap() + { + STRUCTURES_TEXTURES_REVERSED[texture] + } else { + panic!() + }; - 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_coordinates = v.1.map(|index| data.texture[index]); + 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_coordinates = data.texture[v.1.unwrap()]; - let texture_coordinates = texture_coordinates.unwrap_or([0.0, 0.0]); - let texture_index = 5; + let texture_index = if let obj::ObjMaterial::Ref(texture) = group.material.as_ref().unwrap() + { + STRUCTURES_TEXTURES_REVERSED[texture] + } else { + panic!() + }; - vertex_data.push(Vertex { - position, - texture_index, - texture_coordinates, - }) + vertex_data.push(Vertex { + position, + texture_index, + texture_coordinates, + }) + } } } }