Introduce a marble-like noise texture
This commit is contained in:
parent
8400ea96aa
commit
fc35055466
11
perlin.c
11
perlin.c
@ -84,3 +84,14 @@ double perlin_noise(const PerlinData *data, Point3 p) {
|
|||||||
|
|
||||||
return perlin_interp(c, u, v, w);
|
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);
|
||||||
|
}
|
||||||
|
|||||||
3
perlin.h
3
perlin.h
@ -5,10 +5,13 @@
|
|||||||
#include "point3.h"
|
#include "point3.h"
|
||||||
|
|
||||||
#define PERLIN_DEFAULT_POINT_COUNT 256
|
#define PERLIN_DEFAULT_POINT_COUNT 256
|
||||||
|
#define PERLIN_DEFAULT_TURBULENCE_DEPTH 7
|
||||||
|
|
||||||
typedef struct PerlinData PerlinData;
|
typedef struct PerlinData PerlinData;
|
||||||
|
|
||||||
PerlinData *perlin_init(int point_count, Arena *arena);
|
PerlinData *perlin_init(int point_count, Arena *arena);
|
||||||
|
|
||||||
double perlin_noise(const PerlinData *data, Point3 p);
|
double perlin_noise(const PerlinData *data, Point3 p);
|
||||||
|
double perlin_turbulence(const PerlinData *data, Point3 p, int depth);
|
||||||
|
|
||||||
#endif /* INCLUDED_PERLIN_H */
|
#endif /* INCLUDED_PERLIN_H */
|
||||||
|
|||||||
@ -65,8 +65,9 @@ Color checker_value(const CheckerTexture *checker, double u, double v,
|
|||||||
|
|
||||||
Color perlin_value(const PerlinNoiseTexture *perlin, Point3 p) {
|
Color perlin_value(const PerlinNoiseTexture *perlin, Point3 p) {
|
||||||
double noise_value =
|
double noise_value =
|
||||||
0.5 * (1.0 + perlin_noise(perlin->data, (Point3){perlin->scale * p.x,
|
0.5 *
|
||||||
perlin->scale * p.y,
|
(1.0 + sin(perlin->scale * p.z +
|
||||||
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});
|
return color_mul_const(noise_value, (Color){1.0, 1.0, 1.0});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user