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