Fix depsgraph to compute more accurate links for collision & force.
[blender.git] / source / blender / modifiers / intern / MOD_softbody.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_softbody.c
32  *  \ingroup modifiers
33  */
34
35 #include <stdio.h>
36
37 #include "DNA_scene_types.h"
38 #include "DNA_object_force.h"
39
40 #include "BLI_utildefines.h"
41
42 #include "BKE_cdderivedmesh.h"
43 #include "BKE_particle.h"
44 #include "BKE_softbody.h"
45
46 #include "depsgraph_private.h"
47 #include "DEG_depsgraph_build.h"
48
49 #include "MOD_modifiertypes.h"
50
51 static void deformVerts(ModifierData *md, Object *ob,
52                         DerivedMesh *UNUSED(derivedData),
53                         float (*vertexCos)[3],
54                         int numVerts,
55                         ModifierApplyFlag UNUSED(flag))
56 {
57         sbObjectStep(md->scene, ob, (float)md->scene->r.cfra, vertexCos, numVerts);
58 }
59
60 static bool dependsOnTime(ModifierData *UNUSED(md))
61 {
62         return true;
63 }
64
65 static void updateDepgraph(ModifierData *UNUSED(md), DagForest *forest,
66                            struct Main *UNUSED(bmain),
67                            Scene *scene, Object *ob, DagNode *obNode)
68 {
69         if (ob->soft) {
70                 /* Actual code uses ccd_build_deflector_hash */
71                 dag_add_collision_relations(forest, scene, ob, obNode, ob->soft->collision_group, ob->lay, eModifierType_Collision, NULL, false, "Softbody Collision");
72
73                 dag_add_forcefield_relations(forest, scene, ob, obNode, ob->soft->effector_weights, true, 0, "Softbody Field");
74         }
75 }
76
77 static void updateDepsgraph(ModifierData *UNUSED(md),
78                             struct Main *UNUSED(bmain),
79                             struct Scene *scene,
80                             Object *ob,
81                             struct DepsNodeHandle *node)
82 {
83         if (ob->soft) {
84                 /* Actual code uses ccd_build_deflector_hash */
85                 DEG_add_collision_relations(node, scene, ob, ob->soft->collision_group, ob->lay, eModifierType_Collision, NULL, false, "Softbody Collision");
86
87                 DEG_add_forcefield_relations(node, scene, ob, ob->soft->effector_weights, true, 0, "Softbody Field");
88         }
89 }
90
91 ModifierTypeInfo modifierType_Softbody = {
92         /* name */              "Softbody",
93         /* structName */        "SoftbodyModifierData",
94         /* structSize */        sizeof(SoftbodyModifierData),
95         /* type */              eModifierTypeType_OnlyDeform,
96         /* flags */             eModifierTypeFlag_AcceptsCVs |
97                                 eModifierTypeFlag_AcceptsLattice |
98                                 eModifierTypeFlag_RequiresOriginalData |
99                                 eModifierTypeFlag_Single,
100
101         /* copyData */          NULL,
102         /* deformVerts */       deformVerts,
103         /* deformMatrices */    NULL,
104         /* deformVertsEM */     NULL,
105         /* deformMatricesEM */  NULL,
106         /* applyModifier */     NULL,
107         /* applyModifierEM */   NULL,
108         /* initData */          NULL,
109         /* requiredDataMask */  NULL,
110         /* freeData */          NULL,
111         /* isDisabled */        NULL,
112         /* updateDepgraph */    updateDepgraph,
113         /* updateDepsgraph */   updateDepsgraph,
114         /* dependsOnTime */     dependsOnTime,
115         /* dependsOnNormals */  NULL,
116         /* foreachObjectLink */ NULL,
117         /* foreachIDLink */     NULL,
118         /* foreachTexLink */    NULL,
119 };