d2f36a86c25d60c35a16b7a6910064fccdfc32cd
[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                 MEM_freeN(tclmd->sim_parms);
129         if(tclmd->coll_parms)
130                 MEM_freeN(tclmd->coll_parms);
131         
132         BKE_ptcache_free_list(&tclmd->ptcaches);
133         tclmd->point_cache = NULL;
134         
135         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
136         if(clmd->sim_parms->effector_weights)
137                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
138         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
139         tclmd->point_cache = BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches);
140         tclmd->clothObject = NULL;
141 }
142
143 static int dependsOnTime(ModifierData *md)
144 {
145         return 1;
146 }
147
148 static void freeData(ModifierData *md)
149 {
150         ClothModifierData *clmd = (ClothModifierData*) md;
151         
152         if (clmd) 
153         {
154                 if(G.rt > 0)
155                         printf("clothModifier_freeData\n");
156                 
157                 cloth_free_modifier_extern (clmd);
158                 
159                 if(clmd->sim_parms) {
160                         if(clmd->sim_parms->effector_weights)
161                                 MEM_freeN(clmd->sim_parms->effector_weights);
162                         MEM_freeN(clmd->sim_parms);
163                 }
164                 if(clmd->coll_parms)
165                         MEM_freeN(clmd->coll_parms);    
166                 
167                 BKE_ptcache_free_list(&clmd->ptcaches);
168                 clmd->point_cache = NULL;
169         }
170 }
171
172
173 ModifierTypeInfo modifierType_Cloth = {
174         /* name */              "Cloth",
175         /* structName */        "ClothModifierData",
176         /* structSize */        sizeof(ClothModifierData),
177         /* type */              eModifierTypeType_Nonconstructive,
178         /* flags */             eModifierTypeFlag_AcceptsMesh |
179                                                         eModifierTypeFlag_UsesPointCache |
180                                                         eModifierTypeFlag_Single,
181
182         /* copyData */          copyData,
183         /* deformVerts */       0,
184         /* deformVertsEM */     0,
185         /* deformMatricesEM */  0,
186         /* applyModifier */     applyModifier,
187         /* applyModifierEM */   0,
188         /* initData */          initData,
189         /* requiredDataMask */  requiredDataMask,
190         /* freeData */          freeData,
191         /* isDisabled */        0,
192         /* updateDepgraph */    updateDepgraph,
193         /* dependsOnTime */     dependsOnTime,
194         /* foreachObjectLink */ 0,
195         /* foreachIDLink */     0,
196 };