summaryrefslogtreecommitdiff
path: root/src/libs/puddle/puddle.c
blob: e5c0dd4f02b179495c15c5bdc1c235e1f1d8604a (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
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
/*
 *  Copyright (C) 2020 - Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <math.h>
#include <stdlib.h>

#include "puddle.h"

typedef struct puddle_t {
	int	w, h;
	float	*a, *b;
	float	floats[];
} puddle_t;

typedef struct v2f_t {
	float	x, y;
} v2f_t;


puddle_t * puddle_new(int w, int h)
{
	puddle_t	*puddle;

	puddle = calloc(1, sizeof(puddle_t) + sizeof(float) * w * (h + 2) * 2);
	if (!puddle)
		return NULL;

	puddle->w = w;
	puddle->h = h;

	puddle->a = &puddle->floats[w];
	puddle->b = &puddle->floats[w * 2 + w * h + w];

	return puddle;
}


void puddle_free(puddle_t *puddle)
{
	free(puddle);
}


/* Run the puddle simulation for a tick, using the supplied viscosity value.
 * A good viscosity value is ~.01, YMMV.
 */
void puddle_tick(puddle_t *puddle, float viscosity)
{
	float	*a, *b;

	assert(puddle);

	a = puddle->a;
	b = puddle->b;

	for (int y = 0, i = 0; y < puddle->h; y++) {
		for (int x = 0; x < puddle->w; x++, i++) {
			float	tmp =	a[i - puddle->w] +
					a[i - 1] +
					a[i + 1] +
					a[i + puddle->w];

			tmp -= b[i] * 2.f;
			tmp *= .5f;
			tmp -= tmp * viscosity;

			b[i] = tmp;
		}
	}

	puddle->b = a;
	puddle->a = b;
}


/* Set a specific cell in the puddle to the supplied value */
void puddle_set(puddle_t *puddle, int x, int y, float value)
{
	assert(puddle);
	assert(x >= 0 && x < puddle->w);
	assert(y >= 0 && y < puddle->h);

	puddle->a[y * puddle->w + x] = value;
}


static inline float lerp(float a, float b, float t)
{
	return (1.0f - t) * a + t * b;
}


/* Sample the supplied puddle field at the specified coordinate.
 *
 * The puddle field is treated as a unit square mapped to the specified
 * dimensions @ create time.  the sampled value is linearly interpolated from
 * the data.
 */
float puddle_sample(const puddle_t *puddle, const v2f_t *coordinate)
{
	int	x0, y0, x1, y1;
	float	x, y, tx, ty;

	assert(puddle);
	assert(coordinate);

	x = .5f + (coordinate->x * .5f + .5f) * (puddle->w - 2);
	y = .5f + (coordinate->y * .5f + .5f) * (puddle->h - 2);

	x0 = floorf(x);
	y0 = floorf(y);

	x1 = x0 + 1;
	y1 = y0 + 1;

	tx = x - (float)x0;
	ty = y - (float)y0;

	return lerp(lerp(puddle->a[y0 * puddle->w + x0], puddle->a[y0 * puddle->w + x1], tx),
		    lerp(puddle->a[y1 * puddle->w + x0], puddle->a[y1 * puddle->w + x1], tx),
		    ty);
}
© All Rights Reserved