2.5
[blender.git] / source / blender / blenkernel / intern / modifier.c
index 62c9699aaf91610f9aeb6fc708ba3a588e728cf1..80a9f173d6ac119b23c9d0918fc9e670f5d41e90 100644 (file)
@@ -117,6 +117,72 @@ static struct DerivedMesh *NewBooleanDerivedMesh() {return NULL;}
 
 //XXX #include "BIF_meshlaplacian.h"
 
+/* Utility */
+
+static int is_last_displist(Object *ob)
+{
+       Curve *cu = ob->data;
+       static int curvecount=0, totcurve=0;
+
+       if(curvecount == 0){
+               DispList *dl;
+
+               totcurve = 0;
+               for(dl=cu->disp.first; dl; dl=dl->next)
+                       totcurve++;
+       }
+
+       curvecount++;
+
+       if(curvecount == totcurve){
+               curvecount = 0;
+               return 1;
+       }
+
+       return 0;
+}
+
+static DerivedMesh *get_original_dm(Scene *scene, Object *ob, float (*vertexCos)[3], int orco)
+{
+       DerivedMesh *dm= NULL;
+
+       if(ob->type==OB_MESH) {
+               dm = CDDM_from_mesh((Mesh*)(ob->data), ob);
+
+               if(vertexCos) {
+                       CDDM_apply_vert_coords(dm, vertexCos);
+                       //CDDM_calc_normals(dm);
+               }
+               
+               if(orco)
+                       DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
+       }
+       else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
+               Object *tmpobj;
+               Curve *tmpcu;
+
+               if(is_last_displist(ob)) {
+                       /* copies object and modifiers (but not the data) */
+                       tmpobj= copy_object(ob);
+                       tmpcu = (Curve *)tmpobj->data;
+                       tmpcu->id.us--;
+
+                       /* copies the data */
+                       tmpobj->data = copy_curve((Curve *) ob->data);
+
+                       makeDispListCurveTypes(scene, tmpobj, 1);
+                       nurbs_to_mesh(tmpobj);
+
+                       dm = CDDM_from_mesh((Mesh*)(tmpobj->data), tmpobj);
+                       //CDDM_calc_normals(dm);
+
+                       free_libblock_us(&G.main->object, tmpobj);
+               }
+       }
+
+       return dm;
+}
+
 /***/
 
 static int noneModifier_isDisabled(ModifierData *md)
@@ -143,7 +209,7 @@ static void curveModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tcmd->name, cmd->name, 32);
 }
 
-CustomDataMask curveModifier_requiredDataMask(ModifierData *md)
+CustomDataMask curveModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CurveModifierData *cmd = (CurveModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -187,7 +253,7 @@ static void curveModifier_updateDepgraph(
 
 static void curveModifier_deformVerts(
                                      ModifierData *md, Object *ob, DerivedMesh *derivedData,
-         float (*vertexCos)[3], int numVerts)
+         float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        CurveModifierData *cmd = (CurveModifierData*) md;
 
@@ -203,7 +269,7 @@ static void curveModifier_deformVertsEM(
 
        if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
 
-       curveModifier_deformVerts(md, ob, dm, vertexCos, numVerts);
+       curveModifier_deformVerts(md, ob, dm, vertexCos, numVerts, 0, 0);
 
        if(!derivedData) dm->release(dm);
 }
@@ -219,7 +285,7 @@ static void latticeModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tlmd->name, lmd->name, 32);
 }
 
-CustomDataMask latticeModifier_requiredDataMask(ModifierData *md)
+CustomDataMask latticeModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        LatticeModifierData *lmd = (LatticeModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -276,7 +342,7 @@ static void modifier_vgroup_cache(ModifierData *md, float (*vertexCos)[3])
 
 static void latticeModifier_deformVerts(
                                        ModifierData *md, Object *ob, DerivedMesh *derivedData,
-     float (*vertexCos)[3], int numVerts)
+     float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        LatticeModifierData *lmd = (LatticeModifierData*) md;
 
@@ -295,7 +361,7 @@ static void latticeModifier_deformVertsEM(
 
        if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
 
-       latticeModifier_deformVerts(md, ob, dm, vertexCos, numVerts);
+       latticeModifier_deformVerts(md, ob, dm, vertexCos, numVerts, 0, 0);
 
        if(!derivedData) dm->release(dm);
 }
@@ -602,7 +668,7 @@ static void maskModifier_copyData(ModifierData *md, ModifierData *target)
        strcpy(tmmd->vgroup, mmd->vgroup);
 }
 
-static CustomDataMask maskModifier_requiredDataMask(ModifierData *md)
+static CustomDataMask maskModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        return (1 << CD_MDEFORMVERT);
 }
@@ -3330,7 +3396,7 @@ static void bevelModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tbmd->defgrp_name, bmd->defgrp_name, 32);
 }
 
-CustomDataMask bevelModifier_requiredDataMask(ModifierData *md)
+CustomDataMask bevelModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        BevelModifierData *bmd = (BevelModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -3410,7 +3476,7 @@ static void displaceModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tdmd->uvlayer_name, dmd->uvlayer_name, 32);
 }
 
-CustomDataMask displaceModifier_requiredDataMask(ModifierData *md)
+CustomDataMask displaceModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        DisplaceModifierData *dmd = (DisplaceModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -3477,7 +3543,7 @@ static void displaceModifier_updateDepgraph(
        }
 }
 
-static void validate_layer_name(const CustomData *data, int type, char *name)
+static void validate_layer_name(const CustomData *data, int type, char *name, char *outname)
 {
        int index = -1;
 
@@ -3490,8 +3556,10 @@ static void validate_layer_name(const CustomData *data, int type, char *name)
                * deleted, so assign the active layer to name
                */
                index = CustomData_get_active_layer_index(data, CD_MTFACE);
-               strcpy(name, data->layers[index].name);
+               strcpy(outname, data->layers[index].name);
        }
+       else
+               strcpy(outname, name);
 }
 
 static void get_texture_coords(DisplaceModifierData *dmd, Object *ob,
@@ -3517,12 +3585,11 @@ static void get_texture_coords(DisplaceModifierData *dmd, Object *ob,
                        char *done = MEM_callocN(sizeof(*done) * numVerts,
                                        "get_texture_coords done");
                        int numFaces = dm->getNumFaces(dm);
+                       char uvname[32];
                        MTFace *tf;
 
-                       validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name);
-
-                       tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE,
-                                       dmd->uvlayer_name);
+                       validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name, uvname);
+                       tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
 
                        /* verts are given the UV from the first face that uses them */
                        for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
@@ -3686,7 +3753,7 @@ static void displaceModifier_do(
 
 static void displaceModifier_deformVerts(
                                         ModifierData *md, Object *ob, DerivedMesh *derivedData,
-      float (*vertexCos)[3], int numVerts)
+      float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm;
 
@@ -3753,7 +3820,7 @@ static void uvprojectModifier_copyData(ModifierData *md, ModifierData *target)
        tumd->aspecty = umd->aspecty;
 }
 
-CustomDataMask uvprojectModifier_requiredDataMask(ModifierData *md)
+CustomDataMask uvprojectModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CustomDataMask dataMask = 0;
 
@@ -3818,6 +3885,7 @@ static DerivedMesh *uvprojectModifier_do(UVProjectModifierData *umd,
        Projector projectors[MOD_UVPROJECT_MAXPROJECTORS];
        int num_projectors = 0;
        float aspect;
+       char uvname[32];
        
        if(umd->aspecty != 0) aspect = umd->aspectx / umd->aspecty;
        else aspect = 1.0f;
@@ -3832,12 +3900,11 @@ static DerivedMesh *uvprojectModifier_do(UVProjectModifierData *umd,
        if(!dm->getFaceDataArray(dm, CD_MTFACE)) return dm;
 
        /* make sure we're using an existing layer */
-       validate_layer_name(&dm->faceData, CD_MTFACE, umd->uvlayer_name);
+       validate_layer_name(&dm->faceData, CD_MTFACE, umd->uvlayer_name, uvname);
 
        /* make sure we are not modifying the original UV layer */
        tface = CustomData_duplicate_referenced_layer_named(&dm->faceData,
-                       CD_MTFACE,
-   umd->uvlayer_name);
+                       CD_MTFACE, uvname);
 
        numVerts = dm->getNumVerts(dm);
 
@@ -4058,17 +4125,15 @@ static void decimateModifier_copyData(ModifierData *md, ModifierData *target)
        tdmd->percent = dmd->percent;
 }
 
-//XXX
-#if 0 
 static DerivedMesh *decimateModifier_applyModifier(
                ModifierData *md, Object *ob, DerivedMesh *derivedData,
   int useRenderParams, int isFinalCalc)
 {
-       DecimateModifierData *dmd = (DecimateModifierData*) md;
+       // DecimateModifierData *dmd = (DecimateModifierData*) md;
        DerivedMesh *dm = derivedData, *result = NULL;
        MVert *mvert;
        MFace *mface;
-       LOD_Decimation_Info lod;
+       // LOD_Decimation_Info lod;
        int totvert, totface;
        int a, numTris;
 
@@ -4090,6 +4155,8 @@ static DerivedMesh *decimateModifier_applyModifier(
                goto exit;
        }
 
+       // XXX
+#if 0
        lod.vertex_buffer= MEM_mallocN(3*sizeof(float)*totvert, "vertices");
        lod.vertex_normal_buffer= MEM_mallocN(3*sizeof(float)*totvert, "normals");
        lod.triangle_index_buffer= MEM_mallocN(3*sizeof(int)*numTris, "trias");
@@ -4174,11 +4241,14 @@ static DerivedMesh *decimateModifier_applyModifier(
        MEM_freeN(lod.vertex_buffer);
        MEM_freeN(lod.vertex_normal_buffer);
        MEM_freeN(lod.triangle_index_buffer);
+#else
+       modifier_setError(md, "Modifier not working yet in 2.5.");
+       goto exit;
+#endif
 
 exit:
                return result;
 }
-#endif
 
 /* Smooth */
 
@@ -4216,7 +4286,7 @@ int smoothModifier_isDisabled(ModifierData *md)
        return 0;
 }
 
-CustomDataMask smoothModifier_requiredDataMask(ModifierData *md)
+CustomDataMask smoothModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        SmoothModifierData *smd = (SmoothModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -4368,7 +4438,7 @@ static void smoothModifier_do(
 
 static void smoothModifier_deformVerts(
                                       ModifierData *md, Object *ob, DerivedMesh *derivedData,
-          float (*vertexCos)[3], int numVerts)
+          float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm;
 
@@ -4445,7 +4515,7 @@ int castModifier_isDisabled(ModifierData *md)
        return 0;
 }
 
-CustomDataMask castModifier_requiredDataMask(ModifierData *md)
+CustomDataMask castModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CastModifierData *cmd = (CastModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -4948,7 +5018,7 @@ static void castModifier_cuboid_do(
 
 static void castModifier_deformVerts(
                                     ModifierData *md, Object *ob, DerivedMesh *derivedData,
-        float (*vertexCos)[3], int numVerts)
+        float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm = derivedData;
        CastModifierData *cmd = (CastModifierData *)md;
@@ -5076,7 +5146,7 @@ static void waveModifier_updateDepgraph(
        }
 }
 
-CustomDataMask waveModifier_requiredDataMask(ModifierData *md)
+CustomDataMask waveModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        WaveModifierData *wmd = (WaveModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -5116,12 +5186,11 @@ static void wavemod_get_texture_coords(WaveModifierData *wmd, Object *ob,
                        char *done = MEM_callocN(sizeof(*done) * numVerts,
                                        "get_texture_coords done");
                        int numFaces = dm->getNumFaces(dm);
+                       char uvname[32];
                        MTFace *tf;
 
-                       validate_layer_name(&dm->faceData, CD_MTFACE, wmd->uvlayer_name);
-
-                       tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE,
-                                       wmd->uvlayer_name);
+                       validate_layer_name(&dm->faceData, CD_MTFACE, wmd->uvlayer_name, uvname);
+                       tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
 
                        /* verts are given the UV from the first face that uses them */
                        for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
@@ -5351,7 +5420,7 @@ static void waveModifier_do(WaveModifierData *md,
 
 static void waveModifier_deformVerts(
                                     ModifierData *md, Object *ob, DerivedMesh *derivedData,
-        float (*vertexCos)[3], int numVerts)
+        float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm;
        WaveModifierData *wmd = (WaveModifierData *)md;
@@ -5413,7 +5482,7 @@ static void armatureModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(tamd->defgrp_name, amd->defgrp_name, 32);
 }
 
-CustomDataMask armatureModifier_requiredDataMask(ModifierData *md)
+CustomDataMask armatureModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CustomDataMask dataMask = 0;
 
@@ -5456,7 +5525,7 @@ static void armatureModifier_updateDepgraph(
 
 static void armatureModifier_deformVerts(
                                         ModifierData *md, Object *ob, DerivedMesh *derivedData,
-      float (*vertexCos)[3], int numVerts)
+      float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        ArmatureModifierData *amd = (ArmatureModifierData*) md;
 
@@ -5527,7 +5596,7 @@ static void hookModifier_copyData(ModifierData *md, ModifierData *target)
        strncpy(thmd->name, hmd->name, 32);
 }
 
-CustomDataMask hookModifier_requiredDataMask(ModifierData *md)
+CustomDataMask hookModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        HookModifierData *hmd = (HookModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -5577,7 +5646,7 @@ static void hookModifier_updateDepgraph(ModifierData *md, DagForest *forest, Sce
 
 static void hookModifier_deformVerts(
                                     ModifierData *md, Object *ob, DerivedMesh *derivedData,
-        float (*vertexCos)[3], int numVerts)
+        float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        HookModifierData *hmd = (HookModifierData*) md;
        float vec[3], mat[4][4];
@@ -5698,7 +5767,7 @@ static void hookModifier_deformVertsEM(
 
        if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
 
-       hookModifier_deformVerts(md, ob, derivedData, vertexCos, numVerts);
+       hookModifier_deformVerts(md, ob, derivedData, vertexCos, numVerts, 0, 0);
 
        if(!derivedData) dm->release(dm);
 }
@@ -5707,7 +5776,7 @@ static void hookModifier_deformVertsEM(
 
 static void softbodyModifier_deformVerts(
                                         ModifierData *md, Object *ob, DerivedMesh *derivedData,
-      float (*vertexCos)[3], int numVerts)
+      float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        sbObjectStep(md->scene, ob, (float)md->scene->r.cfra, vertexCos, numVerts);
 }
@@ -5786,7 +5855,7 @@ static void clothModifier_updateDepgraph(
        }
 }
 
-CustomDataMask clothModifier_requiredDataMask(ModifierData *md)
+CustomDataMask clothModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        CustomDataMask dataMask = 0;
 
@@ -5895,7 +5964,7 @@ static int collisionModifier_dependsOnTime(ModifierData *md)
 
 static void collisionModifier_deformVerts(
                                          ModifierData *md, Object *ob, DerivedMesh *derivedData,
-       float (*vertexCos)[3], int numVerts)
+       float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        CollisionModifierData *collmd = (CollisionModifierData*) md;
        DerivedMesh *dm = NULL;
@@ -6022,6 +6091,81 @@ static void collisionModifier_deformVerts(
 }
 
 
+
+/* Surface */
+
+static void surfaceModifier_initData(ModifierData *md) 
+{
+       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
+       
+       surmd->bvhtree = NULL;
+}
+
+static void surfaceModifier_freeData(ModifierData *md)
+{
+       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
+       
+       if (surmd)
+       {
+               if(surmd->bvhtree) {
+                       free_bvhtree_from_mesh(surmd->bvhtree);
+                       MEM_freeN(surmd->bvhtree);
+               }
+
+               if(surmd->dm)
+                       surmd->dm->release(surmd->dm);
+               
+               surmd->bvhtree = NULL;
+               surmd->dm = NULL;
+       }
+}
+
+static int surfaceModifier_dependsOnTime(ModifierData *md)
+{
+       return 1;
+}
+
+static void surfaceModifier_deformVerts(
+                                         ModifierData *md, Object *ob, DerivedMesh *derivedData,
+       float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
+{
+       SurfaceModifierData *surmd = (SurfaceModifierData*) md;
+       unsigned int numverts = 0, i = 0;
+       
+       if(surmd->dm)
+               surmd->dm->release(surmd->dm);
+
+       /* if possible use/create DerivedMesh */
+       if(derivedData) surmd->dm = CDDM_copy(derivedData);
+       else surmd->dm = get_original_dm(md->scene, ob, NULL, 0);
+       
+       if(!ob->pd)
+       {
+               printf("surfaceModifier_deformVerts: Should not happen!\n");
+               return;
+       }
+       
+       if(surmd->dm)
+       {
+               CDDM_apply_vert_coords(surmd->dm, vertexCos);
+               CDDM_calc_normals(surmd->dm);
+               
+               numverts = surmd->dm->getNumVerts ( surmd->dm );
+
+               /* convert to global coordinates */
+               for(i = 0; i<numverts; i++)
+                       Mat4MulVecfl(ob->obmat, CDDM_get_vert(surmd->dm, i)->co);
+
+               if(surmd->bvhtree)
+                       free_bvhtree_from_mesh(surmd->bvhtree);
+               else
+                       surmd->bvhtree = MEM_callocN(sizeof(BVHTreeFromMesh), "BVHTreeFromMesh");
+
+               bvhtree_from_mesh_faces(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
+       }
+}
+
+
 /* Boolean */
 
 static void booleanModifier_copyData(ModifierData *md, ModifierData *target)
@@ -6070,22 +6214,44 @@ static DerivedMesh *booleanModifier_applyModifier(
 {
        // XXX doesn't handle derived data
        BooleanModifierData *bmd = (BooleanModifierData*) md;
+       DerivedMesh *dm = mesh_get_derived_final(md->scene, bmd->object, CD_MASK_BAREMESH);
 
        /* we do a quick sanity check */
-       if(((Mesh *)ob->data)->totface > 3
-                   && bmd->object && ((Mesh *)bmd->object->data)->totface > 3) {
-               DerivedMesh *result = NewBooleanDerivedMesh(bmd->object, ob,
+       if(dm && (derivedData->getNumFaces(derivedData) > 3)
+                   && bmd->object && dm->getNumFaces(dm) > 3) {
+               DerivedMesh *result = NewBooleanDerivedMesh(dm, bmd->object, derivedData, ob,
                                1 + bmd->operation);
 
+               if(dm)
+                       dm->release(dm);
+
                /* if new mesh returned, return it; otherwise there was
                * an error, so delete the modifier object */
                if(result)
                        return result;
                else
                        bmd->object = NULL;
-                   }
+       }
+       
+       if(dm)
+                       dm->release(dm);
+
+       return derivedData;
+}
+
+CustomDataMask booleanModifier_requiredDataMask(Object *ob, ModifierData *md)
+{
+       CustomDataMask dataMask = (1 << CD_MTFACE) + (1 << CD_MEDGE);
+
+       dataMask |= (1 << CD_MDEFORMVERT);
+       
+       /* particles only need this if they are after a non deform modifier, and
+       * the modifier stack will only create them in that case. */
+//     dataMask |= CD_MASK_ORIGSPACE;
 
-                   return derivedData;
+//     dataMask |= CD_MASK_ORCO;
+       
+       return dataMask;
 }
 
 /* Particles */
@@ -6121,12 +6287,30 @@ static void particleSystemModifier_copyData(ModifierData *md, ModifierData *targ
        tpsmd->psys = psmd->psys;
 }
 
-CustomDataMask particleSystemModifier_requiredDataMask(ModifierData *md)
+CustomDataMask particleSystemModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
-       CustomDataMask dataMask = (1 << CD_MTFACE) + (1 << CD_MEDGE);
+       CustomDataMask dataMask = 0;
+       Material *ma;
+       MTex *mtex;
        int i;
 
+       if(!psmd->psys->part)
+               return 0;
+
+       ma= give_current_material(ob, psmd->psys->part->omat);
+       if(ma) {
+               for(i=0; i<MAX_MTEX; i++) {
+                       mtex=ma->mtex[i];
+                       if(mtex && (ma->septex & (1<<i))==0)
+                               if(mtex->pmapto && (mtex->texco & TEXCO_UV))
+                                       dataMask |= (1 << CD_MTFACE);
+               }
+       }
+
+       if(psmd->psys->part->tanfac!=0.0)
+               dataMask |= (1 << CD_MTFACE);
+
        /* ask for vertexgroups if we need them */
        for(i=0; i<PSYS_TOT_VG; i++){
                if(psmd->psys->vgroup[i]){
@@ -6143,33 +6327,11 @@ CustomDataMask particleSystemModifier_requiredDataMask(ModifierData *md)
        
        return dataMask;
 }
-static int is_last_displist(Object *ob)
-{
-       Curve *cu = ob->data;
-       static int curvecount=0, totcurve=0;
-
-       if(curvecount==0){
-               DispList *dl;
-
-               totcurve=0;
-               for(dl=cu->disp.first; dl; dl=dl->next){
-                       totcurve++;
-               }
-       }
-
-       curvecount++;
-
-       if(curvecount==totcurve){
-               curvecount=0;
-               return 1;
-       }
 
-       return 0;
-}
 /* saves the current emitter state for a particle system and calculates particles */
 static void particleSystemModifier_deformVerts(
                                               ModifierData *md, Object *ob, DerivedMesh *derivedData,
-           float (*vertexCos)[3], int numVerts)
+           float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm = derivedData;
        ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
@@ -6184,43 +6346,13 @@ static void particleSystemModifier_deformVerts(
        if(!psys_check_enabled(ob, psys))
                return;
 
-       if(dm==0){
-               if(ob->type==OB_MESH){
-                       dm = CDDM_from_mesh((Mesh*)(ob->data), ob);
-
-                       CDDM_apply_vert_coords(dm, vertexCos);
-                       //CDDM_calc_normals(dm);
-                       
-                       DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
-
-                       needsFree=1;
-               }
-               else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)){
-                       Object *tmpobj;
-                       Curve *tmpcu;
-
-                       if(is_last_displist(ob)){
-                               /* copies object and modifiers (but not the data) */
-                               tmpobj= copy_object( ob );
-                               tmpcu = (Curve *)tmpobj->data;
-                               tmpcu->id.us--;
-
-                               /* copies the data */
-                               tmpobj->data = copy_curve( (Curve *) ob->data );
-
-                               makeDispListCurveTypes(md->scene, tmpobj, 1 );
-                               nurbs_to_mesh( tmpobj );
-
-                               dm = CDDM_from_mesh((Mesh*)(tmpobj->data), tmpobj);
-                               //CDDM_calc_normals(dm);
+       if(dm==0) {
+               dm= get_original_dm(md->scene, ob, vertexCos, 1);
 
-                               free_libblock_us( &G.main->object, tmpobj );
+               if(!dm)
+                       return;
 
-                               needsFree=1;
-                       }
-                       else return;
-               }
-               else return;
+               needsFree= 1;
        }
 
        /* clear old dm */
@@ -6248,8 +6380,7 @@ static void particleSystemModifier_deformVerts(
                  psmd->dm->getNumFaces(psmd->dm)!=psmd->totdmface){
                /* in file read dm hasn't really changed but just wasn't saved in file */
 
-               psys->recalc |= PSYS_RECALC_HAIR;
-               psys->recalc |= PSYS_DISTR;
+               psys->recalc |= PSYS_RECALC_RESET;
                psmd->flag |= eParticleSystemFlag_DM_changed;
 
                psmd->totdmvert= psmd->dm->getNumVerts(psmd->dm);
@@ -6377,20 +6508,12 @@ static DerivedMesh * particleInstanceModifier_applyModifier(
        psys->lattice=psys_get_lattice(md->scene, ob, psys);
 
        if(psys->flag & (PSYS_HAIR_DONE|PSYS_KEYED)){
-               float co[3];
-               for(i=0; i< totvert; i++){
-                       dm->getVertCo(dm,i,co);
-                       if(i==0){
-                               min_co=max_co=co[track];
-                       }
-                       else{
-                               if(co[track]<min_co)
-                                       min_co=co[track];
 
-                               if(co[track]>max_co)
-                                       max_co=co[track];
-                       }
-               }
+               float min_r[3], max_r[3];
+               INIT_MINMAX(min_r, max_r);
+               dm->getMinMax(dm, min_r, max_r);                
+               min_co=min_r[track];
+               max_co=max_r[track];
        }
 
        result = CDDM_from_template(dm, maxvert,dm->getNumEdges(dm)*totpart,maxface);
@@ -6532,7 +6655,7 @@ static int explodeModifier_dependsOnTime(ModifierData *md)
 {
        return 1;
 }
-CustomDataMask explodeModifier_requiredDataMask(ModifierData *md)
+CustomDataMask explodeModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        ExplodeModifierData *emd= (ExplodeModifierData*) md;
        CustomDataMask dataMask = 0;
@@ -7198,7 +7321,7 @@ static DerivedMesh * explodeModifier_explodeMesh(ExplodeModifierData *emd,
                        pa= pars+i;
 
                        /* get particle state */
-                       psys_particle_on_emitter(psmd,part->from,pa->num,-1,pa->fuv,pa->foffset,loc0,nor,0,0,0,0);
+                       psys_particle_on_emitter(psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc0,nor,0,0,0,0);
                        Mat4MulVecfl(ob->obmat,loc0);
 
                        state.time=cfra;
@@ -7254,7 +7377,7 @@ static DerivedMesh * explodeModifier_explodeMesh(ExplodeModifierData *emd,
 
                *mf = source;
 
-               test_index_face(mf, &explode->faceData, i, (mf->v4 ? 4 : 3));
+               test_index_face(mf, &explode->faceData, i, (orig_v4 ? 4 : 3));
        }
 
        MEM_printmemlist_stats();
@@ -7446,7 +7569,7 @@ static void meshdeformModifier_copyData(ModifierData *md, ModifierData *target)
        tmmd->object = mmd->object;
 }
 
-CustomDataMask meshdeformModifier_requiredDataMask(ModifierData *md)
+CustomDataMask meshdeformModifier_requiredDataMask(Object *ob, ModifierData *md)
 {      
        MeshDeformModifierData *mmd = (MeshDeformModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -7550,6 +7673,7 @@ static void meshdeformModifier_do(
        DerivedMesh *tmpdm, *cagedm;
        MDeformVert *dvert = NULL;
        MDeformWeight *dw;
+       EditMesh *em = BKE_mesh_get_editmesh(me);
        MVert *cagemvert;
        float imat[4][4], cagemat[4][4], iobmat[4][4], icagemat[3][3], cmat[4][4];
        float weight, totweight, fac, co[3], *weights, (*dco)[3], (*bindcos)[3];
@@ -7559,13 +7683,22 @@ static void meshdeformModifier_do(
                return;
        
        /* get cage derivedmesh */
-       if(me->edit_mesh) {
-               tmpdm= editmesh_get_derived_cage_and_final(md->scene, ob, me->edit_mesh, &cagedm, 0);
+       if(em) {
+               tmpdm= editmesh_get_derived_cage_and_final(md->scene, ob, em, &cagedm, 0);
                if(tmpdm)
                        tmpdm->release(tmpdm);
+               BKE_mesh_end_editmesh(me, em);
        }
        else
                cagedm= mmd->object->derivedFinal;
+
+       /* if we don't have one computed, use derivedmesh from data
+        * without any modifiers */
+       if(!cagedm) {
+               cagedm= get_original_dm(md->scene, mmd->object, NULL, 0);
+               if(cagedm)
+                       cagedm->needsFree= 1;
+       }
        
        if(!cagedm)
                return;
@@ -7696,14 +7829,16 @@ static void meshdeformModifier_do(
 
 static void meshdeformModifier_deformVerts(
                                           ModifierData *md, Object *ob, DerivedMesh *derivedData,
-       float (*vertexCos)[3], int numVerts)
+       float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm;
 
-       if(!derivedData && ob->type==OB_MESH)
-               dm= CDDM_from_mesh(ob->data, ob);
-       else
-               dm= derivedData;
+       if (!derivedData) {
+               dm= get_original_dm(md->scene, ob, NULL, 0);
+               if (dm == NULL) return;
+       }
+       else dm= derivedData;
+
 
        modifier_vgroup_cache(md, vertexCos); /* if next modifier needs original vertices */
        
@@ -7775,6 +7910,8 @@ static DerivedMesh *multiresModifier_applyModifier(ModifierData *md, Object *ob,
                }
                CDDM_calc_normals(final);
 
+               MultiresDM_mark_as_modified(final);
+
                MEM_freeN(mmd->undo_verts);
                mmd->undo_signal = 0;
                mmd->undo_verts = NULL;
@@ -7813,7 +7950,7 @@ static void shrinkwrapModifier_copyData(ModifierData *md, ModifierData *target)
        tsmd->subsurfLevels = smd->subsurfLevels;
 }
 
-CustomDataMask shrinkwrapModifier_requiredDataMask(ModifierData *md)
+CustomDataMask shrinkwrapModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        ShrinkwrapModifierData *smd = (ShrinkwrapModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -7844,10 +7981,10 @@ static void shrinkwrapModifier_foreachObjectLink(ModifierData *md, Object *ob, O
        walk(userData, ob, &smd->auxTarget);
 }
 
-static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
+static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm = NULL;
-       CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(md);
+       CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(ob, md);
 
        /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */
        if(dataMask)
@@ -7857,7 +7994,7 @@ static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, Derived
                else if(ob->type==OB_LATTICE) dm = NULL;
                else return;
 
-               if(dm != NULL && (dataMask & CD_MVERT))
+               if(dm != NULL && (dataMask & (1<<CD_MVERT)))
                {
                        CDDM_apply_vert_coords(dm, vertexCos);
                        CDDM_calc_normals(dm);
@@ -7873,7 +8010,7 @@ static void shrinkwrapModifier_deformVerts(ModifierData *md, Object *ob, Derived
 static void shrinkwrapModifier_deformVertsEM(ModifierData *md, Object *ob, EditMesh *editData, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
 {
        DerivedMesh *dm = NULL;
-       CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(md);
+       CustomDataMask dataMask = shrinkwrapModifier_requiredDataMask(ob, md);
 
        if(dataMask)
        {
@@ -7882,7 +8019,7 @@ static void shrinkwrapModifier_deformVertsEM(ModifierData *md, Object *ob, EditM
                else if(ob->type==OB_LATTICE) dm = NULL;
                else return;
 
-               if(dm != NULL && (dataMask & CD_MVERT))
+               if(dm != NULL && (dataMask & (1<<CD_MVERT)))
                {
                        CDDM_apply_vert_coords(dm, vertexCos);
                        CDDM_calc_normals(dm);
@@ -7932,7 +8069,7 @@ static void simpledeformModifier_copyData(ModifierData *md, ModifierData *target
        memcpy(tsmd->limit, smd->limit, sizeof(tsmd->limit));
 }
 
-static CustomDataMask simpledeformModifier_requiredDataMask(ModifierData *md)
+static CustomDataMask simpledeformModifier_requiredDataMask(Object *ob, ModifierData *md)
 {
        SimpleDeformModifierData *smd = (SimpleDeformModifierData *)md;
        CustomDataMask dataMask = 0;
@@ -7958,10 +8095,10 @@ static void simpledeformModifier_updateDepgraph(ModifierData *md, DagForest *for
                dag_add_relation(forest, dag_get_node(forest, smd->origin), obNode, DAG_RL_OB_DATA, "SimpleDeform Modifier");
 }
 
-static void simpledeformModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
+static void simpledeformModifier_deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)
 {
        DerivedMesh *dm = NULL;
-       CustomDataMask dataMask = simpledeformModifier_requiredDataMask(md);
+       CustomDataMask dataMask = simpledeformModifier_requiredDataMask(ob, md);
 
        /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */
        if(dataMask)
@@ -7988,7 +8125,7 @@ static void simpledeformModifier_deformVerts(ModifierData *md, Object *ob, Deriv
 static void simpledeformModifier_deformVertsEM(ModifierData *md, Object *ob, EditMesh *editData, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
 {
        DerivedMesh *dm = NULL;
-       CustomDataMask dataMask = simpledeformModifier_requiredDataMask(md);
+       CustomDataMask dataMask = simpledeformModifier_requiredDataMask(ob, md);
 
        /* We implement requiredDataMask but thats not really usefull since mesh_calc_modifiers pass a NULL derivedData or without the modified vertexs applied */
        if(dataMask)
@@ -8181,7 +8318,7 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type)
                mti->flags = eModifierTypeFlag_AcceptsMesh;
                mti->initData = decimateModifier_initData;
                mti->copyData = decimateModifier_copyData;
-               //XXX mti->applyModifier = decimateModifier_applyModifier;
+               mti->applyModifier = decimateModifier_applyModifier;
 
                mti = INIT_TYPE(Smooth);
                mti->type = eModifierTypeType_OnlyDeform;
@@ -8275,16 +8412,24 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type)
                mti->deformVerts = collisionModifier_deformVerts;
                // mti->copyData = collisionModifier_copyData;
 
+               mti = INIT_TYPE(Surface);
+               mti->type = eModifierTypeType_OnlyDeform;
+               mti->initData = surfaceModifier_initData;
+               mti->flags = eModifierTypeFlag_AcceptsMesh;
+               mti->dependsOnTime = surfaceModifier_dependsOnTime;
+               mti->freeData = surfaceModifier_freeData; 
+               mti->deformVerts = surfaceModifier_deformVerts;
+
                mti = INIT_TYPE(Boolean);
                mti->type = eModifierTypeType_Nonconstructive;
                mti->flags = eModifierTypeFlag_AcceptsMesh
-                               | eModifierTypeFlag_RequiresOriginalData
                                | eModifierTypeFlag_UsesPointCache;
                mti->copyData = booleanModifier_copyData;
                mti->isDisabled = booleanModifier_isDisabled;
                mti->applyModifier = booleanModifier_applyModifier;
                mti->foreachObjectLink = booleanModifier_foreachObjectLink;
                mti->updateDepgraph = booleanModifier_updateDepgraph;
+               mti->requiredDataMask = booleanModifier_requiredDataMask;
 
                mti = INIT_TYPE(MeshDeform);
                mti->type = eModifierTypeType_OnlyDeform;
@@ -8592,7 +8737,20 @@ int modifiers_isParticleEnabled(Object *ob)
        return (md && md->mode & (eModifierMode_Realtime | eModifierMode_Render));
 }
 
-LinkNode *modifiers_calcDataMasks(ModifierData *md, CustomDataMask dataMask)
+int modifier_isEnabled(ModifierData *md, int required_mode)
+{
+       ModifierTypeInfo *mti = modifierType_getInfo(md->type);
+
+       if((md->mode & required_mode) != required_mode) return 0;
+       if(mti->isDisabled && mti->isDisabled(md)) return 0;
+       if(md->mode & eModifierMode_DisableTemporary) return 0;
+       if(required_mode & eModifierMode_Editmode)
+               if(!(mti->flags & eModifierTypeFlag_SupportsEditmode)) return 0;
+       
+       return 1;
+}
+
+LinkNode *modifiers_calcDataMasks(Object *ob, ModifierData *md, CustomDataMask dataMask, int required_mode)
 {
        LinkNode *dataMasks = NULL;
        LinkNode *curr, *prev;
@@ -8602,7 +8760,9 @@ LinkNode *modifiers_calcDataMasks(ModifierData *md, CustomDataMask dataMask)
                ModifierTypeInfo *mti = modifierType_getInfo(md->type);
                CustomDataMask mask = 0;
 
-               if(mti->requiredDataMask) mask = mti->requiredDataMask(md);
+               if(modifier_isEnabled(md, required_mode))
+                       if(mti->requiredDataMask)
+                               mask = mti->requiredDataMask(ob, md);
 
                BLI_linklist_prepend(&dataMasks, SET_INT_IN_POINTER(mask));
        }
@@ -8804,8 +8964,10 @@ void modifier_freeTemporaryData(ModifierData *md)
        if(md->type == eModifierType_Armature) {
                ArmatureModifierData *amd= (ArmatureModifierData*)md;
 
-               if(amd->prevCos)
+               if(amd->prevCos) {
                        MEM_freeN(amd->prevCos);
+                       amd->prevCos= NULL;
+               }
        }
 }