svn merge ^/trunk/blender -r40644:40720
[blender-staging.git] / source / blender / modifiers / intern / MOD_explode.c
index f1bc0d33fd8a4cdd8fbdb04eb373097f18695517..59461d18f8f138b85244fcc86cd58dcda16ec655 100644 (file)
 #include "DNA_meshdata_types.h"
 #include "DNA_scene_types.h"
 
+#include "BLI_utildefines.h"
 #include "BLI_kdtree.h"
 #include "BLI_rand.h"
 #include "BLI_math.h"
 #include "BLI_edgehash.h"
-#include "BLI_utildefines.h"
 
 #include "BKE_cdderivedmesh.h"
 #include "BKE_deform.h"
@@ -110,8 +110,8 @@ static void createFacepa(ExplodeModifierData *emd,
        int i,p,v1,v2,v3,v4=0;
 
        mvert = dm->getVertArray(dm);
-       mface = dm->getFaceArray(dm);
-       totface= dm->getNumFaces(dm);
+       mface = dm->getTessFaceArray(dm);
+       totface= dm->getNumTessFaces(dm);
        totvert= dm->getNumVerts(dm);
        totpart= psmd->psys->totpart;
 
@@ -200,8 +200,8 @@ static const short add_faces[24] = {
 
 static MFace *get_dface(DerivedMesh *dm, DerivedMesh *split, int cur, int i, MFace *mf)
 {
-       MFace *df = CDDM_get_face(split, cur);
-       DM_copy_face_data(dm, split, i, cur, 1);
+       MFace *df = CDDM_get_tessface(split, cur);
+       DM_copy_tessface_data(dm, split, i, cur, 1);
        *df = *mf;
        return df;
 }
@@ -543,21 +543,24 @@ static void remap_uvs_23(DerivedMesh *dm, DerivedMesh *split, int numlayer, int
        }
 }
 
-static DerivedMesh * cutEdges(ExplodeModifierData *emd, DerivedMesh *dm){
+static DerivedMesh * cutEdges(ExplodeModifierData *emd, DerivedMesh *dm)
+{
        DerivedMesh *splitdm;
        MFace *mf=NULL,*df1=NULL;
-       MFace *mface=dm->getFaceArray(dm);
+       MFace *mface=dm->getTessFaceArray(dm);
        MVert *dupve, *mv;
        EdgeHash *edgehash;
        EdgeHashIterator *ehi;
        int totvert=dm->getNumVerts(dm);
-       int totface=dm->getNumFaces(dm);
+       int totface=dm->getNumTessFaces(dm);
 
        int *facesplit = MEM_callocN(sizeof(int)*totface,"explode_facesplit");
        int *vertpa = MEM_callocN(sizeof(int)*totvert,"explode_vertpa2");
        int *facepa = emd->facepa;
        int *fs, totesplit=0,totfsplit=0,curdupface=0;
-       int i,j,v1,v2,v3,v4,esplit, v[4], uv[4];
+       int i,j,v1,v2,v3,v4,esplit,
+           v[4]  = {0, 0, 0, 0}, /* To quite gcc barking... */
+           uv[4] = {0, 0, 0, 0}; /* To quite gcc barking... */
        int numlayer;
 
        edgehash= BLI_edgehash_new();
@@ -627,7 +630,7 @@ static DerivedMesh * cutEdges(ExplodeModifierData *emd, DerivedMesh *dm){
        for(i=0,fs=facesplit; i<totface; i++,fs++)
                totfsplit += add_faces[*fs];
        
-       splitdm= CDDM_from_template(dm, totesplit, 0, totface+totfsplit);
+       splitdm= CDDM_from_template(dm, totesplit, 0, totface+totfsplit, 0, 0);
        numlayer = CustomData_number_of_layers(&splitdm->faceData, CD_MTFACE);
 
        /* copy new faces & verts (is it really this painful with custom data??) */
@@ -669,7 +672,7 @@ static DerivedMesh * cutEdges(ExplodeModifierData *emd, DerivedMesh *dm){
        curdupface=0;//=totface;
        //curdupin=totesplit;
        for(i=0,fs=facesplit; i<totface; i++,fs++){
-               mf = dm->getFaceData(dm, i, CD_MFACE);
+               mf = dm->getTessFaceData(dm, i, CD_MFACE);
 
                switch(*fs) {
                case 3:
@@ -759,7 +762,7 @@ static DerivedMesh * cutEdges(ExplodeModifierData *emd, DerivedMesh *dm){
        }
 
        for(i=0; i<curdupface; i++) {
-               mf = CDDM_get_face(splitdm, i);
+               mf = CDDM_get_tessface(splitdm, i);
                test_index_face(mf, &splitdm->faceData, i, (mf->flag & ME_FACE_SEL ? 4 : 3));
        }
 
@@ -767,8 +770,11 @@ static DerivedMesh * cutEdges(ExplodeModifierData *emd, DerivedMesh *dm){
        MEM_freeN(facesplit);
        MEM_freeN(vertpa);
 
-       return splitdm;
+       dm = CDDM_copy(splitdm, 1); /*builds ngon faces from tess (mface) faces*/
+       splitdm->needsFree = 1;
+       splitdm->release(splitdm);
 
+       return dm;
 }
 static DerivedMesh * explodeMesh(ExplodeModifierData *emd, 
                ParticleSystemModifierData *psmd, Scene *scene, Object *ob, 
@@ -776,14 +782,14 @@ static DerivedMesh * explodeMesh(ExplodeModifierData *emd,
 {
        DerivedMesh *explode, *dm=to_explode;
        MFace *mf= NULL, *mface;
-       ParticleSettings *part=psmd->psys->part;
+       /* ParticleSettings *part=psmd->psys->part; */ /* UNUSED */
        ParticleSimulationData sim= {NULL};
        ParticleData *pa=NULL, *pars=psmd->psys->particles;
-       ParticleKey state;
+       ParticleKey state, birth;
        EdgeHash *vertpahash;
        EdgeHashIterator *ehi;
        float *vertco= NULL, imat[4][4];
-       float loc0[3], nor[3];
+       float rot[4];
        float cfra;
        /* float timestep; */
        int *facepa=emd->facepa;
@@ -791,9 +797,9 @@ static DerivedMesh * explodeMesh(ExplodeModifierData *emd,
        int i, j, v, mindex=0;
        MTFace *mtface = NULL, *mtf;
 
-       totface= dm->getNumFaces(dm);
+       totface= dm->getNumTessFaces(dm);
        totvert= dm->getNumVerts(dm);
-       mface= dm->getFaceArray(dm);
+       mface= dm->getTessFaceArray(dm);
        totpart= psmd->psys->totpart;
 
        sim.scene= scene;
@@ -814,7 +820,7 @@ static DerivedMesh * explodeMesh(ExplodeModifierData *emd,
        for (i=0; i<totface; i++) {
                /* do mindex + totvert to ensure the vertex index to be the first
                 * with BLI_edgehashIterator_getKey */
-               if(facepa[i]==totpart || cfra <= (pars+facepa[i])->time)
+               if(facepa[i]==totpart || cfra < (pars+facepa[i])->time)
                        mindex = totvert+totpart;
                else 
                        mindex = totvert+facepa[i];
@@ -838,7 +844,7 @@ static DerivedMesh * explodeMesh(ExplodeModifierData *emd,
        BLI_edgehashIterator_free(ehi);
 
        /* the final duplicated vertices */
-       explode= CDDM_from_template(dm, totdup, 0,totface);
+       explode= CDDM_from_template(dm, totdup, 0,totface, 0, 0);
        mtface = CustomData_get_layer_named(&explode->faceData, CD_MTFACE, emd->uvname);
        /*dupvert= CDDM_get_verts(explode);*/
 
@@ -868,26 +874,26 @@ static DerivedMesh * explodeMesh(ExplodeModifierData *emd,
                        /* get particle */
                        pa= pars+i;
 
-                       /* get particle state */
-                       psys_particle_on_emitter(psmd,part->from,pa->num,pa->num_dmcache,pa->fuv,pa->foffset,loc0,nor,NULL,NULL,NULL,NULL);
-                       mul_m4_v3(ob->obmat,loc0);
+                       psys_get_birth_coordinates(&sim, pa, &birth, 0, 0);
 
                        state.time=cfra;
                        psys_get_particle_state(&sim, i, &state, 1);
 
                        vertco=CDDM_get_vert(explode,v)->co;
-                       
                        mul_m4_v3(ob->obmat,vertco);
 
-                       VECSUB(vertco,vertco,loc0);
+                       sub_v3_v3(vertco, birth.co);
 
                        /* apply rotation, size & location */
-                       mul_qt_v3(state.rot,vertco);
+                       sub_qt_qtqt(rot, state.rot, birth.rot);
+                       mul_qt_v3(rot, vertco);
+
                        if(emd->flag & eExplodeFlag_PaSize)
                                mul_v3_fl(vertco,pa->size);
-                       VECADD(vertco,vertco,state.co);
 
-                       mul_m4_v3(imat,vertco);
+                       add_v3_v3(vertco, state.co);
+
+                       mul_m4_v3(imat, vertco);
                }
        }
        BLI_edgehashIterator_free(ehi);
@@ -906,12 +912,12 @@ static DerivedMesh * explodeMesh(ExplodeModifierData *emd,
                        if(pa->alive==PARS_DEAD && (emd->flag&eExplodeFlag_Dead)==0) continue;
                }
 
-               dm->getFace(dm,i,&source);
-               mf=CDDM_get_face(explode,i);
+               dm->getTessFace(dm,i,&source);
+               mf=CDDM_get_tessface(explode,i);
                
                orig_v4 = source.v4;
 
-               if(facepa[i]!=totpart && cfra <= pa->time)
+               if(facepa[i]!=totpart && cfra < pa->time)
                        mindex = totvert+totpart;
                else 
                        mindex = totvert+facepa[i];
@@ -922,7 +928,7 @@ static DerivedMesh * explodeMesh(ExplodeModifierData *emd,
                if(source.v4)
                        source.v4 = edgecut_get(vertpahash, source.v4, mindex);
 
-               DM_copy_face_data(dm,explode,i,i,1);
+               DM_copy_tessface_data(dm,explode,i,i,1);
 
                *mf = source;
 
@@ -946,6 +952,7 @@ static DerivedMesh * explodeMesh(ExplodeModifierData *emd,
 
        /* finalization */
        CDDM_calc_edges(explode);
+       CDDM_tessfaces_to_faces(explode);
        CDDM_calc_normals(explode);
 
        if(psmd->psys->lattice){
@@ -987,7 +994,7 @@ static DerivedMesh * applyModifier(ModifierData *md, Object *ob,
                if(emd->facepa == NULL
                                 || psmd->flag&eParticleSystemFlag_Pars
                                 || emd->flag&eExplodeFlag_CalcFaces
-                                || MEM_allocN_len(emd->facepa)/sizeof(int) != dm->getNumFaces(dm))
+                                || MEM_allocN_len(emd->facepa)/sizeof(int) != dm->getNumTessFaces(dm))
                {
                        if(psmd->flag & eParticleSystemFlag_Pars)
                                psmd->flag &= ~eParticleSystemFlag_Pars;
@@ -1037,4 +1044,5 @@ ModifierTypeInfo modifierType_Explode = {
        /* dependsOnNormals */  NULL,
        /* foreachObjectLink */ NULL,
        /* foreachIDLink */     NULL,
+       /* foreachTexLink */    NULL,
 };