Quiet annoying warning:
[blender.git] / source / blender / blenkernel / intern / DerivedMesh.c
index 4400c895b78a2d98996b00a48e27e861e7b1f866..628c07f7a6d7cc21194081edc2034a3e94af7a34 100644 (file)
@@ -31,7 +31,7 @@
 
 
 #include <string.h>
-
+#include "limits.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -46,6 +46,7 @@
 #include "BLI_editVert.h"
 #include "BLI_math.h"
 #include "BLI_memarena.h"
+#include "BLI_array.h"
 #include "BLI_pbvh.h"
 #include "BLI_utildefines.h"
 #include "BLI_linklist.h"
@@ -60,6 +61,9 @@
 #include "BKE_texture.h"
 #include "BKE_multires.h"
 #include "BKE_armature.h"
+#include "BKE_particle.h"
+#include "BKE_tessmesh.h"
+#include "BKE_bvhutils.h"
 
 #ifdef WITH_GAMEENGINE
 #include "BKE_navmesh_conversion.h"
@@ -77,6 +81,9 @@ static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm);
 
 extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */
 
+static void add_shapekey_layers(DerivedMesh *dm, Mesh *me, Object *ob);
+static void shapekey_layers_to_keyblocks(DerivedMesh *dm, Mesh *me, int actshape_uid);
+
 ///////////////////////////////////
 ///////////////////////////////////
 
@@ -108,20 +115,48 @@ 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);
 
        if (!mface) {
                mface = CustomData_add_layer(&dm->faceData, CD_MFACE, CD_CALLOC, NULL,
-                       dm->getNumFaces(dm));
+                       dm->getNumTessFaces(dm));
                CustomData_set_layer_flag(&dm->faceData, CD_MFACE, CD_FLAG_TEMPORARY);
-               dm->copyFaceArray(dm, mface);
+               dm->copyTessFaceArray(dm, mface);
        }
 
        return mface;
 }
 
+static MLoop *dm_getLoopArray(DerivedMesh *dm)
+{
+       MLoop *mloop = CustomData_get_layer(&dm->loopData, CD_MLOOP);
+
+       if (!mloop) {
+               mloop = CustomData_add_layer(&dm->loopData, CD_MLOOP, CD_CALLOC, NULL,
+                       dm->getNumLoops(dm));
+               CustomData_set_layer_flag(&dm->loopData, CD_MLOOP, CD_FLAG_TEMPORARY);
+               dm->copyLoopArray(dm, mloop);
+       }
+
+       return mloop;
+}
+
+static MPoly *dm_getPolyArray(DerivedMesh *dm)
+{
+       MPoly *mpoly = CustomData_get_layer(&dm->polyData, CD_MPOLY);
+
+       if (!mpoly) {
+               mpoly = CustomData_add_layer(&dm->polyData, CD_MPOLY, CD_CALLOC, NULL,
+                       dm->getNumPolys(dm));
+               CustomData_set_layer_flag(&dm->polyData, CD_MPOLY, CD_FLAG_TEMPORARY);
+               dm->copyPolyArray(dm, mpoly);
+       }
+
+       return mpoly;
+}
+
 static MVert *dm_dupVertArray(DerivedMesh *dm)
 {
        MVert *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumVerts(dm),
@@ -144,41 +179,98 @@ static MEdge *dm_dupEdgeArray(DerivedMesh *dm)
 
 static MFace *dm_dupFaceArray(DerivedMesh *dm)
 {
-       MFace *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumFaces(dm),
+       MFace *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumTessFaces(dm),
                                                         "dm_dupFaceArray tmp");
 
-       if(tmp) dm->copyFaceArray(dm, tmp);
+       if(tmp) dm->copyTessFaceArray(dm, tmp);
 
        return tmp;
 }
 
+static MLoop *dm_dupLoopArray(DerivedMesh *dm)
+{
+       MLoop *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumLoops(dm),
+                                                        "dm_dupLoopArray tmp");
+
+       if(tmp) dm->copyLoopArray(dm, tmp);
+
+       return tmp;
+}
+
+static MPoly *dm_dupPolyArray(DerivedMesh *dm)
+{
+       MPoly *tmp = MEM_callocN(sizeof(*tmp) * dm->getNumPolys(dm),
+                                                        "dm_dupPolyArray tmp");
+
+       if(tmp) dm->copyPolyArray(dm, tmp);
+
+       return tmp;
+}
+
+static CustomData *dm_getVertCData(DerivedMesh *dm)
+{
+       return &dm->vertData;
+}
+
+static CustomData *dm_getEdgeCData(DerivedMesh *dm)
+{
+       return &dm->edgeData;
+}
+
+static CustomData *dm_getTessFaceCData(DerivedMesh *dm)
+{
+       return &dm->faceData;
+}
+
+static CustomData *dm_getLoopCData(DerivedMesh *dm)
+{
+       return &dm->loopData;
+}
+
+static CustomData *dm_getPolyCData(DerivedMesh *dm)
+{
+       return &dm->polyData;
+}
+
 void DM_init_funcs(DerivedMesh *dm)
 {
        /* default function implementations */
        dm->getVertArray = dm_getVertArray;
        dm->getEdgeArray = dm_getEdgeArray;
-       dm->getFaceArray = dm_getFaceArray;
+       dm->getTessFaceArray = dm_getTessFaceArray;
+       dm->getLoopArray = dm_getLoopArray;
+       dm->getPolyArray = dm_getPolyArray;
        dm->dupVertArray = dm_dupVertArray;
        dm->dupEdgeArray = dm_dupEdgeArray;
-       dm->dupFaceArray = dm_dupFaceArray;
+       dm->dupTessFaceArray = dm_dupFaceArray;
+       dm->dupLoopArray = dm_dupLoopArray;
+       dm->dupPolyArray = dm_dupPolyArray;
+
+       dm->getVertDataLayout = dm_getVertCData;
+       dm->getEdgeDataLayout = dm_getEdgeCData;
+       dm->getTessFaceDataLayout = dm_getTessFaceCData;
+       dm->getLoopDataLayout = dm_getLoopCData;
+       dm->getPolyDataLayout = dm_getPolyCData;
 
        dm->getVertData = DM_get_vert_data;
        dm->getEdgeData = DM_get_edge_data;
-       dm->getFaceData = 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->getFaceDataArray = DM_get_face_data_layer;
+       dm->getTessFaceDataArray = DM_get_tessface_data_layer;
 
        bvhcache_init(&dm->bvhCache);
 }
 
-void DM_init(DerivedMesh *dm, DerivedMeshType type,
-                        int numVerts, int numEdges, int numFaces)
+void DM_init(DerivedMesh *dm, DerivedMeshType type, int numVerts, int numEdges,
+            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 = numPolys;
 
        DM_init_funcs(dm);
        
@@ -186,19 +278,26 @@ void DM_init(DerivedMesh *dm, DerivedMeshType type,
 }
 
 void DM_from_template(DerivedMesh *dm, DerivedMesh *source, DerivedMeshType type,
-                                         int numVerts, int numEdges, int numFaces)
+                      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,
+                       CD_CALLOC, numPolys);
 
        dm->type = type;
        dm->numVertData = numVerts;
        dm->numEdgeData = numEdges;
-       dm->numFaceData = numFaces;
+       dm->numTessFaceData = numTessFaces;
+       dm->numLoopData = numLoops;
+       dm->numPolyData = numPolys;
 
        DM_init_funcs(dm);
 
@@ -212,63 +311,137 @@ 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);
 
                return 1;
        }
        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);
 
                return 0;
        }
 }
 
-void DM_to_mesh(DerivedMesh *dm, Mesh *me)
+void DM_DupPolys(DerivedMesh *source, DerivedMesh *target)
+{
+       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;
+
+       if (!CustomData_has_layer(&target->polyData, CD_MPOLY)) {
+               MPoly *mpoly;
+               MLoop *mloop;
+
+               mloop = source->dupLoopArray(source);
+               mpoly = source->dupPolyArray(source);
+               CustomData_add_layer(&target->loopData, CD_MLOOP, CD_ASSIGN, mloop, source->numLoopData);
+               CustomData_add_layer(&target->polyData, CD_MPOLY, CD_ASSIGN, mpoly, source->numPolyData);
+       }
+}
+
+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;
-
+       int totvert, totedge /*, totface */ /* UNUSED */, totloop, totpoly;
+       int did_shapekeys=0;
+       
        memset(&tmp.vdata, 0, sizeof(tmp.vdata));
        memset(&tmp.edata, 0, sizeof(tmp.edata));
        memset(&tmp.fdata, 0, sizeof(tmp.fdata));
+       memset(&tmp.ldata, 0, sizeof(tmp.ldata));
+       memset(&tmp.pdata, 0, sizeof(tmp.pdata));
 
        totvert = tmp.totvert = dm->getNumVerts(dm);
        totedge = tmp.totedge = dm->getNumEdges(dm);
-       totface = tmp.totface = dm->getNumFaces(dm);
+       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);
 
+       if (CustomData_has_layer(&dm->vertData, CD_SHAPEKEY)) {
+               KeyBlock *kb;
+               int i=0;
+               
+               if (ob) {
+                       for (kb=me->key->block.first; kb; kb=kb->next, i++) {
+                               if (i == ob->shapenr-1) {
+                                       i = kb->uid;
+                                       break;
+                               }
+                       }
+                       
+                       if (!kb) {
+                               printf("%s: error - could not find active shapekey %d!\n",
+                                      __func__, ob->shapenr-1);
+
+                               i = INT_MAX;
+                       }
+               } else {
+                       /*if no object, set to INT_MAX so we don't mess up any shapekey layers*/
+                       i = INT_MAX;
+               }
+               
+               shapekey_layers_to_keyblocks(dm, me, i);
+               did_shapekeys = 1;
+       }
+       
        /* not all DerivedMeshes store their verts/edges/faces in CustomData, so
           we set them here in case they are missing */
        if(!CustomData_has_layer(&tmp.vdata, CD_MVERT))
                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->dupFaceArray(dm), totface);
+       if(!CustomData_has_layer(&tmp.pdata, CD_MPOLY)) {
+               tmp.mloop = dm->dupLoopArray(dm);
+               tmp.mpoly = dm->dupPolyArray(dm);
+
+               CustomData_add_layer(&tmp.ldata, CD_MLOOP, CD_ASSIGN, tmp.mloop, tmp.totloop);
+               CustomData_add_layer(&tmp.pdata, CD_MPOLY, CD_ASSIGN, tmp.mpoly, tmp.totpoly);
+       }
 
        /* object had got displacement layer, should copy this layer to save sculpted data */
        /* NOTE: maybe some other layers should be copied? nazgul */
-       if(CustomData_has_layer(&me->fdata, CD_MDISPS)) {
-               if (totface == me->totface) {
-                       MDisps *mdisps = CustomData_get_layer(&me->fdata, CD_MDISPS);
-                       CustomData_add_layer(&tmp.fdata, CD_MDISPS, CD_DUPLICATE, mdisps, totface);
+       if(CustomData_has_layer(&me->ldata, CD_MDISPS)) {
+               if (totloop == me->totloop) {
+                       MDisps *mdisps = CustomData_get_layer(&me->ldata, CD_MDISPS);
+                       CustomData_add_layer(&tmp.ldata, CD_MDISPS, CD_DUPLICATE, mdisps, totloop);
                }
        }
 
-       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);
        CustomData_free(&me->fdata, me->totface);
-
-       /* if the number of verts has changed, remove invalid data */
-       if(tmp.totvert != me->totvert) {
+       CustomData_free(&me->ldata, me->totloop);
+       CustomData_free(&me->pdata, me->totpoly);
+
+       /*  ok, this should now use new CD shapekey data,
+           which shouuld be fed through the modifier 
+               stack*/
+       if(tmp.totvert != me->totvert && !did_shapekeys && me->key) {
+               printf("YEEK! this should be recoded! Shape key loss!!!\n");
                if(tmp.key) tmp.key->id.us--;
                tmp.key = NULL;
        }
@@ -313,1324 +486,207 @@ void DM_add_edge_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
        CustomData_add_layer(&dm->edgeData, type, alloctype, layer, dm->numEdgeData);
 }
 
-void DM_add_face_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);
 }
 
-void *DM_get_vert_data(DerivedMesh *dm, int index, int type)
+void DM_add_loop_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 {
-       return CustomData_get(&dm->vertData, index, type);
+       CustomData_add_layer(&dm->loopData, type, alloctype, layer, dm->numLoopData);
 }
 
-void *DM_get_edge_data(DerivedMesh *dm, int index, int type)
+void DM_add_poly_layer(DerivedMesh *dm, int type, int alloctype, void *layer)
 {
-       return CustomData_get(&dm->edgeData, index, type);
+       CustomData_add_layer(&dm->polyData, type, alloctype, layer, dm->numPolyData);
 }
 
-void *DM_get_face_data(DerivedMesh *dm, int index, int type)
-{
-       return CustomData_get(&dm->faceData, index, type);
-}
-
-void *DM_get_vert_data_layer(DerivedMesh *dm, int type)
-{
-       if(type == CD_MVERT)
-               return dm->getVertArray(dm);
-
-       return CustomData_get_layer(&dm->vertData, type);
-}
-
-void *DM_get_edge_data_layer(DerivedMesh *dm, int type)
-{
-       if(type == CD_MEDGE)
-               return dm->getEdgeArray(dm);
-
-       return CustomData_get_layer(&dm->edgeData, type);
-}
-
-void *DM_get_face_data_layer(DerivedMesh *dm, int type)
-{
-       if(type == CD_MFACE)
-               return dm->getFaceArray(dm);
-
-       return CustomData_get_layer(&dm->faceData, type);
-}
-
-void DM_set_vert_data(DerivedMesh *dm, int index, int type, void *data)
-{
-       CustomData_set(&dm->vertData, index, type, data);
-}
-
-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)
-{
-       CustomData_set(&dm->faceData, index, type, data);
-}
-
-void DM_copy_vert_data(DerivedMesh *source, DerivedMesh *dest,
-                                          int source_index, int dest_index, int count)
-{
-       CustomData_copy_data(&source->vertData, &dest->vertData,
-                                                source_index, dest_index, count);
-}
-
-void DM_copy_edge_data(DerivedMesh *source, DerivedMesh *dest,
-                                          int source_index, int dest_index, int count)
-{
-       CustomData_copy_data(&source->edgeData, &dest->edgeData,
-                                                source_index, dest_index, count);
-}
-
-void DM_copy_face_data(DerivedMesh *source, DerivedMesh *dest,
-                                          int source_index, int dest_index, int count)
-{
-       CustomData_copy_data(&source->faceData, &dest->faceData,
-                                                source_index, dest_index, count);
-}
-
-void DM_free_vert_data(struct DerivedMesh *dm, int index, int count)
-{
-       CustomData_free_elem(&dm->vertData, index, count);
-}
-
-void DM_free_edge_data(struct DerivedMesh *dm, int index, int count)
-{
-       CustomData_free_elem(&dm->edgeData, index, count);
-}
-
-void DM_free_face_data(struct DerivedMesh *dm, int index, int count)
-{
-       CustomData_free_elem(&dm->faceData, index, count);
-}
-
-void DM_interp_vert_data(DerivedMesh *source, DerivedMesh *dest,
-                                                int *src_indices, float *weights,
-                                                int count, int dest_index)
-{
-       CustomData_interp(&source->vertData, &dest->vertData, src_indices,
-                                         weights, NULL, count, dest_index);
-}
-
-void DM_interp_edge_data(DerivedMesh *source, DerivedMesh *dest,
-                                                int *src_indices,
-                                                float *weights, EdgeVertWeight *vert_weights,
-                                                int count, int dest_index)
-{
-       CustomData_interp(&source->edgeData, &dest->edgeData, src_indices,
-                                         weights, (float*)vert_weights, count, dest_index);
-}
-
-void DM_interp_face_data(DerivedMesh *source, DerivedMesh *dest,
-                                                int *src_indices,
-                                                float *weights, FaceVertWeight *vert_weights,
-                                                int count, int dest_index)
-{
-       CustomData_interp(&source->faceData, &dest->faceData, src_indices,
-                                         weights, (float*)vert_weights, count, dest_index);
-}
-
-void DM_swap_face_data(DerivedMesh *dm, int index, const int *corner_indices)
-{
-       CustomData_swap(&dm->faceData, index, corner_indices);
-}
-
-///
-
-DerivedMesh *mesh_create_derived(Mesh *me, Object *ob, float (*vertCos)[3])
-{
-       DerivedMesh *dm = CDDM_from_mesh(me, ob);
-       
-       if(!dm)
-               return NULL;
-       
-       if (vertCos)
-               CDDM_apply_vert_coords(dm, vertCos);
-
-       CDDM_calc_normals(dm);
-
-       return dm;
-}
-
-///
-
-typedef struct {
-       DerivedMesh dm;
-
-       EditMesh *em;
-       float (*vertexCos)[3];
-       float (*vertexNos)[3];
-       float (*faceNos)[3];
-} EditMeshDerivedMesh;
-
-static void emDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no_f, short *no_s), void *userData)
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditVert *eve;
-       int i;
-
-       for (i=0,eve= emdm->em->verts.first; eve; i++,eve=eve->next) {
-               if (emdm->vertexCos) {
-                       func(userData, i, emdm->vertexCos[i], emdm->vertexNos[i], NULL);
-               } else {
-                       func(userData, i, eve->co, eve->no, NULL);
-               }
-       }
-}
-static void emDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData, int index, float *v0co, float *v1co), void *userData)
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditEdge *eed;
-       int i;
-
-       if (emdm->vertexCos) {
-               EditVert *eve;
-
-               for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->tmp.l = (intptr_t) i++;
-               for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next)
-                       func(userData, i, emdm->vertexCos[(int) eed->v1->tmp.l], emdm->vertexCos[(int) eed->v2->tmp.l]);
-       } else {
-               for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next)
-                       func(userData, i, eed->v1->co, eed->v2->co);
-       }
-}
-static void emDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData) 
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditEdge *eed;
-       int i;
-
-       if (emdm->vertexCos) {
-               EditVert *eve;
-
-               for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->tmp.l = (intptr_t) i++;
-
-               glBegin(GL_LINES);
-               for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
-                       if(!setDrawOptions || setDrawOptions(userData, i)) {
-                               glVertex3fv(emdm->vertexCos[(int) eed->v1->tmp.l]);
-                               glVertex3fv(emdm->vertexCos[(int) eed->v2->tmp.l]);
-                       }
-               }
-               glEnd();
-       } else {
-               glBegin(GL_LINES);
-               for(i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
-                       if(!setDrawOptions || setDrawOptions(userData, i)) {
-                               glVertex3fv(eed->v1->co);
-                               glVertex3fv(eed->v2->co);
-                       }
-               }
-               glEnd();
-       }
-}
-static void emDM_drawEdges(DerivedMesh *dm, int UNUSED(drawLooseEdges), int UNUSED(drawAllEdges))
-{
-       emDM_drawMappedEdges(dm, NULL, NULL);
-}
-static void emDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void (*setDrawInterpOptions)(void *userData, int index, float t), void *userData) 
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditEdge *eed;
-       int i;
-
-       if (emdm->vertexCos) {
-               EditVert *eve;
-
-               for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->tmp.l = (intptr_t) i++;
-
-               glBegin(GL_LINES);
-               for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
-                       if(!setDrawOptions || setDrawOptions(userData, i)) {
-                               setDrawInterpOptions(userData, i, 0.0);
-                               glVertex3fv(emdm->vertexCos[(int) eed->v1->tmp.l]);
-                               setDrawInterpOptions(userData, i, 1.0);
-                               glVertex3fv(emdm->vertexCos[(int) eed->v2->tmp.l]);
-                       }
-               }
-               glEnd();
-       } else {
-               glBegin(GL_LINES);
-               for (i=0,eed= emdm->em->edges.first; eed; i++,eed= eed->next) {
-                       if(!setDrawOptions || setDrawOptions(userData, i)) {
-                               setDrawInterpOptions(userData, i, 0.0);
-                               glVertex3fv(eed->v1->co);
-                               setDrawInterpOptions(userData, i, 1.0);
-                               glVertex3fv(eed->v2->co);
-                       }
-               }
-               glEnd();
-       }
-}
-
-static void emDM_drawUVEdges(DerivedMesh *dm)
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditFace *efa;
-       MTFace *tf;
-
-       glBegin(GL_LINES);
-       for(efa= emdm->em->faces.first; efa; efa= efa->next) {
-               tf = CustomData_em_get(&emdm->em->fdata, efa->data, CD_MTFACE);
-
-               if(tf && !(efa->h)) {
-                       glVertex2fv(tf->uv[0]);
-                       glVertex2fv(tf->uv[1]);
-
-                       glVertex2fv(tf->uv[1]);
-                       glVertex2fv(tf->uv[2]);
-
-                       if (!efa->v4) {
-                               glVertex2fv(tf->uv[2]);
-                               glVertex2fv(tf->uv[0]);
-                       } else {
-                               glVertex2fv(tf->uv[2]);
-                               glVertex2fv(tf->uv[3]);
-                               glVertex2fv(tf->uv[3]);
-                               glVertex2fv(tf->uv[0]);
-                       }
-               }
-       }
-       glEnd();
-}
-
-static void emDM__calcFaceCent(EditFace *efa, float cent[3], float (*vertexCos)[3])
-{
-       if (vertexCos) {
-               copy_v3_v3(cent, vertexCos[(int) efa->v1->tmp.l]);
-               add_v3_v3(cent, vertexCos[(int) efa->v2->tmp.l]);
-               add_v3_v3(cent, vertexCos[(int) efa->v3->tmp.l]);
-               if (efa->v4) add_v3_v3(cent, vertexCos[(int) efa->v4->tmp.l]);
-       } else {
-               copy_v3_v3(cent, efa->v1->co);
-               add_v3_v3(cent, efa->v2->co);
-               add_v3_v3(cent, efa->v3->co);
-               if (efa->v4) add_v3_v3(cent, efa->v4->co);
-       }
-
-       if (efa->v4) {
-               mul_v3_fl(cent, 0.25f);
-       } else {
-               mul_v3_fl(cent, 0.33333333333f);
-       }
-}
-static void emDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *userData, int index, float *co, float *no), void *userData)
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditVert *eve;
-       EditFace *efa;
-       float cent[3];
-       int i;
-
-       if (emdm->vertexCos) {
-               for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->tmp.l = (intptr_t) i++;
-       }
-
-       for(i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
-               emDM__calcFaceCent(efa, cent, emdm->vertexCos);
-               func(userData, i, cent, emdm->vertexCos?emdm->faceNos[i]:efa->n);
-       }
-}
-
-/* note, material function is ignored for now. */
-static void emDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int UNUSED(useColors), int (*setMaterial)(int, void *attribs),
-                       int (*compareDrawOptions)(void *userData, int cur_index, int next_index))
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditFace *efa;
-       int i, draw;
-       const int skip_normals= !glIsEnabled(GL_LIGHTING); /* could be passed as an arg */
-
-       /* GL_ZERO is used to detect if drawing has started or not */
-       GLenum poly_prev= GL_ZERO;
-       GLenum shade_prev= GL_ZERO;
-
-       (void)setMaterial; /* unused */
-
-       /* currently unused -- each original face is handled separately */
-       (void)compareDrawOptions;
-
-       if (emdm->vertexCos) {
-               /* add direct access */
-               float (*vertexCos)[3]= emdm->vertexCos;
-               float (*vertexNos)[3]= emdm->vertexNos;
-               float (*faceNos)[3]=   emdm->faceNos;
-               EditVert *eve;
-
-               for (i=0,eve=emdm->em->verts.first; eve; eve= eve->next)
-                       eve->tmp.l = (intptr_t) i++;
-
-               for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
-                       int drawSmooth = (efa->flag & ME_SMOOTH);
-                       draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, i, &drawSmooth);
-                       if(draw) {
-                               const GLenum poly_type= efa->v4 ? GL_QUADS:GL_TRIANGLES;
-                               if (draw==2) { /* enabled with stipple */
-
-                                       if(poly_prev != GL_ZERO) glEnd();
-                                       poly_prev= GL_ZERO; /* force glBegin */
-
-                                       glEnable(GL_POLYGON_STIPPLE);
-                                       glPolygonStipple(stipple_quarttone);
-                               }
-                               
-                               if(skip_normals) {
-                                       if(poly_type != poly_prev) {
-                                               if(poly_prev != GL_ZERO) glEnd();
-                                               glBegin((poly_prev= poly_type));
-                                       }
-                                       glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-                                       if(poly_type == GL_QUADS) glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                               }
-                               else {
-                                       const GLenum shade_type= drawSmooth ? GL_SMOOTH : GL_FLAT;
-                                       if (shade_type != shade_prev) {
-                                               if(poly_prev != GL_ZERO) glEnd();
-                                               glShadeModel((shade_prev= shade_type)); /* same as below but switch shading */
-                                               glBegin((poly_prev= poly_type));
-                                       }
-                                       else if(poly_type != poly_prev) {
-                                               if(poly_prev != GL_ZERO) glEnd();
-                                               glBegin((poly_prev= poly_type));
-                                       }
-
-                                       if (!drawSmooth) {
-                                               glNormal3fv(faceNos[i]);
-                                               glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-                                               glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-                                               glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-                                               if(poly_type == GL_QUADS) glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                                       } else {
-                                               glNormal3fv(vertexNos[(int) efa->v1->tmp.l]);
-                                               glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-                                               glNormal3fv(vertexNos[(int) efa->v2->tmp.l]);
-                                               glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-                                               glNormal3fv(vertexNos[(int) efa->v3->tmp.l]);
-                                               glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-                                               if(poly_type == GL_QUADS) {
-                                                       glNormal3fv(vertexNos[(int) efa->v4->tmp.l]);
-                                                       glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                                               }
-                                       }
-                               }
-
-                               
-                               if (draw==2) {
-                                       glEnd();
-                                       poly_prev= GL_ZERO; /* force glBegin */
-
-                                       glDisable(GL_POLYGON_STIPPLE);
-                               }
-                       }
-               }
-       }
-       else {
-               for (i=0,efa= emdm->em->faces.first; efa; i++,efa= efa->next) {
-                       int drawSmooth = (efa->flag & ME_SMOOTH);
-                       draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, i, &drawSmooth);
-                       if(draw) {
-                               const GLenum poly_type= efa->v4 ? GL_QUADS:GL_TRIANGLES;
-                               if (draw==2) { /* enabled with stipple */
-
-                                       if(poly_prev != GL_ZERO) glEnd();
-                                       poly_prev= GL_ZERO; /* force glBegin */
-
-                                       glEnable(GL_POLYGON_STIPPLE);
-                                       glPolygonStipple(stipple_quarttone);
-                               }
-
-                               if(skip_normals) {
-                                       if(poly_type != poly_prev) {
-                                               if(poly_prev != GL_ZERO) glEnd();
-                                               glBegin((poly_prev= poly_type));
-                                       }
-                                       glVertex3fv(efa->v1->co);
-                                       glVertex3fv(efa->v2->co);
-                                       glVertex3fv(efa->v3->co);
-                                       if(poly_type == GL_QUADS) glVertex3fv(efa->v4->co);
-                               }
-                               else {
-                                       const GLenum shade_type= drawSmooth ? GL_SMOOTH : GL_FLAT;
-                                       if (shade_type != shade_prev) {
-                                               if(poly_prev != GL_ZERO) glEnd();
-                                               glShadeModel((shade_prev= shade_type)); /* same as below but switch shading */
-                                               glBegin((poly_prev= poly_type));
-                                       }
-                                       else if(poly_type != poly_prev) {
-                                               if(poly_prev != GL_ZERO) glEnd();
-                                               glBegin((poly_prev= poly_type));
-                                       }
-
-                                       if (!drawSmooth) {
-                                               glNormal3fv(efa->n);
-                                               glVertex3fv(efa->v1->co);
-                                               glVertex3fv(efa->v2->co);
-                                               glVertex3fv(efa->v3->co);
-                                               if(poly_type == GL_QUADS) glVertex3fv(efa->v4->co);
-                                       } else {
-                                               glNormal3fv(efa->v1->no);
-                                               glVertex3fv(efa->v1->co);
-                                               glNormal3fv(efa->v2->no);
-                                               glVertex3fv(efa->v2->co);
-                                               glNormal3fv(efa->v3->no);
-                                               glVertex3fv(efa->v3->co);
-                                               if(poly_type == GL_QUADS) {
-                                                       glNormal3fv(efa->v4->no);
-                                                       glVertex3fv(efa->v4->co);
-                                               }
-                                       }
-                               }
-
-                               
-                               if (draw==2) {
-                                       glEnd();
-                                       poly_prev= GL_ZERO;
-
-                                       glDisable(GL_POLYGON_STIPPLE);
-                               }
-                       }
-               }
-       }
-
-       /* if non zero we know a face was rendered */
-       if(poly_prev != GL_ZERO) glEnd();
-}
-
-static void emDM_drawFacesTex_common(DerivedMesh *dm,
-                          int (*drawParams)(MTFace *tface, int has_mcol, int matnr),
-                          int (*drawParamsMapped)(void *userData, int index),
-                          void *userData) 
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditMesh *em= emdm->em;
-       float (*vertexCos)[3]= emdm->vertexCos;
-       float (*vertexNos)[3]= emdm->vertexNos;
-       EditFace *efa;
-       int i;
-
-       /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
-       glShadeModel(GL_SMOOTH);
-       
-       if (vertexCos) {
-               EditVert *eve;
-
-               for (i=0,eve=em->verts.first; eve; eve= eve->next)
-                       eve->tmp.l = (intptr_t) i++;
-
-               for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) {
-                       MTFace *tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                       MCol *mcol= CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
-                       unsigned char *cp= NULL;
-                       int drawSmooth= (efa->flag & ME_SMOOTH);
-                       int flag;
-
-                       if(drawParams)
-                               flag= drawParams(tf, (mcol != NULL), efa->mat_nr);
-                       else if(drawParamsMapped)
-                               flag= drawParamsMapped(userData, i);
-                       else
-                               flag= 1;
-
-                       if(flag != 0) { /* flag 0 == the face is hidden or invisible */
-                               
-                               /* we always want smooth here since otherwise vertex colors dont interpolate */
-                               if (mcol) {
-                                       if (flag==1) {
-                                               cp= (unsigned char*)mcol;
-                                       }
-                               } else {
-                                       glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
-                               } 
-                               
-                               glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
-                               if (!drawSmooth) {
-                                       glNormal3fv(emdm->faceNos[i]);
-
-                                       if(tf) glTexCoord2fv(tf->uv[0]);
-                                       if(cp) glColor3ub(cp[3], cp[2], cp[1]);
-                                       glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-
-                                       if(tf) glTexCoord2fv(tf->uv[1]);
-                                       if(cp) glColor3ub(cp[7], cp[6], cp[5]);
-                                       glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-
-                                       if(tf) glTexCoord2fv(tf->uv[2]);
-                                       if(cp) glColor3ub(cp[11], cp[10], cp[9]);
-                                       glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-
-                                       if(efa->v4) {
-                                               if(tf) glTexCoord2fv(tf->uv[3]);
-                                               if(cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                               glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                                       }
-                               } else {
-                                       if(tf) glTexCoord2fv(tf->uv[0]);
-                                       if(cp) glColor3ub(cp[3], cp[2], cp[1]);
-                                       glNormal3fv(vertexNos[(int) efa->v1->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-
-                                       if(tf) glTexCoord2fv(tf->uv[1]);
-                                       if(cp) glColor3ub(cp[7], cp[6], cp[5]);
-                                       glNormal3fv(vertexNos[(int) efa->v2->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-
-                                       if(tf) glTexCoord2fv(tf->uv[2]);
-                                       if(cp) glColor3ub(cp[11], cp[10], cp[9]);
-                                       glNormal3fv(vertexNos[(int) efa->v3->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-
-                                       if(efa->v4) {
-                                               if(tf) glTexCoord2fv(tf->uv[3]);
-                                               if(cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                               glNormal3fv(vertexNos[(int) efa->v4->tmp.l]);
-                                               glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                                       }
-                               }
-                               glEnd();
-                       }
-               }
-       } else {
-               for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) {
-                       MTFace *tf= CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
-                       MCol *mcol= CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
-                       unsigned char *cp= NULL;
-                       int drawSmooth= (efa->flag & ME_SMOOTH);
-                       int flag;
-
-                       if(drawParams)
-                               flag= drawParams(tf, (mcol != NULL), efa->mat_nr);
-                       else if(drawParamsMapped)
-                               flag= drawParamsMapped(userData, i);
-                       else
-                               flag= 1;
-
-                       if(flag != 0) { /* flag 0 == the face is hidden or invisible */
-                               /* we always want smooth here since otherwise vertex colors dont interpolate */
-                               if (mcol) {
-                                       if (flag==1) {
-                                               cp= (unsigned char*)mcol;
-                                       }
-                               } else {
-                                       glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
-                               } 
-
-                               glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
-                               if (!drawSmooth) {
-                                       glNormal3fv(efa->n);
-
-                                       if(tf) glTexCoord2fv(tf->uv[0]);
-                                       if(cp) glColor3ub(cp[3], cp[2], cp[1]);
-                                       glVertex3fv(efa->v1->co);
-
-                                       if(tf) glTexCoord2fv(tf->uv[1]);
-                                       if(cp) glColor3ub(cp[7], cp[6], cp[5]);
-                                       glVertex3fv(efa->v2->co);
-
-                                       if(tf) glTexCoord2fv(tf->uv[2]);
-                                       if(cp) glColor3ub(cp[11], cp[10], cp[9]);
-                                       glVertex3fv(efa->v3->co);
-
-                                       if(efa->v4) {
-                                               if(tf) glTexCoord2fv(tf->uv[3]);
-                                               if(cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                               glVertex3fv(efa->v4->co);
-                                       }
-                               } else {
-                                       if(tf) glTexCoord2fv(tf->uv[0]);
-                                       if(cp) glColor3ub(cp[3], cp[2], cp[1]);
-                                       glNormal3fv(efa->v1->no);
-                                       glVertex3fv(efa->v1->co);
-
-                                       if(tf) glTexCoord2fv(tf->uv[1]);
-                                       if(cp) glColor3ub(cp[7], cp[6], cp[5]);
-                                       glNormal3fv(efa->v2->no);
-                                       glVertex3fv(efa->v2->co);
-
-                                       if(tf) glTexCoord2fv(tf->uv[2]);
-                                       if(cp) glColor3ub(cp[11], cp[10], cp[9]);
-                                       glNormal3fv(efa->v3->no);
-                                       glVertex3fv(efa->v3->co);
-
-                                       if(efa->v4) {
-                                               if(tf) glTexCoord2fv(tf->uv[3]);
-                                               if(cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                               glNormal3fv(efa->v4->no);
-                                               glVertex3fv(efa->v4->co);
-                                       }
-                               }
-                               glEnd();
-                       }
-               }
-       }
-}
-
-static void emDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, int has_mcol, int matnr))
-{
-       emDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
-}
-
-static void emDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
+void *DM_get_vert_data(DerivedMesh *dm, int index, int type)
 {
-       emDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
+       return CustomData_get(&dm->vertData, index, type);
 }
 
-static void emDM_drawMappedFacesGLSL(DerivedMesh *dm,
-                          int (*setMaterial)(int, void *attribs),
-                          int (*setDrawOptions)(void *userData, int index), void *userData) 
+void *DM_get_edge_data(DerivedMesh *dm, int index, int type)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditMesh *em= emdm->em;
-       float (*vertexCos)[3]= emdm->vertexCos;
-       float (*vertexNos)[3]= emdm->vertexNos;
-       EditVert *eve;
-       EditFace *efa;
-       DMVertexAttribs attribs= {{{0}}};
-       GPUVertexAttribs gattribs;
-       /* int tfoffset; */ /* UNUSED */
-       int i, b, matnr, new_matnr, dodraw /* , layer */ /* UNUSED */;
-
-       dodraw = 0;
-       matnr = -1;
-
-       /* layer = CustomData_get_layer_index(&em->fdata, CD_MTFACE); */ /* UNUSED */
-       /* tfoffset = (layer == -1)? -1: em->fdata.layers[layer].offset; */ /* UNUSED */
-
-       /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
-       glShadeModel(GL_SMOOTH);
-
-       for (i=0,eve=em->verts.first; eve; eve= eve->next)
-               eve->tmp.l = (intptr_t) i++;
-
-#define PASSATTRIB(efa, eve, vert) {                                                                                   \
-       if(attribs.totorco) {                                                                                                           \
-               float *orco = attribs.orco.array[eve->tmp.l];                                                   \
-               glVertexAttrib3fvARB(attribs.orco.glIndex, orco);                                               \
-       }                                                                                                                                                       \
-       for(b = 0; b < attribs.tottface; b++) {                                                                         \
-               MTFace *_tf = (MTFace*)((char*)efa->data + attribs.tface[b].emOffset);  \
-               glVertexAttrib2fvARB(attribs.tface[b].glIndex, _tf->uv[vert]);                  \
-       }                                                                                                                                                       \
-       for(b = 0; b < attribs.totmcol; b++) {                                                                          \
-               MCol *cp = (MCol*)((char*)efa->data + attribs.mcol[b].emOffset);                \
-               GLubyte col[4];                                                                                                                 \
-               col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;                             \
-               glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col);                                    \
-       }                                                                                                                                                       \
-       if(attribs.tottang) {                                                                                                           \
-               float *tang = attribs.tang.array[i*4 + vert];                                                   \
-               glVertexAttrib4fvARB(attribs.tang.glIndex, tang);                                               \
-       }                                                                                                                                                       \
-}
-
-       for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) {
-               int drawSmooth= (efa->flag & ME_SMOOTH);
-
-               if(setDrawOptions && !setDrawOptions(userData, i))
-                       continue;
-
-               new_matnr = efa->mat_nr + 1;
-               if(new_matnr != matnr) {
-                       dodraw = setMaterial(matnr = new_matnr, &gattribs);
-                       if(dodraw)
-                               DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
-               }
-
-               if(dodraw) {
-                       glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
-                       if (!drawSmooth) {
-                               if(vertexCos) glNormal3fv(emdm->faceNos[i]);
-                               else glNormal3fv(efa->n);
-
-                               PASSATTRIB(efa, efa->v1, 0);
-                               if(vertexCos) glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-                               else glVertex3fv(efa->v1->co);
-
-                               PASSATTRIB(efa, efa->v2, 1);
-                               if(vertexCos) glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-                               else glVertex3fv(efa->v2->co);
-
-                               PASSATTRIB(efa, efa->v3, 2);
-                               if(vertexCos) glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-                               else glVertex3fv(efa->v3->co);
-
-                               if(efa->v4) {
-                                       PASSATTRIB(efa, efa->v4, 3);
-                                       if(vertexCos) glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                                       else glVertex3fv(efa->v4->co);
-                               }
-                       } else {
-                               PASSATTRIB(efa, efa->v1, 0);
-                               if(vertexCos) {
-                                       glNormal3fv(vertexNos[(int) efa->v1->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-                               }
-                               else {
-                                       glNormal3fv(efa->v1->no);
-                                       glVertex3fv(efa->v1->co);
-                               }
-
-                               PASSATTRIB(efa, efa->v2, 1);
-                               if(vertexCos) {
-                                       glNormal3fv(vertexNos[(int) efa->v2->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-                               }
-                               else {
-                                       glNormal3fv(efa->v2->no);
-                                       glVertex3fv(efa->v2->co);
-                               }
-
-                               PASSATTRIB(efa, efa->v3, 2);
-                               if(vertexCos) {
-                                       glNormal3fv(vertexNos[(int) efa->v3->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-                               }
-                               else {
-                                       glNormal3fv(efa->v3->no);
-                                       glVertex3fv(efa->v3->co);
-                               }
-
-                               if(efa->v4) {
-                                       PASSATTRIB(efa, efa->v4, 3);
-                                       if(vertexCos) {
-                                               glNormal3fv(vertexNos[(int) efa->v4->tmp.l]);
-                                               glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                                       }
-                                       else {
-                                               glNormal3fv(efa->v4->no);
-                                               glVertex3fv(efa->v4->co);
-                                       }
-                               }
-                       }
-                       glEnd();
-               }
-       }
-#undef PASSATTRIB
-}
-
-static void emDM_drawFacesGLSL(DerivedMesh *dm,
-                          int (*setMaterial)(int, void *attribs))
-{
-       dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL);
-}
-
-static void emDM_drawMappedFacesMat(DerivedMesh *dm,
-                          void (*setMaterial)(void *userData, int, void *attribs),
-                          int (*setFace)(void *userData, int index), void *userData) 
-{
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditMesh *em= emdm->em;
-       float (*vertexCos)[3]= emdm->vertexCos;
-       float (*vertexNos)[3]= emdm->vertexNos;
-       EditVert *eve;
-       EditFace *efa;
-       DMVertexAttribs attribs= {{{0}}};
-       GPUVertexAttribs gattribs;
-       int i, b, matnr, new_matnr;
-
-       matnr = -1;
-
-       /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
-       glShadeModel(GL_SMOOTH);
-
-       for (i=0,eve=em->verts.first; eve; eve= eve->next)
-               eve->tmp.l = (intptr_t) i++;
-
-#define PASSATTRIB(efa, eve, vert) {                                                                                   \
-       if(attribs.totorco) {                                                                                                           \
-               float *orco = attribs.orco.array[eve->tmp.l];                                                   \
-               if(attribs.orco.glTexco)                                                                                                \
-                       glTexCoord3fv(orco);                                                                                            \
-               else                                                                                                                                    \
-                       glVertexAttrib3fvARB(attribs.orco.glIndex, orco);                                       \
-       }                                                                                                                                                       \
-       for(b = 0; b < attribs.tottface; b++) {                                                                         \
-               MTFace *_tf = (MTFace*)((char*)efa->data + attribs.tface[b].emOffset);  \
-               if(attribs.tface[b].glTexco)                                                                                    \
-                       glTexCoord2fv(_tf->uv[vert]);                                                                           \
-               else                                                                                                                                    \
-                       glVertexAttrib2fvARB(attribs.tface[b].glIndex, _tf->uv[vert]);          \
-       }                                                                                                                                                       \
-       for(b = 0; b < attribs.totmcol; b++) {                                                                          \
-               MCol *cp = (MCol*)((char*)efa->data + attribs.mcol[b].emOffset);                \
-               GLubyte col[4];                                                                                                                 \
-               col[0]= cp->b; col[1]= cp->g; col[2]= cp->r; col[3]= cp->a;                             \
-               glVertexAttrib4ubvARB(attribs.mcol[b].glIndex, col);                                    \
-       }                                                                                                                                                       \
-       if(attribs.tottang) {                                                                                                           \
-               float *tang = attribs.tang.array[i*4 + vert];                                                   \
-               glVertexAttrib4fvARB(attribs.tang.glIndex, tang);                                               \
-       }                                                                                                                                                       \
-}
-
-       for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) {
-               int drawSmooth= (efa->flag & ME_SMOOTH);
-
-               /* face hiding */
-               if(setFace && !setFace(userData, i))
-                       continue;
-
-               /* material */
-               new_matnr = efa->mat_nr + 1;
-               if(new_matnr != matnr) {
-                       setMaterial(userData, matnr = new_matnr, &gattribs);
-                       DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
-               }
-
-               /* face */
-               glBegin(efa->v4?GL_QUADS:GL_TRIANGLES);
-               if (!drawSmooth) {
-                       if(vertexCos) glNormal3fv(emdm->faceNos[i]);
-                       else glNormal3fv(efa->n);
-
-                       PASSATTRIB(efa, efa->v1, 0);
-                       if(vertexCos) glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-                       else glVertex3fv(efa->v1->co);
-
-                       PASSATTRIB(efa, efa->v2, 1);
-                       if(vertexCos) glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-                       else glVertex3fv(efa->v2->co);
-
-                       PASSATTRIB(efa, efa->v3, 2);
-                       if(vertexCos) glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-                       else glVertex3fv(efa->v3->co);
-
-                       if(efa->v4) {
-                               PASSATTRIB(efa, efa->v4, 3);
-                               if(vertexCos) glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                               else glVertex3fv(efa->v4->co);
-                       }
-               } else {
-                       PASSATTRIB(efa, efa->v1, 0);
-                       if(vertexCos) {
-                               glNormal3fv(vertexNos[(int) efa->v1->tmp.l]);
-                               glVertex3fv(vertexCos[(int) efa->v1->tmp.l]);
-                       }
-                       else {
-                               glNormal3fv(efa->v1->no);
-                               glVertex3fv(efa->v1->co);
-                       }
-
-                       PASSATTRIB(efa, efa->v2, 1);
-                       if(vertexCos) {
-                               glNormal3fv(vertexNos[(int) efa->v2->tmp.l]);
-                               glVertex3fv(vertexCos[(int) efa->v2->tmp.l]);
-                       }
-                       else {
-                               glNormal3fv(efa->v2->no);
-                               glVertex3fv(efa->v2->co);
-                       }
-
-                       PASSATTRIB(efa, efa->v3, 2);
-                       if(vertexCos) {
-                               glNormal3fv(vertexNos[(int) efa->v3->tmp.l]);
-                               glVertex3fv(vertexCos[(int) efa->v3->tmp.l]);
-                       }
-                       else {
-                               glNormal3fv(efa->v3->no);
-                               glVertex3fv(efa->v3->co);
-                       }
-
-                       if(efa->v4) {
-                               PASSATTRIB(efa, efa->v4, 3);
-                               if(vertexCos) {
-                                       glNormal3fv(vertexNos[(int) efa->v4->tmp.l]);
-                                       glVertex3fv(vertexCos[(int) efa->v4->tmp.l]);
-                               }
-                               else {
-                                       glNormal3fv(efa->v4->no);
-                                       glVertex3fv(efa->v4->co);
-                               }
-                       }
-               }
-               glEnd();
-       }
-#undef PASSATTRIB
+       return CustomData_get(&dm->edgeData, index, type);
 }
 
-static void emDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3])
+void *DM_get_tessface_data(DerivedMesh *dm, int index, int type)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditVert *eve;
-       int i;
-
-       if (emdm->em->verts.first) {
-               for (i=0,eve= emdm->em->verts.first; eve; i++,eve= eve->next) {
-                       if (emdm->vertexCos) {
-                               DO_MINMAX(emdm->vertexCos[i], min_r, max_r);
-                       } else {
-                               DO_MINMAX(eve->co, min_r, max_r);
-                       }
-               }
-       } else {
-               min_r[0] = min_r[1] = min_r[2] = max_r[0] = max_r[1] = max_r[2] = 0.0;
-       }
+       return CustomData_get(&dm->faceData, index, type);
 }
-static int emDM_getNumVerts(DerivedMesh *dm)
+
+void *DM_get_vert_data_layer(DerivedMesh *dm, int type)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
+       if(type == CD_MVERT)
+               return dm->getVertArray(dm);
 
-       return BLI_countlist(&emdm->em->verts);
+       return CustomData_get_layer(&dm->vertData, type);
 }
 
-static int emDM_getNumEdges(DerivedMesh *dm)
+void *DM_get_edge_data_layer(DerivedMesh *dm, int type)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
+       if(type == CD_MEDGE)
+               return dm->getEdgeArray(dm);
 
-       return BLI_countlist(&emdm->em->edges);
+       return CustomData_get_layer(&dm->edgeData, type);
 }
 
-static int emDM_getNumFaces(DerivedMesh *dm)
+void *DM_get_tessface_data_layer(DerivedMesh *dm, int type)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
+       if (type == CD_MFACE)
+               return dm->getTessFaceArray(dm);
 
-       return BLI_countlist(&emdm->em->faces);
+       return CustomData_get_layer(&dm->faceData, type);
 }
 
-static void emDM_getVertCos(DerivedMesh *dm, float (*cos_r)[3])
+void *DM_get_poly_data_layer(DerivedMesh *dm, int type)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditVert *eve;
-       int i;
-
-       for (i=0,eve= emdm->em->verts.first; eve; i++,eve=eve->next) {
-               if (emdm->vertexCos) {
-                       copy_v3_v3(cos_r[i], emdm->vertexCos[i]);
-               } else {
-                       copy_v3_v3(cos_r[i], eve->co);
-               }
-       }
+       return CustomData_get_layer(&dm->polyData, type);
 }
 
-static void emDM_getVert(DerivedMesh *dm, int index, MVert *vert_r)
+void DM_set_vert_data(DerivedMesh *dm, int index, int type, void *data)
 {
-       EditVert *ev = ((EditMeshDerivedMesh *)dm)->em->verts.first;
-       int i;
-
-       for(i = 0; i < index; ++i) ev = ev->next;
-
-       copy_v3_v3(vert_r->co, ev->co);
-
-       normal_float_to_short_v3(vert_r->no, ev->no);
-
-       /* TODO what to do with vert_r->flag? */
-       vert_r->bweight = (unsigned char) (ev->bweight*255.0f);
+       CustomData_set(&dm->vertData, index, type, data);
 }
 
-static void emDM_getEdge(DerivedMesh *dm, int index, MEdge *edge_r)
+void DM_set_edge_data(DerivedMesh *dm, int index, int type, void *data)
 {
-       EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
-       EditEdge *ee = em->edges.first;
-       EditVert *ev, *v1, *v2;
-       int i;
-
-       for(i = 0; i < index; ++i) ee = ee->next;
-
-       edge_r->crease = (unsigned char) (ee->crease*255.0f);
-       edge_r->bweight = (unsigned char) (ee->bweight*255.0f);
-       /* TODO what to do with edge_r->flag? */
-       edge_r->flag = ME_EDGEDRAW|ME_EDGERENDER;
-       if (ee->seam) edge_r->flag |= ME_SEAM;
-       if (ee->sharp) edge_r->flag |= ME_SHARP;
-#if 0
-       /* this needs setup of f2 field */
-       if (!ee->f2) edge_r->flag |= ME_LOOSEEDGE;
-#endif
-
-       /* goddamn, we have to search all verts to find indices */
-       v1 = ee->v1;
-       v2 = ee->v2;
-       for(i = 0, ev = em->verts.first; v1 || v2; i++, ev = ev->next) {
-               if(ev == v1) {
-                       edge_r->v1 = i;
-                       v1 = NULL;
-               }
-               if(ev == v2) {
-                       edge_r->v2 = i;
-                       v2 = NULL;
-               }
-       }
+       CustomData_set(&dm->edgeData, index, type, data);
 }
 
-static void emDM_getFace(DerivedMesh *dm, int index, MFace *face_r)
+void DM_set_tessface_data(DerivedMesh *dm, int index, int type, void *data)
 {
-       EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
-       EditFace *ef = em->faces.first;
-       EditVert *ev, *v1, *v2, *v3, *v4;
-       int i;
-
-       for(i = 0; i < index; ++i) ef = ef->next;
-
-       face_r->mat_nr = ef->mat_nr;
-       face_r->flag = ef->flag;
-
-       /* goddamn, we have to search all verts to find indices */
-       v1 = ef->v1;
-       v2 = ef->v2;
-       v3 = ef->v3;
-       v4 = ef->v4;
-       if(!v4) face_r->v4 = 0;
-
-       for(i = 0, ev = em->verts.first; v1 || v2 || v3 || v4;
-               i++, ev = ev->next) {
-               if(ev == v1) {
-                       face_r->v1 = i;
-                       v1 = NULL;
-               }
-               if(ev == v2) {
-                       face_r->v2 = i;
-                       v2 = NULL;
-               }
-               if(ev == v3) {
-                       face_r->v3 = i;
-                       v3 = NULL;
-               }
-               if(ev == v4) {
-                       face_r->v4 = i;
-                       v4 = NULL;
-               }
-       }
-
-       test_index_face(face_r, NULL, 0, ef->v4?4:3);
+       CustomData_set(&dm->faceData, index, type, data);
 }
 
-static void emDM_copyVertArray(DerivedMesh *dm, MVert *vert_r)
+void DM_copy_vert_data(DerivedMesh *source, DerivedMesh *dest,
+                                          int source_index, int dest_index, int count)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditVert *ev = emdm->em->verts.first;
-       int i;
-
-       for(i=0; ev; ev = ev->next, ++vert_r, ++i) {
-               if(emdm->vertexCos)
-                       copy_v3_v3(vert_r->co, emdm->vertexCos[i]);
-               else
-                       copy_v3_v3(vert_r->co, ev->co);
-
-               normal_float_to_short_v3(vert_r->no, ev->no);
-
-               /* TODO what to do with vert_r->flag? */
-               vert_r->flag = 0;
-               vert_r->bweight = (unsigned char) (ev->bweight*255.0f);
-       }
+       CustomData_copy_data(&source->vertData, &dest->vertData,
+                                                source_index, dest_index, count);
 }
 
-static void emDM_copyEdgeArray(DerivedMesh *dm, MEdge *edge_r)
+void DM_copy_edge_data(DerivedMesh *source, DerivedMesh *dest,
+                                          int source_index, int dest_index, int count)
 {
-       EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
-       EditEdge *ee = em->edges.first;
-       EditVert *ev;
-       int i;
-
-       /* store vertex indices in tmp union */
-       for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i)
-               ev->tmp.l = (intptr_t) i;
-
-       for( ; ee; ee = ee->next, ++edge_r) {
-               edge_r->crease = (unsigned char) (ee->crease*255.0f);
-               edge_r->bweight = (unsigned char) (ee->bweight*255.0f);
-               /* TODO what to do with edge_r->flag? */
-               edge_r->flag = ME_EDGEDRAW|ME_EDGERENDER;
-               if (ee->seam) edge_r->flag |= ME_SEAM;
-               if (ee->sharp) edge_r->flag |= ME_SHARP;
-#if 0
-               /* this needs setup of f2 field */
-               if (!ee->f2) edge_r->flag |= ME_LOOSEEDGE;
-#endif
-
-               edge_r->v1 = (int)ee->v1->tmp.l;
-               edge_r->v2 = (int)ee->v2->tmp.l;
-       }
+       CustomData_copy_data(&source->edgeData, &dest->edgeData,
+                                                source_index, dest_index, count);
 }
 
-static void emDM_copyFaceArray(DerivedMesh *dm, MFace *face_r)
+void DM_copy_tessface_data(DerivedMesh *source, DerivedMesh *dest,
+                                          int source_index, int dest_index, int count)
 {
-       EditMesh *em = ((EditMeshDerivedMesh *)dm)->em;
-       EditFace *ef = em->faces.first;
-       EditVert *ev;
-       int i;
-
-       /* store vertexes indices in tmp union */
-       for(ev = em->verts.first, i = 0; ev; ev = ev->next, ++i)
-               ev->tmp.l = (intptr_t) i;
-
-       for( ; ef; ef = ef->next, ++face_r) {
-               face_r->mat_nr = ef->mat_nr;
-               face_r->flag = ef->flag;
-
-               face_r->v1 = (int)ef->v1->tmp.l;
-               face_r->v2 = (int)ef->v2->tmp.l;
-               face_r->v3 = (int)ef->v3->tmp.l;
-               if(ef->v4) face_r->v4 = (int)ef->v4->tmp.l;
-               else face_r->v4 = 0;
-
-               test_index_face(face_r, NULL, 0, ef->v4?4:3);
-       }
+       CustomData_copy_data(&source->faceData, &dest->faceData,
+                                                source_index, dest_index, count);
 }
 
-static void *emDM_getFaceDataArray(DerivedMesh *dm, int type)
+void DM_copy_loop_data(DerivedMesh *source, DerivedMesh *dest,
+                       int source_index, int dest_index, int count)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-       EditMesh *em= emdm->em;
-       EditFace *efa;
-       char *data, *emdata;
-       void *datalayer;
-       int index, size;
-
-       datalayer = DM_get_face_data_layer(dm, type);
-       if(datalayer)
-               return datalayer;
-
-       /* layers are store per face for editmesh, we convert to a temporary
-        * data layer array in the derivedmesh when these are requested */
-       if(type == CD_MTFACE || type == CD_MCOL) {
-               index = CustomData_get_layer_index(&em->fdata, type);
-
-               if(index != -1) {
-                       /* int offset = em->fdata.layers[index].offset; */ /* UNUSED */
-                       size = CustomData_sizeof(type);
-
-                       DM_add_face_layer(dm, type, CD_CALLOC, NULL);
-                       index = CustomData_get_layer_index(&dm->faceData, type);
-                       dm->faceData.layers[index].flag |= CD_FLAG_TEMPORARY;
-
-                       data = datalayer = DM_get_face_data_layer(dm, type);
-                       for(efa=em->faces.first; efa; efa=efa->next, data+=size) {
-                               emdata = CustomData_em_get(&em->fdata, efa->data, type);
-                               memcpy(data, emdata, size);
-                       }
-               }
-       }
+       CustomData_copy_data(&source->loopData, &dest->loopData,
+                            source_index, dest_index, count);
+}
 
-       return datalayer;
+void DM_copy_poly_data(DerivedMesh *source, DerivedMesh *dest,
+                       int source_index, int dest_index, int count)
+{
+       CustomData_copy_data(&source->polyData, &dest->polyData,
+                            source_index, dest_index, count);
 }
 
-static void emDM_release(DerivedMesh *dm)
+void DM_free_vert_data(struct DerivedMesh *dm, int index, int count)
 {
-       EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
+       CustomData_free_elem(&dm->vertData, index, count);
+}
 
-       if (DM_release(dm)) {
-               if (emdm->vertexCos) {
-                       MEM_freeN(emdm->vertexCos);
-                       MEM_freeN(emdm->vertexNos);
-                       MEM_freeN(emdm->faceNos);
-               }
+void DM_free_edge_data(struct DerivedMesh *dm, int index, int count)
+{
+       CustomData_free_elem(&dm->edgeData, index, count);
+}
 
-               MEM_freeN(emdm);
-       }
+void DM_free_tessface_data(struct DerivedMesh *dm, int index, int count)
+{
+       CustomData_free_elem(&dm->faceData, index, count);
 }
 
-DerivedMesh *editmesh_get_derived(EditMesh *em, float (*vertexCos)[3])
+void DM_free_loop_data(struct DerivedMesh *dm, int index, int count)
 {
-       EditMeshDerivedMesh *emdm = MEM_callocN(sizeof(*emdm), "emdm");
+       CustomData_free_elem(&dm->loopData, index, count);
+}
 
-       DM_init(&emdm->dm, DM_TYPE_EDITMESH, BLI_countlist(&em->verts),
-                                        BLI_countlist(&em->edges), BLI_countlist(&em->faces));
+void DM_free_poly_data(struct DerivedMesh *dm, int index, int count)
+{
+       CustomData_free_elem(&dm->polyData, index, count);
+}
 
-       emdm->dm.getMinMax = emDM_getMinMax;
+void DM_interp_vert_data(DerivedMesh *source, DerivedMesh *dest,
+                                                int *src_indices, float *weights,
+                                                int count, int dest_index)
+{
+       CustomData_interp(&source->vertData, &dest->vertData, src_indices,
+                                         weights, NULL, count, dest_index);
+}
 
-       emdm->dm.getNumVerts = emDM_getNumVerts;
-       emdm->dm.getNumEdges = emDM_getNumEdges;
-       emdm->dm.getNumFaces = emDM_getNumFaces;
+void DM_interp_edge_data(DerivedMesh *source, DerivedMesh *dest,
+                                                int *src_indices,
+                                                float *weights, EdgeVertWeight *vert_weights,
+                                                int count, int dest_index)
+{
+       CustomData_interp(&source->edgeData, &dest->edgeData, src_indices,
+                                         weights, (float*)vert_weights, count, dest_index);
+}
 
-       emdm->dm.getVertCos = emDM_getVertCos;
+void DM_interp_tessface_data(DerivedMesh *source, DerivedMesh *dest,
+                                                int *src_indices,
+                                                float *weights, FaceVertWeight *vert_weights,
+                                                int count, int dest_index)
+{
+       CustomData_interp(&source->faceData, &dest->faceData, src_indices,
+                                         weights, (float*)vert_weights, count, dest_index);
+}
 
-       emdm->dm.getVert = emDM_getVert;
-       emdm->dm.getEdge = emDM_getEdge;
-       emdm->dm.getFace = emDM_getFace;
-       emdm->dm.copyVertArray = emDM_copyVertArray;
-       emdm->dm.copyEdgeArray = emDM_copyEdgeArray;
-       emdm->dm.copyFaceArray = emDM_copyFaceArray;
-       emdm->dm.getFaceDataArray = emDM_getFaceDataArray;
+void DM_swap_tessface_data(DerivedMesh *dm, int index, const int *corner_indices)
+{
+       CustomData_swap(&dm->faceData, index, corner_indices);
+}
 
-       emdm->dm.foreachMappedVert = emDM_foreachMappedVert;
-       emdm->dm.foreachMappedEdge = emDM_foreachMappedEdge;
-       emdm->dm.foreachMappedFaceCenter = emDM_foreachMappedFaceCenter;
+void DM_interp_loop_data(DerivedMesh *source, DerivedMesh *dest,
+                         int *src_indices,
+                         float *weights, int count, int dest_index)
+{
+       CustomData_interp(&source->loopData, &dest->loopData, src_indices,
+                         weights, NULL, count, dest_index);
+}
 
-       emdm->dm.drawEdges = emDM_drawEdges;
-       emdm->dm.drawMappedEdges = emDM_drawMappedEdges;
-       emdm->dm.drawMappedEdgesInterp = emDM_drawMappedEdgesInterp;
-       emdm->dm.drawMappedFaces = emDM_drawMappedFaces;
-       emdm->dm.drawMappedFacesTex = emDM_drawMappedFacesTex;
-       emdm->dm.drawMappedFacesGLSL = emDM_drawMappedFacesGLSL;
-       emdm->dm.drawFacesTex = emDM_drawFacesTex;
-       emdm->dm.drawFacesGLSL = emDM_drawFacesGLSL;
-       emdm->dm.drawMappedFacesMat = emDM_drawMappedFacesMat;
-       emdm->dm.drawUVEdges = emDM_drawUVEdges;
+void DM_interp_poly_data(DerivedMesh *source, DerivedMesh *dest,
+                         int *src_indices,
+                         float *weights, int count, int dest_index)
+{
+       CustomData_interp(&source->polyData, &dest->polyData, src_indices,
+                         weights, NULL, count, dest_index);
+}
 
-       emdm->dm.release = emDM_release;
+///
+DerivedMesh *mesh_create_derived(Mesh *me, Object *ob, float (*vertCos)[3])
+{
+       DerivedMesh *dm = CDDM_from_mesh(me, ob);
        
-       emdm->em = em;
-       emdm->vertexCos = vertexCos;
-
-       if(CustomData_has_layer(&em->vdata, CD_MDEFORMVERT)) {
-               EditVert *eve;
-               int i;
-
-               DM_add_vert_layer(&emdm->dm, CD_MDEFORMVERT, CD_CALLOC, NULL);
-
-               for(eve = em->verts.first, i = 0; eve; eve = eve->next, ++i)
-                       DM_set_vert_data(&emdm->dm, i, CD_MDEFORMVERT,
-                                                        CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT));
-       }
-
-       if(vertexCos) {
-               EditVert *eve;
-               EditFace *efa;
-               int totface = BLI_countlist(&em->faces);
-               int i;
-
-               for (i=0,eve=em->verts.first; eve; eve= eve->next)
-                       eve->tmp.l = (intptr_t) i++;
-
-               emdm->vertexNos = MEM_callocN(sizeof(*emdm->vertexNos)*i, "emdm_vno");
-               emdm->faceNos = MEM_mallocN(sizeof(*emdm->faceNos)*totface, "emdm_vno");
-
-               for(i=0, efa= em->faces.first; efa; i++, efa=efa->next) {
-                       float *v1 = vertexCos[(int) efa->v1->tmp.l];
-                       float *v2 = vertexCos[(int) efa->v2->tmp.l];
-                       float *v3 = vertexCos[(int) efa->v3->tmp.l];
-                       float *no = emdm->faceNos[i];
-                       
-                       if(efa->v4) {
-                               float *v4 = vertexCos[(int) efa->v4->tmp.l];
-
-                               normal_quad_v3( no,v1, v2, v3, v4);
-                               add_v3_v3(emdm->vertexNos[(int) efa->v4->tmp.l], no);
-                       }
-                       else {
-                               normal_tri_v3( no,v1, v2, v3);
-                       }
-
-                       add_v3_v3(emdm->vertexNos[(int) efa->v1->tmp.l], no);
-                       add_v3_v3(emdm->vertexNos[(int) efa->v2->tmp.l], no);
-                       add_v3_v3(emdm->vertexNos[(int) efa->v3->tmp.l], no);
-               }
+       if(!dm)
+               return NULL;
+       
+       if (vertCos)
+               CDDM_apply_vert_coords(dm, vertCos);
 
-               for(i=0, eve= em->verts.first; eve; i++, eve=eve->next) {
-                       float *no = emdm->vertexNos[i];
-                       /* following Mesh convention; we use vertex coordinate itself
-                        * for normal in this case */
-                       if (normalize_v3(no) == 0.0f) {
-                               normalize_v3_v3(no, vertexCos[i]);
-                       }
-               }
-       }
+       CDDM_calc_normals(dm);
 
-       return (DerivedMesh*) emdm;
+       return dm;
 }
 
 /***/
 
-DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, ModifierData *md)
+DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, 
+       ModifierData *md, int build_shapekey_layers)
 {
        Mesh *me = ob->data;
        ModifierTypeInfo *mti = modifierType_getInfo(md->type);
@@ -1640,7 +696,11 @@ DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, Modifier
        
        if (!(md->mode&eModifierMode_Realtime)) return NULL;
        if (mti->isDisabled && mti->isDisabled(md, 0)) return NULL;
-
+       
+       if (build_shapekey_layers && me->key && ob->shapenr <= BLI_countlist(&me->key->block)) {
+               key_to_mesh(BLI_findlink(&me->key->block, ob->shapenr-1), me);
+       }
+       
        if (mti->type==eModifierTypeType_OnlyDeform) {
                int numVerts;
                float (*deformedVerts)[3] = mesh_getVertexCos(me, &numVerts);
@@ -1648,9 +708,16 @@ DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, Modifier
                mti->deformVerts(md, ob, NULL, deformedVerts, numVerts, 0, 0);
                dm = mesh_create_derived(me, ob, deformedVerts);
 
+               if (build_shapekey_layers)
+                       add_shapekey_layers(dm, me, ob);
+               
                MEM_freeN(deformedVerts);
        } else {
                DerivedMesh *tdm = mesh_create_derived(me, ob, NULL);
+
+               if (build_shapekey_layers)
+                       add_shapekey_layers(tdm, me, ob);
+               
                dm = mti->applyModifier(md, ob, tdm, 0, 0);
 
                if(tdm != dm) tdm->release(tdm);
@@ -1659,22 +726,22 @@ DerivedMesh *mesh_create_derived_for_modifier(Scene *scene, Object *ob, Modifier
        return dm;
 }
 
-static float *get_editmesh_orco_verts(EditMesh *em)
+static float *get_editbmesh_orco_verts(BMEditMesh *em)
 {
-       EditVert *eve;
+       BMIter iter;
+       BMVert *eve;
        float *orco;
        int a, totvert;
 
        /* these may not really be the orco's, but it's only for preview.
         * could be solver better once, but isn't simple */
 
-       totvert= 0;
-       for(eve=em->verts.first; eve; eve=eve->next)
-               totvert++;
+       totvert= em->bm->totvert;
        
        orco = MEM_mallocN(sizeof(float)*3*totvert, "EditMesh Orco");
 
-       for(a=0, eve=em->verts.first; eve; eve=eve->next, a+=3) {
+       eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
+       for (a=0; eve; eve=BMIter_Step(&iter), a+=3) {
                copy_v3_v3(orco+a, eve->co);
        }
        
@@ -1682,8 +749,7 @@ static float *get_editmesh_orco_verts(EditMesh *em)
 }
 
 /* orco custom data layer */
-
-static void *get_orco_coords_dm(Object *ob, EditMesh *em, int layer, int *free)
+static void *get_orco_coords_dm(Object *ob, BMEditMesh *em, int layer, int *free)
 {
        *free= 0;
 
@@ -1692,7 +758,7 @@ static void *get_orco_coords_dm(Object *ob, EditMesh *em, int layer, int *free)
                *free= 1;
 
                if(em)
-                       return (float(*)[3])get_editmesh_orco_verts(em);
+                       return (float(*)[3])get_editbmesh_orco_verts(em);
                else
                        return (float(*)[3])get_mesh_orco_verts(ob);
        }
@@ -1713,13 +779,13 @@ static void *get_orco_coords_dm(Object *ob, EditMesh *em, int layer, int *free)
        return NULL;
 }
 
-static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, EditMesh *em, int layer)
+static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, BMEditMesh *em, int layer)
 {
        DerivedMesh *dm;
        float (*orco)[3];
        int free;
 
-       if(em) dm= CDDM_from_editmesh(em, me);
+       if(em) dm= CDDM_from_BMEditMesh(em, me, 0);
        else dm= CDDM_from_mesh(me, ob);
 
        orco= get_orco_coords_dm(ob, em, layer, &free);
@@ -1734,7 +800,8 @@ static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, EditMesh *em, int layer
        return dm;
 }
 
-static void add_orco_dm(Object *ob, EditMesh *em, DerivedMesh *dm, DerivedMesh *orcodm, int layer)
+static void add_orco_dm(Object *ob, BMEditMesh *em, DerivedMesh *dm,
+                                               DerivedMesh *orcodm, int layer)
 {
        float (*orco)[3], (*layerorco)[3];
        int totvert, free;
@@ -1775,31 +842,29 @@ static void add_orco_dm(Object *ob, EditMesh *em, DerivedMesh *dm, DerivedMesh *
  * 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;
        }
 }
 
@@ -1864,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);
@@ -1881,21 +946,28 @@ void vDM_ColorBand_store(ColorBand *coba)
 
 static void add_weight_mcol_dm(Object *ob, DerivedMesh *dm, int const draw_flag)
 {
-       Mesh *me = ob->data;
-       MFace *mf = me->mface;
+       // Mesh *me = ob->data; // UNUSED
+       MFace *mf = dm->getTessFaceArray(dm);
+       MLoop *mloop = dm->getLoopArray(dm), *ml;
+       MPoly *mp = dm->getPolyArray(dm);
        ColorBand *coba= stored_cb;     /* warning, not a local var */
        unsigned char *wtcol;
-       int i;
-       
+       unsigned char(*wlcol)[4] = NULL;
+       BLI_array_declare(wlcol);
+       int i, j, totface=dm->getNumTessFaces(dm), totloop;
+       int *origIndex = dm->getVertDataArray(dm, CD_ORIGINDEX);
+
        int defbase_len = BLI_countlist(&ob->defbase);
        char *defbase_sel = MEM_mallocN(defbase_len * sizeof(char), __func__);
        int selected = get_selected_defgroups(ob, defbase_sel, defbase_len);
        int unselected = defbase_len - selected;
 
-       wtcol = MEM_callocN (sizeof (unsigned char) * me->totface*4*4, "weightmap");
+       wtcol = MEM_callocN (sizeof (unsigned char) * totface*4*4, "weightmap");
        
-       memset(wtcol, 0x55, sizeof (unsigned char) * me->totface*4*4);
-       for (i=0; i<me->totface; i++, mf++) {
+       /*first add colors to the tesselation faces*/
+       memset(wtcol, 0x55, sizeof (unsigned char) * totface*4*4);
+       for (i=0; i<totface; i++, mf++) {
+               /*origindex being NULL means we're operating on original mesh data*/
                calc_weightpaint_vert_color(ob, coba, mf->v1, &wtcol[(i*4 + 0)*4], defbase_sel, selected, unselected, draw_flag);
                calc_weightpaint_vert_color(ob, coba, mf->v2, &wtcol[(i*4 + 1)*4], defbase_sel, selected, unselected, draw_flag);
                calc_weightpaint_vert_color(ob, coba, mf->v3, &wtcol[(i*4 + 2)*4], defbase_sel, selected, unselected, draw_flag);
@@ -1903,9 +975,110 @@ static void add_weight_mcol_dm(Object *ob, DerivedMesh *dm, int const draw_flag)
                        calc_weightpaint_vert_color(ob, coba, mf->v4, &wtcol[(i*4 + 3)*4], defbase_sel, selected, unselected, draw_flag);
        }
        
+       CustomData_add_layer(&dm->faceData, CD_WEIGHT_MCOL, CD_ASSIGN, wtcol, totface);
+
+       /*now add to loops, so the data can be passed through the modifier stack*/
+       totloop = 0;
+       for (i=0; i<dm->numPolyData; i++, mp++) {
+               ml = mloop + mp->loopstart;
+
+               for (j=0; j<mp->totloop; j++, ml++, totloop++) {
+                       BLI_array_growone(wlcol);
+
+                       calc_weightpaint_vert_color(ob, coba, origIndex ? origIndex[ml->v] : ml->v,
+                                                                               (unsigned char *)&wlcol[totloop], defbase_sel, selected, unselected, draw_flag);
+               }
+       }
+
        MEM_freeN(defbase_sel);
 
-       CustomData_add_layer(&dm->faceData, CD_WEIGHT_MCOL, CD_ASSIGN, wtcol, dm->numFaceData);
+       CustomData_add_layer(&dm->loopData, CD_WEIGHT_MLOOPCOL, CD_ASSIGN, wlcol, totloop);
+}
+
+
+static void shapekey_layers_to_keyblocks(DerivedMesh *dm, Mesh *me, int actshape_uid)
+{
+       KeyBlock *kb;
+       int i, j, tot;
+       
+       if (!me->key)
+               return; 
+       
+       tot = CustomData_number_of_layers(&dm->vertData, CD_SHAPEKEY);
+       for (i=0; i<tot; i++) {
+               CustomDataLayer *layer = &dm->vertData.layers[CustomData_get_layer_index_n(&dm->vertData, CD_SHAPEKEY, i)];
+               float (*cos)[3], (*kbcos)[3];
+               
+               for (kb=me->key->block.first; kb; kb=kb->next) {
+                       if (kb->uid == layer->uid)
+                               break;
+               }
+               
+               if (!kb) {
+                       kb = add_keyblock(me->key, layer->name);
+                       kb->uid = layer->uid;
+               }
+               
+               if (kb->data)
+                       MEM_freeN(kb->data);
+               
+               cos = CustomData_get_layer_n(&dm->vertData, CD_SHAPEKEY, i);
+               kb->totelem = dm->numVertData;
+
+               kb->data = kbcos = MEM_mallocN(sizeof(float)*3*kb->totelem, "kbcos DerivedMesh.c");
+               if (kb->uid == actshape_uid) {
+                       MVert *mvert = dm->getVertArray(dm);
+                       
+                       for (j=0; j<dm->numVertData; j++, kbcos++, mvert++) {
+                               copy_v3_v3(*kbcos, mvert->co);
+                       }
+               } else {
+                       for (j=0; j<kb->totelem; j++, cos++, kbcos++) {
+                               copy_v3_v3(*kbcos, *cos);
+                       }
+               }
+       }
+       
+       for (kb=me->key->block.first; kb; kb=kb->next) {
+               if (kb->totelem != dm->numVertData) {
+                       if (kb->data)
+                               MEM_freeN(kb->data);
+                       
+                       kb->totelem = dm->numVertData;
+                       kb->data = MEM_callocN(sizeof(float)*3*kb->totelem, "kb->data derivedmesh.c");
+                       fprintf(stderr, "%s: lost a shapekey layer! (bmesh internal error)\n", __func__);
+               }
+       }
+}
+
+static void add_shapekey_layers(DerivedMesh *dm, Mesh *me, Object *UNUSED(ob))
+{
+       KeyBlock *kb;
+       Key *key = me->key;
+       int a, b;
+       
+       if (!me->key)
+               return;
+       
+       if (dm->numVertData != me->totvert) {
+               printf("error in add_shapekey_layers: dm isn't the same size as me\n");
+               return;
+       }
+               
+       for (a=0, kb=key->block.first; kb; kb=kb->next, a++) {
+               float (*cos)[3] = CustomData_add_layer_named(&dm->vertData, CD_SHAPEKEY, CD_CALLOC, NULL, dm->numVertData, kb->name);
+               int ci = CustomData_get_layer_index_n(&dm->vertData, CD_SHAPEKEY, a);
+               
+               dm->vertData.layers[ci].uid = kb->uid;
+               if (kb->totelem != dm->numVertData) {
+                       printf("error in add_shapekey_layers: totelem and totvert don't match");
+                       continue;
+               }
+               
+               for (b=0; b<kb->totelem; b++, cos++) {
+                       copy_v3_v3((float *)cos, ((float*)kb->data)+b*3);
+               }
+       }
 }
 
 /* new value for useDeform -1  (hack for the gameengine):
@@ -1916,14 +1089,15 @@ static void add_weight_mcol_dm(Object *ob, DerivedMesh *dm, int const draw_flag)
 static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos)[3],
                                                                DerivedMesh **deform_r, DerivedMesh **final_r,
                                                                int useRenderParams, int useDeform,
-                                                               int needMapping, CustomDataMask dataMask, int index, int useCache)
+                                                               int needMapping, CustomDataMask dataMask, 
+                                                               int index, int useCache, int build_shapekey_layers)
 {
        Mesh *me = ob->data;
        ModifierData *firstmd, *md;
        LinkNode *datamasks, *curr;
        CustomDataMask mask, nextmask, append_mask = 0;
        float (*deformedVerts)[3] = NULL;
-       DerivedMesh *dm, *orcodm, *clothorcodm, *finaldm;
+       DerivedMesh *dm=NULL, *orcodm, *clothorcodm, *finaldm;
        int numVerts = me->totvert;
        int required_mode;
        int isPrevDeform= FALSE;
@@ -1935,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;
 
@@ -1994,7 +1170,10 @@ 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);
+                       
                        if(deformedVerts) {
                                CDDM_apply_vert_coords(*deform_r, deformedVerts);
                                CDDM_calc_normals(*deform_r);
@@ -2077,7 +1256,7 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
 
                        /* if this is not the last modifier in the stack then recalculate the normals
                         * to avoid giving bogus normals to the next modifier see: [#23673] */
-                       if(isPrevDeform &&  mti->dependsOnNormals && mti->dependsOnNormals(md)) {
+                       if(dm && isPrevDeform &&  mti->dependsOnNormals && mti->dependsOnNormals(md)) {
                                /* XXX, this covers bug #23673, but we may need normal calc for other types */
                                if(dm && dm->type == DM_TYPE_CDDM) {
                                        CDDM_apply_vert_coords(dm, deformedVerts);
@@ -2098,7 +1277,7 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                        /* apply vertex coordinates or build a DerivedMesh as necessary */
                        if(dm) {
                                if(deformedVerts) {
-                                       DerivedMesh *tdm = CDDM_copy(dm);
+                                       DerivedMesh *tdm = CDDM_copy(dm, 0);
                                        dm->release(dm);
                                        dm = tdm;
 
@@ -2108,14 +1287,14 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                        } else {
                                dm = CDDM_from_mesh(me, ob);
 
+                               if (build_shapekey_layers)
+                                       add_shapekey_layers(dm, me, ob);
+
                                if(deformedVerts) {
                                        CDDM_apply_vert_coords(dm, deformedVerts);
                                        CDDM_calc_normals(dm);
                                }
 
-                               if((dataMask & CD_MASK_WEIGHT_MCOL) && (ob->mode & OB_MODE_WEIGHT_PAINT))
-                                       add_weight_mcol_dm(ob, dm, draw_flag); 
-
                                /* Constructive modifiers need to have an origindex
                                 * otherwise they wont have anywhere to copy the data from.
                                 *
@@ -2127,12 +1306,16 @@ 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->numFaceData, 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))
+                                       add_weight_mcol_dm(ob, dm, draw_flag);
+
                        }
 
                        
@@ -2150,7 +1333,7 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
                        /* add an origspace layer if needed */
                        if(((CustomDataMask)GET_INT_FROM_POINTER(curr->link)) & CD_MASK_ORIGSPACE)
                                if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
-                                       DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
+                                       DM_add_tessface_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
 
                        ndm = mti->applyModifier(md, ob, dm, useRenderParams, useCache);
 
@@ -2223,27 +1406,47 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
         * DerivedMesh then we need to build one.
         */
        if(dm && deformedVerts) {
-               finaldm = CDDM_copy(dm);
+               finaldm = CDDM_copy(dm, 0);
 
                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 {
-               finaldm = CDDM_from_mesh(me, ob);
+               int recalc_normals= 0;
 
+               finaldm = CDDM_from_mesh(me, ob);
+               
+               if(build_shapekey_layers) {
+                       add_shapekey_layers(finaldm, me, ob);
+                       recalc_normals= 1;
+               }
+               
                if(deformedVerts) {
                        CDDM_apply_vert_coords(finaldm, deformedVerts);
-                       CDDM_calc_normals(finaldm);
+                       recalc_normals= 1;
                }
 
+               if(recalc_normals)
+                       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= FALSE;
        }
 
        /* add an orco layer if needed */
@@ -2266,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)
@@ -2279,21 +1500,24 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
        BLI_linklist_free(datamasks, NULL);
 }
 
-float (*editmesh_get_vertex_cos(EditMesh *em, int *numVerts_r))[3]
+float (*editbmesh_get_vertex_cos(BMEditMesh *em, int *numVerts_r))[3]
 {
-       int i, numVerts = *numVerts_r = BLI_countlist(&em->verts);
+       int i, numVerts = *numVerts_r = em->bm->totvert;
        float (*cos)[3];
-       EditVert *eve;
+       BMIter iter;
+       BMVert *eve;
 
-       cos = MEM_mallocN(sizeof(*cos)*numVerts, "vertexcos");
-       for (i=0,eve=em->verts.first; i<numVerts; i++,eve=eve->next) {
+       cos = MEM_mallocN(sizeof(float)*3*numVerts, "vertexcos");
+
+       eve = BMIter_New(&iter, em->bm, BM_VERTS_OF_MESH, NULL);
+       for (i=0; eve; eve=BMIter_Step(&iter), i++) {
                copy_v3_v3(cos[i], eve->co);
        }
 
        return cos;
 }
 
-int editmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm)
+int editbmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm)
 {
        ModifierTypeInfo *mti = modifierType_getInfo(md->type);
        int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
@@ -2307,14 +1531,14 @@ int editmesh_modifier_is_enabled(Scene *scene, ModifierData *md, DerivedMesh *dm
        return 1;
 }
 
-static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, DerivedMesh **cage_r,
+static void editbmesh_calc_modifiers(Scene *scene, Object *ob, BMEditMesh *em, DerivedMesh **cage_r,
                                                                        DerivedMesh **final_r,
                                                                        CustomDataMask dataMask)
 {
        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;
@@ -2322,10 +1546,9 @@ static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, Deri
        modifiers_clearErrors(ob);
 
        if(cage_r && cageIndex == -1) {
-               *cage_r = editmesh_get_derived(em, NULL);
+               *cage_r = getEditDerivedBMesh(em, ob, NULL);
        }
 
-       dm = NULL;
        md = modifiers_getVirtualModifierList(ob);
 
        datamasks = modifiers_calcDataMasks(scene, ob, md, dataMask, required_mode);
@@ -2336,7 +1559,7 @@ static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, Deri
 
                md->scene= scene;
                
-               if(!editmesh_modifier_is_enabled(scene, md, dm))
+               if(!editbmesh_modifier_is_enabled(scene, md, dm))
                        continue;
 
                /* add an orco layer if needed by this modifier */
@@ -2364,7 +1587,7 @@ static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, Deri
                                                MEM_mallocN(sizeof(*deformedVerts) * numVerts, "dfmv");
                                        dm->getVertCos(dm, deformedVerts);
                                } else {
-                                       deformedVerts = editmesh_get_vertex_cos(em, &numVerts);
+                                       deformedVerts = editbmesh_get_vertex_cos(em, &numVerts);
                                }
                        }
 
@@ -2377,7 +1600,7 @@ static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, Deri
                        /* apply vertex coordinates or build a DerivedMesh as necessary */
                        if(dm) {
                                if(deformedVerts) {
-                                       DerivedMesh *tdm = CDDM_copy(dm);
+                                       DerivedMesh *tdm = CDDM_copy(dm, 0);
                                        if(!(cage_r && dm == *cage_r)) dm->release(dm);
                                        dm = tdm;
 
@@ -2386,11 +1609,11 @@ static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, Deri
                                } else if(cage_r && dm == *cage_r) {
                                        /* dm may be changed by this modifier, so we need to copy it
                                         */
-                                       dm = CDDM_copy(dm);
+                                       dm = CDDM_copy(dm, 0);
                                }
 
                        } else {
-                               dm = CDDM_from_editmesh(em, ob->data);
+                               dm = CDDM_from_BMEditMesh(em, ob->data, 0);
 
                                if(deformedVerts) {
                                        CDDM_apply_vert_coords(dm, deformedVerts);
@@ -2426,7 +1649,7 @@ static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, Deri
 
                        if(mask & CD_MASK_ORIGSPACE)
                                if(!CustomData_has_layer(&dm->faceData, CD_ORIGSPACE))
-                                       DM_add_face_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
+                                       DM_add_tessface_layer(dm, CD_ORIGSPACE, CD_DEFAULT, NULL);
                        
                        if (mti->applyModifierEM)
                                ndm = mti->applyModifierEM(md, ob, em, dm);
@@ -2448,13 +1671,13 @@ static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, Deri
 
                if(cage_r && i == cageIndex) {
                        if(dm && deformedVerts) {
-                               *cage_r = CDDM_copy(dm);
+                               *cage_r = CDDM_copy(dm, 0);
                                CDDM_apply_vert_coords(*cage_r, deformedVerts);
                        } else if(dm) {
                                *cage_r = dm;
                        } else {
                                *cage_r =
-                                       editmesh_get_derived(em,
+                                       getEditDerivedBMesh(em, ob,
                                                deformedVerts ? MEM_dupallocN(deformedVerts) : NULL);
                        }
                }
@@ -2467,21 +1690,24 @@ static void editmesh_calc_modifiers(Scene *scene, Object *ob, EditMesh *em, Deri
         * then we need to build one.
         */
        if(dm && deformedVerts) {
-               *final_r = CDDM_copy(dm);
+               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 = editmesh_get_derived(em, 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);
@@ -2525,18 +1751,19 @@ static void clear_mesh_caches(Object *ob)
        }
 }
 
-static void mesh_build_data(Scene *scene, Object *ob, CustomDataMask dataMask)
+static void mesh_build_data(Scene *scene, Object *ob, CustomDataMask dataMask,
+       int build_shapekey_layers)
 {
        Object *obact = scene->basact?scene->basact->object:NULL;
-       int editing = paint_facesel_test(ob) || paint_vertsel_test(ob);/* paint_vertsel_test */
+       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);
 
        mesh_calc_modifiers(scene, ob, NULL, &ob->derivedDeform,
                                                &ob->derivedFinal, 0, 1,
-                                               needMapping, dataMask, -1, 1);
+                                               needMapping, dataMask, -1, 1, build_shapekey_layers);
 
        DM_set_object_boundbox (ob, ob->derivedFinal);
 
@@ -2545,7 +1772,7 @@ static void mesh_build_data(Scene *scene, Object *ob, CustomDataMask dataMask)
        ob->lastDataMask = dataMask;
 }
 
-static void editmesh_build_data(Scene *scene, Object *obedit, EditMesh *em, CustomDataMask dataMask)
+static void editbmesh_build_data(Scene *scene, Object *obedit, BMEditMesh *em, CustomDataMask dataMask)
 {
        clear_mesh_caches(obedit);
 
@@ -2562,7 +1789,7 @@ static void editmesh_build_data(Scene *scene, Object *obedit, EditMesh *em, Cust
                em->derivedCage = NULL;
        }
 
-       editmesh_calc_modifiers(scene, obedit, em, &em->derivedCage, &em->derivedFinal, dataMask);
+       editbmesh_calc_modifiers(scene, obedit, em, &em->derivedCage, &em->derivedFinal, dataMask);
        DM_set_object_boundbox (obedit, em->derivedFinal);
 
        em->lastDataMask = dataMask;
@@ -2570,12 +1797,13 @@ static void editmesh_build_data(Scene *scene, Object *obedit, EditMesh *em, Cust
        em->derivedCage->needsFree = 0;
 }
 
-void makeDerivedMesh(Scene *scene, Object *ob, EditMesh *em, CustomDataMask dataMask)
+void makeDerivedMesh(Scene *scene, Object *ob, BMEditMesh *em,
+       CustomDataMask dataMask, int build_shapekey_layers)
 {
        if (em) {
-               editmesh_build_data(scene, ob, em, dataMask);
+               editbmesh_build_data(scene, ob, em, dataMask);
        } else {
-               mesh_build_data(scene, ob, dataMask);
+               mesh_build_data(scene, ob, dataMask, build_shapekey_layers);
        }
 }
 
@@ -2587,7 +1815,7 @@ DerivedMesh *mesh_get_derived_final(Scene *scene, Object *ob, CustomDataMask dat
         * the data we need, rebuild the derived mesh
         */
        if(!ob->derivedFinal || (dataMask & ob->lastDataMask) != dataMask)
-               mesh_build_data(scene, ob, dataMask);
+               mesh_build_data(scene, ob, dataMask, 0);
 
        return ob->derivedFinal;
 }
@@ -2598,7 +1826,7 @@ DerivedMesh *mesh_get_derived_deform(Scene *scene, Object *ob, CustomDataMask da
         * the data we need, rebuild the derived mesh
         */
        if(!ob->derivedDeform || (dataMask & ob->lastDataMask) != dataMask)
-               mesh_build_data(scene, ob, dataMask);
+               mesh_build_data(scene, ob, dataMask, 0);
 
        return ob->derivedDeform;
 }
@@ -2607,7 +1835,7 @@ DerivedMesh *mesh_create_derived_render(Scene *scene, Object *ob, CustomDataMask
 {
        DerivedMesh *final;
        
-       mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, -1, 0);
+       mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, -1, 0, 0);
 
        return final;
 }
@@ -2616,7 +1844,7 @@ DerivedMesh *mesh_create_derived_index_render(Scene *scene, Object *ob, CustomDa
 {
        DerivedMesh *final;
        
-       mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, index, 0);
+       mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 1, 1, 0, dataMask, index, 0, 0);
 
        return final;
 }
@@ -2625,7 +1853,7 @@ DerivedMesh *mesh_create_derived_view(Scene *scene, Object *ob, CustomDataMask d
 {
        DerivedMesh *final;
 
-       mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 0, 1, 0, dataMask, -1, 0);
+       mesh_calc_modifiers(scene, ob, NULL, NULL, &final, 0, 1, 0, dataMask, -1, 0, 0);
 
        return final;
 }
@@ -2635,7 +1863,7 @@ DerivedMesh *mesh_create_derived_no_deform(Scene *scene, Object *ob, float (*ver
 {
        DerivedMesh *final;
        
-       mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, 0, 0, dataMask, -1, 0);
+       mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, 0, 0, dataMask, -1, 0, 0);
 
        return final;
 }
@@ -2645,7 +1873,7 @@ DerivedMesh *mesh_create_derived_no_virtual(Scene *scene, Object *ob, float (*ve
 {
        DerivedMesh *final;
        
-       mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 0, dataMask, -1, 0);
+       mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 0, dataMask, -1, 0, 0);
 
        return final;
 }
@@ -2655,7 +1883,7 @@ DerivedMesh *mesh_create_derived_physics(Scene *scene, Object *ob, float (*vertC
 {
        DerivedMesh *final;
        
-       mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 1, dataMask, -1, 0);
+       mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 0, -1, 1, dataMask, -1, 0, 0);
 
        return final;
 }
@@ -2666,14 +1894,14 @@ DerivedMesh *mesh_create_derived_no_deform_render(Scene *scene, Object *ob,
 {
        DerivedMesh *final;
 
-       mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 1, 0, 0, dataMask, -1, 0);
+       mesh_calc_modifiers(scene, ob, vertCos, NULL, &final, 1, 0, 0, dataMask, -1, 0, 0);
 
        return final;
 }
 
 /***/
 
-DerivedMesh *editmesh_get_derived_cage_and_final(Scene *scene, Object *obedit, EditMesh *em, DerivedMesh **final_r,
+DerivedMesh *editbmesh_get_derived_cage_and_final(Scene *scene, Object *obedit, BMEditMesh *em, DerivedMesh **final_r,
                                                                                                 CustomDataMask dataMask)
 {
        /* if there's no derived mesh or the last data mask used doesn't include
@@ -2681,27 +1909,27 @@ DerivedMesh *editmesh_get_derived_cage_and_final(Scene *scene, Object *obedit, E
         */
        if(!em->derivedCage ||
           (em->lastDataMask & dataMask) != dataMask)
-               editmesh_build_data(scene, obedit, em, dataMask);
+               editbmesh_build_data(scene, obedit, em, dataMask);
 
        *final_r = em->derivedFinal;
        return em->derivedCage;
 }
 
-DerivedMesh *editmesh_get_derived_cage(Scene *scene, Object *obedit, EditMesh *em, CustomDataMask dataMask)
+DerivedMesh *editbmesh_get_derived_cage(Scene *scene, Object *obedit, BMEditMesh *em, CustomDataMask dataMask)
 {
        /* if there's no derived mesh or the last data mask used doesn't include
         * the data we need, rebuild the derived mesh
         */
        if(!em->derivedCage ||
           (em->lastDataMask & dataMask) != dataMask)
-               editmesh_build_data(scene, obedit, em, dataMask);
+               editbmesh_build_data(scene, obedit, em, dataMask);
 
        return em->derivedCage;
 }
 
-DerivedMesh *editmesh_get_derived_base(Object *UNUSED(obedit), EditMesh *em)
+DerivedMesh *editbmesh_get_derived_base(Object *obedit, BMEditMesh *em)
 {
-       return editmesh_get_derived(em, NULL);
+       return getEditDerivedBMesh(em, obedit, NULL);
 }
 
 
@@ -2741,7 +1969,7 @@ float *mesh_get_mapped_verts_nors(Scene *scene, Object *ob)
        if(ob->type!=OB_MESH || me->totvert==0)
                return NULL;
        
-       dm= mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
+       dm= mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH|CD_MASK_ORIGINDEX);
        vertexcosnos= MEM_callocN(6*sizeof(float)*me->totvert, "vertexcosnos map");
        
        if(dm->foreachMappedVert) {
@@ -2771,7 +1999,7 @@ typedef struct
        MVert * mvert;          // vertices & normals
        float (*orco)[3];
        float (*tangent)[4];    // destination
-       int numFaces;
+       int numTessFaces;
 
 } SGLSLMeshToTangent;
 
@@ -2781,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)
@@ -2870,15 +2098,15 @@ void DM_add_tangent_layer(DerivedMesh *dm)
        if(CustomData_get_layer_index(&dm->faceData, CD_TANGENT) != -1)
                return;
 
-       nors = dm->getFaceDataArray(dm, CD_NORMAL);
+       nors = dm->getTessFaceDataArray(dm, CD_NORMAL);
 
        /* check we have all the needed layers */
        totvert= dm->getNumVerts(dm);
-       totface= dm->getNumFaces(dm);
+       totface= dm->getNumTessFaces(dm);
 
        mvert= dm->getVertArray(dm);
-       mface= dm->getFaceArray(dm);
-       mtface= dm->getFaceDataArray(dm, CD_MTFACE);
+       mface= dm->getTessFaceArray(dm);
+       mtface= dm->getTessFaceDataArray(dm, CD_MTFACE);
 
        if(!mtface) {
                orco= dm->getVertDataArray(dm, CD_ORCO);
@@ -2887,8 +2115,8 @@ void DM_add_tangent_layer(DerivedMesh *dm)
        }
        
        /* create tangent layer */
-       DM_add_face_layer(dm, CD_TANGENT, CD_CALLOC, NULL);
-       tangent= DM_get_face_data_layer(dm, CD_TANGENT);
+       DM_add_tessface_layer(dm, CD_TANGENT, CD_CALLOC, NULL);
+       tangent= DM_get_tessface_data_layer(dm, CD_TANGENT);
        
        /* allocate some space */
        arena= BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "tangent layer arena");
@@ -2908,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;
@@ -3009,15 +2237,8 @@ void DM_vertex_attributes_from_gpu(DerivedMesh *dm, GPUVertexAttribs *gattribs,
        memset(attribs, 0, sizeof(DMVertexAttribs));
 
        vdata = &dm->vertData;
-       fdata = &dm->faceData;
-
-       /* ugly hack, editmesh derivedmesh doesn't copy face data, this way we
-        * can use offsets instead */
-       if(dm->release == emDM_release)
-               tfdata = &((EditMeshDerivedMesh*)dm)->em->fdata;
-       else
-               tfdata = fdata;
-
+       fdata = tfdata = dm->getTessFaceDataLayout(dm);
+       
        /* add a tangent layer if necessary */
        for(b = 0; b < gattribs->totlayer; b++)
                if(gattribs->layer[b].type == CD_TANGENT)
@@ -3039,8 +2260,30 @@ 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;
-                               attribs->tface[a].glTexco = gattribs->layer[b].gltexco;
+                               /* 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->getPolyDataLayout(dm);
+                               
+                               if(gattribs->layer[b].name[0])
+                                       player = CustomData_get_named_layer_index(pdata, CD_MTEXPOLY,
+                                               gattribs->layer[b].name);
+                               else
+                                       player = CustomData_get_active_layer_index(pdata, CD_MTEXPOLY);
+                               
+                               if (player != -1) {
+                                       a = attribs->tottface++;
+       
+                                       attribs->tface[a].array = NULL;
+                                       attribs->tface[a].emOffset = pdata->layers[layer].offset;
+                                       attribs->tface[a].glIndex = gattribs->layer[b].glindex;
+                                       attribs->tface[a].glTexco = gattribs->layer[b].gltexco;
+                                       
+                               }
                        }
+                       */
                }
                else if(gattribs->layer[b].type == CD_MCOL) {
                        /* vertex colors */
@@ -3143,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) {
@@ -3170,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);
 }
@@ -3191,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;
@@ -3199,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) {