summaryrefslogtreecommitdiff
path: root/engine/src/texture.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-11-27 20:02:08 +0100
committerHampusM <hampus@hampusmat.com>2023-11-27 20:02:08 +0100
commitc230f5aaea3df46ae9a4d7c1c9761e55ef827b82 (patch)
tree9e429a33df6e12f4b2f9adf87d08dad2a0127756 /engine/src/texture.rs
parent935f35455ac2e3547cdd21cd4596538958a7217e (diff)
feat(engine): add lighting maps
Diffstat (limited to 'engine/src/texture.rs')
-rw-r--r--engine/src/texture.rs58
1 files changed, 57 insertions, 1 deletions
diff --git a/engine/src/texture.rs b/engine/src/texture.rs
index f32bc0a..f644b58 100644
--- a/engine/src/texture.rs
+++ b/engine/src/texture.rs
@@ -1,8 +1,9 @@
use std::path::Path;
use image::io::Reader as ImageReader;
-use image::{DynamicImage, ImageError};
+use image::{DynamicImage, EncodableLayout, ImageError, Rgb, RgbImage};
+use crate::color::Color;
use crate::opengl::texture::{PixelDataFormat, Texture as InnerTexture};
use crate::vector::Vec2;
@@ -67,6 +68,39 @@ impl Texture
Ok(me)
}
+ #[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]),
+ );
+
+ let inner = InnerTexture::new();
+
+ inner.bind(|texture_curr_bound| {
+ InnerTexture::generate(
+ &texture_curr_bound,
+ dimensions,
+ image.as_bytes(),
+ PixelDataFormat::Rgb,
+ );
+ });
+
+ let me = Self {
+ inner,
+ 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
+ }
+
pub fn set_wrap(&self, wrapping: Wrapping)
{
self.inner.bind(|texture_curr_bound| {
@@ -126,3 +160,25 @@ pub enum Error
#[error("Unsupported image data kind")]
UnsupportedImageDataKind,
}
+
+/// 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
+ }
+}