diff options
author | HampusM <hampus@hampusmat.com> | 2023-11-12 13:40:49 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-11-12 13:40:49 +0100 |
commit | 67023d6a095f457a2579367d59d13c6c804e7108 (patch) | |
tree | c4ddf7d960daf6976e834df18f480bac5fddc75c | |
parent | 7d01458a6ea591f1d6215c6c9b247410807ed60d (diff) |
feat(engine): add support for textures with 8-bit rgba data
-rw-r--r-- | engine/src/opengl/texture.rs | 30 | ||||
-rw-r--r-- | engine/src/texture.rs | 13 |
2 files changed, 36 insertions, 7 deletions
diff --git a/engine/src/opengl/texture.rs b/engine/src/opengl/texture.rs index c7bf75b..68ac050 100644 --- a/engine/src/opengl/texture.rs +++ b/engine/src/opengl/texture.rs @@ -32,18 +32,23 @@ impl Texture cb(currently_bound); } - pub fn generate(_: &CurrentlyBound<Self>, dimens: &Vec2<u32>, data: &[u8]) + pub fn generate( + _: &CurrentlyBound<Self>, + dimens: &Vec2<u32>, + data: &[u8], + pixel_data_format: PixelDataFormat, + ) { #[allow(clippy::cast_possible_wrap)] unsafe { gl::TexImage2D( gl::TEXTURE_2D, 0, - gl::RGB as i32, + pixel_data_format.to_gl() as i32, dimens.x as i32, dimens.y as i32, 0, - gl::RGB, + pixel_data_format.to_gl(), gl::UNSIGNED_BYTE, data.as_ptr().cast(), ); @@ -142,3 +147,22 @@ impl Filtering } } } + +/// Texture pixel data format. +#[derive(Debug, Clone, Copy)] +pub enum PixelDataFormat +{ + Rgb, + Rgba, +} + +impl PixelDataFormat +{ + fn to_gl(self) -> gl::types::GLenum + { + match self { + Self::Rgb => gl::RGB, + Self::Rgba => gl::RGBA, + } + } +} diff --git a/engine/src/texture.rs b/engine/src/texture.rs index 7874df4..bde3cd2 100644 --- a/engine/src/texture.rs +++ b/engine/src/texture.rs @@ -3,7 +3,7 @@ use std::path::Path; use image::io::Reader as ImageReader; use image::{DynamicImage, ImageError}; -use crate::opengl::texture::Texture as InnerTexture; +use crate::opengl::texture::{PixelDataFormat, Texture as InnerTexture}; use crate::vector::Vec2; mod reexports @@ -35,9 +35,13 @@ impl Texture .decode() .map_err(Error::DecodeImageFailed)?; - if !matches!(image, DynamicImage::ImageRgb8(_)) { - return Err(Error::UnsupportedImageDataKind); - } + let pixel_data_format = match &image { + DynamicImage::ImageRgb8(_) => PixelDataFormat::Rgb, + DynamicImage::ImageRgba8(_) => PixelDataFormat::Rgba, + _ => { + return Err(Error::UnsupportedImageDataKind); + } + }; let inner = InnerTexture::new(); @@ -46,6 +50,7 @@ impl Texture &texture_curr_bound, &Vec2 { x: image.width(), y: image.height() }, image.as_bytes(), + pixel_data_format, ); }); |