Introduce a marble-like noise texture

This commit is contained in:
Jean-Michel Gorius 2022-11-13 16:01:49 +01:00
parent 8400ea96aa
commit fc35055466
3 changed files with 18 additions and 3 deletions

View File

@ -84,3 +84,14 @@ double perlin_noise(const PerlinData *data, Point3 p) {
return perlin_interp(c, u, v, w);
}
double perlin_turbulence(const PerlinData *data, Point3 p, int depth) {
double accum = 0.0;
double weight = 1.0;
for (int i = 0; i < depth; ++i) {
accum += weight * perlin_noise(data, p);
weight *= 0.5;
p = (Point3){2 * p.x, 2 * p.y, 2 * p.z};
}
return fabs(accum);
}

View File

@ -5,10 +5,13 @@
#include "point3.h"
#define PERLIN_DEFAULT_POINT_COUNT 256
#define PERLIN_DEFAULT_TURBULENCE_DEPTH 7
typedef struct PerlinData PerlinData;
PerlinData *perlin_init(int point_count, Arena *arena);
double perlin_noise(const PerlinData *data, Point3 p);
double perlin_turbulence(const PerlinData *data, Point3 p, int depth);
#endif /* INCLUDED_PERLIN_H */

View File

@ -65,8 +65,9 @@ Color checker_value(const CheckerTexture *checker, double u, double v,
Color perlin_value(const PerlinNoiseTexture *perlin, Point3 p) {
double noise_value =
0.5 * (1.0 + perlin_noise(perlin->data, (Point3){perlin->scale * p.x,
perlin->scale * p.y,
perlin->scale * p.z}));
0.5 *
(1.0 + sin(perlin->scale * p.z +
10 * perlin_turbulence(perlin->data, p,
PERLIN_DEFAULT_TURBULENCE_DEPTH)));
return color_mul_const(noise_value, (Color){1.0, 1.0, 1.0});
}