style cleanup, brackets in else/if, some indentation.
[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_meshdata_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_scene_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_utildefines.h"
38
39 #include "BKE_cdderivedmesh.h"
40 #include "BKE_dynamicpaint.h"
41 #include "BKE_modifier.h"
42
43 #include "depsgraph_private.h"
44
45 #include "MOD_util.h"
46
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(ModifierData *md, ModifierData *target)
58 {
59         DynamicPaintModifierData *pmd  = (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                                 dataMask |= (1 << CD_MTFACE);
83                         }
84                         /* mcol */
85                         if (surface->type == MOD_DPAINT_SURFACE_T_PAINT ||
86                                 surface->init_color_type == MOD_DPAINT_INITIAL_VERTEXCOLOR) {
87                                 dataMask |= (1 << CD_MCOL);
88                         }
89                         /* CD_MDEFORMVERT */
90                         if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) {
91                                 dataMask |= (1 << CD_MDEFORMVERT);
92                         }
93                 }
94         }
95
96         if (pmd->brush) {
97                 if (pmd->brush->flags & MOD_DPAINT_USE_MATERIAL) {
98                         dataMask |= (1 << CD_MTFACE);
99                 }
100         }
101         return dataMask;
102 }
103
104 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, 
105                                                 DerivedMesh *dm,
106                                                 int UNUSED(useRenderParams),
107                                                 int UNUSED(isFinalCalc))
108 {
109         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
110
111         return dynamicPaint_Modifier_do(pmd, md->scene, ob, dm);
112 }
113
114 static void updateDepgraph(ModifierData *md, DagForest *forest,
115                                                 struct Scene *scene,
116                                                 Object *ob,
117                                                 DagNode *obNode)
118 {
119         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
120
121         /* add relation from canvases to all brush objects */
122         if (pmd && pmd->canvas) {
123                 Base *base = scene->base.first;
124
125                 for (; base; base = base->next) {
126                         DynamicPaintModifierData *pmd2 = (DynamicPaintModifierData *)modifiers_findByType(base->object, eModifierType_DynamicPaint);
127
128                         if (pmd2 && pmd2->brush && ob!=base->object) {
129                                 DagNode *brushNode = dag_get_node(forest, base->object);
130                                 dag_add_relation(forest, brushNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Dynamic Paint Brush");
131                         }
132                 }
133         }
134 }
135
136 static int dependsOnTime(ModifierData *UNUSED(md))
137 {
138         return 1;
139 }
140
141 static void foreachIDLink(ModifierData *md, Object *ob,
142                                            IDWalkFunc walk, void *userData)
143 {
144         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
145
146         if(pmd->canvas) {
147                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
148
149                 for(; surface; surface=surface->next) {
150                         walk(userData, ob, (ID **)&surface->brush_group);
151                         walk(userData, ob, (ID **)&surface->init_texture);
152                 }
153         }
154         if (pmd->brush) {
155                 walk(userData, ob, (ID **)&pmd->brush->mat);
156         }
157 }
158
159 static void foreachTexLink(ModifierData *UNUSED(md), Object *UNUSED(ob),
160                                            TexWalkFunc UNUSED(walk), void *UNUSED(userData))
161 {
162         //walk(userData, ob, md, ""); /* re-enable when possible */
163 }
164
165 ModifierTypeInfo modifierType_DynamicPaint = {
166         /* name */              "Dynamic Paint",
167         /* structName */        "DynamicPaintModifierData",
168         /* structSize */        sizeof(DynamicPaintModifierData),
169         /* type */              eModifierTypeType_Constructive,
170         /* flags */             eModifierTypeFlag_AcceptsMesh
171 /*                             |eModifierTypeFlag_SupportsMapping*/
172                                |eModifierTypeFlag_UsesPointCache
173                                |eModifierTypeFlag_Single
174                                |eModifierTypeFlag_UsesPreview,
175
176         /* copyData */          copyData,
177         /* deformVerts */       NULL,
178         /* deformMatrices */    NULL,
179         /* deformVertsEM */     NULL,
180         /* deformMatricesEM */  NULL,
181         /* applyModifier */     applyModifier,
182         /* applyModifierEM */   NULL,
183         /* initData */          initData,
184         /* requiredDataMask */  requiredDataMask,
185         /* freeData */          freeData,
186         /* isDisabled */        NULL,
187         /* updateDepgraph */    updateDepgraph,
188         /* dependsOnTime */     dependsOnTime,
189         /* dependsOnNormals */  NULL,
190         /* foreachObjectLink */ NULL,
191         /* foreachIDLink */     foreachIDLink,
192         /* foreachTexLink */    foreachTexLink,
193 };