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