Mercurial > hg > dmlib
annotate src/dmperlin.c @ 2289:81b561abb6e9
Cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 27 Jun 2019 14:19:33 +0300 |
parents | 848a88ce7a57 |
children | 69a5af2eb1ea |
rev | line source |
---|---|
0 | 1 /* |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
2 * DMLib |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
3 * -- Perlin noise functionality |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
4 * Programmed and designed by Matti 'ccr' Hamalainen |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
5 * (C) Copyright 2012 Tecnic Software productions (TNSP) |
0 | 6 */ |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
7 #include "dmperlin.h" |
979
76e8dcfeb808
Curve macros are needed in dmperlin module.
Matti Hamalainen <ccr@tnsp.org>
parents:
889
diff
changeset
|
8 #include "dmcurves.h" |
0 | 9 #include <math.h> |
10 | |
11 | |
639 | 12 #define DM_PERLIN_SETUP(i, b0, b1, r0, r1) \ |
13 { \ | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
14 t = (vec[i] + DM_PLNS_N); \ |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
15 b0 = (((int) t) & DM_PLNS_BM); \ |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
16 b1 = ((b0 + 1) & DM_PLNS_BM); \ |
639 | 17 r0 = (t - ((int) t)); \ |
18 r1 = (r0 - 1.0f); \ | |
19 } | |
0 | 20 |
21 #define DM_PERLIN_AT2(rx,ry) ((rx) * q[0] + (ry) * q[1]) | |
22 | |
23 | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
24 static DMFloat dmPerlinDoNoise2(const DMPerlinContext *ctx, const DMFloat vec[2]) |
0 | 25 { |
26 int bx0, bx1, by0, by1, b00, b10, b01, b11; | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
27 DMFloat rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
28 const DMFloat *q; |
0 | 29 int i, j; |
30 | |
31 DM_PERLIN_SETUP(0, bx0, bx1, rx0, rx1); | |
32 DM_PERLIN_SETUP(1, by0, by1, ry0, ry1); | |
33 | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
34 i = ctx->p[bx0]; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
35 j = ctx->p[bx1]; |
0 | 36 |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
37 b00 = ctx->p[i + by0]; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
38 b10 = ctx->p[j + by0]; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
39 b01 = ctx->p[i + by1]; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
40 b11 = ctx->p[j + by1]; |
0 | 41 |
42 sx = DMM_S_CURVE(rx0); | |
43 sy = DMM_S_CURVE(ry0); | |
44 | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
45 q = ctx->g2[b00]; |
0 | 46 u = DM_PERLIN_AT2(rx0, ry0); |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
47 |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
48 q = ctx->g2[b10]; |
0 | 49 v = DM_PERLIN_AT2(rx1, ry0); |
50 a = DMM_LERP(sx, u, v); | |
51 | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
52 q = ctx->g2[b01]; |
0 | 53 u = DM_PERLIN_AT2(rx0, ry1); |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
54 |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
55 q = ctx->g2[b11]; |
0 | 56 v = DM_PERLIN_AT2(rx1, ry1); |
57 b = DMM_LERP(sx, u, v); | |
58 | |
59 return DMM_LERP(sy, a, b); | |
60 } | |
61 | |
62 | |
63 static void dmPerlinNormalize2(DMFloat v[2]) | |
64 { | |
639 | 65 DMFloat s = sqrt(v[0] * v[0] + v[1] * v[1]); |
0 | 66 v[0] /= s; |
67 v[1] /= s; | |
68 } | |
69 | |
70 | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
71 int dmPerlinInit(DMPerlinContext *ctx, const int seed) |
0 | 72 { |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
73 int i, j; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
74 |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
75 if (ctx == NULL) |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
76 return DMERR_NULLPTR; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
77 |
1167 | 78 dmMemset(ctx, 0, sizeof(*ctx)); |
0 | 79 |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
80 if (seed < 0) |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
81 return DMERR_INVALID_ARGS; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
82 |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
83 srand(seed); |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
84 |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
85 for (i = 0; i < DM_PLNS_B; i++) |
0 | 86 { |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
87 ctx->p[i] = i; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
88 ctx->g1[i] = (DMFloat) ((rand() % (DM_PLNS_B + DM_PLNS_B)) - DM_PLNS_B) / DM_PLNS_B; |
0 | 89 |
90 for (j = 0; j < 2; j++) | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
91 ctx->g2[i][j] = (DMFloat) ((rand() % (DM_PLNS_B + DM_PLNS_B)) - DM_PLNS_B) / DM_PLNS_B; |
0 | 92 |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
93 dmPerlinNormalize2(ctx->g2[i]); |
0 | 94 } |
95 | |
96 while (--i) | |
97 { | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
98 int k = ctx->p[i]; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
99 ctx->p[i] = ctx->p[j = rand() % DM_PLNS_B]; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
100 ctx->p[j] = k; |
0 | 101 } |
102 | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
103 for (i = 0; i < DM_PLNS_B + 2; i++) |
0 | 104 { |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
105 ctx->p[DM_PLNS_B + i] = ctx->p[i]; |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
106 ctx->g1[DM_PLNS_B + i] = ctx->g1[i]; |
0 | 107 |
108 for (j = 0; j < 2; j++) | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
109 ctx->g2[DM_PLNS_B + i][j] = ctx->g2[i][j]; |
0 | 110 } |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
111 |
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
112 return DMERR_OK; |
0 | 113 } |
114 | |
115 | |
116 /* Harmonic summing functions - PDB | |
117 * In what follows "alpha" is the weight when the sum is formed. | |
118 * Typically it is 2, As this approaches 1 the function is noisier. | |
119 * "beta" is the harmonic scaling/spacing, typically 2. | |
120 */ | |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
121 DMFloat dmPerlinNoise2D(const DMPerlinContext *ctx, DMFloat x, DMFloat y, DMFloat alpha, DMFloat beta, int n) |
0 | 122 { |
123 DMFloat val, sum = 0; | |
124 DMFloat p[2], scale = 1; | |
125 | |
126 p[0] = x; | |
127 p[1] = y; | |
2289 | 128 for (int i = 0; i < n; i++) |
0 | 129 { |
889
e5fde730b4fa
Move perlin noise functions to dmperlin.[ch] and make the API re-entrant.
Matti Hamalainen <ccr@tnsp.org>
parents:
812
diff
changeset
|
130 val = dmPerlinDoNoise2(ctx, p); |
0 | 131 sum += val / scale; |
132 scale *= alpha; | |
133 p[0] *= beta; | |
134 p[1] *= beta; | |
135 } | |
136 | |
639 | 137 return sum; |
0 | 138 } |