1 | [vert.glsl] |
---|

2 | |
---|

3 | #version 120 |
---|

4 | |
---|

5 | attribute vec2 in_Position; |
---|

6 | |
---|

7 | varying vec4 pass_Position; |
---|

8 | |
---|

9 | void 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 | |
---|

19 | uniform sampler2D u_Texture; |
---|

20 | |
---|

21 | varying vec4 pass_Position; |
---|

22 | |
---|

23 | float segdist(vec2 p1, vec2 p2, vec2 a) |
---|

24 | { |
---|

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)); |
---|

28 | } |
---|

29 | |
---|

30 | void main(void) |
---|

31 | { |
---|

32 | float width = 800.0; |
---|

33 | float height = 600.0; |
---|

34 | float line_width = 1.8; |
---|

35 | vec4 delta = vec4(1.0 / 128, 0.0, |
---|

36 | 2.0 / 128, 0.0); |
---|

37 | |
---|

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; |
---|

46 | |
---|

47 | /* Quick hack: artificially compress display in Y */ |
---|

48 | c *= 0.3; |
---|

49 | |
---|

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); |
---|

55 | |
---|

56 | float d0 = segdist(p0, p1, a); |
---|

57 | float d1 = segdist(p1, p2, a); |
---|

58 | float d2 = segdist(p2, p3, a); |
---|

59 | |
---|

60 | float d = clamp(line_width - min(min(d0, d1), d2), 0.0, 1.0); |
---|

61 | |
---|

62 | /* Choose some funny colours */ |
---|

63 | gl_FragColor = vec4(p.y, d, mix(p.x, 0.3, d), 1.0); |
---|

64 | } |
---|

65 | |
---|