Skip to content

Commit

Permalink
Added procedural marble based on perlin noise
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Jorquera committed Mar 14, 2024
1 parent 837d356 commit b1be90b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/perlin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,17 @@ double Perlin::noise(const Point& point) const {

return interpolation(c, u, v, w);
}

double Perlin::turbulence(const Point& p, int depth) const {
auto accum = 0.0;
auto tmp = p;
auto weight = 1.0;

for (int i = 0; i < depth; i++) {
accum += weight * noise(tmp);
weight *= 0.5;
tmp *= 2.0;
}

return fabs(accum);
}
1 change: 1 addition & 0 deletions src/perlin.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class Perlin {
~Perlin();

double noise(const Point& point) const;
double turbulence(const Point& p, int depth = 7) const;
};
4 changes: 3 additions & 1 deletion src/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <memory>
#include <string>
#include <cmath>

class Texture {

Expand Down Expand Up @@ -104,7 +105,8 @@ class NoiseTexture : public Texture {
NoiseTexture(double scale):_scale(scale) {}

Color color(double u, double v, const Point &point) override {
return Color(1.0, 1.0, 1.0) * 0.5 * (1.0 + _noise.noise(_scale * point));
auto s = _scale * point;
return Color(1.0, 1.0, 1.0) * 0.5 * (1.0 + std::sin(s.z() + 10.0 * _noise.turbulence(s)));
}

};

0 comments on commit b1be90b

Please sign in to comment.