moai_core/gl/
texture.rs

1use std::ffi::c_void;
2
3// TODO: Make a way to set the configuration for the texture
4pub unsafe fn gen_texture() -> u32{
5    // Generate texture and set id
6    let mut texture_id = 0;
7    gl::GenTextures(1, &mut texture_id);
8    gl::BindTexture(gl::TEXTURE_2D, texture_id);
9
10    // Set the configuration for the texture
11    gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::REPEAT as i32);
12    gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::REPEAT as i32);
13    gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR_MIPMAP_LINEAR as i32);
14    gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
15
16    return texture_id;
17}
18
19pub unsafe fn set_texture_data(texture_id: u32, data: &[u8], dimensions: (u32, u32), mipmap: bool) {
20    gl::BindTexture(gl::TEXTURE_2D, texture_id);
21    gl::TexImage2D(gl::TEXTURE_2D,
22        0,
23        gl::RGB as i32,
24        dimensions.0 as i32,
25        dimensions.1 as i32,
26        0,
27        gl::RGB,
28        gl::UNSIGNED_BYTE,
29        &data[0] as *const u8 as *const c_void);
30    if mipmap {gl::GenerateMipmap(gl::TEXTURE_2D)};
31}