-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaultgen.c
119 lines (97 loc) · 2.05 KB
/
faultgen.c
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
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "faultgen.h"
#include "mcc.h"
struct faultgen_t
{
int x, y;
float map[];
};
struct faultgen_t *faultgen_init(int x, int y)
{
struct faultgen_t *f = malloc(sizeof *f + sizeof *f->map * x * y);
if (f == NULL)
{
LOG("[faultgen] faultgen_init(): couldn't allocate %zu bytes\n", sizeof *f + sizeof *f->map * x * y);
return NULL;
}
f->x = x;
f->y = y;
return f;
}
void faultgen_deinit(struct faultgen_t *f)
{
free(f);
}
void faultgen_create(struct faultgen_t *f, bool mountains)
{
float disp_max;
float disp_delta;
float start_height;
if (mountains)
{
disp_max = 0.02f;
disp_delta = -0.0025f;
start_height = 0.6f;
}
else
{
disp_max = 0.05f;
disp_delta = -0.005f;
start_height = 0.5f;
}
int hx = f->x / 2;
int hy = f->y / 2;
int hx2 = hx * hx;
int hy2 = hy * hy;
float d = sqrt(hx2 + hy2);
float disp_min = -disp_max;
float disp = disp_max;
int i;
LOG("faultgen: generating %d by %d map\n", f->x, f->y);
for (i = 0; i < f->x * f->y; i++)
{
f->map[i] = start_height;
}
LOG("faultgen: starting %d iterations\n", f->x + f->y);
for (i = 0; i < f->x + f->y; i++)
{
// float w = (rand() % 3600) / 10.0;
float w = ((float)rand() / RAND_MAX) * 2.0 * M_PI;
float a = cos(w);
float b = sin(w);
float c = ((float)rand() / RAND_MAX) * 2.0 * d - d;
int x, y;
for (x = 0; x < f->x; x++)
{
for (y = 0; y < f->y; y++)
{
float h = ((y - hy) * a + (x - hx) * b + c > 0) ? disp : -disp;
float *m = &f->map[x + y * f->x];
*m += h;
//if (*m > 1) *m = 1;
//if (*m < 0) *m = 0;
}
}
disp += disp_delta;
if (disp < disp_min) disp = disp_max;
}
LOG("faultgen: normalizing\n");
float min = +INFINITY;
float max = -INFINITY;
for (i = 0; i < f->x * f->y; i++)
{
if (f->map[i] < min) min = f->map[i];
if (f->map[i] > max) max = f->map[i];
}
for (i = 0; i < f->x * f->y; i++)
{
f->map[i] = (f->map[i] - min) / (max - min);
}
LOG("faultgen: complete\n");
}
const float *faultgen_map(struct faultgen_t *f)
{
return f->map;
}