From 3653ac23907c051d2a29d8917f1bd1cded655558 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 10 Nov 2019 19:01:02 -0800 Subject: flui2d: add some rudimentary settings Viscosity and diffusion are supported, it'd be neat to add a configurable size (the ROOT define) for the flow field in the future. I didn't go crazy here, it's just a list of orders of magnitude you choose from for each. It'd probably be more interesting to change this into a single knob with descriptive names like "smoke" "goop" "water" mapping to a LUT. --- src/modules/flui2d/flui2d.c | 79 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/modules/flui2d/flui2d.c b/src/modules/flui2d/flui2d.c index a8b30a4..65aeace 100644 --- a/src/modules/flui2d/flui2d.c +++ b/src/modules/flui2d/flui2d.c @@ -5,6 +5,7 @@ #include "fb.h" #include "rototiller.h" +#include "settings.h" /* This code is almost entirely taken from the paper: @@ -17,11 +18,11 @@ #if 1 /* These knobs affect how the simulated fluid behaves */ -#define VISCOSITY .000000001f -#define DIFFUSION .00001f +#define DEFAULT_VISCOSITY .000000001f +#define DEFAULT_DIFFUSION .00001f #else -#define VISCOSITY .00001f -#define DIFFUSION .000001f +#define DEFAULT_VISCOSITY .00001f +#define DEFAULT_DIFFUSION .000001f #endif #define ROOT 128 // Change this to vary the density field resolution @@ -29,6 +30,9 @@ #define IX(i, j) ((i) + (ROOT + 2) * (j)) #define SWAP(x0, x) {float *tmp = x0; x0 = x; x = tmp;} +static float flui2d_viscosity = DEFAULT_VISCOSITY; +static float flui2d_diffusion = DEFAULT_DIFFUSION; + typedef struct flui2d_t { float u[SIZE], v[SIZE], u_prev[SIZE], v_prev[SIZE]; float dens[SIZE], dens_prev[SIZE]; @@ -191,8 +195,8 @@ static void * flui2d_create_context(void) if (!ctxt) return NULL; - ctxt->fluid.visc = VISCOSITY; - ctxt->fluid.diff = DIFFUSION; + ctxt->fluid.visc = flui2d_viscosity; + ctxt->fluid.diff = flui2d_diffusion; return ctxt; } @@ -279,6 +283,68 @@ static void flui2d_render_fragment(void *context, fb_fragment_t *fragment) } +/* Settings hooks for configurable variables */ +int flui2d_setup(const settings_t *settings, setting_desc_t **next_setting) +{ + const char *viscosity; + const char *diffusion; + const char *values[] = { + ".000000000001f", + ".0000000001f", + ".000000001f", + ".00000001f", + ".0000001f", + ".000001f", + ".00001f", + ".0001f", + NULL + }; + + + viscosity = settings_get_value(settings, "viscosity"); + if (!viscosity) { + int r; + + r = setting_desc_clone(&(setting_desc_t){ + .name = "Fluid Viscosity", + .key = "viscosity", + .regex = "\\.[0-9]+", + .preferred = SETTINGS_STR(DEFAULT_VISCOSITY), + .values = values, + .annotations = NULL + }, next_setting); + if (r < 0) + return r; + + return 1; + } + + diffusion = settings_get_value(settings, "diffusion"); + if (!diffusion) { + int r; + + r = setting_desc_clone(&(setting_desc_t){ + .name = "Fluid Diffusion", + .key = "diffusion", + .regex = "\\.[0-9]+", + .preferred = SETTINGS_STR(DEFAULT_DIFFUSION), + .values = values, + .annotations = NULL + }, next_setting); + if (r < 0) + return r; + + return 1; + } + + /* TODO: return -EINVAL on parse errors? */ + sscanf(viscosity, "%f", &flui2d_viscosity); + sscanf(diffusion, "%f", &flui2d_diffusion); + + return 0; +} + + rototiller_module_t flui2d_module = { .create_context = flui2d_create_context, .destroy_context = flui2d_destroy_context, @@ -288,4 +354,5 @@ rototiller_module_t flui2d_module = { .description = "Fluid dynamics simulation in 2D (threaded (poorly))", .author = "Vito Caputo ", .license = "Unknown", + .setup = flui2d_setup, }; -- cgit v1.2.1