svn merge ^/trunk/blender -r42973:42991
[blender.git] / source / blender / editors / space_clip / clip_draw.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_draw.c
29  *  \ingroup spclip
30  */
31
32 #include "DNA_gpencil_types.h"
33 #include "DNA_movieclip_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_object_types.h"   /* SELECT */
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BKE_context.h"
40 #include "BKE_movieclip.h"
41 #include "BKE_tracking.h"
42
43 #include "IMB_imbuf_types.h"
44 #include "IMB_imbuf.h"
45
46 #include "BLI_utildefines.h"
47 #include "BLI_math.h"
48 #include "BLI_string.h"
49 #include "BLI_rect.h"
50 #include "BLI_math_base.h"
51
52 #include "ED_screen.h"
53 #include "ED_clip.h"
54 #include "ED_gpencil.h"
55
56 #include "BIF_gl.h"
57 #include "BIF_glutil.h"
58
59 #include "WM_api.h"
60 #include "WM_types.h"
61
62 #include "UI_interface.h"
63 #include "UI_resources.h"
64 #include "UI_view2d.h"
65
66 #include "RNA_access.h"
67
68 #include "BLF_api.h"
69
70 #include "clip_intern.h"        // own include
71
72 /*********************** main area drawing *************************/
73
74 void clip_draw_curfra_label(SpaceClip *sc, float x, float y)
75 {
76         uiStyle *style= UI_GetStyle();
77         int fontid= style->widget.uifont_id;
78         char str[32];
79         float fontsize, fontwidth;
80
81         /* frame number */
82         BLF_size(fontid, 11.0f, U.dpi);
83         BLI_snprintf(str, sizeof(str), "%d", sc->user.framenr);
84         fontsize= BLF_height(fontid, str);
85         fontwidth= BLF_width(fontid, str);
86
87         glRecti(x, y, x+fontwidth+6, y+fontsize+4);
88
89         UI_ThemeColor(TH_TEXT);
90         BLF_position(fontid, x+2.0f, y+2.0f, 0.0f);
91         BLF_draw(fontid, str, strlen(str));
92 }
93
94 static void draw_movieclip_cache(SpaceClip *sc, ARegion *ar, MovieClip *clip, Scene *scene)
95 {
96         float x;
97         int *points, totseg, i, a;
98         float sfra= SFRA, efra= EFRA, framelen= ar->winx/(efra-sfra+1);
99
100         glEnable(GL_BLEND);
101
102         /* cache background */
103         glColor4ub(128, 128, 255, 64);
104         glRecti(0, 0, ar->winx, 8);
105
106         /* cached segments -- could be usefu lto debug caching strategies */
107         BKE_movieclip_get_cache_segments(clip, &sc->user, &totseg, &points);
108         if(totseg) {
109                 glColor4ub(128, 128, 255, 128);
110
111                 for(a= 0; a<totseg; a++) {
112                         float x1, x2;
113
114                         x1= (points[a*2]-sfra)/(efra-sfra+1)*ar->winx;
115                         x2= (points[a*2+1]-sfra+1)/(efra-sfra+1)*ar->winx;
116
117                         glRecti(x1, 0, x2, 8);
118                 }
119         }
120
121         /* track */
122         if(clip->tracking.act_track) {
123                 MovieTrackingTrack *track= clip->tracking.act_track;
124
125                 for(i= sfra, a= 0; i <= efra; i++) {
126                         int framenr;
127                         MovieTrackingMarker *marker;
128
129                         while(a<track->markersnr) {
130                                 if(track->markers[a].framenr>=i)
131                                         break;
132
133                                 if(a<track->markersnr-1 && track->markers[a+1].framenr>i)
134                                         break;
135
136                                 a++;
137                         }
138
139                         if(a<track->markersnr) marker= &track->markers[a];
140                         else marker= &track->markers[track->markersnr-1];
141
142                         if((marker->flag&MARKER_DISABLED)==0) {
143                                 framenr= marker->framenr;
144
145                                 if(framenr!=i) glColor4ub(128, 128, 0, 96);
146                                 else if((marker->flag&MARKER_TRACKED)==0) glColor4ub(255, 255, 0, 196);
147                                 else glColor4ub(255, 255, 0, 96);
148
149                                 glRecti((i-sfra)*framelen, 0, (i-sfra+1)*framelen, 4);
150                         }
151                 }
152         }
153
154         /* failed frames */
155         if(clip->tracking.reconstruction.flag&TRACKING_RECONSTRUCTED) {
156                 int n= clip->tracking.reconstruction.camnr;
157                 MovieReconstructedCamera *cameras= clip->tracking.reconstruction.cameras;
158
159                 glColor4ub(255, 0, 0, 96);
160
161                 for(i= sfra, a= 0; i <= efra; i++) {
162                         int ok= 0;
163
164                         while(a<n) {
165                                 if(cameras[a].framenr==i) {
166                                         ok= 1;
167                                         break;
168                                 }
169                                 else if(cameras[a].framenr>i) {
170                                         break;
171                                 }
172
173                                 a++;
174                         }
175
176                         if(!ok)
177                                 glRecti((i-sfra)*framelen, 0, (i-sfra+1)*framelen, 8);
178                 }
179         }
180
181         glDisable(GL_BLEND);
182
183         /* current frame */
184         x= (sc->user.framenr-sfra)/(efra-sfra+1)*ar->winx;
185
186         UI_ThemeColor(TH_CFRAME);
187         glRecti(x, 0, x+framelen, 8);
188
189         clip_draw_curfra_label(sc, x, 8.0f);
190 }
191
192 static void draw_movieclip_notes(SpaceClip *sc, ARegion *ar)
193 {
194         MovieClip *clip= ED_space_clip(sc);
195         MovieTracking *tracking= &clip->tracking;
196         char str[256]= {0};
197         int block= 0;
198
199         if(tracking->stats) {
200                 BLI_strncpy(str, tracking->stats->message, sizeof(str));
201                 block= 1;
202         } else {
203                 if(sc->flag&SC_LOCK_SELECTION)
204                         strcpy(str, "Locked");
205         }
206
207         if(str[0])
208                 ED_region_info_draw(ar, str, block, 0.6f);
209 }
210
211 static void draw_movieclip_buffer(SpaceClip *sc, ARegion *ar, ImBuf *ibuf,
212                         int width, int height, float zoomx, float zoomy)
213 {
214         int x, y;
215         MovieClip *clip= ED_space_clip(sc);
216
217         /* set zoom */
218         glPixelZoom(zoomx*width/ibuf->x, zoomy*height/ibuf->y);
219
220         /* find window pixel coordinates of origin */
221         UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x, &y);
222
223         if(sc->flag&SC_MUTE_FOOTAGE) {
224                 glColor3f(0.0f, 0.0f, 0.0f);
225                 glRectf(x, y, x+zoomx*width, y+zoomy*height);
226         } else {
227                 if(ibuf->rect_float && !ibuf->rect) {
228                         IMB_rect_from_float(ibuf);
229                 }
230
231                 if(ibuf->rect)
232                         glaDrawPixelsSafe(x, y, ibuf->x, ibuf->y, ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
233         }
234
235         /* draw boundary border for frame if stabilization is enabled */
236         if(sc->flag&SC_SHOW_STABLE && clip->tracking.stabilization.flag&TRACKING_2D_STABILIZATION) {
237                 glColor3f(0.0f, 0.0f, 0.0f);
238                 glLineStipple(3, 0xaaaa);
239                 glEnable(GL_LINE_STIPPLE);
240                 glEnable(GL_COLOR_LOGIC_OP);
241                 glLogicOp(GL_NOR);
242
243                 glPushMatrix();
244                 glTranslatef(x, y, 0);
245
246                 glScalef(zoomx, zoomy, 0);
247                 glMultMatrixf(sc->stabmat);
248
249                 glBegin(GL_LINE_LOOP);
250                         glVertex2f(0.0f, 0.0f);
251                         glVertex2f(ibuf->x, 0.0f);
252                         glVertex2f(ibuf->x, ibuf->y);
253                         glVertex2f(0.0f, ibuf->y);
254                 glEnd();
255
256                 glPopMatrix();
257
258                 glDisable(GL_COLOR_LOGIC_OP);
259                 glDisable(GL_LINE_STIPPLE);
260         }
261
262
263         /* reset zoom */
264         glPixelZoom(1.0f, 1.0f);
265 }
266
267 static void draw_track_path(SpaceClip *sc, MovieClip *UNUSED(clip), MovieTrackingTrack *track)
268 {
269         int count= sc->path_length;
270         int i, a, b, curindex= -1;
271         float path[102][2];
272         int tiny= sc->flag&SC_SHOW_TINY_MARKER, framenr;
273         MovieTrackingMarker *marker;
274
275         if(count==0)
276                 return;
277
278         marker= BKE_tracking_get_marker(track, sc->user.framenr);
279         if(marker->framenr!=sc->user.framenr || marker->flag&MARKER_DISABLED)
280                 return;
281
282         framenr= marker->framenr;
283
284         a= count;
285         i= framenr-1;
286         while(i>=framenr-count) {
287                 marker= BKE_tracking_get_marker(track, i);
288
289                 if(!marker || marker->flag&MARKER_DISABLED)
290                         break;
291
292                 if(marker->framenr==i) {
293                         add_v2_v2v2(path[--a], marker->pos, track->offset);
294                         ED_clip_point_undistorted_pos(sc, path[a], path[a]);
295
296                         if(marker->framenr==sc->user.framenr)
297                                 curindex= a;
298                 } else
299                         break;
300
301                 i--;
302         }
303
304         b= count;
305         i= framenr;
306         while(i<=framenr+count) {
307                 marker= BKE_tracking_get_marker(track, i);
308
309                 if(!marker || marker->flag&MARKER_DISABLED)
310                         break;
311
312                 if(marker->framenr==i) {
313                         if(marker->framenr==sc->user.framenr)
314                                 curindex= b;
315
316                         add_v2_v2v2(path[b++], marker->pos, track->offset);
317                         ED_clip_point_undistorted_pos(sc, path[b-1], path[b-1]);
318                 } else
319                         break;
320
321                 i++;
322         }
323
324         if(!tiny) {
325                 UI_ThemeColor(TH_MARKER_OUTLINE);
326
327                 if(TRACK_VIEW_SELECTED(sc, track)) {
328                         glPointSize(5.0f);
329                         glBegin(GL_POINTS);
330                                 for(i= a; i<b; i++) {
331                                         if(i!=curindex)
332                                                 glVertex2f(path[i][0], path[i][1]);
333                                 }
334                         glEnd();
335                 }
336
337                 glLineWidth(3.0f);
338                 glBegin(GL_LINE_STRIP);
339                         for(i= a; i<b; i++)
340                                 glVertex2f(path[i][0], path[i][1]);
341                 glEnd();
342                 glLineWidth(1.0f);
343         }
344
345         UI_ThemeColor(TH_PATH_BEFORE);
346
347         if(TRACK_VIEW_SELECTED(sc, track)) {
348                 glPointSize(3.0f);
349                 glBegin(GL_POINTS);
350                         for(i= a; i<b; i++) {
351                                 if(i==count+1)
352                                         UI_ThemeColor(TH_PATH_AFTER);
353
354                                 if(i!=curindex)
355                                         glVertex2f(path[i][0], path[i][1]);
356                         }
357                 glEnd();
358         }
359
360         UI_ThemeColor(TH_PATH_BEFORE);
361
362         glBegin(GL_LINE_STRIP);
363                 for(i= a; i<b; i++) {
364                         if(i==count+1)
365                                 UI_ThemeColor(TH_PATH_AFTER);
366
367                         glVertex2f(path[i][0], path[i][1]);
368                 }
369         glEnd();
370         glPointSize(1.0f);
371 }
372
373 static void draw_marker_outline(SpaceClip *sc, MovieTrackingTrack *track, MovieTrackingMarker *marker, float marker_pos[2], int width, int height)
374 {
375         int tiny= sc->flag&SC_SHOW_TINY_MARKER;
376         int show_search= 0;
377         float px[2];
378
379         UI_ThemeColor(TH_MARKER_OUTLINE);
380
381         px[0]= 1.0f/width/sc->zoom;
382         px[1]= 1.0f/height/sc->zoom;
383
384         if((marker->flag&MARKER_DISABLED)==0) {
385                 float pos[2];
386                 rctf r;
387
388                 BLI_init_rctf(&r, track->pat_min[0], track->pat_max[0], track->pat_min[1], track->pat_max[1]);
389                 add_v2_v2v2(pos, marker->pos, track->offset);
390
391                 ED_clip_point_undistorted_pos(sc, pos, pos);
392
393                 if(BLI_in_rctf(&r, pos[0]-marker_pos[0], pos[1]-marker_pos[1])) {
394                         if(tiny) glPointSize(3.0f);
395                         else glPointSize(4.0f);
396                         glBegin(GL_POINTS);
397                                 glVertex2f(pos[0], pos[1]);
398                         glEnd();
399                         glPointSize(1.0f);
400                 } else {
401                         if(!tiny) glLineWidth(3.0f);
402                         glBegin(GL_LINES);
403                                 glVertex2f(pos[0] + px[0]*2, pos[1]);
404                                 glVertex2f(pos[0] + px[0]*8, pos[1]);
405
406                                 glVertex2f(pos[0] - px[0]*2, pos[1]);
407                                 glVertex2f(pos[0] - px[0]*8, pos[1]);
408
409                                 glVertex2f(pos[0], pos[1] - px[1]*2);
410                                 glVertex2f(pos[0], pos[1] - px[1]*8);
411
412                                 glVertex2f(pos[0], pos[1] + px[1]*2);
413                                 glVertex2f(pos[0], pos[1] + px[1]*8);
414                         glEnd();
415                         if(!tiny) glLineWidth(1.0f);
416                 }
417         }
418
419         /* pattern and search outline */
420         glPushMatrix();
421         glTranslatef(marker_pos[0], marker_pos[1], 0);
422
423         if(!tiny) glLineWidth(3.0f);
424
425         if(sc->flag&SC_SHOW_MARKER_PATTERN) {
426                 glBegin(GL_LINE_LOOP);
427                         glVertex2f(track->pat_min[0], track->pat_min[1]);
428                         glVertex2f(track->pat_max[0], track->pat_min[1]);
429                         glVertex2f(track->pat_max[0], track->pat_max[1]);
430                         glVertex2f(track->pat_min[0], track->pat_max[1]);
431                 glEnd();
432         }
433
434         show_search= TRACK_VIEW_SELECTED(sc, track) && ((marker->flag&MARKER_DISABLED)==0 || (sc->flag&SC_SHOW_MARKER_PATTERN)==0);
435         if(sc->flag&SC_SHOW_MARKER_SEARCH && show_search) {
436                 glBegin(GL_LINE_LOOP);
437                         glVertex2f(track->search_min[0], track->search_min[1]);
438                         glVertex2f(track->search_max[0], track->search_min[1]);
439                         glVertex2f(track->search_max[0], track->search_max[1]);
440                         glVertex2f(track->search_min[0], track->search_max[1]);
441                 glEnd();
442         }
443         glPopMatrix();
444
445         if(!tiny) glLineWidth(1.0f);
446 }
447
448 static void track_colors(MovieTrackingTrack *track, int act, float col[3], float scol[3])
449 {
450         if(track->flag&TRACK_CUSTOMCOLOR) {
451                 if(act) UI_GetThemeColor3fv(TH_ACT_MARKER, scol);
452                 else copy_v3_v3(scol, track->color);
453
454                 mul_v3_v3fl(col, track->color, 0.5f);
455         } else {
456                 UI_GetThemeColor3fv(TH_MARKER, col);
457
458                 if(act) UI_GetThemeColor3fv(TH_ACT_MARKER, scol);
459                 else UI_GetThemeColor3fv(TH_SEL_MARKER, scol);
460         }
461 }
462
463 static void draw_marker_areas(SpaceClip *sc, MovieTrackingTrack *track, MovieTrackingMarker *marker, float marker_pos[2], int width, int height, int act, int sel)
464 {
465         int tiny= sc->flag&SC_SHOW_TINY_MARKER;
466         int show_search= 0;
467         float col[3], scol[3], px[2];
468
469         track_colors(track, act, col, scol);
470
471         px[0]= 1.0f/width/sc->zoom;
472         px[1]= 1.0f/height/sc->zoom;
473
474         /* marker position and offset position */
475         if((track->flag&SELECT)==sel && (marker->flag&MARKER_DISABLED)==0) {
476                 float pos[2];
477                 rctf r;
478
479                 if(track->flag&TRACK_LOCKED) {
480                         if(act) UI_ThemeColor(TH_ACT_MARKER);
481                         else if(track->flag&SELECT) UI_ThemeColorShade(TH_LOCK_MARKER, 64);
482                         else UI_ThemeColor(TH_LOCK_MARKER);
483                 } else {
484                         if(track->flag&SELECT) glColor3fv(scol);
485                         else glColor3fv(col);
486                 }
487
488                 BLI_init_rctf(&r, track->pat_min[0], track->pat_max[0], track->pat_min[1], track->pat_max[1]);
489                 add_v2_v2v2(pos, marker->pos, track->offset);
490                 ED_clip_point_undistorted_pos(sc, pos, pos);
491
492                 if(BLI_in_rctf(&r, pos[0]-marker_pos[0], pos[1]-marker_pos[1])) {
493                         if(!tiny) glPointSize(2.0f);
494                         glBegin(GL_POINTS);
495                                 glVertex2f(pos[0], pos[1]);
496                         glEnd();
497                         if(!tiny) glPointSize(1.0f);
498                 } else {
499                         glBegin(GL_LINES);
500                                 glVertex2f(pos[0] + px[0]*3, pos[1]);
501                                 glVertex2f(pos[0] + px[0]*7, pos[1]);
502
503                                 glVertex2f(pos[0] - px[0]*3, pos[1]);
504                                 glVertex2f(pos[0] - px[0]*7, pos[1]);
505
506                                 glVertex2f(pos[0], pos[1] - px[1]*3);
507                                 glVertex2f(pos[0], pos[1] - px[1]*7);
508
509                                 glVertex2f(pos[0], pos[1] + px[1]*3);
510                                 glVertex2f(pos[0], pos[1] + px[1]*7);
511                         glEnd();
512
513                         glColor3f(0.0f, 0.0f, 0.0f);
514                         glLineStipple(3, 0xaaaa);
515                         glEnable(GL_LINE_STIPPLE);
516                         glEnable(GL_COLOR_LOGIC_OP);
517                         glLogicOp(GL_NOR);
518
519                         glBegin(GL_LINES);
520                                 glVertex2fv(pos);
521                                 glVertex2fv(marker_pos);
522                         glEnd();
523
524                         glDisable(GL_COLOR_LOGIC_OP);
525                         glDisable(GL_LINE_STIPPLE);
526                 }
527         }
528
529         /* pattern */
530         glPushMatrix();
531         glTranslatef(marker_pos[0], marker_pos[1], 0);
532
533         if(tiny) {
534                 glLineStipple(3, 0xaaaa);
535                 glEnable(GL_LINE_STIPPLE);
536         }
537
538         if((track->pat_flag&SELECT)==sel && (sc->flag&SC_SHOW_MARKER_PATTERN)) {
539                 if(track->flag&TRACK_LOCKED) {
540                         if(act) UI_ThemeColor(TH_ACT_MARKER);
541                         else if(track->pat_flag&SELECT) UI_ThemeColorShade(TH_LOCK_MARKER, 64);
542                         else UI_ThemeColor(TH_LOCK_MARKER);
543                 }
544                 else if(marker->flag&MARKER_DISABLED) {
545                         if(act) UI_ThemeColor(TH_ACT_MARKER);
546                         else if(track->pat_flag&SELECT) UI_ThemeColorShade(TH_DIS_MARKER, 128);
547                         else UI_ThemeColor(TH_DIS_MARKER);
548                 } else {
549                         if(track->pat_flag&SELECT) glColor3fv(scol);
550                         else glColor3fv(col);
551                 }
552
553                 glBegin(GL_LINE_LOOP);
554                         glVertex2f(track->pat_min[0], track->pat_min[1]);
555                         glVertex2f(track->pat_max[0], track->pat_min[1]);
556                         glVertex2f(track->pat_max[0], track->pat_max[1]);
557                         glVertex2f(track->pat_min[0], track->pat_max[1]);
558                 glEnd();
559         }
560
561         /* search */
562         show_search= TRACK_VIEW_SELECTED(sc, track) && ((marker->flag&MARKER_DISABLED)==0 || (sc->flag&SC_SHOW_MARKER_PATTERN)==0);
563         if((track->search_flag&SELECT)==sel && (sc->flag&SC_SHOW_MARKER_SEARCH) && show_search) {
564                 if(track->flag&TRACK_LOCKED) {
565                         if(act) UI_ThemeColor(TH_ACT_MARKER);
566                         else if(track->search_flag&SELECT) UI_ThemeColorShade(TH_LOCK_MARKER, 64);
567                         else UI_ThemeColor(TH_LOCK_MARKER);
568                 }
569                 else if(marker->flag&MARKER_DISABLED) {
570                         if(act) UI_ThemeColor(TH_ACT_MARKER);
571                         else if(track->search_flag&SELECT) UI_ThemeColorShade(TH_DIS_MARKER, 128);
572                         else UI_ThemeColor(TH_DIS_MARKER);
573                 } else {
574                         if(track->search_flag&SELECT) glColor3fv(scol);
575                         else glColor3fv(col);
576                 }
577
578                 glBegin(GL_LINE_LOOP);
579                         glVertex2f(track->search_min[0], track->search_min[1]);
580                         glVertex2f(track->search_max[0], track->search_min[1]);
581                         glVertex2f(track->search_max[0], track->search_max[1]);
582                         glVertex2f(track->search_min[0], track->search_max[1]);
583                 glEnd();
584         }
585
586         /* pyramid */
587         if(sel && TRACK_SELECTED(track) && (sc->flag&SC_SHOW_PYRAMID_LEVELS) && (track->tracker==TRACKER_KLT) && (marker->flag&MARKER_DISABLED)==0) {
588                 if(track->flag&TRACK_LOCKED) {
589                         if(act) UI_ThemeColor(TH_ACT_MARKER);
590                         else if(track->pat_flag&SELECT) UI_ThemeColorShade(TH_LOCK_MARKER, 64);
591                         else UI_ThemeColor(TH_LOCK_MARKER);
592                 }
593                 else if(marker->flag&MARKER_DISABLED) {
594                         if(act) UI_ThemeColor(TH_ACT_MARKER);
595                         else if(track->pat_flag&SELECT) UI_ThemeColorShade(TH_DIS_MARKER, 128);
596                         else UI_ThemeColor(TH_DIS_MARKER);
597                 } else {
598                         if(track->pat_flag&SELECT) glColor3fv(scol);
599                         else glColor3fv(col);
600                 }
601
602                 {
603                         int i = 0;
604                         glPushMatrix();
605                         glEnable(GL_LINE_STIPPLE);
606                         for (i = 1; i < track->pyramid_levels; ++i) {
607                                 glScalef(2.0f, 2.0f, 1.0);
608                         }
609                         /* only draw a pattern for the coarsest level */
610                         glBegin(GL_LINE_LOOP);
611                                 glVertex2f(track->pat_min[0], track->pat_min[1]);
612                                 glVertex2f(track->pat_max[0], track->pat_min[1]);
613                                 glVertex2f(track->pat_max[0], track->pat_max[1]);
614                                 glVertex2f(track->pat_min[0], track->pat_max[1]);
615                         glEnd();
616                         glDisable(GL_LINE_STIPPLE);
617                         glPopMatrix();
618                 }
619         }
620
621         if(tiny)
622                 glDisable(GL_LINE_STIPPLE);
623
624         glPopMatrix();
625 }
626
627 static void draw_marker_slide_zones(SpaceClip *sc, MovieTrackingTrack *track, MovieTrackingMarker *marker,
628                         float marker_pos[2], int outline, int sel, int act, int width, int height)
629 {
630         float x, y, dx, dy, patdx, patdy, searchdx, searchdy, tdx, tdy;
631         int tiny= sc->flag&SC_SHOW_TINY_MARKER;
632         float col[3], scol[3], px[2];
633
634         if((tiny && outline) || (marker->flag&MARKER_DISABLED))
635                 return;
636
637         if(!TRACK_VIEW_SELECTED(sc, track) || track->flag&TRACK_LOCKED)
638                 return;
639
640         track_colors(track, act, col, scol);
641
642         if(outline) {
643                 glLineWidth(3.0f);
644                 UI_ThemeColor(TH_MARKER_OUTLINE);
645         }
646
647         glPushMatrix();
648         glTranslatef(marker_pos[0], marker_pos[1], 0);
649
650         dx= 6.0f/width/sc->zoom;
651         dy= 6.0f/height/sc->zoom;
652
653         patdx= MIN2(dx*2.0f/3.0f, (track->pat_max[0]-track->pat_min[0])/6.0f);
654         patdy= MIN2(dy*2.0f/3.0f, (track->pat_max[1]-track->pat_min[1])/6.0f);
655
656         searchdx= MIN2(dx, (track->search_max[0]-track->search_min[0])/6.0f);
657         searchdy= MIN2(dy, (track->search_max[1]-track->search_min[1])/6.0f);
658
659         px[0]= 1.0f/sc->zoom/width/sc->scale;
660         px[1]= 1.0f/sc->zoom/height/sc->scale;
661
662         if((sc->flag&SC_SHOW_MARKER_SEARCH) && ((track->search_flag&SELECT)==sel || outline)) {
663                 if(!outline) {
664                         if(track->search_flag&SELECT) glColor3fv(scol);
665                         else glColor3fv(col);
666                 }
667
668                 /* search offset square */
669                 x= track->search_min[0];
670                 y= track->search_max[1];
671
672                 tdx= searchdx;
673                 tdy= searchdy;
674
675                 if(outline) {
676                         tdx+= px[0];
677                         tdy+= px[1];
678                 }
679
680                 glBegin(GL_QUADS);
681                         glVertex3f(x-tdx, y+tdy, 0);
682                         glVertex3f(x+tdx, y+tdy, 0);
683                         glVertex3f(x+tdx, y-tdy, 0);
684                         glVertex3f(x-tdx, y-tdy, 0);
685                 glEnd();
686
687                 /* search resizing triangle */
688                 x= track->search_max[0];
689                 y= track->search_min[1];
690
691                 tdx= searchdx*2.0f;
692                 tdy= searchdy*2.0f;
693
694                 if(outline) {
695                         tdx+= px[0];
696                         tdy+= px[1];
697                 }
698
699                 glBegin(GL_TRIANGLES);
700                         glVertex3f(x, y, 0);
701                         glVertex3f(x-tdx, y, 0);
702                         glVertex3f(x, y+tdy, 0);
703                 glEnd();
704         }
705
706         if((sc->flag&SC_SHOW_MARKER_PATTERN) && ((track->pat_flag&SELECT)==sel || outline)) {
707                 if(!outline) {
708                         if(track->pat_flag&SELECT) glColor3fv(scol);
709                         else glColor3fv(col);
710                 }
711
712                 /* pattern offset square */
713                 x= track->pat_min[0];
714                 y= track->pat_max[1];
715
716                 tdx= patdx;
717                 tdy= patdy;
718
719                 if(outline) {
720                         tdx+= px[0];
721                         tdy+= px[1];
722                 }
723
724                 glBegin(GL_QUADS);
725                         glVertex3f(x-tdx, y+tdy, 0);
726                         glVertex3f(x+tdx, y+tdy, 0);
727                         glVertex3f(x+tdx, y-tdy, 0);
728                         glVertex3f(x-tdx, y-tdy, 0);
729                 glEnd();
730
731                 /* pattern resizing triangle */
732                 x= track->pat_max[0];
733                 y= track->pat_min[1];
734
735                 tdx= patdx*2.0f;
736                 tdy= patdy*2.0f;
737
738                 if(outline) {
739                         tdx+= px[0];
740                         tdy+= px[1];
741                 }
742
743                 glBegin(GL_TRIANGLES);
744                         glVertex3f(x, y, 0);
745                         glVertex3f(x-tdx, y, 0);
746                         glVertex3f(x, y+tdy, 0);
747                 glEnd();
748         }
749
750         glPopMatrix();
751
752         if(outline)
753                 glLineWidth(1.0f);
754 }
755
756 static void draw_marker_texts(SpaceClip *sc, MovieTrackingTrack *track, MovieTrackingMarker *marker, float marker_pos[2], int act,
757                         int width, int height, float zoomx, float zoomy)
758 {
759         char str[128]= {0}, state[64]= {0};
760         float dx= 0.0f, dy= 0.0f, fontsize, pos[3];
761         uiStyle *style= U.uistyles.first;
762         int fontid= style->widget.uifont_id;
763
764         if(!TRACK_VIEW_SELECTED(sc, track))
765                 return;
766
767         BLF_size(fontid, 11.0f, U.dpi);
768         fontsize= BLF_height_max(fontid);
769
770         if(marker->flag&MARKER_DISABLED) {
771                 if(act) UI_ThemeColor(TH_ACT_MARKER);
772                 else UI_ThemeColorShade(TH_DIS_MARKER, 128);
773         } else {
774                 if(act) UI_ThemeColor(TH_ACT_MARKER);
775                 else UI_ThemeColor(TH_SEL_MARKER);
776         }
777
778         if(sc->flag&SC_SHOW_MARKER_SEARCH) {
779                 dx= track->search_min[0];
780                 dy= track->search_min[1];
781         } else if(sc->flag&SC_SHOW_MARKER_PATTERN) {
782                 dx= track->pat_min[0];
783                 dy= track->pat_min[1];
784         }
785
786         pos[0]= (marker_pos[0]+dx)*width;
787         pos[1]= (marker_pos[1]+dy)*height;
788         pos[2]= 0.0f;
789
790         mul_m4_v3(sc->stabmat, pos);
791
792         pos[0]= pos[0]*zoomx;
793         pos[1]= pos[1]*zoomy - fontsize;
794
795         if(marker->flag&MARKER_DISABLED) strcpy(state, "disabled");
796         else if(marker->framenr!=sc->user.framenr) strcpy(state, "estimated");
797         else if(marker->flag&MARKER_TRACKED) strcpy(state, "tracked");
798         else strcpy(state, "keyframed");
799
800         if(state[0])
801                 BLI_snprintf(str, sizeof(str), "%s: %s", track->name, state);
802         else
803                 BLI_snprintf(str, sizeof(str), "%s", track->name);
804
805         BLF_position(fontid, pos[0], pos[1], 0.0f);
806         BLF_draw(fontid, str, strlen(str));
807         pos[1]-= fontsize;
808
809         if(track->flag&TRACK_HAS_BUNDLE) {
810                 BLI_snprintf(str, sizeof(str), "Average error: %.3f", track->error);
811                 BLF_position(fontid, pos[0], pos[1], 0.0f);
812                 BLF_draw(fontid, str, strlen(str));
813                 pos[1]-= fontsize;
814         }
815
816         if(track->flag&TRACK_LOCKED) {
817                 BLF_position(fontid, pos[0], pos[1], 0.0f);
818                 BLF_draw(fontid, "locked", 6);
819         }
820 }
821
822 static void view2d_to_region_float(View2D *v2d, float x, float y, float *regionx, float *regiony)
823 {
824         /* express given coordinates as proportional values */
825         x= -v2d->cur.xmin / (v2d->cur.xmax-v2d->cur.xmin);
826         y= -v2d->cur.ymin / (v2d->cur.ymax-v2d->cur.ymin);
827
828         /* convert proportional distances to screen coordinates */
829         *regionx= v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin);
830         *regiony= v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin);
831 }
832
833 static void draw_tracking_tracks(SpaceClip *sc, ARegion *ar, MovieClip *clip,
834                         int width, int height, float zoomx, float zoomy)
835 {
836         float x, y;
837         MovieTracking* tracking= &clip->tracking;
838         MovieTrackingMarker *marker;
839         MovieTrackingTrack *track, *act_track;
840         int framenr= sc->user.framenr;
841         int undistort= sc->user.render_flag&MCLIP_PROXY_RENDER_UNDISTORT;
842         float *marker_pos= NULL, *fp, *active_pos= NULL, cur_pos[2];
843
844         /* ** find window pixel coordinates of origin ** */
845
846         /* UI_view2d_to_region_no_clip return integer values, this could
847            lead to 1px flickering when view is locked to selection during playbeck.
848            to avoid this flickering, calclate base point in the same way as it happens
849            in UI_view2d_to_region_no_clip, but do it in floats here */
850
851         view2d_to_region_float(&ar->v2d, 0.0f, 0.0f, &x, &y);
852
853         glPushMatrix();
854         glTranslatef(x, y, 0);
855
856         glPushMatrix();
857         glScalef(zoomx, zoomy, 0);
858         glMultMatrixf(sc->stabmat);
859         glScalef(width, height, 0);
860
861         act_track= clip->tracking.act_track;
862
863         if(sc->user.render_flag&MCLIP_PROXY_RENDER_UNDISTORT) {
864                 int count= 0;
865
866                 /* count */
867                 track= tracking->tracks.first;
868                 while(track) {
869                         if((track->flag&TRACK_HIDDEN)==0) {
870                                 marker= BKE_tracking_get_marker(track, framenr);
871
872                                 if(MARKER_VISIBLE(sc, marker))
873                                         count++;
874                         }
875
876                         track= track->next;
877                 }
878
879                 /* undistort */
880                 if(count) {
881                         marker_pos= MEM_callocN(2*sizeof(float)*count, "draw_tracking_tracks marker_pos");
882
883                         track= tracking->tracks.first;
884                         fp= marker_pos;
885                         while(track) {
886                                 if((track->flag&TRACK_HIDDEN)==0) {
887                                         marker= BKE_tracking_get_marker(track, framenr);
888
889                                         if(MARKER_VISIBLE(sc, marker)) {
890                                                 ED_clip_point_undistorted_pos(sc, marker->pos, fp);
891
892                                                 if(track==act_track)
893                                                         active_pos= fp;
894
895                                                 fp+= 2;
896                                         }
897                                 }
898
899                                 track= track->next;
900                         }
901                 }
902         }
903
904         if(sc->flag&SC_SHOW_TRACK_PATH) {
905                 track= tracking->tracks.first;
906                 while(track) {
907                         if((track->flag&TRACK_HIDDEN)==0)
908                                 draw_track_path(sc, clip, track);
909
910                         track= track->next;
911                 }
912         }
913
914         /* markers outline and non-selected areas */
915         track= tracking->tracks.first;
916         fp= marker_pos;
917         while(track) {
918                 if((track->flag&TRACK_HIDDEN)==0) {
919                         marker= BKE_tracking_get_marker(track, framenr);
920
921                         if(MARKER_VISIBLE(sc, marker)) {
922                                 copy_v2_v2(cur_pos, fp ? fp : marker->pos);
923
924                                 draw_marker_outline(sc, track, marker, cur_pos, width, height);
925                                 draw_marker_areas(sc, track, marker, cur_pos, width, height, 0, 0);
926                                 draw_marker_slide_zones(sc, track, marker, cur_pos, 1, 0, 0, width, height);
927                                 draw_marker_slide_zones(sc, track, marker, cur_pos, 0, 0, 0, width, height);
928
929                                 if(fp)
930                                         fp+= 2;
931                         }
932                 }
933
934                 track= track->next;
935         }
936
937         /* selected areas only, so selection wouldn't be overlapped by
938            non-selected areas */
939         track= tracking->tracks.first;
940         fp= marker_pos;
941         while(track) {
942                 if((track->flag&TRACK_HIDDEN)==0) {
943                         int act= track==act_track;
944                         marker= BKE_tracking_get_marker(track, framenr);
945
946                         if(MARKER_VISIBLE(sc, marker)) {
947                                 if(!act) {
948                                         copy_v2_v2(cur_pos, fp ? fp : marker->pos);
949
950                                         draw_marker_areas(sc, track, marker, cur_pos, width, height, 0, 1);
951                                         draw_marker_slide_zones(sc, track, marker, cur_pos, 0, 1, 0, width, height);
952                                 }
953
954                                 if(fp)
955                                         fp+= 2;
956                         }
957                 }
958
959                 track= track->next;
960         }
961
962         /* active marker would be displayed on top of everything else */
963         if(act_track) {
964                 if((act_track->flag&TRACK_HIDDEN)==0) {
965                         marker= BKE_tracking_get_marker(act_track, framenr);
966
967                         if(MARKER_VISIBLE(sc, marker)) {
968                                 copy_v2_v2(cur_pos, active_pos ? active_pos : marker->pos);
969
970                                 draw_marker_areas(sc, act_track, marker, cur_pos, width, height, 1, 1);
971                                 draw_marker_slide_zones(sc, act_track, marker, cur_pos, 0, 1, 1, width, height);
972                         }
973                 }
974         }
975
976         if(sc->flag&SC_SHOW_BUNDLES) {
977                 float pos[4], vec[4], mat[4][4], aspy;
978
979                 glEnable(GL_POINT_SMOOTH);
980                 glPointSize(3.0f);
981
982                 aspy= 1.0f/clip->tracking.camera.pixel_aspect;
983                 BKE_tracking_projection_matrix(tracking, framenr, width, height, mat);
984
985                 track= tracking->tracks.first;
986                 while(track) {
987                         if((track->flag&TRACK_HIDDEN)==0 && track->flag&TRACK_HAS_BUNDLE) {
988                                 marker= BKE_tracking_get_marker(track, framenr);
989
990                                 if(MARKER_VISIBLE(sc, marker)) {
991                                         float npos[2];
992                                         copy_v4_v4(vec, track->bundle_pos);
993                                         vec[3]=1;
994
995                                         mul_v4_m4v4(pos, mat, vec);
996
997                                         pos[0]= (pos[0]/(pos[3]*2.0f)+0.5f)*width;
998                                         pos[1]= (pos[1]/(pos[3]*2.0f)+0.5f)*height*aspy;
999
1000                                         BKE_tracking_apply_intrinsics(tracking, pos, npos);
1001
1002                                         if(npos[0]>=0.0f && npos[1]>=0.0f && npos[0]<=width && npos[1]<=height*aspy) {
1003                                                 vec[0]= (marker->pos[0]+track->offset[0])*width;
1004                                                 vec[1]= (marker->pos[1]+track->offset[1])*height*aspy;
1005
1006                                                 sub_v2_v2(vec, npos);
1007
1008                                                 if(len_v2(vec)<3) glColor3f(0.0f, 1.0f, 0.0f);
1009                                                 else glColor3f(1.0f, 0.0f, 0.0f);
1010
1011                                                 glBegin(GL_POINTS);
1012                                                         if(undistort) glVertex3f(pos[0]/width, pos[1]/(height*aspy), 0);
1013                                                         else glVertex3f(npos[0]/width, npos[1]/(height*aspy), 0);
1014                                                 glEnd();
1015                                         }
1016                                 }
1017                         }
1018
1019                         track= track->next;
1020                 }
1021
1022                 glPointSize(1.0f);
1023                 glDisable(GL_POINT_SMOOTH);
1024         }
1025
1026         glPopMatrix();
1027
1028         if(sc->flag&SC_SHOW_NAMES) {
1029                 /* scaling should be cleared before drawing texts, otherwise font would also be scaled */
1030                 track= tracking->tracks.first;
1031                 fp= marker_pos;
1032                 while(track) {
1033                         if((track->flag&TRACK_HIDDEN)==0) {
1034                                 marker= BKE_tracking_get_marker(track, framenr);
1035
1036                                 if(MARKER_VISIBLE(sc, marker)) {
1037                                         int act= track==act_track;
1038
1039                                         copy_v2_v2(cur_pos, fp ? fp : marker->pos);
1040
1041                                         draw_marker_texts(sc, track, marker, cur_pos, act, width, height, zoomx, zoomy);
1042
1043                                         if(fp) fp+= 2;
1044                                 }
1045                         }
1046
1047                         track= track->next;
1048                 }
1049         }
1050
1051         glPopMatrix();
1052
1053         if(marker_pos)
1054                 MEM_freeN(marker_pos);
1055 }
1056
1057 static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip, int width, int height, float zoomx, float zoomy)
1058 {
1059         float x, y;
1060         const int n= 10;
1061         int i, j, a;
1062         float pos[2], tpos[2], grid[11][11][2];
1063         MovieTracking *tracking= &clip->tracking;
1064         float aspy= 1.0f/tracking->camera.pixel_aspect;
1065         float dx= (float)width/n, dy= (float)height/n*aspy;
1066
1067         if(sc->mode!=SC_MODE_DISTORTION)
1068                 return;
1069
1070         if(!tracking->camera.focal)
1071                 return;
1072
1073         if((sc->flag&SC_SHOW_GRID)==0 && (sc->flag&SC_MANUAL_CALIBRATION)==0)
1074                 return;
1075
1076         view2d_to_region_float(&ar->v2d, 0.0f, 0.0f, &x, &y);
1077
1078         glPushMatrix();
1079         glTranslatef(x, y, 0);
1080         glScalef(zoomx, zoomy, 0);
1081         glMultMatrixf(sc->stabmat);
1082         glScalef(width, height, 0);
1083
1084         /* grid */
1085         if(sc->flag&SC_SHOW_GRID) {
1086                 float val[4][2], idx[4][2];
1087                 float min[2], max[2];
1088
1089                 for(a=0; a<4; a++) {
1090                         if(a<2) val[a][a%2]= FLT_MAX;
1091                         else val[a][a%2]= -FLT_MAX;
1092                 }
1093
1094                 zero_v2(pos);
1095                 for(i= 0; i<=n; i++) {
1096                         for(j= 0; j<=n; j++) {
1097                                 if(i==0 || j==0 || i==n || j==n) {
1098                                         BKE_tracking_apply_intrinsics(tracking, pos, tpos);
1099
1100                                         for(a=0; a<4; a++) {
1101                                                 int ok;
1102
1103                                                 if(a<2) ok= tpos[a%2] < val[a][a%2];
1104                                                 else ok= tpos[a%2] > val[a][a%2];
1105
1106                                                 if(ok) {
1107                                                         copy_v2_v2(val[a], tpos);
1108                                                         idx[a][0]= j;
1109                                                         idx[a][1]= i;
1110                                                 }
1111                                         }
1112                                 }
1113
1114                                 pos[0]+= dx;
1115                         }
1116
1117                         pos[0]= 0.0f;
1118                         pos[1]+= dy;
1119                 }
1120
1121                 INIT_MINMAX2(min, max);
1122
1123                 for(a= 0; a<4; a++) {
1124                         pos[0]= idx[a][0]*dx;
1125                         pos[1]= idx[a][1]*dy;
1126
1127                         BKE_tracking_invert_intrinsics(tracking, pos, tpos);
1128
1129                         DO_MINMAX2(tpos, min, max);
1130                 }
1131
1132                 copy_v2_v2(pos, min);
1133                 dx= (max[0]-min[0])/n;
1134                 dy= (max[1]-min[1])/n;
1135
1136                 for(i= 0; i<=n; i++) {
1137                         for(j= 0; j<=n; j++) {
1138                                 BKE_tracking_apply_intrinsics(tracking, pos, grid[i][j]);
1139
1140                                 grid[i][j][0]/= width;
1141                                 grid[i][j][1]/= height*aspy;
1142
1143                                 pos[0]+= dx;
1144                         }
1145
1146                         pos[0]= min[0];
1147                         pos[1]+= dy;
1148                 }
1149
1150                 glColor3f(1.0f, 0.0f, 0.0f);
1151
1152                 for(i= 0; i<=n; i++) {
1153                         glBegin(GL_LINE_STRIP);
1154                                 for(j= 0; j<=n; j++) {
1155                                         glVertex2fv(grid[i][j]);
1156                                 }
1157                         glEnd();
1158                 }
1159
1160                 for(j= 0; j<=n; j++) {
1161                         glBegin(GL_LINE_STRIP);
1162                                 for(i= 0; i<=n; i++) {
1163                                         glVertex2fv(grid[i][j]);
1164                                 }
1165                         glEnd();
1166                 }
1167         }
1168
1169         if(sc->flag&SC_MANUAL_CALIBRATION && clip->gpd) {
1170                 bGPDlayer *layer= clip->gpd->layers.first;
1171
1172                 while(layer) {
1173                         bGPDframe *frame= layer->frames.first;
1174
1175                         glColor4fv(layer->color);
1176                         glLineWidth(layer->thickness);
1177                         glPointSize((float)(layer->thickness + 2));
1178
1179                         while(frame) {
1180                                 bGPDstroke *stroke= frame->strokes.first;
1181
1182                                 while(stroke) {
1183                                         if(stroke->flag&GP_STROKE_2DSPACE) {
1184                                                 if(stroke->totpoints>1) {
1185                                                         glBegin(GL_LINE_STRIP);
1186                                                                 for(i= 0; i<stroke->totpoints-1; i++) {
1187                                                                         float npos[2], dpos[2], len;
1188                                                                         int steps;
1189
1190                                                                         pos[0]= stroke->points[i].x*width;
1191                                                                         pos[1]= stroke->points[i].y*height*aspy;
1192
1193                                                                         npos[0]= stroke->points[i+1].x*width;
1194                                                                         npos[1]= stroke->points[i+1].y*height*aspy;
1195
1196                                                                         len= len_v2v2(pos, npos);
1197                                                                         steps= ceil(len/5.0f);
1198
1199                                                                         /* we want to distort only long straight lines */
1200                                                                         if(stroke->totpoints==2) {
1201                                                                                 BKE_tracking_invert_intrinsics(tracking, pos, pos);
1202                                                                                 BKE_tracking_invert_intrinsics(tracking, npos, npos);
1203                                                                         }
1204
1205                                                                         sub_v2_v2v2(dpos, npos, pos);
1206                                                                         mul_v2_fl(dpos, 1.0f/steps);
1207
1208                                                                         for(j= 0; j<=steps; j++) {
1209                                                                                 BKE_tracking_apply_intrinsics(tracking, pos, tpos);
1210                                                                                 glVertex2f(tpos[0]/width, tpos[1]/(height*aspy));
1211
1212                                                                                 add_v2_v2(pos, dpos);
1213                                                                         }
1214                                                                 }
1215                                                         glEnd();
1216                                                 }
1217                                                 else if(stroke->totpoints==1) {
1218                                                         glBegin(GL_POINTS);
1219                                                                 glVertex2f(stroke->points[0].x, stroke->points[0].y);
1220                                                         glEnd();
1221                                                 }
1222                                         }
1223
1224                                         stroke= stroke->next;
1225                                 }
1226
1227                                 frame= frame->next;
1228                         }
1229
1230                         layer= layer->next;
1231                 }
1232
1233                 glLineWidth(1.0f);
1234                 glPointSize(1.0f);
1235         }
1236
1237         glPopMatrix();
1238 }
1239
1240 void clip_draw_main(SpaceClip *sc, ARegion *ar, Scene *scene)
1241 {
1242         MovieClip *clip= ED_space_clip(sc);
1243         ImBuf *ibuf;
1244         int width, height;
1245         float zoomx, zoomy;
1246
1247         /* if no clip, nothing to do */
1248         if(!clip)
1249                 return;
1250
1251         ED_space_clip_size(sc, &width, &height);
1252         ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
1253
1254         if(sc->flag&SC_SHOW_STABLE) {
1255                 float smat[4][4], ismat[4][4];
1256
1257                 ibuf= ED_space_clip_get_stable_buffer(sc, sc->loc, &sc->scale, &sc->angle);
1258                 BKE_tracking_stabdata_to_mat4(width, height, sc->loc, sc->scale, sc->angle, sc->stabmat);
1259
1260                 unit_m4(smat);
1261                 smat[0][0]= 1.0f/width;
1262                 smat[1][1]= 1.0f/height;
1263                 invert_m4_m4(ismat, smat);
1264
1265                 mul_serie_m4(sc->unistabmat, smat, sc->stabmat, ismat, NULL, NULL, NULL, NULL, NULL);
1266         } else {
1267                 ibuf= ED_space_clip_get_buffer(sc);
1268
1269                 zero_v2(sc->loc);
1270                 sc->scale= 1.0f;
1271                 unit_m4(sc->stabmat);
1272                 unit_m4(sc->unistabmat);
1273         }
1274
1275         if(ibuf) {
1276                 draw_movieclip_buffer(sc, ar, ibuf, width, height, zoomx, zoomy);
1277                 IMB_freeImBuf(ibuf);
1278
1279                 draw_tracking_tracks(sc, ar, clip, width, height, zoomx, zoomy);
1280                 draw_distortion(sc, ar, clip, width, height, zoomx, zoomy);
1281         }
1282
1283         draw_movieclip_cache(sc, ar, clip, scene);
1284         draw_movieclip_notes(sc, ar);
1285 }
1286
1287 /* draw grease pencil */
1288 void clip_draw_grease_pencil(bContext *C, int onlyv2d)
1289 {
1290         SpaceClip *sc= CTX_wm_space_clip(C);
1291         MovieClip *clip= ED_space_clip(sc);
1292         ImBuf *ibuf;
1293
1294         if((sc->flag&SC_SHOW_GPENCIL)==0 || !clip)
1295                 return;
1296
1297         if(onlyv2d) {
1298                 /* if manual calibration is used then grase pencil data is already
1299                     drawed in draw_distortion */
1300                 if((sc->flag&SC_MANUAL_CALIBRATION)==0 || sc->mode!=SC_MODE_DISTORTION) {
1301                         ibuf= ED_space_clip_get_buffer(sc);
1302
1303                         if(ibuf) {
1304                                 glPushMatrix();
1305                                 glMultMatrixf(sc->unistabmat);
1306                                 draw_gpencil_2dimage(C, ibuf);
1307
1308                                 IMB_freeImBuf(ibuf);
1309                                 glPopMatrix();
1310                         }
1311                 }
1312         } else {
1313                 draw_gpencil_view2d(C, 0);
1314         }
1315 }