use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender.git] / source / blender / editors / space_view3d / drawobject.c
index e40fe61..1f57845 100644 (file)
@@ -1500,48 +1500,49 @@ static void draw_bundle_sphere(void)
        glCallList(displist);
 }
 
-static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d, MovieClip *clip, int flag)
+static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D *v3d,
+                       MovieClip *clip, MovieTrackingObject *tracking_object, int flag, int *global_track_index)
 {
        MovieTracking *tracking= &clip->tracking;
        MovieTrackingTrack *track;
-       float mat[4][4], imat[4][4], curcol[4];
+       float mat[4][4], imat[4][4];
        unsigned char col[4], scol[4];
-       int bundlenr= 1;
-
-       if((v3d->flag2&V3D_SHOW_RECONSTRUCTION)==0)
-               return;
-
-       if(v3d->flag2&V3D_RENDER_OVERRIDE)
-               return;
-
-       glGetFloatv(GL_CURRENT_COLOR, curcol);
+       int tracknr= *global_track_index;
+       ListBase *tracksbase= BKE_tracking_object_tracks(tracking, tracking_object);
 
        UI_GetThemeColor4ubv(TH_TEXT, col);
        UI_GetThemeColor4ubv(TH_SELECT, scol);
 
        BKE_get_tracking_mat(scene, base->object, mat);
 
-       glEnable(GL_LIGHTING);
-       glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
-       glEnable(GL_COLOR_MATERIAL);
-       glShadeModel(GL_SMOOTH);
+       glPushMatrix();
 
-       /* current ogl matrix is translated in camera space, bundles should
-          be rendered in world space, so camera matrix should be "removed"
-          from current ogl matrix */
-       invert_m4_m4(imat, base->object->obmat);
+       if(tracking_object->flag & TRACKING_OBJECT_CAMERA) {
+               /* current ogl matrix is translated in camera space, bundles should
+                  be rendered in world space, so camera matrix should be "removed"
+                  from current ogl matrix */
+               invert_m4_m4(imat, base->object->obmat);
 
-       glPushMatrix();
-       glMultMatrixf(imat);
-       glMultMatrixf(mat);
+               glMultMatrixf(imat);
+               glMultMatrixf(mat);
+       }
+       else {
+               float obmat[4][4];
+
+               BKE_tracking_get_interpolated_camera(tracking, tracking_object, scene->r.cfra, obmat);
+
+               invert_m4_m4(imat, obmat);
+               glMultMatrixf(imat);
+       }
+
+       for (track= tracksbase->first; track; track= track->next) {
+               int selected= TRACK_SELECTED(track);
 
-       for ( track= tracking->tracks.first; track; track= track->next) {
-               int selected= track->flag&SELECT || track->pat_flag&SELECT || track->search_flag&SELECT;
                if((track->flag&TRACK_HAS_BUNDLE)==0)
                        continue;
 
                if(flag&DRAW_PICKING)
-                       glLoadName(base->selcol + (bundlenr<<16));
+                       glLoadName(base->selcol + (tracknr<<16));
 
                glPushMatrix();
                        glTranslatef(track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
@@ -1549,7 +1550,6 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d,
 
                        if(v3d->drawtype==OB_WIRE) {
                                glDisable(GL_LIGHTING);
-                               glDepthMask(0);
 
                                if(selected) {
                                        if(base==BASACT) UI_ThemeColor(TH_ACTIVE);
@@ -1561,7 +1561,6 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d,
 
                                drawaxes(0.05f, v3d->bundle_drawtype);
 
-                               glDepthMask(1);
                                glEnable(GL_LIGHTING);
                        } else if(v3d->drawtype>OB_WIRE) {
                                if(v3d->bundle_drawtype==OB_EMPTY_SPHERE) {
@@ -1570,7 +1569,6 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d,
                                                if(base==BASACT) UI_ThemeColor(TH_ACTIVE);
                                                else UI_ThemeColor(TH_SELECT);
 
-                                               glDepthMask(0);
                                                glLineWidth(2.f);
                                                glDisable(GL_LIGHTING);
                                                glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@@ -1580,7 +1578,6 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d,
                                                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                                                glEnable(GL_LIGHTING);
                                                glLineWidth(1.f);
-                                               glDepthMask(1);
                                        }
 
                                        if(track->flag&TRACK_CUSTOMCOLOR) glColor3fv(track->color);
@@ -1589,7 +1586,6 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d,
                                        draw_bundle_sphere();
                                } else {
                                        glDisable(GL_LIGHTING);
-                                       glDepthMask(0);
 
                                        if(selected) {
                                                if(base==BASACT) UI_ThemeColor(TH_ACTIVE);
@@ -1601,7 +1597,6 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d,
 
                                        drawaxes(0.05f, v3d->bundle_drawtype);
 
-                                       glDepthMask(1);
                                        glEnable(GL_LIGHTING);
                                }
                        }
@@ -1619,32 +1614,67 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d,
                        view3d_cached_text_draw_add(pos, track->name, 10, V3D_CACHE_TEXT_GLOBALSPACE, tcol);
                }
 
-               bundlenr++;
+               tracknr++;
        }
 
        if((flag & DRAW_PICKING)==0) {
-               if(v3d->flag2&V3D_SHOW_CAMERAPATH && clip->tracking.reconstruction.camnr) {
-                       int a= 0;
-                       MovieTrackingReconstruction *reconstruction= &tracking->reconstruction;
-                       MovieReconstructedCamera *camera= tracking->reconstruction.cameras;
+               if((v3d->flag2&V3D_SHOW_CAMERAPATH) && (tracking_object->flag&TRACKING_OBJECT_CAMERA)) {
+                       MovieTrackingReconstruction *reconstruction;
+                       reconstruction= BKE_tracking_object_reconstruction(tracking, tracking_object);
 
-                       glDisable(GL_LIGHTING);
-                       UI_ThemeColor(TH_CAMERA_PATH);
-                       glLineWidth(2.0f);
+                       if(reconstruction->camnr) {
+                               MovieReconstructedCamera *camera= reconstruction->cameras;
+                               int a= 0;
 
-                       glBegin(GL_LINE_STRIP);
-                               for(a= 0; a<reconstruction->camnr; a++, camera++) {
-                                       glVertex3fv(camera->mat[3]);
-                               }
-                       glEnd();
+                               glDisable(GL_LIGHTING);
+                               UI_ThemeColor(TH_CAMERA_PATH);
+                               glLineWidth(2.0f);
 
-                       glLineWidth(1.0f);
-                       glEnable(GL_LIGHTING);
+                               glBegin(GL_LINE_STRIP);
+                                       for(a= 0; a<reconstruction->camnr; a++, camera++) {
+                                               glVertex3fv(camera->mat[3]);
+                                       }
+                                       glEnd();
+
+                                       glLineWidth(1.0f);
+                                       glEnable(GL_LIGHTING);
+                       }
                }
        }
 
        glPopMatrix();
 
+       *global_track_index= tracknr;
+}
+
+static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d, MovieClip *clip, int flag)
+{
+       MovieTracking *tracking= &clip->tracking;
+       MovieTrackingObject *tracking_object;
+       float curcol[4];
+       int global_track_index= 1;
+
+       if((v3d->flag2&V3D_SHOW_RECONSTRUCTION)==0)
+               return;
+
+       if(v3d->flag2&V3D_RENDER_OVERRIDE)
+               return;
+
+       glGetFloatv(GL_CURRENT_COLOR, curcol);
+
+       glEnable(GL_LIGHTING);
+       glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
+       glEnable(GL_COLOR_MATERIAL);
+       glShadeModel(GL_SMOOTH);
+
+       tracking_object= tracking->objects.first;
+       while(tracking_object) {
+               draw_viewport_object_reconstruction(scene, base, v3d, clip, tracking_object,
+                                       flag, &global_track_index);
+
+               tracking_object= tracking_object->next;
+       }
+
        /* restore */
        glShadeModel(GL_FLAT);
        glDisable(GL_COLOR_MATERIAL);
@@ -2633,7 +2663,7 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, EditMesh *em, UnitSet
        EditFace *efa;
        float v1[3], v2[3], v3[3], v4[3], vmid[3];
        float fvec[3];
-       char val[32]; /* Stores the measurement display text here */
+       char numstr[32]; /* Stores the measurement display text here */
        const char *conv_float; /* Use a float conversion matching the grid size */
        unsigned char col[4]= {0, 0, 0, 255}; /* color of the text to draw */
        float area; /* area of the face */
@@ -2666,11 +2696,11 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, EditMesh *em, UnitSet
                                        mul_mat3_m4_v3(ob->obmat, v2);
                                }
                                if(unit->system)
-                                       bUnit_AsString(val, sizeof(val), len_v3v3(v1, v2)*unit->scale_length, 3, unit->system, B_UNIT_LENGTH, do_split, FALSE);
+                                       bUnit_AsString(numstr, sizeof(numstr), len_v3v3(v1, v2)*unit->scale_length, 3, unit->system, B_UNIT_LENGTH, do_split, FALSE);
                                else
-                                       sprintf(val, conv_float, len_v3v3(v1, v2));
+                                       sprintf(numstr, conv_float, len_v3v3(v1, v2));
 
-                               view3d_cached_text_draw_add(vmid, val, 0, V3D_CACHE_TEXT_ASCII, col);
+                               view3d_cached_text_draw_add(vmid, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
                        }
                }
        }
@@ -2700,11 +2730,11 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, EditMesh *em, UnitSet
                                        area = area_tri_v3(v1, v2, v3);
 
                                if(unit->system)
-                                       bUnit_AsString(val, sizeof(val), area*unit->scale_length, 3, unit->system, B_UNIT_LENGTH, do_split, FALSE); // XXX should be B_UNIT_AREA
+                                       bUnit_AsString(numstr, sizeof(numstr), area*unit->scale_length, 3, unit->system, B_UNIT_LENGTH, do_split, FALSE); // XXX should be B_UNIT_AREA
                                else
-                                       sprintf(val, conv_float, area);
+                                       sprintf(numstr, conv_float, area);
 
-                               view3d_cached_text_draw_add(efa->cent, val, 0, V3D_CACHE_TEXT_ASCII, col);
+                               view3d_cached_text_draw_add(efa->cent, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
                        }
                }
        }
@@ -2738,31 +2768,31 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, EditMesh *em, UnitSet
                                
                        if( (e4->f & e1->f & SELECT) || (do_moving && (efa->v1->f & SELECT)) ) {
                                /* Vec 1 */
-                               sprintf(val,"%.3g", RAD2DEGF(angle_v3v3v3(v4, v1, v2)));
+                               sprintf(numstr,"%.3g", RAD2DEGF(angle_v3v3v3(v4, v1, v2)));
                                interp_v3_v3v3(fvec, efa->cent, efa->v1->co, 0.8f);
-                               view3d_cached_text_draw_add(fvec, val, 0, V3D_CACHE_TEXT_ASCII, col);
+                               view3d_cached_text_draw_add(fvec, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
                        }
                        if( (e1->f & e2->f & SELECT) || (do_moving && (efa->v2->f & SELECT)) ) {
                                /* Vec 2 */
-                               sprintf(val,"%.3g", RAD2DEGF(angle_v3v3v3(v1, v2, v3)));
+                               sprintf(numstr,"%.3g", RAD2DEGF(angle_v3v3v3(v1, v2, v3)));
                                interp_v3_v3v3(fvec, efa->cent, efa->v2->co, 0.8f);
-                               view3d_cached_text_draw_add(fvec, val, 0, V3D_CACHE_TEXT_ASCII, col);
+                               view3d_cached_text_draw_add(fvec, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
                        }
                        if( (e2->f & e3->f & SELECT) || (do_moving && (efa->v3->f & SELECT)) ) {
                                /* Vec 3 */
                                if(efa->v4) 
-                                       sprintf(val,"%.3g", RAD2DEGF(angle_v3v3v3(v2, v3, v4)));
+                                       sprintf(numstr,"%.3g", RAD2DEGF(angle_v3v3v3(v2, v3, v4)));
                                else
-                                       sprintf(val,"%.3g", RAD2DEGF(angle_v3v3v3(v2, v3, v1)));
+                                       sprintf(numstr,"%.3g", RAD2DEGF(angle_v3v3v3(v2, v3, v1)));
                                interp_v3_v3v3(fvec, efa->cent, efa->v3->co, 0.8f);
-                               view3d_cached_text_draw_add(fvec, val, 0, V3D_CACHE_TEXT_ASCII, col);
+                               view3d_cached_text_draw_add(fvec, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
                        }
                                /* Vec 4 */
                        if(efa->v4) {
                                if( (e3->f & e4->f & SELECT) || (do_moving && (efa->v4->f & SELECT)) ) {
-                                       sprintf(val,"%.3g", RAD2DEGF(angle_v3v3v3(v3, v4, v1)));
+                                       sprintf(numstr,"%.3g", RAD2DEGF(angle_v3v3v3(v3, v4, v1)));
                                        interp_v3_v3v3(fvec, efa->cent, efa->v4->co, 0.8f);
-                                       view3d_cached_text_draw_add(fvec, val, 0, V3D_CACHE_TEXT_ASCII, col);
+                                       view3d_cached_text_draw_add(fvec, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
                                }
                        }
                }
@@ -2775,34 +2805,40 @@ static void draw_em_indices(EditMesh *em)
        EditFace *f;
        EditVert *v;
        int i;
-       char val[32];
+       char numstr[32];
        float pos[3];
        unsigned char col[4];
 
        /* For now, reuse appropriate theme colors from stats text colors */
 
-       UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEANG, col);
-       for (v = em->verts.first, i = 0; v; v = v->next, i++) {
-               if (v->f & SELECT) {
-                       sprintf(val, "%d", i);
-                       view3d_cached_text_draw_add(v->co, val, 0, V3D_CACHE_TEXT_ASCII, col);
+       if (em->selectmode & SCE_SELECT_VERTEX) {
+               UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEANG, col);
+               for (v = em->verts.first, i = 0; v; v = v->next, i++) {
+                       if (v->f & SELECT) {
+                               sprintf(numstr, "%d", i);
+                               view3d_cached_text_draw_add(v->co, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
+                       }
                }
        }
 
-       UI_GetThemeColor3ubv(TH_DRAWEXTRA_EDGELEN, col);
-       for (e = em->edges.first, i = 0; e; e = e->next, i++) {
-               if (e->f & SELECT) {
-                       sprintf(val, "%d", i);
-                       mid_v3_v3v3(pos, e->v1->co, e->v2->co);
-                       view3d_cached_text_draw_add(pos, val, 0, V3D_CACHE_TEXT_ASCII, col);
+       if (em->selectmode & SCE_SELECT_EDGE) {
+               UI_GetThemeColor3ubv(TH_DRAWEXTRA_EDGELEN, col);
+               for (e = em->edges.first, i = 0; e; e = e->next, i++) {
+                       if (e->f & SELECT) {
+                               sprintf(numstr, "%d", i);
+                               mid_v3_v3v3(pos, e->v1->co, e->v2->co);
+                               view3d_cached_text_draw_add(pos, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
+                       }
                }
        }
 
-       UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEAREA, col);
-       for (f = em->faces.first, i = 0; f; f = f->next, i++) {
-               if (f->f & SELECT) {
-                       sprintf(val, "%d", i);
-                       view3d_cached_text_draw_add(f->cent, val, 0, V3D_CACHE_TEXT_ASCII, col);
+       if (em->selectmode & SCE_SELECT_FACE) {
+               UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEAREA, col);
+               for (f = em->faces.first, i = 0; f; f = f->next, i++) {
+                       if (f->f & SELECT) {
+                               sprintf(numstr, "%d", i);
+                               view3d_cached_text_draw_add(f->cent, numstr, 0, V3D_CACHE_TEXT_ASCII, col);
+                       }
                }
        }
 }
@@ -4006,7 +4042,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
        int a, totpart, totpoint=0, totve=0, drawn, draw_as, totchild=0;
        int select=ob->flag&SELECT, create_cdata=0, need_v=0;
        GLint polygonmode[2];
-       char val[32];
+       char numstr[32];
        unsigned char tcol[4]= {0, 0, 0, 255};
 
 /* 1. */
@@ -4394,8 +4430,8 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
                                if((part->draw & PART_DRAW_NUM || part->draw & PART_DRAW_HEALTH) && (v3d->flag2 & V3D_RENDER_OVERRIDE)==0){
                                        float vec_txt[3];
-                                       char *val_pos= val;
-                                       val[0]= '\0';
+                                       char *val_pos= numstr;
+                                       numstr[0]= '\0';
 
                                        if(part->draw&PART_DRAW_NUM) {
                                                if(a < totpart && (part->draw & PART_DRAW_HEALTH) && (part->phystype==PART_PHYS_BOIDS)) {
@@ -4414,7 +4450,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                                        /* in path drawing state.co is the end point */
                                        /* use worldspace beause object matrix is already applied */
                                        mul_v3_m4v3(vec_txt, ob->imat, state.co);
-                                       view3d_cached_text_draw_add(vec_txt, val, 10, V3D_CACHE_TEXT_WORLDSPACE|V3D_CACHE_TEXT_ASCII, tcol);
+                                       view3d_cached_text_draw_add(vec_txt, numstr, 10, V3D_CACHE_TEXT_WORLDSPACE|V3D_CACHE_TEXT_ASCII, tcol);
                                }
                        }
                }
@@ -4503,10 +4539,10 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
                        for(a=0, pa=psys->particles; a<totpart; a++, pa++){
                                float vec_txt[3];
-                               sprintf(val, "%i", a);
+                               BLI_snprintf(numstr, sizeof(numstr), "%i", a);
                                /* use worldspace beause object matrix is already applied */
                                mul_v3_m4v3(vec_txt, ob->imat, cache[a]->co);
-                               view3d_cached_text_draw_add(vec_txt, val, 10, V3D_CACHE_TEXT_WORLDSPACE|V3D_CACHE_TEXT_ASCII, tcol);
+                               view3d_cached_text_draw_add(vec_txt, numstr, 10, V3D_CACHE_TEXT_WORLDSPACE|V3D_CACHE_TEXT_ASCII, tcol);
                        }
                }
        }
@@ -6843,7 +6879,34 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                ListBase targets = {NULL, NULL};
                                bConstraintTarget *ct;
                                
-                               if ((curcon->flag & CONSTRAINT_EXPAND) && (cti) && (cti->get_constraint_targets)) {
+                               if(ELEM(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_OBJECTSOLVER)) {
+                                       /* special case for object solver and follow track constraints because they don't fill
+                                          constraint targets properly (design limitation -- scene is needed for their target
+                                          but it can't be accessed from get_targets callvack) */
+
+                                       Object *camob= NULL;
+
+                                       if(cti->type==CONSTRAINT_TYPE_FOLLOWTRACK) {
+                                               bFollowTrackConstraint *data= (bFollowTrackConstraint *)curcon->data;
+
+                                               camob= data->camera ? data->camera : scene->camera;
+                                       }
+                                       else if(cti->type==CONSTRAINT_TYPE_OBJECTSOLVER) {
+                                               bObjectSolverConstraint *data= (bObjectSolverConstraint *)curcon->data;
+
+                                               camob= data->camera ? data->camera : scene->camera;
+                                       }
+
+                                       if(camob) {
+                                               setlinestyle(3);
+                                               glBegin(GL_LINES);
+                                               glVertex3fv(camob->obmat[3]);
+                                               glVertex3fv(ob->obmat[3]);
+                                               glEnd();
+                                               setlinestyle(0);
+                                       }
+                               }
+                               else if ((curcon->flag & CONSTRAINT_EXPAND) && (cti) && (cti->get_constraint_targets)) {
                                        cti->get_constraint_targets(curcon, &targets);
                                        
                                        for (ct= targets.first; ct; ct= ct->next) {