summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-01-08 20:22:36 -0800
committerVito Caputo <vcaputo@pengaru.com>2020-01-08 20:24:01 -0800
commit728f54b0f7b471e784782c65f41c6f307a056bba (patch)
treef6e31b9f56ee968a9a402a13798384545adfa032 /src
parent6628eefdf9f157388e9e755194a6066b6f389e1b (diff)
libs/puddle: add a classic 2D raindrop sim lib
These were commonish in the 90s demo days, done as a library to encourage use by different modules. You can simply render this directly onto a frame buffer like the old days, or sample it as a height map or density field for more complex compositions.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am2
-rw-r--r--src/libs/Makefile.am2
-rw-r--r--src/libs/puddle/Makefile.am3
-rw-r--r--src/libs/puddle/puddle.c136
-rw-r--r--src/libs/puddle/puddle.h30
5 files changed, 171 insertions, 2 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 43c79e7..8e4ae2e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,4 +4,4 @@ rototiller_SOURCES = fb.c fb.h fps.c fps.h rototiller.c rototiller.h sdl_fb.c se
if ENABLE_DRM
rototiller_SOURCES += drm_fb.c
endif
-rototiller_LDADD = modules/flui2d/libflui2d.a modules/julia/libjulia.a modules/meta2d/libmeta2d.a modules/montage/libmontage.a modules/pixbounce/libpixbounce.a modules/plasma/libplasma.a modules/ray/libray.a modules/roto/libroto.a modules/rtv/librtv.a modules/snow/libsnow.a modules/sparkler/libsparkler.a modules/spiro/libspiro.a modules/stars/libstars.a modules/submit/libsubmit.a modules/swab/libswab.a libs/grid/libgrid.a libs/ray/libray.a libs/txt/libtxt.a libs/ascii/libascii.a libs/din/libdin.a -lm
+rototiller_LDADD = modules/flui2d/libflui2d.a modules/julia/libjulia.a modules/meta2d/libmeta2d.a modules/montage/libmontage.a modules/pixbounce/libpixbounce.a modules/plasma/libplasma.a modules/ray/libray.a modules/roto/libroto.a modules/rtv/librtv.a modules/snow/libsnow.a modules/sparkler/libsparkler.a modules/spiro/libspiro.a modules/stars/libstars.a modules/submit/libsubmit.a modules/swab/libswab.a libs/grid/libgrid.a libs/puddle/libpuddle.a libs/ray/libray.a libs/txt/libtxt.a libs/ascii/libascii.a libs/din/libdin.a -lm
diff --git a/src/libs/Makefile.am b/src/libs/Makefile.am
index 9f3499d..820efef 100644
--- a/src/libs/Makefile.am
+++ b/src/libs/Makefile.am
@@ -1 +1 @@
-SUBDIRS = ascii grid din ray txt
+SUBDIRS = ascii grid din puddle ray txt
diff --git a/src/libs/puddle/Makefile.am b/src/libs/puddle/Makefile.am
new file mode 100644
index 0000000..faacb49
--- /dev/null
+++ b/src/libs/puddle/Makefile.am
@@ -0,0 +1,3 @@
+noinst_LIBRARIES = libpuddle.a
+libpuddle_a_SOURCES = puddle.c puddle.h
+libpuddle_a_CPPFLAGS = -I@top_srcdir@/src
diff --git a/src/libs/puddle/puddle.c b/src/libs/puddle/puddle.c
new file mode 100644
index 0000000..e5c0dd4
--- /dev/null
+++ b/src/libs/puddle/puddle.c
@@ -0,0 +1,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);
+}
diff --git a/src/libs/puddle/puddle.h b/src/libs/puddle/puddle.h
new file mode 100644
index 0000000..cc8988e
--- /dev/null
+++ b/src/libs/puddle/puddle.h
@@ -0,0 +1,30 @@
+/*
+ * 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/>.
+ */
+
+#ifndef _PUDDLE_H
+#define _PUDDLE_H
+
+typedef struct puddle_t puddle_t;
+typedef struct v2f_t v2f_t;
+
+puddle_t * puddle_new(int w, int h);
+void puddle_free(puddle_t *puddle);
+void puddle_tick(puddle_t *puddle, float viscosity);
+void puddle_set(puddle_t *puddle, int x, int y, float v);
+float puddle_sample(const puddle_t *puddle, const v2f_t *coordinate);
+
+
+#endif
© All Rights Reserved