diff --git a/src/main.rs b/src/main.rs index 5c90d04..f8188cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,10 +18,15 @@ fn main() { // building the vertex and index buffers let vertex_buffer = support::load_ground(&display); - let image = image::load(Cursor::new(&include_bytes!("../opengl.png")), - image::ImageFormat::Png).unwrap().to_rgba8(); + let image = image::load( + Cursor::new(&include_bytes!("../grassGreen.jpg")), + 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); + let image = + glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions); let texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap(); // the program diff --git a/src/support/mod.rs b/src/support/mod.rs index 7039d58..8cba47d 100644 --- a/src/support/mod.rs +++ b/src/support/mod.rs @@ -3,6 +3,7 @@ use glium::glutin::event::{Event, StartCause}; use glium::glutin::event_loop::{ControlFlow, EventLoop}; use glium::vertex::VertexBufferAny; use glium::{self, Display}; +use std::fs; use std::time::{Duration, Instant}; pub mod camera; @@ -65,43 +66,66 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { implement_vertex!(Vertex, position, normal, tex_coords); - let mut vertex_data = Vec::new(); + let mut vertex_data: Vec = Vec::new(); - vertex_data.push(Vertex { - position: [0.0, 0.0, 0.0], - normal: [0.0, 0.0, 1.0], - tex_coords: [0.0, 0.0], - }); + let contents = fs::read_to_string("Extensions/LemnosLife/Map/Altis/Ground/13 13.height").expect("Unable to load ground file!"); - vertex_data.push(Vertex { - position: [1.0, 0.0, 0.0], - normal: [0.0, 0.0, 1.0], - tex_coords: [1.0, 0.0], - }); + let lines: Vec<&str> = contents.split('\n').collect(); - vertex_data.push(Vertex { - position: [0.0, 0.0, 1.0], - normal: [0.0, 0.0, 1.0], - tex_coords: [0.0, 1.0], - }); + for lines_index in 0..lines.len() - 1 { + let line = lines[lines_index]; + let next_line = lines[lines_index + 1]; + let columns: Vec<&str> = line.split(' ').collect(); + let next_columns: Vec<&str> = next_line.split(' ').collect(); + for columns_index in 0..columns.len() - 1 { + let column = columns[columns_index].parse::().unwrap(); + let column_right = columns[columns_index + 1].parse::().unwrap(); + let column_below = next_columns[columns_index].parse::().unwrap(); + let column_below_right = next_columns[columns_index + 1].parse::().unwrap(); - vertex_data.push(Vertex { - position: [1.0, 0.0, 0.0], - normal: [0.0, 0.0, 1.0], - tex_coords: [1.0, 0.0], - }); + // TODO: normals if necessary. - vertex_data.push(Vertex { - position: [0.0, 0.0, 1.0], - normal: [0.0, 0.0, 1.0], - tex_coords: [0.0, 1.0], - }); + // First triangle. - vertex_data.push(Vertex { - position: [1.0, 0.0, 1.0], - normal: [0.0, 0.0, 1.0], - tex_coords: [1.0, 1.0], - }); + vertex_data.push(Vertex { + position: [lines_index as f32, column, columns_index as f32], + normal: [0.0, 0.0, 1.0], + tex_coords: [0.0, 0.0], + }); + + vertex_data.push(Vertex { + position: [lines_index as f32, column_right, (columns_index + 1) as f32], + normal: [0.0, 0.0, 1.0], + tex_coords: [0.0, 1.0], + }); + + vertex_data.push(Vertex { + position: [(lines_index + 1) as f32, column_below, columns_index as f32], + normal: [0.0, 0.0, 1.0], + tex_coords: [1.0, 0.0], + }); + + // Second triangle. + + vertex_data.push(Vertex { + position: [(lines_index + 1) as f32, column_below_right, (columns_index + 1) as f32], + normal: [0.0, 0.0, 1.0], + tex_coords: [1.0, 1.0], + }); + + vertex_data.push(Vertex { + position: [lines_index as f32, column_right, (columns_index + 1) as f32], + normal: [0.0, 0.0, 1.0], + tex_coords: [0.0, 1.0], + }); + + vertex_data.push(Vertex { + position: [(lines_index + 1) as f32, column_below, columns_index as f32], + normal: [0.0, 0.0, 1.0], + tex_coords: [1.0, 0.0], + }); + } + } glium::vertex::VertexBuffer::new(display, &vertex_data) .unwrap()