]> git.sesse.net Git - nageru/blobdiff - motion_search.frag
Implement coarse-to-fine. Still too buggy.
[nageru] / motion_search.frag
index 616e2080dc1084342dd1723c283ba9deab755fc9..e31a673484be9479f71a7428feec4348604bbf5d 100644 (file)
@@ -87,8 +87,16 @@ void main()
 
        mat2 H_inv = inverse(H);
 
-       // Fetch the initial guess for the flow.
-       vec2 initial_u = texture(flow_tex, flow_tc).xy;
+       // Fetch the initial guess for the flow. (We need the normalization step
+       // because densification works by accumulating; see the comments on the
+       // Densify class.)
+       vec3 prev_flow = texture(flow_tex, flow_tc).xyz;
+       vec2 initial_u;
+       if (prev_flow.z < 1e-3) {
+               initial_u = vec2(0.0, 0.0);
+       } else {
+               initial_u = prev_flow.xy / prev_flow.z;
+       }
        vec2 u = initial_u;
 
        for (uint i = 0; i < num_iterations; ++i) {
@@ -104,10 +112,13 @@ void main()
                                du += grad * (warped - t);
                        }
                }
-               u += H_inv * du * vec2(inv_image_width, inv_image_height);
+               u += (H_inv * du) * vec2(inv_image_width, inv_image_height);
        }
 
-       // TODO: reject if moving too far
+       // Reject if we moved too far.
+       if (length((u - initial_u) * vec2(image_width, image_height)) > patch_size) {
+               u = initial_u;
+       }
 
        out_flow = u;
 }