NLA SoC: Start of 'Meta' Strips
[blender.git] / source / blender / blenkernel / intern / subsurf_ccg.c
index 5f7b656d45acfaada717f1a23190983067146164..15969fc9ab91b810b26735afb4f743a861e46018 100644 (file)
@@ -1,15 +1,12 @@
 /**
  * $Id$
  *
- * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version. The Blender
- * Foundation also sells licenses for use in proprietary software under
- * the Blender License.  See http://www.blender.org/BL/ for information
- * about this.
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -27,7 +24,7 @@
  *
  * Contributor(s): none yet.
  *
- * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ * ***** END GPL LICENSE BLOCK *****
  */
 
 #include <stdlib.h>
 #include "DNA_meshdata_types.h"
 #include "DNA_modifier_types.h"
 #include "DNA_object_types.h"
+#include "DNA_scene_types.h"
 
-#include "BKE_bad_level_calls.h"
 #include "BKE_cdderivedmesh.h"
 #include "BKE_customdata.h"
+#include "BKE_DerivedMesh.h"
+#include "BKE_displist.h"
 #include "BKE_utildefines.h"
 #include "BKE_global.h"
 #include "BKE_mesh.h"
+#include "BKE_multires.h"
+#include "BKE_scene.h"
 #include "BKE_subsurf.h"
-#include "BKE_displist.h"
-#include "BKE_DerivedMesh.h"
 
 #include "BLI_blenlib.h"
 #include "BLI_editVert.h"
 
 #include "BIF_gl.h"
 
+#include "GPU_draw.h"
+#include "GPU_extensions.h"
+#include "GPU_material.h"
+
 #include "CCGSubSurf.h"
 
 typedef struct _VertData {
@@ -75,8 +78,6 @@ struct CCGDerivedMesh {
        CCGSubSurf *ss;
        int drawInteriorEdges, useSubsurfUv;
 
-       Mesh *me;
-
        struct {int startVert; CCGVert *vert;} *vertMap;
        struct {int startVert; int startEdge; CCGEdge *edge;} *edgeMap;
        struct {int startVert; int startEdge;
@@ -85,9 +86,9 @@ struct CCGDerivedMesh {
 
 typedef struct CCGDerivedMesh CCGDerivedMesh;
 
-static int ccgDM_getVertMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGVert *v);
-static int ccgDM_getEdgeMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGEdge *e);
-static int ccgDM_getFaceMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGFace *f);
+static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v);
+static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e);
+static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f);
 
 ///
 
@@ -160,8 +161,8 @@ static CCGSubSurf *_getSubSurf(CCGSubSurf *prevSS, int subdivLevels, int useAgin
 }
 
 static int getEdgeIndex(CCGSubSurf *ss, CCGEdge *e, int x, int edgeSize) {
-       CCGVert *v0 = ccgSubSurf_getEdgeVert0(ss, e);
-       CCGVert *v1 = ccgSubSurf_getEdgeVert1(ss, e);
+       CCGVert *v0 = ccgSubSurf_getEdgeVert0(e);
+       CCGVert *v1 = ccgSubSurf_getEdgeVert1(e);
        int v0idx = *((int*) ccgSubSurf_getVertUserData(ss, v0));
        int v1idx = *((int*) ccgSubSurf_getVertUserData(ss, v1));
        int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e));
@@ -176,7 +177,7 @@ static int getEdgeIndex(CCGSubSurf *ss, CCGEdge *e, int x, int edgeSize) {
 }
 static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edgeSize, int gridSize) {
        int faceBase = *((int*) ccgSubSurf_getFaceUserData(ss, f));
-       int numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+       int numVerts = ccgSubSurf_getFaceNumVerts(f);
 
        if (x==gridSize-1 && y==gridSize-1) {
                CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
@@ -185,7 +186,7 @@ static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edg
                CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
                CCGEdge *e = ccgSubSurf_getFaceEdge(ss, f, S);
                int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e));
-               if (v==ccgSubSurf_getEdgeVert0(ss, e)) {
+               if (v==ccgSubSurf_getEdgeVert0(e)) {
                        return edgeBase + (gridSize-1-y)-1;
                } else {
                        return edgeBase + (edgeSize-2-1)-((gridSize-1-y)-1);
@@ -194,7 +195,7 @@ static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edg
                CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
                CCGEdge *e = ccgSubSurf_getFaceEdge(ss, f, (S+numVerts-1)%numVerts);
                int edgeBase = *((int*) ccgSubSurf_getEdgeUserData(ss, e));
-               if (v==ccgSubSurf_getEdgeVert0(ss, e)) {
+               if (v==ccgSubSurf_getEdgeVert0(e)) {
                        return edgeBase + (gridSize-1-x)-1;
                } else {
                        return edgeBase + (edgeSize-2-1)-((gridSize-1-x)-1);
@@ -211,42 +212,6 @@ static int getFaceIndex(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edg
        }
 }
 
-static float *getFaceUV(CCGSubSurf *ss, CCGFace *f, int S, int x, int y, int edgeSize, int gridSize)
-{
-       int numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
-
-       if (x==gridSize-1 && y==gridSize-1) {
-               CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
-               return ccgSubSurf_getVertData(ss, v);
-       }
-       else if (x==gridSize-1) {
-               CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
-               CCGEdge *e = ccgSubSurf_getFaceEdge(ss, f, S);
-
-               if (v==ccgSubSurf_getEdgeVert0(ss, e))
-                       return ccgSubSurf_getEdgeData(ss, e, gridSize-1-y);
-               else
-                       return ccgSubSurf_getEdgeData(ss, e, (edgeSize-2-1)-(gridSize-1-y-2));
-       }
-       else if (y==gridSize-1) {
-               CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
-               CCGEdge *e = ccgSubSurf_getFaceEdge(ss, f, (S+numVerts-1)%numVerts);
-
-               if (v==ccgSubSurf_getEdgeVert0(ss, e))
-                       return ccgSubSurf_getEdgeData(ss, e, gridSize-1-x);
-               else
-                       return ccgSubSurf_getEdgeData(ss, e, (edgeSize-2-1)-(gridSize-1-x-2));
-       }
-       else if (x==0 && y==0)
-               return ccgSubSurf_getFaceCenterData(ss, f);
-       else if (x==0)
-               return ccgSubSurf_getFaceGridEdgeData(ss, f, (S+numVerts-1)%numVerts, y);
-       else if (y==0)
-               return ccgSubSurf_getFaceGridEdgeData(ss, f, S, x);
-       else
-               return ccgSubSurf_getFaceGridData(ss, f, S, x, y);
-}
-
 static void get_face_uv_map_vert(UvVertMap *vmap, struct MFace *mf, int fi, CCGVertHDL *fverts) {
        unsigned int *fv = &mf->v1;
        UvMapVert *v, *nv;
@@ -260,14 +225,13 @@ static void get_face_uv_map_vert(UvVertMap *vmap, struct MFace *mf, int fi, CCGV
                                break;
                }
 
-               fverts[j]= (CCGVertHDL)(nv->f*4 + nv->tfindex);
+               fverts[j]= SET_INT_IN_POINTER(nv->f*4 + nv->tfindex);
        }
 }
 
-static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm) {
+static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm, MTFace *tface) {
        MFace *mface = dm->getFaceArray(dm);
        MVert *mvert = dm->getVertArray(dm);
-       MTFace *tface = dm->getFaceDataArray(dm, CD_MTFACE);
        int totvert = dm->getNumVerts(dm);
        int totface = dm->getNumFaces(dm);
        int i, j, seam;
@@ -278,7 +242,7 @@ static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm)
        EdgeHash *ehash;
        float creaseFactor = (float)ccgSubSurf_getSubdivisionLevels(ss);
 
-       limit[0]= limit[1]= 0.0001f;
+       limit[0]= limit[1]= STD_UV_CONNECT_LIMIT;
        vmap= make_uv_vert_map(mface, tface, totface, totvert, 0, limit);
        if (!vmap)
                return 0;
@@ -299,7 +263,7 @@ static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm)
                for (v=get_uv_map_vert(vmap, i); v; v=v->next) {
                        if (v->separate) {
                                CCGVert *ssv;
-                               CCGVertHDL vhdl = (CCGVertHDL)(v->f*4 + v->tfindex);
+                               CCGVertHDL vhdl = SET_INT_IN_POINTER(v->f*4 + v->tfindex);
                                float uv[3];
 
                                uv[0]= (tface+v->f)->uv[v->tfindex][0];
@@ -317,26 +281,26 @@ static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm)
        for (i=0; i<totface; i++) {
                MFace *mf = &((MFace*) mface)[i];
                int nverts= mf->v4? 4: 3;
-               CCGFace *origf= ccgSubSurf_getFace(origss, (CCGFaceHDL)i);
+               CCGFace *origf= ccgSubSurf_getFace(origss, SET_INT_IN_POINTER(i));
                unsigned int *fv = &mf->v1;
 
                get_face_uv_map_vert(vmap, mf, i, fverts);
 
                for (j=0; j<nverts; j++) {
-                       int v0 = (int)fverts[j];
-                       int v1 = (int)fverts[(j+1)%nverts];
+                       int v0 = GET_INT_FROM_POINTER(fverts[j]);
+                       int v1 = GET_INT_FROM_POINTER(fverts[(j+1)%nverts]);
                        MVert *mv0 = mvert + *(fv+j);
                        MVert *mv1 = mvert + *(fv+((j+1)%nverts));
 
                        if (!BLI_edgehash_haskey(ehash, v0, v1)) {
                                CCGEdge *e, *orige= ccgSubSurf_getFaceEdge(origss, origf, j);
-                               CCGEdgeHDL ehdl= (CCGEdgeHDL)(i*4 + j);
+                               CCGEdgeHDL ehdl= SET_INT_IN_POINTER(i*4 + j);
                                float crease;
 
                                if ((mv0->flag&mv1->flag) & ME_VERT_MERGED)
                                        crease = creaseFactor;
                                else
-                                       crease = ccgSubSurf_getEdgeCrease(origss, orige);
+                                       crease = ccgSubSurf_getEdgeCrease(orige);
 
                                ccgSubSurf_syncEdge(ss, ehdl, fverts[j], fverts[(j+1)%nverts], crease, &e);
                                BLI_edgehash_insert(ehash, v0, v1, NULL);
@@ -353,7 +317,7 @@ static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm)
                CCGFace *f;
 
                get_face_uv_map_vert(vmap, mf, i, fverts);
-               ccgSubSurf_syncFace(ss, (CCGFaceHDL)i, nverts, fverts, &f);
+               ccgSubSurf_syncFace(ss, SET_INT_IN_POINTER(i), nverts, fverts, &f);
        }
 
        free_uv_vert_map(vmap);
@@ -362,16 +326,85 @@ static int ss_sync_from_uv(CCGSubSurf *ss, CCGSubSurf *origss, DerivedMesh *dm)
        return 1;
 }
 
+static void set_subsurf_uv(CCGSubSurf *ss, DerivedMesh *dm, DerivedMesh *result, int n)
+{
+       CCGSubSurf *uvss;
+       CCGFace **faceMap;
+       MTFace *tf;
+       CCGFaceIterator *fi;
+       int index, gridSize, gridFaces, edgeSize, totface, x, y, S;
+       MTFace *dmtface = CustomData_get_layer_n(&dm->faceData, CD_MTFACE, n);
+       MTFace *tface = CustomData_get_layer_n(&result->faceData, CD_MTFACE, n);
+
+       if(!dmtface || !tface)
+               return;
+
+       /* create a CCGSubsurf from uv's */
+       uvss = _getSubSurf(NULL, ccgSubSurf_getSubdivisionLevels(ss), 0, 1, 0);
+
+       if(!ss_sync_from_uv(uvss, ss, dm, dmtface)) {
+               ccgSubSurf_free(uvss);
+               return;
+       }
+
+       /* get some info from CCGSubsurf */
+       totface = ccgSubSurf_getNumFaces(uvss);
+       edgeSize = ccgSubSurf_getEdgeSize(uvss);
+       gridSize = ccgSubSurf_getGridSize(uvss);
+       gridFaces = gridSize - 1;
+
+       /* make a map from original faces to CCGFaces */
+       faceMap = MEM_mallocN(totface*sizeof(*faceMap), "facemapuv");
+
+       fi = ccgSubSurf_getFaceIterator(uvss);
+       for(; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
+               CCGFace *f = ccgFaceIterator_getCurrent(fi);
+               faceMap[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(uvss, f))] = f;
+       }
+       ccgFaceIterator_free(fi);
+
+       /* load coordinates from uvss into tface */
+       tf= tface;
+
+       for(index = 0; index < totface; index++) {
+               CCGFace *f = faceMap[index];
+               int numVerts = ccgSubSurf_getFaceNumVerts(f);
+
+               for (S=0; S<numVerts; S++) {
+                       VertData *faceGridData= ccgSubSurf_getFaceGridDataArray(uvss, f, S);
+
+                       for(y = 0; y < gridFaces; y++) {
+                               for(x = 0; x < gridFaces; x++) {
+                                       float *a = faceGridData[(y + 0)*gridSize + x + 0].co;
+                                       float *b = faceGridData[(y + 0)*gridSize + x + 1].co;
+                                       float *c = faceGridData[(y + 1)*gridSize + x + 1].co;
+                                       float *d = faceGridData[(y + 1)*gridSize + x + 0].co;
+
+                                       tf->uv[0][0] = a[0]; tf->uv[0][1] = a[1];
+                                       tf->uv[1][0] = d[0]; tf->uv[1][1] = d[1];
+                                       tf->uv[2][0] = c[0]; tf->uv[2][1] = c[1];
+                                       tf->uv[3][0] = b[0]; tf->uv[3][1] = b[1];
+
+                                       tf++;
+                               }
+                       }
+               }
+       }
+
+       ccgSubSurf_free(uvss);
+       MEM_freeN(faceMap);
+}
+
 #if 0
 static unsigned int ss_getEdgeFlags(CCGSubSurf *ss, CCGEdge *e, int ssFromEditmesh, DispListMesh *dlm, MEdge *medge, MTFace *tface)
 {
        unsigned int flags = 0;
-       int N = ccgSubSurf_getEdgeNumFaces(ss, e);
+       int N = ccgSubSurf_getEdgeNumFaces(e);
 
        if (!N) flags |= ME_LOOSEEDGE;
 
        if (ssFromEditmesh) {
-               EditEdge *eed = ccgSubSurf_getEdgeEdgeHandle(ss, e);
+               EditEdge *eed = ccgSubSurf_getEdgeEdgeHandle(e);
 
                flags |= ME_EDGEDRAW|ME_EDGERENDER;
                if (eed->seam) {
@@ -393,7 +426,7 @@ static unsigned int ss_getEdgeFlags(CCGSubSurf *ss, CCGEdge *e, int ssFromEditme
 }
 #endif
 
-/* face weighting - taken from Brecht's element data patch */
+/* face weighting */
 static void calc_ss_weights(int gridFaces,
                             FaceVertWeight **qweight, FaceVertWeight **tweight)
 {
@@ -437,9 +470,9 @@ static void calc_ss_weights(int gridFaces,
        }
 }
 
-DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
+static DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
                                  int drawInteriorEdges, int useSubsurfUv,
-                                 DerivedMesh *dm)
+                                 DerivedMesh *dm, MultiresSubsurf *ms)
 {
        DerivedMesh *result;
        int edgeSize = ccgSubSurf_getEdgeSize(ss);
@@ -447,19 +480,16 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
        int gridFaces = gridSize - 1;
        int edgeBase, faceBase;
        int i, j, k, S, x, y, index;
-       int vertBase = 0;
        CCGVertIterator *vi;
        CCGEdgeIterator *ei;
        CCGFaceIterator *fi;
-       CCGFace **faceMap2, **faceMap2Uv = NULL;
+       CCGFace **faceMap2;
        CCGEdge **edgeMap2;
        CCGVert **vertMap2;
        int totvert, totedge, totface;
        MVert *mvert;
        MEdge *med;
        MFace *mf;
-       MTFace *tface;
-       CCGSubSurf *uvss = NULL;
        int *origIndex;
        FaceVertWeight *qweight, *tweight;
 
@@ -472,7 +502,7 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
        for(; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
                CCGVert *v = ccgVertIterator_getCurrent(vi);
 
-               vertMap2[(int) ccgSubSurf_getVertVertHandle(ss, v)] = v;
+               vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))] = v;
        }
        ccgVertIterator_free(vi);
 
@@ -482,7 +512,7 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
        for(; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
                CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
 
-               edgeMap2[(int) ccgSubSurf_getEdgeEdgeHandle(ss, e)] = e;
+               edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))] = e;
        }
 
        totface = ccgSubSurf_getNumFaces(ss);
@@ -491,36 +521,24 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
        for(; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
 
-               faceMap2[(int) ccgSubSurf_getFaceFaceHandle(ss, f)] = f;
+               faceMap2[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f))] = f;
        }
        ccgFaceIterator_free(fi);
 
-       if(dm) {
-               result = CDDM_from_template(dm, ccgSubSurf_getNumFinalVerts(ss),
-                                           ccgSubSurf_getNumFinalEdges(ss),
-                                           ccgSubSurf_getNumFinalFaces(ss));
-               tface = dm->getFaceDataArray(dm, CD_MTFACE);
-       } else {
-               result = CDDM_new(ccgSubSurf_getNumFinalVerts(ss),
-                                 ccgSubSurf_getNumFinalEdges(ss),
-                                 ccgSubSurf_getNumFinalFaces(ss));
-               tface = NULL;
+       if(ms) {
+               result = MultiresDM_new(ms, dm, ccgSubSurf_getNumFinalVerts(ss),
+                                       ccgSubSurf_getNumFinalEdges(ss),
+                                       ccgSubSurf_getNumFinalFaces(ss));
        }
-
-       if(useSubsurfUv && result->getFaceDataArray(result, CD_MTFACE)) {
-               uvss = _getSubSurf(NULL, ccgSubSurf_getSubdivisionLevels(ss),
-                                  0, 1, 0);
-
-               if(ss_sync_from_uv(uvss, ss, dm)) {
-                       faceMap2Uv = MEM_mallocN(totface * sizeof(*faceMap2Uv),
-                                                "facemapuv");
-
-                       fi = ccgSubSurf_getFaceIterator(uvss);
-                       for(; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
-                               CCGFace *f = ccgFaceIterator_getCurrent(fi);
-                               faceMap2Uv[(int) ccgSubSurf_getFaceFaceHandle(uvss, f)] = f;
-                       }
-                       ccgFaceIterator_free(fi);
+       else {
+               if(dm) {
+                       result = CDDM_from_template(dm, ccgSubSurf_getNumFinalVerts(ss),
+                                                   ccgSubSurf_getNumFinalEdges(ss),
+                                                   ccgSubSurf_getNumFinalFaces(ss));
+               } else {
+                       result = CDDM_new(ccgSubSurf_getNumFinalVerts(ss),
+                                         ccgSubSurf_getNumFinalEdges(ss),
+                                         ccgSubSurf_getNumFinalFaces(ss));
                }
        }
 
@@ -531,27 +549,28 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
 
        for(index = 0; index < totface; index++) {
                CCGFace *f = faceMap2[index];
-               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
                FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight;
                int vertIdx[4];
 
                for(S = 0; S < numVerts; S++) {
                        CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
 
-                       vertIdx[S] = (int)ccgSubSurf_getVertVertHandle(ss, v);
+                       vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
                }
 
                DM_interp_vert_data(dm, result, vertIdx, weight[0][0], numVerts, i);
-               VecCopyf(mvert->co, ccgSubSurf_getFaceCenterData(ss, f));
+               VecCopyf(mvert->co, ccgSubSurf_getFaceCenterData(f));
                *origIndex = ORIGINDEX_NONE;
                ++mvert;
                ++origIndex;
                i++;
-               
+
                for(S = 0; S < numVerts; S++) {
                        int prevS = (S - 1 + numVerts) % numVerts;
                        int nextS = (S + 1) % numVerts;
                        int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3;
+
                        for(x = 1; x < gridFaces; x++) {
                                float w[4];
                                w[prevS]  = weight[x][0][0];
@@ -561,6 +580,7 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
                                DM_interp_vert_data(dm, result, vertIdx, w, numVerts, i);
                                VecCopyf(mvert->co,
                                         ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
+
                                *origIndex = ORIGINDEX_NONE;
                                ++mvert;
                                ++origIndex;
@@ -572,6 +592,7 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
                        int prevS = (S - 1 + numVerts) % numVerts;
                        int nextS = (S + 1) % numVerts;
                        int otherS = (numVerts == 4) ? (S + 2) % numVerts : 3;
+
                        for(y = 1; y < gridFaces; y++) {
                                for(x = 1; x < gridFaces; x++) {
                                        float w[4];
@@ -601,10 +622,10 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
                int vertIdx[2];
 
                CCGVert *v;
-               v = ccgSubSurf_getEdgeVert0(ss, e);
-               vertIdx[0] = (int)ccgSubSurf_getVertVertHandle(ss, v);
-               v = ccgSubSurf_getEdgeVert1(ss, e);
-               vertIdx[1] = (int)ccgSubSurf_getVertVertHandle(ss, v);
+               v = ccgSubSurf_getEdgeVert0(e);
+               vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
+               v = ccgSubSurf_getEdgeVert1(e);
+               vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
 
                for(x = 1; x < edgeSize - 1; x++) {
                        float w[2];
@@ -622,18 +643,17 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
                edgeBase += edgeSize-2;
        }
 
-       vertBase = i;
        for(index = 0; index < totvert; index++) {
                CCGVert *v = vertMap2[index];
                int vertIdx;
 
-               vertIdx = (int)ccgSubSurf_getVertVertHandle(ss, v);
+               vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
 
                DM_copy_vert_data(dm, result, vertIdx, i, 1);
                VecCopyf(mvert->co, ccgSubSurf_getVertData(ss, v));
 
                *((int*)ccgSubSurf_getVertUserData(ss, v)) = i;
-               *origIndex = ccgDM_getVertMapIndex(NULL, ss, v);
+               *origIndex = ccgDM_getVertMapIndex(ss, v);
                ++mvert;
                ++origIndex;
                i++;
@@ -646,7 +666,7 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
 
        for(index = 0; index < totface; index++) {
                CCGFace *f = faceMap2[index];
-               int numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int numVerts = ccgSubSurf_getFaceNumVerts(f);
 
                for(k = 0; k < numVerts; k++) {
                        for(x = 0; x < gridFaces; x++) {
@@ -688,9 +708,10 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
        for(index = 0; index < totedge; index++) {
                CCGEdge *e = edgeMap2[index];
                unsigned int flags = 0;
-               int edgeIdx = (int)ccgSubSurf_getEdgeEdgeHandle(ss, e);
+               char bweight = 0;
+               int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
 
-               if(!ccgSubSurf_getEdgeNumFaces(ss, e)) flags |= ME_LOOSEEDGE;
+               if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
 
 
                if(edgeIdx != -1 && dm) {
@@ -698,13 +719,15 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
                        dm->getEdge(dm, edgeIdx, &origMed);
 
                        flags |= origMed.flag;
+                       bweight = origMed.bweight;
                }
 
                for(x = 0; x < edgeSize - 1; x++) {
                        med->v1 = getEdgeIndex(ss, e, x, edgeSize);
                        med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize);
                        med->flag = flags;
-                       *origIndex = ccgDM_getEdgeMapIndex(NULL, ss, e);
+                       med->bweight = bweight;
+                       *origIndex = ccgDM_getEdgeMapIndex(ss, e);
                        ++med;
                        ++origIndex;
                        i++;
@@ -718,12 +741,11 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
 
        for(index = 0; index < totface; index++) {
                CCGFace *f = faceMap2[index];
-               CCGFace *uvf = faceMap2Uv ? faceMap2Uv[index] : NULL;
-               int numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int numVerts = ccgSubSurf_getFaceNumVerts(f);
                int mat_nr;
                int flag;
-               int mapIndex = ccgDM_getFaceMapIndex(NULL, ss, f);
-               int faceIdx = (int)ccgSubSurf_getFaceFaceHandle(ss, f);
+               int mapIndex = ccgDM_getFaceMapIndex(ss, f);
+               int faceIdx = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
 
                if(!ssFromEditmesh) {
                        MFace origMFace;
@@ -771,25 +793,6 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
                                                weight++;
                                        }
 
-                                       if(uvf) {
-                                               MTFace *tf = DM_get_face_data(result, i,
-                                                                            CD_MTFACE);
-                                               float *newuv;
-
-                                               newuv = getFaceUV(uvss, uvf, S, x + 0, y + 0,
-                                                                 edgeSize, gridSize);
-                                               tf->uv[0][0] = newuv[0]; tf->uv[0][1] = newuv[1];
-                                               newuv = getFaceUV(uvss, uvf, S, x + 0, y + 1,
-                                                                 edgeSize, gridSize);
-                                               tf->uv[1][0] = newuv[0]; tf->uv[1][1] = newuv[1];
-                                               newuv = getFaceUV(uvss, uvf, S, x + 1, y + 1,
-                                                                 edgeSize, gridSize);
-                                               tf->uv[2][0] = newuv[0]; tf->uv[2][1] = newuv[1];
-                                               newuv = getFaceUV(uvss, uvf, S, x + 1, y + 0,
-                                                                 edgeSize, gridSize);
-                                               tf->uv[3][0] = newuv[0]; tf->uv[3][1] = newuv[1];
-                                       }
-
                                        *origIndex = mapIndex;
                                        ++mf;
                                        ++origIndex;
@@ -806,9 +809,14 @@ DerivedMesh *ss_to_cdderivedmesh(CCGSubSurf *ss, int ssFromEditmesh,
        MEM_freeN(tweight);
        MEM_freeN(qweight);
 
-       if(uvss) {
-               ccgSubSurf_free(uvss);
-               MEM_freeN(faceMap2Uv);
+       if(useSubsurfUv) {
+               CustomData *fdata = &result->faceData;
+               CustomData *dmfdata = &dm->faceData;
+               int numlayer = CustomData_number_of_layers(fdata, CD_MTFACE);
+               int dmnumlayer = CustomData_number_of_layers(dmfdata, CD_MTFACE);
+               
+               for (i=0; i<numlayer && i<dmnumlayer; i++)
+                       set_subsurf_uv(ss, dm, result, i);
        }
 
        CDDM_calc_normals(result);
@@ -841,9 +849,9 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm,
                CCGVert *v;
 
                if(vertexCos) {
-                       ccgSubSurf_syncVert(ss, (CCGVertHDL)i, vertexCos[i], 0, &v);
+                       ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), vertexCos[i], 0, &v);
                } else {
-                       ccgSubSurf_syncVert(ss, (CCGVertHDL)i, mv->co, 0, &v);
+                       ccgSubSurf_syncVert(ss, SET_INT_IN_POINTER(i), mv->co, 0, &v);
                }
 
                ((int*)ccgSubSurf_getVertUserData(ss, v))[1] = *index;
@@ -858,8 +866,8 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm,
                crease = useFlatSubdiv ? creaseFactor :
                                         me->crease * creaseFactor / 255.0f;
 
-               ccgSubSurf_syncEdge(ss, (CCGEdgeHDL)i, (CCGVertHDL)me->v1,
-                                   (CCGVertHDL)me->v2, crease, &e);
+               ccgSubSurf_syncEdge(ss, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(me->v1),
+                                   SET_INT_IN_POINTER(me->v2), crease, &e);
 
                ((int*)ccgSubSurf_getEdgeUserData(ss, e))[1] = *index;
        }
@@ -869,22 +877,22 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm,
        for (i = 0; i < totface; i++, mf++, index++) {
                CCGFace *f;
 
-               fVerts[0] = (CCGVertHDL) mf->v1;
-               fVerts[1] = (CCGVertHDL) mf->v2;
-               fVerts[2] = (CCGVertHDL) mf->v3;
-               fVerts[3] = (CCGVertHDL) mf->v4;
+               fVerts[0] = SET_INT_IN_POINTER(mf->v1);
+               fVerts[1] = SET_INT_IN_POINTER(mf->v2);
+               fVerts[2] = SET_INT_IN_POINTER(mf->v3);
+               fVerts[3] = SET_INT_IN_POINTER(mf->v4);
 
                // this is very bad, means mesh is internally consistent.
                // it is not really possible to continue without modifying
                // other parts of code significantly to handle missing faces.
                // since this really shouldn't even be possible we just bail.
-               if(ccgSubSurf_syncFace(ss, (CCGFaceHDL)i, fVerts[3] ? 4 : 3,
+               if(ccgSubSurf_syncFace(ss, SET_INT_IN_POINTER(i), fVerts[3] ? 4 : 3,
                                       fVerts, &f) == eCCGError_InvalidValue) {
                        static int hasGivenError = 0;
 
                        if(!hasGivenError) {
-                               error("Unrecoverable error in SubSurf calculation,"
-                                     " mesh is inconsistent.");
+                               //XXX error("Unrecoverable error in SubSurf calculation,"
+                               //      " mesh is inconsistent.");
 
                                hasGivenError = 1;
                        }
@@ -900,15 +908,15 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm,
 
 /***/
 
-static int ccgDM_getVertMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGVert *v) {
+static int ccgDM_getVertMapIndex(CCGSubSurf *ss, CCGVert *v) {
        return ((int*) ccgSubSurf_getVertUserData(ss, v))[1];
 }
 
-static int ccgDM_getEdgeMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGEdge *e) {
+static int ccgDM_getEdgeMapIndex(CCGSubSurf *ss, CCGEdge *e) {
        return ((int*) ccgSubSurf_getEdgeUserData(ss, e))[1];
 }
 
-static int ccgDM_getFaceMapIndex(CCGDerivedMesh *ccgdm, CCGSubSurf *ss, CCGFace *f) {
+static int ccgDM_getFaceMapIndex(CCGSubSurf *ss, CCGFace *f) {
        return ((int*) ccgSubSurf_getFaceUserData(ss, f))[1];
 }
 
@@ -941,7 +949,7 @@ static void ccgDM_getMinMax(DerivedMesh *dm, float min_r[3], float max_r[3]) {
 
        for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
-               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
 
                for (S=0; S<numVerts; S++) {
                        VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
@@ -980,7 +988,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
 
        memset(mv, 0, sizeof(*mv));
 
-       if(vertNum < ccgdm->edgeMap[0].startVert) {
+       if((vertNum < ccgdm->edgeMap[0].startVert) && (ccgSubSurf_getNumFaces(ss) > 0)) {
                /* this vert comes from face data */
                int lastface = ccgSubSurf_getNumFaces(ss) - 1;
                CCGFace *f;
@@ -997,7 +1005,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
                        ++i;
 
                f = ccgdm->faceMap[i].face;
-               numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               numVerts = ccgSubSurf_getFaceNumVerts(f);
 
                gridSideVerts = gridSize - 2;
                gridInternalVerts = gridSideVerts * gridSideVerts;
@@ -1007,7 +1015,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
 
                offset = vertNum - ccgdm->faceMap[i].startVert;
                if(offset < 1) {
-                       VecCopyf(mv->co, ccgSubSurf_getFaceCenterData(ss, f));
+                       VecCopyf(mv->co, ccgSubSurf_getFaceCenterData(f));
                } else if(offset < gridSideEnd) {
                        offset -= 1;
                        grid = offset / gridSideVerts;
@@ -1021,7 +1029,7 @@ static void ccgDM_getFinalVert(DerivedMesh *dm, int vertNum, MVert *mv)
                        x = offset % gridSideVerts + 1;
                        VecCopyf(mv->co, ccgSubSurf_getFaceGridData(ss, f, grid, x, y));
                }
-       } else if(vertNum < ccgdm->vertMap[0].startVert) {
+       } else if((vertNum < ccgdm->vertMap[0].startVert) && (ccgSubSurf_getNumEdges(ss) > 0)) {
                /* this vert comes from edge data */
                CCGEdge *e;
                int lastedge = ccgSubSurf_getNumEdges(ss) - 1;
@@ -1069,7 +1077,7 @@ static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med)
                        ++i;
 
                f = ccgdm->faceMap[i].face;
-               numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               numVerts = ccgSubSurf_getFaceNumVerts(f);
 
                gridSideEdges = gridSize - 1;
                gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2; 
@@ -1098,33 +1106,26 @@ static void ccgDM_getFinalEdge(DerivedMesh *dm, int edgeNum, MEdge *med)
                /* this vert comes from edge data */
                CCGEdge *e;
                int edgeSize = ccgSubSurf_getEdgeSize(ss);
-               int x;
+               int x, *edgeFlag;
                unsigned int flags = 0;
 
                i = (edgeNum - ccgdm->edgeMap[0].startEdge) / (edgeSize - 1);
 
                e = ccgdm->edgeMap[i].edge;
 
-               if(!ccgSubSurf_getEdgeNumFaces(ss, e)) flags |= ME_LOOSEEDGE;
+               if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
 
                x = edgeNum - ccgdm->edgeMap[i].startEdge;
 
                med->v1 = getEdgeIndex(ss, e, x, edgeSize);
                med->v2 = getEdgeIndex(ss, e, x+1, edgeSize);
 
-               if(ccgdm->me) {
-                       int edgeIdx = (int) ccgSubSurf_getEdgeEdgeHandle(ss, e);
-
-                       if(edgeIdx!=-1) {
-                               MEdge *medge = ccgdm->me->medge;
-                               MEdge *origMed = &medge[edgeIdx];
-
-                               flags |= (origMed->flag & (ME_SEAM | ME_SHARP))
-                                        | ME_EDGEDRAW | ME_EDGERENDER;
-                       }
-               } else {
+               edgeFlag = dm->getEdgeData(dm, edgeNum, CD_FLAGS);
+               if(edgeFlag)
+                       flags |= (*edgeFlag & (ME_SEAM | ME_SHARP))
+                                        | ME_EDGEDRAW | ME_EDGERENDER;
+               else
                        flags |= ME_EDGEDRAW | ME_EDGERENDER;
-               }
 
                med->flag = flags;
        }
@@ -1145,7 +1146,7 @@ static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf)
        int grid;
        int x, y;
        int lastface = ccgSubSurf_getNumFaces(ss) - 1;
-       int *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS);
+       char *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS);
 
        memset(mf, 0, sizeof(*mf));
 
@@ -1154,7 +1155,7 @@ static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf)
                ++i;
 
        f = ccgdm->faceMap[i].face;
-       numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+       numVerts = ccgSubSurf_getFaceNumVerts(f);
 
        offset = faceNum - ccgdm->faceMap[i].startFace;
        grid = offset / gridFaces;
@@ -1167,7 +1168,7 @@ static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf)
        mf->v3 = getFaceIndex(ss, f, grid, x+1, y+1, edgeSize, gridSize);
        mf->v4 = getFaceIndex(ss, f, grid, x+1, y+0, edgeSize, gridSize);
 
-       if(faceFlags) mf->flag = faceFlags[i];
+       if(faceFlags) mf->flag = faceFlags[i*4];
        else mf->flag = ME_SMOOTH;
 }
 
@@ -1184,9 +1185,9 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
        totface = ccgSubSurf_getNumFaces(ss);
        for(index = 0; index < totface; index++) {
                CCGFace *f = ccgdm->faceMap[index].face;
-               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
 
-               VecCopyf(mvert[i++].co, ccgSubSurf_getFaceCenterData(ss, f));
+               VecCopyf(mvert[i++].co, ccgSubSurf_getFaceCenterData(f));
                
                for(S = 0; S < numVerts; S++) {
                        for(x = 1; x < gridSize - 1; x++) {
@@ -1234,14 +1235,12 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
        int gridSize = ccgSubSurf_getGridSize(ss);
        int edgeSize = ccgSubSurf_getEdgeSize(ss);
        int i = 0;
-       MEdge *origEdges = NULL;
-
-       if(ccgdm->me) origEdges = ccgdm->me->medge;
+       int *edgeFlags = dm->getEdgeDataArray(dm, CD_FLAGS);
 
        totface = ccgSubSurf_getNumFaces(ss);
        for(index = 0; index < totface; index++) {
                CCGFace *f = ccgdm->faceMap[index].face;
-               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
 
                for(S = 0; S < numVerts; S++) {
                        for(x = 0; x < gridSize - 1; x++) {
@@ -1285,16 +1284,13 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
                CCGEdge *e = ccgdm->edgeMap[index].edge;
                unsigned int flags = 0;
                int x;
+               int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
 
-               if(!ccgSubSurf_getEdgeNumFaces(ss, e)) flags |= ME_LOOSEEDGE;
-
-               if(origEdges){
-                       int edgeIdx = (int)ccgSubSurf_getEdgeEdgeHandle(ss, e);
+               if(!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
 
+               if(edgeFlags) {
                        if(edgeIdx != -1) {
-                               MEdge *origMed = &origEdges[edgeIdx];
-
-                               flags |= (origMed->flag & (ME_SEAM | ME_SHARP))
+                               flags |= (edgeFlags[i] & (ME_SEAM | ME_SHARP))
                                         | ME_EDGEDRAW | ME_EDGERENDER;
                        }
                } else {
@@ -1320,28 +1316,15 @@ static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface)
        int gridSize = ccgSubSurf_getGridSize(ss);
        int edgeSize = ccgSubSurf_getEdgeSize(ss);
        int i = 0;
-       MFace *origFaces = NULL;
-       int *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS);
-
-       if(ccgdm->me) origFaces = ccgdm->me->mface;
+       char *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS);
 
        totface = ccgSubSurf_getNumFaces(ss);
        for(index = 0; index < totface; index++) {
                CCGFace *f = ccgdm->faceMap[index].face;
-               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
                int mat_nr = 0;
                int flag = ME_SMOOTH; /* assume face is smooth by default */
 
-               if(!faceFlags) {
-                       if(origFaces) {
-                               int origIdx = (int) ccgSubSurf_getFaceFaceHandle(ss, f);
-                               MFace *origMFace = &origFaces[origIdx];
-
-                               mat_nr = origMFace->mat_nr;
-                               flag = origMFace->flag;
-                       }
-               }
-
                for(S = 0; S < numVerts; S++) {
                        for(y = 0; y < gridSize - 1; y++) {
                                for(x = 0; x < gridSize - 1; x++) {
@@ -1355,7 +1338,7 @@ static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface)
                                        mf->v4 = getFaceIndex(ss, f, S, x + 1, y + 0,
                                                              edgeSize, gridSize);
                                        mf->mat_nr = mat_nr;
-                                       if(faceFlags) mf->flag = faceFlags[index];
+                                       if(faceFlags) mf->flag = faceFlags[index*4];
                                        else mf->flag = flag;
 
                                        i++;
@@ -1385,7 +1368,7 @@ static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3]) {
        for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
                CCGVert *v = ccgVertIterator_getCurrent(vi);
 
-               vertMap2[(int) ccgSubSurf_getVertVertHandle(ss, v)] = v;
+               vertMap2[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))] = v;
        }
        ccgVertIterator_free(vi);
 
@@ -1395,7 +1378,7 @@ static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3]) {
        for (i=0; !ccgEdgeIterator_isStopped(ei); i++,ccgEdgeIterator_next(ei)) {
                CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
 
-               edgeMap2[(int) ccgSubSurf_getEdgeEdgeHandle(ss, e)] = e;
+               edgeMap2[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))] = e;
        }
 
        totface = ccgSubSurf_getNumFaces(ss);
@@ -1404,16 +1387,16 @@ static void ccgdm_getVertCos(DerivedMesh *dm, float (*cos)[3]) {
        for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
 
-               faceMap2[(int) ccgSubSurf_getFaceFaceHandle(ss, f)] = f;
+               faceMap2[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f))] = f;
        }
        ccgFaceIterator_free(fi);
 
        i = 0;
        for (index=0; index<totface; index++) {
                CCGFace *f = faceMap2[index];
-               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
 
-               VecCopyf(cos[i++], ccgSubSurf_getFaceCenterData(ss, f));
+               VecCopyf(cos[i++], ccgSubSurf_getFaceCenterData(f));
                
                for (S=0; S<numVerts; S++) {
                        for (x=1; x<gridSize-1; x++) {
@@ -1455,7 +1438,7 @@ static void ccgDM_foreachMappedVert(DerivedMesh *dm, void (*func)(void *userData
        for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
                CCGVert *v = ccgVertIterator_getCurrent(vi);
                VertData *vd = ccgSubSurf_getVertData(ccgdm->ss, v);
-               int index = ccgDM_getVertMapIndex(ccgdm, ccgdm->ss, v);
+               int index = ccgDM_getVertMapIndex(ccgdm->ss, v);
 
                if (index!=-1)
                        func(userData, index, vd->co, vd->no, NULL);
@@ -1472,7 +1455,7 @@ static void ccgDM_foreachMappedEdge(DerivedMesh *dm, void (*func)(void *userData
        for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
                CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
                VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
-               int index = ccgDM_getEdgeMapIndex(ccgdm, ss, e);
+               int index = ccgDM_getEdgeMapIndex(ss, e);
 
                if (index!=-1) {
                        for (i=0; i<edgeSize-1; i++)
@@ -1513,9 +1496,9 @@ static void ccgDM_drawVerts(DerivedMesh *dm) {
        fi = ccgSubSurf_getFaceIterator(ss);
        for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
-               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
 
-               glVertex3fv(ccgSubSurf_getFaceCenterData(ss, f));
+               glVertex3fv(ccgSubSurf_getFaceCenterData(f));
                for (S=0; S<numVerts; S++)
                        for (x=1; x<gridSize-1; x++)
                                glVertex3fv(ccgSubSurf_getFaceGridEdgeData(ss, f, S, x));
@@ -1542,7 +1525,7 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) {
                CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
                VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
 
-               if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(ss, e))
+               if (!drawLooseEdges && !ccgSubSurf_getEdgeNumFaces(e))
                        continue;
 
                if (useAging && !(G.f&G_BACKBUFSEL)) {
@@ -1565,7 +1548,7 @@ static void ccgDM_drawEdges(DerivedMesh *dm, int drawLooseEdges) {
        if (ccgdm->drawInteriorEdges) {
                for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                        CCGFace *f = ccgFaceIterator_getCurrent(fi);
-                       int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+                       int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
 
                        for (S=0; S<numVerts; S++) {
                                VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
@@ -1603,7 +1586,7 @@ static void ccgDM_drawLooseEdges(DerivedMesh *dm) {
                CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
                VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
 
-               if (!ccgSubSurf_getEdgeNumFaces(ss, e)) {
+               if (!ccgSubSurf_getEdgeNumFaces(e)) {
                        glBegin(GL_LINE_STRIP);
                        for (i=0; i<edgeSize-1; i++) {
                                glVertex3fv(edgeData[i].co);
@@ -1616,28 +1599,51 @@ static void ccgDM_drawLooseEdges(DerivedMesh *dm) {
        ccgEdgeIterator_free(ei);
 }
 
+static void ccgDM_glNormalFast(float *a, float *b, float *c, float *d)
+{
+       float a_cX = c[0]-a[0], a_cY = c[1]-a[1], a_cZ = c[2]-a[2];
+       float b_dX = d[0]-b[0], b_dY = d[1]-b[1], b_dZ = d[2]-b[2];
+       float no[3];
+
+       no[0] = b_dY*a_cZ - b_dZ*a_cY;
+       no[1] = b_dZ*a_cX - b_dX*a_cZ;
+       no[2] = b_dX*a_cY - b_dY*a_cX;
+
+       /* don't normalize, GL_NORMALIZE is be enabled */
+       glNormal3fv(no);
+}
+
        /* Only used by non-editmesh types */
-static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) {
+static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) {
        CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
        CCGSubSurf *ss = ccgdm->ss;
        CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
        int gridSize = ccgSubSurf_getGridSize(ss);
-       MFace *mface = ccgdm->me->mface;
+       char *faceFlags = DM_get_face_data_layer(dm, CD_FLAGS);
 
        for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
-               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
-               int index = (int) ccgSubSurf_getFaceFaceHandle(ss, f);
-               MFace *mf = &mface[index];
+               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
+               int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
+               int drawSmooth, mat_nr;
+
+               if(faceFlags) {
+                       drawSmooth = (faceFlags[index*4] & ME_SMOOTH);
+                       mat_nr= faceFlags[index*4 + 1];
+               }
+               else {
+                       drawSmooth = 1;
+                       mat_nr= 0;
+               }
                
-               if (!setMaterial(mf->mat_nr+1))
+               if (!setMaterial(mat_nr+1, NULL))
                        continue;
 
-               glShadeModel((mf->flag&ME_SMOOTH)?GL_SMOOTH:GL_FLAT);
+               glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT);
                for (S=0; S<numVerts; S++) {
                        VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
 
-                       if (mf->flag&ME_SMOOTH) {
+                       if (drawSmooth) {
                                for (y=0; y<gridSize-1; y++) {
                                        glBegin(GL_QUAD_STRIP);
                                        for (x=0; x<gridSize; x++) {
@@ -1659,14 +1665,8 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) {
                                                float *b = faceGridData[(y+0)*gridSize + x + 1].co;
                                                float *c = faceGridData[(y+1)*gridSize + x + 1].co;
                                                float *d = faceGridData[(y+1)*gridSize + x].co;
-                                               float a_cX = c[0]-a[0], a_cY = c[1]-a[1], a_cZ = c[2]-a[2];
-                                               float b_dX = d[0]-b[0], b_dY = d[1]-b[1], b_dZ = d[2]-b[2];
-                                               float no[3];
 
-                                               no[0] = b_dY*a_cZ - b_dZ*a_cY;
-                                               no[1] = b_dZ*a_cX - b_dX*a_cZ;
-                                               no[2] = b_dX*a_cY - b_dY*a_cX;
-                                               glNormal3fv(no);
+                                               ccgDM_glNormalFast(a, b, c, d);
 
                                                glVertex3fv(d);
                                                glVertex3fv(c);
@@ -1681,6 +1681,168 @@ static void ccgDM_drawFacesSolid(DerivedMesh *dm, int (*setMaterial)(int)) {
 
        ccgFaceIterator_free(fi);
 }
+
+       /* Only used by non-editmesh types */
+static void ccgDM_drawMappedFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs), int (*setDrawOptions)(void *userData, int index), void *userData) {
+       CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
+       CCGSubSurf *ss = ccgdm->ss;
+       CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
+       GPUVertexAttribs gattribs;
+       DMVertexAttribs attribs;
+       MTFace *tf = dm->getFaceDataArray(dm, CD_MTFACE);
+       int gridSize = ccgSubSurf_getGridSize(ss);
+       int gridFaces = gridSize - 1;
+       int edgeSize = ccgSubSurf_getEdgeSize(ss);
+       int transp, orig_transp, new_transp;
+       char *faceFlags = DM_get_face_data_layer(dm, CD_FLAGS);
+       int a, b, i, doDraw, numVerts, matnr, new_matnr, totface;
+
+       doDraw = 0;
+       numVerts = 0;
+       matnr = -1;
+       transp = GPU_get_material_blend_mode();
+       orig_transp = transp;
+
+       memset(&attribs, 0, sizeof(attribs));
+
+#define PASSATTRIB(dx, dy, vert) {                                                                                             \
+       if(attribs.totorco) {                                                                                                           \
+               index = getFaceIndex(ss, f, S, x+dx, y+dy, edgeSize, gridSize);                 \
+               glVertexAttrib3fvARB(attribs.orco.glIndex, attribs.orco.array[index]);  \
+       }                                                                                                                                                       \
+       for(b = 0; b < attribs.tottface; b++) {                                                                         \
+               MTFace *tf = &attribs.tface[b].array[a];                                                                \
+               glVertexAttrib2fvARB(attribs.tface[b].glIndex, tf->uv[vert]);                   \
+       }                                                                                                                                                       \
+       for(b = 0; b < attribs.totmcol; b++) {                                                                          \
+               MCol *cp = &attribs.mcol[b].array[a*4 + vert];                                                  \
+               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[a*4 + vert];                                                   \
+               glVertexAttrib3fvARB(attribs.tang.glIndex, tang);                                               \
+       }                                                                                                                                                       \
+}
+
+       totface = ccgSubSurf_getNumFaces(ss);
+       for(a = 0, i = 0; i < totface; i++) {
+               CCGFace *f = ccgdm->faceMap[i].face;
+               int S, x, y, drawSmooth;
+               int index = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
+               int origIndex = ccgDM_getFaceMapIndex(ss, f);
+               
+               numVerts = ccgSubSurf_getFaceNumVerts(f);
+
+               if(faceFlags) {
+                       drawSmooth = (faceFlags[index*4] & ME_SMOOTH);
+                       new_matnr= faceFlags[index*4 + 1] + 1;
+               }
+               else {
+                       drawSmooth = 1;
+                       new_matnr= 1;
+               }
+
+               if(new_matnr != matnr) {
+                       doDraw = setMaterial(matnr = new_matnr, &gattribs);
+                       if(doDraw)
+                               DM_vertex_attributes_from_gpu(dm, &gattribs, &attribs);
+               }
+
+               if(!doDraw || (setDrawOptions && !setDrawOptions(userData, origIndex))) {
+                       a += gridFaces*gridFaces*numVerts;
+                       continue;
+               }
+
+               if(tf) {
+                       new_transp = tf[i].transp;
+
+                       if(new_transp != transp) {
+                               if(new_transp == GPU_BLEND_SOLID && orig_transp != GPU_BLEND_SOLID)
+                                       GPU_set_material_blend_mode(orig_transp);
+                               else
+                                       GPU_set_material_blend_mode(new_transp);
+                               transp = new_transp;
+                       }
+               }
+
+               glShadeModel(drawSmooth? GL_SMOOTH: GL_FLAT);
+               for (S=0; S<numVerts; S++) {
+                       VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
+                       VertData *vda, *vdb;
+
+                       if (drawSmooth) {
+                               for (y=0; y<gridFaces; y++) {
+                                       glBegin(GL_QUAD_STRIP);
+                                       for (x=0; x<gridFaces; x++) {
+                                               vda = &faceGridData[(y+0)*gridSize + x];
+                                               vdb = &faceGridData[(y+1)*gridSize + x];
+                                               
+                                               PASSATTRIB(0, 0, 0);
+                                               glNormal3fv(vda->no);
+                                               glVertex3fv(vda->co);
+
+                                               PASSATTRIB(0, 1, 1);
+                                               glNormal3fv(vdb->no);
+                                               glVertex3fv(vdb->co);
+
+                                               if(x != gridFaces-1)
+                                                       a++;
+                                       }
+
+                                       vda = &faceGridData[(y+0)*gridSize + x];
+                                       vdb = &faceGridData[(y+1)*gridSize + x];
+
+                                       PASSATTRIB(0, 0, 3);
+                                       glNormal3fv(vda->no);
+                                       glVertex3fv(vda->co);
+
+                                       PASSATTRIB(0, 1, 2);
+                                       glNormal3fv(vdb->no);
+                                       glVertex3fv(vdb->co);
+
+                                       glEnd();
+
+                                       a++;
+                               }
+                       } else {
+                               glBegin(GL_QUADS);
+                               for (y=0; y<gridFaces; y++) {
+                                       for (x=0; x<gridFaces; x++) {
+                                               float *aco = faceGridData[(y+0)*gridSize + x].co;
+                                               float *bco = faceGridData[(y+0)*gridSize + x + 1].co;
+                                               float *cco = faceGridData[(y+1)*gridSize + x + 1].co;
+                                               float *dco = faceGridData[(y+1)*gridSize + x].co;
+
+                                               ccgDM_glNormalFast(aco, bco, cco, dco);
+
+                                               PASSATTRIB(0, 1, 1);
+                                               glVertex3fv(dco);
+                                               PASSATTRIB(1, 1, 2);
+                                               glVertex3fv(cco);
+                                               PASSATTRIB(1, 0, 3);
+                                               glVertex3fv(bco);
+                                               PASSATTRIB(0, 0, 0);
+                                               glVertex3fv(aco);
+                                               
+                                               a++;
+                                       }
+                               }
+                               glEnd();
+                       }
+               }
+       }
+
+#undef PASSATTRIB
+
+       ccgFaceIterator_free(fi);
+}
+
+static void ccgDM_drawFacesGLSL(DerivedMesh *dm, int (*setMaterial)(int, void *attribs)) {
+       dm->drawMappedFacesGLSL(dm, setMaterial, NULL, NULL);
+}
+
 static void ccgDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned char *col1, unsigned char *col2) {
        CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
        CCGSubSurf *ss = ccgdm->ss;
@@ -1704,7 +1866,7 @@ static void ccgDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned ch
        glBegin(GL_QUADS);
        for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
-               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
 
                for (S=0; S<numVerts; S++) {
                        VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
@@ -1746,188 +1908,255 @@ static void ccgDM_drawFacesColored(DerivedMesh *dm, int useTwoSided, unsigned ch
        ccgFaceIterator_free(fi);
 }
 
-static void ccgDM_drawFacesTex(DerivedMesh *dm, int (*setDrawParams)(MTFace *tface, MCol *mcol, int matnr))
+static void ccgDM_drawFacesTex_common(DerivedMesh *dm,
+       int (*drawParams)(MTFace *tface, MCol *mcol, int matnr),
+       int (*drawParamsMapped)(void *userData, int index),
+       void *userData) 
 {
-       /* unimplemented, no textures in editmode anyway */
-}
-static void ccgDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawParams)(void *userData, int index), void *userData)
-{
-       /* unfinished code, no textures in editmode anyway */
-
        CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
        CCGSubSurf *ss = ccgdm->ss;
-       CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
-       int gridSize = ccgSubSurf_getGridSize(ss);
-       MFace *mface = ccgdm->me->mface;
-       MCol *mcol = ccgdm->me->mcol;
-//     float uv[4][2];
-//     float col[4][3];
+       MCol *mcol = DM_get_face_data_layer(dm, CD_MCOL);
+       MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
+       char *faceFlags = DM_get_face_data_layer(dm, CD_FLAGS);
+       int i, totface, flag, gridSize = ccgSubSurf_getGridSize(ss);
+       int gridFaces = gridSize - 1;
 
-       glBegin(GL_QUADS);
-       for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
-               CCGFace *f = ccgFaceIterator_getCurrent(fi);
-               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
-               int index = (int) ccgSubSurf_getFaceFaceHandle(ss, f);
-               MFace *mf = &mface[index];
+       totface = ccgSubSurf_getNumFaces(ss);
+       for(i = 0; i < totface; i++) {
+               CCGFace *f = ccgdm->faceMap[i].face;
+               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
+               int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f);
+               int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
                unsigned char *cp= NULL;
-               int findex = ccgDM_getFaceMapIndex(ccgdm, ss, f); 
-               int flag = (findex == -1)? 0: setDrawParams(userData, findex);
+               int mat_nr;
+
+               if(faceFlags) {
+                       drawSmooth = (faceFlags[origIndex*4] & ME_SMOOTH);
+                       mat_nr= faceFlags[origIndex*4 + 1];
+               }
+               else {
+                       drawSmooth = 1;
+                       mat_nr= 0;
+               }
 
-               if (flag==0)
+               if(drawParams)
+                       flag = drawParams(tf, mcol, mat_nr);
+               else
+                       flag= (drawParamsMapped)? drawParamsMapped(userData, index): 1;
+               
+               if (flag == 0) { /* flag 0 == the face is hidden or invisible */
+                       if(tf) tf += gridFaces*gridFaces*numVerts;
+                       if(mcol) mcol += gridFaces*gridFaces*numVerts*4;
                        continue;
-               else if (flag==1 && mcol)
-                       cp= (unsigned char*) &mcol[index*4];
+               }
+
+               /* flag 1 == use vertex colors */
+               if(mcol) {
+                       if(flag==1) cp= (unsigned char*)mcol;
+                       mcol += gridFaces*gridFaces*numVerts*4;
+               }
 
                for (S=0; S<numVerts; S++) {
                        VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
-                       for (y=0; y<gridSize-1; y++) {
-                               for (x=0; x<gridSize-1; x++) {
-                                       VertData *a = &faceGridData[(y+0)*gridSize + x + 0];
-                                       VertData *b = &faceGridData[(y+0)*gridSize + x + 1];
-                                       VertData *c = &faceGridData[(y+1)*gridSize + x + 1];
-                                       VertData *d = &faceGridData[(y+1)*gridSize + x + 0];
+                       VertData *a, *b;
 
-                                       if (!(mf->flag&ME_SMOOTH)) {
-                                               float a_cX = c->co[0]-a->co[0], a_cY = c->co[1]-a->co[1], a_cZ = c->co[2]-a->co[2];
-                                               float b_dX = d->co[0]-b->co[0], b_dY = d->co[1]-b->co[1], b_dZ = d->co[2]-b->co[2];
-                                               float no[3];
+                       if (drawSmooth) {
+                               glShadeModel(GL_SMOOTH);
+                               for (y=0; y<gridFaces; y++) {
+                                       glBegin(GL_QUAD_STRIP);
+                                       for (x=0; x<gridFaces; x++) {
+                                               a = &faceGridData[(y+0)*gridSize + x];
+                                               b = &faceGridData[(y+1)*gridSize + x];
 
-                                               no[0] = b_dY*a_cZ - b_dZ*a_cY;
-                                               no[1] = b_dZ*a_cX - b_dX*a_cZ;
-                                               no[2] = b_dX*a_cY - b_dY*a_cX;
+                                               if(tf) glTexCoord2fv(tf->uv[0]);
+                                               if(cp) glColor3ub(cp[3], cp[2], cp[1]);
+                                               glNormal3fv(a->no);
+                                               glVertex3fv(a->co);
 
-                                               glNormal3fv(no);
+                                               if(tf) glTexCoord2fv(tf->uv[1]);
+                                               if(cp) glColor3ub(cp[7], cp[6], cp[5]);
+                                               glNormal3fv(b->no);
+                                               glVertex3fv(b->co);
+                                               
+                                               if(x != gridFaces-1) {
+                                                       if(tf) tf++;
+                                                       if(cp) cp += 16;
+                                               }
                                        }
 
-//                                     if (tf) glTexCoord2fv(tf->uv[0]);
-//                                     if (cp) glColor3ub(cp[3], cp[2], cp[1]);
-//                                     if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v1].no);
-//                                     glVertex3fv(mvert[mf->v1].co);
+                                       a = &faceGridData[(y+0)*gridSize + x];
+                                       b = &faceGridData[(y+1)*gridSize + x];
 
-/*
-                                       {
-                                               float x_v = (float) fx/(gridSize-1);
-                                               float y_v = (float) fy/(gridSize-1);
-                                               float data[6];
+                                       if(tf) glTexCoord2fv(tf->uv[3]);
+                                       if(cp) glColor3ub(cp[15], cp[14], cp[13]);
+                                       glNormal3fv(a->no);
+                                       glVertex3fv(a->co);
 
-                                               for (k=0; k<numDataComponents; k++) {
-                                                       data[k] = (center_data[k]*(1.0f-x_v) + edge_data[S][k]*x_v)*(1.0f-y_v) + 
-                                                                       (edge_data[prevS][k]*(1.0f-x_v) + corner_data[S][k]*x_v)*y_v;
-                                       }
-*/
-
-//                                     if (cp) glColor3ub(cp[3], cp[2], cp[1]);
-                                       if (mf->flag&ME_SMOOTH) glNormal3fv(d->no);
-                                       glVertex3fv(d->co);
-//                                     if (cp) glColor3ub(cp[7], cp[6], cp[5]);
-                                       if (mf->flag&ME_SMOOTH) glNormal3fv(c->no);
-                                       glVertex3fv(c->co);
-//                                     if (cp) glColor3ub(cp[11], cp[10], cp[9]);
-                                       if (mf->flag&ME_SMOOTH) glNormal3fv(b->no);
+                                       if(tf) glTexCoord2fv(tf->uv[2]);
+                                       if(cp) glColor3ub(cp[11], cp[10], cp[9]);
+                                       glNormal3fv(b->no);
                                        glVertex3fv(b->co);
-//                                     if (cp) glColor3ub(cp[15], cp[14], cp[13]);
-                                       if (mf->flag&ME_SMOOTH) glNormal3fv(a->no);
-                                       glVertex3fv(a->co);
+
+                                       if(tf) tf++;
+                                       if(cp) cp += 16;
+
+                                       glEnd();
                                }
+                       } else {
+                               glShadeModel(GL_FLAT);
+                               glBegin(GL_QUADS);
+                               for (y=0; y<gridFaces; y++) {
+                                       for (x=0; x<gridFaces; x++) {
+                                               float *a_co = faceGridData[(y+0)*gridSize + x].co;
+                                               float *b_co = faceGridData[(y+0)*gridSize + x + 1].co;
+                                               float *c_co = faceGridData[(y+1)*gridSize + x + 1].co;
+                                               float *d_co = faceGridData[(y+1)*gridSize + x].co;
+
+                                               ccgDM_glNormalFast(a_co, b_co, c_co, d_co);
+
+                                               if(tf) glTexCoord2fv(tf->uv[1]);
+                                               if(cp) glColor3ub(cp[7], cp[6], cp[5]);
+                                               glVertex3fv(d_co);
+
+                                               if(tf) glTexCoord2fv(tf->uv[2]);
+                                               if(cp) glColor3ub(cp[11], cp[10], cp[9]);
+                                               glVertex3fv(c_co);
+
+                                               if(tf) glTexCoord2fv(tf->uv[3]);
+                                               if(cp) glColor3ub(cp[15], cp[14], cp[13]);
+                                               glVertex3fv(b_co);
+
+                                               if(tf) glTexCoord2fv(tf->uv[0]);
+                                               if(cp) glColor3ub(cp[3], cp[2], cp[1]);
+                                               glVertex3fv(a_co);
+
+                                               if(tf) tf++;
+                                               if(cp) cp += 16;
+                                       }
+                               }
+                               glEnd();
                        }
                }
        }
-       glEnd();
+}
 
-       ccgFaceIterator_free(fi);
-/*
-       MeshDerivedMesh *mdm = (MeshDerivedMesh*) dm;
-       Mesh *me = mdm->me;
-       MVert *mvert= mdm->verts;
-       MFace *mface= me->mface;
-       MTFace *tface = me->mtface;
-       float *nors = mdm->nors;
-       int a;
-
-       for (a=0; a<me->totface; a++) {
-               MFace *mf= &mface[a];
-               if (tf) glTexCoord2fv(tf->uv[1]);
-               if (cp) glColor3ub(cp[7], cp[6], cp[5]);
-               if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v2].no);
-               glVertex3fv(mvert[mf->v2].co);
-
-               if (tf) glTexCoord2fv(tf->uv[2]);
-               if (cp) glColor3ub(cp[11], cp[10], cp[9]);
-               if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v3].no);
-               glVertex3fv(mvert[mf->v3].co);
-
-               if(mf->v4) {
-                       if (tf) glTexCoord2fv(tf->uv[3]);
-                       if (cp) glColor3ub(cp[15], cp[14], cp[13]);
-                       if (mf->flag&ME_SMOOTH) glNormal3sv(mvert[mf->v4].no);
-                       glVertex3fv(mvert[mf->v4].co);
+static void ccgDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
+{
+       ccgDM_drawFacesTex_common(dm, setDrawOptions, NULL, NULL);
+}
+
+static void ccgDM_drawMappedFacesTex(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index), void *userData)
+{
+       ccgDM_drawFacesTex_common(dm, NULL, setDrawOptions, userData);
+}
+
+static void ccgDM_drawUVEdges(DerivedMesh *dm)
+{
+
+       MFace *mf = dm->getFaceArray(dm);
+       MTFace *tf = DM_get_face_data_layer(dm, CD_MTFACE);
+       int i;
+       
+       if (tf) {
+               glBegin(GL_LINES);
+               for(i = 0; i < dm->numFaceData; i++, mf++, tf++) {
+                       if(!(mf->flag&ME_HIDE)) {
+                               glVertex2fv(tf->uv[0]);
+                               glVertex2fv(tf->uv[1]);
+       
+                               glVertex2fv(tf->uv[1]);
+                               glVertex2fv(tf->uv[2]);
+       
+                               if(!mf->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 ccgDM_drawMappedFaces(DerivedMesh *dm, int (*setDrawOptions)(void *userData, int index, int *drawSmooth_r), void *userData, int useColors) {
        CCGDerivedMesh *ccgdm = (CCGDerivedMesh*) dm;
        CCGSubSurf *ss = ccgdm->ss;
        CCGFaceIterator *fi = ccgSubSurf_getFaceIterator(ss);
        int i, gridSize = ccgSubSurf_getGridSize(ss);
-       int *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS);
+       char *faceFlags = dm->getFaceDataArray(dm, CD_FLAGS);
 
        for (i=0; !ccgFaceIterator_isStopped(fi); i++,ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
-               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
-               int drawSmooth, index = ccgDM_getFaceMapIndex(ccgdm, ss, f);
+               int S, x, y, numVerts = ccgSubSurf_getFaceNumVerts(f);
+               int drawSmooth, index = ccgDM_getFaceMapIndex(ss, f);
                int origIndex;
 
-               origIndex = (int)ccgSubSurf_getFaceFaceHandle(ss, f);
+               origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
 
-               if(faceFlags) drawSmooth = (faceFlags[origIndex] & ME_SMOOTH);
+               if(faceFlags) drawSmooth = (faceFlags[origIndex*4] & ME_SMOOTH);
                else drawSmooth = 1;
-
-               if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index, &drawSmooth))) {
-                       for (S=0; S<numVerts; S++) {
-                               VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
-                               if (drawSmooth) {
-                                       glShadeModel(GL_SMOOTH);
-                                       for (y=0; y<gridSize-1; y++) {
-                                               glBegin(GL_QUAD_STRIP);
-                                               for (x=0; x<gridSize; x++) {
-                                                       VertData *a = &faceGridData[(y+0)*gridSize + x];
-                                                       VertData *b = &faceGridData[(y+1)*gridSize + x];
-
-                                                       glNormal3fv(a->no);
-                                                       glVertex3fv(a->co);
-                                                       glNormal3fv(b->no);
-                                                       glVertex3fv(b->co);
+               
+               if (index!=-1) {
+                       int draw;
+                       draw = setDrawOptions==NULL ? 1 : setDrawOptions(userData, index, &drawSmooth);
+                       
+                       if (draw) {
+                               if (draw==2) {
+                                       glEnable(GL_POLYGON_STIPPLE);
+                                       glPolygonStipple(stipple_quarttone);
+                               }
+                               
+                               for (S=0; S<numVerts; S++) {
+                                       VertData *faceGridData = ccgSubSurf_getFaceGridDataArray(ss, f, S);
+                                       if (drawSmooth) {
+                                               glShadeModel(GL_SMOOTH);
+                                               for (y=0; y<gridSize-1; y++) {
+                                                       glBegin(GL_QUAD_STRIP);
+                                                       for (x=0; x<gridSize; x++) {
+                                                               VertData *a = &faceGridData[(y+0)*gridSize + x];
+                                                               VertData *b = &faceGridData[(y+1)*gridSize + x];
+       
+                                                               glNormal3fv(a->no);
+                                                               glVertex3fv(a->co);
+                                                               glNormal3fv(b->no);
+                                                               glVertex3fv(b->co);
+                                                       }
+                                                       glEnd();
                                                }
-                                               glEnd();
-                                       }
-                               } else {
-                                       glShadeModel(GL_FLAT);
-                                       glBegin(GL_QUADS);
-                                       for (y=0; y<gridSize-1; y++) {
-                                               for (x=0; x<gridSize-1; x++) {
-                                                       float *a = faceGridData[(y+0)*gridSize + x].co;
-                                                       float *b = faceGridData[(y+0)*gridSize + x + 1].co;
-                                                       float *c = faceGridData[(y+1)*gridSize + x + 1].co;
-                                                       float *d = faceGridData[(y+1)*gridSize + x].co;
-                                                       float a_cX = c[0]-a[0], a_cY = c[1]-a[1], a_cZ = c[2]-a[2];
-                                                       float b_dX = d[0]-b[0], b_dY = d[1]-b[1], b_dZ = d[2]-b[2];
-                                                       float no[3];
-
-                                                       no[0] = b_dY*a_cZ - b_dZ*a_cY;
-                                                       no[1] = b_dZ*a_cX - b_dX*a_cZ;
-                                                       no[2] = b_dX*a_cY - b_dY*a_cX;
-                                                       glNormal3fv(no);
-
-                                                       glVertex3fv(d);
-                                                       glVertex3fv(c);
-                                                       glVertex3fv(b);
-                                                       glVertex3fv(a);
+                                       } else {
+                                               glShadeModel(GL_FLAT);
+                                               glBegin(GL_QUADS);
+                                               for (y=0; y<gridSize-1; y++) {
+                                                       for (x=0; x<gridSize-1; x++) {
+                                                               float *a = faceGridData[(y+0)*gridSize + x].co;
+                                                               float *b = faceGridData[(y+0)*gridSize + x + 1].co;
+                                                               float *c = faceGridData[(y+1)*gridSize + x + 1].co;
+                                                               float *d = faceGridData[(y+1)*gridSize + x].co;
+                                                               float a_cX = c[0]-a[0], a_cY = c[1]-a[1], a_cZ = c[2]-a[2];
+                                                               float b_dX = d[0]-b[0], b_dY = d[1]-b[1], b_dZ = d[2]-b[2];
+                                                               float no[3];
+       
+                                                               no[0] = b_dY*a_cZ - b_dZ*a_cY;
+                                                               no[1] = b_dZ*a_cX - b_dX*a_cZ;
+                                                               no[2] = b_dX*a_cY - b_dY*a_cX;
+                                                               glNormal3fv(no);
+       
+                                                               glVertex3fv(d);
+                                                               glVertex3fv(c);
+                                                               glVertex3fv(b);
+                                                               glVertex3fv(a);
+                                                       }
                                                }
+                                               glEnd();
                                        }
-                                       glEnd();
                                }
+                               if (draw==2)
+                                       glDisable(GL_POLYGON_STIPPLE);
                        }
                }
        }
@@ -1945,7 +2174,7 @@ static void ccgDM_drawMappedEdges(DerivedMesh *dm, int (*setDrawOptions)(void *u
        for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
                CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
                VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
-               int index = ccgDM_getEdgeMapIndex(ccgdm, ss, e);
+               int index = ccgDM_getEdgeMapIndex(ss, e);
 
                glBegin(GL_LINE_STRIP);
                if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) {
@@ -1975,7 +2204,7 @@ static void ccgDM_drawMappedEdgesInterp(DerivedMesh *dm, int (*setDrawOptions)(v
        for (; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
                CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
                VertData *edgeData = ccgSubSurf_getEdgeDataArray(ss, e);
-               int index = ccgDM_getEdgeMapIndex(ccgdm, ss, e);
+               int index = ccgDM_getEdgeMapIndex(ss, e);
 
                glBegin(GL_LINE_STRIP);
                if (index!=-1 && (!setDrawOptions || setDrawOptions(userData, index))) {
@@ -2002,7 +2231,7 @@ static void ccgDM_foreachMappedFaceCenter(DerivedMesh *dm, void (*func)(void *us
 
        for (; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
-               int index = ccgDM_getFaceMapIndex(ccgdm, ss, f);
+               int index = ccgDM_getFaceMapIndex(ss, f);
 
                if (index!=-1) {
                                /* Face center data normal isn't updated atm. */
@@ -2028,7 +2257,7 @@ static void ccgDM_release(DerivedMesh *dm) {
 
 static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
                                          int drawInteriorEdges,
-                                         int useSubsurfUv, Mesh *me,
+                                         int useSubsurfUv,
                                          DerivedMesh *dm)
 {
        CCGDerivedMesh *ccgdm = MEM_callocN(sizeof(*ccgdm), "ccgdm");
@@ -2038,37 +2267,29 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        int index, totvert, totedge, totface;
        int i;
        int vertNum, edgeNum, faceNum;
-       int *vertOrigIndex, *edgeOrigIndex, *faceOrigIndex;
-       int *faceFlags;
+       int *vertOrigIndex, *faceOrigIndex; /* *edgeOrigIndex - as yet, unused  */
+       int *edgeFlags;
+       char *faceFlags;
        int edgeSize;
        int gridSize;
        int gridFaces;
        int gridSideVerts;
-       int gridInternalVerts;
+       /*int gridInternalVerts; - as yet unused */
        int gridSideEdges;
        int gridInternalEdges;
-       MVert *mvert = NULL;
+       /* MVert *mvert = NULL; - as yet unused */
        MEdge *medge = NULL;
        MFace *mface = NULL;
-       MTFace *tface;
-       CCGSubSurf *uvss = NULL;
-       CCGFace **faceMap2Uv = NULL;
        FaceVertWeight *qweight, *tweight;
 
-       if(dm) {
-               DM_from_template(&ccgdm->dm, dm, ccgSubSurf_getNumFinalVerts(ss),
-                                ccgSubSurf_getNumFinalEdges(ss),
-                                ccgSubSurf_getNumFinalFaces(ss));
-               tface = dm->getFaceDataArray(dm, CD_MTFACE);
-               DM_add_face_layer(&ccgdm->dm, CD_FLAGS, CD_FLAG_NOCOPY, NULL);
-       } else {
-               DM_init(&ccgdm->dm, ccgSubSurf_getNumFinalVerts(ss),
-                       ccgSubSurf_getNumFinalEdges(ss),
-                       ccgSubSurf_getNumFinalFaces(ss));
+       DM_from_template(&ccgdm->dm, dm, ccgSubSurf_getNumFinalVerts(ss),
+                                        ccgSubSurf_getNumFinalEdges(ss),
+                                        ccgSubSurf_getNumFinalFaces(ss));
+       DM_add_face_layer(&ccgdm->dm, CD_FLAGS, CD_CALLOC, NULL);
+       DM_add_edge_layer(&ccgdm->dm, CD_FLAGS, CD_CALLOC, NULL);
 
-               if(me) tface = me->mtface;
-               else tface = NULL;
-       }
+       CustomData_set_layer_flag(&ccgdm->dm.faceData, CD_FLAGS, CD_FLAG_NOCOPY);
+       CustomData_set_layer_flag(&ccgdm->dm.edgeData, CD_FLAGS, CD_FLAG_NOCOPY);
 
        ccgdm->dm.getMinMax = ccgDM_getMinMax;
        ccgdm->dm.getNumVerts = ccgDM_getNumVerts;
@@ -2099,8 +2320,11 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        ccgdm->dm.drawFacesSolid = ccgDM_drawFacesSolid;
        ccgdm->dm.drawFacesColored = ccgDM_drawFacesColored;
        ccgdm->dm.drawFacesTex = ccgDM_drawFacesTex;
+       ccgdm->dm.drawFacesGLSL = ccgDM_drawFacesGLSL;
        ccgdm->dm.drawMappedFaces = ccgDM_drawMappedFaces;
        ccgdm->dm.drawMappedFacesTex = ccgDM_drawMappedFacesTex;
+       ccgdm->dm.drawMappedFacesGLSL = ccgDM_drawMappedFacesGLSL;
+       ccgdm->dm.drawUVEdges = ccgDM_drawUVEdges;
 
        ccgdm->dm.drawMappedEdgesInterp = ccgDM_drawMappedEdgesInterp;
        ccgdm->dm.drawMappedEdges = ccgDM_drawMappedEdges;
@@ -2110,7 +2334,6 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        ccgdm->ss = ss;
        ccgdm->drawInteriorEdges = drawInteriorEdges;
        ccgdm->useSubsurfUv = useSubsurfUv;
-       ccgdm->me = me;
 
        totvert = ccgSubSurf_getNumVerts(ss);
        ccgdm->vertMap = MEM_mallocN(totvert * sizeof(*ccgdm->vertMap), "vertMap");
@@ -2118,7 +2341,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        for(; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
                CCGVert *v = ccgVertIterator_getCurrent(vi);
 
-               ccgdm->vertMap[(int) ccgSubSurf_getVertVertHandle(ss, v)].vert = v;
+               ccgdm->vertMap[GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v))].vert = v;
        }
        ccgVertIterator_free(vi);
 
@@ -2128,7 +2351,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        for(; !ccgEdgeIterator_isStopped(ei); ccgEdgeIterator_next(ei)) {
                CCGEdge *e = ccgEdgeIterator_getCurrent(ei);
 
-               ccgdm->edgeMap[(int) ccgSubSurf_getEdgeEdgeHandle(ss, e)].edge = e;
+               ccgdm->edgeMap[GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e))].edge = e;
        }
 
        totface = ccgSubSurf_getNumFaces(ss);
@@ -2137,33 +2360,15 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        for(; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
                CCGFace *f = ccgFaceIterator_getCurrent(fi);
 
-               ccgdm->faceMap[(int) ccgSubSurf_getFaceFaceHandle(ss, f)].face = f;
+               ccgdm->faceMap[GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f))].face = f;
        }
        ccgFaceIterator_free(fi);
 
-       if(useSubsurfUv && dm->getFaceDataArray(&ccgdm->dm, CD_MTFACE)) {
-               /* not for editmesh currently */
-               uvss = _getSubSurf(NULL, ccgSubSurf_getSubdivisionLevels(ss),
-                                  0, 1, 0);
-
-               if(ss_sync_from_uv(uvss, ss, dm)) {
-                       faceMap2Uv = MEM_mallocN(totface * sizeof(*faceMap2Uv),
-                                                "facemapuv");
-
-                       fi = ccgSubSurf_getFaceIterator(uvss);
-                       for(; !ccgFaceIterator_isStopped(fi); ccgFaceIterator_next(fi)) {
-                               CCGFace *f = ccgFaceIterator_getCurrent(fi);
-                               faceMap2Uv[(int) ccgSubSurf_getFaceFaceHandle(uvss, f)] = f;
-                       }
-                       ccgFaceIterator_free(fi);
-               }
-       }
-
        edgeSize = ccgSubSurf_getEdgeSize(ss);
        gridSize = ccgSubSurf_getGridSize(ss);
        gridFaces = gridSize - 1;
        gridSideVerts = gridSize - 2;
-       gridInternalVerts = gridSideVerts * gridSideVerts;
+       /*gridInternalVerts = gridSideVerts * gridSideVerts; - as yet, unused */
        gridSideEdges = gridSize - 1;
        gridInternalEdges = (gridSideEdges - 1) * gridSideEdges * 2; 
 
@@ -2173,29 +2378,22 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        edgeNum = 0;
        faceNum = 0;
 
-       if(dm) {
-               mvert = dm->getVertArray(dm);
-               medge = dm->getEdgeArray(dm);
-               mface = dm->getFaceArray(dm);
-       } else if(me) {
-               mvert = me->mvert;
-               medge = me->medge;
-               mface = me->mface;
-       }
+       /* mvert = dm->getVertArray(dm); - as yet unused */
+       medge = dm->getEdgeArray(dm);
+       mface = dm->getFaceArray(dm);
 
        vertOrigIndex = DM_get_vert_data_layer(&ccgdm->dm, CD_ORIGINDEX);
-       edgeOrigIndex = DM_get_edge_data_layer(&ccgdm->dm, CD_ORIGINDEX);
+       /*edgeOrigIndex = DM_get_edge_data_layer(&ccgdm->dm, CD_ORIGINDEX);*/
        faceOrigIndex = DM_get_face_data_layer(&ccgdm->dm, CD_ORIGINDEX);
 
        faceFlags = DM_get_face_data_layer(&ccgdm->dm, CD_FLAGS);
 
        for(index = 0; index < totface; ++index) {
                CCGFace *f = ccgdm->faceMap[index].face;
-               CCGFace *uvf = faceMap2Uv ? faceMap2Uv[index] : NULL;
-               int numVerts = ccgSubSurf_getFaceNumVerts(ss, f);
+               int numVerts = ccgSubSurf_getFaceNumVerts(f);
                int numFinalEdges = numVerts * (gridSideEdges + gridInternalEdges);
-               int mapIndex = ccgDM_getFaceMapIndex(ccgdm, ss, f);
-               int origIndex = (int)ccgSubSurf_getFaceFaceHandle(ss, f);
+               int mapIndex = ccgDM_getFaceMapIndex(ss, f);
+               int origIndex = GET_INT_FROM_POINTER(ccgSubSurf_getFaceFaceHandle(ss, f));
                FaceVertWeight *weight = (numVerts == 4) ? qweight : tweight;
                int S, x, y;
                int vertIdx[4];
@@ -2210,7 +2408,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
                for(S = 0; S < numVerts; S++) {
                        CCGVert *v = ccgSubSurf_getFaceVert(ss, f, S);
 
-                       vertIdx[S] = (int)ccgSubSurf_getVertVertHandle(ss, v);
+                       vertIdx[S] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
                }
 
                DM_interp_vert_data(dm, &ccgdm->dm, vertIdx, weight[0][0],
@@ -2284,25 +2482,6 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
                                                            &w, 1, faceNum);
                                        weight++;
 
-                                       if(uvf) {
-                                               MTFace *tf = DM_get_face_data(&ccgdm->dm, faceNum,
-                                                                             CD_MTFACE);
-                                               float *newuv;
-
-                                               newuv = getFaceUV(uvss, uvf, S, x + 0, y + 0,
-                                                                 edgeSize, gridSize);
-                                               tf->uv[0][0] = newuv[0]; tf->uv[0][1] = newuv[1];
-                                               newuv = getFaceUV(uvss, uvf, S, x + 0, y + 1,
-                                                                 edgeSize, gridSize);
-                                               tf->uv[1][0] = newuv[0]; tf->uv[1][1] = newuv[1];
-                                               newuv = getFaceUV(uvss, uvf, S, x + 1, y + 1,
-                                                                 edgeSize, gridSize);
-                                               tf->uv[2][0] = newuv[0]; tf->uv[2][1] = newuv[1];
-                                               newuv = getFaceUV(uvss, uvf, S, x + 1, y + 0,
-                                                                 edgeSize, gridSize);
-                                               tf->uv[3][0] = newuv[0]; tf->uv[3][1] = newuv[1];
-                                       }
-
                                        *faceOrigIndex = mapIndex;
 
                                        ++faceOrigIndex;
@@ -2311,28 +2490,37 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
                        }
                }
 
-               faceFlags[index] = mface[origIndex].flag;
+               faceFlags[index*4] = mface[origIndex].flag;
+               faceFlags[index*4 + 1] = mface[origIndex].mat_nr;
 
                edgeNum += numFinalEdges;
        }
 
-       if(uvss) {
-               ccgSubSurf_free(uvss);
-               MEM_freeN(faceMap2Uv);
+       if(useSubsurfUv) {
+               CustomData *fdata = &ccgdm->dm.faceData;
+               CustomData *dmfdata = &dm->faceData;
+               int numlayer = CustomData_number_of_layers(fdata, CD_MTFACE);
+               int dmnumlayer = CustomData_number_of_layers(dmfdata, CD_MTFACE);
+
+               for (i=0; i<numlayer && i<dmnumlayer; i++)
+                       set_subsurf_uv(ss, dm, &ccgdm->dm, i);
        }
 
+       edgeFlags = DM_get_edge_data_layer(&ccgdm->dm, CD_FLAGS);
+
        for(index = 0; index < totedge; ++index) {
                CCGEdge *e = ccgdm->edgeMap[index].edge;
                int numFinalEdges = edgeSize - 1;
-               int mapIndex = ccgDM_getEdgeMapIndex(ccgdm, ss, e);
+               int mapIndex = ccgDM_getEdgeMapIndex(ss, e);
                int x;
                int vertIdx[2];
+               int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
 
                CCGVert *v;
-               v = ccgSubSurf_getEdgeVert0(ss, e);
-               vertIdx[0] = (int)ccgSubSurf_getVertVertHandle(ss, v);
-               v = ccgSubSurf_getEdgeVert1(ss, e);
-               vertIdx[1] = (int)ccgSubSurf_getVertVertHandle(ss, v);
+               v = ccgSubSurf_getEdgeVert0(e);
+               vertIdx[0] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
+               v = ccgSubSurf_getEdgeVert1(e);
+               vertIdx[1] = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
 
                ccgdm->edgeMap[index].startVert = vertNum;
                ccgdm->edgeMap[index].startEdge = edgeNum;
@@ -2350,19 +2538,23 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
                        ++vertNum;
                }
 
-               for(i = 0; i < numFinalEdges; ++i)
+               for(i = 0; i < numFinalEdges; ++i) {
+                       if(edgeIdx >= 0 && edgeFlags)
+                                       edgeFlags[edgeNum + i] = medge[edgeIdx].flag;
+
                        *(int *)DM_get_edge_data(&ccgdm->dm, edgeNum + i,
                                                 CD_ORIGINDEX) = mapIndex;
+               }
 
                edgeNum += numFinalEdges;
        }
 
        for(index = 0; index < totvert; ++index) {
                CCGVert *v = ccgdm->vertMap[index].vert;
-               int mapIndex = ccgDM_getVertMapIndex(ccgdm, ccgdm->ss, v);
+               int mapIndex = ccgDM_getVertMapIndex(ccgdm->ss, v);
                int vertIdx;
 
-               vertIdx = (int)ccgSubSurf_getVertVertHandle(ss, v);
+               vertIdx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
 
                ccgdm->vertMap[index].startVert = vertNum;
 
@@ -2384,9 +2576,10 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
 
 /***/
 
-struct DerivedMesh *subsurf_make_derived_from_derived(
+struct DerivedMesh *subsurf_make_derived_from_derived_with_multires(
                         struct DerivedMesh *dm,
                         struct SubsurfModifierData *smd,
+                       struct MultiresSubsurf *ms,
                         int useRenderParams, float (*vertCos)[3],
                         int isFinalCalc, int editMode)
 {
@@ -2403,15 +2596,22 @@ struct DerivedMesh *subsurf_make_derived_from_derived(
 
                return (DerivedMesh *)getCCGDerivedMesh(smd->emCache,
                                                        drawInteriorEdges,
-                                                   useSubsurfUv, NULL, dm);
+                                                   useSubsurfUv, dm);
        } else if(useRenderParams) {
                /* Do not use cache in render mode. */
-               CCGSubSurf *ss = _getSubSurf(NULL, smd->renderLevels, 0, 1, useSimple);
+               CCGSubSurf *ss;
+               int levels;
+               
+               levels= smd->renderLevels; // XXX get_render_subsurf_level(&scene->r, smd->renderLevels);
+               if(levels == 0)
+                       return dm;
+               
+               ss = _getSubSurf(NULL, levels, 0, 1, useSimple);
 
                ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
 
                result = ss_to_cdderivedmesh(ss, 0, drawInteriorEdges,
-                                            useSubsurfUv, dm);
+                                            useSubsurfUv, dm, ms);
 
                ccgSubSurf_free(ss);
                
@@ -2440,8 +2640,13 @@ struct DerivedMesh *subsurf_make_derived_from_derived(
 
                        ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
 
+
                        return ss_to_cdderivedmesh(ss, 0, drawInteriorEdges,
-                                              useSubsurfUv, dm);
+                                                  useSubsurfUv, dm, ms);
+
+                       /*return (DerivedMesh *)getCCGDerivedMesh(smd->mCache,
+                                                       drawInteriorEdges,
+                                                   useSubsurfUv, dm);*/
                } else {
                        if (smd->mCache && isFinalCalc) {
                                ccgSubSurf_free(smd->mCache);
@@ -2451,8 +2656,13 @@ struct DerivedMesh *subsurf_make_derived_from_derived(
                        ss = _getSubSurf(NULL, smd->levels, 0, 1, useSimple);
                        ss_sync_from_derivedmesh(ss, dm, vertCos, useSimple);
 
+                       /*smd->mCache = ss;
+                       result = (DerivedMesh *)getCCGDerivedMesh(smd->mCache,
+                                                       drawInteriorEdges,
+                                                   useSubsurfUv, dm);*/
+
                        result = ss_to_cdderivedmesh(ss, 0, drawInteriorEdges,
-                                                    useSubsurfUv, dm);
+                                                    useSubsurfUv, dm, ms);
 
                        ccgSubSurf_free(ss);
 
@@ -2461,13 +2671,22 @@ struct DerivedMesh *subsurf_make_derived_from_derived(
        }
 }
 
+struct DerivedMesh *subsurf_make_derived_from_derived(
+                        struct DerivedMesh *dm,
+                        struct SubsurfModifierData *smd,
+                        int useRenderParams, float (*vertCos)[3],
+                        int isFinalCalc, int editMode)
+{
+       return subsurf_make_derived_from_derived_with_multires(dm, smd, NULL, useRenderParams, vertCos, isFinalCalc, editMode);
+}
+
 void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3]) 
 {
-               /* Finds the subsurf limit positions for the verts in a mesh 
-                * and puts them in an array of floats. Please note that the 
-                * calculated vert positions is incorrect for the verts 
-                * on the boundary of the mesh.
-                */
+       /* Finds the subsurf limit positions for the verts in a mesh 
+        * and puts them in an array of floats. Please note that the 
+        * calculated vert positions is incorrect for the verts 
+        * on the boundary of the mesh.
+        */
        CCGSubSurf *ss = _getSubSurf(NULL, 1, 0, 1, 0);
        float edge_sum[3], face_sum[3];
        CCGVertIterator *vi;
@@ -2478,9 +2697,9 @@ void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3])
        vi = ccgSubSurf_getVertIterator(ss);
        for (; !ccgVertIterator_isStopped(vi); ccgVertIterator_next(vi)) {
                CCGVert *v = ccgVertIterator_getCurrent(vi);
-               int idx = (int) ccgSubSurf_getVertVertHandle(ss, v);
-               int N = ccgSubSurf_getVertNumEdges(ss, v);
-               int numFaces = ccgSubSurf_getVertNumFaces(ss, v);
+               int idx = GET_INT_FROM_POINTER(ccgSubSurf_getVertVertHandle(v));
+               int N = ccgSubSurf_getVertNumEdges(v);
+               int numFaces = ccgSubSurf_getVertNumFaces(v);
                float *co;
                int i;
                 
@@ -2488,14 +2707,19 @@ void subsurf_calculate_limit_positions(Mesh *me, float (*positions_r)[3])
                face_sum[0]= face_sum[1]= face_sum[2]= 0.0;
 
                for (i=0; i<N; i++) {
-                       CCGEdge *e = ccgSubSurf_getVertEdge(ss, v, i);
+                       CCGEdge *e = ccgSubSurf_getVertEdge(v, i);
                        VecAddf(edge_sum, edge_sum, ccgSubSurf_getEdgeData(ss, e, 1));
                }
                for (i=0; i<numFaces; i++) {
-                       CCGFace *f = ccgSubSurf_getVertFace(ss, v, i);
-                       VecAddf(face_sum, face_sum, ccgSubSurf_getFaceCenterData(ss, f));
+                       CCGFace *f = ccgSubSurf_getVertFace(v, i);
+                       VecAddf(face_sum, face_sum, ccgSubSurf_getFaceCenterData(f));
                }
 
+               /* ad-hoc correction for boundary vertices, to at least avoid them
+                  moving completely out of place (brecht) */
+               if(numFaces && numFaces != N)
+                       VecMulf(face_sum, (float)N/(float)numFaces);
+
                co = ccgSubSurf_getVertData(ss, v);
                positions_r[idx][0] = (co[0]*N*N + edge_sum[0]*4 + face_sum[0])/(N*(N+5));
                positions_r[idx][1] = (co[1]*N*N + edge_sum[1]*4 + face_sum[1])/(N*(N+5));