svn merge ^/trunk/blender -r43294:43338
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
index 68712983003952dd4f3982551ce932c6d98d186c..a64326d8d987941513a320269314d08c3630348f 100644 (file)
 
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
+#include "BLI_memarena.h"
 #include "BLI_utildefines.h"
 #include "BLI_ghash.h"
+#include "BLI_cellalloc.h"
 
 #include "IMB_imbuf.h"
 #include "IMB_imbuf_types.h"
@@ -102,7 +104,7 @@ int vertex_paint_mode_poll(bContext *C)
 {
        Object *ob = CTX_data_active_object(C);
 
-       return ob && ob->mode == OB_MODE_VERTEX_PAINT && ((Mesh *)ob->data)->totface;
+       return ob && ob->mode == OB_MODE_VERTEX_PAINT && ((Mesh *)ob->data)->totpoly;
 }
 
 int vertex_paint_poll(bContext *C)
@@ -123,7 +125,7 @@ int weight_paint_mode_poll(bContext *C)
 {
        Object *ob = CTX_data_active_object(C);
 
-       return ob && ob->mode == OB_MODE_WEIGHT_PAINT && ((Mesh *)ob->data)->totface;
+       return ob && ob->mode == OB_MODE_WEIGHT_PAINT && ((Mesh *)ob->data)->totpoly;
 }
 
 int weight_paint_poll(bContext *C)
@@ -159,7 +161,7 @@ static VPaint *new_vpaint(int wpaint)
 
 static int *get_indexarray(Mesh *me)
 {
-       return MEM_mallocN(sizeof(int)*(me->totface+1), "vertexpaint");
+       return MEM_mallocN(sizeof(int)*(me->totpoly+1), "vertexpaint");
 }
 
 
@@ -197,7 +199,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 */
@@ -263,18 +265,82 @@ 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, has_shared = 0;
+
+       /* 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)) && (me->editflag & ME_EDIT_PAINT_MASK)!=0)
+                       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;
+               has_shared = 1;
+       }
+       
+       if (has_shared) {
+               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);
+
+       if (has_shared) {
+               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, TRUE);
        }
 
        //if(shade)
@@ -323,7 +389,7 @@ static int wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active)
        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);
@@ -331,10 +397,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);
        
 }
 
@@ -357,12 +423,14 @@ 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;
+       if(me==NULL || me->totpoly==0) return;
 
        if(!me->mcol) make_vertexcol(ob);
        if(!me->mcol) return; /* possible we can't make mcol's */
@@ -380,6 +448,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);
 }
@@ -389,7 +469,7 @@ void vpaint_fill(Object *ob, unsigned int paintcol)
 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
 {
        Mesh *me= ob->data;
-       MFace *mf;
+       MPoly *mf;
        MDeformWeight *dw, *uw;
        int vgroup_active, vgroup_mirror= -1;
        unsigned int index;
@@ -397,7 +477,7 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
        /* mutually exclusive, could be made into a */
        const short paint_selmode= ME_EDIT_PAINT_SEL_MODE(me);
 
-       if(me->totface==0 || me->dvert==NULL || !me->mface) return;
+       if(me->totpoly==0 || me->dvert==NULL || !me->mpoly) return;
        
        vgroup_active = ob->actdef - 1;
 
@@ -408,15 +488,15 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
        
        copy_wpaint_prev(wp, me->dvert, me->totvert);
        
-       for(index=0, mf= me->mface; index < me->totface; index++, mf++) {
-               unsigned int fidx= mf->v4 ? 3:2;
+       for(index=0, mf= me->mpoly; index < me->totpoly; index++, mf++) {
+               unsigned int fidx= mf->totloop - 1;
 
                if ((paint_selmode == SCE_SELECT_FACE) && !(mf->flag & ME_FACE_SEL)) {
                        continue;
                }
 
                do {
-                       unsigned int vidx= *(&mf->v1 + fidx);
+                       unsigned int vidx= me->mloop[mf->loopstart + fidx].v;
 
                        if(!me->dvert[vidx].flag) {
                                if((paint_selmode == SCE_SELECT_VERTEX) && !(me->mvert[vidx].flag & SELECT)) {
@@ -464,7 +544,7 @@ void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
 }
 
 /* XXX: should be re-implemented as a vertex/weight paint 'color correct' operator
+
 void vpaint_dogamma(Scene *scene)
 {
        VPaint *vp= scene->toolsettings->vpaint;
@@ -482,12 +562,12 @@ void vpaint_dogamma(Scene *scene)
 
        igam= 1.0/vp->gamma;
        for(a=0; a<256; a++) {
-               
+
                fac= ((float)a)/255.0;
                fac= vp->mul*pow( fac, igam);
-               
+
                temp= 255.9*fac;
-               
+
                if(temp<=0) gamtab[a]= 0;
                else if(temp>=255) gamtab[a]= 255;
                else gamtab[a]= temp;
@@ -496,16 +576,17 @@ void vpaint_dogamma(Scene *scene)
        a= 4*me->totface;
        cp= (unsigned char *)me->mcol;
        while(a--) {
-               
+
                cp[1]= gamtab[ cp[1] ];
                cp[2]= gamtab[ cp[2] ];
                cp[3]= gamtab[ cp[3] ];
-               
+
                cp+= 4;
        }
 }
  */
 
+
 static unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
 {
        char *cp1, *cp2, *cp;
@@ -890,14 +971,14 @@ static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
 
                index= view3d_sample_backbuf(&vc, event->mval[0], event->mval[1]);
 
-               if(index && index<=me->totface) {
+               if(index && index<=me->totpoly) {
                        DerivedMesh *dm= mesh_get_derived_final(vc.scene, vc.obact, CD_MASK_BAREMESH);
 
                        if(dm->getVertCo==NULL) {
                                BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
                        }
                        else {
-                               MFace *mf= ((MFace *)me->mface) + index-1;
+                               MPoly *mf= ((MPoly *)me->mpoly) + index-1;
                                const int vgroup_active= vc.obact->actdef - 1;
                                ToolSettings *ts= vc.scene->toolsettings;
                                float mval_f[2];
@@ -908,10 +989,10 @@ static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
                                mval_f[0]= (float)event->mval[0];
                                mval_f[1]= (float)event->mval[1];
 
-                               fidx= mf->v4 ? 3:2;
+                               fidx= mf->totloop - 1;
                                do {
                                        float co[3], sco[3], len;
-                                       const int v_idx= (*(&mf->v1 + fidx));
+                                       const int v_idx= me->mloop[mf->loopstart + fidx].v;
                                        dm->getVertCo(dm, v_idx, co);
                                        project_float_noclip(vc.ar, co, sco);
                                        len= len_squared_v2v2(mval_f, sco);
@@ -974,16 +1055,16 @@ static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA
 
                                index= view3d_sample_backbuf(&vc, win->eventstate->x - vc.ar->winrct.xmin, win->eventstate->y - vc.ar->winrct.ymin);
 
-                               if(index && index<=me->totface) {
+                               if(index && index<=me->totpoly) {
                                        const int defbase_tot= BLI_countlist(&vc.obact->defbase);
                                        if(defbase_tot) {
-                                               MFace *mf= ((MFace *)me->mface) + index-1;
-                                               unsigned int fidx= mf->v4 ? 3:2;
+                                               MPoly *mf= ((MPoly *)me->mpoly) + index-1;
+                                               unsigned int fidx= mf->totloop - 1;
                                                int *groups= MEM_callocN(defbase_tot*sizeof(int), "groups");
                                                int found= FALSE;
 
                                                do {
-                                                       MDeformVert *dvert= me->dvert + (*(&mf->v1 + fidx));
+                                                       MDeformVert *dvert= me->dvert + me->mloop[mf->loopstart + fidx].v;
                                                        int i= dvert->totweight;
                                                        MDeformWeight *dw;
                                                        for(dw= dvert->dw; i > 0; dw++, i--) {
@@ -1504,7 +1585,7 @@ static int apply_mp_locks_normalize(Mesh *me, const WeightPaintInfo *wpi,
        MDeformVert *dv= &me->dvert[index];
        MDeformVert dv_test= {NULL};
 
-       dv_test.dw= MEM_dupallocN(dv->dw);
+       dv_test.dw= BLI_cellalloc_dupalloc(dv->dw);
        dv_test.flag = dv->flag;
        dv_test.totweight = dv->totweight;
        /* do not multi-paint if a locked group is selected or the active group is locked
@@ -1532,19 +1613,19 @@ static int apply_mp_locks_normalize(Mesh *me, const WeightPaintInfo *wpi,
                if(tdw->weight != oldw) {
                        if(neww > oldw) {
                                if(tdw->weight <= oldw) {
-                                       MEM_freeN(dv_test.dw);
+                                       BLI_cellalloc_free(dv_test.dw);
                                        return TRUE;
                                }
                        }
                        else {
                                if(tdw->weight >= oldw) {
-                                       MEM_freeN(dv_test.dw);
+                                       BLI_cellalloc_free(dv_test.dw);
                                        return TRUE;
                                }
                        }
                }
        }
-       MEM_freeN(dv_test.dw);
+       BLI_cellalloc_free(dv_test.dw);
        return FALSE;
 }
 
@@ -1636,7 +1717,7 @@ static void do_weight_paint_vertex( /* vars which remain the same for every vert
                
                /* setup multi-paint */
                if(wpi->defbase_tot_sel > 1 && wpi->do_multipaint) {
-                       dv_copy.dw= MEM_dupallocN(dv->dw);
+                       dv_copy.dw= BLI_cellalloc_dupalloc(dv->dw);
                        dv_copy.flag = dv->flag;
                        dv_copy.totweight = dv->totweight;
                        tdw = dw;
@@ -1690,7 +1771,7 @@ static void do_weight_paint_vertex( /* vars which remain the same for every vert
                        oldChange = 0;
                }
                if(dv_copy.dw) {
-                       MEM_freeN(dv_copy.dw);
+                       BLI_cellalloc_free(dv_copy.dw);
                }
 #if 0
                /* dv may have been altered greatly */
@@ -1889,7 +1970,7 @@ 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;
+       if(me==NULL || me->totpoly==0) return OPERATOR_PASS_THROUGH;
        
        /* if nothing was added yet, we make dverts and a vertex deform group */
        if (!me->dvert) {
@@ -2050,7 +2131,7 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
        if(wp->flag & VP_AREA) {
                /* Ugly hack, to avoid drawing vertex index when getting the face index buffer - campbell */
                me->editflag &= ~ME_EDIT_VERT_SEL;
-               totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size_final);
+               totindex= sample_backbuf_area(vc, indexar, me->totpoly, mval[0], mval[1], brush_size_final);
                me->editflag |= use_vert_sel ? ME_EDIT_VERT_SEL : 0;
        }
        else {
@@ -2061,22 +2142,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;
                                }
                        }                                       
@@ -2092,42 +2173,39 @@ 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);
-                                       const float fac = calc_vp_strength_dl(wp, vc, wpd->vertexcosnos+6*vidx, mval, brush_size_final);
+                                               
+                               ml = me->mloop + mpoly->loopstart;
+                               for (i=0; i<mpoly->totloop; i++, ml++) {
+                                       const float fac = calc_vp_strength_dl(wp, vc, wpd->vertexcosnos+6*ml->v, mval, brush_size_final);
                                        if (fac > 0.0f) {
-                                               dw = dw_func(&me->dvert[vidx], wpi.vgroup_active);
+                                               dw = dw_func(&me->dvert[ml->v], wpi.vgroup_active);
                                                paintweight += dw ? (dw->weight * fac) : 0.0f;
                                                totw += fac;
                                        }
-
-                               } while (fidx--);
-
+                               }
                        }
                }
        }
@@ -2137,14 +2215,14 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
        }
 
        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, brush_size_final, brush_alpha_final);
                                        if(alpha) {
@@ -2152,7 +2230,7 @@ static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
                                        }
                                        me->dvert[vidx].flag= 0;
                                }
-                       } while (fidx--);
+                       }
                }
        }
 
@@ -2292,7 +2370,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) {
@@ -2360,14 +2438,52 @@ 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)
+{
+       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);
+
+       origIndex = CustomData_get_layer(&me->fdata, CD_POLYINDEX);
+       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);
@@ -2380,10 +2496,13 @@ static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent
 
        /* 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");
@@ -2393,9 +2512,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);
        
        /* 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 */
        mult_m4_m4m4(mat, vpd->vc.rv3d->viewmat, ob->obmat);
@@ -2405,6 +2525,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,
                               const unsigned int index, const float mval[2],
                               const float brush_size_final, const float brush_alpha_final,
@@ -2443,6 +2564,7 @@ static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob,
                }
        }
 }
+#endif
 
 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
 {
@@ -2453,6 +2575,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;
@@ -2465,7 +2588,9 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P
 
        RNA_float_get_array(itemptr, "mouse", mval);
        flip = RNA_boolean_get(itemptr, "pen_flip");
-                       
+
+       (void)flip; /* BMESH_TODO */
+
        view3d_operator_needs_opengl(C);
                        
        /* load projection matrix */
@@ -2477,7 +2602,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_final);
+               totindex= sample_backbuf_area(vc, indexar, me->totpoly, mval[0], mval[1], brush_size_final);
        }
        else {
                indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
@@ -2487,20 +2612,118 @@ 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, brush_size_final, brush_alpha_final, 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,
+                                                       brush_size_final, brush_alpha_final);
+                               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);
                        
+       /* 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);           
        DAG_id_tag_update(ob->data, 0);
 }
 
@@ -2515,7 +2738,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);
 }