source: trunk/tutorial/04_texture.lolfx @ 2133

Last change on this file since 2133 was 2133, checked in by sam, 8 years ago

tutorial: change the terrain drawing into an antialiased line graph.

File size: 1.8 KB
Line 
1[vert.glsl]
2
3#version 120
4
5attribute vec2 in_Position;
6
7varying vec4 pass_Position;
8
9void main(void)
10{
11    pass_Position = vec4(0.5 * in_Position + 0.5, 0.0, 1.0);
12    gl_Position = vec4(in_Position, 0.5, 1.0);
13}
14
15[frag.glsl]
16
17#version 120
18
19uniform sampler2D u_Texture;
20
21varying vec4 pass_Position;
22
23float rand(in vec2 p, in float v)
24{
25    return fract(v * sin(dot(p, vec2(1298.9837, 7823.33145))));
26}
27
28float 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
42
43void main(void)
44{
45    float width = 800.0;
46    float height = 600.0;
47    float line_width = 2.0;
48
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;
54
55    float c0 = texture2D(u_Texture, tc0).x;
56    float c1 = texture2D(u_Texture, tc1).x;
57    float c2 = texture2D(u_Texture, tc2).x;
58    float c3 = texture2D(u_Texture, tc3).x;
59
60    /* Artificially compress in Y */
61    c0 *= 0.3;
62    c1 *= 0.3;
63    c2 *= 0.3;
64    c3 *= 0.3;
65
66    vec2 p0 = vec2(tc0.x * width, c0 * height);
67    vec2 p1 = vec2(tc1.x * width, c1 * height);
68    vec2 p2 = vec2(tc2.x * width, c2 * height);
69    vec2 p3 = vec2(tc3.x * width, c3 * height);
70    vec2 a = vec2(t.x * width, t.y * height);
71
72    float d0 = point2segment(p0, p1, a);
73    float d1 = point2segment(p1, p2, a);
74    float d2 = point2segment(p2, p3, a);
75
76    float d = clamp(line_width - min(min(d0, d1), d2), 0.0, 1.0);
77
78    gl_FragColor = vec4(t.y, d, d * 0.3, 1.0);
79}
80
Note: See TracBrowser for help on using the repository browser.