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
|
use crate::data_types::dimens::Dimens;
use crate::texture::Properties;
#[derive(Debug)]
pub struct Texture
{
texture: gl::types::GLuint,
}
impl Texture
{
pub fn new() -> Self
{
let mut texture = gl::types::GLuint::default();
unsafe {
gl::CreateTextures(gl::TEXTURE_2D, 1, &mut texture);
};
Self { texture }
}
pub fn bind(&self)
{
unsafe {
gl::BindTexture(gl::TEXTURE_2D, self.texture);
}
}
pub fn generate(
&mut self,
dimens: Dimens<u32>,
data: &[u8],
pixel_data_format: PixelDataFormat,
)
{
self.alloc_image(pixel_data_format, dimens, data);
unsafe {
gl::GenerateTextureMipmap(self.texture);
}
}
pub fn apply_properties(&mut self, properties: &Properties)
{
self.set_wrap(properties.wrap);
self.set_magnifying_filter(properties.magnifying_filter);
self.set_minifying_filter(properties.minifying_filter);
}
pub fn set_wrap(&mut self, wrapping: Wrapping)
{
let wrapping_gl = wrapping.to_gl();
#[allow(clippy::cast_possible_wrap)]
unsafe {
gl::TextureParameteri(self.texture, gl::TEXTURE_WRAP_S, wrapping_gl as i32);
gl::TextureParameteri(self.texture, gl::TEXTURE_WRAP_T, wrapping_gl as i32);
}
}
pub fn set_magnifying_filter(&mut self, filtering: Filtering)
{
let filtering_gl = filtering.to_gl();
#[allow(clippy::cast_possible_wrap)]
unsafe {
gl::TextureParameteri(
self.texture,
gl::TEXTURE_MAG_FILTER,
filtering_gl as i32,
);
}
}
pub fn set_minifying_filter(&mut self, filtering: Filtering)
{
let filtering_gl = filtering.to_gl();
#[allow(clippy::cast_possible_wrap)]
unsafe {
gl::TextureParameteri(
self.texture,
gl::TEXTURE_MIN_FILTER,
filtering_gl as i32,
);
}
}
fn alloc_image(
&mut self,
pixel_data_format: PixelDataFormat,
dimens: Dimens<u32>,
data: &[u8],
)
{
unsafe {
#[allow(clippy::cast_possible_wrap)]
gl::TextureStorage2D(
self.texture,
1,
pixel_data_format.to_sized_internal_format(),
dimens.width as i32,
dimens.height as i32,
);
#[allow(clippy::cast_possible_wrap)]
gl::TextureSubImage2D(
self.texture,
0,
0,
0,
dimens.width as i32,
dimens.height as i32,
pixel_data_format.to_format(),
gl::UNSIGNED_BYTE,
data.as_ptr().cast(),
);
}
}
}
impl Drop for Texture
{
fn drop(&mut self)
{
unsafe {
gl::DeleteTextures(1, &self.texture);
}
}
}
/// Texture wrapping.
#[derive(Debug, Clone, Copy)]
pub enum Wrapping
{
Repeat,
MirroredRepeat,
ClampToEdge,
ClampToBorder,
}
impl Wrapping
{
fn to_gl(self) -> gl::types::GLenum
{
match self {
Self::Repeat => gl::REPEAT,
Self::MirroredRepeat => gl::MIRRORED_REPEAT,
Self::ClampToEdge => gl::CLAMP_TO_EDGE,
Self::ClampToBorder => gl::CLAMP_TO_BORDER,
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum Filtering
{
Nearest,
Linear,
}
impl Filtering
{
fn to_gl(self) -> gl::types::GLenum
{
match self {
Self::Linear => gl::LINEAR,
Self::Nearest => gl::NEAREST,
}
}
}
/// Texture pixel data format.
#[derive(Debug, Clone, Copy)]
pub enum PixelDataFormat
{
Rgb8,
Rgba8,
}
impl PixelDataFormat
{
fn to_sized_internal_format(self) -> gl::types::GLenum
{
match self {
Self::Rgb8 => gl::RGB8,
Self::Rgba8 => gl::RGBA8,
}
}
fn to_format(self) -> gl::types::GLenum
{
match self {
Self::Rgb8 => gl::RGB,
Self::Rgba8 => gl::RGBA,
}
}
}
pub fn set_active_texture_unit(texture_unit: TextureUnit)
{
unsafe {
gl::ActiveTexture(texture_unit.into_gl());
}
}
macro_rules! texture_unit_enum {
(cnt=$cnt: literal) => {
seq_macro::seq!(N in 0..$cnt {
#[derive(Debug, Clone, Copy)]
pub enum TextureUnit {
#(
No~N,
)*
}
impl TextureUnit {
fn into_gl(self) -> gl::types::GLenum {
match self {
#(
Self::No~N => gl::TEXTURE~N,
)*
}
}
pub fn from_num(num: usize) -> Option<Self> {
match num {
#(
N => Some(Self::No~N),
)*
_ => None
}
}
}
});
};
}
texture_unit_enum!(cnt = 31);
|