add UNUSED() to modifiers, also removed some unused args.
[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 "DNA_cloth_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_object_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BKE_utildefines.h"
40 #include "BKE_cloth.h"
41 #include "BKE_cdderivedmesh.h"
42 #include "BKE_global.h"
43 #include "BKE_modifier.h"
44 #include "BKE_pointcache.h"
45
46 #include "depsgraph_private.h"
47
48
49 static void initData(ModifierData *md) 
50 {
51         ClothModifierData *clmd = (ClothModifierData*) md;
52         
53         clmd->sim_parms = MEM_callocN(sizeof(ClothSimSettings), "cloth sim parms");
54         clmd->coll_parms = MEM_callocN(sizeof(ClothCollSettings), "cloth coll parms");
55         clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
56         
57         /* check for alloc failing */
58         if(!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache)
59                 return;
60         
61         cloth_init (clmd);
62 }
63
64 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
65                 DerivedMesh *derivedData, int useRenderParams, int isFinalCalc)
66 {
67         ClothModifierData *clmd = (ClothModifierData*) md;
68         DerivedMesh *result=NULL;
69         
70         /* check for alloc failing */
71         if(!clmd->sim_parms || !clmd->coll_parms)
72         {
73                 initData(md);
74                 
75                 if(!clmd->sim_parms || !clmd->coll_parms)
76                         return derivedData;
77         }
78
79         result = clothModifier_do(clmd, md->scene, ob, derivedData, useRenderParams, isFinalCalc);
80
81         if(result)
82         {
83                 CDDM_calc_normals(result);
84                 return result;
85         }
86         return derivedData;
87 }
88
89 static void updateDepgraph(
90                                          ModifierData *md, DagForest *forest, Scene *scene, Object *ob,
91           DagNode *obNode)
92 {
93         ClothModifierData *clmd = (ClothModifierData*) md;
94         
95         Base *base;
96         
97         if(clmd)
98         {
99                 for(base = scene->base.first; base; base= base->next) 
100                 {
101                         Object *ob1= base->object;
102                         if(ob1 != ob)
103                         {
104                                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
105                                 if(coll_clmd)
106                                 {
107                                         DagNode *curNode = dag_get_node(forest, ob1);
108                                         dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Cloth Collision");
109                                 }
110                         }
111                 }
112         }
113 }
114
115 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
116 {
117         CustomDataMask dataMask = 0;
118         ClothModifierData *clmd = (ClothModifierData*)md;
119
120         if(cloth_uses_vgroup(clmd))
121                 dataMask |= (1 << CD_MDEFORMVERT);
122
123         if(clmd->sim_parms->shapekey_rest != 0)
124                 dataMask |= (1 << CD_CLOTH_ORCO);
125
126         return dataMask;
127 }
128
129 static void copyData(ModifierData *md, ModifierData *target)
130 {
131         ClothModifierData *clmd = (ClothModifierData*) md;
132         ClothModifierData *tclmd = (ClothModifierData*) target;
133
134         if(tclmd->sim_parms) {
135                 if(tclmd->sim_parms->effector_weights)
136                         MEM_freeN(tclmd->sim_parms->effector_weights);
137                 MEM_freeN(tclmd->sim_parms);
138         }
139
140         if(tclmd->coll_parms)
141                 MEM_freeN(tclmd->coll_parms);
142         
143         BKE_ptcache_free_list(&tclmd->ptcaches);
144         tclmd->point_cache = NULL;
145
146         tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
147         if(clmd->sim_parms->effector_weights)
148                 tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
149         tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
150         tclmd->point_cache = BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches);
151         tclmd->clothObject = NULL;
152 }
153
154 static int dependsOnTime(ModifierData *UNUSED(md))
155 {
156         return 1;
157 }
158
159 static void freeData(ModifierData *md)
160 {
161         ClothModifierData *clmd = (ClothModifierData*) md;
162         
163         if (clmd) 
164         {
165                 if(G.rt > 0)
166                         printf("clothModifier_freeData\n");
167                 
168                 cloth_free_modifier_extern (clmd);
169                 
170                 if(clmd->sim_parms) {
171                         if(clmd->sim_parms->effector_weights)
172                                 MEM_freeN(clmd->sim_parms->effector_weights);
173                         MEM_freeN(clmd->sim_parms);
174                 }
175                 if(clmd->coll_parms)
176                         MEM_freeN(clmd->coll_parms);    
177                 
178                 BKE_ptcache_free_list(&clmd->ptcaches);
179                 clmd->point_cache = NULL;
180         }
181 }
182
183
184 ModifierTypeInfo modifierType_Cloth = {
185         /* name */              "Cloth",
186         /* structName */        "ClothModifierData",
187         /* structSize */        sizeof(ClothModifierData),
188         /* type */              eModifierTypeType_Nonconstructive,
189         /* flags */             eModifierTypeFlag_AcceptsMesh
190                                                         | eModifierTypeFlag_UsesPointCache
191                                                         | eModifierTypeFlag_Single,
192
193         /* copyData */          copyData,
194         /* deformVerts */       0,
195         /* deformVertsEM */     0,
196         /* deformMatricesEM */  0,
197         /* applyModifier */     applyModifier,
198         /* applyModifierEM */   0,
199         /* initData */          initData,
200         /* requiredDataMask */  requiredDataMask,
201         /* freeData */          freeData,
202         /* isDisabled */        0,
203         /* updateDepgraph */    updateDepgraph,
204         /* dependsOnTime */     dependsOnTime,
205         /* dependsOnNormals */  0,
206         /* foreachObjectLink */ 0,
207         /* foreachIDLink */     0,
208 };