From 194328d92fce361d7d432db25e689c30b7847555 Mon Sep 17 00:00:00 2001 From: Djairo Date: Wed, 13 May 2026 13:25:57 +0200 Subject: [PATCH] fx: texture colouring --- src/objects/materials/texture.rs | 21 +++++++++++++++++++-- src/raytracer.rs | 4 ++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/objects/materials/texture.rs b/src/objects/materials/texture.rs index f610d4b..fac07f1 100644 --- a/src/objects/materials/texture.rs +++ b/src/objects/materials/texture.rs @@ -1,4 +1,5 @@ use image::{DynamicImage, ImageReader}; +use log::info; use crate::{ objects::{hit::Hit, materials::traits::Material}, @@ -11,15 +12,31 @@ pub struct Texture { source: DynamicImage, width: u32, height: u32, + stride: usize, } impl Texture { pub fn new(texture: &str) -> Self { let img = ImageReader::open(texture).unwrap().decode().unwrap(); // FIXME: unwraps + info!("{} * {} = {} bytes", img.width(), img.height(), img.as_bytes().len()); + let stride = match img.color() { // TODO: support other types of image + image::ColorType::L8 => todo!(), + image::ColorType::La8 => todo!(), + image::ColorType::Rgb8 => 3, + image::ColorType::Rgba8 => 4, + image::ColorType::L16 => todo!(), + image::ColorType::La16 => todo!(), + image::ColorType::Rgb16 => todo!(), + image::ColorType::Rgba16 => todo!(), + image::ColorType::Rgb32F => todo!(), + image::ColorType::Rgba32F => todo!(), + _ => 3, + }; Self { width: img.width(), height: img.height(), source: img, + stride: stride, } } @@ -31,10 +48,10 @@ impl Texture { } fn _at(&self, u: f32, v: f32) -> Vec3 { - let b = self.source.as_bytes(); // FIXME: i think im indexing colours incorrectly + let b = self.source.as_bytes(); let idx = self._idx(u, v); - Vec3::from_u8(b[3 * idx], b[3 * idx + 1], b[3 * idx + 2]) + Vec3::from_u8(b[self.stride * idx + 0], b[self.stride * idx + 2], b[self.stride * idx + 1]) } } diff --git a/src/raytracer.rs b/src/raytracer.rs index 60f26bb..78a9545 100644 --- a/src/raytracer.rs +++ b/src/raytracer.rs @@ -1,5 +1,5 @@ use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder}; -use log::{error, info}; +use log::{error, info, trace}; use rayon::iter::IntoParallelIterator; use rayon::prelude::*; use std::{fs::File, sync::Arc, time::Instant}; @@ -69,7 +69,7 @@ pub fn render_chunk(chunk: &mut [u8], scene: &Scene, y: u32) { chunk[(i * 3) as usize + 1] = g; chunk[(i * 3) as usize + 2] = b; } - info!("Line {}\tfinished.", y); + trace!("Line {}\tfinished.", y); } pub fn ray_colour(hittables: &Vec>, r: &Ray, depth: u32) -> Colour {