]> git.sesse.net Git - nageru/blob - motion_search.vert
Use textureSize() instead of sending in uniforms manually. Same result, less code...
[nageru] / motion_search.vert
1 #version 450 core
2
3 in vec2 position;
4 out vec2 flow_tc;
5 out vec2 patch_center;
6
7 uniform sampler2D flow_tex;
8
9 void main()
10 {
11         // Patch placement: We want the outermost patches to have centers exactly in the
12         // image corners, so that the bottom-left patch has centre (0,0) and the
13         // upper-right patch has center (1,1). The position we get in is _almost_ there;
14         // since the quad's corners are in (0,0) and (1,1), the fragment shader will get
15         // centers in x=0.5/w, x=1.5/w and so on (and similar for y).
16         //
17         // In other words, find some f(x) = ax + b so that
18         //
19         //   a 0.5 / w + b = 0
20         //   a (1.0 - 0.5 / w) + b = 1
21         //
22         // which gives
23         //
24         //   a = 1 / (w - 1)
25         //   b = w / 2 (w - 1)
26         vec2 flow_size = textureSize(flow_tex, 0);
27         vec2 a = flow_size / (flow_size - 1);
28         vec2 b = -1.0 / (2 * (flow_size - 1.0));
29         patch_center = a * position + b;
30
31         // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is:
32         //
33         //   2.000  0.000  0.000 -1.000
34         //   0.000  2.000  0.000 -1.000
35         //   0.000  0.000 -2.000 -1.000
36         //   0.000  0.000  0.000  1.000
37         gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0);
38         flow_tc = position;
39 }