svn merge ^/trunk/blender -r42991:43009
[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         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(ibuf->x, 0.0f);
254                         glVertex2f(ibuf->x, ibuf->y);
255                         glVertex2f(0.0f, ibuf->y);
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) && (sc->flag&SC_SHOW_PYRAMID_LEVELS) && (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                 dx= track->search_min[0];
782                 dy= track->search_min[1];
783         } else if(sc->flag&SC_SHOW_MARKER_PATTERN) {
784                 dx= track->pat_min[0];
785                 dy= track->pat_min[1];
786         }
787
788         pos[0]= (marker_pos[0]+dx)*width;
789         pos[1]= (marker_pos[1]+dy)*height;
790         pos[2]= 0.0f;
791
792         mul_m4_v3(sc->stabmat, pos);
793
794         pos[0]= pos[0]*zoomx;
795         pos[1]= pos[1]*zoomy - fontsize;
796
797         if(marker->flag&MARKER_DISABLED) strcpy(state, "disabled");
798         else if(marker->framenr!=sc->user.framenr) strcpy(state, "estimated");
799         else if(marker->flag&MARKER_TRACKED) strcpy(state, "tracked");
800         else strcpy(state, "keyframed");
801
802         if(state[0])
803                 BLI_snprintf(str, sizeof(str), "%s: %s", track->name, state);
804         else
805                 BLI_snprintf(str, sizeof(str), "%s", track->name);
806
807         BLF_position(fontid, pos[0], pos[1], 0.0f);
808         BLF_draw(fontid, str, strlen(str));
809         pos[1]-= fontsize;
810
811         if(track->flag&TRACK_HAS_BUNDLE) {
812                 BLI_snprintf(str, sizeof(str), "Average error: %.3f", track->error);
813                 BLF_position(fontid, pos[0], pos[1], 0.0f);
814                 BLF_draw(fontid, str, strlen(str));
815                 pos[1]-= fontsize;
816         }
817
818         if(track->flag&TRACK_LOCKED) {
819                 BLF_position(fontid, pos[0], pos[1], 0.0f);
820                 BLF_draw(fontid, "locked", 6);
821         }
822 }
823
824 static void view2d_to_region_float(View2D *v2d, float x, float y, float *regionx, float *regiony)
825 {
826         /* express given coordinates as proportional values */
827         x= -v2d->cur.xmin / (v2d->cur.xmax-v2d->cur.xmin);
828         y= -v2d->cur.ymin / (v2d->cur.ymax-v2d->cur.ymin);
829
830         /* convert proportional distances to screen coordinates */
831         *regionx= v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin);
832         *regiony= v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin);
833 }
834
835 static void draw_tracking_tracks(SpaceClip *sc, ARegion *ar, MovieClip *clip,
836                         int width, int height, float zoomx, float zoomy)
837 {
838         float x, y;
839         MovieTracking* tracking= &clip->tracking;
840         ListBase *tracksbase= BKE_tracking_get_tracks(tracking);
841         MovieTrackingTrack *track, *act_track;
842         MovieTrackingMarker *marker;
843         int framenr= sc->user.framenr;
844         int undistort= sc->user.render_flag&MCLIP_PROXY_RENDER_UNDISTORT;
845         float *marker_pos= NULL, *fp, *active_pos= NULL, cur_pos[2];
846
847         /* ** find window pixel coordinates of origin ** */
848
849         /* UI_view2d_to_region_no_clip return integer values, this could
850            lead to 1px flickering when view is locked to selection during playbeck.
851            to avoid this flickering, calclate base point in the same way as it happens
852            in UI_view2d_to_region_no_clip, but do it in floats here */
853
854         view2d_to_region_float(&ar->v2d, 0.0f, 0.0f, &x, &y);
855
856         glPushMatrix();
857         glTranslatef(x, y, 0);
858
859         glPushMatrix();
860         glScalef(zoomx, zoomy, 0);
861         glMultMatrixf(sc->stabmat);
862         glScalef(width, height, 0);
863
864         act_track= BKE_tracking_active_track(tracking);
865
866         if(sc->user.render_flag&MCLIP_PROXY_RENDER_UNDISTORT) {
867                 int count= 0;
868
869                 /* count */
870                 track= tracksbase->first;
871                 while(track) {
872                         if((track->flag&TRACK_HIDDEN)==0) {
873                                 marker= BKE_tracking_get_marker(track, framenr);
874
875                                 if(MARKER_VISIBLE(sc, marker))
876                                         count++;
877                         }
878
879                         track= track->next;
880                 }
881
882                 /* undistort */
883                 if(count) {
884                         marker_pos= MEM_callocN(2*sizeof(float)*count, "draw_tracking_tracks marker_pos");
885
886                         track= tracksbase->first;
887                         fp= marker_pos;
888                         while(track) {
889                                 if((track->flag&TRACK_HIDDEN)==0) {
890                                         marker= BKE_tracking_get_marker(track, framenr);
891
892                                         if(MARKER_VISIBLE(sc, marker)) {
893                                                 ED_clip_point_undistorted_pos(sc, marker->pos, fp);
894
895                                                 if(track==act_track)
896                                                         active_pos= fp;
897
898                                                 fp+= 2;
899                                         }
900                                 }
901
902                                 track= track->next;
903                         }
904                 }
905         }
906
907         if(sc->flag&SC_SHOW_TRACK_PATH) {
908                 track= tracksbase->first;
909                 while(track) {
910                         if((track->flag&TRACK_HIDDEN)==0)
911                                 draw_track_path(sc, clip, track);
912
913                         track= track->next;
914                 }
915         }
916
917         /* markers outline and non-selected areas */
918         track= tracksbase->first;
919         fp= marker_pos;
920         while(track) {
921                 if((track->flag&TRACK_HIDDEN)==0) {
922                         marker= BKE_tracking_get_marker(track, framenr);
923
924                         if(MARKER_VISIBLE(sc, marker)) {
925                                 copy_v2_v2(cur_pos, fp ? fp : marker->pos);
926
927                                 draw_marker_outline(sc, track, marker, cur_pos, width, height);
928                                 draw_marker_areas(sc, track, marker, cur_pos, width, height, 0, 0);
929                                 draw_marker_slide_zones(sc, track, marker, cur_pos, 1, 0, 0, width, height);
930                                 draw_marker_slide_zones(sc, track, marker, cur_pos, 0, 0, 0, width, height);
931
932                                 if(fp)
933                                         fp+= 2;
934                         }
935                 }
936
937                 track= track->next;
938         }
939
940         /* selected areas only, so selection wouldn't be overlapped by
941            non-selected areas */
942         track= tracksbase->first;
943         fp= marker_pos;
944         while(track) {
945                 if((track->flag&TRACK_HIDDEN)==0) {
946                         int act= track==act_track;
947                         marker= BKE_tracking_get_marker(track, framenr);
948
949                         if(MARKER_VISIBLE(sc, marker)) {
950                                 if(!act) {
951                                         copy_v2_v2(cur_pos, fp ? fp : marker->pos);
952
953                                         draw_marker_areas(sc, track, marker, cur_pos, width, height, 0, 1);
954                                         draw_marker_slide_zones(sc, track, marker, cur_pos, 0, 1, 0, width, height);
955                                 }
956
957                                 if(fp)
958                                         fp+= 2;
959                         }
960                 }
961
962                 track= track->next;
963         }
964
965         /* active marker would be displayed on top of everything else */
966         if(act_track) {
967                 if((act_track->flag&TRACK_HIDDEN)==0) {
968                         marker= BKE_tracking_get_marker(act_track, framenr);
969
970                         if(MARKER_VISIBLE(sc, marker)) {
971                                 copy_v2_v2(cur_pos, active_pos ? active_pos : marker->pos);
972
973                                 draw_marker_areas(sc, act_track, marker, cur_pos, width, height, 1, 1);
974                                 draw_marker_slide_zones(sc, act_track, marker, cur_pos, 0, 1, 1, width, height);
975                         }
976                 }
977         }
978
979         if(sc->flag&SC_SHOW_BUNDLES) {
980                 MovieTrackingObject *object= BKE_tracking_active_object(tracking);
981                 float pos[4], vec[4], mat[4][4], aspy;
982
983                 glEnable(GL_POINT_SMOOTH);
984                 glPointSize(3.0f);
985
986                 aspy= 1.0f/clip->tracking.camera.pixel_aspect;
987                 BKE_tracking_projection_matrix(tracking, object, framenr, width, height, mat);
988
989                 track= tracksbase->first;
990                 while(track) {
991                         if((track->flag&TRACK_HIDDEN)==0 && track->flag&TRACK_HAS_BUNDLE) {
992                                 marker= BKE_tracking_get_marker(track, framenr);
993
994                                 if(MARKER_VISIBLE(sc, marker)) {
995                                         float npos[2];
996                                         copy_v4_v4(vec, track->bundle_pos);
997                                         vec[3]=1;
998
999                                         mul_v4_m4v4(pos, mat, vec);
1000
1001                                         pos[0]= (pos[0]/(pos[3]*2.0f)+0.5f)*width;
1002                                         pos[1]= (pos[1]/(pos[3]*2.0f)+0.5f)*height*aspy;
1003
1004                                         BKE_tracking_apply_intrinsics(tracking, pos, npos);
1005
1006                                         if(npos[0]>=0.0f && npos[1]>=0.0f && npos[0]<=width && npos[1]<=height*aspy) {
1007                                                 vec[0]= (marker->pos[0]+track->offset[0])*width;
1008                                                 vec[1]= (marker->pos[1]+track->offset[1])*height*aspy;
1009
1010                                                 sub_v2_v2(vec, npos);
1011
1012                                                 if(len_v2(vec)<3) glColor3f(0.0f, 1.0f, 0.0f);
1013                                                 else glColor3f(1.0f, 0.0f, 0.0f);
1014
1015                                                 glBegin(GL_POINTS);
1016                                                         if(undistort) glVertex3f(pos[0]/width, pos[1]/(height*aspy), 0);
1017                                                         else glVertex3f(npos[0]/width, npos[1]/(height*aspy), 0);
1018                                                 glEnd();
1019                                         }
1020                                 }
1021                         }
1022
1023                         track= track->next;
1024                 }
1025
1026                 glPointSize(1.0f);
1027                 glDisable(GL_POINT_SMOOTH);
1028         }
1029
1030         glPopMatrix();
1031
1032         if(sc->flag&SC_SHOW_NAMES) {
1033                 /* scaling should be cleared before drawing texts, otherwise font would also be scaled */
1034                 track= tracksbase->first;
1035                 fp= marker_pos;
1036                 while(track) {
1037                         if((track->flag&TRACK_HIDDEN)==0) {
1038                                 marker= BKE_tracking_get_marker(track, framenr);
1039
1040                                 if(MARKER_VISIBLE(sc, marker)) {
1041                                         int act= track==act_track;
1042
1043                                         copy_v2_v2(cur_pos, fp ? fp : marker->pos);
1044
1045                                         draw_marker_texts(sc, track, marker, cur_pos, act, width, height, zoomx, zoomy);
1046
1047                                         if(fp) fp+= 2;
1048                                 }
1049                         }
1050
1051                         track= track->next;
1052                 }
1053         }
1054
1055         glPopMatrix();
1056
1057         if(marker_pos)
1058                 MEM_freeN(marker_pos);
1059 }
1060
1061 static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip, int width, int height, float zoomx, float zoomy)
1062 {
1063         float x, y;
1064         const int n= 10;
1065         int i, j, a;
1066         float pos[2], tpos[2], grid[11][11][2];
1067         MovieTracking *tracking= &clip->tracking;
1068         float aspy= 1.0f/tracking->camera.pixel_aspect;
1069         float dx= (float)width/n, dy= (float)height/n*aspy;
1070
1071         if(sc->mode!=SC_MODE_DISTORTION)
1072                 return;
1073
1074         if(!tracking->camera.focal)
1075                 return;
1076
1077         if((sc->flag&SC_SHOW_GRID)==0 && (sc->flag&SC_MANUAL_CALIBRATION)==0)
1078                 return;
1079
1080         view2d_to_region_float(&ar->v2d, 0.0f, 0.0f, &x, &y);
1081
1082         glPushMatrix();
1083         glTranslatef(x, y, 0);
1084         glScalef(zoomx, zoomy, 0);
1085         glMultMatrixf(sc->stabmat);
1086         glScalef(width, height, 0);
1087
1088         /* grid */
1089         if(sc->flag&SC_SHOW_GRID) {
1090                 float val[4][2], idx[4][2];
1091                 float min[2], max[2];
1092
1093                 for(a=0; a<4; a++) {
1094                         if(a<2) val[a][a%2]= FLT_MAX;
1095                         else val[a][a%2]= -FLT_MAX;
1096                 }
1097
1098                 zero_v2(pos);
1099                 for(i= 0; i<=n; i++) {
1100                         for(j= 0; j<=n; j++) {
1101                                 if(i==0 || j==0 || i==n || j==n) {
1102                                         BKE_tracking_apply_intrinsics(tracking, pos, tpos);
1103
1104                                         for(a=0; a<4; a++) {
1105                                                 int ok;
1106
1107                                                 if(a<2) ok= tpos[a%2] < val[a][a%2];
1108                                                 else ok= tpos[a%2] > val[a][a%2];
1109
1110                                                 if(ok) {
1111                                                         copy_v2_v2(val[a], tpos);
1112                                                         idx[a][0]= j;
1113                                                         idx[a][1]= i;
1114                                                 }
1115                                         }
1116                                 }
1117
1118                                 pos[0]+= dx;
1119                         }
1120
1121                         pos[0]= 0.0f;
1122                         pos[1]+= dy;
1123                 }
1124
1125                 INIT_MINMAX2(min, max);
1126
1127                 for(a= 0; a<4; a++) {
1128                         pos[0]= idx[a][0]*dx;
1129                         pos[1]= idx[a][1]*dy;
1130
1131                         BKE_tracking_invert_intrinsics(tracking, pos, tpos);
1132
1133                         DO_MINMAX2(tpos, min, max);
1134                 }
1135
1136                 copy_v2_v2(pos, min);
1137                 dx= (max[0]-min[0])/n;
1138                 dy= (max[1]-min[1])/n;
1139
1140                 for(i= 0; i<=n; i++) {
1141                         for(j= 0; j<=n; j++) {
1142                                 BKE_tracking_apply_intrinsics(tracking, pos, grid[i][j]);
1143
1144                                 grid[i][j][0]/= width;
1145                                 grid[i][j][1]/= height*aspy;
1146
1147                                 pos[0]+= dx;
1148                         }
1149
1150                         pos[0]= min[0];
1151                         pos[1]+= dy;
1152                 }
1153
1154                 glColor3f(1.0f, 0.0f, 0.0f);
1155
1156                 for(i= 0; i<=n; i++) {
1157                         glBegin(GL_LINE_STRIP);
1158                                 for(j= 0; j<=n; j++) {
1159                                         glVertex2fv(grid[i][j]);
1160                                 }
1161                         glEnd();
1162                 }
1163
1164                 for(j= 0; j<=n; j++) {
1165                         glBegin(GL_LINE_STRIP);
1166                                 for(i= 0; i<=n; i++) {
1167                                         glVertex2fv(grid[i][j]);
1168                                 }
1169                         glEnd();
1170                 }
1171         }
1172
1173         if(sc->flag&SC_MANUAL_CALIBRATION && clip->gpd) {
1174                 bGPDlayer *layer= clip->gpd->layers.first;
1175
1176                 while(layer) {
1177                         bGPDframe *frame= layer->frames.first;
1178
1179                         glColor4fv(layer->color);
1180                         glLineWidth(layer->thickness);
1181                         glPointSize((float)(layer->thickness + 2));
1182
1183                         while(frame) {
1184                                 bGPDstroke *stroke= frame->strokes.first;
1185
1186                                 while(stroke) {
1187                                         if(stroke->flag&GP_STROKE_2DSPACE) {
1188                                                 if(stroke->totpoints>1) {
1189                                                         glBegin(GL_LINE_STRIP);
1190                                                                 for(i= 0; i<stroke->totpoints-1; i++) {
1191                                                                         float npos[2], dpos[2], len;
1192                                                                         int steps;
1193
1194                                                                         pos[0]= stroke->points[i].x*width;
1195                                                                         pos[1]= stroke->points[i].y*height*aspy;
1196
1197                                                                         npos[0]= stroke->points[i+1].x*width;
1198                                                                         npos[1]= stroke->points[i+1].y*height*aspy;
1199
1200                                                                         len= len_v2v2(pos, npos);
1201                                                                         steps= ceil(len/5.0f);
1202
1203                                                                         /* we want to distort only long straight lines */
1204                                                                         if(stroke->totpoints==2) {
1205                                                                                 BKE_tracking_invert_intrinsics(tracking, pos, pos);
1206                                                                                 BKE_tracking_invert_intrinsics(tracking, npos, npos);
1207                                                                         }
1208
1209                                                                         sub_v2_v2v2(dpos, npos, pos);
1210                                                                         mul_v2_fl(dpos, 1.0f/steps);
1211
1212                                                                         for(j= 0; j<=steps; j++) {
1213                                                                                 BKE_tracking_apply_intrinsics(tracking, pos, tpos);
1214                                                                                 glVertex2f(tpos[0]/width, tpos[1]/(height*aspy));
1215
1216                                                                                 add_v2_v2(pos, dpos);
1217                                                                         }
1218                                                                 }
1219                                                         glEnd();
1220                                                 }
1221                                                 else if(stroke->totpoints==1) {
1222                                                         glBegin(GL_POINTS);
1223                                                                 glVertex2f(stroke->points[0].x, stroke->points[0].y);
1224                                                         glEnd();
1225                                                 }
1226                                         }
1227
1228                                         stroke= stroke->next;
1229                                 }
1230
1231                                 frame= frame->next;
1232                         }
1233
1234                         layer= layer->next;
1235                 }
1236
1237                 glLineWidth(1.0f);
1238                 glPointSize(1.0f);
1239         }
1240
1241         glPopMatrix();
1242 }
1243
1244 void clip_draw_main(SpaceClip *sc, ARegion *ar, Scene *scene)
1245 {
1246         MovieClip *clip= ED_space_clip(sc);
1247         ImBuf *ibuf;
1248         int width, height;
1249         float zoomx, zoomy;
1250
1251         /* if no clip, nothing to do */
1252         if(!clip)
1253                 return;
1254
1255         ED_space_clip_size(sc, &width, &height);
1256         ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
1257
1258         if(sc->flag&SC_SHOW_STABLE) {
1259                 float smat[4][4], ismat[4][4];
1260
1261                 ibuf= ED_space_clip_get_stable_buffer(sc, sc->loc, &sc->scale, &sc->angle);
1262                 BKE_tracking_stabdata_to_mat4(width, height, sc->loc, sc->scale, sc->angle, sc->stabmat);
1263
1264                 unit_m4(smat);
1265                 smat[0][0]= 1.0f/width;
1266                 smat[1][1]= 1.0f/height;
1267                 invert_m4_m4(ismat, smat);
1268
1269                 mul_serie_m4(sc->unistabmat, smat, sc->stabmat, ismat, NULL, NULL, NULL, NULL, NULL);
1270         } else {
1271                 ibuf= ED_space_clip_get_buffer(sc);
1272
1273                 zero_v2(sc->loc);
1274                 sc->scale= 1.0f;
1275                 unit_m4(sc->stabmat);
1276                 unit_m4(sc->unistabmat);
1277         }
1278
1279         if(ibuf) {
1280                 draw_movieclip_buffer(sc, ar, ibuf, width, height, zoomx, zoomy);
1281                 IMB_freeImBuf(ibuf);
1282
1283                 draw_tracking_tracks(sc, ar, clip, width, height, zoomx, zoomy);
1284                 draw_distortion(sc, ar, clip, width, height, zoomx, zoomy);
1285         }
1286
1287         draw_movieclip_cache(sc, ar, clip, scene);
1288         draw_movieclip_notes(sc, ar);
1289 }
1290
1291 /* draw grease pencil */
1292 void clip_draw_grease_pencil(bContext *C, int onlyv2d)
1293 {
1294         SpaceClip *sc= CTX_wm_space_clip(C);
1295         MovieClip *clip= ED_space_clip(sc);
1296         ImBuf *ibuf;
1297
1298         if((sc->flag&SC_SHOW_GPENCIL)==0 || !clip)
1299                 return;
1300
1301         if(onlyv2d) {
1302                 /* if manual calibration is used then grase pencil data is already
1303                     drawed in draw_distortion */
1304                 if((sc->flag&SC_MANUAL_CALIBRATION)==0 || sc->mode!=SC_MODE_DISTORTION) {
1305                         ibuf= ED_space_clip_get_buffer(sc);
1306
1307                         if(ibuf) {
1308                                 glPushMatrix();
1309                                 glMultMatrixf(sc->unistabmat);
1310                                 draw_gpencil_2dimage(C, ibuf);
1311
1312                                 IMB_freeImBuf(ibuf);
1313                                 glPopMatrix();
1314                         }
1315                 }
1316         } else {
1317                 draw_gpencil_view2d(C, 0);
1318         }
1319 }