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
use super::{Filter, Wrap};
use crate::gles::{core30::gl, enums::*};
// use std::rc::Rc;
// pub type TextureId = <Context as HasContext>::Texture;
pub struct Texture {
id: u32,
}
impl Texture {
pub fn new() -> Result<Self, String> {
let id = gl::gen_texture();
Ok(Self { id })
}
pub fn id(&self) -> u32 {
self.id
}
pub fn bind(&self) {
gl::bind_texture(GL_TEXTURE_2D, self.id);
}
pub fn unbind(&self) {
gl::bind_texture(GL_TEXTURE_2D, 0);
}
pub fn init_image(&self, width: u32, height: u32, pixels: Option<&[u8]>) {
match pixels {
Some(pixels) => {
gl::tex_image_2d(
GL_TEXTURE_2D,
0,
GL_RGBA as i32,
width as i32,
height as i32,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
pixels,
);
}
None => {
gl::empty_tex_image_2d(
GL_TEXTURE_2D,
0,
GL_RGBA as i32,
width as i32,
height as i32,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
);
}
}
}
pub fn sub_image(
&self,
offset_x: u32,
offset_y: u32,
width: u32,
height: u32,
pixels: Option<&[u8]>,
) {
match pixels {
Some(pixels) => {
gl::tex_sub_image_2d(
GL_TEXTURE_2D,
0,
offset_x as i32,
offset_y as i32,
width as i32,
height as i32,
GL_RGBA,
GL_UNSIGNED_BYTE,
pixels,
);
}
None => {
gl::empty_tex_sub_image_2d(
GL_TEXTURE_2D,
0,
offset_x as i32,
offset_y as i32,
width as i32,
height as i32,
GL_RGBA,
GL_UNSIGNED_BYTE,
);
}
}
}
pub fn set_filter(&self, filter: Filter) {
gl::tex_parameteri(
GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER,
filter.to_min_flag() as i32,
);
gl::tex_parameteri(
GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER,
filter.to_mag_flag() as i32,
);
}
pub fn generate_mipmap(&self) {
gl::generate_mipmap(GL_TEXTURE_2D);
}
pub fn set_wrap(&self, wrap: Wrap) {
gl::tex_parameteri(
GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S,
wrap.horizontal.to_flag() as i32,
);
gl::tex_parameteri(
GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T,
wrap.vertical.to_flag() as i32,
);
}
}
impl Drop for Texture {
fn drop(&mut self) {
gl::delete_textures(&[self.id]);
}
}
impl PartialEq for Texture {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}