[vert.glsl]

#version 130

in vec2 in_position;

uniform float u_time;

out vec4 pass_position;
out vec3 water[4];
out vec3 fire[4];

float mod289(float x)
{
    return x - floor(x * (1.0 / 289.0)) * 289.0;
}

void main(void)
{
    mat3 r = mat3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
    vec3 p_pos = r * vec3(in_position * vec2(16.0, 9.0), 0.0);
    vec3 p_time = r * vec3(0.0, 0.0, u_time * 2.0);

    /* Noise sampling points for water */
    water[0] = p_pos / 2.0 + p_time;
    water[1] = p_pos / 4.0 + p_time;
    water[2] = p_pos / 8.0 + p_time;
    water[3] = p_pos / 16.0 + p_time;

    /* Noise sampling points for fire */
    p_pos = 16.0 * p_pos - r * vec3(0.0, mod289(u_time) * 128.0, 0.0);
    fire[0] = p_pos / 2.0 + p_time * 2.0;
    fire[1] = p_pos / 4.0 + p_time * 1.5;
    fire[2] = p_pos / 8.0 + p_time;
    fire[3] = p_pos / 16.0 + p_time;

    /* Pass rotated screen coordinates */
    pass_position.xy = in_position;
    mat2 rot = mat2(cos(u_time), sin(u_time), -sin(u_time), cos(u_time));
    pass_position.zw = rot * in_position;

    gl_Position = vec4(in_position, 0.0, 1.0);
}

[frag.glsl]

#version 130

#if defined GL_ES
precision highp float;
#endif

in vec4 pass_position;
in vec3 water[4];
in vec3 fire[4];

uniform float u_time;

vec4 mod289(vec4 x)
{
    return x - floor(x * (1.0 / 289.0)) * 289.0;
}

vec4 perm(vec4 x)
{
    return mod289(((x * 34.0) + 1.0) * x);
}

float noise3d(vec3 p)
{
    vec3 a = floor(p);
    vec3 d = p - a;
    d = d * d * (3.0 - 2.0 * d);

    vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0);
    vec4 k1 = perm(b.xyxy);
    vec4 k2 = perm(k1.xyxy + b.zzww);

    vec4 c = k2 + a.zzzz;
    vec4 k3 = perm(c);
    vec4 k4 = perm(c + 1.0);

    vec4 o1 = fract(k3 * (1.0 / 41.0));
    vec4 o2 = fract(k4 * (1.0 / 41.0));

    vec4 o3 = o2 * d.z + o1 * (1.0 - d.z);
    vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);

    return o4.y * d.y + o4.x * (1.0 - d.y);
}

void main(void)
{
    /* Dither the transition between water and fire */
    float test = pass_position.z * pass_position.w + 1.5 * sin(u_time);
    vec2 d = vec2(16.0, 9.0) * pass_position.xy;
    test += 0.5 * (length(fract(d) - 0.5) - length(fract(d + 0.5) - 0.5));

    /* Compute 4 octaves of noise */
#if defined GL_ES
    vec4 n = vec4(noise3d((test > 0.0) ? fire[0] : water[0]),
                  noise3d((test > 0.0) ? fire[1] : water[1]),
                  noise3d((test > 0.0) ? fire[2] : water[2]),
                  noise3d((test > 0.0) ? fire[3] : water[3]));
#else
    vec3 points[4] = (test > 0.0) ? fire : water;
    vec4 n = vec4(noise3d(points[0]),
                  noise3d(points[1]),
                  noise3d(points[2]),
                  noise3d(points[3]));
#endif

    vec4 color;

    if (test > 0.0)
    {
        /* Use noise results for fire */
        float p = dot(n, vec4(0.125, 0.125, 0.25, 0.5));

        /* Fade to black on top of screen */
        p -= pass_position.y * 0.8 + 0.25;
        p = max(p, 0.0);
        p = min(p, 1.0);

        float q = p * p * (3.0 - 2.0 * p);
        float r = q * q * (3.0 - 2.0 * q);
        color = vec4(min(q * 2.0, 1.0),
                     max(r * 1.5 - 0.5, 0.0),
                     max(q * 8.0 - 7.3, 0.0),
                     1.0);
    }
    else
    {
        /* Use noise results for water */
        float p = dot(abs(2.0 * n - 1.0),
                      vec4(0.5, 0.25, 0.125, 0.125));
        float q = sqrt(p);

        color = vec4(1.0 - q,
                     1.0 - 0.5 * q,
                     1.0,
                     1.0);
    }

    gl_FragColor = color;
}

