Merge with trunk, revision 28528 - 28976.
[blender.git] / source / blender / modifiers / intern / MOD_cloth.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 #include "BKE_cloth.h"
34 #include "BKE_cdderivedmesh.h"
35 #include "BKE_global.h"
36 #include "BKE_modifier.h"
37 #include "BKE_pointcache.h"
38
39 #include "depsgraph_private.h"
40
41
42 static void initData(ModifierData *md) 
43 {
44         ClothModifierData *clmd = (ClothModifierData*) md;
45         
46         clmd->sim_parms = MEM_callocN(sizeof(ClothSimSettings), "cloth sim parms");
47         clmd->coll_parms = MEM_callocN(sizeof(ClothCollSettings), "cloth coll parms");
48         clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
49         
50         /* check for alloc failing */
51         if(!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache)
52                 return;
53         
54         cloth_init (clmd);
55 }
56
57 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
58                 DerivedMesh *derivedData, int useRenderParams, int isFinalCalc)
59 {
60         ClothModifierData *clmd = (ClothModifierData*) md;
61         DerivedMesh *result=NULL;
62         
63         /* check for alloc failing */
64         if(!clmd->sim_parms || !clmd->coll_parms)
65         {
66                 initData(md);
67                 
68                 if(!clmd->sim_parms || !clmd->coll_parms)
69                         return derivedData;
70         }
71
72         result = clothModifier_do(clmd, md->scene, ob, derivedData, useRenderParams, isFinalCalc);
73
74         if(result)
75         {
76                 CDDM_calc_normals(result);
77                 return result;
78         }
79         return derivedData;
80 }
81
82 static void updateDepgraph(
83                                          ModifierData *md, DagForest *forest, Scene *scene, Object *ob,
84           DagNode *obNode)
85 {
86         ClothModifierData *clmd = (ClothModifierData*) md;
87         
88         Base *base;
89         
90         if(clmd)
91         {
92                 for(base = scene->base.first; base; base= base->next) 
93                 {
94                         Object *ob1= base->object;
95                         if(ob1 != ob)
96                         {
97                                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
98                                 if(coll_clmd)
99                                 {
100                                         DagNode *curNode = dag_get_node(forest, ob1);
101                                         dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Cloth Collision");
102                                 }
103                         }
104                 }
105         }
106 }
107
108 static CustomDataMask requiredDataMask(Object *ob, ModifierData *md)
109 {
110         CustomDataMask dataMask = 0;
111         ClothModifierData *clmd = (ClothModifierData*)md;
112
113         if(cloth_uses_vgroup(clmd))
114                 dataMask |= (1 << CD_MDEFORMVERT);
115
116         if(clmd->sim_parms->shapekey_rest != 0)
117                 dataMask |= (1 << CD_CLOTH_ORCO);
118
119         return dataMask;
120 }
121
122 static void copyData(ModifierData *md, ModifierData *target)
123 {
124         ClothModifierData *clmd = (ClothModifierData*) md;
125         ClothModifierData *tclmd = (ClothModifierData*) target;
126
127         if(tclmd->sim_parms) {
128                 if(tclmd->sim_parms->effector_weights)
129                         MEM_freeN(tclmd->sim_parms->effector_weights);
130                 MEM_freeN(tclmd->sim_parms);
131         }
132
133         if(tclmd->coll_parms)
134                 MEM_freeN(tclmd->coll_parms);
135         
136         BKE_ptcache_free_list(&tclmd->ptcaches);
137         tclmd->point_cache = NULL;
138
139         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
140         if(clmd->sim_parms->effector_weights)
141                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
142         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
143         tclmd->point_cache = BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches);
144         tclmd->clothObject = NULL;
145 }
146
147 static int dependsOnTime(ModifierData *md)
148 {
149         return 1;
150 }
151
152 static void freeData(ModifierData *md)
153 {
154         ClothModifierData *clmd = (ClothModifierData*) md;
155         
156         if (clmd) 
157         {
158                 if(G.rt > 0)
159                         printf("clothModifier_freeData\n");
160                 
161                 cloth_free_modifier_extern (clmd);
162                 
163                 if(clmd->sim_parms) {
164                         if(clmd->sim_parms->effector_weights)
165                                 MEM_freeN(clmd->sim_parms->effector_weights);
166                         MEM_freeN(clmd->sim_parms);
167                 }
168                 if(clmd->coll_parms)
169                         MEM_freeN(clmd->coll_parms);    
170                 
171                 BKE_ptcache_free_list(&clmd->ptcaches);
172                 clmd->point_cache = NULL;
173         }
174 }
175
176
177 ModifierTypeInfo modifierType_Cloth = {
178         /* name */              "Cloth",
179         /* structName */        "ClothModifierData",
180         /* structSize */        sizeof(ClothModifierData),
181         /* type */              eModifierTypeType_Nonconstructive,
182         /* flags */             eModifierTypeFlag_AcceptsMesh
183                                                         | eModifierTypeFlag_UsesPointCache
184                                                         | eModifierTypeFlag_Single,
185
186         /* copyData */          copyData,
187         /* deformVerts */       0,
188         /* deformVertsEM */     0,
189         /* deformMatricesEM */  0,
190         /* applyModifier */     applyModifier,
191         /* applyModifierEM */   0,
192         /* initData */          initData,
193         /* requiredDataMask */  requiredDataMask,
194         /* freeData */          freeData,
195         /* isDisabled */        0,
196         /* updateDepgraph */    updateDepgraph,
197         /* dependsOnTime */     dependsOnTime,
198         /* foreachObjectLink */ 0,
199         /* foreachIDLink */     0,
200 };