From 697e29e5ad4a09110180736294b8a1330a13eb6e Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 1 Mar 2024 19:34:09 +0100 Subject: refactor(engine): create texture OpenGL objects when needed --- engine/src/texture.rs | 119 ++++++++++++++++++++------------------------------ 1 file changed, 47 insertions(+), 72 deletions(-) (limited to 'engine/src/texture.rs') diff --git a/engine/src/texture.rs b/engine/src/texture.rs index f644b58..2d7ba51 100644 --- a/engine/src/texture.rs +++ b/engine/src/texture.rs @@ -1,10 +1,10 @@ use std::path::Path; use image::io::Reader as ImageReader; -use image::{DynamicImage, EncodableLayout, ImageError, Rgb, RgbImage}; +use image::{DynamicImage, ImageError, Rgb, RgbImage}; use crate::color::Color; -use crate::opengl::texture::{PixelDataFormat, Texture as InnerTexture}; +use crate::opengl::texture::PixelDataFormat; use crate::vector::Vec2; mod reexports @@ -14,12 +14,13 @@ mod reexports pub use reexports::*; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Texture { - inner: InnerTexture, + image: DynamicImage, pixel_data_format: PixelDataFormat, dimensions: Vec2, + properties: Properties, } impl Texture @@ -46,26 +47,14 @@ impl Texture } }; - let inner = InnerTexture::new(); - let dimensions = Vec2 { x: image.width(), y: image.height() }; - inner.bind(|texture_curr_bound| { - InnerTexture::generate( - &texture_curr_bound, - &dimensions, - image.as_bytes(), - pixel_data_format, - ); - }); - - let me = Self { inner, pixel_data_format, dimensions }; - - me.set_wrap(Wrapping::Repeat); - me.set_magnifying_filter(Filtering::Linear); - me.set_minifying_filter(Filtering::Nearest); - - Ok(me) + Ok(Self { + image, + pixel_data_format, + dimensions, + properties: Properties::default(), + }) } #[must_use] @@ -77,73 +66,37 @@ impl Texture Rgb([color.red, color.green, color.blue]), ); - let inner = InnerTexture::new(); - - inner.bind(|texture_curr_bound| { - InnerTexture::generate( - &texture_curr_bound, - dimensions, - image.as_bytes(), - PixelDataFormat::Rgb, - ); - }); - - let me = Self { - inner, + Self { + image: image.into(), pixel_data_format: PixelDataFormat::Rgb, dimensions: dimensions.clone(), - }; - - me.set_wrap(Wrapping::Repeat); - me.set_magnifying_filter(Filtering::Linear); - me.set_minifying_filter(Filtering::Nearest); - - me + properties: Properties::default(), + } } - pub fn set_wrap(&self, wrapping: Wrapping) + pub fn properties(&self) -> &Properties { - self.inner.bind(|texture_curr_bound| { - InnerTexture::set_wrap(texture_curr_bound, wrapping); - }); + &self.properties } - pub fn set_magnifying_filter(&self, filtering: Filtering) + pub fn properties_mut(&mut self) -> &mut Properties { - self.inner.bind(|texture_curr_bound| { - InnerTexture::set_magnifying_filter(texture_curr_bound, filtering); - }); + &mut self.properties } - pub fn set_minifying_filter(&self, filtering: Filtering) + pub fn dimensions(&self) -> &Vec2 { - self.inner.bind(|texture_curr_bound| { - InnerTexture::set_minifying_filter(texture_curr_bound, filtering); - }); + &self.dimensions } - pub(crate) fn inner(&self) -> &InnerTexture + pub fn pixel_data_format(&self) -> PixelDataFormat { - &self.inner + self.pixel_data_format } -} -impl Clone for Texture -{ - fn clone(&self) -> Self + pub fn image(&self) -> &DynamicImage { - let inner = self.inner.copy( - &self.dimensions, - self.pixel_data_format, - &Vec2::ZERO, - &Vec2::ZERO, - ); - - Self { - inner, - pixel_data_format: self.pixel_data_format, - dimensions: self.dimensions.clone(), - } + &self.image } } @@ -161,6 +114,28 @@ pub enum Error UnsupportedImageDataKind, } +/// Texture properties +#[derive(Debug, Clone)] +#[non_exhaustive] +pub struct Properties +{ + pub wrap: Wrapping, + pub magnifying_filter: Filtering, + pub minifying_filter: Filtering, +} + +impl Default for Properties +{ + fn default() -> Self + { + Self { + wrap: Wrapping::Repeat, + magnifying_filter: Filtering::Linear, + minifying_filter: Filtering::Nearest, + } + } +} + /// Texture ID. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Id -- cgit v1.2.3-18-g5258