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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
extern crate glutin;
#[macro_use]
extern crate glium;
mod support;
use glium::Surface;
fn main() {
use glium::DisplayBuild;
// building the display, ie. the main object
let display = glutin::WindowBuilder::new()
.build_glium()
.unwrap();
// building the vertex buffer, which contains all the vertices that we will draw
let vertex_buffer = {
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 2],
color: [f32; 3],
}
implement_vertex!(Vertex, position, color);
glium::VertexBuffer::new(&display,
vec![
Vertex { position: [-0.5, -0.5], color: [0.0, 1.0, 0.0] },
Vertex { position: [ 0.0, 0.5], color: [0.0, 0.0, 1.0] },
Vertex { position: [ 0.5, -0.5], color: [1.0, 0.0, 0.0] },
]
)
};
// building the index buffer
let index_buffer = glium::IndexBuffer::new(&display,
glium::index::TrianglesList(vec![0u16, 1, 2]));
// compiling shaders and linking them together
let program = program!(&display,
140 => {
vertex: "
#version 140
uniform mat4 matrix;
in vec2 position;
in vec3 color;
out vec3 vColor;
void main() {
gl_Position = vec4(position, 0.0, 1.0) * matrix;
vColor = color;
}
",
fragment: "
#version 140
in vec3 vColor;
out vec4 f_color;
void main() {
f_color = vec4(vColor, 1.0);
}
"
},
110 => {
vertex: "
#version 110
uniform mat4 matrix;
attribute vec2 position;
attribute vec3 color;
varying vec3 vColor;
void main() {
gl_Position = vec4(position, 0.0, 1.0) * matrix;
vColor = color;
}
",
fragment: "
#version 110
varying vec3 vColor;
void main() {
gl_FragColor = vec4(vColor, 1.0);
}
",
},
).unwrap();
// the main loop
support::start_loop(|| {
// building the uniforms
let uniforms = uniform! {
matrix: [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
]
};
// drawing a frame
let mut target = display.draw();
target.clear_color(0.0, 0.0, 0.0, 0.0);
target.draw(&vertex_buffer, &index_buffer, &program, &uniforms, &std::default::Default::default()).unwrap();
target.finish();
// polling and handling the events received by the window
for event in display.poll_events() {
match event {
glutin::Event::Closed => return support::Action::Stop,
_ => ()
}
}
support::Action::Continue
});
}