[#14437] Modifier Stack Refactor
[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 "stddef.h"
34 #include "string.h"
35 #include "stdarg.h"
36 #include "math.h"
37 #include "float.h"
38
39 #include "BLI_kdtree.h"
40 #include "BLI_rand.h"
41 #include "BLI_uvproject.h"
42
43 #include "MEM_guardedalloc.h"
44
45 #include "DNA_armature_types.h"
46 #include "DNA_camera_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_key_types.h"
49 #include "DNA_material_types.h"
50 #include "DNA_object_fluidsim.h"
51
52
53 #include "BKE_action.h"
54 #include "BKE_bmesh.h"
55 #include "BKE_booleanops.h"
56 #include "BKE_cloth.h"
57 #include "BKE_cdderivedmesh.h"
58 #include "BKE_displist.h"
59 #include "BKE_fluidsim.h"
60 #include "BKE_global.h"
61 #include "BKE_multires.h"
62 #include "BKE_key.h"
63 #include "BKE_lattice.h"
64 #include "BKE_material.h"
65 #include "BKE_mesh.h"
66 #include "BKE_modifier.h"
67 #include "BKE_object.h"
68 #include "BKE_paint.h"
69 #include "BKE_particle.h"
70 #include "BKE_pointcache.h"
71 #include "BKE_scene.h"
72 #include "BKE_smoke.h"
73 #include "BKE_softbody.h"
74 #include "BKE_subsurf.h"
75 #include "BKE_texture.h"
76
77 #include "depsgraph_private.h"
78 #include "BKE_deform.h"
79 #include "BKE_shrinkwrap.h"
80
81 #include "LOD_decimation.h"
82
83 #include "CCGSubSurf.h"
84
85 #include "RE_shader_ext.h"
86
87 #include "MOD_modifiertypes.h"
88
89
90 static void initData(ModifierData *md) 
91 {
92         ClothModifierData *clmd = (ClothModifierData*) md;
93         
94         clmd->sim_parms = MEM_callocN(sizeof(ClothSimSettings), "cloth sim parms");
95         clmd->coll_parms = MEM_callocN(sizeof(ClothCollSettings), "cloth coll parms");
96         clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
97         
98         /* check for alloc failing */
99         if(!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache)
100                 return;
101         
102         cloth_init (clmd);
103 }
104
105 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
106                 DerivedMesh *derivedData, int useRenderParams, int isFinalCalc)
107 {
108         ClothModifierData *clmd = (ClothModifierData*) md;
109         DerivedMesh *result=NULL;
110         
111         /* check for alloc failing */
112         if(!clmd->sim_parms || !clmd->coll_parms)
113         {
114                 initData(md);
115                 
116                 if(!clmd->sim_parms || !clmd->coll_parms)
117                         return derivedData;
118         }
119
120         result = clothModifier_do(clmd, md->scene, ob, derivedData, useRenderParams, isFinalCalc);
121
122         if(result)
123         {
124                 CDDM_calc_normals(result);
125                 return result;
126         }
127         return derivedData;
128 }
129
130 static void updateDepgraph(
131                                          ModifierData *md, DagForest *forest, Scene *scene, Object *ob,
132           DagNode *obNode)
133 {
134         ClothModifierData *clmd = (ClothModifierData*) md;
135         
136         Base *base;
137         
138         if(clmd)
139         {
140                 for(base = scene->base.first; base; base= base->next) 
141                 {
142                         Object *ob1= base->object;
143                         if(ob1 != ob)
144                         {
145                                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
146                                 if(coll_clmd)
147                                 {
148                                         DagNode *curNode = dag_get_node(forest, ob1);
149                                         dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Cloth Collision");
150                                 }
151                         }
152                 }
153         }
154 }
155
156 static CustomDataMask requiredDataMask(Object *ob, ModifierData *md)
157 {
158         CustomDataMask dataMask = 0;
159         ClothModifierData *clmd = (ClothModifierData*)md;
160
161         if(cloth_uses_vgroup(clmd))
162                 dataMask |= (1 << CD_MDEFORMVERT);
163
164         if(clmd->sim_parms->shapekey_rest != 0)
165                 dataMask |= (1 << CD_CLOTH_ORCO);
166
167         return dataMask;
168 }
169
170 static void copyData(ModifierData *md, ModifierData *target)
171 {
172         ClothModifierData *clmd = (ClothModifierData*) md;
173         ClothModifierData *tclmd = (ClothModifierData*) target;
174         
175         if(tclmd->sim_parms)
176                 MEM_freeN(tclmd->sim_parms);
177         if(tclmd->coll_parms)
178                 MEM_freeN(tclmd->coll_parms);
179         
180         BKE_ptcache_free_list(&tclmd->ptcaches);
181         tclmd->point_cache = NULL;
182         
183         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
184         if(clmd->sim_parms->effector_weights)
185                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
186         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
187         tclmd->point_cache = BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches);
188         tclmd->clothObject = NULL;
189 }
190
191 static int dependsOnTime(ModifierData *md)
192 {
193         return 1;
194 }
195
196 static void freeData(ModifierData *md)
197 {
198         ClothModifierData *clmd = (ClothModifierData*) md;
199         
200         if (clmd) 
201         {
202                 if(G.rt > 0)
203                         printf("clothModifier_freeData\n");
204                 
205                 cloth_free_modifier_extern (clmd);
206                 
207                 if(clmd->sim_parms) {
208                         if(clmd->sim_parms->effector_weights)
209                                 MEM_freeN(clmd->sim_parms->effector_weights);
210                         MEM_freeN(clmd->sim_parms);
211                 }
212                 if(clmd->coll_parms)
213                         MEM_freeN(clmd->coll_parms);    
214                 
215                 BKE_ptcache_free_list(&clmd->ptcaches);
216                 clmd->point_cache = NULL;
217         }
218 }
219
220
221 ModifierTypeInfo modifierType_Cloth = {
222         /* name */              "Cloth",
223         /* structName */        "ClothModifierData",
224         /* structSize */        sizeof(ClothModifierData),
225         /* type */              eModifierTypeType_Nonconstructive,
226         /* flags */             eModifierTypeFlag_AcceptsMesh |
227                                                         eModifierTypeFlag_UsesPointCache |
228                                                         eModifierTypeFlag_Single,
229
230         /* copyData */          copyData,
231         /* deformVerts */       0,
232         /* deformVertsEM */     0,
233         /* deformMatricesEM */  0,
234         /* applyModifier */     applyModifier,
235         /* applyModifierEM */   0,
236         /* initData */          0,
237         /* requiredDataMask */  requiredDataMask,
238         /* freeData */          freeData,
239         /* isDisabled */        0,
240         /* updateDepgraph */    updateDepgraph,
241         /* dependsOnTime */     dependsOnTime,
242         /* foreachObjectLink */ 0,
243         /* foreachIDLink */     0,
244 };