[vert.glsl]

#version 130

in vec3 in_position;
in vec4 in_color;

uniform mat4 u_projection;
uniform mat4 u_view;
uniform mat4 u_model;

out vec4 pass_color;

void main()
{
    gl_Position = u_projection * u_view * u_model
                * vec4(in_position, 1.0);
    pass_color = in_color;
}

[frag.glsl]

#version 130

#if defined GL_ES
precision highp float;
#endif

in vec4 pass_color;

mat4 bayer = mat4( 0.0, 12.0,  3.0, 15.0,
                   8.0,  4.0, 11.0,  7.0,
                   2.0, 14.0,  1.0, 13.0,
                  10.0,  6.0,  9.0,  5.0);

mat4 cluster = mat4(12.0,  5.0,  6.0, 13.0,
                     4.0,  0.0,  1.0,  7.0,
                    11.0,  3.0,  2.0,  8.0,
                    15.0, 10.0,  9.0, 14.0);

float rand(vec2 p)
{
    return fract(sin(dot(p, vec2(12.9898, 78.2333))) * 123.4567);
}

void main()
{
    vec4 col = pass_color;
#if defined GL_ES
    int dx = int(mod(gl_FragCoord.x, 4.0));
    int dy = int(mod(gl_FragCoord.y, 4.0));
    float t;
    if (dx == 0)
    {
        if (dy == 0) t = cluster[0][0]; else if (dy == 1) t = cluster[0][1]; else if (dy == 2) t = cluster[0][2]; else t = cluster[0][3];
    }
    else if (dx == 1)
    {
        if (dy == 0) t = cluster[1][0]; else if (dy == 1) t = cluster[1][1]; else if (dy == 2) t = cluster[1][2]; else t = cluster[1][3];
    }
    else if (dx == 2)
    {
        if (dy == 0) t = cluster[2][0]; else if (dy == 1) t = cluster[2][1]; else if (dy == 2) t = cluster[2][2]; else t = cluster[2][3];
    }
    else
    {
        if (dy == 0) t = cluster[3][0]; else if (dy == 1) t = cluster[3][1]; else if (dy == 2) t = cluster[3][2]; else t = cluster[3][3];
    }
#else
    float t = cluster[int(mod(gl_FragCoord.x, 4.0))]
                     [int(mod(gl_FragCoord.y, 4.0))];
#endif
    t += rand(gl_FragCoord.xy) - 0.5;
    t = (t + 0.5) / 17.0;
    col.x += fract(t - col.x) - t;
    col.y += fract(t - col.y) - t;
    col.z += fract(t - col.z) - t;
    gl_FragColor = col;
}

