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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#[macro_use]
extern crate glium;
mod support;
#[allow(unused_imports)]
use glium::{glutin, Surface};
use glium::index::PrimitiveType;
fn main() {
let event_loop = glutin::event_loop::EventLoop::new();
let wb = glutin::window::WindowBuilder::new();
let cb = glutin::ContextBuilder::new();
let display = glium::Display::new(wb, cb, &event_loop).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,
&[
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] },
]
).unwrap()
};
// building the index buffer
let index_buffer = glium::IndexBuffer::new(&display, PrimitiveType::TrianglesList,
&[0u16, 1, 2]).unwrap();
// 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);
}
",
},
100 => {
vertex: "
#version 100
uniform lowp mat4 matrix;
attribute lowp vec2 position;
attribute lowp vec3 color;
varying lowp vec3 vColor;
void main() {
gl_Position = vec4(position, 0.0, 1.0) * matrix;
vColor = color;
}
",
fragment: "
#version 100
varying lowp vec3 vColor;
void main() {
gl_FragColor = vec4(vColor, 1.0);
}
",
},
).unwrap();
// Here we draw the black background and triangle to the screen using the previously
// initialised resources.
//
// In this case we use a closure for simplicity, however keep in mind that most serious
// applications should probably use a function that takes the resources as an argument.
let draw = move || {
// 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, &Default::default()).unwrap();
target.finish().unwrap();
};
// Draw the triangle to the screen.
draw();
// the main loop
event_loop.run(move |event, _, control_flow| {
*control_flow = match event {
glutin::event::Event::WindowEvent { event, .. } => match event {
// Break from the main loop when the window is closed.
glutin::event::WindowEvent::CloseRequested => glutin::event_loop::ControlFlow::Exit,
// Redraw the triangle when the window is resized.
glutin::event::WindowEvent::Resized(..) => {
draw();
glutin::event_loop::ControlFlow::Poll
},
_ => glutin::event_loop::ControlFlow::Poll,
},
_ => glutin::event_loop::ControlFlow::Poll,
};
});
}