Merged changes in the trunk up to revision 51853.
[blender-staging.git] / source / blender / blenkernel / intern / subsurf_ccg.c
index 33350a80928014c1cc00abc6e99fbfd9fd3266de..469b012f9c319d8254e6aa6ff2e4f5ad4830d5c9 100644 (file)
@@ -602,7 +602,7 @@ static void ss_sync_from_derivedmesh(CCGSubSurf *ss, DerivedMesh *dm,
        }
 
        mp = mpoly;
-       index = DM_get_poly_data_layer(dm, CD_ORIGINDEX);
+       index = (int *)dm->getPolyDataArray(dm, CD_ORIGINDEX);
        for (i = 0; i < dm->numPolyData; i++, mp++) {
                CCGFace *f;
 
@@ -2703,6 +2703,30 @@ static void *ccgDM_get_edge_data_layer(DerivedMesh *dm, int type)
 }
 
 static void *ccgDM_get_tessface_data_layer(DerivedMesh *dm, int type)
+{
+       if (type == CD_ORIGINDEX) {
+               /* create origindex on demand to save memory */
+               int *origindex;
+
+               /* Avoid re-creation if the layer exists already */
+               origindex = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
+               if (origindex) {
+                       return origindex;
+               }
+
+               DM_add_tessface_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
+               origindex = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
+
+               /* silly loop counting up */
+               range_vn_i(origindex, dm->getNumTessFaces(dm), 0);
+
+               return origindex;
+       }
+
+       return DM_get_tessface_data_layer(dm, type);
+}
+
+static void *ccgDM_get_poly_data_layer(DerivedMesh *dm, int type)
 {
        if (type == CD_ORIGINDEX) {
                /* create origindex on demand to save memory */
@@ -2713,13 +2737,13 @@ static void *ccgDM_get_tessface_data_layer(DerivedMesh *dm, int type)
                int gridFaces = ccgSubSurf_getGridSize(ss) - 1;
 
                /* Avoid re-creation if the layer exists already */
-               origindex = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
+               origindex = DM_get_poly_data_layer(dm, CD_ORIGINDEX);
                if (origindex) {
                        return origindex;
                }
 
-               DM_add_tessface_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
-               origindex = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
+               DM_add_poly_layer(dm, CD_ORIGINDEX, CD_CALLOC, NULL);
+               origindex = DM_get_poly_data_layer(dm, CD_ORIGINDEX);
 
                totface = ccgSubSurf_getNumFaces(ss);
 
@@ -2735,7 +2759,7 @@ static void *ccgDM_get_tessface_data_layer(DerivedMesh *dm, int type)
                return origindex;
        }
 
-       return DM_get_tessface_data_layer(dm, type);
+       return DM_get_poly_data_layer(dm, type);
 }
 
 static void *ccgDM_get_vert_data(DerivedMesh *dm, int index, int type)
@@ -2768,6 +2792,16 @@ static void *ccgDM_get_tessface_data(DerivedMesh *dm, int index, int type)
        return DM_get_tessface_data(dm, index, type);
 }
 
+static void *ccgDM_get_poly_data(DerivedMesh *dm, int index, int type)
+{
+       if (type == CD_ORIGINDEX) {
+               /* ensure creation of CD_ORIGINDEX layer */
+               ccgDM_get_tessface_data_layer(dm, type);
+       }
+
+       return DM_get_poly_data(dm, index, type);
+}
+
 static int ccgDM_getNumGrids(DerivedMesh *dm)
 {
        CCGDerivedMesh *ccgdm = (CCGDerivedMesh *)dm;
@@ -3098,7 +3132,7 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        }
 
        /* We absolutely need that layer, else it's no valid tessellated data! */
-       polyidx = CustomData_add_layer(&ccgdm->dm.faceData, CD_POLYINDEX, CD_CALLOC,
+       polyidx = CustomData_add_layer(&ccgdm->dm.faceData, CD_ORIGINDEX, CD_CALLOC,
                                       NULL, ccgSubSurf_getNumFinalFaces(ss));
 
        ccgdm->dm.getMinMax = ccgDM_getMinMax;
@@ -3126,9 +3160,11 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
        ccgdm->dm.getVertData = ccgDM_get_vert_data;
        ccgdm->dm.getEdgeData = ccgDM_get_edge_data;
        ccgdm->dm.getTessFaceData = ccgDM_get_tessface_data;
+       ccgdm->dm.getPolyData = ccgDM_get_poly_data;
        ccgdm->dm.getVertDataArray = ccgDM_get_vert_data_layer;
        ccgdm->dm.getEdgeDataArray = ccgDM_get_edge_data_layer;
        ccgdm->dm.getTessFaceDataArray = ccgDM_get_tessface_data_layer;
+       ccgdm->dm.getPolyDataArray = ccgDM_get_poly_data_layer;
        ccgdm->dm.getNumGrids = ccgDM_getNumGrids;
        ccgdm->dm.getGridSize = ccgDM_getGridSize;
        ccgdm->dm.getGridData = ccgDM_getGridData;
@@ -3227,8 +3263,8 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
 
        vertOrigIndex = DM_get_vert_data_layer(&ccgdm->dm, CD_ORIGINDEX);
        /*edgeOrigIndex = DM_get_edge_data_layer(&ccgdm->dm, CD_ORIGINDEX);*/
-       faceOrigIndex = DM_get_tessface_data_layer(&ccgdm->dm, CD_ORIGINDEX);
 
+       faceOrigIndex = DM_get_tessface_data_layer(&ccgdm->dm, CD_ORIGINDEX);
        polyOrigIndex = DM_get_poly_data_layer(&ccgdm->dm, CD_ORIGINDEX);
 
 #if 0
@@ -3366,7 +3402,8 @@ static CCGDerivedMesh *getCCGDerivedMesh(CCGSubSurf *ss,
                                        
                                        /*set original index data*/
                                        if (faceOrigIndex) {
-                                               *faceOrigIndex = origIndex;
+                                               /* reference the index in 'polyOrigIndex' */
+                                               *faceOrigIndex = faceNum;
                                                faceOrigIndex++;
                                        }
                                        if (polyOrigIndex) {