From f56947eca2d13eea833e953e0013c15488528669 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Fri, 5 Mar 2021 18:09:08 +0530 Subject: [PATCH] Fixed a bug in perlin noise function, Removed some commented code --- src/demos/perlin_noise_ball.rs | 2 +- src/demos/two_spheres.rs | 2 +- src/texture/perlin.rs | 29 +++++++---------------------- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/src/demos/perlin_noise_ball.rs b/src/demos/perlin_noise_ball.rs index c1e87e2..f1bb98f 100644 --- a/src/demos/perlin_noise_ball.rs +++ b/src/demos/perlin_noise_ball.rs @@ -21,7 +21,7 @@ impl Demo for PerlinNoiseBall { } fn world(&self) -> Self::DemoT { - let mut world: Vec> = Vec::with_capacity(500); + let mut world: Vec> = Vec::with_capacity(2); let mut rng = rand::thread_rng(); let mut rng = SmallRng::from_rng(&mut rng).unwrap(); diff --git a/src/demos/two_spheres.rs b/src/demos/two_spheres.rs index 1baec28..de3af1b 100644 --- a/src/demos/two_spheres.rs +++ b/src/demos/two_spheres.rs @@ -21,7 +21,7 @@ impl Demo for TwoSpheres { } fn world(&self) -> Self::DemoT { - let mut world: Vec> = Vec::with_capacity(500); + let mut world: Vec> = Vec::with_capacity(2); let mut rng = rand::thread_rng(); let mut rng = SmallRng::from_rng(&mut rng).unwrap(); diff --git a/src/texture/perlin.rs b/src/texture/perlin.rs index ab029fe..a3809e5 100644 --- a/src/texture/perlin.rs +++ b/src/texture/perlin.rs @@ -13,11 +13,10 @@ pub struct Perlin { impl Perlin { pub fn new(rng: &mut R) -> Self { - let mut points = vec![0.0; POINT_COUNT]; - - for p in points.iter_mut() { - *p = rng.gen_range(0.0..=1.0) - } + let points = rng + .sample_iter(Uniform::from(0.0..=1.0)) + .take(POINT_COUNT) + .collect::>(); let permute_x = Self::perlin_generate_permutation(rng); let permute_y = Self::perlin_generate_permutation(rng); @@ -38,23 +37,9 @@ impl Perlin { } pub fn noise(&self, p: &Vec3) -> f64 { - let i = (4.0 * p.x()) as usize & 255; - let j = (4.0 * p.y()) as usize & 255; - let k = (4.0 * p.z()) as usize & 255; - - // if p.x() > 1.0 && p.y() > 1.0 && p.z() > 1.0 { - // println!( - // "p = {} i = {} j = {} k = {} pi = {} pj = {} pk = {} point = {}", - // p, - // i, - // j, - // k, - // self.permute_x[i], - // self.permute_x[j], - // self.permute_x[k], - // self.points[self.permute_x[i] ^ self.permute_y[j] ^ self.permute_z[k]] - // ); - // } + let i = ((4.0 * p.x()) as i32 & 255) as usize; + let j = ((4.0 * p.y()) as i32 & 255) as usize; + let k = ((4.0 * p.z()) as i32 & 255) as usize; self.points[self.permute_x[i] ^ self.permute_y[j] ^ self.permute_z[k]] }