Quiet annoying warning:
[blender.git] / source / blender / blenkernel / intern / DerivedMesh.c
index 769f6b04cf482cefa3ef719042c9c80194d59f87..628c07f7a6d7cc21194081edc2034a3e94af7a34 100644 (file)
@@ -115,7 +115,7 @@ static MEdge *dm_getEdgeArray(DerivedMesh *dm)
        return medge;
 }
 
-static MFace *dm_getFaceArray(DerivedMesh *dm)
+static MFace *dm_getTessFaceArray(DerivedMesh *dm)
 {
        MFace *mface = CustomData_get_layer(&dm->faceData, CD_MFACE);
 
@@ -135,7 +135,7 @@ static MLoop *dm_getLoopArray(DerivedMesh *dm)
 
        if (!mloop) {
                mloop = CustomData_add_layer(&dm->loopData, CD_MLOOP, CD_CALLOC, NULL,
-                       dm->numLoopData);
+                       dm->getNumLoops(dm));
                CustomData_set_layer_flag(&dm->loopData, CD_MLOOP, CD_FLAG_TEMPORARY);
                dm->copyLoopArray(dm, mloop);
        }
@@ -149,7 +149,7 @@ static MPoly *dm_getPolyArray(DerivedMesh *dm)
 
        if (!mpoly) {
                mpoly = CustomData_add_layer(&dm->polyData, CD_MPOLY, CD_CALLOC, NULL,
-                       dm->getNumFaces(dm));
+                       dm->getNumPolys(dm));
                CustomData_set_layer_flag(&dm->polyData, CD_MPOLY, CD_FLAG_TEMPORARY);
                dm->copyPolyArray(dm, mpoly);
        }
@@ -189,7 +189,7 @@ static MFace *dm_dupFaceArray(DerivedMesh *dm)
 
 static MLoop *dm_dupLoopArray(DerivedMesh *dm)
 {
-       MLoop *tmp = MEM_callocN(sizeof(*tmp) * dm->numLoopData,
+       MLoop *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumLoops(dm),
                                                         "dm_dupLoopArray tmp");
 
        if(tmp) dm->copyLoopArray(dm, tmp);
@@ -199,7 +199,7 @@ static MLoop *dm_dupLoopArray(DerivedMesh *dm)
 
 static MPoly *dm_dupPolyArray(DerivedMesh *dm)
 {
-       MPoly *tmp = MEM_callocN(sizeof(*tmp) * dm->numFaceData,
+       MPoly *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumPolys(dm),
                                                         "dm_dupPolyArray tmp");
 
        if(tmp) dm->copyPolyArray(dm, tmp);
@@ -217,7 +217,7 @@ static CustomData *dm_getEdgeCData(DerivedMesh *dm)
        return &dm->edgeData;
 }
 
-static CustomData *dm_getFaceCData(DerivedMesh *dm)
+static CustomData *dm_getTessFaceCData(DerivedMesh *dm)
 {
        return &dm->faceData;
 }
@@ -237,7 +237,7 @@ void DM_init_funcs(DerivedMesh *dm)
        /* default function implementations */
        dm->getVertArray = dm_getVertArray;
        dm->getEdgeArray = dm_getEdgeArray;
-       dm->getTessFaceArray = dm_getFaceArray;
+       dm->getTessFaceArray = dm_getTessFaceArray;
        dm->getLoopArray = dm_getLoopArray;
        dm->getPolyArray = dm_getPolyArray;
        dm->dupVertArray = dm_dupVertArray;
@@ -248,13 +248,13 @@ void DM_init_funcs(DerivedMesh *dm)
 
        dm->getVertDataLayout = dm_getVertCData;
        dm->getEdgeDataLayout = dm_getEdgeCData;
-       dm->getTessFaceDataLayout = dm_getFaceCData;
+       dm->getTessFaceDataLayout = dm_getTessFaceCData;
        dm->getLoopDataLayout = dm_getLoopCData;
-       dm->getFaceDataLayout = dm_getPolyCData;
+       dm->getPolyDataLayout = dm_getPolyCData;
 
        dm->getVertData = DM_get_vert_data;
        dm->getEdgeData = DM_get_edge_data;
-       dm->getTessFaceData = DM_get_face_data;
+       dm->getTessFaceData = DM_get_tessface_data;
        dm->getVertDataArray = DM_get_vert_data_layer;
        dm->getEdgeDataArray = DM_get_edge_data_layer;
        dm->getTessFaceDataArray = DM_get_tessface_data_layer;
@@ -263,14 +263,14 @@ void DM_init_funcs(DerivedMesh *dm)
 }
 
 void DM_init(DerivedMesh *dm, DerivedMeshType type, int numVerts, int numEdges,
-            int numFaces, int numLoops, int numPoly)
+            int numTessFaces, int numLoops, int numPolys)
 {
        dm->type = type;
        dm->numVertData = numVerts;
        dm->numEdgeData = numEdges;
-       dm->numFaceData = numFaces;
+       dm->numTessFaceData = numTessFaces;
        dm->numLoopData = numLoops;
-       dm->numPolyData = numPoly;
+       dm->numPolyData = numPolys;
 
        DM_init_funcs(dm);
        
@@ -278,15 +278,15 @@ void DM_init(DerivedMesh *dm, DerivedMeshType type, int numVerts, int numEdges,
 }
 
 void DM_from_template(DerivedMesh *dm, DerivedMesh *source, DerivedMeshType type,
-                      int numVerts, int numEdges, int numFaces,
-                     int numLoops, int numPolys)
+                      int numVerts, int numEdges, int numTessFaces,
+                      int numLoops, int numPolys)
 {
        CustomData_copy(&source->vertData, &dm->vertData, CD_MASK_DERIVEDMESH,
                                        CD_CALLOC, numVerts);
        CustomData_copy(&source->edgeData, &dm->edgeData, CD_MASK_DERIVEDMESH,
                                        CD_CALLOC, numEdges);
        CustomData_copy(&source->faceData, &dm->faceData, CD_MASK_DERIVEDMESH,
-                                       CD_CALLOC, numFaces);
+                                       CD_CALLOC, numTessFaces);
        CustomData_copy(&source->loopData, &dm->loopData, CD_MASK_DERIVEDMESH,
                        CD_CALLOC, numLoops);
        CustomData_copy(&source->polyData, &dm->polyData, CD_MASK_DERIVEDMESH,
@@ -295,7 +295,7 @@ void DM_from_template(DerivedMesh *dm, DerivedMesh *source, DerivedMeshType type
        dm->type = type;
        dm->numVertData = numVerts;
        dm->numEdgeData = numEdges;
-       dm->numFaceData = numFaces;
+       dm->numTessFaceData = numTessFaces;
        dm->numLoopData = numLoops;
        dm->numPolyData = numPolys;
 
@@ -311,7 +311,7 @@ int DM_release(DerivedMesh *dm)
                GPU_drawobject_free( dm );
                CustomData_free(&dm->vertData, dm->numVertData);
                CustomData_free(&dm->edgeData, dm->numEdgeData);
-               CustomData_free(&dm->faceData, dm->numFaceData);
+               CustomData_free(&dm->faceData, dm->numTessFaceData);
                CustomData_free(&dm->loopData, dm->numLoopData);
                CustomData_free(&dm->polyData, dm->numPolyData);
 
@@ -320,7 +320,7 @@ int DM_release(DerivedMesh *dm)
        else {
                CustomData_free_temporary(&dm->vertData, dm->numVertData);
                CustomData_free_temporary(&dm->edgeData, dm->numEdgeData);
-               CustomData_free_temporary(&dm->faceData, dm->numFaceData);
+               CustomData_free_temporary(&dm->faceData, dm->numTessFaceData);
                CustomData_free_temporary(&dm->loopData, dm->numLoopData);
                CustomData_free_temporary(&dm->polyData, dm->numPolyData);
 
@@ -330,8 +330,11 @@ int DM_release(DerivedMesh *dm)
 
 void DM_DupPolys(DerivedMesh *source, DerivedMesh *target)
 {
-       CustomData_copy(&source->loopData, &target->loopData, CD_MASK_DERIVEDMESH, CD_CALLOC, source->numLoopData);
-       CustomData_copy(&source->polyData, &target->polyData, CD_MASK_DERIVEDMESH, CD_CALLOC, source->numPolyData);
+       CustomData_free(&target->loopData, source->numLoopData);
+       CustomData_free(&target->polyData, source->numPolyData);
+
+       CustomData_copy(&source->loopData, &target->loopData, CD_MASK_DERIVEDMESH, CD_DUPLICATE, source->numLoopData);
+       CustomData_copy(&source->polyData, &target->polyData, CD_MASK_DERIVEDMESH, CD_DUPLICATE, source->numPolyData);
 
        target->numLoopData = source->numLoopData;
        target->numPolyData = source->numPolyData;
@@ -351,7 +354,7 @@ void DM_to_mesh(DerivedMesh *dm, Mesh *me, Object *ob)
 {
        /* dm might depend on me, so we need to do everything with a local copy */
        Mesh tmp = *me;
-       int totvert, totedge, totface, totloop, totpoly;
+       int totvert, totedge /*, totface */ /* UNUSED */, totloop, totpoly;
        int did_shapekeys=0;
        
        memset(&tmp.vdata, 0, sizeof(tmp.vdata));
@@ -362,13 +365,11 @@ void DM_to_mesh(DerivedMesh *dm, Mesh *me, Object *ob)
 
        totvert = tmp.totvert = dm->getNumVerts(dm);
        totedge = tmp.totedge = dm->getNumEdges(dm);
-       totface = tmp.totface = dm->getNumTessFaces(dm);
-       totpoly = tmp.totpoly = dm->getNumFaces(dm);
-       totloop = tmp.totloop = dm->numLoopData;
+       totloop = tmp.totloop = dm->getNumLoops(dm);
+       totpoly = tmp.totpoly = dm->getNumPolys(dm);
 
        CustomData_copy(&dm->vertData, &tmp.vdata, CD_MASK_MESH, CD_DUPLICATE, totvert);
        CustomData_copy(&dm->edgeData, &tmp.edata, CD_MASK_MESH, CD_DUPLICATE, totedge);
-       CustomData_copy(&dm->faceData, &tmp.fdata, CD_MASK_MESH, CD_DUPLICATE, totface);
        CustomData_copy(&dm->loopData, &tmp.ldata, CD_MASK_MESH, CD_DUPLICATE, totloop);
        CustomData_copy(&dm->polyData, &tmp.pdata, CD_MASK_MESH, CD_DUPLICATE, totpoly);
 
@@ -385,7 +386,9 @@ void DM_to_mesh(DerivedMesh *dm, Mesh *me, Object *ob)
                        }
                        
                        if (!kb) {
-                               printf("error in DM_to_mesh: could not find active shapekey! eek!!\n");
+                               printf("%s: error - could not find active shapekey %d!\n",
+                                      __func__, ob->shapenr-1);
+
                                i = INT_MAX;
                        }
                } else {
@@ -403,8 +406,6 @@ void DM_to_mesh(DerivedMesh *dm, Mesh *me, Object *ob)
                CustomData_add_layer(&tmp.vdata, CD_MVERT, CD_ASSIGN, dm->dupVertArray(dm), totvert);
        if(!CustomData_has_layer(&tmp.edata, CD_MEDGE))
                CustomData_add_layer(&tmp.edata, CD_MEDGE, CD_ASSIGN, dm->dupEdgeArray(dm), totedge);
-       if(!CustomData_has_layer(&tmp.fdata, CD_MFACE))
-               CustomData_add_layer(&tmp.fdata, CD_MFACE, CD_ASSIGN, dm->dupTessFaceArray(dm), totface);
        if(!CustomData_has_layer(&tmp.pdata, CD_MPOLY)) {
                tmp.mloop = dm->dupLoopArray(dm);
                tmp.mpoly = dm->dupPolyArray(dm);
@@ -422,7 +423,13 @@ void DM_to_mesh(DerivedMesh *dm, Mesh *me, Object *ob)
                }
        }
 
-       mesh_update_customdata_pointers(&tmp);
+       /* yes, must be before _and_ after tesselate */
+       mesh_update_customdata_pointers(&tmp, TRUE);
+
+       tmp.totface = mesh_recalcTesselation(&tmp.fdata, &tmp.ldata, &tmp.pdata, tmp.mvert, tmp.totface, tmp.totloop, tmp.totpoly);
+
+       mesh_update_customdata_pointers(&tmp, TRUE);
+
 
        CustomData_free(&me->vdata, me->totvert);
        CustomData_free(&me->edata, me->totedge);
@@ -458,7 +465,7 @@ void DM_to_meshkey(DerivedMesh *dm, Mesh *me, KeyBlock *kb)
        mvert=dm->getVertDataArray(dm, CD_MVERT);
        
        for(a=0; a<kb->totelem; a++, fp+=3, mvert++) {
-               VECCOPY(fp, mvert->co);
+               copy_v3_v3(fp, mvert->co);
        }
 }
 
@@ -481,15 +488,15 @@ void DM_add_edge_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 
 void DM_add_tessface_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 {
-       CustomData_add_layer(&dm->faceData, type, alloctype, layer, dm->numFaceData);
+       CustomData_add_layer(&dm->faceData, type, alloctype, layer, dm->numTessFaceData);
 }
 
-static void DM_add_loop_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
+void DM_add_loop_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 {
        CustomData_add_layer(&dm->loopData, type, alloctype, layer, dm->numLoopData);
 }
 
-void DM_add_face_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
+void DM_add_poly_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 {
        CustomData_add_layer(&dm->polyData, type, alloctype, layer, dm->numPolyData);
 }
@@ -504,7 +511,7 @@ void *DM_get_edge_data(DerivedMesh *dm, int index, int type)
        return CustomData_get(&dm->edgeData, index, type);
 }
 
-void *DM_get_face_data(DerivedMesh *dm, int index, int type)
+void *DM_get_tessface_data(DerivedMesh *dm, int index, int type)
 {
        return CustomData_get(&dm->faceData, index, type);
 }
@@ -527,13 +534,13 @@ void *DM_get_edge_data_layer(DerivedMesh *dm, int type)
 
 void *DM_get_tessface_data_layer(DerivedMesh *dm, int type)
 {
-       if(type == CD_MFACE)
+       if (type == CD_MFACE)
                return dm->getTessFaceArray(dm);
 
        return CustomData_get_layer(&dm->faceData, type);
 }
 
-void *DM_get_face_data_layer(DerivedMesh *dm, int type)
+void *DM_get_poly_data_layer(DerivedMesh *dm, int type)
 {
        return CustomData_get_layer(&dm->polyData, type);
 }
@@ -548,7 +555,7 @@ void DM_set_edge_data(DerivedMesh *dm, int index, int type, void *data)
        CustomData_set(&dm->edgeData, index, type, data);
 }
 
-void DM_set_face_data(DerivedMesh *dm, int index, int type, void *data)
+void DM_set_tessface_data(DerivedMesh *dm, int index, int type, void *data)
 {
        CustomData_set(&dm->faceData, index, type, data);
 }
@@ -581,7 +588,7 @@ void DM_copy_loop_data(DerivedMesh *source, DerivedMesh *dest,
                             source_index, dest_index, count);
 }
 
-void DM_copy_face_data(DerivedMesh *source, DerivedMesh *dest,
+void DM_copy_poly_data(DerivedMesh *source, DerivedMesh *dest,
                        int source_index, int dest_index, int count)
 {
        CustomData_copy_data(&source->polyData, &dest->polyData,
@@ -608,7 +615,7 @@ void DM_free_loop_data(struct DerivedMesh *dm, int index, int count)
        CustomData_free_elem(&dm->loopData, index, count);
 }
 
-void DM_free_face_data(struct DerivedMesh *dm, int index, int count)
+void DM_free_poly_data(struct DerivedMesh *dm, int index, int count)
 {
        CustomData_free_elem(&dm->polyData, index, count);
 }
@@ -652,7 +659,7 @@ void DM_interp_loop_data(DerivedMesh *source, DerivedMesh *dest,
                          weights, NULL, count, dest_index);
 }
 
-void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
+void DM_interp_poly_data(DerivedMesh *source, DerivedMesh *dest,
                          int *src_indices,
                          float *weights, int count, int dest_index)
 {
@@ -734,8 +741,9 @@ static float *get_editbmesh_orco_verts(BMEditMesh *em)
        orco = MEM_mallocN(sizeof(float)*3*totvert, "EditMesh Orco");
 
        eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
-       for (a=0; eve; eve=BMIter_Step(&iter), a+=3)
-               VECCOPY(orco+a, eve->co);
+       for (a=0; eve; eve=BMIter_Step(&iter), a+=3) {
+               copy_v3_v3(orco+a, eve->co);
+       }
        
        return orco;
 }
@@ -759,12 +767,10 @@ static void *get_orco_coords_dm(Object *ob, BMEditMesh *em, int layer, int *free
                   by a more flexible customdata system, but not simple */
                if(!em) {
                        ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(ob, eModifierType_Cloth);
-                       if (clmd) {
-                               KeyBlock *kb= key_get_keyblock(ob_get_key(ob), clmd->sim_parms->shapekey_rest);
-       
-                               if(kb->data)
-                                       return kb->data;
-                       }
+                       KeyBlock *kb= key_get_keyblock(ob_get_key(ob), clmd->sim_parms->shapekey_rest);
+
+                       if(kb->data)
+                               return kb->data;
                }
 
                return NULL;
@@ -836,31 +842,29 @@ static void add_orco_dm(Object *ob, BMEditMesh *em, DerivedMesh *dm,
  * happens on enter/exit wpaint.
  */
 
-void weight_to_rgb(float input, float *fr, float *fg, float *fb)
+void weight_to_rgb(float r_rgb[3], const float weight)
 {
-       float blend;
-       
-       blend= ((input/2.0f)+0.5f);
-       
-       if (input<=0.25f){      // blue->cyan
-               *fr= 0.0f;
-               *fg= blend*input*4.0f;
-               *fb= blend;
+       const float blend= ((weight/2.0f)+0.5f);
+
+       if (weight<=0.25f){     // blue->cyan
+               r_rgb[0]= 0.0f;
+               r_rgb[1]= blend*weight*4.0f;
+               r_rgb[2]= blend;
        }
-       else if (input<=0.50f){ // cyan->green
-               *fr= 0.0f;
-               *fg= blend;
-               *fb= blend*(1.0f-((input-0.25f)*4.0f)); 
+       else if (weight<=0.50f){        // cyan->green
+               r_rgb[0]= 0.0f;
+               r_rgb[1]= blend;
+               r_rgb[2]= blend*(1.0f-((weight-0.25f)*4.0f));
        }
-       else if (input <= 0.75f){       // green->yellow
-               *fr= blend * ((input-0.50f)*4.0f);
-               *fg= blend;
-               *fb= 0.0f;
+       else if (weight <= 0.75f){      // green->yellow
+               r_rgb[0]= blend * ((weight-0.50f)*4.0f);
+               r_rgb[1]= blend;
+               r_rgb[2]= 0.0f;
        }
-       else if (input <= 1.0f){ // yellow->red
-               *fr= blend;
-               *fg= blend * (1.0f-((input-0.75f)*4.0f)); 
-               *fb= 0.0f;
+       else if (weight <= 1.0f){ // yellow->red
+               r_rgb[0]= blend;
+               r_rgb[1]= blend * (1.0f-((weight-0.75f)*4.0f));
+               r_rgb[2]= 0.0f;
        }
 }
 
@@ -925,7 +929,7 @@ static void calc_weightpaint_vert_color(Object *ob, ColorBand *coba, int vert, u
        if(coba)
                do_colorband(coba, input, colf);
        else
-               weight_to_rgb(input, colf, colf+1, colf+2);
+               weight_to_rgb(colf, input);
        
        col[3] = (unsigned char)(colf[0] * 255.0f);
        col[2] = (unsigned char)(colf[1] * 255.0f);
@@ -1091,7 +1095,7 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
        Mesh *me = ob->data;
        ModifierData *firstmd, *md;
        LinkNode *datamasks, *curr;
-       CustomDataMask mask, nextmask;
+       CustomDataMask mask, nextmask, append_mask = 0;
        float (*deformedVerts)[3] = NULL;
        DerivedMesh *dm=NULL, *orcodm, *clothorcodm, *finaldm;
        int numVerts = me->totvert;
@@ -1105,6 +1109,8 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
        int draw_flag= ((scene->toolsettings->multipaint ? CALC_WP_MULTIPAINT : 0) |
                        (scene->toolsettings->auto_normalize ? CALC_WP_AUTO_NORMALIZE : 0));
 
+       short do_re_tessellate;
+
        if(mmd && !mmd->sculptlvl)
                has_multires = 0;
 
@@ -1164,7 +1170,7 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                 */
                if (deform_r) {
                        *deform_r = CDDM_from_mesh(me, ob);
-                       
+                        
                        if (build_shapekey_layers)
                                add_shapekey_layers(dm, me, ob);
                        
@@ -1300,11 +1306,11 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                                        /* calc */
                                        DM_add_vert_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
                                        DM_add_edge_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
-                                       DM_add_face_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
+                                       DM_add_poly_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
 
-                                       range_vni(DM_get_vert_data_layer(dm, CD_ORIGINDEX), dm->numVertData, 0);
-                                       range_vni(DM_get_edge_data_layer(dm, CD_ORIGINDEX), dm->numEdgeData, 0);
-                                       range_vni(DM_get_face_data_layer(dm, CD_ORIGINDEX), dm->numPolyData, 0);
+                                       range_vn_i(DM_get_vert_data_layer(dm, CD_ORIGINDEX), dm->numVertData, 0);
+                                       range_vn_i(DM_get_edge_data_layer(dm, CD_ORIGINDEX), dm->numEdgeData, 0);
+                                       range_vn_i(DM_get_poly_data_layer(dm, CD_ORIGINDEX), dm->numPolyData, 0);
                                }
 
                                if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
@@ -1317,6 +1323,7 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                        mask= (CustomDataMask)GET_INT_FROM_POINTER(curr->link);
                        /* needMapping check here fixes bug [#28112], otherwise its
                         * possible that it wont be copied */
+                       mask |= append_mask;
                        DM_set_only_copy(dm, mask | (needMapping ? CD_MASK_ORIGINDEX : 0));
                        
                        /* add cloth rest shape key if need */
@@ -1375,6 +1382,10 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                                        clothorcodm = ndm;
                                }
                        }
+
+                       /* in case of dynamic paint, make sure preview mask remains for following modifiers */
+                       if (md->type == eModifierType_DynamicPaint)
+                               append_mask |= CD_MASK_WEIGHT_MCOL;
                }
 
                isPrevDeform= (mti->type == eModifierTypeType_OnlyDeform);
@@ -1400,12 +1411,20 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                dm->release(dm);
 
                CDDM_apply_vert_coords(finaldm, deformedVerts);
+
+               /* BMESH_TODO, do_re_tesselate recalculates normals anyway, this seems redundant! - campbell */
                CDDM_calc_normals(finaldm);
 
                if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
                        add_weight_mcol_dm(ob, finaldm, draw_flag);
+
+               do_re_tessellate= TRUE;
+
        } else if(dm) {
                finaldm = dm;
+
+               do_re_tessellate= TRUE;
+
        } else {
                int recalc_normals= 0;
 
@@ -1426,6 +1445,8 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                
                if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
                        add_weight_mcol_dm(ob, finaldm, draw_flag);
+
+               do_re_tessellate= FALSE;
        }
 
        /* add an orco layer if needed */
@@ -1448,6 +1469,24 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
        }
 #endif /* WITH_GAMEENGINE */
 
+
+       /* need to check when this isnt needed.
+        * - when the mesh has no modifiers (shouldnt be needed)
+        * - deform only? (unside, can try skip) but need to double check
+        * - rebuild mesh with constructive modifier (ofcourse)
+        *
+        * Need to watch this, it can cause issues, see bug [#29338]
+        */
+       if (do_re_tessellate) {
+               /* Re-tesselation is necessary to push render data (uvs, textures, colors)
+                * from loops and polys onto the tessfaces. This may be currently be redundant
+                * in cases where the render mode doesn't use these inputs, but ideally
+                * eventually tesselation would happen on-demand, and this is one of the primary
+                * places it would be needed. */
+               finaldm->recalcTesselation(finaldm);
+               finaldm->calcNormals(finaldm);
+       }
+
        *final_r = finaldm;
 
        if(orcodm)
@@ -1472,7 +1511,7 @@ float (*editbmesh_get_vertex_cos(BMEditMesh *em, int *numVerts_r))[3]
 
        eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
        for (i=0; eve; eve=BMIter_Step(&iter), i++) {
-               VECCOPY(cos[i], eve->co);
+               copy_v3_v3(cos[i], eve->co);
        }
 
        return cos;
@@ -1499,7 +1538,7 @@ static void editbmesh_calc_modifiers(Scene *scene, Object *ob, BMEditMesh *em, D
        ModifierData *md;
        float (*deformedVerts)[3] = NULL;
        CustomDataMask mask;
-       DerivedMesh *dm, *orcodm = NULL;
+       DerivedMesh *dm = NULL, *orcodm = NULL, *finaldm = NULL;
        int i, numVerts = 0, cageIndex = modifiers_getCageIndex(scene, ob, NULL, 1);
        LinkNode *datamasks, *curr;
        int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
@@ -1510,7 +1549,6 @@ static void editbmesh_calc_modifiers(Scene *scene, Object *ob, BMEditMesh *em, D
                *cage_r = getEditDerivedBMesh(em, ob, NULL);
        }
 
-       dm = NULL;
        md = modifiers_getVirtualModifierList(ob);
 
        datamasks = modifiers_calcDataMasks(scene, ob, md, dataMask, required_mode);
@@ -1652,21 +1690,24 @@ static void editbmesh_calc_modifiers(Scene *scene, Object *ob, BMEditMesh *em, D
         * then we need to build one.
         */
        if(dm && deformedVerts) {
-               *final_r = CDDM_copy(dm, 0);
+               finaldm = CDDM_copy(dm, 0);
 
                if(!(cage_r && dm == *cage_r)) dm->release(dm);
 
                CDDM_apply_vert_coords(*final_r, deformedVerts);
-               CDDM_calc_normals(*final_r);
        } else if (dm) {
-               *final_r = dm;
+               finaldm = dm;
        } else if (!deformedVerts && cage_r && *cage_r) {
-               *final_r = *cage_r;
+               finaldm = *cage_r;
        } else {
-               *final_r = getEditDerivedBMesh(em, ob, deformedVerts);
+               finaldm = getEditDerivedBMesh(em, ob, deformedVerts);
                deformedVerts = NULL;
        }
 
+       finaldm->calcNormals(finaldm);
+
+       *final_r = finaldm;
+
        /* add an orco layer if needed */
        if(dataMask & CD_MASK_ORCO)
                add_orco_dm(ob, em, *final_r, orcodm, CD_ORCO);
@@ -1716,7 +1757,7 @@ static void mesh_build_data(Scene *scene, Object *ob, CustomDataMask dataMask,
        Object *obact = scene->basact?scene->basact->object:NULL;
        int editing = paint_facesel_test(ob);
        /* weight paint and face select need original indices because of selection buffer drawing */
-       int needMapping = (ob==obact) && (editing || (ob->mode & (OB_MODE_WEIGHT_PAINT|OB_MODE_VERTEX_PAINT)));
+       int needMapping = (ob==obact) && (editing || (ob->mode & (OB_MODE_WEIGHT_PAINT|OB_MODE_VERTEX_PAINT|OB_MODE_TEXTURE_PAINT)));
 
        clear_mesh_caches(ob);
 
@@ -1958,7 +1999,7 @@ typedef struct
        MVert * mvert;          // vertices & normals
        float (*orco)[3];
        float (*tangent)[4];    // destination
-       int numFaces;
+       int numTessFaces;
 
 } SGLSLMeshToTangent;
 
@@ -1968,7 +2009,7 @@ typedef struct
 static int GetNumFaces(const SMikkTSpaceContext * pContext)
 {
        SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
-       return pMesh->numFaces;
+       return pMesh->numTessFaces;
 }
 
 static int GetNumVertsOfFace(const SMikkTSpaceContext * pContext, const int face_num)
@@ -1982,7 +2023,7 @@ static void GetPosition(const SMikkTSpaceContext * pContext, float fPos[], const
        //assert(vert_index>=0 && vert_index<4);
        SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
        const float *co= pMesh->mvert[(&pMesh->mface[face_num].v1)[vert_index]].co;
-       VECCOPY(fPos, co);
+       copy_v3_v3(fPos, co);
 }
 
 static void GetTextureCoordinate(const SMikkTSpaceContext * pContext, float fUV[], const int face_num, const int vert_index)
@@ -2008,7 +2049,7 @@ static void GetNormal(const SMikkTSpaceContext * pContext, float fNorm[], const
        const int smoothnormal = (pMesh->mface[face_num].flag & ME_SMOOTH);
        if(!smoothnormal) {     // flat
                if(pMesh->precomputedFaceNormals) {
-                       VECCOPY(fNorm, &pMesh->precomputedFaceNormals[3*face_num]);
+                       copy_v3_v3(fNorm, &pMesh->precomputedFaceNormals[3*face_num]);
                }
                else {
                        MFace *mf= &pMesh->mface[face_num];
@@ -2035,7 +2076,7 @@ static void SetTSpace(const SMikkTSpaceContext * pContext, const float fvTangent
        //assert(vert_index>=0 && vert_index<4);
        SGLSLMeshToTangent * pMesh = (SGLSLMeshToTangent *) pContext->m_pUserData;
        float * pRes = pMesh->tangent[4*face_num+iVert];
-       VECCOPY(pRes, fvTangent);
+       copy_v3_v3(pRes, fvTangent);
        pRes[3]=fSign;
 }
 
@@ -2095,7 +2136,7 @@ void DM_add_tangent_layer(DerivedMesh *dm)
                mesh2tangent.mvert = mvert;
                mesh2tangent.orco = orco;
                mesh2tangent.tangent = tangent;
-               mesh2tangent.numFaces = totface;
+               mesh2tangent.numTessFaces = totface;
 
                sContext.m_pUserData = &mesh2tangent;
                sContext.m_pInterface = &sInterface;
@@ -2219,9 +2260,12 @@ void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs,
                                attribs->tface[a].array = tfdata->layers[layer].data;
                                attribs->tface[a].emOffset = tfdata->layers[layer].offset;
                                attribs->tface[a].glIndex = gattribs->layer[b].glindex;
-                       } /*else {
+                               /* attribs->tface[a].glTexco = gattribs->layer[b].gltexco; */ /* BMESH_TODO, trunk has this but not bmesh, need to investigate whats going on here - campbell */
+                       }
+                       /* BMESH ONLY, may need to get this working?, otherwise remove */
+                       /* else {
                                int player;
-                               CustomData *pdata = dm->getFaceDataLayout(dm);
+                               CustomData *pdata = dm->getPolyDataLayout(dm);
                                
                                if(gattribs->layer[b].name[0])
                                        player = CustomData_get_named_layer_index(pdata, CD_MTEXPOLY,
@@ -2238,7 +2282,8 @@ void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs,
                                        attribs->tface[a].glTexco = gattribs->layer[b].gltexco;
                                        
                                }
-                       }*/
+                       }
+                       */
                }
                else if(gattribs->layer[b].type == CD_MCOL) {
                        /* vertex colors */
@@ -2341,7 +2386,7 @@ static void navmesh_drawColored(DerivedMesh *dm)
                DEBUG_VBO( "Using legacy code. drawNavMeshColored\n" );
                //glShadeModel(GL_SMOOTH);
                glBegin(glmode = GL_QUADS);
-               for(a = 0; a < dm->numFaceData; a++, mface++) {
+               for(a = 0; a < dm->numTessFaceData; a++, mface++) {
                        int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
                        int pi = polygonIdx[a];
                        if (pi <= 0) {
@@ -2368,9 +2413,14 @@ static void navmesh_drawColored(DerivedMesh *dm)
        glEnable(GL_LIGHTING);
 }
 
-static void navmesh_DM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr))
+static void navmesh_DM_drawFacesTex(DerivedMesh *dm,
+                       int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr),
+                       int (*compareDrawOptions)(void *userData, int cur_index, int next_index),
+                       void *userData)
 {
        (void) setDrawOptions;
+       (void) compareDrawOptions;
+       (void) userData;
 
        navmesh_drawColored(dm);
 }
@@ -2389,7 +2439,7 @@ static void navmesh_DM_drawFacesSolid(DerivedMesh *dm,
 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm)
 {
        DerivedMesh *result;
-       int maxFaces = dm->getNumFaces(dm);
+       int maxFaces = dm->getNumPolys(dm);
        int *recastData;
        int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0;
        float* verts=NULL;
@@ -2397,7 +2447,7 @@ static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm)
        int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
        int res;
 
-       result = CDDM_copy(dm);
+       result = CDDM_copy(dm, 0);
        if (!CustomData_has_layer(&result->faceData, CD_RECAST)) {
                int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
                if (sourceRecastData) {