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