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