fx: texture colouring

This commit is contained in:
2026-05-13 13:25:57 +02:00
parent 6a1e50fb7a
commit 194328d92f
2 changed files with 21 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
use image::{DynamicImage, ImageReader}; use image::{DynamicImage, ImageReader};
use log::info;
use crate::{ use crate::{
objects::{hit::Hit, materials::traits::Material}, objects::{hit::Hit, materials::traits::Material},
@@ -11,15 +12,31 @@ pub struct Texture {
source: DynamicImage, source: DynamicImage,
width: u32, width: u32,
height: u32, height: u32,
stride: usize,
} }
impl Texture { impl Texture {
pub fn new(texture: &str) -> Self { pub fn new(texture: &str) -> Self {
let img = ImageReader::open(texture).unwrap().decode().unwrap(); // FIXME: unwraps 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 { Self {
width: img.width(), width: img.width(),
height: img.height(), height: img.height(),
source: img, source: img,
stride: stride,
} }
} }
@@ -31,10 +48,10 @@ impl Texture {
} }
fn _at(&self, u: f32, v: f32) -> Vec3 { 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); 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])
} }
} }

View File

@@ -1,5 +1,5 @@
use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder}; use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder};
use log::{error, info}; use log::{error, info, trace};
use rayon::iter::IntoParallelIterator; use rayon::iter::IntoParallelIterator;
use rayon::prelude::*; use rayon::prelude::*;
use std::{fs::File, sync::Arc, time::Instant}; 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 + 1] = g;
chunk[(i * 3) as usize + 2] = b; chunk[(i * 3) as usize + 2] = b;
} }
info!("Line {}\tfinished.", y); trace!("Line {}\tfinished.", y);
} }
pub fn ray_colour(hittables: &Vec<Arc<dyn Hittable>>, r: &Ray, depth: u32) -> Colour { pub fn ray_colour(hittables: &Vec<Arc<dyn Hittable>>, r: &Ray, depth: u32) -> Colour {