Set the correct texture

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