/
sprites-batching.rs
277 lines (227 loc) · 9.56 KB
/
sprites-batching.rs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#[macro_use]
extern crate glium;
use glium::index::PrimitiveType;
use glium::{Display, Surface};
use glutin::surface::WindowSurface;
use support::{ApplicationContext, State};
mod support;
#[derive(Copy, Clone)]
struct Vertex {
i_position: [f32; 2],
i_tex_id: u32,
}
implement_vertex!(Vertex, i_position, i_tex_id);
const SPRITES_COUNT: usize = 1024;
struct Application {
pub vertex_buffer: glium::VertexBuffer<Vertex>,
pub index_buffer: glium::IndexBuffer<u16>,
pub texture: glium::texture::Texture2dArray,
pub program: glium::Program,
}
impl ApplicationContext for Application {
const WINDOW_TITLE:&'static str = "Glium sprites-batching example";
fn new(display: &Display<WindowSurface>) -> Self {
// generating a bunch of unicolor 2D images that will be used for a texture
// we store all of them in a `Texture2dArray`
let texture = {
let images = (0..64)
.map(|_| {
let color1: (f32, f32, f32) = (rand::random(), rand::random(), rand::random());
let color2: (f32, f32, f32) = (rand::random(), rand::random(), rand::random());
vec![vec![color1], vec![color2]]
})
.collect::<Vec<_>>();
glium::texture::Texture2dArray::new(display, images).unwrap()
};
// building the vertex buffer and index buffers that will be filled with the data of
// the sprites
let (vertex_buffer, index_buffer) = {
let mut vb: glium::VertexBuffer<Vertex> =
glium::VertexBuffer::empty_dynamic(display, SPRITES_COUNT * 4).unwrap();
let mut ib_data = Vec::with_capacity(SPRITES_COUNT * 6);
// initializing with random data
for (num, sprite) in vb.map().chunks_mut(4).enumerate() {
let tex_id: u32 = rand::random();
let tex_id = tex_id % texture.get_array_size().unwrap();
let position: (f32, f32) = (rand::random(), rand::random());
let position: (f32, f32) = (position.0 * 2.0 - 1.0, position.1 * 2.0 - 1.0);
sprite[0].i_position[0] = position.0 - 0.1;
sprite[0].i_position[1] = position.1 + 0.1;
sprite[0].i_tex_id = tex_id;
sprite[1].i_position[0] = position.0 + 0.1;
sprite[1].i_position[1] = position.1 + 0.1;
sprite[1].i_tex_id = tex_id;
sprite[2].i_position[0] = position.0 - 0.1;
sprite[2].i_position[1] = position.1 - 0.1;
sprite[2].i_tex_id = tex_id;
sprite[3].i_position[0] = position.0 + 0.1;
sprite[3].i_position[1] = position.1 - 0.1;
sprite[3].i_tex_id = tex_id;
let num = num as u16;
ib_data.push(num * 4);
ib_data.push(num * 4 + 1);
ib_data.push(num * 4 + 2);
ib_data.push(num * 4 + 1);
ib_data.push(num * 4 + 3);
ib_data.push(num * 4 + 2);
}
(
vb,
glium::index::IndexBuffer::new(display, PrimitiveType::TrianglesList, &ib_data)
.unwrap(),
)
};
// we determine the texture coordinates depending on the ID the of vertex
let program = program!(display,
140 => {
vertex: "
#version 140
in vec2 i_position;
in uint i_tex_id;
out vec2 v_tex_coords;
flat out uint v_tex_id;
void main() {
gl_Position = vec4(i_position, 0.0, 1.0);
if (gl_VertexID % 4 == 0) {
v_tex_coords = vec2(0.0, 1.0);
} else if (gl_VertexID % 4 == 1) {
v_tex_coords = vec2(1.0, 1.0);
} else if (gl_VertexID % 4 == 2) {
v_tex_coords = vec2(0.0, 0.0);
} else {
v_tex_coords = vec2(1.0, 0.0);
}
v_tex_id = i_tex_id;
}
",
fragment: "
#version 140
uniform sampler2DArray tex;
in vec2 v_tex_coords;
flat in uint v_tex_id;
out vec4 f_color;
void main() {
f_color = texture(tex, vec3(v_tex_coords, float(v_tex_id)));
}
"
},
110 => {
vertex: "
#version 110
in vec2 i_position;
in uint i_tex_id;
varying vec2 v_tex_coords;
flat varying uint v_tex_id;
void main() {
gl_Position = vec4(i_position, 0.0, 1.0);
if (gl_VertexID % 4 == 0) {
v_tex_coords = vec2(0.0, 1.0);
} else if (gl_VertexID % 4 == 1) {
v_tex_coords = vec2(1.0, 1.0);
} else if (gl_VertexID % 4 == 2) {
v_tex_coords = vec2(0.0, 0.0);
} else {
v_tex_coords = vec2(1.0, 0.0);
}
v_tex_id = i_tex_id;
}
",
fragment: "
#version 110
uniform sampler2DArray tex;
varying vec2 v_tex_coords;
flat varying uint v_tex_id;
void main() {
gl_FragColor = texture2DArray(tex, vec3(v_tex_coords, float(v_tex_id)));
}
"
},
100 => {
vertex: "
#version 100
attribute lowp vec2 i_position;
attribute uint i_tex_id;
varying lowp vec2 v_tex_coords;
flat varying uint v_tex_id;
void main() {
gl_Position = vec4(i_position, 0.0, 1.0);
if (gl_VertexID % 4 == 0) {
v_tex_coords = vec2(0.0, 1.0);
} else if (gl_VertexID % 4 == 1) {
v_tex_coords = vec2(1.0, 1.0);
} else if (gl_VertexID % 4 == 2) {
v_tex_coords = vec2(0.0, 0.0);
} else {
v_tex_coords = vec2(1.0, 0.0);
}
v_tex_id = i_tex_id;
}
",
fragment: "
#version 100
uniform sampler2DArray tex;
varying lowp vec2 v_tex_coords;
flat varying uint v_tex_id;
void main() {
gl_FragColor = texture2DArray(tex, vec3(v_tex_coords, float(v_tex_id)));
}
"
},
)
.unwrap();
Self {
vertex_buffer,
index_buffer,
texture,
program,
}
}
fn draw_frame(&mut self, display: &Display<WindowSurface>) {
let mut frame = display.draw();
// we must only draw the number of sprites that we have written in the vertex buffer
// if you only want to draw 20 sprites for example, you should pass `0 .. 20 * 6` instead
let ib_slice = self.index_buffer.slice(0..SPRITES_COUNT * 6).unwrap();
// drawing a frame
frame.clear_color(0.0, 0.0, 0.0, 0.0);
frame
.draw(
&self.vertex_buffer,
&ib_slice,
&self.program,
&uniform! { tex: &self.texture },
&Default::default(),
)
.unwrap();
frame.finish().unwrap();
}
fn update(&mut self) {
// moving the sprites in a random direction
// in a game, you would typically write the exact positions and texture IDs of your sprites
let mut mapping = self.vertex_buffer.map();
for sprite in mapping.chunks_mut(4) {
let mv: (f32, f32) = (rand::random(), rand::random());
let mv = (mv.0 * 0.01 - 0.005, mv.1 * 0.01 - 0.005);
sprite[0].i_position[0] += mv.0;
sprite[0].i_position[1] += mv.1;
sprite[1].i_position[0] += mv.0;
sprite[1].i_position[1] += mv.1;
sprite[2].i_position[0] += mv.0;
sprite[2].i_position[1] += mv.1;
sprite[3].i_position[0] += mv.0;
sprite[3].i_position[1] += mv.1;
// sprite[...].i_tex_id = ...; // if you want to set the texture
}
}
}
fn main() {
println!(
"This example demonstrates how to draw a lot of sprites in an efficient manner. \n\n\
Instead of drawing sprites one by one, it writes the list of sprites in a buffer \
and draws everything at once. Textures are accessed though a Texture2dArray to \
avoid the problem of binding textures one by one.\n\n\
Performances are limited by the synchronization required to write on the color \
buffer. Enabling depth test would likely increase the framerate.\n"
);
println!("Number of sprites: {}", SPRITES_COUNT);
State::<Application>::run_loop();
}