summaryrefslogtreecommitdiff
path: root/src/procgen.cpp
blob: b5e1d6fe9c82212b6e1f7226a7118c35015c3f15 (plain)
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
#include "common.hpp"
#include <cmath>

namespace procgen {

void prng_t::seed(uint32_t seed)
{
	state = seed;
	next();
	next();
}

uint32_t prng_t::next(void)
{
	// glibc's LCG parameters (with m = 2^32 - 1).
	state *= 1103515245;
	state += 12345;
	return state;
}

float prng_t::next_float(void)
{
	return (float)next() / 4294967295.0f;
}

void prng_t::unit_vec(float out[2])
{
	float t;

	t = next_float() * 2.0f * M_PI;
	out[0] = cos(t);
	out[1] = sin(t);
}

void perlin_noise_t::generate(prng_t *prng, size_t size_)
{
	if (table)
		delete table;

	size = size_;
	table = new float[size * size][2];

	for (size_t y = 0; y < size; y++)
	for (size_t x = 0; x < size; x++)
		prng->unit_vec(table[y * size + x]);
}

float perlin_noise_t::table_dot(size_t nx, size_t ny, float dx, float dy)
{
	return table[ny * size + nx][0] * dx + table[ny * size + nx][1] * dy;
}

static float smooth_step(float x)
{
	return 3 * x * x - 2 * x * x * x;
}

float perlin_noise_t::get(float x, float y, float scale)
{
	size_t nx, ny, nx1, ny1;
	float s, t, a, b, c, d;

	x /= scale;
	y /= scale;

	nx = (ssize_t)floor(x) & (size - 1);
	ny = (ssize_t)floor(y) & (size - 1);

	nx1 = (nx == size - 1 ? 0 : nx + 1);
	ny1 = (ny == size - 1 ? 0 : ny + 1);

	s = smooth_step(x - floor(x));
	t = smooth_step(y - floor(y));

	a = table_dot(nx,  ny,  -s,        -t);
	b = table_dot(nx1, ny,  -s + 1.0f, -t);
	c = table_dot(nx,  ny1, -s,        -t + 1.0f);
	d = table_dot(nx1, ny1, -s + 1.0f, -t + 1.0f);

	return bilerp(a, b, c, d, s, t);
}

perlin_noise_t::~perlin_noise_t()
{
	delete table;
}

} // namespace procgen