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