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