diff --git a/Cargo.toml b/Cargo.toml index 1e458d8..b99720b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" [dependencies] glium = "0.32.1" +image = "0.24.4" glutin = "0.29.1" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 13d2ca1..5c90d04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ #[macro_use] extern crate glium; +use std::io::Cursor; + #[allow(unused_imports)] use glium::{glutin, Surface}; @@ -16,6 +18,12 @@ 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_dimensions = 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 let program = program!(&display, 140 => { @@ -27,12 +35,16 @@ fn main() { in vec3 position; in vec3 normal; + in vec2 tex_coords; + out vec3 v_position; out vec3 v_normal; + out vec2 v_tex_coords; void main() { v_position = position; v_normal = normal; + v_tex_coords = tex_coords; gl_Position = persp_matrix * view_matrix * vec4(v_position * 0.005, 1.0); } ", @@ -41,14 +53,16 @@ fn main() { #version 140 in vec3 v_normal; + in vec2 v_tex_coords; out vec4 f_color; + uniform sampler2D tex; + const vec3 LIGHT = vec3(0.0, -1.0, 0.0); void main() { float lum = max(dot(normalize(v_normal), normalize(LIGHT)), 0.0); - vec3 color = (0.3 + 0.7 * lum) * vec3(1.0, 1.0, 1.0); - f_color = vec4(color, 1.0); + f_color = (0.3 + 0.7 * lum, 1.0) * texture(tex, v_tex_coords); } " }, @@ -65,6 +79,7 @@ fn main() { let uniforms = uniform! { persp_matrix: camera.get_perspective(), view_matrix: camera.get_view(), + tex: &texture, }; // draw parameters diff --git a/src/support/mod.rs b/src/support/mod.rs index eaa5ca6..7b270b6 100644 --- a/src/support/mod.rs +++ b/src/support/mod.rs @@ -60,47 +60,47 @@ pub fn load_ground(display: &Display) -> VertexBufferAny { struct Vertex { position: [f32; 3], normal: [f32; 3], - texture: [f32; 2], + tex_coords: [f32; 2], } - implement_vertex!(Vertex, position, normal, texture); + implement_vertex!(Vertex, position, normal, tex_coords); let mut vertex_data = Vec::new(); vertex_data.push(Vertex { position: [0.0, 0.0, 0.0], normal: [0.0, 0.0, 1.0], - texture: [0.0, 0.0], + tex_coords: [0.0, 0.0], }); vertex_data.push(Vertex { position: [1.0, 0.0, 0.0], normal: [0.0, 0.0, 1.0], - texture: [0.0, 0.0], + tex_coords: [1.0, 0.0], }); vertex_data.push(Vertex { position: [0.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0], - texture: [0.0, 0.0], + tex_coords: [0.0, 1.0], }); vertex_data.push(Vertex { position: [1.0, 0.0, 0.0], normal: [0.0, 0.0, 1.0], - texture: [0.0, 0.0], + tex_coords: [1.0, 0.0], }); vertex_data.push(Vertex { position: [0.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0], - texture: [0.0, 0.0], + tex_coords: [0.0, 1.0], }); vertex_data.push(Vertex { position: [1.0, 0.0, 1.0], normal: [0.0, 0.0, 1.0], - texture: [0.0, 0.0], + tex_coords: [1.0, 1.0], }); /*for object in data.objects.iter() {