svn merge ^/trunk/blender -r40720:40872
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
index cf90c43f3e11c79a3e2519ea89ba8d0ef4e6d2f6..ecab180822b084491b74284d539919fc84833bd4 100644 (file)
@@ -45,6 +45,7 @@
 
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
+#include "BLI_memarena.h"
 #include "BLI_utildefines.h"
 #include "BLI_ghash.h"
 
@@ -199,7 +200,7 @@ unsigned int vpaint_get_current_col(VPaint *vp)
        return rgba_to_mcol(brush->rgb[0], brush->rgb[1], brush->rgb[2], 1.0f);
 }
 
-static void do_shared_vertexcol(Mesh *me)
+static void do_shared_vertex_tesscol(Mesh *me)
 {
        /* if no mcol: do not do */
        /* if tface: only the involved faces, otherwise all */
@@ -265,19 +266,78 @@ static void do_shared_vertexcol(Mesh *me)
        MEM_freeN(scolmain);
 }
 
+void do_shared_vertexcol(Mesh *me)
+{
+       MLoop *ml = me->mloop;
+       MLoopCol *lcol = me->mloopcol;
+       MTexPoly *mtp = me->mtpoly;
+       MPoly *mp = me->mpoly;
+       float (*scol)[5];
+       int i;
+
+       /* if no mloopcol: do not do */
+       /* if mtexpoly: only the involved faces, otherwise all */
+
+       if(me->mloopcol==0 || me->totvert==0 || me->totpoly==0) return;
+
+       scol = MEM_callocN(sizeof(float)*me->totvert*5, "scol");
+
+       for (i=0; i<me->totloop; i++, ml++, lcol++) {
+               if (i >= mp->loopstart + mp->totloop) {
+                       mp++;
+                       if (mtp) mtp++;
+               }
+
+               if (mtp && !(mtp->mode & TF_SHAREDCOL))
+                       continue;
+
+               scol[ml->v][0] += lcol->r;
+               scol[ml->v][1] += lcol->g;
+               scol[ml->v][2] += lcol->b;
+               scol[ml->v][3] += lcol->a;
+               scol[ml->v][4] += 1.0;
+       }
+       
+       for (i=0; i<me->totvert; i++) {
+               if (!scol[i][4]) continue;
+
+               scol[i][0] /= scol[i][4];
+               scol[i][1] /= scol[i][4];
+               scol[i][2] /= scol[i][4];
+               scol[i][3] /= scol[i][4];
+       }
+       
+       ml = me->mloop;
+       lcol = me->mloopcol;
+       for (i=0; i<me->totloop; i++, ml++, lcol++) {
+               if (!scol[ml->v][4]) continue;
+
+               lcol->r = scol[ml->v][0];
+               lcol->g = scol[ml->v][1];
+               lcol->b = scol[ml->v][2];
+               lcol->a = scol[ml->v][3];
+       }
+
+       MEM_freeN(scol);
+
+       do_shared_vertex_tesscol(me);
+}
+
 static void make_vertexcol(Object *ob) /* single ob */
 {
        Mesh *me;
        if(!ob || ob->id.lib) return;
        me= get_mesh(ob);
        if(me==NULL) return;
-       if(me->edit_mesh) return;
+       if(me->edit_btmesh) return;
 
        /* copies from shadedisplist to mcol */
-       if(!me->mcol) {
-               CustomData_add_layer(&me->fdata, CD_MCOL, CD_CALLOC, NULL, me->totface);
-               mesh_update_customdata_pointers(me);
-       }
+       if(!me->mcol)
+               CustomData_add_layer(&me->fdata, CD_MCOL, CD_DEFAULT, NULL, me->totface);
+       if (!me->mloopcol)
+               CustomData_add_layer(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop);
+       
+       mesh_update_customdata_pointers(me);
 
        //if(shade)
        //      shadeMeshMCol(scene, ob, me);
@@ -325,7 +385,7 @@ static int wpaint_mirror_vgroup_ensure(Object *ob)
        return -1;
 }
 
-static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
+static void copy_vpaint_prev(VPaint *vp, unsigned int *lcol, int tot)
 {
        if(vp->vpaint_prev) {
                MEM_freeN(vp->vpaint_prev);
@@ -333,10 +393,10 @@ static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
        }
        vp->tot= tot;   
        
-       if(mcol==NULL || tot==0) return;
+       if(lcol==NULL || tot==0) return;
        
-       vp->vpaint_prev= MEM_mallocN(4*sizeof(int)*tot, "vpaint_prev");
-       memcpy(vp->vpaint_prev, mcol, 4*sizeof(int)*tot);
+       vp->vpaint_prev= MEM_mallocN(sizeof(int)*tot, "vpaint_prev");
+       memcpy(vp->vpaint_prev, lcol, sizeof(int)*tot);
        
 }
 
@@ -359,9 +419,11 @@ static void copy_wpaint_prev (VPaint *wp, MDeformVert *dverts, int dcount)
 void vpaint_fill(Object *ob, unsigned int paintcol)
 {
        Mesh *me;
-       MFace *mf;
+       MFace *mf;
+       MPoly *mp;
+       MLoopCol *lcol;
        unsigned int *mcol;
-       int i, selected;
+       int i, j, selected;
 
        me= get_mesh(ob);
        if(me==NULL || me->totface==0) return;
@@ -381,6 +443,18 @@ void vpaint_fill(Object *ob, unsigned int paintcol)
                        mcol[3] = paintcol;
                }
        }
+
+       mp = me->mpoly;
+       lcol = me->mloopcol;
+       for (i=0; i<me->totpoly; i++, mp++) {
+               if (!(!selected || mp->flag & ME_FACE_SEL))
+                       continue;
+
+               lcol = me->mloopcol + mp->loopstart;
+               for (j=0; j<mp->totloop; j++, lcol++) {
+                       *(int*)lcol = paintcol;
+               }
+       }
        
        DAG_id_tag_update(&me->id, 0);
 }
@@ -390,6 +464,7 @@ void vpaint_fill(Object *ob, unsigned int paintcol)
 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
 {
        Mesh *me;
+       MFace *mface;
        MDeformWeight *dw, *uw;
        int *indexar;
        unsigned int index;
@@ -408,9 +483,8 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
        indexar= get_indexarray(me);
 
        if(selected) {
-               MFace *mf;
-               for(index=0, mf= me->mface; index<me->totface; index++, mf++) {
-                       if((mf->flag & ME_FACE_SEL)==0)
+               for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
+                       if((mface->flag & ME_FACE_SEL)==0)
                                indexar[index]= 0;
                        else
                                indexar[index]= index+1;
@@ -450,7 +524,7 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
                                                dw->weight= paintweight;
 
                                                if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
-                                                       int j= mesh_get_x_mirror_vert(ob, vidx);
+                                                       int j= -1; //BMESH_TODO mesh_get_x_mirror_vert(ob, faceverts[i]);
                                                        if(j>=0) {
                                                                /* copy, not paint again */
                                                                if(vgroup_mirror != -1) {
@@ -889,6 +963,7 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
 
 /* sets wp->weight to the closest weight value to vertex */
 /* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
+#if 0// BMESH_TODO
 static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        ViewContext vc;
@@ -955,6 +1030,7 @@ static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
                return OPERATOR_CANCELLED;
        }
 }
+#endif
 
 void PAINT_OT_weight_sample(wmOperatorType *ot)
 {
@@ -963,7 +1039,9 @@ void PAINT_OT_weight_sample(wmOperatorType *ot)
        ot->idname= "PAINT_OT_weight_sample";
 
        /* api callbacks */
+#if 0 // BMESH_TODO
        ot->invoke= weight_sample_invoke;
+#endif
        ot->poll= weight_paint_mode_poll;
 
        /* flags */
@@ -971,6 +1049,7 @@ void PAINT_OT_weight_sample(wmOperatorType *ot)
 }
 
 /* samples cursor location, and gives menu with vertex groups to activate */
+#if 0 // BMESH_TODO
 static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
 {
        if (C) {
@@ -1037,6 +1116,7 @@ static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA
 
        return DummyRNA_NULL_items;
 }
+#endif
 
 static int weight_sample_group_exec(bContext *C, wmOperator *op)
 {
@@ -1070,12 +1150,13 @@ void PAINT_OT_weight_sample_group(wmOperatorType *ot)
 
        /* keyingset to use (dynamic enum) */
        prop= RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
+#if 0 // BMESH_TODO
        RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf);
+#endif
        ot->prop= prop;
 }
 
 
-#if 0 /* UNUSED */
 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
                                           int paint_nr, char *map)
 {
@@ -1110,7 +1191,6 @@ static void do_weight_paint_auto_normalize(MDeformVert *dvert,
                }
        }
 }
-#endif
 
 /* the active group should be involved in auto normalize */
 static void do_weight_paint_auto_normalize_all_groups(MDeformVert *dvert, const char *vgroup_validmap, char do_auto_normalize)
@@ -1748,7 +1828,7 @@ static int set_wpaint(bContext *C, wmOperator *UNUSED(op))                /* toggle */
                paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
                paint_cursor_start(C, weight_paint_poll);
                
-               mesh_octree_table(ob, NULL, NULL, 's');
+               //BMESH_TODO mesh_octree_table(ob, NULL, NULL, 's');
                
                /* verify if active weight group is also active bone */
                par= modifiers_isDeformedByArmature(ob);
@@ -1760,8 +1840,8 @@ static int set_wpaint(bContext *C, wmOperator *UNUSED(op))                /* toggle */
                }
        }
        else {
-               mesh_octree_table(NULL, NULL, NULL, 'e');
-               mesh_mirrtopo_table(NULL, 'e');
+               //BMESH_TODO mesh_octree_table(NULL, NULL, NULL, 'e');
+               //BMESH_TODO mesh_mirrtopo_table(NULL, 'e');
        }
        
        WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
@@ -1882,6 +1962,13 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED
        me= get_mesh(ob);
        if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
        
+       /*we can't assume mfaces have a correct origindex layer that indices to mpolys.
+         so instead we have to regenerate the tesselation faces altogether.*/
+       me->totface = mesh_recalcTesselation(&me->fdata, &me->ldata, &me->pdata, 
+               me->mvert, me->totface, me->totloop, me->totpoly, 1, 0);
+       mesh_update_customdata_pointers(me);
+       makeDerivedMesh(scene, ob, NULL, CD_MASK_BAREMESH, 0);
+
        /* if nothing was added yet, we make dverts and a vertex deform group */
        if (!me->dvert) {
                ED_vgroup_data_create(&me->id);
@@ -2024,22 +2111,22 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                        
        if(wp->flag & VP_COLINDEX) {
                for(index=0; index<totindex; index++) {
-                       if(indexar[index] && indexar[index]<=me->totface) {
-                               MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
+                       if(indexar[index] && indexar[index]<=me->totpoly) {
+                               MPoly *mpoly= ((MPoly *)me->mpoly) + (indexar[index]-1);
                                                
-                               if(mface->mat_nr!=ob->actcol-1) {
+                               if(mpoly->mat_nr!=ob->actcol-1) {
                                        indexar[index]= 0;
                                }
                        }
                }
        }
                        
-       if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
+       if((me->editflag & ME_EDIT_PAINT_MASK) && me->mpoly) {
                for(index=0; index<totindex; index++) {
-                       if(indexar[index] && indexar[index]<=me->totface) {
-                               MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
+                       if(indexar[index] && indexar[index]<=me->totpoly) {
+                               MPoly *mpoly= ((MPoly *)me->mpoly) + (indexar[index]-1);
                                                
-                               if((mface->flag & ME_FACE_SEL)==0) {
+                               if((mpoly->flag & ME_FACE_SEL)==0) {
                                        indexar[index]= 0;
                                }
                        }                                       
@@ -2055,42 +2142,38 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                paintweight= ts->vgroup_weight;
                        
        for(index=0; index<totindex; index++) {
-               if(indexar[index] && indexar[index]<=me->totface) {
-                       MFace *mface= me->mface + (indexar[index]-1);
+               if(indexar[index] && indexar[index]<=me->totpoly) {
+                       MPoly *mpoly= me->mpoly + (indexar[index]-1);
+                       MLoop *ml = me->mloop + mpoly->loopstart;
+                       int i;
 
                        if(use_vert_sel) {
-                               me->dvert[mface->v1].flag = (me->mvert[mface->v1].flag & SELECT);
-                               me->dvert[mface->v2].flag = (me->mvert[mface->v2].flag & SELECT);
-                               me->dvert[mface->v3].flag = (me->mvert[mface->v3].flag & SELECT);
-                               if(mface->v4) me->dvert[mface->v4].flag = (me->mvert[mface->v4].flag & SELECT);
+                               for (i=0; i<mpoly->totloop; i++, ml++) {
+                                       me->dvert[ml->v].flag = (me->mvert[ml->v].flag & SELECT);
+                               }
                        }
                        else {
-                               me->dvert[mface->v1].flag= 1;
-                               me->dvert[mface->v2].flag= 1;
-                               me->dvert[mface->v3].flag= 1;
-                               if(mface->v4) me->dvert[mface->v4].flag= 1;
+                               for (i=0; i<mpoly->totloop; i++, ml++) {
+                                       me->dvert[ml->v].flag = 1;
+                               }
                        }
                                        
                        if(brush->vertexpaint_tool==VP_BLUR) {
                                MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
-                               unsigned int fidx= mface->v4 ? 3:2;
                                                
                                if(wp->flag & VP_ONLYVGROUP)
                                        dw_func= (MDeformWeight *(*)(MDeformVert *, const int))defvert_find_index;
                                else
                                        dw_func= defvert_verify_index;
-
-                               do {
-                                       unsigned int vidx= *(&mface->v1 + fidx);
-
-                                       dw= dw_func(me->dvert+vidx, ob->actdef-1);
-                                       if(dw) {
-                                               paintweight+= dw->weight;
+                                               
+                               ml = me->mloop + mpoly->loopstart;
+                               for (i=0; i<mpoly->totloop; i++, ml++) {
+                                       dw = dw_func(me->dvert+ml->v, ob->actdef-1);
+                                       if (dw) {
+                                               paintweight += dw->weight;
                                                totw++;
                                        }
-
-                               } while (fidx--);
-
+                               }
                        }
                }
        }
@@ -2099,21 +2182,21 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                paintweight/= (float)totw;
                        
        for(index=0; index<totindex; index++) {
-                               
-               if(indexar[index] && indexar[index]<=me->totface) {
-                       MFace *mf= me->mface + (indexar[index]-1);
-                       unsigned int fidx= mf->v4 ? 3:2;
-                       do {
-                               unsigned int vidx= *(&mf->v1 + fidx);
-
-                               if(me->dvert[vidx].flag) {
+               if(indexar[index] && indexar[index]<=me->totpoly) {
+                       MPoly *mpoly= me->mpoly + (indexar[index]-1);
+                       MLoop *ml=me->mloop+mpoly->loopstart;
+                       int i;
+
+                       for (i=0; i<mpoly->totloop; i++, ml++) {
+                               unsigned int vidx= ml->v;
+                               if (me->dvert[vidx].flag) {
                                        alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*vidx, mval, pressure);
                                        if(alpha) {
                                                do_weight_paint_vertex(wp, ob, &wpi, vidx, alpha, paintweight);
                                        }
                                        me->dvert[vidx].flag= 0;
                                }
-                       } while (fidx--);
+                       }
                }
        }
 
@@ -2253,7 +2336,7 @@ static int set_vpaint(bContext *C, wmOperator *op)                /* toggle */
                return OPERATOR_PASS_THROUGH;
        }
        
-       if(me && me->mcol==NULL) make_vertexcol(ob);
+       if(me && me->mloopcol==NULL) make_vertexcol(ob);
        
        /* toggle: end vpaint */
        if(ob->mode & OB_MODE_VERTEX_PAINT) {
@@ -2321,14 +2404,60 @@ For future:
 
 */
 
+typedef struct polyfacemap_e {
+       struct polyfacemap_e *next, *prev;
+       int facenr;
+} polyfacemap_e;
+
 typedef struct VPaintData {
        ViewContext vc;
        unsigned int paintcol;
        int *indexar;
        float *vertexcosnos;
        float vpimat[3][3];
+       
+       /*mpoly -> mface mapping*/
+       MemArena *arena;
+       ListBase *polyfacemap;
 } VPaintData;
 
+static void vpaint_build_poly_facemap(struct VPaintData *vd, Mesh *me,
+                                     Object *ob, Scene *scene)
+{
+       MFace *mf;
+       polyfacemap_e *e;
+       int *origIndex;
+       int i;
+
+       vd->arena = BLI_memarena_new(1<<13, "vpaint tmp");
+       BLI_memarena_use_calloc(vd->arena);
+
+       vd->polyfacemap = BLI_memarena_alloc(vd->arena, sizeof(ListBase)*me->totpoly);
+
+       /*we can't assume mfaces have a correct origindex layer that indices to mpolys.
+         so instead we have to regenerate the tesselation faces altogether.*/
+       me->totface = mesh_recalcTesselation(&me->fdata, &me->ldata, &me->pdata, 
+               me->mvert, me->totface, me->totloop, me->totpoly, 1, 0);
+       mesh_update_customdata_pointers(me);
+       makeDerivedMesh(scene, ob, NULL, CD_MASK_BAREMESH, 0);
+
+       origIndex = CustomData_get_layer(&me->fdata, CD_ORIGINDEX);
+       mf = me->mface;
+
+       if (!origIndex)
+               return;
+
+       for (i=0; i<me->totface; i++, mf++, origIndex++) {
+               if (*origIndex == ORIGINDEX_NONE)
+                       continue;
+
+               e = BLI_memarena_alloc(vd->arena, sizeof(polyfacemap_e));
+               e->facenr = i;
+               
+               BLI_addtail(&vd->polyfacemap[*origIndex], e);
+       }
+}
+
 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *UNUSED(event))
 {
        ToolSettings *ts= CTX_data_tool_settings(C);
@@ -2336,15 +2465,19 @@ static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent
        VPaint *vp= ts->vpaint;
        struct VPaintData *vpd;
        Object *ob= CTX_data_active_object(C);
+       Scene *scene = CTX_data_scene(C);
        Mesh *me;
        float mat[4][4], imat[4][4];
 
        /* context checks could be a poll() */
        me= get_mesh(ob);
-       if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
+       if(me==NULL || me->totpoly==0)
+               return OPERATOR_PASS_THROUGH;
        
-       if(me->mcol==NULL) make_vertexcol(ob);
-       if(me->mcol==NULL) return OPERATOR_CANCELLED;
+       if(me->mloopcol==NULL)
+               make_vertexcol(ob);
+       if(me->mloopcol==NULL)
+               return OPERATOR_CANCELLED;
        
        /* make mode data storage */
        vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
@@ -2354,9 +2487,10 @@ static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent
        vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
        vpd->indexar= get_indexarray(me);
        vpd->paintcol= vpaint_get_current_col(vp);
+       vpaint_build_poly_facemap(vpd, me, ob, scene);
        
        /* for filtering */
-       copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
+       copy_vpaint_prev(vp, (unsigned int *)me->mloopcol, me->totloop);
        
        /* some old cruft to sort out later */
        mul_m4_m4m4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
@@ -2366,7 +2500,8 @@ static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent
        return 1;
 }
 
-static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, const unsigned int index, const float mval[2], float pressure, int UNUSED(flip))
+#if 0
+static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index, const float mval[2], float pressure, int UNUSED(flip))
 {
        ViewContext *vc = &vpd->vc;
        Brush *brush = paint_brush(&vp->paint);
@@ -2399,6 +2534,7 @@ static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, const uns
                        vpaint_blend(vp, mcol+i, mcolorig+i, vpd->paintcol, (int)(alpha*255.0f));
        }
 }
+#endif
 
 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
 {
@@ -2408,6 +2544,7 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
        Brush *brush = paint_brush(&vp->paint);
        ViewContext *vc= &vpd->vc;
        Object *ob= vc->obact;
+       polyfacemap_e *e;
        Mesh *me= ob->data;
        float mat[4][4];
        int *indexar= vpd->indexar;
@@ -2429,7 +2566,7 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                        
        /* which faces are involved */
        if(vp->flag & VP_AREA) {
-               totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
+               totindex= sample_backbuf_area(vc, indexar, me->totpoly, mval[0], mval[1], brush_size(brush));
        }
        else {
                indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
@@ -2439,19 +2576,114 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                        
        swap_m4m4(vc->rv3d->persmat, mat);
                        
-       for(index=0; index<totindex; index++) {                         
-               if(indexar[index] && indexar[index]<=me->totface)
-                       vpaint_paint_face(vp, vpd, ob, indexar[index]-1, mval, pressure, flip);
+       if(vp->flag & VP_COLINDEX) {
+               for(index=0; index<totindex; index++) {
+                       if(indexar[index] && indexar[index]<=me->totpoly) {
+                               MPoly *mpoly= ((MPoly *)me->mpoly) + (indexar[index]-1);
+                                               
+                               if(mpoly->mat_nr!=ob->actcol-1) {
+                                       indexar[index]= 0;
+                               }
+                       }                                       
+               }
        }
+
+       if((me->editflag & ME_EDIT_PAINT_MASK) && me->mpoly) {
+               for(index=0; index<totindex; index++) {
+                       if(indexar[index] && indexar[index]<=me->totpoly) {
+                               MPoly *mpoly= ((MPoly *)me->mpoly) + (indexar[index]-1);
                                                
+                               if((mpoly->flag & ME_FACE_SEL)==0)
+                                       indexar[index]= 0;
+                       }                                       
+               }
+       }
+       
        swap_m4m4(vc->rv3d->persmat, mat);
 
        /* was disabled because it is slow, but necessary for blur */
        if(brush->vertexpaint_tool == VP_BLUR)
                do_shared_vertexcol(me);
                        
-       ED_region_tag_redraw(vc->ar);
+       for(index=0; index<totindex; index++) {
+                               
+               if(indexar[index] && indexar[index]<=me->totpoly) {
+                       MPoly *mpoly= ((MPoly *)me->mpoly) + (indexar[index]-1);
+                       MFace *mf;
+                       MCol *mc;
+                       MLoop *ml;
+                       MLoopCol *mlc;
+                       unsigned int *lcol = ((unsigned int*)me->mloopcol) + mpoly->loopstart;
+                       unsigned int *lcolorig = ((unsigned int*)vp->vpaint_prev) + mpoly->loopstart;
+                       float alpha;
+                       int i, j;
+                                       
+                       if(brush->vertexpaint_tool==VP_BLUR) {
+                               unsigned int blend[5] = {0};
+                               char *col;
+
+                               for (j=0; j<mpoly->totloop; j += 2) {
+                                       col = (char*)(lcol + j);
+                                       blend[0] += col[0];
+                                       blend[1] += col[1];
+                                       blend[2] += col[2];
+                                       blend[3] += col[3];
+                               }
+
+                               blend[0] /= mpoly->totloop;
+                               blend[1] /= mpoly->totloop;
+                               blend[2] /= mpoly->totloop;
+                               blend[3] /= mpoly->totloop;
+                               col = (char*)(blend + 4);
+                               col[0] = blend[0];
+                               col[1] = blend[1];
+                               col[2] = blend[2];
+                               col[3] = blend[3];
+
+                               vpd->paintcol = *((unsigned int*)col);
+                       }
+
+                       ml = me->mloop + mpoly->loopstart;
+                       for (i=0; i<mpoly->totloop; i++, ml++) {
+                               alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, 
+                                          vpd->vertexcosnos+6*ml->v, mval, pressure);
+                               if(alpha > 0.0f) vpaint_blend(vp, lcol+i, lcolorig+i, vpd->paintcol, (int)(alpha*255.0f));
+                       }
+       
+                       #ifdef CPYCOL
+                       #undef CPYCOL
+                       #endif
+                       #define CPYCOL(c, l) (c)->a = (l)->a, (c)->r = (l)->r, (c)->g = (l)->g, (c)->b = (l)->b
+
+                       /*update vertex colors for tesselations incrementally,
+                         rather then regenerating the tesselation altogether.*/
+                       for (e=vpd->polyfacemap[(indexar[index]-1)].first; e; e=e->next) {
+                               mf = me->mface + e->facenr;
+                               mc = me->mcol + e->facenr*4;
+                               
+                               ml = me->mloop + mpoly->loopstart;
+                               mlc = me->mloopcol + mpoly->loopstart;
+                               for (j=0; j<mpoly->totloop; j++, ml++, mlc++) {
+                                       if (ml->v == mf->v1)
+                                               CPYCOL(mc, mlc);
+                                       else if (ml->v == mf->v2)
+                                               CPYCOL(mc+1, mlc);
+                                       else if (ml->v == mf->v3)
+                                               CPYCOL(mc+2, mlc);
+                                       else if (mf->v4 && ml->v == mf->v4)
+                                               CPYCOL(mc+3, mlc);
+
+                               }
+                       }
+                       #undef CPYCOL
+               }
+       }
+               
+       swap_m4m4(vc->rv3d->persmat, mat);
                        
+       do_shared_vertexcol(me);
+
+       ED_region_tag_redraw(vc->ar);           
        DAG_id_tag_update(ob->data, 0);
 }
 
@@ -2466,7 +2698,8 @@ static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
        
        /* frees prev buffer */
        copy_vpaint_prev(ts->vpaint, NULL, 0);
-       
+       BLI_memarena_free(vpd->arena);
+
        MEM_freeN(vpd);
 }