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