-
Notifications
You must be signed in to change notification settings - Fork 52
/
opengl.rs
712 lines (641 loc) · 23 KB
/
opengl.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Example of glyph_brush usage with raw OpenGL.
//!
//! Uses instanced rendering with 1 vertex per glyph referencing a 1 byte per pixel texture.
//!
//! Controls
//! * Scroll to size text.
//! * Type to modify text.
//! * Resize window.
//!
//! The main operating structure is as follows:
//! * Load a font
//! * Initialize the brush
//! * Set up the glyph cache texture
//!
//! Per frame:
//! * Queue up Sections of text, containing per-glyph colors and layout information
//! * Process the text into vertices, increasing glyph cache size if necessary
//! * Upload the vertices to the GPU if they've changed, and draw to the screen
use gl::types::*;
use glutin::{
context::PossiblyCurrentContext,
display::GetGlDisplay,
prelude::{GlConfig, GlDisplay, NotCurrentGlContext},
surface::{GlSurface, Surface, WindowSurface},
};
use glutin_winit::GlWindow;
use glyph_brush::{ab_glyph::*, *};
use raw_window_handle::HasWindowHandle;
use std::{
env,
ffi::CString,
io::{self, Write},
mem, ptr, str,
time::Duration,
};
use winit::{
dpi::PhysicalSize,
event::{ElementState, KeyEvent, MouseScrollDelta, WindowEvent},
event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
keyboard::{Key, NamedKey},
window::Window,
};
const TITLE: &str = "glyph_brush opengl example - scroll to size, type to modify";
pub type Res<T> = Result<T, Box<dyn std::error::Error>>;
/// `[left_top * 3, right_bottom * 2, tex_left_top * 2, tex_right_bottom * 2, color * 4]`
pub type Vertex = [GLfloat; 13];
macro_rules! gl_assert_ok {
() => {{
let err = gl::GetError();
assert_eq!(err, gl::NO_ERROR, "{}", gl_err_to_str(err));
}};
}
#[allow(unused)] // it _is_ used
fn main() -> Res<()> {
env_logger::init();
// disables vsync maybe
if env::var_os("vblank_mode").is_none() {
env::set_var("vblank_mode", "0");
}
EventLoop::new()?.run_app(&mut WinitApp::None)?;
Ok(())
}
enum WinitApp {
None,
Resumed(App),
}
impl winit::application::ApplicationHandler for WinitApp {
fn resumed(&mut self, events: &ActiveEventLoop) {
events.set_control_flow(ControlFlow::Poll);
*self = Self::Resumed(App::new(events));
}
fn window_event(
&mut self,
events: &ActiveEventLoop,
_: winit::window::WindowId,
event: WindowEvent,
) {
if let Self::Resumed(app) = self {
app.window_event(events, event);
}
}
fn about_to_wait(&mut self, _events: &ActiveEventLoop) {
if let Self::Resumed(App { window, .. }) = self {
window.request_redraw();
};
}
}
struct App {
glyph_brush: GlyphBrush<Vertex, Extra, FontRef<'static>>,
texture: GlGlyphTexture,
text_pipe: GlTextPipe,
text: String,
font_size: f32,
interval: spin_sleep_util::Interval,
reporter: spin_sleep_util::RateReporter,
max_image_dimension: u32,
dimensions: PhysicalSize<u32>,
gl_surface: Surface<WindowSurface>,
gl_ctx: PossiblyCurrentContext,
window: Window,
}
impl App {
fn new(events: &ActiveEventLoop) -> Self {
let (window, gl_config) = glutin_winit::DisplayBuilder::new()
.with_window_attributes(Some(
winit::window::Window::default_attributes()
.with_inner_size(winit::dpi::PhysicalSize::new(1024, 576))
.with_title(TITLE),
))
.build(events, <_>::default(), |mut configs| {
configs
.find(|c| c.srgb_capable() && c.num_samples() == 0)
.unwrap()
})
.unwrap();
let window = window.unwrap(); // set in display builder
let window_handle = window.window_handle().unwrap();
let gl_display = gl_config.display();
let context_attributes = glutin::context::ContextAttributesBuilder::new()
.with_profile(glutin::context::GlProfile::Core)
.with_context_api(glutin::context::ContextApi::OpenGl(Some(
glutin::context::Version::new(3, 2),
)))
.build(Some(window_handle.as_raw()));
let dimensions = window.inner_size();
let (gl_surface, gl_ctx) = {
let attrs = window.build_surface_attributes(<_>::default()).unwrap();
let surface = unsafe {
gl_display
.create_window_surface(&gl_config, &attrs)
.unwrap()
};
let context = unsafe {
gl_display
.create_context(&gl_config, &context_attributes)
.unwrap()
}
.make_current(&surface)
.unwrap();
(surface, context)
};
// Load the OpenGL function pointers
gl::load_with(|symbol| gl_display.get_proc_address(&CString::new(symbol).unwrap()) as _);
let max_image_dimension = {
let mut value = 0;
unsafe { gl::GetIntegerv(gl::MAX_TEXTURE_SIZE, &mut value) };
value as u32
};
let sans =
FontRef::try_from_slice(include_bytes!("../../fonts/OpenSans-Light.ttf")).unwrap();
let glyph_brush = GlyphBrushBuilder::using_font(sans).build();
let texture = GlGlyphTexture::new(glyph_brush.texture_dimensions());
let text_pipe = GlTextPipe::new(dimensions).unwrap();
Self {
window,
gl_surface,
gl_ctx,
glyph_brush,
texture,
text_pipe,
text: include_str!("text/lipsum.txt").into(),
font_size: 18.0,
interval: spin_sleep_util::interval(Duration::from_secs(1) / 250),
reporter: spin_sleep_util::RateReporter::new(Duration::from_secs(1)),
max_image_dimension,
dimensions,
}
}
fn window_event(&mut self, events: &ActiveEventLoop, event: WindowEvent) {
match event {
WindowEvent::CloseRequested => events.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key,
state: ElementState::Pressed,
..
},
..
} => match logical_key {
Key::Named(NamedKey::Escape) => events.exit(),
Key::Named(NamedKey::Backspace) => {
self.text.pop();
}
key => {
if let Some(str) = key.to_text() {
self.text.push_str(str);
}
}
},
WindowEvent::MouseWheel {
delta: MouseScrollDelta::LineDelta(_, y),
..
} => {
// increase/decrease font size
let old_size = self.font_size;
let mut size = self.font_size;
if y > 0.0 {
size += (size / 4.0).max(2.0)
} else {
size *= 4.0 / 5.0
};
self.font_size = size.clamp(1.0, 2000.0);
if (self.font_size - old_size).abs() > 1e-2 {
eprint!("\r \r");
eprint!("font-size -> {:.1}", self.font_size);
let _ = io::stderr().flush();
}
}
WindowEvent::RedrawRequested => self.draw(),
_ => (),
}
}
fn draw(&mut self) {
let Self {
window,
gl_surface,
gl_ctx,
glyph_brush,
texture,
text_pipe,
text,
font_size,
interval,
reporter,
max_image_dimension,
dimensions,
} = self;
// handle window size changes
let window_size = window.inner_size();
if *dimensions != window_size {
*dimensions = window_size;
window.resize_surface(gl_surface, gl_ctx);
unsafe {
gl::Viewport(0, 0, dimensions.width as _, dimensions.height as _);
}
text_pipe.update_geometry(window_size);
}
let width = dimensions.width as f32;
let height = dimensions.height as _;
let scale = (*font_size * window.scale_factor() as f32).round();
let base_text = Text::new(text).with_scale(scale);
// Queue up all sections of text to be drawn
glyph_brush.queue(
Section::default()
.add_text(base_text.with_color([0.9, 0.3, 0.3, 1.0]))
.with_bounds((width / 3.15, height)),
);
glyph_brush.queue(
Section::default()
.add_text(base_text.with_color([0.3, 0.9, 0.3, 1.0]))
.with_screen_position((width / 2.0, height / 2.0))
.with_bounds((width / 3.15, height))
.with_layout(
Layout::default()
.h_align(HorizontalAlign::Center)
.v_align(VerticalAlign::Center),
),
);
glyph_brush.queue(
Section::default()
.add_text(base_text.with_color([0.3, 0.3, 0.9, 1.0]))
.with_screen_position((width, height))
.with_bounds((width / 3.15, height))
.with_layout(
Layout::default()
.h_align(HorizontalAlign::Right)
.v_align(VerticalAlign::Bottom),
),
);
// Tell glyph_brush to process the queued text
let mut brush_action;
loop {
brush_action = glyph_brush.process_queued(
|rect, tex_data| unsafe {
// Update part of gpu texture with new glyph alpha values
gl::BindTexture(gl::TEXTURE_2D, texture.name);
gl::TexSubImage2D(
gl::TEXTURE_2D,
0,
rect.min[0] as _,
rect.min[1] as _,
rect.width() as _,
rect.height() as _,
gl::RED,
gl::UNSIGNED_BYTE,
tex_data.as_ptr() as _,
);
gl_assert_ok!();
},
to_vertex,
);
// If the cache texture is too small to fit all the glyphs, resize and try again
match brush_action {
Ok(_) => break,
Err(BrushError::TextureTooSmall { suggested, .. }) => {
let (new_width, new_height) = if (suggested.0 > *max_image_dimension
|| suggested.1 > *max_image_dimension)
&& (glyph_brush.texture_dimensions().0 < *max_image_dimension
|| glyph_brush.texture_dimensions().1 < *max_image_dimension)
{
(*max_image_dimension, *max_image_dimension)
} else {
suggested
};
eprint!("\r \r");
eprintln!("Resizing glyph texture -> {new_width}x{new_height}");
// Recreate texture as a larger size to fit more
*texture = GlGlyphTexture::new((new_width, new_height));
glyph_brush.resize_texture(new_width, new_height);
}
}
}
// If the text has changed from what was last drawn, upload the new vertices to GPU
match brush_action.unwrap() {
BrushAction::Draw(vertices) => text_pipe.upload_vertices(&vertices),
BrushAction::ReDraw => {}
}
// Draw the text to the screen
unsafe {
gl::ClearColor(0.02, 0.02, 0.02, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
}
text_pipe.draw();
gl_surface.swap_buffers(gl_ctx).unwrap();
if let Some(rate) = reporter.increment_and_report() {
window.set_title(&format!("{TITLE} {rate:.0} FPS"));
}
interval.tick();
}
}
pub fn gl_err_to_str(err: u32) -> &'static str {
match err {
gl::INVALID_ENUM => "INVALID_ENUM",
gl::INVALID_VALUE => "INVALID_VALUE",
gl::INVALID_OPERATION => "INVALID_OPERATION",
gl::INVALID_FRAMEBUFFER_OPERATION => "INVALID_FRAMEBUFFER_OPERATION",
gl::OUT_OF_MEMORY => "OUT_OF_MEMORY",
gl::STACK_UNDERFLOW => "STACK_UNDERFLOW",
gl::STACK_OVERFLOW => "STACK_OVERFLOW",
_ => "Unknown error",
}
}
pub fn compile_shader(src: &str, ty: GLenum) -> Res<GLuint> {
let shader;
unsafe {
shader = gl::CreateShader(ty);
// Attempt to compile the shader
let c_str = CString::new(src.as_bytes())?;
gl::ShaderSource(shader, 1, &c_str.as_ptr(), ptr::null());
gl::CompileShader(shader);
// Get the compile status
let mut status = GLint::from(gl::FALSE);
gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut status);
// Fail on error
if status != GLint::from(gl::TRUE) {
let mut len = 0;
gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut len);
let mut buf = vec![0; len as usize - 1]; // -1 to skip the trailing null character
gl::GetShaderInfoLog(
shader,
len,
ptr::null_mut(),
buf.as_mut_ptr() as *mut GLchar,
);
return Err(str::from_utf8(&buf)?.into());
}
}
Ok(shader)
}
pub fn link_program(vs: GLuint, fs: GLuint) -> Res<GLuint> {
unsafe {
let program = gl::CreateProgram();
gl::AttachShader(program, vs);
gl::AttachShader(program, fs);
gl::LinkProgram(program);
// Get the link status
let mut status = GLint::from(gl::FALSE);
gl::GetProgramiv(program, gl::LINK_STATUS, &mut status);
// Fail on error
if status != GLint::from(gl::TRUE) {
let mut len: GLint = 0;
gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut len);
let mut buf = vec![0; len as usize - 1]; // -1 to skip the trailing null character
gl::GetProgramInfoLog(
program,
len,
ptr::null_mut(),
buf.as_mut_ptr() as *mut GLchar,
);
return Err(str::from_utf8(&buf)?.into());
}
Ok(program)
}
}
#[inline]
pub fn to_vertex(
glyph_brush::GlyphVertex {
mut tex_coords,
pixel_coords,
bounds,
extra,
}: glyph_brush::GlyphVertex,
) -> Vertex {
let gl_bounds = bounds;
let mut gl_rect = Rect {
min: point(pixel_coords.min.x, pixel_coords.min.y),
max: point(pixel_coords.max.x, pixel_coords.max.y),
};
// handle overlapping bounds, modify uv_rect to preserve texture aspect
if gl_rect.max.x > gl_bounds.max.x {
let old_width = gl_rect.width();
gl_rect.max.x = gl_bounds.max.x;
tex_coords.max.x = tex_coords.min.x + tex_coords.width() * gl_rect.width() / old_width;
}
if gl_rect.min.x < gl_bounds.min.x {
let old_width = gl_rect.width();
gl_rect.min.x = gl_bounds.min.x;
tex_coords.min.x = tex_coords.max.x - tex_coords.width() * gl_rect.width() / old_width;
}
if gl_rect.max.y > gl_bounds.max.y {
let old_height = gl_rect.height();
gl_rect.max.y = gl_bounds.max.y;
tex_coords.max.y = tex_coords.min.y + tex_coords.height() * gl_rect.height() / old_height;
}
if gl_rect.min.y < gl_bounds.min.y {
let old_height = gl_rect.height();
gl_rect.min.y = gl_bounds.min.y;
tex_coords.min.y = tex_coords.max.y - tex_coords.height() * gl_rect.height() / old_height;
}
[
gl_rect.min.x,
gl_rect.max.y,
extra.z,
gl_rect.max.x,
gl_rect.min.y,
tex_coords.min.x,
tex_coords.max.y,
tex_coords.max.x,
tex_coords.min.y,
extra.color[0],
extra.color[1],
extra.color[2],
extra.color[3],
]
}
#[rustfmt::skip]
pub fn ortho(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> [f32; 16] {
let tx = -(right + left) / (right - left);
let ty = -(top + bottom) / (top - bottom);
let tz = -(far + near) / (far - near);
[
2.0 / (right - left), 0.0, 0.0, 0.0,
0.0, 2.0 / (top - bottom), 0.0, 0.0,
0.0, 0.0, -2.0 / (far - near), 0.0,
tx, ty, tz, 1.0,
]
}
/// The texture used to cache drawn glyphs
pub struct GlGlyphTexture {
pub name: GLuint,
}
impl GlGlyphTexture {
pub fn new((width, height): (u32, u32)) -> Self {
let mut name = 0;
unsafe {
// Create a texture for the glyphs
// The texture holds 1 byte per pixel as alpha data
gl::PixelStorei(gl::UNPACK_ALIGNMENT, 1);
gl::GenTextures(1, &mut name);
gl::BindTexture(gl::TEXTURE_2D, name);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as _);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as _);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as _);
gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as _);
gl::TexImage2D(
gl::TEXTURE_2D,
0,
gl::RED as _,
width as _,
height as _,
0,
gl::RED,
gl::UNSIGNED_BYTE,
ptr::null(),
);
gl_assert_ok!();
Self { name }
}
}
pub fn clear(&self) {
unsafe {
gl::BindTexture(gl::TEXTURE_2D, self.name);
gl::ClearTexImage(
self.name,
0,
gl::RED,
gl::UNSIGNED_BYTE,
[12_u8].as_ptr() as _,
);
gl_assert_ok!();
}
}
}
impl Drop for GlGlyphTexture {
fn drop(&mut self) {
unsafe {
gl::DeleteTextures(1, &self.name);
}
}
}
pub struct GlTextPipe {
shaders: [GLuint; 2],
program: GLuint,
vao: GLuint,
vbo: GLuint,
transform_uniform: GLint,
vertex_count: usize,
vertex_buffer_len: usize,
}
impl GlTextPipe {
pub fn new(window_size: winit::dpi::PhysicalSize<u32>) -> Res<Self> {
let (w, h) = (window_size.width as f32, window_size.height as f32);
let vs = compile_shader(include_str!("shader/text.vs"), gl::VERTEX_SHADER)?;
let fs = compile_shader(include_str!("shader/text.fs"), gl::FRAGMENT_SHADER)?;
let program = link_program(vs, fs)?;
let mut vao = 0;
let mut vbo = 0;
let transform_uniform = unsafe {
// Create Vertex Array Object
gl::GenVertexArrays(1, &mut vao);
gl::BindVertexArray(vao);
// Create a Vertex Buffer Object
gl::GenBuffers(1, &mut vbo);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
// Use shader program
gl::UseProgram(program);
gl::BindFragDataLocation(program, 0, CString::new("out_color")?.as_ptr());
// Specify the layout of the vertex data
let uniform = gl::GetUniformLocation(program, CString::new("transform")?.as_ptr());
if uniform < 0 {
return Err(format!("GetUniformLocation(\"transform\") -> {uniform}").into());
}
let transform = ortho(0.0, w, 0.0, h, 1.0, -1.0);
gl::UniformMatrix4fv(uniform, 1, 0, transform.as_ptr());
let mut offset = 0;
for (v_field, float_count) in &[
("left_top", 3),
("right_bottom", 2),
("tex_left_top", 2),
("tex_right_bottom", 2),
("color", 4),
] {
let attr = gl::GetAttribLocation(program, CString::new(*v_field)?.as_ptr());
if attr < 0 {
return Err(format!("{v_field} GetAttribLocation -> {attr}").into());
}
gl::VertexAttribPointer(
attr as _,
*float_count,
gl::FLOAT,
gl::FALSE as _,
mem::size_of::<Vertex>() as _,
offset as _,
);
gl::EnableVertexAttribArray(attr as _);
gl::VertexAttribDivisor(attr as _, 1); // Important for use with DrawArraysInstanced
offset += float_count * 4;
}
gl_assert_ok!();
uniform
};
Ok(Self {
shaders: [vs, fs],
program,
vao,
vbo,
transform_uniform,
vertex_count: 0,
vertex_buffer_len: 0,
})
}
pub fn upload_vertices(&mut self, vertices: &[Vertex]) {
// Draw new vertices
self.vertex_count = vertices.len();
unsafe {
gl::BindVertexArray(self.vao);
gl::BindBuffer(gl::ARRAY_BUFFER, self.vbo);
if self.vertex_buffer_len < self.vertex_count {
gl::BufferData(
gl::ARRAY_BUFFER,
(self.vertex_count * mem::size_of::<Vertex>()) as GLsizeiptr,
vertices.as_ptr() as _,
gl::DYNAMIC_DRAW,
);
self.vertex_buffer_len = self.vertex_count;
} else {
gl::BufferSubData(
gl::ARRAY_BUFFER,
0,
(self.vertex_count * mem::size_of::<Vertex>()) as GLsizeiptr,
vertices.as_ptr() as _,
);
}
gl_assert_ok!();
}
}
pub fn update_geometry(&self, window_size: winit::dpi::PhysicalSize<u32>) {
let (w, h) = (window_size.width as f32, window_size.height as f32);
let transform = ortho(0.0, w, 0.0, h, 1.0, -1.0);
unsafe {
gl::UseProgram(self.program);
gl::UniformMatrix4fv(self.transform_uniform, 1, 0, transform.as_ptr());
gl_assert_ok!();
}
}
pub fn draw(&self) {
unsafe {
// Enabled alpha blending
gl::Enable(gl::BLEND);
gl::BlendFunc(gl::SRC_ALPHA, gl::ONE);
// Use srgb for consistency with other examples
gl::Enable(gl::FRAMEBUFFER_SRGB);
gl::UseProgram(self.program);
gl::BindVertexArray(self.vao);
// If implementing this yourself, make sure to set VertexAttribDivisor as well
gl::DrawArraysInstanced(gl::TRIANGLE_STRIP, 0, 4, self.vertex_count as _);
gl_assert_ok!();
}
}
}
impl Drop for GlTextPipe {
fn drop(&mut self) {
unsafe {
gl::DeleteProgram(self.program);
self.shaders.iter().for_each(|s| gl::DeleteShader(*s));
gl::DeleteBuffers(1, &self.vbo);
gl::DeleteVertexArrays(1, &self.vao);
}
}
}