New: Editable cache. Fixed: Don't clear cache on e.g. lamp moving. Changed: Switched...
authorDaniel Genrich <daniel.genrich@gmx.net>
Thu, 24 Jan 2008 10:43:12 +0000 (10:43 +0000)
committerDaniel Genrich <daniel.genrich@gmx.net>
Thu, 24 Jan 2008 10:43:12 +0000 (10:43 +0000)
source/blender/blenkernel/BKE_cloth.h
source/blender/blenkernel/intern/cloth.c
source/blender/blenkernel/intern/implicit.c
source/blender/blenkernel/intern/modifier.c
source/blender/src/buttons_object.c
source/blender/src/editmesh.c
source/blender/src/transform_conversions.c
source/blender/src/vpaint.c

index 4e704098ebd149d1dcf2d7022dae69b72f37bbb0..e3fdb9e0f25ea73d4490caa9479025636ba2b16b 100644 (file)
@@ -160,16 +160,6 @@ typedef enum
 #define CLOTH_VERT_FLAG_PINNED 1
 #define CLOTH_VERT_FLAG_COLLISION 2
 
-
-// needed for buttons_object.c
-void cloth_clear_cache(Object *ob, ClothModifierData *clmd, float framenr);
-
-// needed for cloth.c
-void implicit_set_positions ( ClothModifierData *clmd );
-
-// from cloth.c, needed for modifier.c
-void clothModifier_do ( ClothModifierData *clmd, Object *ob, DerivedMesh *dm, float ( *vertexCos ) [3], int numverts );
-
 typedef void ( *CM_COLLISION_RESPONSE ) ( ClothModifierData *clmd, CollisionModifierData *collmd, CollisionTree *tree1, CollisionTree *tree2 );
 
 
@@ -193,6 +183,7 @@ int bvh_traverse ( ClothModifierData * clmd, CollisionModifierData * collmd, Col
 int implicit_init ( Object *ob, ClothModifierData *clmd );
 int implicit_free ( ClothModifierData *clmd );
 int implicit_solver ( Object *ob, float frame, ClothModifierData *clmd, ListBase *effectors );
+void implicit_set_positions ( ClothModifierData *clmd );
 ////////////////////////////////////////////////
 
 
@@ -204,13 +195,16 @@ int implicit_solver ( Object *ob, float frame, ClothModifierData *clmd, ListBase
 void cloth_free_modifier_extern (ClothModifierData *clmd);
 void cloth_free_modifier (Object *ob, ClothModifierData *clmd);
 void cloth_init (ClothModifierData *clmd);
-void cloth_deform_verts (struct Object *ob, float framenr, float ( *vertexCos ) [3], int numVerts, void *derivedData, ClothModifierData *clmd);
+DerivedMesh *clothModifier_do(ClothModifierData *clmd,Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc);
 
 void cloth_update_normals (ClothVertex *verts, int nVerts, MFace *face, int totface);
 
 // needed for collision.c
 void bvh_update_from_cloth(ClothModifierData *clmd, int moving);
 
+// needed for editmesh.c
+void cloth_write_cache(Object *ob, ClothModifierData *clmd, float framenr);
+
 ////////////////////////////////////////////////
 
 
index df57440996ade10952da33b1fd03e6c5ab89b533..f22fd28d96eaefbe55bf9d00b8fb4509de145907 100644 (file)
@@ -121,9 +121,9 @@ static CM_SOLVER_DEF        solvers [] =
 /* ********** cloth engine ******* */
 /* Prototypes for internal functions.
 */
-static void cloth_to_object ( Object *ob, ClothModifierData *clmd, float ( *vertexCos ) [3], unsigned int numverts );
+static void cloth_to_object (Object *ob,  ClothModifierData *clmd, DerivedMesh *dm);
 static void cloth_from_mesh ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm );
-static int cloth_from_object ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float ( *vertexCos ) [3], unsigned int numverts, float framenr );
+static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr);
 int cloth_build_springs ( ClothModifierData *clmd, DerivedMesh *dm );
 static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm, short vgroup );
 
@@ -545,7 +545,7 @@ void cloth_clear_cache(Object *ob, ClothModifierData *clmd, float framenr)
                cloth_read_cache(ob, clmd, framenr);
        }
 }
-static void cloth_write_cache(Object *ob, ClothModifierData *clmd, float framenr)
+void cloth_write_cache(Object *ob, ClothModifierData *clmd, float framenr)
 {
        FILE *fp = NULL;
        int stack_index = -1;
@@ -579,26 +579,46 @@ static void cloth_write_cache(Object *ob, ClothModifierData *clmd, float framenr
 
 
 
-/**
-* cloth_deform_verts - simulates one step, framenr is in frames.
-*
-**/
-void clothModifier_do ( ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
-                        float ( *vertexCos ) [3], int numverts )
+/************************************************
+ * clothModifier_do - main simulation function
+************************************************/
+DerivedMesh *clothModifier_do(ClothModifierData *clmd,Object *ob, DerivedMesh *dm, int useRenderParams, int isFinalCalc)
+
 {
        unsigned int i;
        Cloth *cloth = clmd->clothObject;
        unsigned int framenr = ( float ) G.scene->r.cfra;
        float current_time = bsystem_time ( ob, ( float ) G.scene->r.cfra, 0.0 );
-       ListBase        *effectors = NULL;
+       ListBase *effectors = NULL;
        ClothVertex *verts = NULL;
        float deltaTime = current_time - clmd->sim_parms->sim_time;
+       unsigned int numverts = -1;
+       unsigned int numedges = -1;
+       unsigned int numfaces = -1;
+       MVert *mvert = NULL;
+       MEdge *medge = NULL;
+       MFace *mface = NULL;
+       DerivedMesh *result = NULL;
+       
+       result = CDDM_copy(dm);
+       
+       if(!result)
+       {
+               return dm;
+       }
+       
+       numverts = result->getNumVerts(result);
+       numedges = result->getNumEdges(result);
+       numfaces = result->getNumFaces(result);
+       mvert = CDDM_get_verts(result);
+       medge = CDDM_get_edges(result);
+       mface = CDDM_get_faces(result);
        
        // only be active during a specific period:
        // that's "first frame" and "last frame" on GUI
        if ( current_time < clmd->sim_parms->firstframe )
        {
-               return;
+               return result;
        }
        else if ( current_time > clmd->sim_parms->lastframe )
        {
@@ -609,10 +629,10 @@ void clothModifier_do ( ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
                        if(cloth_read_cache(ob, clmd, framenr))
                        {
                                // Copy the result back to the object.
-                               cloth_to_object ( ob, clmd, vertexCos, numverts );
+                               cloth_to_object (ob, clmd, result);
                        }
                }
-               return;
+               return result;
        }
        
        // printf("ct: %f, st: %f, r.cfra: %f, dt: %f\n", current_time, clmd->sim_parms->sim_time, ( float ) G.scene->r.cfra, deltaTime);
@@ -639,11 +659,11 @@ void clothModifier_do ( ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
                {
                        cloth_clear_cache(ob, clmd, 0);
                        
-                       if ( !cloth_from_object ( ob, clmd, dm, vertexCos, numverts, framenr ) )
-                               return;
+                       if ( !cloth_from_object ( ob, clmd, result, framenr ) )
+                               return result;
 
                        if ( clmd->clothObject == NULL )
-                               return;
+                               return result;
 
                        cloth = clmd->clothObject;
                }
@@ -665,7 +685,7 @@ void clothModifier_do ( ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
                                        VECCOPY ( verts->txold, verts->x );
 
                                        // Get the current position.
-                                       VECCOPY ( verts->xconst, vertexCos[i] );
+                                       VECCOPY ( verts->xconst, mvert[i].co );
                                        Mat4MulVecfl ( ob->obmat, verts->xconst );
                                }
 
@@ -679,14 +699,10 @@ void clothModifier_do ( ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
                                // printf ( "Cloth simulation time: %f\n", ( float ) tval() );
 
                                cloth_write_cache(ob, clmd, framenr);
-
                        }
                        
                        // Copy the result back to the object.
-                       cloth_to_object ( ob, clmd, vertexCos, numverts );
-
-                       // bvh_free(clmd->clothObject->tree);
-                       // clmd->clothObject->tree = bvh_build(clmd, clmd->coll_parms->epsilon);
+                       cloth_to_object (ob, clmd, result);
                }
 
        }
@@ -696,7 +712,7 @@ void clothModifier_do ( ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
                {
                        if(cloth_read_cache(ob, clmd, framenr))
                        {
-                               cloth_to_object ( ob, clmd, vertexCos, numverts );
+                               cloth_to_object (ob, clmd, result);
                        }
                }
                else
@@ -704,6 +720,8 @@ void clothModifier_do ( ClothModifierData *clmd, Object *ob, DerivedMesh *dm,
                        cloth_clear_cache(ob, clmd, 0);
                }
        }
+       
+       return result;
 
 }
 
@@ -783,59 +801,55 @@ void cloth_free_modifier_extern ( ClothModifierData *clmd )
                return;
 
        cloth = clmd->clothObject;
+       
+       if ( cloth )
+       {       
+               // If our solver provides a free function, call it
+               if ( cloth->old_solver_type < 255 && solvers [cloth->old_solver_type].free )
+               {
+                       solvers [cloth->old_solver_type].free ( clmd );
+               }
 
-       if ( ! ( clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_CCACHE_PROTECT ) )
-       {
-               if ( cloth )
-               {       
-                       // If our solver provides a free function, call it
-                       if ( cloth->old_solver_type < 255 && solvers [cloth->old_solver_type].free )
-                       {
-                               solvers [cloth->old_solver_type].free ( clmd );
-                       }
-
-                       // Free the verts.
-                       if ( cloth->verts != NULL )
-                               MEM_freeN ( cloth->verts );
+               // Free the verts.
+               if ( cloth->verts != NULL )
+                       MEM_freeN ( cloth->verts );
 
-                       cloth->verts = NULL;
-                       cloth->numverts = 0;
+               cloth->verts = NULL;
+               cloth->numverts = 0;
 
-                       // Free the springs.
-                       if ( cloth->springs != NULL )
+               // Free the springs.
+               if ( cloth->springs != NULL )
+               {
+                       LinkNode *search = cloth->springs;
+                       while(search)
                        {
-                               LinkNode *search = cloth->springs;
-                               while(search)
-                               {
-                                       ClothSpring *spring = search->link;
-                                                       
-                                       MEM_freeN ( spring );
-                                       search = search->next;
-                               }
-                               BLI_linklist_free(cloth->springs, NULL);
-                       
-                               cloth->springs = NULL;
+                               ClothSpring *spring = search->link;
+                                               
+                               MEM_freeN ( spring );
+                               search = search->next;
                        }
-
+                       BLI_linklist_free(cloth->springs, NULL);
+               
                        cloth->springs = NULL;
-                       cloth->numsprings = 0;
+               }
 
-                       // free BVH collision tree
-                       if ( cloth->tree )
-                               bvh_free ( ( BVH * ) cloth->tree );
+               cloth->springs = NULL;
+               cloth->numsprings = 0;
 
-                       // we save our faces for collision objects
-                       if ( cloth->mfaces )
-                               MEM_freeN ( cloth->mfaces );
-                       /*
-                       if(clmd->clothObject->facemarks)
-                       MEM_freeN(clmd->clothObject->facemarks);
-                       */
-                       MEM_freeN ( cloth );
-                       clmd->clothObject = NULL;
-               }
+               // free BVH collision tree
+               if ( cloth->tree )
+                       bvh_free ( ( BVH * ) cloth->tree );
+
+               // we save our faces for collision objects
+               if ( cloth->mfaces )
+                       MEM_freeN ( cloth->mfaces );
+               /*
+               if(clmd->clothObject->facemarks)
+               MEM_freeN(clmd->clothObject->facemarks);
+               */
+               MEM_freeN ( cloth );
+               clmd->clothObject = NULL;
        }
-       clmd->sim_parms->flags &= ~CLOTH_SIMSETTINGS_FLAG_RESET;
 }
 
 /******************************************************************************
@@ -847,24 +861,25 @@ void cloth_free_modifier_extern ( ClothModifierData *clmd )
 /**
 * cloth_to_object - copies the deformed vertices to the object.
 *
-* This function is a modified version of the softbody.c:softbody_to_object() function.
 **/
-static void cloth_to_object ( Object *ob, ClothModifierData *clmd, float ( *vertexCos ) [3], unsigned int numverts )
+static void cloth_to_object (Object *ob,  ClothModifierData *clmd, DerivedMesh *dm)
 {
-       ClothVertex     *verts = NULL;
        unsigned int    i = 0;
+       MVert *mvert = NULL;
+       unsigned int numverts;
+       Cloth *cloth = clmd->clothObject;
 
-       if ( clmd->clothObject )
-       {
-               verts = clmd->clothObject->verts;
-
+       if (clmd->clothObject) {
                /* inverse matrix is not uptodate... */
-               Mat4Invert ( ob->imat, ob->obmat );
+               Mat4Invert (ob->imat, ob->obmat);
+
+               mvert = CDDM_get_verts(dm);
+               numverts = dm->getNumVerts(dm);
 
-               for ( i = 0; i < numverts; i++, verts++ )
+               for (i = 0; i < numverts; i++)
                {
-                       VECCOPY ( vertexCos[i], verts->x );
-                       Mat4MulVecfl ( ob->imat, vertexCos[i] );        /* cloth is in global coords */
+                       VECCOPY (mvert[i].co, cloth->verts[i].x);
+                       Mat4MulVecfl (ob->imat, mvert[i].co);   /* cloth is in global coords */
                }
        }
 }
@@ -948,20 +963,18 @@ float cloth_globallen ( float *v1,float *v2,Object *ob )
        return VecLenf ( p1,p2 );
 }
 
-// only meshes supported at the moment
-static int cloth_from_object ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float ( *vertexCos ) [3], unsigned int numverts, float framenr )
+static int cloth_from_object(Object *ob, ClothModifierData *clmd, DerivedMesh *dm, float framenr)
 {
        unsigned int i = 0;
-       // dm->getNumVerts(dm);
-       MVert *mvert = NULL; // CDDM_get_verts(dm);
+       MVert *mvert = NULL;
        ClothVertex *verts = NULL;
        float tnull[3] = {0,0,0};
 
-       /* If we have a clothObject, free it. */
+       // If we have a clothObject, free it. 
        if ( clmd->clothObject != NULL )
                cloth_free_modifier ( ob, clmd );
 
-       /* Allocate a new cloth object. */
+       // Allocate a new cloth object.
        clmd->clothObject = MEM_callocN ( sizeof ( Cloth ), "cloth" );
        if ( clmd->clothObject )
        {
@@ -974,88 +987,74 @@ static int cloth_from_object ( Object *ob, ClothModifierData *clmd, DerivedMesh
                return 0;
        }
 
-       switch ( ob->type )
-       {
-               case OB_MESH:
-
-                       // mesh input objects need DerivedMesh
-                       if ( !dm )
-                               return 0;
+       // mesh input objects need DerivedMesh
+       if ( !dm )
+               return 0;
 
-                       cloth_from_mesh ( ob, clmd, dm );
+       cloth_from_mesh ( ob, clmd, dm );
 
-                       if ( clmd->clothObject != NULL )
-                       {
-                               /* create springs */
-                               clmd->clothObject->springs = NULL;
-                               clmd->clothObject->numsprings = -1;
-                               
-                               mvert = CDDM_get_verts ( dm );
-                               verts = clmd->clothObject->verts;
-
-                               /* set initial values */
-                               for ( i = 0; i < numverts; i++, verts++ )
-                               {
-                                       VECCOPY ( verts->x, mvert[i].co );
-                                       Mat4MulVecfl ( ob->obmat, verts->x );
+       if ( clmd->clothObject != NULL )
+       {
+               // create springs 
+               clmd->clothObject->springs = NULL;
+               clmd->clothObject->numsprings = -1;
+               
+               mvert = CDDM_get_verts ( dm );
+               verts = clmd->clothObject->verts;
 
-                                       verts->mass = clmd->sim_parms->mass;
+               // set initial values
+               for ( i = 0; i < dm->getNumVerts(dm); i++, verts++ )
+               {
+                       VECCOPY ( verts->x, mvert[i].co );
+                       Mat4MulVecfl ( ob->obmat, verts->x );
 
-                                       if ( clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL )
-                                               verts->goal= clmd->sim_parms->defgoal;
-                                       else
-                                               verts->goal= 0.0f;
+                       verts->mass = clmd->sim_parms->mass;
 
-                                       verts->flags = 0;
-                                       VECCOPY ( verts->xold, verts->x );
-                                       VECCOPY ( verts->xconst, verts->x );
-                                       VECCOPY ( verts->txold, verts->x );
-                                       VecMulf ( verts->v, 0.0f );
+                       if ( clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_GOAL )
+                               verts->goal= clmd->sim_parms->defgoal;
+                       else
+                               verts->goal= 0.0f;
 
-                                       verts->impulse_count = 0;
-                                       VECCOPY ( verts->impulse, tnull );
-                               }
-                               
-                               if ( !cloth_build_springs ( clmd, dm ) )
-                               {
-                                       cloth_free_modifier ( ob, clmd );
-                                       modifier_setError ( & ( clmd->modifier ), "Can't build springs." );
-                                       return 0;
-                               }
+                       verts->flags = 0;
+                       VECCOPY ( verts->xold, verts->x );
+                       VECCOPY ( verts->xconst, verts->x );
+                       VECCOPY ( verts->txold, verts->x );
+                       VecMulf ( verts->v, 0.0f );
 
-                               // apply / set vertex groups
-                               if ( clmd->sim_parms->vgroup_mass > 0 )
-                                       cloth_apply_vgroup ( clmd, dm, clmd->sim_parms->vgroup_mass );
+                       verts->impulse_count = 0;
+                       VECCOPY ( verts->impulse, tnull );
+               }
+               
+               if ( !cloth_build_springs ( clmd, dm ) )
+               {
+                       cloth_free_modifier ( ob, clmd );
+                       modifier_setError ( & ( clmd->modifier ), "Can't build springs." );
+                       return 0;
+               }
 
-                               // init our solver
-                               if ( solvers [clmd->sim_parms->solver_type].init )
-                                       solvers [clmd->sim_parms->solver_type].init ( ob, clmd );
+               // apply / set vertex groups
+               if ( clmd->sim_parms->vgroup_mass > 0 )
+                       cloth_apply_vgroup ( clmd, dm, clmd->sim_parms->vgroup_mass );
 
-                               clmd->clothObject->tree = bvh_build_from_cloth ( clmd, clmd->coll_parms->epsilon );
+               // init our solver
+               if ( solvers [clmd->sim_parms->solver_type].init )
+                       solvers [clmd->sim_parms->solver_type].init ( ob, clmd );
 
-                               cloth_write_cache(ob, clmd, framenr-1);
-                       }
+               clmd->clothObject->tree = bvh_build_from_cloth ( clmd, clmd->coll_parms->epsilon );
 
-                       return 1;
-               case OB_LATTICE:
-                       printf ( "Not supported: OB_LATTICE\n" );
-                       // lattice_to_softbody(ob);
-                       return 0;
-               case OB_CURVE:
-               case OB_SURF:
-                       printf ( "Not supported: OB_SURF| OB_CURVE\n" );
-                       return 0;
-               default: return 0; // TODO - we do not support changing meshes
+               cloth_write_cache(ob, clmd, framenr-1);
+               
+               return 1;
        }
-
        return 0;
 }
 
+
 static void cloth_from_mesh ( Object *ob, ClothModifierData *clmd, DerivedMesh *dm )
 {
        unsigned int numverts = dm->getNumVerts ( dm );
        unsigned int numfaces = dm->getNumFaces ( dm );
-       MFace *mface = dm->getFaceArray ( dm );
+       MFace *mface = CDDM_get_faces(dm);
        unsigned int i = 0;
 
        /* Allocate our vertices.
index 3f21bf2bfc5c730def92a9d92589106a5a24a803..69cc3b165c2535ec4cecfbb90535d2916defb1bb 100644 (file)
@@ -569,33 +569,35 @@ DO_INLINE void mul_bfmatrix_S(fmatrix3x3 *matrix, float scalar)
 
 /* SPARSE SYMMETRIC multiply big matrix with long vector*/
 /* STATUS: verified */
-DO_INLINE void mul_bfmatrix_lfvector( float (*to)[3], fmatrix3x3 *from, float (*fLongVector)[3])
+DO_INLINE void mul_bfmatrix_lfvector( float (*to)[3], fmatrix3x3 *from, lfVector *fLongVector)
 {
        unsigned int i = 0;
+       lfVector *temp = create_lfvector(from[0].vcount);
+       
        zero_lfvector(to, from[0].vcount);
-       /* process diagonal elements */ 
-       for(i = 0; i < from[0].vcount; i++)
-       {
-               muladd_fmatrix_fvector(to[from[i].r], from[i].m, fLongVector[from[i].c]);       
-       }
 
-       /* process off-diagonal entries (every off-diagonal entry needs to be symmetric) */
-       // TODO: pragma below is wrong, correct it!
-       // #pragma omp parallel for shared(to,from, fLongVector) private(i) 
-       for(i = from[0].vcount; i < from[0].vcount+from[0].scount; i++)
+#pragma omp parallel sections private(i)
        {
-               // muladd_fmatrix_fvector(to[from[i].c], from[i].m, fLongVector[from[i].r]);
-               
-               to[from[i].c][0] += INPR(from[i].m[0],fLongVector[from[i].r]);
-               to[from[i].c][1] += INPR(from[i].m[1],fLongVector[from[i].r]);
-               to[from[i].c][2] += INPR(from[i].m[2],fLongVector[from[i].r]);  
-               
-               // muladd_fmatrix_fvector(to[from[i].r], from[i].m, fLongVector[from[i].c]);
-               
-               to[from[i].r][0] += INPR(from[i].m[0],fLongVector[from[i].c]);
-               to[from[i].r][1] += INPR(from[i].m[1],fLongVector[from[i].c]);
-               to[from[i].r][2] += INPR(from[i].m[2],fLongVector[from[i].c]);  
+#pragma omp section
+               {
+                       for(i = from[0].vcount; i < from[0].vcount+from[0].scount; i++)
+                       {
+                               muladd_fmatrix_fvector(to[from[i].c], from[i].m, fLongVector[from[i].r]);
+                       }
+               }       
+#pragma omp section
+               {
+                       for(i = 0; i < from[0].vcount+from[0].scount; i++)
+                       {
+                               muladd_fmatrix_fvector(temp[from[i].r], from[i].m, fLongVector[from[i].c]);
+                       }
+               }
        }
+       add_lfvector_lfvector(to, to, temp, from[0].vcount);
+       
+       del_lfvector(temp);
+       
+       
 }
 
 /* SPARSE SYMMETRIC multiply big matrix with long vector (for diagonal preconditioner) */
@@ -1088,7 +1090,6 @@ DO_INLINE void cloth_calc_spring_force(ClothModifierData *clmd, ClothSpring *s,
        float bending_force[3] = {0,0,0};
        float damping_force[3] = {0,0,0};
        float nulldfdx[3][3]={ {0,0,0}, {0,0,0}, {0,0,0}};
-       Cloth *cloth = clmd->clothObject;
        
        VECCOPY(s->f, nullf);
        cp_fmatrix(s->dfdx, nulldfdx);
index 3ea4d4fb58491259df22e2de9f3b84f01afd69a2..428c136a7fe7bad1350af77fc54fd2e499e1e248 100644 (file)
@@ -4958,29 +4958,23 @@ static void clothModifier_initData(ModifierData *md)
        cloth_init (clmd);
 }
 
-static void clothModifier_deformVerts(
-               ModifierData *md, Object *ob, DerivedMesh *derivedData,
-               float (*vertexCos)[3], int numVerts)
+static DerivedMesh *clothModifier_applyModifier(ModifierData *md, Object *ob,
+                DerivedMesh *derivedData, int useRenderParams, int isFinalCalc)
 {
-       DerivedMesh *dm = NULL;
-
-       /* if possible use/create DerivedMesh */
-       
-       if(derivedData) dm = CDDM_copy(derivedData);
-       else if(ob->type==OB_MESH) dm = CDDM_from_mesh(ob->data, ob);
+       ClothModifierData *clmd = (ClothModifierData*) md;
+       DerivedMesh *result=NULL;
        
-       if(dm)
+       /* check for alloc failing */
+       if(!clmd->sim_parms || !clmd->coll_parms)
+               return derivedData;
+
+       result = clothModifier_do(clmd, ob, derivedData, useRenderParams, isFinalCalc);
+
+       if(result)
        {
-               CDDM_apply_vert_coords(dm, vertexCos);
-               CDDM_calc_normals(dm);
+               return result;
        }
-       
-       /* TODO: check for sim_parms / coll_parms NOT NULL */
-       
-       clothModifier_do((ClothModifierData *)md, ob, dm, vertexCos, numVerts);
-       
-       if(dm)
-               dm->release(dm);
+       return derivedData;
 }
 
 static void clothModifier_updateDepgraph(
@@ -5035,8 +5029,6 @@ static int clothModifier_dependsOnTime(ModifierData *md)
 static void clothModifier_freeData(ModifierData *md)
 {
        ClothModifierData *clmd = (ClothModifierData*) md;
-       Object *ob = NULL;
-       ClothModifierData *clmd2 = NULL;
        
        if (clmd) 
        {
@@ -7017,10 +7009,10 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type)
                mti->deformVerts = softbodyModifier_deformVerts;
        
                mti = INIT_TYPE(Cloth);
-               mti->type = eModifierTypeType_OnlyDeform;
+               mti->type = eModifierTypeType_Nonconstructive;
                mti->initData = clothModifier_initData;
-               mti->flags = eModifierTypeFlag_AcceptsCVs;
-                                       // | eModifierTypeFlag_RequiresOriginalData;
+               mti->flags = eModifierTypeFlag_AcceptsMesh
+                               | eModifierTypeFlag_RequiresOriginalData;
                                        // | eModifierTypeFlag_SupportsMapping
                                        // | eModifierTypeFlag_SupportsEditmode 
                                        // | eModifierTypeFlag_EnableInEditmode;
@@ -7028,7 +7020,8 @@ ModifierTypeInfo *modifierType_getInfo(ModifierType type)
                mti->freeData = clothModifier_freeData; 
                mti->requiredDataMask = clothModifier_requiredDataMask;
                // mti->copyData = clothModifier_copyData;
-               mti->deformVerts = clothModifier_deformVerts;
+               // mti->deformVerts = clothModifier_deformVerts;
+               mti->applyModifier = clothModifier_applyModifier;
                mti->updateDepgraph = clothModifier_updateDepgraph;
                
                mti = INIT_TYPE(Collision);
index 1d7a1972d362c84764be1185e2ed6ab61d27df29..a064e266d6d8c15aa2394f5e9ee80f9f91983ab5 100644 (file)
@@ -5087,7 +5087,7 @@ static void object_panel_cloth_II(Object *ob)
                }
                else
                {
-                       uiDefButBitI(block, TOG, CLOTH_SIMSETTINGS_FLAG_CCACHE_PROTECT, REDRAWVIEW3D, "Protect Cache",  10,120,300,20, &clmd->sim_parms->flags, 0, 0, 0, 0, "Protect cache from automatic freeing when scene changed");
+                       uiDefButBitI(block, TOG, CLOTH_SIMSETTINGS_FLAG_CCACHE_PROTECT, REDRAWVIEW3D, "Protect Cache & Enable Cache Editing",   10,120,300,20, &clmd->sim_parms->flags, 0, 0, 0, 0, "Protect cache from automatic freeing when scene changed. This also enabled the cache beeing edited in editmode.");
                        
                        if(!(clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_CCACHE_PROTECT))
                        {
index 8bd397b0da41d6e8741d26d365e219dd1a1eb6db..7b51d50ae7b39508838beb190bbf309e72425bff 100644 (file)
@@ -76,6 +76,7 @@
 #include "BKE_modifier.h"
 #include "BKE_multires.h"
 #include "BKE_object.h"
+#include "BKE_pointcache.h"
 #include "BKE_texture.h"
 #include "BKE_utildefines.h"
 
@@ -804,7 +805,10 @@ void make_editMesh()
        EditFace *efa;
        EditEdge *eed;
        EditSelection *ese;
-       int tot, a, eekadoodle= 0;
+       int tot, a, eekadoodle= 0, cloth_enabled = 0;
+       ClothModifierData *clmd = NULL;
+       Cloth *cloth = NULL;
+       float temp[3];
 
 #ifdef WITH_VERSE
        if(me->vnode){
@@ -839,10 +843,43 @@ void make_editMesh()
        /* make editverts */
        CustomData_copy(&me->vdata, &em->vdata, CD_MASK_EDITMESH, CD_CALLOC, 0);
        mvert= me->mvert;
+       
+       /* lots of checks to be sure if we have nice cloth object */
+       if(modifiers_isClothEnabled(G.obedit))
+       {
+               clmd = (ClothModifierData *) modifiers_findByType(G.obedit, eModifierType_Cloth);
+               cloth = clmd->clothObject;
+               
+               /* just to be sure also check vertcount */
+               /* also check if we have a protected cache */
+               if(cloth && (tot == cloth->numverts) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_CCACHE_PROTECT))
+               {
+                       /* check if we have cache for this frame */
+                       int stack_index = modifiers_indexInObject(G.obedit, (ModifierData *)clmd);
+               
+                       if(BKE_ptcache_id_exist((ID *)G.obedit, (float) G.scene->r.cfra, stack_index))
+                       {
+                               cloth_enabled = 1;
+                               
+                               /* inverse matrix is not uptodate... */
+                               Mat4Invert ( G.obedit->imat, G.obedit->obmat );
+                       }
+               }
+       }
 
        evlist= (EditVert **)MEM_mallocN(tot*sizeof(void *),"evlist");
        for(a=0; a<tot; a++, mvert++) {
-               eve= addvertlist(mvert->co, NULL);
+               
+               if(cloth_enabled)
+               {
+                       VECCOPY(temp, cloth->verts[a].x);
+                       Mat4MulVecfl ( G.obedit->imat, temp );
+                       eve= addvertlist(temp, NULL);
+                       
+                       /* TODO: what about normals? */
+               }
+               else    
+                       eve= addvertlist(mvert->co, NULL);
                evlist[a]= eve;
                
                // face select sets selection in next loop
@@ -971,7 +1008,10 @@ void load_editMesh(void)
        EditEdge *eed;
        EditSelection *ese;
        float *fp, *newkey, *oldkey, nor[3];
-       int i, a, ototvert, totedge=0;
+       int i, a, ototvert, totedge=0, cloth_enabled = 0;
+       ClothModifierData *clmd = NULL;
+       Cloth *cloth = NULL;
+       float temp[3], dt = 0.0;
 
 #ifdef WITH_VERSE
        if(em->vnode) {
@@ -1038,9 +1078,52 @@ void load_editMesh(void)
        /* the vertices, use ->tmp.l as counter */
        eve= em->verts.first;
        a= 0;
-
+       
+       /* lots of checks to be sure if we have nice cloth object */
+       if(modifiers_isClothEnabled(G.obedit))
+       {
+               clmd = (ClothModifierData *) modifiers_findByType(G.obedit, eModifierType_Cloth);
+               cloth = clmd->clothObject;
+               
+               /* just to be sure also check vertcount */
+               /* also check if we have a protected cache */
+               if(cloth && (G.totvert == cloth->numverts) && (clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_CCACHE_PROTECT))
+               {
+                       /* check if we have cache for this frame */
+                       int stack_index = modifiers_indexInObject(G.obedit, (ModifierData *)clmd);
+               
+                       if(BKE_ptcache_id_exist((ID *)G.obedit, (float) G.scene->r.cfra, stack_index))
+                       {
+                               cloth_enabled = 1;
+                               
+                               /* inverse matrix is not uptodate... */
+                               Mat4Invert ( G.obedit->imat, G.obedit->obmat );
+                               dt = 1.0f / clmd->sim_parms->stepsPerFrame;
+                       }
+               }
+       }
+       
+       i=0;
        while(eve) {
-               VECCOPY(mvert->co, eve->co);
+               
+               if(cloth_enabled)
+               {       
+                       VECCOPY(temp, cloth->verts[i].x);
+                       VECCOPY(cloth->verts[i].x, eve->co);
+                       Mat4MulVecfl ( G.obedit->obmat, cloth->verts[i].x );
+                       /*
+                       // not physical correct but gives nicer results when commented
+                       VECSUB(temp, cloth->verts[i].x, temp);
+                       VecMulf(temp, 1.0f / dt);
+                       VECADD(cloth->verts[i].v, cloth->verts[i].v, temp);
+                       */
+                       if(oldverts) {
+                               VECCOPY(mvert->co, oldverts[i].co);
+                       }
+                       i++;
+               }
+               else    
+                       VECCOPY(mvert->co, eve->co);
                mvert->mat_nr= 255;  /* what was this for, halos? */
                
                /* vertex normal */
@@ -1068,6 +1151,10 @@ void load_editMesh(void)
                eve= eve->next;
                mvert++;
        }
+       
+       /* burn changes to cache */
+       if(cloth_enabled)
+               cloth_write_cache(G.obedit, clmd, (float) G.scene->r.cfra);
 
        /* the edges */
        a= 0;
@@ -1330,7 +1417,13 @@ void load_editMesh(void)
                        if(base->object->data==me) {                            
                                if(modifiers_isClothEnabled(base->object)) {
                                        ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(base->object, eModifierType_Cloth);
-                                       clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
+                                       
+                                       /* only reset cloth when no cache was used */
+                                       if(!cloth_enabled)
+                                       {
+                                               clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
+                                       }
+                                       
                                }
                                
                                base->object->softflag |= OB_SB_REDO;
index 882a16bb9ceb95167d102e0579bf1ba5a8d795b3..5d2e7cd741054480e49e278f0011f3ea26e36d52 100644 (file)
@@ -3404,7 +3404,7 @@ void special_aftertrans_update(TransInfo *t)
                        ob= base->object;
                        
                        if (modifiers_isSoftbodyEnabled(ob)) ob->softflag |= OB_SB_REDO;
-                       else if(modifiers_isClothEnabled(ob)) {
+                       else if((ob == OBACT) && modifiers_isClothEnabled(ob)) {
                                ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(ob, eModifierType_Cloth);
                                clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
                        }
index dec15f96b8afcb1a2ebf0f47161e03e3c3476ccd..163b93468fec4690564cbcfe4f8bdc0400eb5384 100644 (file)
@@ -1353,6 +1353,7 @@ void weight_paint(void)
        if(modifiers_isClothEnabled(ob)) {
                ClothModifierData *clmd = (ClothModifierData *)modifiers_findByType(ob, eModifierType_Cloth);
                clmd->sim_parms->flags |= CLOTH_SIMSETTINGS_FLAG_RESET;
+               printf("vpaint.c\n");
        }       
        
        BIF_undo_push("Weight Paint");