summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2016-07-06 22:38:52 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2016-07-06 22:38:52 -0400
commita72cfc1d2e9382f14ca2e06b46323f1431c967dd (patch)
tree60fb1c496e4a86462580e78bdc60f6e504a8ef57
parent13c9b28e8a1e61032ae651b5b2f685dfbd1f5867 (diff)
1.6 Textures.
-rw-r--r--rust/opengl/Cargo.toml2
-rw-r--r--rust/opengl/src/main.rs49
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 <kjetil.orbekk@gmail.com>"]
[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();