1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#[macro_use]
extern crate glium;
extern crate image;
use std::io::Cursor;
use glium::DisplayBuild;
use glium::Surface;
fn main() {
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)
.with_title(format!("Hello world"))
.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, tex_coords);
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();
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
let vertex_shader_src = r#"
#version 140
in vec2 position;
in vec2 tex_coords;
out vec2 v_tex_coords;
uniform mat4 trans;
void main() {
v_tex_coords = tex_coords;
gl_Position = trans * vec4(position, 0.0, 1.0);
}
"#;
let fragment_shader_src = r#"
#version 140
in vec2 v_tex_coords;
out vec4 color;
uniform sampler2D tex;
void main() {
color = texture(tex, v_tex_coords);
}
"#;
let program = glium::Program::from_source(
&display, vertex_shader_src, fragment_shader_src, None).unwrap();
let mut t: f32 = -0.5;
loop {
t += 0.002;
if t > 1.5 {
t = -0.5;
}
let u: f32 = if t > 0.5 {
1.0 - t
} else {
t
};
let uniforms = uniform! {
trans: [
[u.cos(), u.sin(), 0.0, 0.0],
[-u.sin(), u.cos(), 0.0, 0.0],
[1.0, 0.0, 1.0, 0.0],
[u, 0.0, 0.0, 1.0f32],
],
tex: &texture,
};
let mut target = display.draw();
target.clear_color(0.0, 0.0, 0.0, 1.0);
target.draw(&vertex_buffer, &indices, &program, &uniforms,
&Default::default()).unwrap();
target.finish().unwrap();
for ev in display.poll_events() {
match ev {
glium::glutin::Event::Closed => return,
_ => ()
}
}
}
}
|