From a72cfc1d2e9382f14ca2e06b46323f1431c967dd Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Wed, 6 Jul 2016 22:38:52 -0400 Subject: 1.6 Textures. --- rust/opengl/Cargo.toml | 2 +- rust/opengl/src/main.rs | 49 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/rust/opengl/Cargo.toml b/rust/opengl/Cargo.toml index 6407830..8c14875 100644 --- a/rust/opengl/Cargo.toml +++ b/rust/opengl/Cargo.toml @@ -5,4 +5,4 @@ authors = ["Kjetil Orbekk "] [dependencies] glium = "0.14.0" - +image = "*" diff --git a/rust/opengl/src/main.rs b/rust/opengl/src/main.rs index 29ee2ec..20834ca 100644 --- a/rust/opengl/src/main.rs +++ b/rust/opengl/src/main.rs @@ -1,9 +1,17 @@ #[macro_use] extern crate glium; +extern crate image; + +use std::io::Cursor; +use glium::DisplayBuild; +use glium::Surface; fn main() { - use glium::DisplayBuild; - use glium::Surface; + let image = image::load(Cursor::new(&include_bytes!("data/image.png")[..]), + image::PNG).unwrap().to_rgba(); + let image_dimensions = image.dimensions(); + let image = glium::texture::RawImage2d::from_raw_rgba_reversed( + image.into_raw(), image_dimensions); let display = glium::glutin::WindowBuilder::new() .with_dimensions(1024, 768) @@ -11,16 +19,19 @@ fn main() { .build_glium() .unwrap(); + let texture = glium::texture::Texture2d::new(&display, image).unwrap(); + #[derive(Copy, Clone)] struct Vertex { position: [f32; 2], + tex_coords: [f32; 2], } - implement_vertex!(Vertex, position); + implement_vertex!(Vertex, position, tex_coords); - let vertex1 = Vertex { position: [-0.5, -0.5] }; - let vertex2 = Vertex { position: [ 0.0, 0.5] }; - let vertex3 = Vertex { position: [ 0.5, -0.25] }; + let vertex1 = Vertex { position: [-0.5, -0.5], tex_coords: [0.0, 0.0] }; + let vertex2 = Vertex { position: [ 0.0, 0.5], tex_coords: [0.5, 1.0] }; + let vertex3 = Vertex { position: [ 0.5, -0.25], tex_coords: [1.0, 0.0]}; let shape = vec![vertex1, vertex2, vertex3]; let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap(); @@ -29,20 +40,22 @@ fn main() { let vertex_shader_src = r#" #version 140 in vec2 position; - out vec2 my_attr; + in vec2 tex_coords; + out vec2 v_tex_coords; uniform mat4 trans; void main() { - my_attr = position; + v_tex_coords = tex_coords; gl_Position = trans * vec4(position, 0.0, 1.0); } "#; let fragment_shader_src = r#" #version 140 - in vec2 my_attr; + in vec2 v_tex_coords; out vec4 color; + uniform sampler2D tex; void main() { - color = vec4(my_attr, 0.0, 1.0); + color = texture(tex, v_tex_coords); } "#; @@ -52,17 +65,23 @@ fn main() { let mut t: f32 = -0.5; loop { t += 0.002; - if t > 0.5 { + if t > 1.5 { t = -0.5; } + let u: f32 = if t > 0.5 { + 1.0 - t + } else { + t + }; let uniforms = uniform! { trans: [ - [t.cos(), t.sin(), 0.0, 0.0], - [-t.sin(), t.cos(), 0.0, 0.0], + [u.cos(), u.sin(), 0.0, 0.0], + [-u.sin(), u.cos(), 0.0, 0.0], [1.0, 0.0, 1.0, 0.0], - [t, 0.0, 0.0, 1.0f32], - ] + [u, 0.0, 0.0, 1.0f32], + ], + tex: &texture, }; let mut target = display.draw(); -- cgit v1.2.3