Add an arbitrary texture to structures

This commit is contained in:
2022-10-31 20:43:53 +01:00
parent e4ad531a7e
commit b23fa769fa
2 changed files with 21 additions and 17 deletions
+14 -3
View File
@@ -181,7 +181,10 @@ fn main() {
let mut camera = support::camera::CameraState::new(); let mut camera = support::camera::CameraState::new();
let vertex_buffer = support::load_wavefront(&display, &load_bytes!("../Extensions/LemnosLife/Map/Common/Structures/24.obj")); let vertex_buffer = support::load_wavefront(
&display,
load_bytes!("../Extensions/LemnosLife/Map/Common/Structures/24.obj"),
);
let structures_program = program!(&display, let structures_program = program!(&display,
140 => { 140 => {
@@ -193,8 +196,11 @@ fn main() {
in vec3 w_position; in vec3 w_position;
in vec3 position; in vec3 position;
in vec2 texture;
out vec2 v_texture;
void main() { void main() {
v_texture = texture;
gl_Position = persp_matrix * view_matrix * vec4(position + w_position, 1.0); gl_Position = persp_matrix * view_matrix * vec4(position + w_position, 1.0);
} }
", ",
@@ -202,14 +208,18 @@ fn main() {
fragment: " fragment: "
#version 140 #version 140
uniform sampler2D tex;
in vec2 v_texture;
out vec4 f_color; out vec4 f_color;
void main() { void main() {
f_color = vec4(1.0, 1.0, 1.0, 1.0); f_color = texture(tex, v_texture);
} }
", ",
}, },
).unwrap(); )
.unwrap();
let per_instance = vec![ let per_instance = vec![
PerInstance { PerInstance {
@@ -252,6 +262,7 @@ fn main() {
let structures_uniforms = uniform! { let structures_uniforms = uniform! {
persp_matrix: camera.get_perspective(), persp_matrix: camera.get_perspective(),
view_matrix: camera.get_view(), view_matrix: camera.get_view(),
tex: &textures[0],
}; };
// draw parameters // draw parameters
+6 -13
View File
@@ -119,7 +119,7 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
fs::read_to_string(&format!( fs::read_to_string(&format!(
"Extensions/LemnosLife/Map/Altis/{folder}/{file_name}.{extension}" "Extensions/LemnosLife/Map/Altis/{folder}/{file_name}.{extension}"
)) ))
.expect(&format!("Unable to load {} file!", extension)) .unwrap_or_else(|_| panic!("Unable to load {} file!", extension))
}); });
let height_lines: Vec<&str> = height_contents.split('\n').rev().collect(); let height_lines: Vec<&str> = height_contents.split('\n').rev().collect();
@@ -144,7 +144,7 @@ pub fn load_ground(display: &Display) -> VertexBufferAny {
for columns_index in 0..height_columns.len() { for columns_index in 0..height_columns.len() {
let [column, column_below]: [f32; 2] = [&height_columns, &height_next_columns] let [column, column_below]: [f32; 2] = [&height_columns, &height_next_columns]
.map(|height_columns| get_altitude(&height_columns, columns_index)); .map(|height_columns| get_altitude(height_columns, columns_index));
let biome_index = if lines_index % 2 == 0 { let biome_index = if lines_index % 2 == 0 {
columns_index columns_index
@@ -215,17 +215,13 @@ pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny {
for polygon in object.groups.iter().flat_map(|g| g.polys.iter()) { for polygon in object.groups.iter().flat_map(|g| g.polys.iter()) {
match polygon { match polygon {
obj::SimplePolygon(indices) => { obj::SimplePolygon(indices) => {
for i in 0..=2 { for v in indices.iter().take(3) {
let v = indices[i];
let position = data.position[v.0]; let position = data.position[v.0];
let texture = v.1.map(|index| data.texture[index]); let texture = v.1.map(|index| data.texture[index]);
let texture = texture.unwrap_or([0.0, 0.0]); let texture = texture.unwrap_or([0.0, 0.0]);
vertex_data.push(Vertex { vertex_data.push(Vertex { position, texture })
position,
texture,
})
} }
for i in [0, 2, 3] { for i in [0, 2, 3] {
let v = indices[i]; let v = indices[i];
@@ -234,12 +230,9 @@ pub fn load_wavefront(display: &Display, data: &[u8]) -> VertexBufferAny {
let texture = texture.unwrap_or([0.0, 0.0]); let texture = texture.unwrap_or([0.0, 0.0]);
vertex_data.push(Vertex { vertex_data.push(Vertex { position, texture })
position,
texture,
})
} }
}, }
} }
} }
} }