svn merge -r36603:36628 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
index 90cfdda78a872ae7fda039f6c40ef7ea51de2282..cc3ee805a8b938db3b46cfbedece7a966cf1d2c8 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"
 
@@ -196,7 +197,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 */
@@ -262,31 +263,90 @@ 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);
        //else
 
        memset(me->mcol, 255, 4*sizeof(MCol)*me->totface);
-       
+
        DAG_id_tag_update(&me->id, 0);
        
 }
 
-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);
@@ -294,10 +354,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);
        
 }
 
@@ -320,9 +380,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;
@@ -342,6 +404,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);
 }
@@ -427,7 +501,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, faceverts[i]);
+                                                       int j= -1; //BMESH_TODO mesh_get_x_mirror_vert(ob, faceverts[i]);
                                                        if(j>=0) {
                                                                /* copy, not paint again */
                                                                if(vgroup_mirror != -1) {
@@ -861,13 +935,14 @@ static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float
 /* else */
 /*     sets wp->weight to the closest weight value to vertex */
 /*     note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
+#if 0
 static void sample_wpaint(Scene *scene, ARegion *ar, View3D *UNUSED(v3d), int mode)
 {
        ViewContext vc;
        ToolSettings *ts= scene->toolsettings;
        Object *ob= OBACT;
        Mesh *me= get_mesh(ob);
-       int index;
+       int index, i;
        short mval[2] = {0, 0}, sco[2];
        int vgroup= ob->actdef-1;
 
@@ -876,13 +951,13 @@ static void sample_wpaint(Scene *scene, ARegion *ar, View3D *UNUSED(v3d), int mo
 //     getmouseco_areawin(mval);
        index= view3d_sample_backbuf(&vc, mval[0], mval[1]);
        
-       if(index && index<=me->totface) {
-               MFace *mface;
-               
-               mface= ((MFace *)me->mface) + index-1;
+       if(index && index<=me->totpoly) {
+               MPoly *mpoly;
                
+               mpoly= ((MPoly *)me->mpoly) + index-1;          
                if(mode==1) {   /* sampe which groups are in here */
                        MDeformVert *dv;
+                       MLoop *ml;
                        int a, totgroup;
                        
                        totgroup= BLI_countlist(&ob->defbase);
@@ -890,28 +965,14 @@ static void sample_wpaint(Scene *scene, ARegion *ar, View3D *UNUSED(v3d), int mo
                                int totmenu=0;
                                int *groups=MEM_callocN(totgroup*sizeof(int), "groups");
                                
-                               dv= me->dvert+mface->v1;
-                               for(a=0; a<dv->totweight; a++) {
-                                       if (dv->dw[a].def_nr<totgroup)
-                                               groups[dv->dw[a].def_nr]= 1;
-                               }
-                               dv= me->dvert+mface->v2;
-                               for(a=0; a<dv->totweight; a++) {
-                                       if (dv->dw[a].def_nr<totgroup)
-                                               groups[dv->dw[a].def_nr]= 1;
-                               }
-                               dv= me->dvert+mface->v3;
-                               for(a=0; a<dv->totweight; a++) {
+                               ml = me->mloop+mpoly->loopstart;
+                               for (i=0; i<mpoly->totloop; i++, ml++) {
+                                       dv = me->dvert+ml->v;
                                        if (dv->dw[a].def_nr<totgroup)
                                                groups[dv->dw[a].def_nr]= 1;
+                                       
                                }
-                               if(mface->v4) {
-                                       dv= me->dvert+mface->v4;
-                                       for(a=0; a<dv->totweight; a++) {
-                                               if (dv->dw[a].def_nr<totgroup)
-                                                       groups[dv->dw[a].def_nr]= 1;
-                                       }
-                               }
+
                                for(a=0; a<totgroup; a++)
                                        if(groups[a]) totmenu++;
                                
@@ -944,28 +1005,19 @@ static void sample_wpaint(Scene *scene, ARegion *ar, View3D *UNUSED(v3d), int mo
                }
                else {
                        DerivedMesh *dm;
-                       float w1, w2, w3, w4, co[3], fac;
+                       MLoop *ml;
+                       float w, co[3], fmin = 10000000.0f;
+                       int loopi = mpoly->loopstart;
                        
                        dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
                        if(dm->getVertCo==NULL) {
                                //notice("Not supported yet");
                        }
                        else {
-                               /* calc 3 or 4 corner weights */
-                               dm->getVertCo(dm, mface->v1, co);
-                               project_short_noclip(ar, co, sco);
-                               w1= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
-                               
-                               dm->getVertCo(dm, mface->v2, co);
-                               project_short_noclip(ar, co, sco);
-                               w2= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
-                               
-                               dm->getVertCo(dm, mface->v3, co);
-                               project_short_noclip(ar, co, sco);
-                               w3= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
-                               
-                               if(mface->v4) {
-                                       dm->getVertCo(dm, mface->v4, co);
+                               /* find minimum distance */
+                               ml = me->mloop + mpoly->loopstart;
+                               for (i=0; i<mpoly->totloop; i++, ml++) {
+                                       dm->getVertCo(dm, ml->v, co);
                                        project_short_noclip(ar, co, sco);
                                        w4= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
                                }
@@ -984,8 +1036,13 @@ static void sample_wpaint(Scene *scene, ARegion *ar, View3D *UNUSED(v3d), int mo
                                else if(w4==fac) {
                                        if(mface->v4) {
                                                ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v4, vgroup);
+>>>>>>> .merge-right.r23146
                                        }
                                }
+
+                               ml = me->mloop + loopi;
+                               dw= get_defweight(me->dvert+ml->v, ob->actdef-1);
+                               if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
                        }
                        dm->release(dm);
                }               
@@ -993,6 +1050,7 @@ static void sample_wpaint(Scene *scene, ARegion *ar, View3D *UNUSED(v3d), int mo
        }
        
 }
+#endif
 
 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
                                           int paint_nr, char *map)
@@ -1052,7 +1110,7 @@ static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index,
        do_weight_paint_auto_normalize(me->dvert+index, vgroup, validmap);
 
        if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
-               int j= mesh_get_x_mirror_vert(ob, index);
+               int j= -1; //BMESH_TODO mesh_get_x_mirror_vert(ob, index);
                if(j>=0) {
                        /* copy, not paint again */
                        if(vgroup_mirror != -1)
@@ -1100,7 +1158,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);
@@ -1112,8 +1170,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);
@@ -1235,6 +1293,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);
@@ -1378,22 +1443,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;
                                }
                        }                                       
@@ -1409,13 +1474,14 @@ 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);
-                                       
-                       (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;
+               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++) {
+                               (me->dvert+ml->v)->flag = 1;
+                       }
                                        
                        if(brush->vertexpaint_tool==VP_BLUR) {
                                MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
@@ -1425,15 +1491,13 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                                else
                                        dw_func= defvert_verify_index;
                                                
-                               dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
-                               if(dw) {paintweight+= dw->weight; totw++;}
-                               dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
-                               if(dw) {paintweight+= dw->weight; totw++;}
-                               dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
-                               if(dw) {paintweight+= dw->weight; totw++;}
-                               if(mface->v4) {
-                                       dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
-                                       if(dw) {paintweight+= dw->weight; totw++;}
+                               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++;
+                                       }                                       
                                }
                        }
                }
@@ -1443,49 +1507,20 @@ 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 *mface= me->mface + (indexar[index]-1);
-                                       
-                       if((me->dvert+mface->v1)->flag) {
-                               alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
-                               if(alpha) {
-                                       do_weight_paint_vertex(wp, ob, mface->v1, 
-                                               alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap);
-                               }
-                               (me->dvert+mface->v1)->flag= 0;
-                       }
-                                       
-                       if((me->dvert+mface->v2)->flag) {
-                               alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
-                               if(alpha) {
-                                       do_weight_paint_vertex(wp, ob, mface->v2, 
-                                               alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap);
-                               }
-                               (me->dvert+mface->v2)->flag= 0;
-                       }
-                                       
-                       if((me->dvert+mface->v3)->flag) {
-                               alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
-                               if(alpha) {
-                                       do_weight_paint_vertex(wp, ob, mface->v3, 
-                                               alpha, paintweight, flip, wpd->vgroup_mirror, 
-                                               wpd->vgroup_validmap);
-                               }
-                               (me->dvert+mface->v3)->flag= 0;
-                       }
-                                       
-                       if((me->dvert+mface->v4)->flag) {
-                               if(mface->v4) {
-                                       alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
-                                       if(alpha) {
-                                               do_weight_paint_vertex(wp, ob, mface->v4, 
-                                                       alpha, paintweight, flip, wpd->vgroup_mirror,
+               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++) {
+                               if ((me->dvert+ml->v)->flag) {
+                                       alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*ml->v, mval, pressure);
+                                       if(alpha != 0.0f) {
+                                               do_weight_paint_vertex(wp, ob, ml->v, 
+                                                       alpha, paintweight, flip, wpd->vgroup_mirror, 
                                                        wpd->vgroup_validmap);
                                        }
-                                       (me->dvert+mface->v4)->flag= 0;
+                                       (me->dvert+ml->v)->flag= 0;
                                }
                        }
                }
@@ -1611,7 +1646,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) {
@@ -1679,14 +1714,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);
@@ -1694,15 +1775,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");
@@ -1712,9 +1797,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);
@@ -1724,6 +1810,7 @@ static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent
        return 1;
 }
 
+#if 0
 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index, float mval[2], float pressure, int UNUSED(flip))
 {
        ViewContext *vc = &vpd->vc;
@@ -1757,6 +1844,7 @@ static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index
                        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)
 {
@@ -1766,6 +1854,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;
@@ -1787,7 +1876,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]);
@@ -1797,19 +1886,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);
 }
 
@@ -1824,7 +2008,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);
 }