diff --git a/src/noise.rs b/src/noise.rs index dcb4287..8a75355 100644 --- a/src/noise.rs +++ b/src/noise.rs @@ -9,8 +9,6 @@ pub struct NoiseTexture { impl Texture for NoiseTexture { fn value(&self, _u: f64, _v: f64, point: &Point3) -> Color { - Color::new(1.0, 1.0, 1.0) - * 0.5 - * (1.0 + self.noise.noise(&(*point * self.scale))) + Color::new(1.0, 1.0, 1.0) * self.noise.default_turbulence(&(*point * self.scale)) } } diff --git a/src/perlin.rs b/src/perlin.rs index 92f7925..ef43d56 100644 --- a/src/perlin.rs +++ b/src/perlin.rs @@ -22,6 +22,22 @@ impl Perlin { } } + pub fn default_turbulence(&self, point: &Point3) -> f64 { + self.turbulence(point, 7) + } + + pub fn turbulence(&self, point: &Point3, depth: i32) -> f64 { + let mut accum = 0.0; + let mut temp_p = point.clone(); + let mut weight = 1.0; + for _i in 0..depth { + accum += weight * self.noise(&temp_p); + weight *= 0.5; + temp_p *= 2.0; + } + accum.abs() + } + pub fn noise(&self, point: &Point3) -> f64 { let u = point.x() - point.x().floor(); let v = point.y() - point.y().floor(); diff --git a/src/vec3.rs b/src/vec3.rs index 5861583..e4a9813 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -1,5 +1,5 @@ use std::fmt::{Display, Formatter}; -use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, Index}; +use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, Index, MulAssign}; use rand::distributions::{Distribution, Uniform}; #[derive(Debug, Clone, Copy)] @@ -228,6 +228,32 @@ impl Mul for f64 { } } +impl Mul for &Vec3 { + type Output = Vec3; + + fn mul(self, other: f64) -> Vec3 { + Vec3 { + x: self.x * other, + y: self.y * other, + z: self.z * other, + } + } +} + +impl Mul<&Vec3> for f64 { + type Output = Vec3; + + fn mul(self, other: &Vec3) -> Vec3 { + other * self + } +} + +impl MulAssign for Vec3 { + fn mul_assign(&mut self, rhs: f64) { + *self = *self * rhs; + } +} + impl Div for Vec3 { type Output = Vec3;