1extern vec2 inputSize;
2extern vec2 textureSize;
3
4
5#define distortion 0.2
6
7/*
8#define f 0.6
9#define ox 0.5
10#define oy 0.5
11#define scale 0.8
12#define k1 0.7
13#define k2 -0.5
14
15vec2 barrelDistort(vec2 coord)
16{
17	vec2 xy = (coord - vec2(ox, oy))/vec2(f) * scale;
18
19	vec2 r = vec2(sqrt(dot(xy, xy)));
20
21	float r2 = float(r*r);
22
23	float r4 = r2*r2;
24
25	float coeff = (k1*r2 + k2*r4);
26
27	return ((xy+xy*coeff) * f) + vec2(ox, oy);
28}
29*/
30vec2 radialDistortion(vec2 coord, const vec2 ratio)
31{
32	float offsety = 1.0 - ratio.y;
33	coord.y -= offsety;
34	coord /= ratio;
35
36	vec2 cc = coord - 0.5;
37	float dist = dot(cc, cc) * distortion;
38	vec2 result = coord + cc * (1.0 + dist) * dist;
39
40	result *= ratio;
41	result.y += offsety;
42
43	return result;
44}
45/*
46vec4 checkTexelBounds(Image texture, vec2 coords, vec2 bounds)
47{
48	vec4 color = Texel(texture, coords) *
49
50	vec2 ss = step(coords, vec2(bounds.x, 1.0)) * step(vec2(0.0, bounds.y), coords);
51
52	color.rgb *= ss.x * ss.y;
53	color.a = step(color.a, ss.x * ss.y);
54
55	return color;
56}*/
57
58vec4 checkTexelBounds(Image texture, vec2 coords, vec2 bounds)
59{
60	vec2 ss = step(coords, vec2(bounds.x, 1.0)) * step(vec2(0.0, bounds.y), coords);
61	return Texel(texture, coords) * ss.x * ss.y;
62}
63
64/*
65vec4 checkTexelBounds(Image texture, vec2 coords)
66{
67	vec2 bounds = vec2(inputSize.x / textureSize.x, 1.0 - inputSize.y / textureSize.y);
68
69	vec4 color;
70	if (coords.x > bounds.x || coords.x < 0.0 || coords.y > 1.0 || coords.y < bounds.y)
71		color = vec4(0.0, 0.0, 0.0, 1.0);
72	else
73		color = Texel(texture, coords);
74
75	return color;
76}
77*/
78
79vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords)
80{
81	vec2 coords = radialDistortion(texture_coords, inputSize / textureSize);
82
83	vec4 texcolor = checkTexelBounds(texture, coords, vec2(inputSize.x / textureSize.x, 1.0 - inputSize.y / textureSize.y));
84	texcolor.a = 1.0;
85
86	return texcolor;
87}
88
89