-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperlin.h
144 lines (104 loc) · 3.78 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef PERLIN_H
#define PERLIN_H
#include "rtweekend.h"
#include "vec3.h"
class perlin {
public:
perlin(){
ranvec = new vec3[point_count];
for(int i = 0; i < point_count; i++){
ranvec[i] = unit_vector(random(-1,1));
}
perm_x = perlin_generate_perm();
perm_y = perlin_generate_perm();
perm_z = perlin_generate_perm();
}
~perlin(){
delete[] ranvec;
delete[] perm_x;
delete[] perm_y;
delete[] perm_z;
}
// double step_noise(const point3& p) const{
// auto i = static_cast<int>(4 * p.x()) & 255;
// auto j = static_cast<int>(4 * p.y()) & 255;
// auto k = static_cast<int>(4 * p.z()) & 255;
// return ranfloat[perm_x[i] ^ perm_y[j] ^ perm_z[k]];
// }
double noise(const point3& p) const{
auto u = p.x() - floor(p.x());
auto v = p.y() - floor(p.y());
auto w = p.z() - floor(p.z());
auto i = static_cast<int>(floor(p.x()));
auto j = static_cast<int>(floor(p.y()));
auto k = static_cast<int>(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);
}
double turb(const point3& p, int depth = 3) const {
auto accum = 0.0;
auto temp_p = p;
auto weight = 1.0;
for(int i = 0; i < depth; i++){
accum += weight * noise(temp_p);
weight *=0.5;
temp_p *=2;
}
return fabs(accum);
}
private:
static const int point_count = 256;
vec3* ranvec;
int* perm_x;
int* perm_y;
int* perm_z;
static int* perlin_generate_perm() {
auto p = new int[point_count];
for(int i = 0; i < point_count; i++){
p[i] = i;
}
permute(p, point_count);
return p;
}
static void permute(int* p, int n){
for(int i = n - 1; i > 0; i --){
int target = random_int(0, i);
int tmp = p[i];
p[i] = p[target];
p[target] = tmp;
}
}
static double perlin_interp(vec3 c[2][2][2], double u, double v, double w){
auto accum = 0.0;
auto uu = u*u*(3 - 2*u);
auto vv = v*v*(3 - 2*v);
auto ww = w*w*(3 - 2*w);
for (int i=0; i < 2; i++){
for (int j=0; j < 2; j++){
for (int k=0; k < 2; k++){
vec3 disp(u-i,v-j,w-k);
accum += dot(c[i][j][k],disp) * (ww*k + (1-ww)*(1-k)) * (vv*j + (1-vv)*(1-j)) *
(uu*i + (1-uu)*(1-i));
}
}
}
return accum;
}
// static double trilinear_interp(double c[2][2][2], double u, double v, double w){
// auto accum = 0.0;
// for (int i=0; i < 2; i++)
// for (int j=0; j < 2; j++)
// for (int k=0; k < 2; k++)
// accum += c[i][j][k] * (w*k + (1-w)*(1-k)) * (v*j + (1-v)*(1-j)) *
// (u*i + (1-u)*(1-i));
// return accum;
// }
};
#endif