-
Notifications
You must be signed in to change notification settings - Fork 75
/
crater.c
163 lines (138 loc) · 4.39 KB
/
crater.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright (C) 2015 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "crater.h"
#include "mtwist.h"
#include "png_utils.h"
#include "mathutils.h"
unsigned char base_level = 127;
static float zerotoone()
{
return 0.5 * (1.0 + snis_random_float());
}
static int distsqrd(int x1, int y1, int x2, int y2)
{
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
}
static void add_cone(unsigned char *image, int imagew, int imageh, int x, int y, int r, int height)
{
float d, h, n;
int i, j, ix, iy, index, d2;
for (i = 0; i < r * 2; i++) {
for (j = 0; j < r * 2; j++) {
d2 = distsqrd(i - r, j - r, 0, 0);
if (d2 > r * r)
continue;
d = sqrtf((float) d2);
h = height * (r - d) / r;
ix = (x - r) + i;
iy = (y - r) + j;
if (ix < 0 || ix >= imagew || iy < 0 || iy >= imageh)
continue;
index = 3 * (iy * imagew + ix);
n = (float) image[index] + h;
image[index] = (unsigned char) n;
image[index + 1] = image[index];
image[index + 2] = image[index];
}
}
}
static float crater_depth_profile(float x)
{
/* values chosen empirically */
return sigmoid(x / 50.0, 10.0, 2.0);
}
static void crater_dimple(unsigned char *image, int imagew, int imageh, int x, int y, int r, unsigned char base)
{
int i, j, ix, iy, index, d2;
float d;
float base_height;
float level;
float crater_depth = 0.02 * r;
if (crater_depth < 0.2)
crater_depth = 0.2;
ix = x + r;
iy = y + r;
if (ix < 0 || ix >= imagew || iy < 0 || iy >= imageh) {
base_height = base;
} else {
index = 3 * (iy * imagew + ix);
base_height = (float) image[index];
}
if (crater_depth > 1.0)
crater_depth = 1.0;
for (i = 0; i < r * 2; i++) {
for (j = 0; j < r * 2; j++) {
d2 = distsqrd(i - r, j - r, 0, 0);
if (d2 <= r * r) {
ix = (x - r) + i;
iy = (y - r) + j;
if (ix < 0 || ix >= imagew || iy < 0 || iy >= imageh)
continue;
d = sqrtf((float) d2);
index = 3 * (iy * imagew + ix);
level = (1.0 - crater_depth) * base_height +
crater_depth * crater_depth_profile(1.0 - (d / r));
base = (unsigned char) level;
image[index] = (unsigned char) base;
image[index + 1] = image[index];
image[index + 2] = image[index];
}
}
}
}
static void add_ray(unsigned char *image, int imagew, int imageh, int x, int y, int r, int h)
{
int i;
int npoints = snis_randn(100);
float angle = snis_random_float() * 2.0 * M_PI;
float radius = r;
float con_rad = radius * 0.1;
float tx, ty;
float height = h;
for (i = 0; i < npoints; i++) {
tx = x + cos(angle) * radius;
ty = y - sin(angle) * radius;
add_cone(image, imagew, imageh, tx, ty, (int) con_rad, height);
con_rad = con_rad * 0.98;
height = height * (0.9 + 0.08 * zerotoone());
radius = radius + zerotoone() * r * 0.3;
}
}
static void add_rays(unsigned char *image, int imagew, int imageh, int x, int y, int r, int h, int n)
{
int i;
for (i = 0; i < n; i++)
add_ray(image, imagew, imageh, x, y, r, h);
}
void create_crater_heightmap(unsigned char *image, int imagew, int imageh, int x, int y, int r, int h)
{
int i, npoints = (int) (1.7 * (M_PI * 2.0 * r));
float angle;
crater_dimple(image, imagew, imageh, x, y, r * 0.95, base_level);
for (i = 0; i < npoints; i++) {
int tx, ty;
angle = (2.0 * M_PI * i) / (float) npoints + snis_random_float() * 10.0 * M_PI / 180.0;
tx = x + cos(angle) * r + snis_random_float() * snis_random_float() * r * 3.9;
ty = y - sin(angle) * r + snis_random_float() * snis_random_float() * r * 3.9;
add_cone(image, imagew, imageh, tx, ty, (0.2 + 0.50 * zerotoone()) * r, h);
}
add_rays(image, imagew, imageh, x, y, r, h * 2.75, 50 + snis_randn(350));
}