diff options
Diffstat (limited to 'src/plasma-node.c')
-rw-r--r-- | src/plasma-node.c | 137 |
1 files changed, 109 insertions, 28 deletions
diff --git a/src/plasma-node.c b/src/plasma-node.c index 8afdb75..24b799a 100644 --- a/src/plasma-node.c +++ b/src/plasma-node.c @@ -26,7 +26,7 @@ #include "m4f.h" typedef struct plasma_node_t { - int *maga; + unsigned *maga; float *gloom; } plasma_node_t; @@ -74,7 +74,6 @@ static const char *plasma_fs = "" "uniform float alpha;" "uniform float time;" "uniform float gloom;" - "uniform int maga;" " float dostar(vec2 uv, float size) {" " uv.x = abs(uv.x);" @@ -98,6 +97,7 @@ static const char *plasma_fs = "" "void main() {" " float v;" " float stime = sin(time * .01) * 100.0;" + " vec3 col;" #ifdef __EMSCRIPTEN__ " vec2 c = UV;" @@ -118,24 +118,84 @@ static const char *plasma_fs = "" " v += sin(sqrt(c.x * c.x + c.y * c.y + 1.0) + stime);" - " if (maga == 1) {" - " float r, g, b;" - " float star = dostar(mod(cc, .2) - .1, (sin(time + c.x + c.y) * .5 + .5) * .01 + .01);" - " b = smoothstep(.25, .8, cos(2.666 * PI + PI * v + sin(time)));" - " star *= b;" - " b = max(star, b);" - " r = max(smoothstep(.25, .8, cos(PI * v + sin(time))), star);" - " g = max(smoothstep(.25, .8, cos(1.333 * PI + PI * v + sin(time))), star);" - - " gl_FragColor = vec4(max(r, g), g, max(b, g), 1.);" - " } else {" - " vec3 col = vec3(cos(PI * v + sin(time)), sin(PI * v + cos(time * .33)), cos(PI * v + sin(time * .66)));" - " gl_FragColor = vec4((col * .5 + .5) * (1. - gloom), alpha);" - " }" + " col = vec3(cos(PI * v + sin(time)), sin(PI * v + cos(time * .33)), cos(PI * v + sin(time * .66)));" + " gl_FragColor = vec4((col * .5 + .5) * (1. - gloom), alpha);" "}" ""; +static const char *plasma_maga_fs = "" +#ifdef __EMSCRIPTEN__ + "#version 100\n" +#else + "#version 120\n" +#endif + + "#define PI 3.1415926535897932384626433832795\n" + +#ifdef __EMSCRIPTEN__ + "precision mediump float;" + "varying vec2 UV;" +#endif + + "uniform float alpha;" + "uniform float time;" + "uniform float gloom;" + +" float dostar(vec2 uv, float size) {" +" uv.x = abs(uv.x);" +" float a = 6.2832/5.;" +" float d1 = dot(uv, vec2(sin(a), cos(a)));" +" a = 3. * 6.2832/5.;" +" float d2 = dot(uv, vec2(sin(a), cos(a)));" +" a = 2. * 6.2832/5.;" +" float d4 = dot(uv, vec2(sin(a), cos(a)));" +" float d = min(max(d1, d2), max(uv.y, d4));" +#ifdef __EMSCRIPTEN__ + /* GLSL 1.0 lacks fwidth() */ +" float w = .001;" +#else +" float w = fwidth(d);" +#endif + +" return smoothstep(w, -w, d - size);" +" }" + + "void main() {" + " float v;" + " float stime = sin(time * .01) * 100.0;" + " float star, r, g, b;" + +#ifdef __EMSCRIPTEN__ + " vec2 c = UV;" +#else + " vec2 c = gl_TexCoord[0].st;" +#endif + " vec2 cc = c;" + + // this zooms the texture coords in and out a bit with time + " c *= (sin(stime * .01) *.5 + .5) * 3.0 + 1.0;" + + // plasma calculations, stime instead of time directly to vary directions and speed + " v = sin((c.x + stime));" + " v += sin((c.y + stime) * .5);" + " v += sin((c.x + c.y +stime) * .5);" + + " c += vec2(sin(stime * .33), cos(stime * .5)) * 3.0;" + + " v += sin(sqrt(c.x * c.x + c.y * c.y + 1.0) + stime);" + + " star = dostar(mod(cc, .2) - .1, (sin(time + c.x + c.y) * .5 + .5) * .01 + .01);" + " b = smoothstep(.25, .8, cos(2.666 * PI + PI * v + sin(time)));" + " star *= b;" + " b = max(star, b);" + " r = max(smoothstep(.25, .8, cos(PI * v + sin(time))), star);" + " g = max(smoothstep(.25, .8, cos(1.333 * PI + PI * v + sin(time))), star);" + + " gl_FragColor = vec4(max(r, g), g, max(b, g), 1.);" + "}" +""; + static void plasma_uniforms(void *uniforms_ctxt, void *render_ctxt, unsigned n_uniforms, const int *uniforms, const m4f_t *model_x, float alpha) { plasma_node_t *plasma = uniforms_ctxt; @@ -145,12 +205,11 @@ static void plasma_uniforms(void *uniforms_ctxt, void *render_ctxt, unsigned n_u glUniform1f(uniforms[1], play_ticks(play, PLAY_TICKS_TIMER0) * .001f); // FIXME KLUDGE ALERT glUniformMatrix4fv(uniforms[2], 1, GL_FALSE, &model_x->m[0][0]); glUniform1f(uniforms[3], *(plasma->gloom)); - glUniform1i(uniforms[4], *(plasma->maga)); } /* create plasma rendering stage */ -stage_t * plasma_node_new(const stage_conf_t *conf, m4f_t *projection_x, float *gloom, int *maga) +stage_t * plasma_node_new(const stage_conf_t *conf, m4f_t *projection_x, float *gloom, unsigned *maga) { plasma_node_t *ctxt; @@ -170,15 +229,37 @@ stage_t * plasma_node_new(const stage_conf_t *conf, m4f_t *projection_x, float * fatal_if(!ctxt, "unable to allocate plasma_node_t"); ctxt->gloom = gloom; - ctxt->maga = maga; - - return shader_node_new_src(conf, plasma_vs, plasma_fs, projection_x, plasma_uniforms, ctxt, 5, - (const char *[]){ - "alpha", - "time", - "projection_x", - "gloom", - "maga", - } + + return shader_node_new_srcv(conf, 2, + (shader_src_conf_t[]){ + { + .vs_src = plasma_vs, + .fs_src = plasma_fs, + .transform = projection_x, + .uniforms_func = plasma_uniforms, + .uniforms_ctxt = ctxt, + .n_uniforms = 4, + .uniforms = (const char *[]){ + "alpha", + "time", + "projection_x", + "gloom", + }, + }, { + .vs_src = plasma_vs, + .fs_src = plasma_maga_fs, + .transform = projection_x, + .uniforms_func = plasma_uniforms, + .uniforms_ctxt = ctxt, + .n_uniforms = 4, + .uniforms = (const char *[]){ + "alpha", + "time", + "projection_x", + "gloom", + }, + }, + }, + maga ); } |