Blender V2.61 - r43446

MOD_cloth.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software  Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * Contributor(s): Daniel Dunbar
00022  *                 Ton Roosendaal,
00023  *                 Ben Batt,
00024  *                 Brecht Van Lommel,
00025  *                 Campbell Barton
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  *
00029  */
00030 
00036 #include "DNA_cloth_types.h"
00037 #include "DNA_scene_types.h"
00038 #include "DNA_object_types.h"
00039 
00040 #include "MEM_guardedalloc.h"
00041 
00042 #include "BLI_utildefines.h"
00043 
00044 
00045 #include "BKE_cloth.h"
00046 #include "BKE_cdderivedmesh.h"
00047 #include "BKE_global.h"
00048 #include "BKE_modifier.h"
00049 #include "BKE_pointcache.h"
00050 
00051 #include "depsgraph_private.h"
00052 
00053 #include "MOD_util.h"
00054 
00055 static void initData(ModifierData *md) 
00056 {
00057     ClothModifierData *clmd = (ClothModifierData*) md;
00058     
00059     clmd->sim_parms = MEM_callocN(sizeof(ClothSimSettings), "cloth sim parms");
00060     clmd->coll_parms = MEM_callocN(sizeof(ClothCollSettings), "cloth coll parms");
00061     clmd->point_cache = BKE_ptcache_add(&clmd->ptcaches);
00062     
00063     /* check for alloc failing */
00064     if(!clmd->sim_parms || !clmd->coll_parms || !clmd->point_cache)
00065         return;
00066     
00067     cloth_init (clmd);
00068 }
00069 
00070 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
00071                         DerivedMesh *dm,
00072                         int UNUSED(useRenderParams),
00073                         int UNUSED(isFinalCalc))
00074 {
00075     ClothModifierData *clmd = (ClothModifierData*) md;
00076     DerivedMesh *result=NULL;
00077     
00078     /* check for alloc failing */
00079     if(!clmd->sim_parms || !clmd->coll_parms)
00080     {
00081         initData(md);
00082         
00083         if(!clmd->sim_parms || !clmd->coll_parms)
00084             return dm;
00085     }
00086 
00087     result = clothModifier_do(clmd, md->scene, ob, dm);
00088 
00089     if(result)
00090     {
00091         CDDM_calc_normals(result);
00092         return result;
00093     }
00094     return dm;
00095 }
00096 
00097 static void updateDepgraph(
00098                      ModifierData *md, DagForest *forest, Scene *scene, Object *ob,
00099       DagNode *obNode)
00100 {
00101     ClothModifierData *clmd = (ClothModifierData*) md;
00102     
00103     Base *base;
00104     
00105     if(clmd)
00106     {
00107         for(base = scene->base.first; base; base= base->next) 
00108         {
00109             Object *ob1= base->object;
00110             if(ob1 != ob)
00111             {
00112                 CollisionModifierData *coll_clmd = (CollisionModifierData *)modifiers_findByType(ob1, eModifierType_Collision);
00113                 if(coll_clmd)
00114                 {
00115                     DagNode *curNode = dag_get_node(forest, ob1);
00116                     dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Cloth Collision");
00117                 }
00118             }
00119         }
00120     }
00121 }
00122 
00123 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
00124 {
00125     CustomDataMask dataMask = 0;
00126     ClothModifierData *clmd = (ClothModifierData*)md;
00127 
00128     if(cloth_uses_vgroup(clmd))
00129         dataMask |= CD_MASK_MDEFORMVERT;
00130 
00131     if(clmd->sim_parms->shapekey_rest != 0)
00132         dataMask |= CD_MASK_CLOTH_ORCO;
00133 
00134     return dataMask;
00135 }
00136 
00137 static void copyData(ModifierData *md, ModifierData *target)
00138 {
00139     ClothModifierData *clmd = (ClothModifierData*) md;
00140     ClothModifierData *tclmd = (ClothModifierData*) target;
00141 
00142     if(tclmd->sim_parms) {
00143         if(tclmd->sim_parms->effector_weights)
00144             MEM_freeN(tclmd->sim_parms->effector_weights);
00145         MEM_freeN(tclmd->sim_parms);
00146     }
00147 
00148     if(tclmd->coll_parms)
00149         MEM_freeN(tclmd->coll_parms);
00150     
00151     BKE_ptcache_free_list(&tclmd->ptcaches);
00152     tclmd->point_cache = NULL;
00153 
00154     tclmd->sim_parms = MEM_dupallocN(clmd->sim_parms);
00155     if(clmd->sim_parms->effector_weights)
00156         tclmd->sim_parms->effector_weights = MEM_dupallocN(clmd->sim_parms->effector_weights);
00157     tclmd->coll_parms = MEM_dupallocN(clmd->coll_parms);
00158     tclmd->point_cache = BKE_ptcache_copy_list(&tclmd->ptcaches, &clmd->ptcaches);
00159     tclmd->clothObject = NULL;
00160 }
00161 
00162 static int dependsOnTime(ModifierData *UNUSED(md))
00163 {
00164     return 1;
00165 }
00166 
00167 static void freeData(ModifierData *md)
00168 {
00169     ClothModifierData *clmd = (ClothModifierData*) md;
00170     
00171     if (clmd) 
00172     {
00173         if(G.rt > 0)
00174             printf("clothModifier_freeData\n");
00175         
00176         cloth_free_modifier_extern (clmd);
00177         
00178         if(clmd->sim_parms) {
00179             if(clmd->sim_parms->effector_weights)
00180                 MEM_freeN(clmd->sim_parms->effector_weights);
00181             MEM_freeN(clmd->sim_parms);
00182         }
00183         if(clmd->coll_parms)
00184             MEM_freeN(clmd->coll_parms);    
00185         
00186         BKE_ptcache_free_list(&clmd->ptcaches);
00187         clmd->point_cache = NULL;
00188     }
00189 }
00190 
00191 static void foreachIDLink(ModifierData *md, Object *ob,
00192                        IDWalkFunc walk, void *userData)
00193 {
00194     ClothModifierData *clmd = (ClothModifierData*) md;
00195 
00196     if(clmd->coll_parms) {
00197         walk(userData, ob, (ID **)&clmd->coll_parms->group);
00198     }
00199 
00200     if(clmd->sim_parms && clmd->sim_parms->effector_weights) {
00201         walk(userData, ob, (ID **)&clmd->sim_parms->effector_weights->group);
00202     }
00203 }
00204 
00205 ModifierTypeInfo modifierType_Cloth = {
00206     /* name */              "Cloth",
00207     /* structName */        "ClothModifierData",
00208     /* structSize */        sizeof(ClothModifierData),
00209     /* type */              eModifierTypeType_Nonconstructive,
00210     /* flags */             eModifierTypeFlag_AcceptsMesh
00211                             | eModifierTypeFlag_UsesPointCache
00212                             | eModifierTypeFlag_Single,
00213 
00214     /* copyData */          copyData,
00215     /* deformVerts */       NULL,
00216     /* deformMatrices */    NULL,
00217     /* deformVertsEM */     NULL,
00218     /* deformMatricesEM */  NULL,
00219     /* applyModifier */     applyModifier,
00220     /* applyModifierEM */   NULL,
00221     /* initData */          initData,
00222     /* requiredDataMask */  requiredDataMask,
00223     /* freeData */          freeData,
00224     /* isDisabled */        NULL,
00225     /* updateDepgraph */    updateDepgraph,
00226     /* dependsOnTime */     dependsOnTime,
00227     /* dependsOnNormals */  NULL,
00228     /* foreachObjectLink */ NULL,
00229     /* foreachIDLink */     foreachIDLink,
00230     /* foreachTexLink */    NULL,
00231 };