Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_clip / clip_utils.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/clip_utils.c
29  *  \ingroup spclip
30  */
31
32 #include "DNA_scene_types.h"
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_utildefines.h"
37 #include "BLI_listbase.h"
38 #include "BLI_string.h"
39
40 #include "BKE_animsys.h"
41 #include "BKE_context.h"
42 #include "BKE_movieclip.h"
43 #include "BKE_tracking.h"
44 #include "BKE_depsgraph.h"
45
46 #include "GPU_immediate.h"
47 #include "GPU_matrix.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "ED_screen.h"
53 #include "ED_clip.h"
54
55 #include "UI_interface.h"
56 #include "UI_resources.h"
57 #include "UI_view2d.h"
58
59 #include "clip_intern.h"    // own include
60
61 void clip_graph_tracking_values_iterate_track(
62         SpaceClip *sc, MovieTrackingTrack *track, void *userdata,
63         void (*func)(void *userdata, MovieTrackingTrack *track, MovieTrackingMarker *marker, int coord,
64                      int scene_framenr, float val),
65         void (*segment_start)(void *userdata, MovieTrackingTrack *track, int coord, bool is_point),
66         void (*segment_end)(void *userdata, int coord))
67 {
68         MovieClip *clip = ED_space_clip_get_clip(sc);
69         int width, height, coord;
70
71         BKE_movieclip_get_size(clip, &sc->user, &width, &height);
72
73         for (coord = 0; coord < 2; coord++) {
74                 int i, prevfra = track->markers[0].framenr;
75                 bool open = false;
76                 float prevval = 0.0f;
77
78                 for (i = 0; i < track->markersnr; i++) {
79                         MovieTrackingMarker *marker = &track->markers[i];
80                         float val;
81
82                         if (marker->flag & MARKER_DISABLED) {
83                                 if (open) {
84                                         if (segment_end)
85                                                 segment_end(userdata, coord);
86
87                                         open = false;
88                                 }
89
90                                 continue;
91                         }
92
93                         if (!open) {
94                                 if (segment_start) {
95                                         if ((i + 1) == track->markersnr) {
96                                                 segment_start(userdata, track, coord, true);
97                                         }
98                                         else {
99                                                 segment_start(userdata, track, coord, (track->markers[i + 1].flag & MARKER_DISABLED));
100                                         }
101                                 }
102
103                                 open = true;
104                                 prevval = marker->pos[coord];
105                         }
106
107                         /* value is a pixels per frame speed */
108                         val = (marker->pos[coord] - prevval) * ((coord == 0) ? (width) : (height));
109                         val /= marker->framenr - prevfra;
110
111                         if (func) {
112                                 int scene_framenr = BKE_movieclip_remap_clip_to_scene_frame(clip, marker->framenr);
113
114                                 func(userdata, track, marker, coord, scene_framenr, val);
115                         }
116
117                         prevval = marker->pos[coord];
118                         prevfra = marker->framenr;
119                 }
120
121                 if (open) {
122                         if (segment_end)
123                                 segment_end(userdata, coord);
124                 }
125         }
126 }
127
128 void clip_graph_tracking_values_iterate(
129         SpaceClip *sc, bool selected_only, bool include_hidden, void *userdata,
130         void (*func)(void *userdata, MovieTrackingTrack *track, MovieTrackingMarker *marker,
131                      int coord, int scene_framenr, float val),
132         void (*segment_start)(void *userdata, MovieTrackingTrack *track, int coord, bool is_point),
133         void (*segment_end)(void *userdata, int coord))
134 {
135         MovieClip *clip = ED_space_clip_get_clip(sc);
136         MovieTracking *tracking = &clip->tracking;
137         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
138         MovieTrackingTrack *track;
139
140         for (track = tracksbase->first; track; track = track->next) {
141                 if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0)
142                         continue;
143
144                 if (selected_only && !TRACK_SELECTED(track))
145                         continue;
146
147                 clip_graph_tracking_values_iterate_track(sc, track, userdata, func, segment_start, segment_end);
148         }
149 }
150
151 void clip_graph_tracking_iterate(SpaceClip *sc, bool selected_only, bool include_hidden, void *userdata,
152                                  void (*func)(void *userdata, MovieTrackingMarker *marker))
153 {
154         MovieClip *clip = ED_space_clip_get_clip(sc);
155         MovieTracking *tracking = &clip->tracking;
156         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
157         MovieTrackingTrack *track;
158
159         for (track = tracksbase->first; track; track = track->next) {
160                 int i;
161
162                 if (!include_hidden && (track->flag & TRACK_HIDDEN) != 0)
163                         continue;
164
165                 if (selected_only && !TRACK_SELECTED(track))
166                         continue;
167
168                 for (i = 0; i < track->markersnr; i++) {
169                         MovieTrackingMarker *marker = &track->markers[i];
170
171                         if (marker->flag & MARKER_DISABLED)
172                                 continue;
173
174                         if (func)
175                                 func(userdata, marker);
176                 }
177         }
178 }
179
180 void clip_delete_track(bContext *C, MovieClip *clip, MovieTrackingTrack *track)
181 {
182         MovieTracking *tracking = &clip->tracking;
183         MovieTrackingTrack *act_track = BKE_tracking_track_get_active(tracking);
184         ListBase *tracksbase = BKE_tracking_get_active_tracks(tracking);
185         bool has_bundle = false;
186         char track_name_escaped[MAX_NAME], prefix[MAX_NAME * 2];
187         const bool used_for_stabilization = (track->flag & (TRACK_USE_2D_STAB | TRACK_USE_2D_STAB_ROT));
188
189         if (track == act_track)
190                 tracking->act_track = NULL;
191
192         /* handle reconstruction display in 3d viewport */
193         if (track->flag & TRACK_HAS_BUNDLE)
194                 has_bundle = true;
195
196         /* Make sure no plane will use freed track */
197         BKE_tracking_plane_tracks_remove_point_track(tracking, track);
198
199         /* Delete f-curves associated with the track (such as weight, i.e.) */
200         BLI_strescape(track_name_escaped, track->name, sizeof(track_name_escaped));
201         BLI_snprintf(prefix, sizeof(prefix), "tracks[\"%s\"]", track_name_escaped);
202         BKE_animdata_fix_paths_remove(&clip->id, prefix);
203
204         BKE_tracking_track_free(track);
205         BLI_freelinkN(tracksbase, track);
206
207         WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
208
209         if (used_for_stabilization) {
210                 WM_event_add_notifier(C, NC_MOVIECLIP | ND_DISPLAY, clip);
211         }
212
213         DAG_id_tag_update(&clip->id, 0);
214
215         if (has_bundle)
216                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, NULL);
217 }
218
219 void clip_delete_marker(bContext *C, MovieClip *clip, MovieTrackingTrack *track,
220                         MovieTrackingMarker *marker)
221 {
222         if (track->markersnr == 1) {
223                 clip_delete_track(C, clip, track);
224         }
225         else {
226                 BKE_tracking_marker_delete(track, marker->framenr);
227
228                 WM_event_add_notifier(C, NC_MOVIECLIP | NA_EDITED, clip);
229         }
230 }
231
232 void clip_view_center_to_point(SpaceClip *sc, float x, float y)
233 {
234         int width, height;
235         float aspx, aspy;
236
237         ED_space_clip_get_size(sc, &width, &height);
238         ED_space_clip_get_aspect(sc, &aspx, &aspy);
239
240         sc->xof = (x - 0.5f) * width * aspx;
241         sc->yof = (y - 0.5f) * height * aspy;
242 }
243
244 void clip_draw_cfra(SpaceClip *sc, ARegion *ar, Scene *scene)
245 {
246         /* Draw a light green line to indicate current frame */
247         View2D *v2d = &ar->v2d;
248         float x = (float)(sc->user.framenr * scene->r.framelen);
249
250         unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
251
252         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
253         immUniformThemeColor(TH_CFRAME);
254         glLineWidth(2.0f);
255
256         immBegin(PRIM_LINES, 2);
257         immVertex2f(pos, x, v2d->cur.ymin);
258         immVertex2f(pos, x, v2d->cur.ymax);
259         immEnd();
260
261         immUnbindProgram();
262
263         UI_view2d_view_orthoSpecial(ar, v2d, 1);
264
265         /* because the frame number text is subject to the same scaling as the contents of the view */
266         float xscale;
267         UI_view2d_scale_get(v2d, &xscale, NULL);
268         gpuPushMatrix();
269         gpuScale2f(1.0f / xscale, 1.0f);
270
271         ED_region_cache_draw_curfra_label(sc->user.framenr, (float)sc->user.framenr * xscale, 18);
272
273         /* restore view transform */
274         gpuPopMatrix();
275 }
276
277 void clip_draw_sfra_efra(View2D *v2d, Scene *scene)
278 {
279         UI_view2d_view_ortho(v2d);
280
281         /* currently clip editor supposes that editing clip length is equal to scene frame range */
282         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
283         glEnable(GL_BLEND);
284
285         unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_F32, 2, KEEP_FLOAT);
286         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
287
288         immUniformColor4f(0.0f, 0.0f, 0.0f, 0.4f);
289         immRectf(pos, v2d->cur.xmin, v2d->cur.ymin, (float)SFRA, v2d->cur.ymax);
290         immRectf(pos, (float)EFRA, v2d->cur.ymin, v2d->cur.xmax, v2d->cur.ymax);
291
292         glDisable(GL_BLEND);
293
294         immUniformThemeColorShade(TH_BACK, -60);
295
296         /* thin lines where the actual frames are */
297         glLineWidth(1.0f);
298
299         immBegin(PRIM_LINES, 4);
300         immVertex2f(pos, (float)SFRA, v2d->cur.ymin);
301         immVertex2f(pos, (float)SFRA, v2d->cur.ymax);
302         immVertex2f(pos, (float)EFRA, v2d->cur.ymin);
303         immVertex2f(pos, (float)EFRA, v2d->cur.ymax);
304         immEnd();
305
306         immUnbindProgram();
307 }