Merge with trunk r39062
[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 * Contributor(s): Miika Hämäläinen
10 *
11 * ***** END GPL LICENSE BLOCK *****
12 *
13 */
14
15 #include "stddef.h"
16
17 #include "DNA_meshdata_types.h"
18 #include "DNA_object_types.h"
19 #include "DNA_scene_types.h"
20
21 #include "MEM_guardedalloc.h"
22
23 #include "BKE_cdderivedmesh.h"
24 #include "BKE_modifier.h"
25 #include "BKE_dynamicpaint.h"
26
27 #include "depsgraph_private.h"
28
29 #include "MOD_util.h"
30
31
32 static void initData(ModifierData *md) 
33 {
34         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
35         
36         pmd->canvas = NULL;
37         pmd->brush = NULL;
38         pmd->type = MOD_DYNAMICPAINT_TYPE_CANVAS;
39 }
40
41 static void copyData(ModifierData *md, ModifierData *target)
42 {
43         DynamicPaintModifierData *pmd  = (DynamicPaintModifierData*)md;
44         DynamicPaintModifierData *tpmd = (DynamicPaintModifierData*)target;
45         
46         dynamicPaint_Modifier_copy(pmd, tpmd);
47 }
48
49 static void freeData(ModifierData *md)
50 {
51         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
52         dynamicPaint_Modifier_free (pmd);
53 }
54
55 static CustomDataMask requiredDataMask(Object *ob, ModifierData *md)
56 {
57         CustomDataMask dataMask = 0;
58
59         dataMask |= (1 << CD_MTFACE);
60         dataMask |= (1 << CD_MCOL);
61         dataMask |= (1 << CD_MDEFORMVERT);
62
63         return dataMask;
64 }
65
66 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, 
67                                                 DerivedMesh *dm,
68                                                 int useRenderParams,
69                                                 int isFinalCalc)
70 {
71         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
72
73         return dynamicPaint_Modifier_do(pmd, md->scene, ob, dm);
74 }
75
76 static void updateDepgraph(ModifierData *md, DagForest *forest,
77                                                 struct Scene *scene,
78                                                 Object *ob,
79                                                 DagNode *obNode)
80 {
81         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
82
83         /* add relation from canvases to all brush objects */
84         if(pmd && pmd->canvas)
85         {
86                 Base *base = scene->base.first;
87
88                 for(; base; base = base->next) {
89                         DynamicPaintModifierData *pmd2 = (DynamicPaintModifierData *)modifiers_findByType(base->object, eModifierType_DynamicPaint);
90
91                         if(pmd2 && pmd2->brush && ob!=base->object)
92                         {
93                                 DagNode *brushNode = dag_get_node(forest, base->object);
94                                 dag_add_relation(forest, brushNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Dynamic Paint Brush");
95                         }
96                 }
97         }
98 }
99
100 static int dependsOnTime(ModifierData *md)
101 {
102         return 1;
103 }
104
105 static void foreachIDLink(ModifierData *md, Object *ob,
106                                            IDWalkFunc walk, void *userData)
107 {
108         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
109
110         if(pmd->canvas) {
111                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
112
113                 for(; surface; surface=surface->next) {
114                         walk(userData, ob, (ID **)&surface->brush_group);
115                 }
116         }
117         if (pmd->brush) {
118                 walk(userData, ob, (ID **)&pmd->brush->mat);
119         }
120 }
121
122 ModifierTypeInfo modifierType_DynamicPaint = {
123         /* name */              "Dynamic Paint",
124         /* structName */        "DynamicPaintModifierData",
125         /* structSize */        sizeof(DynamicPaintModifierData),
126         /* type */              eModifierTypeType_Constructive,
127         /* flags */             eModifierTypeFlag_AcceptsMesh
128                                                         | eModifierTypeFlag_UsesPointCache
129                                                         | eModifierTypeFlag_Single,
130
131         /* copyData */          copyData,
132         /* deformVerts */       0,
133         /* deformMatrices */    0,
134         /* deformVertsEM */     0,
135         /* deformMatricesEM */  0,
136         /* applyModifier */     applyModifier,
137         /* applyModifierEM */   0,
138         /* initData */          initData,
139         /* requiredDataMask */  requiredDataMask,
140         /* freeData */          freeData,
141         /* isDisabled */        0,
142         /* updateDepgraph */    updateDepgraph,
143         /* dependsOnTime */     dependsOnTime,
144         /* dependsOnNormals */  0,
145         /* foreachObjectLink */ 0,
146         /* foreachIDLink */     foreachIDLink,
147 };