ft: texture loading err handling

now uses a default placeholder texture + colour source
This commit is contained in:
2026-09-13 21:01:47 +02:00
parent 5c6bf98f3e
commit f1f0bee4cf
6 changed files with 83 additions and 55 deletions

View File

@@ -11,6 +11,12 @@ pub enum ColourSource {
Texture(Texture),
}
impl Default for ColourSource {
fn default() -> Self {
Self::rgb(1., 1., 1.)
}
}
impl ColourSource {
pub fn get_colour(&self, hit: &Hit) -> Colour {
match self {
@@ -24,12 +30,6 @@ impl ColourSource {
}
}
impl Default for ColourSource {
fn default() -> Self {
Self::rgb(1., 1., 1.)
}
}
#[derive(Deserialize)]
#[serde(untagged)]
enum RawColourSource {

View File

@@ -1,5 +1,8 @@
use image::{DynamicImage, ImageReader};
use log::trace;
use std::error::Error;
use image::{DynamicImage, ImageReader, Rgb};
use image::{RgbImage};
use log::warn;
use crate::{objects::hit::Hit, vec3::Vec3};
@@ -13,14 +16,26 @@ pub struct Texture {
impl Texture {
pub fn new(texture: &str) -> Self {
let img = ImageReader::open(texture).unwrap().decode().unwrap(); // FIXME: unwraps
trace!(
"texture '{}' is {} by {} pixels, with {} bytes total",
texture,
img.width(),
img.height(),
img.as_bytes().len()
);
match ImageReader::open(texture) {
Ok(r) => {
match r.decode() {
Ok(img) => Self::_build(img),
Err(e) => {
// ImageError
warn!("Failed to parse image \"{}\": {}", texture, e);
Self::default()
}
}
}
Err(e) => {
// IoError
warn!("Failed to read file \"{}\": {}", texture, e);
Self::default()
}
}
}
fn _build(img: DynamicImage) -> Self {
let stride = match img.color() {
// TODO: support other types of image
image::ColorType::L8 => todo!(),
@@ -35,6 +50,7 @@ impl Texture {
image::ColorType::Rgba32F => todo!(),
_ => 3,
};
Self {
width: img.width(),
height: img.height(),
@@ -66,3 +82,16 @@ impl Texture {
self._at(u, 1. - v)
}
}
impl Default for Texture {
fn default() -> Self {
let mut img = RgbImage::new(1, 1);
img.put_pixel(0,0,Rgb([250,0,250]));
Self {
source: DynamicImage::ImageRgb8(img),
width: 1,
height: 1,
stride: 3,
}
}
}

View File

@@ -1,4 +1,4 @@
use image::{codecs::png::PngEncoder, ExtendedColorType, ImageEncoder};
use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder};
use log::{error, info, trace};
use rayon::iter::IntoParallelIterator;
use rayon::prelude::*;

View File

@@ -6,7 +6,6 @@ use crate::objects::materials::{
dielectric::Dielectric,
lambertian::{Lambertian, Metal},
normal::Normal,
texture::Texture,
traits::Material,
};