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
|
use glow::HasContext;
use image::EncodableLayout;
use crate::context;
pub struct Texture {
pub tex: glow::Texture,
}
impl Texture {
pub fn new(ctx: &context::Context, bytes: &[u8]) -> Self {
let rgba = image::io::Reader::new(std::io::Cursor::new(bytes))
.with_guessed_format()
.expect("failed to guess image format")
.decode()
.expect("failed to decode image")
.into_rgba8();
let pixels = rgba.as_bytes();
unsafe {
let tex = ctx.gl.create_texture().expect("failed to create texture");
ctx.gl.bind_texture(glow::TEXTURE_2D, Some(tex));
ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE as i32);
ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE as i32);
ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, glow::NEAREST as i32);
ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::NEAREST as i32);
ctx.gl.tex_image_2d(
glow::TEXTURE_2D,
0,
glow::RGBA as i32,
rgba.width() as i32,
rgba.height() as i32,
0,
glow::RGBA,
glow::UNSIGNED_BYTE,
Some(pixels),
);
ctx.gl.generate_mipmap(glow::TEXTURE_2D);
Self {
tex,
}
}
}
pub fn set_linear_filtering(&self, ctx: &context::Context) {
unsafe {
ctx.gl.bind_texture(glow::TEXTURE_2D, Some(self.tex));
ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, glow::NEAREST_MIPMAP_NEAREST as i32);
ctx.gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::NEAREST_MIPMAP_NEAREST as i32);
}
}
pub fn bind(&self, ctx: &context::Context) {
unsafe {
ctx.gl.active_texture(glow::TEXTURE0);
ctx.gl.bind_texture(glow::TEXTURE_2D, Some(self.tex));
}
}
}
|