1
0

Fixed a bug in perlin noise function, Removed some commented code

This commit is contained in:
Ishan Jain 2021-03-05 18:09:08 +05:30
parent 4f2365dbca
commit f56947eca2
No known key found for this signature in database
GPG Key ID: F261A0E73038D89D
3 changed files with 9 additions and 24 deletions

View File

@ -21,7 +21,7 @@ impl Demo for PerlinNoiseBall {
} }
fn world(&self) -> Self::DemoT { fn world(&self) -> Self::DemoT {
let mut world: Vec<Arc<dyn ParallelHit>> = Vec::with_capacity(500); let mut world: Vec<Arc<dyn ParallelHit>> = Vec::with_capacity(2);
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut rng = SmallRng::from_rng(&mut rng).unwrap(); let mut rng = SmallRng::from_rng(&mut rng).unwrap();

View File

@ -21,7 +21,7 @@ impl Demo for TwoSpheres {
} }
fn world(&self) -> Self::DemoT { fn world(&self) -> Self::DemoT {
let mut world: Vec<Arc<dyn ParallelHit>> = Vec::with_capacity(500); let mut world: Vec<Arc<dyn ParallelHit>> = Vec::with_capacity(2);
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let mut rng = SmallRng::from_rng(&mut rng).unwrap(); let mut rng = SmallRng::from_rng(&mut rng).unwrap();

View File

@ -13,11 +13,10 @@ pub struct Perlin {
impl Perlin { impl Perlin {
pub fn new<R: Rng + ?Sized>(rng: &mut R) -> Self { pub fn new<R: Rng + ?Sized>(rng: &mut R) -> Self {
let mut points = vec![0.0; POINT_COUNT]; let points = rng
.sample_iter(Uniform::from(0.0..=1.0))
for p in points.iter_mut() { .take(POINT_COUNT)
*p = rng.gen_range(0.0..=1.0) .collect::<Vec<f64>>();
}
let permute_x = Self::perlin_generate_permutation(rng); let permute_x = Self::perlin_generate_permutation(rng);
let permute_y = 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 { pub fn noise(&self, p: &Vec3) -> f64 {
let i = (4.0 * p.x()) as usize & 255; let i = ((4.0 * p.x()) as i32 & 255) as usize;
let j = (4.0 * p.y()) as usize & 255; let j = ((4.0 * p.y()) as i32 & 255) as usize;
let k = (4.0 * p.z()) as usize & 255; let k = ((4.0 * p.z()) as i32 & 255) as usize;
// 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]]
// );
// }
self.points[self.permute_x[i] ^ self.permute_y[j] ^ self.permute_z[k]] self.points[self.permute_x[i] ^ self.permute_y[j] ^ self.permute_z[k]]
} }