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