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