[vert.glsl]

#version 120

attribute vec2 in_Position;

varying vec4 pass_Position;

void main(void)
{
    pass_Position = vec4(0.5 * in_Position + 0.5, 0.0, 1.0);
    gl_Position = vec4(in_Position, 0.5, 1.0);
}

[frag.glsl]

#version 120

#if defined GL_ES
precision highp float;
#endif

uniform sampler2D u_Texture;

varying vec4 pass_Position;

float segdist(vec2 p1, vec2 p2, vec2 a)
{
    float d = max(1e-10, dot(p2 - p1, p2 - p1));
    float t = clamp(dot(a - p1, p2 - p1) / d, 0.0, 1.0);
    return distance(a, mix(p1, p2, t));
}

vec3 hsv2rgb(vec3 c)
{
    vec3 tmp = abs(fract(c[0] + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0 - 3.0);
    return c[2] * mix(vec3(1.0), clamp((tmp - 1.0), 0.0, 1.0), c[1]);
}

vec3 rgb2hsv(vec3 c)
{
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 v1 = mix(vec4(c.bg, K.wz),
                  vec4(c.gb, K.xy),
                  step(c.b, c.g));

    vec4 v3 = mix(vec4(v1.x, v1.yw, c.r),
                  vec4(c.r, v1.yzx),
                  step(v1.x, c.r));

    float chroma = v3.r - min(v3.a, v3.g);
float    h = abs(v3.b + (v3.a - v3.g) / (6.0 * chroma + 1.0e-10));
float    s = chroma / (v3.r + 1.0e-10);
float    v = v3.r;
 return vec3(h, s, v);
}

void main(void)
{
    float width = 800.0;
    float height = 600.0;
    float texture_width = 256.0;
    float line_width = 1.2;
    float dot_size = 1.0;
    vec4 delta = vec4(1.0 / texture_width, 0.0,
                      2.0 / texture_width, 0.0);

    vec2 p = pass_Position.xy;
    vec2 tc = vec2(floor(p.x * texture_width) / texture_width, p.y);
    float t = p.x * texture_width - floor(p.x * texture_width);
    vec4 c;
    c[0] = texture2D(u_Texture, tc - delta.xy).x;
    c[1] = texture2D(u_Texture, tc).x;
    c[2] = texture2D(u_Texture, tc + delta.xy).x;
    c[3] = texture2D(u_Texture, tc + delta.zw).x;

    /* Find the 4 closest points in screen space */
    vec2 p0 = vec2((tc.x - delta.x) * width, c[0] * height);
    vec2 p1 = vec2((tc.x          ) * width, c[1] * height);
    vec2 p2 = vec2((tc.x + delta.x) * width, c[2] * height);
    vec2 p3 = vec2((tc.x + delta.z) * width, c[3] * height);
    vec2 a = vec2(p.x * width, p.y * height);

    /* Compute distance to segments */
    float d =        segdist(p0, p1, a);
          d = min(d, segdist(p1, p2, a));
          d = min(d, segdist(p2, p3, a));

    /* Compute distance to dots */
    d = min(d, length(a - p0) - dot_size);
    d = min(d, length(a - p1) - dot_size);
    d = min(d, length(a - p2) - dot_size);
    d = min(d, length(a - p3) - dot_size);

    /* Add line width */
    float lum = clamp(line_width - d, 0.0, 1.0);

    /* Compensate for sRGB */
    lum = pow(1.0 - lum, 1.0 / 2.4);

    /* Choose some funny colours */
    vec4 col = vec4(mix(p.x, 1.0, lum), lum, lum, 1.0);
    col.rgb = hsv2rgb(rgb2hsv(col.rgb) * vec3(1.0, 1.0, 0.5) + vec3(p.x, 0.5, 0.0));
    col.rgb = hsv2rgb(rgb2hsv(col.rgb));
    col.rgb = hsv2rgb(rgb2hsv(col.rgb));
    col.rgb = hsv2rgb(rgb2hsv(col.rgb));
    gl_FragColor = col;
}

[vert.hlsl]

void main(float2 in_Position : POSITION,
          out float4 out_Position : POSITION,
          out float4 pass_Position : TEXCOORD0)
{
    pass_Position = float4(0.5 * in_Position + 0.5, 0.0, 1.0);
    out_Position = float4(in_Position, 0.5, 1.0);
}

[frag.hlsl]

float segdist(float2 p1, float2 p2, float2 a)
{
    float d = max(1e-10, dot(p2 - p1, p2 - p1));
    float t = clamp(dot(a - p1, p2 - p1) / d, 0.0, 1.0);
    return distance(a, lerp(p1, p2, t));
}

void main(in float4 pass_Position : TEXCOORD0,
          uniform sampler2D u_Texture,
          out float4 out_FragColor : COLOR)
{
    float width = 800.0;
    float height = 600.0;
    float texture_width = 256.0;
    float line_width = 1.2;
    float dot_size = 1.0;
    float4 delta = float4(1.0 / texture_width, 0.0,
                          2.0 / texture_width, 0.0);

    float2 p = pass_Position.xy;
    float2 tc = float2(floor(p.x * texture_width) / texture_width, p.y);
    float t = p.x * texture_width - floor(p.x * texture_width);
    float4 c;
    c[0] = tex2D(u_Texture, tc - delta.xy).x;
    c[1] = tex2D(u_Texture, tc).x;
    c[2] = tex2D(u_Texture, tc + delta.xy).x;
    c[3] = tex2D(u_Texture, tc + delta.zw).x;

    /* Find the 4 closest points in screen space */
    float2 p0 = float2((tc.x - delta.x) * width, c[0] * height);
    float2 p1 = float2((tc.x          ) * width, c[1] * height);
    float2 p2 = float2((tc.x + delta.x) * width, c[2] * height);
    float2 p3 = float2((tc.x + delta.z) * width, c[3] * height);
    float2 a = float2(p.x * width, p.y * height);

    /* Compute distance to segments */
    float d =        segdist(p0, p1, a);
          d = min(d, segdist(p1, p2, a));
          d = min(d, segdist(p2, p3, a));

    /* Compute distance to dots */
    d = min(d, length(a - p0) - dot_size);
    d = min(d, length(a - p1) - dot_size);
    d = min(d, length(a - p2) - dot_size);
    d = min(d, length(a - p3) - dot_size);

    /* Add line width */
    float lum = clamp(line_width - d, 0.0, 1.0);

    /* Compensate for sRGB */
    lum = pow(1.0 - lum, 1.0 / 2.4);

    /* Choose some funny colours */
    out_FragColor = float4(lerp(p.x, 1.0, lum), lum, lum, 1.0);
}

