base_ui/graphics/
texture.rs1use gl::types::*;
2
3pub struct Texture {
4 id: GLuint,
5}
6
7impl Texture {
8 pub fn new() -> Self {
9 let mut id = 0;
10 unsafe {
11 gl::GenTextures(1, &mut id);
12 }
13 Self { id }
14 }
15
16 pub fn bind(&self) {
17 unsafe {
18 gl::BindTexture(gl::TEXTURE_2D, self.id);
19 }
20 }
21
22 pub fn upload_data(&self, width: i32, height: i32, data: &[u8]) {
23 unsafe {
24 gl::BindTexture(gl::TEXTURE_2D, self.id);
25
26 gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE as i32);
28 gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::CLAMP_TO_EDGE as i32);
29 gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
30 gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
31
32 gl::PixelStorei(gl::UNPACK_ALIGNMENT, 1);
34
35 gl::TexImage2D(
37 gl::TEXTURE_2D,
38 0,
39 gl::RED as i32,
40 width,
41 height,
42 0,
43 gl::RED,
44 gl::UNSIGNED_BYTE,
45 data.as_ptr() as *const _
46 );
47 }
48 }
49}
50
51impl Drop for Texture {
52 fn drop(&mut self) {
53 unsafe {
54 gl::DeleteTextures(1, &self.id);
55 }
56 }
57}