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
use crate::graphics::{
GLCore,
core::GLError,
core::texture::{
TextureBindTarget,
Texture2DRewriteTarget,
Texture2DWriteTarget,
Texture2DInternalFormat,
ImageDataFormat,
TextureParameterTarget,
TextureMinFilter,
TextureMagFilter,
TextureCompareFunction,
TextureCompareMode,
TextureWrap,
},
};
use core::mem::MaybeUninit;
pub struct Texture{
id:u32,
}
impl Texture{
/// Generates a texture.
pub fn generate()->Texture{
unsafe{
let mut id=MaybeUninit::uninit().assume_init();
GLCore.texture.generate_one(&mut id);
Self{
id,
}
}
}
/// Generates a texture with the given target.
///
/// Returns `GLError::InvalidValue` if there's no current context.
pub fn create(target:TextureBindTarget)->Result<Texture,GLError>{
let texture=Texture::generate();
let error=texture.bind(target);
if error.is_error(){
Err(error)
}
else{
Ok(texture)
}
}
/// Binds a texture to a texturing target.
///
/// When a texture is bound to a target,
/// the previous binding for that target is automatically broken.
///
/// Returns `GLError::NoError` if no error has accured.
///
/// Returns `GLError::InvalidValue` if there's no current context.
///
/// Returns `GLError::InvalidOperation`
/// if texture was previously created with a target that doesn't match that of target.
pub fn bind(&self,target:TextureBindTarget)->GLError{
unsafe{
GLCore.texture.bind(target,self.id);
GLCore.get_error()
}
}
/// Binds the zero-named texture to a texturing target.
///
/// When a texture is bound to a target,
/// the previous binding for that target is automatically broken.
///
/// Returns `GLError::NoError` if no error has accured.
///
/// Returns `GLError::InvalidValue` if there's no current context.
pub fn unbind(target:TextureBindTarget)->GLError{
unsafe{
GLCore.texture.bind(target,0);
GLCore.get_error()
}
}
#[inline(always)]
pub fn id(&self)->u32{
self.id
}
}
impl Texture{
/// Specifies the index of the lowest defined mipmap level.
///
/// The initial value is 0.
#[inline(always)]
pub fn set_base_level(target:TextureParameterTarget,level:i32){
unsafe{
GLCore.texture.set_base_level(target,level)
}
}
/// Specifies the comparison operator.
///
/// The comparison operator is used when `TEXTURE_COMPARE_MODE` is set to `COMPARE_REF_TO_TEXTURE`.
#[inline(always)]
pub fn set_compare_function(&self,target:TextureParameterTarget,function:TextureCompareFunction){
unsafe{
GLCore.texture.set_compare_function(target,function)
}
}
/// Specifies the texture comparison mode for currently bound depth textures (the iternal format = `DEPTH_COMPONENT`).
#[inline(always)]
pub fn set_compare_mode(&self,target:TextureParameterTarget,mode:TextureCompareMode){
unsafe{
GLCore.texture.set_compare_mode(target,mode)
}
}
/// Specifies the texture magnification function.
///
/// The texture magnification function is used whenever the level-of-detail function used
/// when sampling from the texture determines that the texture should be magified.
///
/// Initially, it is set to `TextureMagFilter::Linear`.
#[inline(always)]
pub fn set_mag_filter(target:TextureParameterTarget,filter:TextureMagFilter){
unsafe{
GLCore.texture.set_mag_filter(target,filter)
}
}
/// Specifies the texture minifying function.
///
/// The texture minifying function is used whenever the level-of-detail function used
/// when sampling from the texture determines that the texture should be minified.
///
/// Initially, it is set to `TextureMinFilter::LinearMipmapLinear`.
#[inline(always)]
pub fn set_min_filter(target:TextureParameterTarget,filter:TextureMinFilter){
unsafe{
GLCore.texture.set_min_filter(target,filter);
}
}
/// Sets the wrap parameter for texture coordinate `s`.
///
/// Initially, it is set to `TextureWrap::Repeat`.
#[inline(always)]
pub fn set_wrap_s(target:TextureParameterTarget,value:TextureWrap){
unsafe{
GLCore.texture.set_wrap_s(target,value);
}
}
/// Sets the wrap parameter for texture coordinate `t`.
///
/// Initially, it is set to `TextureWrap::Repeat`.
#[inline(always)]
pub fn set_wrap_t(target:TextureParameterTarget,value:TextureWrap){
unsafe{
GLCore.texture.set_wrap_t(target,value);
}
}
/// Sets the wrap parameter for texture coordinate `r`.
///
/// Initially, it is set to `TextureWrap::Repeat`.
#[inline(always)]
pub fn set_wrap_r(target:TextureParameterTarget,value:TextureWrap){
unsafe{
GLCore.texture.set_wrap_r(target,value);
}
}
}
impl Texture{
pub fn rewrite_image_2d(
target:Texture2DRewriteTarget,
mipmap_level:i32,
texture_internal_format:Texture2DInternalFormat,
size:[i32;2],
image_data_format:ImageDataFormat,
data:&[u8]
)->GLError{
unsafe{
let data_ref=if data.len()!=0{
(data as *const [u8]) as *const core::ffi::c_void
}
else{
0 as *const core::ffi::c_void
};
GLCore.texture.rewrite_image_2d(
target,
mipmap_level,
texture_internal_format,
size,
image_data_format,
data_ref
);
GLCore.get_error()
}
}
pub fn write_image_2d(
target:Texture2DWriteTarget,
mipmap_level:i32,
[x,y,width,height]:[i32;4],
image_data_format:ImageDataFormat,
data:&[u8]
)->GLError{
unsafe{
let data_ref=if data.len()!=0{
(data as *const [u8]) as *const core::ffi::c_void
}
else{
0 as *const core::ffi::c_void
};
GLCore.texture.write_image_2d(
target,
mipmap_level,
[x,y,width,height],
image_data_format,
data_ref
);
GLCore.get_error()
}
}
}
impl Drop for Texture{
fn drop(&mut self){
unsafe{
GLCore.texture.delete_one(&self.id)
}
}
}