summaryrefslogtreecommitdiff
path: root/engine/src/texture.rs
blob: 2d7ba51bfda996fa508527431ac6786ad610c224 (plain)
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
use std::path::Path;

use image::io::Reader as ImageReader;
use image::{DynamicImage, ImageError, Rgb, RgbImage};

use crate::color::Color;
use crate::opengl::texture::PixelDataFormat;
use crate::vector::Vec2;

mod reexports
{
    pub use crate::opengl::texture::{Filtering, Wrapping};
}

pub use reexports::*;

#[derive(Debug, Clone)]
pub struct Texture
{
    image: DynamicImage,
    pixel_data_format: PixelDataFormat,
    dimensions: Vec2<u32>,
    properties: Properties,
}

impl Texture
{
    /// Opens a texture image.
    ///
    /// # Errors
    /// Will return `Err` if:
    /// - Opening the image fails
    /// - The image data is not 8-bit/color RGB
    #[allow(clippy::new_without_default)]
    pub fn open(path: &Path) -> Result<Self, Error>
    {
        let image = ImageReader::open(path)
            .map_err(Error::OpenImageFailed)?
            .decode()
            .map_err(Error::DecodeImageFailed)?;

        let pixel_data_format = match &image {
            DynamicImage::ImageRgb8(_) => PixelDataFormat::Rgb,
            DynamicImage::ImageRgba8(_) => PixelDataFormat::Rgba,
            _ => {
                return Err(Error::UnsupportedImageDataKind);
            }
        };

        let dimensions = Vec2 { x: image.width(), y: image.height() };

        Ok(Self {
            image,
            pixel_data_format,
            dimensions,
            properties: Properties::default(),
        })
    }

    #[must_use]
    pub fn new_from_color(dimensions: &Vec2<u32>, color: &Color<u8>) -> Self
    {
        let image = RgbImage::from_pixel(
            dimensions.x,
            dimensions.y,
            Rgb([color.red, color.green, color.blue]),
        );

        Self {
            image: image.into(),
            pixel_data_format: PixelDataFormat::Rgb,
            dimensions: dimensions.clone(),
            properties: Properties::default(),
        }
    }

    pub fn properties(&self) -> &Properties
    {
        &self.properties
    }

    pub fn properties_mut(&mut self) -> &mut Properties
    {
        &mut self.properties
    }

    pub fn dimensions(&self) -> &Vec2<u32>
    {
        &self.dimensions
    }

    pub fn pixel_data_format(&self) -> PixelDataFormat
    {
        self.pixel_data_format
    }

    pub fn image(&self) -> &DynamicImage
    {
        &self.image
    }
}

/// Texture error.
#[derive(Debug, thiserror::Error)]
pub enum Error
{
    #[error("Failed to open texture image")]
    OpenImageFailed(#[source] std::io::Error),

    #[error("Failed to decode texture image")]
    DecodeImageFailed(#[source] ImageError),

    #[error("Unsupported image data kind")]
    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
{
    id: u32,
}

impl Id
{
    /// Returns a new texture ID.
    #[must_use]
    pub fn new(id: u32) -> Self
    {
        Self { id }
    }

    pub(crate) fn into_inner(self) -> u32
    {
        self.id
    }
}