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