-
Notifications
You must be signed in to change notification settings - Fork 1
/
perlin.h
89 lines (79 loc) · 2.49 KB
/
perlin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef PERLIN_H_
#define PERLIN_H_
#include "vec3.h"
inline float perlin_interp(vec3 c[2][2][2], float u, float v, float w) {
float uu = u * u * (3 - 2 * u);
float vv = v * v * (3 - 2 * v);
float ww = w * w * (3 - 2 * w);
float accum = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) {
vec3 weight_v(u - i, v - j, w - k);
accum += (i * uu + (1 - i) * (1 - uu)) *
(j * vv + (1 - j) * (1 - vv)) *
(k * ww + (1 - k) * (1 - ww)) * dot(c[i][j][k], weight_v);
}
return accum;
}
class perlin {
public:
float noise(const vec3& p) const {
float u = p.x() - floor(p.x());
float v = p.y() - floor(p.y());
float w = p.z() - floor(p.z());
int i = floor(p.x());
int j = floor(p.y());
int k = floor(p.z());
vec3 c[2][2][2];
for (int di = 0; di < 2; di++)
for (int dj = 0; dj < 2; dj++)
for (int dk = 0; dk < 2; dk++)
c[di][dj][dk] = ranvec[perm_x[(i + di) & 255] ^ perm_y[(j + dj) & 255] ^ perm_z[(k + dk) & 255]];
return perlin_interp(c, u, v, w);
}
float turb(const vec3 &p, int depth = 7) const {
float accum = 0;
vec3 temp_p = p;
float weight = 1.0;
for (int i = 0; i < depth; i++) {
accum += weight * noise(temp_p);
weight *= 0.5;
temp_p *= 2;
}
return fabs(accum);
}
static vec3 *ranvec;
static int *perm_x;
static int *perm_y;
static int *perm_z;
};
static vec3* perlin_generate() {
vec3 *p = new vec3[256];
for (int i = 0; i < 256; i++) {
p[i] = unit_vector(vec3(-1 + 2 * drand(), -1 + 2 * drand(), -1 + 2 * drand()));
}
return p;
}
void permute (int *p, int n ) {
for(int i=n-1;i>0;i--){
int target = int(drand() * (i+1));
int tmp = p[i];
p[i] = p[target];
p[target] = tmp;
}
return;
}
static int* perlin_generate_perm() {
int * p = new int[256];
for(int i=0;i<256;i++){
p[i] = i;
}
permute(p, 256);
return p;
}
vec3 *perlin::ranvec = perlin_generate();
int *perlin::perm_x = perlin_generate_perm();
int *perlin::perm_y = perlin_generate_perm();
int *perlin::perm_z = perlin_generate_perm();
#endif // PERLIN_H_