Merging r46725 through r46963 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_clip / clip_draw.c
index c00359f..c0ac262 100644 (file)
@@ -428,14 +428,17 @@ static void draw_marker_outline(SpaceClip *sc, MovieTrackingTrack *track, MovieT
 
        if ((marker->flag & MARKER_DISABLED) == 0) {
                float pos[2];
-               rctf r;
+               float p[2];
 
-               BLI_init_rctf(&r, track->pat_min[0], track->pat_max[0], track->pat_min[1], track->pat_max[1]);
                add_v2_v2v2(pos, marker->pos, track->offset);
 
                ED_clip_point_undistorted_pos(sc, pos, pos);
 
-               if (BLI_in_rctf(&r, pos[0] - marker_pos[0], pos[1] - marker_pos[1])) {
+               sub_v2_v2v2(p, pos, marker_pos);
+
+               if (isect_point_quad_v2(p, marker->pattern_corners[0], marker->pattern_corners[1],
+                                       marker->pattern_corners[2], marker->pattern_corners[3]))
+               {
                        if (tiny) glPointSize(3.0f);
                        else glPointSize(4.0f);
                        glBegin(GL_POINTS);
@@ -471,10 +474,10 @@ static void draw_marker_outline(SpaceClip *sc, MovieTrackingTrack *track, MovieT
 
        if (sc->flag & SC_SHOW_MARKER_PATTERN) {
                glBegin(GL_LINE_LOOP);
-                       glVertex2f(track->pat_min[0], track->pat_min[1]);
-                       glVertex2f(track->pat_max[0], track->pat_min[1]);
-                       glVertex2f(track->pat_max[0], track->pat_max[1]);
-                       glVertex2f(track->pat_min[0], track->pat_max[1]);
+                       glVertex2fv(marker->pattern_corners[0]);
+                       glVertex2fv(marker->pattern_corners[1]);
+                       glVertex2fv(marker->pattern_corners[2]);
+                       glVertex2fv(marker->pattern_corners[3]);
                glEnd();
        }
 
@@ -482,10 +485,10 @@ static void draw_marker_outline(SpaceClip *sc, MovieTrackingTrack *track, MovieT
                      ((marker->flag & MARKER_DISABLED) == 0 || (sc->flag & SC_SHOW_MARKER_PATTERN) == 0);
        if (sc->flag & SC_SHOW_MARKER_SEARCH && show_search) {
                glBegin(GL_LINE_LOOP);
-                       glVertex2f(track->search_min[0], track->search_min[1]);
-                       glVertex2f(track->search_max[0], track->search_min[1]);
-                       glVertex2f(track->search_max[0], track->search_max[1]);
-                       glVertex2f(track->search_min[0], track->search_max[1]);
+                       glVertex2f(marker->search_min[0], marker->search_min[1]);
+                       glVertex2f(marker->search_max[0], marker->search_min[1]);
+                       glVertex2f(marker->search_max[0], marker->search_max[1]);
+                       glVertex2f(marker->search_min[0], marker->search_max[1]);
                glEnd();
        }
        glPopMatrix();
@@ -528,8 +531,7 @@ static void draw_marker_areas(SpaceClip *sc, MovieTrackingTrack *track, MovieTra
 
        /* marker position and offset position */
        if ((track->flag & SELECT) == sel && (marker->flag & MARKER_DISABLED) == 0) {
-               float pos[2];
-               rctf r;
+               float pos[2], p[2];
 
                if (track->flag & TRACK_LOCKED) {
                        if (act)
@@ -546,11 +548,14 @@ static void draw_marker_areas(SpaceClip *sc, MovieTrackingTrack *track, MovieTra
                                glColor3fv(col);
                }
 
-               BLI_init_rctf(&r, track->pat_min[0], track->pat_max[0], track->pat_min[1], track->pat_max[1]);
                add_v2_v2v2(pos, marker->pos, track->offset);
                ED_clip_point_undistorted_pos(sc, pos, pos);
 
-               if (BLI_in_rctf(&r, pos[0] - marker_pos[0], pos[1] - marker_pos[1])) {
+               sub_v2_v2v2(p, pos, marker_pos);
+
+               if (isect_point_quad_v2(p, marker->pattern_corners[0], marker->pattern_corners[1],
+                                       marker->pattern_corners[2], marker->pattern_corners[3]))
+               {
                        if (!tiny)
                                glPointSize(2.0f);
 
@@ -623,10 +628,10 @@ static void draw_marker_areas(SpaceClip *sc, MovieTrackingTrack *track, MovieTra
                }
 
                glBegin(GL_LINE_LOOP);
-                       glVertex2f(track->pat_min[0], track->pat_min[1]);
-                       glVertex2f(track->pat_max[0], track->pat_min[1]);
-                       glVertex2f(track->pat_max[0], track->pat_max[1]);
-                       glVertex2f(track->pat_min[0], track->pat_max[1]);
+                       glVertex2fv(marker->pattern_corners[0]);
+                       glVertex2fv(marker->pattern_corners[1]);
+                       glVertex2fv(marker->pattern_corners[2]);
+                       glVertex2fv(marker->pattern_corners[3]);
                glEnd();
        }
 
@@ -656,70 +661,82 @@ static void draw_marker_areas(SpaceClip *sc, MovieTrackingTrack *track, MovieTra
                }
 
                glBegin(GL_LINE_LOOP);
-                       glVertex2f(track->search_min[0], track->search_min[1]);
-                       glVertex2f(track->search_max[0], track->search_min[1]);
-                       glVertex2f(track->search_max[0], track->search_max[1]);
-                       glVertex2f(track->search_min[0], track->search_max[1]);
+                       glVertex2f(marker->search_min[0], marker->search_min[1]);
+                       glVertex2f(marker->search_max[0], marker->search_min[1]);
+                       glVertex2f(marker->search_max[0], marker->search_max[1]);
+                       glVertex2f(marker->search_min[0], marker->search_max[1]);
                glEnd();
        }
 
-       /* pyramid */
-       if (sel && TRACK_VIEW_SELECTED(sc, track) &&
-           (track->tracker == TRACKER_KLT) &&
-               (marker->flag & MARKER_DISABLED) == 0)
-       {
-               if (track->flag & TRACK_LOCKED) {
-                       if (act)
-                               UI_ThemeColor(TH_ACT_MARKER);
-                       else if (track->pat_flag & SELECT)
-                               UI_ThemeColorShade(TH_LOCK_MARKER, 64);
-                       else UI_ThemeColor(TH_LOCK_MARKER);
-               }
-               else if (marker->flag & MARKER_DISABLED) {
-                       if (act)
-                               UI_ThemeColor(TH_ACT_MARKER);
-                       else if (track->pat_flag & SELECT)
-                               UI_ThemeColorShade(TH_DIS_MARKER, 128);
-                       else UI_ThemeColor(TH_DIS_MARKER);
-               }
-               else {
-                       if (track->pat_flag & SELECT)
-                               glColor3fv(scol);
-                       else
-                               glColor3fv(col);
-               }
-
-               {
-                       int i = 0;
-                       glPushMatrix();
-                       glEnable(GL_LINE_STIPPLE);
-                       for (i = 1; i < track->pyramid_levels; ++i) {
-                               glScalef(2.0f, 2.0f, 1.0);
-                       }
-                       /* only draw a pattern for the coarsest level */
-                       glBegin(GL_LINE_LOOP);
-                               glVertex2f(track->pat_min[0], track->pat_min[1]);
-                               glVertex2f(track->pat_max[0], track->pat_min[1]);
-                               glVertex2f(track->pat_max[0], track->pat_max[1]);
-                               glVertex2f(track->pat_min[0], track->pat_max[1]);
-                       glEnd();
-                       glDisable(GL_LINE_STIPPLE);
-                       glPopMatrix();
-               }
-       }
-
        if (tiny)
                glDisable(GL_LINE_STIPPLE);
 
        glPopMatrix();
 }
 
+static float get_shortest_pattern_side(MovieTrackingMarker *marker)
+{
+       int i, next;
+       float len = FLT_MAX;
+
+       for (i = 0; i < 4; i++) {
+               float cur_len;
+
+               next = (i + 1) % 4;
+
+               cur_len = len_v2v2(marker->pattern_corners[i], marker->pattern_corners[next]);
+
+               len = MIN2(cur_len, len);
+       }
+
+       return len;
+}
+
+static void draw_marker_slide_square(float x, float y, float dx, float dy, int outline, float px[2])
+{
+       float tdx, tdy;
+
+       tdx = dx;
+       tdy = dy;
+
+       if (outline) {
+               tdx += px[0];
+               tdy += px[1];
+       }
+
+       glBegin(GL_QUADS);
+               glVertex3f(x - tdx, y + tdy, 0.0f);
+               glVertex3f(x + tdx, y + tdy, 0.0f);
+               glVertex3f(x + tdx, y - tdy, 0.0f);
+               glVertex3f(x - tdx, y - tdy, 0.0f);
+       glEnd();
+}
+
+static void draw_marker_slide_triangle(float x, float y, float dx, float dy, int outline, float px[2])
+{
+       float tdx, tdy;
+
+       tdx = dx * 2.0f;
+       tdy = dy * 2.0f;
+
+       if (outline) {
+               tdx += px[0];
+               tdy += px[1];
+       }
+
+       glBegin(GL_TRIANGLES);
+               glVertex3f(x,       y,       0.0f);
+               glVertex3f(x - tdx, y,       0.0f);
+               glVertex3f(x,       y + tdy, 0.0f);
+       glEnd();
+}
+
 static void draw_marker_slide_zones(SpaceClip *sc, MovieTrackingTrack *track, MovieTrackingMarker *marker,
                                     float marker_pos[2], int outline, int sel, int act, int width, int height)
 {
-       float x, y, dx, dy, patdx, patdy, searchdx, searchdy, tdx, tdy;
+       float dx, dy, patdx, patdy, searchdx, searchdy;
        int tiny = sc->flag & SC_SHOW_TINY_MARKER;
-       float col[3], scol[3], px[2];
+       float col[3], scol[3], px[2], side;
 
        if ((tiny && outline) || (marker->flag & MARKER_DISABLED))
                return;
@@ -740,11 +757,12 @@ static void draw_marker_slide_zones(SpaceClip *sc, MovieTrackingTrack *track, Mo
        dx = 6.0f / width / sc->zoom;
        dy = 6.0f / height / sc->zoom;
 
-       patdx = MIN2(dx * 2.0f / 3.0f, (track->pat_max[0] - track->pat_min[0]) / 6.0f);
-       patdy = MIN2(dy * 2.0f / 3.0f, (track->pat_max[1] - track->pat_min[1]) / 6.0f);
+       side = get_shortest_pattern_side(marker);
+       patdx = MIN2(dx * 2.0f / 3.0f, side / 6.0f);
+       patdy = MIN2(dy * 2.0f / 3.0f, side * width / height / 6.0f);
 
-       searchdx = MIN2(dx, (track->search_max[0] - track->search_min[0]) / 6.0f);
-       searchdy = MIN2(dy, (track->search_max[1] - track->search_min[1]) / 6.0f);
+       searchdx = MIN2(dx, (marker->search_max[0] - marker->search_min[0]) / 6.0f);
+       searchdy = MIN2(dy, (marker->search_max[1] - marker->search_min[1]) / 6.0f);
 
        px[0] = 1.0f / sc->zoom / width / sc->scale;
        px[1] = 1.0f / sc->zoom / height / sc->scale;
@@ -758,41 +776,10 @@ static void draw_marker_slide_zones(SpaceClip *sc, MovieTrackingTrack *track, Mo
                }
 
                /* search offset square */
-               x = track->search_min[0];
-               y = track->search_max[1];
-
-               tdx = searchdx;
-               tdy = searchdy;
-
-               if (outline) {
-                       tdx += px[0];
-                       tdy += px[1];
-               }
-
-               glBegin(GL_QUADS);
-                       glVertex3f(x - tdx, y + tdy, 0);
-                       glVertex3f(x + tdx, y + tdy, 0);
-                       glVertex3f(x + tdx, y - tdy, 0);
-                       glVertex3f(x - tdx, y - tdy, 0);
-               glEnd();
+               draw_marker_slide_square(marker->search_min[0], marker->search_max[1], searchdx, searchdy, outline, px);
 
                /* search re-sizing triangle */
-               x = track->search_max[0];
-               y = track->search_min[1];
-
-               tdx = searchdx * 2.0f;
-               tdy = searchdy * 2.0f;
-
-               if (outline) {
-                       tdx += px[0];
-                       tdy += px[1];
-               }
-
-               glBegin(GL_TRIANGLES);
-                       glVertex3f(x, y, 0);
-                       glVertex3f(x - tdx, y, 0);
-                       glVertex3f(x, y + tdy, 0);
-               glEnd();
+               draw_marker_slide_triangle(marker->search_max[0], marker->search_min[1], searchdx, searchdy, outline, px);
        }
 
        if ((sc->flag & SC_SHOW_MARKER_PATTERN) && ((track->pat_flag & SELECT) == sel || outline)) {
@@ -803,42 +790,26 @@ static void draw_marker_slide_zones(SpaceClip *sc, MovieTrackingTrack *track, Mo
                                glColor3fv(col);
                }
 
-               /* pattern offset square */
-               x = track->pat_min[0];
-               y = track->pat_max[1];
-
-               tdx = patdx;
-               tdy = patdy;
+               /* XXX: need to be real check if affine tracking is enabled, but for now not
+                *      sure how to do this, so assume affine tracker is always enabled */
+               if (TRUE) {
+                       int i;
 
-               if (outline) {
-                       tdx += px[0];
-                       tdy += px[1];
+                       /* pattern's corners sliding squares */
+                       for (i = 0; i < 4; i++) {
+                               draw_marker_slide_square(marker->pattern_corners[i][0], marker->pattern_corners[i][1],
+                                                        patdx / 1.5f, patdy / 1.5f, outline, px);
+                       }
                }
+               else {
+                       /* pattern offset square */
+                       draw_marker_slide_square(marker->pattern_corners[3][0], marker->pattern_corners[3][1],
+                                                patdx, patdy, outline, px);
 
-               glBegin(GL_QUADS);
-                       glVertex3f(x - tdx, y + tdy, 0);
-                       glVertex3f(x + tdx, y + tdy, 0);
-                       glVertex3f(x + tdx, y - tdy, 0);
-                       glVertex3f(x - tdx, y - tdy, 0);
-               glEnd();
-
-               /* pattern re-sizing triangle */
-               x = track->pat_max[0];
-               y = track->pat_min[1];
-
-               tdx = patdx*2.0f;
-               tdy = patdy*2.0f;
-
-               if (outline) {
-                       tdx += px[0];
-                       tdy += px[1];
+                       /* pattern re-sizing triangle */
+                       draw_marker_slide_triangle(marker->pattern_corners[1][0], marker->pattern_corners[1][1],
+                                                  patdx, patdy, outline, px);
                }
-
-               glBegin(GL_TRIANGLES);
-                       glVertex3f(x, y, 0);
-                       glVertex3f(x - tdx, y, 0);
-                       glVertex3f(x, y + tdy, 0);
-               glEnd();
        }
 
        glPopMatrix();
@@ -877,12 +848,15 @@ static void draw_marker_texts(SpaceClip *sc, MovieTrackingTrack *track, MovieTra
        if ((sc->flag & SC_SHOW_MARKER_SEARCH) &&
           ((marker->flag & MARKER_DISABLED) == 0 || (sc->flag & SC_SHOW_MARKER_PATTERN) == 0))
        {
-               dx = track->search_min[0];
-               dy = track->search_min[1];
+               dx = marker->search_min[0];
+               dy = marker->search_min[1];
        }
        else if (sc->flag & SC_SHOW_MARKER_PATTERN) {
-               dx = track->pat_min[0];
-               dy = track->pat_min[1];
+               float pat_min[2], pat_max[2];
+
+               BKE_tracking_marker_pattern_minmax(marker, pat_min, pat_max);
+               dx = pat_min[0];
+               dy = pat_min[1];
        }
 
        pos[0] = (marker_pos[0] + dx) * width;
@@ -1175,8 +1149,10 @@ static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip,
        int i, j, a;
        float pos[2], tpos[2], grid[11][11][2];
        MovieTracking *tracking = &clip->tracking;
+       bGPdata *gpd = NULL;
        float aspy = 1.0f / tracking->camera.pixel_aspect;
        float dx = (float)width / n, dy = (float)height / n * aspy;
+       float offsx = 0.0f, offsy = 0.0f;
 
        if (sc->mode != SC_MODE_DISTORTION)
                return;
@@ -1284,8 +1260,26 @@ static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip,
                }
        }
 
-       if (sc->flag & SC_MANUAL_CALIBRATION && clip->gpd) {
-               bGPDlayer *layer = clip->gpd->layers.first;
+       if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
+               MovieTrackingTrack *track = BKE_tracking_active_track(&sc->clip->tracking);
+
+               if (track) {
+                       int framenr = sc->user.framenr;
+                       MovieTrackingMarker *marker = BKE_tracking_exact_marker(track, framenr);
+
+                       offsx = marker->pos[0];
+                       offsy = marker->pos[1];
+
+                       gpd = track->gpd;
+               }
+
+       }
+       else {
+               gpd = clip->gpd;
+       }
+
+       if (sc->flag & SC_MANUAL_CALIBRATION && gpd) {
+               bGPDlayer *layer = gpd->layers.first;
 
                while (layer) {
                        bGPDframe *frame = layer->frames.first;
@@ -1310,11 +1304,11 @@ static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip,
                                                                        float npos[2], dpos[2], len;
                                                                        int steps;
 
-                                                                       pos[0] = stroke->points[i].x * width;
-                                                                       pos[1] = stroke->points[i].y * height * aspy;
+                                                                       pos[0] = (stroke->points[i].x + offsx) * width;
+                                                                       pos[1] = (stroke->points[i].y + offsy) * height * aspy;
 
-                                                                       npos[0] = stroke->points[i + 1].x * width;
-                                                                       npos[1] = stroke->points[i + 1].y * height * aspy;
+                                                                       npos[0] = (stroke->points[i + 1].x + offsx) * width;
+                                                                       npos[1] = (stroke->points[i + 1].y + offsy) * height * aspy;
 
                                                                        len = len_v2v2(pos, npos);
                                                                        steps = ceil(len / 5.0f);
@@ -1339,7 +1333,7 @@ static void draw_distortion(SpaceClip *sc, ARegion *ar, MovieClip *clip,
                                                }
                                                else if (stroke->totpoints == 1) {
                                                        glBegin(GL_POINTS);
-                                                               glVertex2f(stroke->points[0].x, stroke->points[0].y);
+                                                               glVertex2f(stroke->points[0].x + offsx, stroke->points[0].y + offsy);
                                                        glEnd();
                                                }
                                        }
@@ -1443,6 +1437,18 @@ void clip_draw_grease_pencil(bContext *C, int onlyv2d)
                        if (ibuf) {
                                glPushMatrix();
                                glMultMatrixf(sc->unistabmat);
+
+                               if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
+                                       MovieTrackingTrack *track = BKE_tracking_active_track(&sc->clip->tracking);
+
+                                       if (track) {
+                                               int framenr = sc->user.framenr;
+                                               MovieTrackingMarker *marker = BKE_tracking_exact_marker(track, framenr);
+
+                                               glTranslatef(marker->pos[0], marker->pos[1], 0.0f);
+                                       }
+                               }
+
                                draw_gpencil_2dimage(C, ibuf);
 
                                IMB_freeImBuf(ibuf);