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