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