From 2f0ee090844ca41e4adf44af05e6194d27986bb4 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sun, 23 Sep 2018 21:29:14 -0700 Subject: [PATCH] Fix out of bounds texture lookup and tweak noise function. --- rtiow/src/texture.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/rtiow/src/texture.rs b/rtiow/src/texture.rs index a0eeeb7..69cc715 100644 --- a/rtiow/src/texture.rs +++ b/rtiow/src/texture.rs @@ -60,9 +60,9 @@ impl NoiseTexture { impl Texture for NoiseTexture { fn value(&self, _u: f32, _v: f32, p: Vec3) -> Vec3 { - //Vec3::new(1., 1., 1.) * turb(self.scale * p, 7) + Vec3::new(1., 1., 1.) * turb(self.scale * p, 7) //Vec3::new(1., 1., 1.) * 0.5 * (1. + turb(self.scale * p, 7)) - Vec3::new(1., 1., 1.) * 0.5 * (1. + (self.scale * p.x + 5. * turb(p, 7)).sin()) + //Vec3::new(1., 1., 1.) * 0.5 * (1. + (self.scale * p.x + 5. * turb(p, 7)).sin()) //Vec3::new(1., 1., 1.) * 0.5 * (1. + GENERATOR.noise(self.scale * p)) } } @@ -87,8 +87,20 @@ impl ImageTexture { impl Texture for ImageTexture { fn value(&self, u: f32, v: f32, _p: Vec3) -> Vec3 { // Wrap texcoords by default. - let x = (u % 1. * self.width) as u32; - let y = ((1. - v % 1.) * self.height) as u32; + let x = (u % 1. * (self.width - 1.)) as u32; + let y = ((1. - v % 1.) * (self.height - 1.)) as u32; + if x >= self.width as u32 { + panic!(format!( + "u {} v {} x {} y {} w {} h {}", + u, v, x, y, self.width, self.height + )); + } + if y >= self.height as u32 { + panic!(format!( + "u {} v {} x {} y {} w {} h {}", + u, v, x, y, self.width, self.height + )); + } let p = self.img.get_pixel(x, y); let rgb = Vec3::new(p[0] as f32 / 255., p[1] as f32 / 255., p[2] as f32 / 255.); rgb