diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2017-04-22 16:01:59 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2017-04-22 16:01:59 -0700 |
commit | 0dfeaa9dbc9305b7cabd3498f77bc27739e9d842 (patch) | |
tree | f6ab0baa52c794085cd88006f6d828894b06bc21 | |
parent | d28ceb85a6b43a503c608116798945baab0e060f (diff) |
julia: utilize context struct for module state
-rw-r--r-- | src/modules/julia/julia.c | 130 |
1 files changed, 76 insertions, 54 deletions
diff --git a/src/modules/julia/julia.c b/src/modules/julia/julia.c index 3811d0d..3215902 100644 --- a/src/modules/julia/julia.c +++ b/src/modules/julia/julia.c @@ -1,6 +1,7 @@ #include <stdint.h> #include <inttypes.h> #include <math.h> +#include <stdlib.h> #include "fb.h" #include "rototiller.h" @@ -11,11 +12,69 @@ /* TODO: explore using C99 complex.h and its types? */ -static float rr; -static float realscale; -static float imagscale; -static float creal; -static float cimag; +typedef struct julia_context_t { + float rr; + float realscale; + float imagscale; + float creal; + float cimag; +} julia_context_t; + +static uint32_t colors[] = { + /* this palette is just something I slapped together, definitely needs improvement. TODO */ + 0x000000, + 0x000044, + 0x000088, + 0x0000aa, + 0x0000ff, + 0x0044ff, + 0x0088ff, + 0x00aaff, + 0x00ffff, + 0x44ffaa, + 0x88ff88, + 0xaaff44, + 0xffff00, + 0xffaa00, + 0xff8800, + 0xff4400, + 0xff0000, + 0xaa0000, + 0x880000, + 0x440000, + 0x440044, + 0x880088, + 0xaa00aa, + 0xff00ff, + 0xff4400, + 0xff8800, + 0xffaa00, + 0xffff00, + 0xaaff44, + 0x88ff88, + 0x44ffaa, + 0x00ffff, + 0x00aaff, + 0x0088ff, + 0xff4400, + 0xff00ff, + 0xaa00aa, + 0x880088, + 0x440044, + }; + + +static void * julia_create_context(void) +{ + return calloc(1, sizeof(julia_context_t)); +} + + +static void julia_destroy_context(void *context) +{ + free(context); + +} static inline unsigned julia_iter(float real, float imag, float creal, float cimag, unsigned max_iters) { @@ -44,66 +103,26 @@ static inline unsigned julia_iter(float real, float imag, float creal, float cim /* Prepare a frame for concurrent drawing of fragment using multiple fragments */ static void julia_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t *fragment, rototiller_frame_t *res_frame) { + julia_context_t *ctxt = context; + res_frame->n_fragments = n_cpus; fb_fragment_divide(fragment, n_cpus, res_frame->fragments); - rr += .01; + ctxt->rr += .01; /* Rather than just sweeping creal,cimag from -2.0-+2.0, I try to keep things confined * to an interesting (visually) range. TODO: could certainly use refinement. */ - realscale = 0.01f * cosf(rr) + 0.01f; - imagscale = 0.01f * sinf(rr * 3.0f) + 0.01f; - creal = (1.01f + (realscale * cosf(1.5f*M_PI+rr) + realscale)) * cosf(rr * .3f); - cimag = (1.01f + (imagscale * sinf(rr * 3.0f) + imagscale)) * sinf(rr); + ctxt->realscale = 0.01f * cosf(ctxt->rr) + 0.01f; + ctxt->imagscale = 0.01f * sinf(ctxt->rr * 3.0f) + 0.01f; + ctxt->creal = (1.01f + (ctxt->realscale * cosf(1.5f * M_PI + ctxt->rr) + ctxt->realscale)) * cosf(ctxt->rr * .3f); + ctxt->cimag = (1.01f + (ctxt->imagscale * sinf(ctxt->rr * 3.0f) + ctxt->imagscale)) * sinf(ctxt->rr); } /* Draw a morphing Julia set */ static void julia_render_fragment(void *context, fb_fragment_t *fragment) { - static uint32_t colors[] = { - /* this palette is just something I slapped together, definitely needs improvement. TODO */ - 0x000000, - 0x000044, - 0x000088, - 0x0000aa, - 0x0000ff, - 0x0044ff, - 0x0088ff, - 0x00aaff, - 0x00ffff, - 0x44ffaa, - 0x88ff88, - 0xaaff44, - 0xffff00, - 0xffaa00, - 0xff8800, - 0xff4400, - 0xff0000, - 0xaa0000, - 0x880000, - 0x440000, - 0x440044, - 0x880088, - 0xaa00aa, - 0xff00ff, - 0xff4400, - 0xff8800, - 0xffaa00, - 0xffff00, - 0xaaff44, - 0x88ff88, - 0x44ffaa, - 0x00ffff, - 0x00aaff, - 0x0088ff, - 0xff4400, - 0xff00ff, - 0xaa00aa, - 0x880088, - 0x440044, - }; - + julia_context_t *ctxt = context; unsigned x, y; unsigned stride = fragment->stride / 4, width = fragment->width, height = fragment->height; uint32_t *buf = fragment->buf; @@ -114,14 +133,17 @@ static void julia_render_fragment(void *context, fb_fragment_t *fragment) /* Complex plane confined to {-1.8 - 1.8} on both axis (slightly zoomed), no dynamic zooming is performed. */ for (imag = 1.8 + -(imagstep * (float)fragment->y), y = fragment->y; y < fragment->y + height; y++, imag += -imagstep) { for (real = -1.8 + realstep * (float)fragment->x, x = fragment->x; x < fragment->x + width; x++, buf++, real += realstep) { - *buf = colors[julia_iter(real, imag, creal, cimag, sizeof(colors) / sizeof(*colors))]; + *buf = colors[julia_iter(real, imag, ctxt->creal, ctxt->cimag, sizeof(colors) / sizeof(*colors))]; } buf += stride; } } + rototiller_module_t julia_module = { + .create_context = julia_create_context, + .destroy_context = julia_destroy_context, .prepare_frame = julia_prepare_frame, .render_fragment = julia_render_fragment, .name = "julia", |