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