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