Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / blender / blenkernel / intern / cloth.c
index d25c329..3acaaec 100644 (file)
@@ -347,7 +347,7 @@ void cloth_clear_cache(Object *ob, ClothModifierData *clmd, float framenr)
        BKE_ptcache_id_from_cloth(&pid, ob, clmd);
 
        // don't do anything as long as we're in editmode!
-       if(pid.cache->edit && ob->mode & OB_MODE_PARTICLE_EDIT)
+       if(pid.cache->flag & PTCACHE_BAKE_EDIT_ACTIVE)
                return;
        
        BKE_ptcache_id_clear(&pid, PTCACHE_CLEAR_AFTER, framenr);
@@ -496,32 +496,23 @@ DerivedMesh *clothModifier_do(ClothModifierData *clmd, Scene *scene, Object *ob,
        if(!do_init_cloth(ob, clmd, result, framenr))
                return result;
 
-       if(framenr == startframe) {
-               BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
-               do_init_cloth(ob, clmd, result, framenr);
-               cache->simframe= framenr;
-               cache->flag |= PTCACHE_SIMULATION_VALID;
-               cache->flag &= ~PTCACHE_REDO_NEEDED;
-               return result;
-       }
-
        /* try to read from cache */
        cache_result = BKE_ptcache_read_cache(&pid, (float)framenr, scene->r.frs_sec);
 
        if(cache_result == PTCACHE_READ_EXACT || cache_result == PTCACHE_READ_INTERPOLATED) {
-               implicit_set_positions(clmd);
-               cloth_to_object (ob, clmd, result);
-
-               cache->simframe= framenr;
                cache->flag |= PTCACHE_SIMULATION_VALID;
+               cache->simframe= framenr;
 
-               if(cache_result == PTCACHE_READ_INTERPOLATED && cache->flag & PTCACHE_REDO_NEEDED)
-                       BKE_ptcache_write_cache(&pid, framenr);
+               implicit_set_positions(clmd);
+               cloth_to_object (ob, clmd, result);
 
                return result;
        }
        else if(cache_result==PTCACHE_READ_OLD) {
+               BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_FREE);
+
                implicit_set_positions(clmd);
+
                cache->flag |= PTCACHE_SIMULATION_VALID;
        }
        else if(ob->id.lib || (cache->flag & PTCACHE_BAKED)) {
@@ -532,25 +523,38 @@ DerivedMesh *clothModifier_do(ClothModifierData *clmd, Scene *scene, Object *ob,
                return result;
        }
 
-       /* if on second frame, write cache for first frame */
-       if(cache->simframe == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact==0))
-               BKE_ptcache_write_cache(&pid, startframe);
+       if(framenr == startframe) {
+               if(cache->flag & PTCACHE_REDO_NEEDED) {
+                       BKE_ptcache_id_reset(scene, &pid, PTCACHE_RESET_OUTDATED);
+                       do_init_cloth(ob, clmd, result, framenr);
+               }
+               cache->flag |= PTCACHE_SIMULATION_VALID;
+               cache->simframe= framenr;
+
+               /* don't write cache on first frame, but on second frame write
+                * cache for frame 1 and 2 */
+       }
+       else {
+               /* if on second frame, write cache for first frame */
+               if(cache->simframe == startframe && (cache->flag & PTCACHE_OUTDATED || cache->last_exact==0))
+                       BKE_ptcache_write_cache(&pid, startframe);
+
+               clmd->sim_parms->timescale *= framenr - cache->simframe;
 
-       clmd->sim_parms->timescale *= framenr - cache->simframe;
+               /* do simulation */
+               cache->flag |= PTCACHE_SIMULATION_VALID;
+               cache->simframe= framenr;
 
-       /* do simulation */
-       cache->flag |= PTCACHE_SIMULATION_VALID;
-       cache->simframe= framenr;
+               if(!do_step_cloth(ob, clmd, result, framenr)) {
+                       cache->flag &= ~PTCACHE_SIMULATION_VALID;
+                       cache->simframe= 0;
+                       cache->last_exact= 0;
+               }
+               else
+                       BKE_ptcache_write_cache(&pid, framenr);
 
-       if(!do_step_cloth(ob, clmd, result, framenr)) {
-               cache->flag &= ~PTCACHE_SIMULATION_VALID;
-               cache->simframe= 0;
-               cache->last_exact= 0;
+               cloth_to_object (ob, clmd, result);
        }
-       else
-               BKE_ptcache_write_cache(&pid, framenr);
-
-       cloth_to_object (ob, clmd, result);
 
        return result;
 }