Changeset 2136


Ignore:
Timestamp:
Dec 7, 2012, 1:41:42 PM (10 years ago)
Author:
sam
Message:

tutorial: optimise the line graph rendering shader.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tutorial/04_texture.lolfx

    r2134 r2136  
    2121varying vec4 pass_Position;
    2222
    23 float rand(in vec2 p, in float v)
     23float segdist(vec2 p1, vec2 p2, vec2 a)
    2424{
    25     return fract(v * sin(dot(p, vec2(1298.9837, 7823.33145))));
     25    float d = max(1e-10, dot(p2 - p1, p2 - p1));
     26    float t = clamp(dot(a - p1, p2 - p1) / d, 0.0, 1.0);
     27    return distance(a, mix(p1, p2, t));
    2628}
    27 
    28 float point2segment(vec2 p1, vec2 p2, vec2 a)
    29 {
    30     float l2 = dot(p2 - p1, p2 - p1);
    31     if (l2 == 0.0)
    32         return distance(a, p1);
    33     float t = dot(a - p1, p2 - p1) / l2;
    34     if (t < 0.0)
    35         return distance(a, p1);
    36     else if (t > 1.0)
    37         return distance(a, p2);
    38     vec2 proj = p1 + t * (p2 - p1);
    39     return distance(a, proj);
    40 }
    41 
    4229
    4330void main(void)
     
    4532    float width = 800.0;
    4633    float height = 600.0;
    47     float line_width = 2.0;
     34    float line_width = 1.8;
     35    vec4 delta = vec4(1.0 / 128, 0.0,
     36                      2.0 / 128, 0.0);
    4837
    49     vec2 t = pass_Position.xy;
    50     vec2 tc1 = floor(t * 128.0) / 128.0;
    51     vec2 tc2 = tc1 + vec2(1.0, 1.0) / 128.0;
    52     vec2 tc0 = tc1 - vec2(1.0, 1.0) / 128.0;
    53     vec2 tc3 = tc2 + vec2(1.0, 1.0) / 128.0;
     38    vec2 p = pass_Position.xy;
     39    vec2 tc = vec2(floor(p.x * 128.0) / 128.0, p.y);
     40    float t = p.x * 128.0 - floor(p.x * 128.0);
     41    vec4 c;
     42    c[0] = texture2D(u_Texture, tc - delta.xy).x;
     43    c[1] = texture2D(u_Texture, tc).x;
     44    c[2] = texture2D(u_Texture, tc + delta.xy).x;
     45    c[3] = texture2D(u_Texture, tc + delta.zw).x;
    5446
    55     vec4 c;
    56     c[0] = texture2D(u_Texture, tc0).x;
    57     c[1] = texture2D(u_Texture, tc1).x;
    58     c[2] = texture2D(u_Texture, tc2).x;
    59     c[3] = texture2D(u_Texture, tc3).x;
    60 
    61     /* Artificially compress in Y */
     47    /* Quick hack: artificially compress display in Y */
    6248    c *= 0.3;
    6349
    64     vec2 p0 = vec2(tc0.x * width, c[0] * height);
    65     vec2 p1 = vec2(tc1.x * width, c[1] * height);
    66     vec2 p2 = vec2(tc2.x * width, c[2] * height);
    67     vec2 p3 = vec2(tc3.x * width, c[3] * height);
    68     vec2 a = vec2(t.x * width, t.y * height);
     50    vec2 p0 = vec2((tc.x - delta.x) * width, c[0] * height);
     51    vec2 p1 = vec2((tc.x) * width,          c[1] * height);
     52    vec2 p2 = vec2((tc.x + delta.x) * width, c[2] * height);
     53    vec2 p3 = vec2((tc.x + delta.z) * width, c[3] * height);
     54    vec2 a = vec2(p.x * width, p.y * height);
    6955
    70     float d0 = point2segment(p0, p1, a);
    71     float d1 = point2segment(p1, p2, a);
    72     float d2 = point2segment(p2, p3, a);
     56    float d0 = segdist(p0, p1, a);
     57    float d1 = segdist(p1, p2, a);
     58    float d2 = segdist(p2, p3, a);
    7359
    7460    float d = clamp(line_width - min(min(d0, d1), d2), 0.0, 1.0);
    7561
    76     gl_FragColor = vec4(t.y, d, d * 0.3, 1.0);
     62    /* Choose some funny colours */
     63    gl_FragColor = vec4(p.y, d, mix(p.x, 0.3, d), 1.0);
    7764}
    7865
Note: See TracChangeset for help on using the changeset viewer.