Cleanup: code-style, duplicate header
[blender.git] / source / blender / modifiers / intern / MOD_dynamicpaint.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  * Contributor(s): Miika Hämäläinen
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  *
22  */
23
24 /** \file blender/modifiers/intern/MOD_dynamicpaint.c
25  *  \ingroup modifiers
26  */
27
28 #include <stddef.h>
29
30 #include "DNA_dynamicpaint_types.h"
31 #include "DNA_object_types.h"
32 #include "DNA_object_force.h"
33 #include "DNA_scene_types.h"
34
35 #include "BLI_utildefines.h"
36
37 #include "BKE_cdderivedmesh.h"
38 #include "BKE_dynamicpaint.h"
39 #include "BKE_library_query.h"
40 #include "BKE_modifier.h"
41
42 #include "depsgraph_private.h"
43 #include "DEG_depsgraph_build.h"
44
45
46 static void initData(ModifierData *md) 
47 {
48         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
49         
50         pmd->canvas = NULL;
51         pmd->brush = NULL;
52         pmd->type = MOD_DYNAMICPAINT_TYPE_CANVAS;
53 }
54
55 static void copyData(ModifierData *md, ModifierData *target)
56 {
57         DynamicPaintModifierData *pmd  = (DynamicPaintModifierData *)md;
58         DynamicPaintModifierData *tpmd = (DynamicPaintModifierData *)target;
59         
60         dynamicPaint_Modifier_copy(pmd, tpmd);
61 }
62
63 static void freeData(ModifierData *md)
64 {
65         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
66         dynamicPaint_Modifier_free(pmd);
67 }
68
69 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
70 {
71         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
72         CustomDataMask dataMask = 0;
73
74         if (pmd->canvas) {
75                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
76                 for (; surface; surface = surface->next) {
77                         /* tface */
78                         if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ || 
79                             surface->init_color_type == MOD_DPAINT_INITIAL_TEXTURE)
80                         {
81                                 dataMask |= CD_MASK_MLOOPUV | CD_MASK_MTEXPOLY;
82                         }
83                         /* mcol */
84                         if (surface->type == MOD_DPAINT_SURFACE_T_PAINT ||
85                             surface->init_color_type == MOD_DPAINT_INITIAL_VERTEXCOLOR)
86                         {
87                                 dataMask |= CD_MASK_MLOOPCOL;
88                         }
89                         /* CD_MDEFORMVERT */
90                         if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) {
91                                 dataMask |= CD_MASK_MDEFORMVERT;
92                         }
93                 }
94         }
95
96         if (pmd->brush) {
97                 if (pmd->brush->flags & MOD_DPAINT_USE_MATERIAL) {
98                         dataMask |= CD_MASK_MLOOPUV | CD_MASK_MTEXPOLY;
99                 }
100         }
101         return dataMask;
102 }
103
104 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, 
105                                   DerivedMesh *dm,
106                                   ModifierApplyFlag flag)
107 {
108         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
109
110         /* dont apply dynamic paint on orco dm stack */
111         if (!(flag & MOD_APPLY_ORCO)) {
112                 return dynamicPaint_Modifier_do(pmd, md->scene, ob, dm);
113         }
114         return dm;
115 }
116
117 static bool is_brush_cb(Object *UNUSED(ob), ModifierData *pmd)
118 {
119         return ((DynamicPaintModifierData *)pmd)->brush != NULL;
120 }
121
122 static void updateDepgraph(ModifierData *md, DagForest *forest,
123                            struct Main *UNUSED(bmain),
124                            struct Scene *scene,
125                            Object *ob,
126                            DagNode *obNode)
127 {
128         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
129
130         /* add relation from canvases to all brush objects */
131         if (pmd && pmd->canvas) {
132 #ifdef WITH_LEGACY_DEPSGRAPH
133                 for (DynamicPaintSurface *surface = pmd->canvas->surfaces.first; surface; surface = surface->next) {
134                         if (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) {
135                                 dag_add_forcefield_relations(forest, scene, ob, obNode, surface->effector_weights, true, 0, "Dynamic Paint Field");
136                         }
137
138                         /* Actual code uses custom loop over group/scene without layer checks in dynamicPaint_doStep */
139                         dag_add_collision_relations(forest, scene, ob, obNode, surface->brush_group, -1, eModifierType_DynamicPaint, is_brush_cb, false, "Dynamic Paint Brush");
140                 }
141 #else
142         (void)forest;
143         (void)scene;
144         (void)ob;
145         (void)obNode;
146 #endif
147         }
148 }
149
150 static void updateDepsgraph(ModifierData *md,
151                             struct Main *UNUSED(bmain),
152                             struct Scene *scene,
153                             Object *ob,
154                             struct DepsNodeHandle *node)
155 {
156         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *)md;
157         /* Add relation from canvases to all brush objects. */
158         if (pmd->canvas != NULL) {
159                 for (DynamicPaintSurface *surface = pmd->canvas->surfaces.first; surface; surface = surface->next) {
160                         if (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) {
161                                 DEG_add_forcefield_relations(node, scene, ob, surface->effector_weights, true, 0, "Dynamic Paint Field");
162                         }
163
164                         /* Actual code uses custom loop over group/scene without layer checks in dynamicPaint_doStep */
165                         DEG_add_collision_relations(node, scene, ob, surface->brush_group, -1, eModifierType_DynamicPaint, is_brush_cb, false, "Dynamic Paint Brush");
166                 }
167         }
168 }
169
170 static bool dependsOnTime(ModifierData *UNUSED(md))
171 {
172         return true;
173 }
174
175 static void foreachIDLink(ModifierData *md, Object *ob,
176                           IDWalkFunc walk, void *userData)
177 {
178         DynamicPaintModifierData *pmd = (DynamicPaintModifierData *) md;
179
180         if (pmd->canvas) {
181                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
182
183                 for (; surface; surface = surface->next) {
184                         walk(userData, ob, (ID **)&surface->brush_group, IDWALK_CB_NOP);
185                         walk(userData, ob, (ID **)&surface->init_texture, IDWALK_CB_USER);
186                         if (surface->effector_weights) {
187                                 walk(userData, ob, (ID **)&surface->effector_weights->group, IDWALK_CB_NOP);
188                         }
189                 }
190         }
191         if (pmd->brush) {
192                 walk(userData, ob, (ID **)&pmd->brush->mat, IDWALK_CB_USER);
193         }
194 }
195
196 static void foreachTexLink(ModifierData *UNUSED(md), Object *UNUSED(ob),
197                            TexWalkFunc UNUSED(walk), void *UNUSED(userData))
198 {
199         //walk(userData, ob, md, ""); /* re-enable when possible */
200 }
201
202 ModifierTypeInfo modifierType_DynamicPaint = {
203         /* name */              "Dynamic Paint",
204         /* structName */        "DynamicPaintModifierData",
205         /* structSize */        sizeof(DynamicPaintModifierData),
206         /* type */              eModifierTypeType_Constructive,
207         /* flags */             eModifierTypeFlag_AcceptsMesh |
208 /*                              eModifierTypeFlag_SupportsMapping |*/
209                                 eModifierTypeFlag_UsesPointCache |
210                                 eModifierTypeFlag_Single |
211                                 eModifierTypeFlag_UsesPreview,
212
213         /* copyData */          copyData,
214         /* deformVerts */       NULL,
215         /* deformMatrices */    NULL,
216         /* deformVertsEM */     NULL,
217         /* deformMatricesEM */  NULL,
218         /* applyModifier */     applyModifier,
219         /* applyModifierEM */   NULL,
220         /* initData */          initData,
221         /* requiredDataMask */  requiredDataMask,
222         /* freeData */          freeData,
223         /* isDisabled */        NULL,
224         /* updateDepgraph */    updateDepgraph,
225         /* updateDepsgraph */   updateDepsgraph,
226         /* dependsOnTime */     dependsOnTime,
227         /* dependsOnNormals */  NULL,
228         /* foreachObjectLink */ NULL,
229         /* foreachIDLink */     foreachIDLink,
230         /* foreachTexLink */    foreachTexLink,
231 };