0
|
1 /*
|
|
2 * Coherent noise function over 1 or 2 dimensions.
|
|
3 */
|
|
4 #include "dmlib.h"
|
|
5 #include <math.h>
|
|
6
|
|
7 #define B (0x100)
|
|
8 #define BM (0x0ff)
|
|
9 #define NP (12)
|
|
10 #define N (0x1000)
|
|
11 #define NM (0x0fff)
|
|
12
|
|
13
|
639
|
14 #define DM_PERLIN_SETUP(i, b0, b1, r0, r1) \
|
|
15 { \
|
|
16 t = (vec[i] + N); \
|
|
17 b0 = (((int) t) & BM); \
|
|
18 b1 = ((b0 + 1) & BM); \
|
|
19 r0 = (t - ((int) t)); \
|
|
20 r1 = (r0 - 1.0f); \
|
|
21 }
|
0
|
22
|
|
23
|
|
24 #define DM_PERLIN_AT2(rx,ry) ((rx) * q[0] + (ry) * q[1])
|
|
25
|
|
26
|
|
27 static int p[B + B + 2];
|
|
28 static DMFloat g2[B + B + 2][2];
|
|
29 static DMFloat g1[B + B + 2];
|
|
30
|
|
31
|
|
32 static DMFloat dmPerlinDoNoise2(DMFloat vec[2])
|
|
33 {
|
|
34 int bx0, bx1, by0, by1, b00, b10, b01, b11;
|
|
35 DMFloat rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
|
|
36 int i, j;
|
|
37
|
|
38 DM_PERLIN_SETUP(0, bx0, bx1, rx0, rx1);
|
|
39 DM_PERLIN_SETUP(1, by0, by1, ry0, ry1);
|
|
40
|
|
41 i = p[bx0];
|
|
42 j = p[bx1];
|
|
43
|
|
44 b00 = p[i + by0];
|
|
45 b10 = p[j + by0];
|
|
46 b01 = p[i + by1];
|
|
47 b11 = p[j + by1];
|
|
48
|
|
49 sx = DMM_S_CURVE(rx0);
|
|
50 sy = DMM_S_CURVE(ry0);
|
|
51
|
|
52 q = g2[b00];
|
|
53 u = DM_PERLIN_AT2(rx0, ry0);
|
|
54 q = g2[b10];
|
|
55 v = DM_PERLIN_AT2(rx1, ry0);
|
|
56 a = DMM_LERP(sx, u, v);
|
|
57
|
|
58 q = g2[b01];
|
|
59 u = DM_PERLIN_AT2(rx0, ry1);
|
|
60 q = g2[b11];
|
|
61 v = DM_PERLIN_AT2(rx1, ry1);
|
|
62 b = DMM_LERP(sx, u, v);
|
|
63
|
|
64 return DMM_LERP(sy, a, b);
|
|
65 }
|
|
66
|
|
67
|
|
68 static void dmPerlinNormalize2(DMFloat v[2])
|
|
69 {
|
639
|
70 DMFloat s = sqrt(v[0] * v[0] + v[1] * v[1]);
|
0
|
71 v[0] /= s;
|
|
72 v[1] /= s;
|
|
73 }
|
|
74
|
|
75
|
|
76 void dmPerlinInit(void)
|
|
77 {
|
|
78 int i, j, k;
|
|
79
|
|
80 srand(32);
|
|
81
|
|
82 for (i = 0; i < B; i++)
|
|
83 {
|
|
84 p[i] = i;
|
|
85 g1[i] = (DMFloat) ((rand() % (B + B)) - B) / B;
|
|
86
|
|
87 for (j = 0; j < 2; j++)
|
|
88 g2[i][j] = (DMFloat) ((rand() % (B + B)) - B) / B;
|
|
89
|
|
90 dmPerlinNormalize2(g2[i]);
|
|
91 }
|
|
92
|
|
93 while (--i)
|
|
94 {
|
|
95 k = p[i];
|
|
96 p[i] = p[j = rand() % B];
|
|
97 p[j] = k;
|
|
98 }
|
|
99
|
|
100 for (i = 0; i < B + 2; i++)
|
|
101 {
|
|
102 p[B + i] = p[i];
|
|
103 g1[B + i] = g1[i];
|
|
104
|
|
105 for (j = 0; j < 2; j++)
|
|
106 g2[B + i][j] = g2[i][j];
|
|
107 }
|
|
108 }
|
|
109
|
|
110
|
|
111 /* Harmonic summing functions - PDB
|
|
112 * In what follows "alpha" is the weight when the sum is formed.
|
|
113 * Typically it is 2, As this approaches 1 the function is noisier.
|
|
114 * "beta" is the harmonic scaling/spacing, typically 2.
|
|
115 */
|
|
116 DMFloat dmPerlinNoise2D(DMFloat x, DMFloat y, DMFloat alpha, DMFloat beta, int n)
|
|
117 {
|
|
118 int i;
|
|
119 DMFloat val, sum = 0;
|
|
120 DMFloat p[2], scale = 1;
|
|
121
|
|
122 p[0] = x;
|
|
123 p[1] = y;
|
|
124 for (i = 0; i < n; i++)
|
|
125 {
|
|
126 val = dmPerlinDoNoise2(p);
|
|
127 sum += val / scale;
|
|
128 scale *= alpha;
|
|
129 p[0] *= beta;
|
|
130 p[1] *= beta;
|
|
131 }
|
|
132
|
639
|
133 return sum;
|
0
|
134 }
|