Merge with trunk r41197
[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_dynamicpaint.h"
25 #include "BKE_modifier.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         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*)md;
58         CustomDataMask dataMask = 0;
59
60         if (pmd->canvas) {
61                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
62                 for(; surface; surface=surface->next) {
63                         /* tface */
64                         if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ || 
65                                 surface->init_color_type == MOD_DPAINT_INITIAL_TEXTURE) {
66                                 dataMask |= (1 << CD_MTFACE);
67                         }
68                         /* mcol */
69                         if (surface->type == MOD_DPAINT_SURFACE_T_PAINT ||
70                                 surface->init_color_type == MOD_DPAINT_INITIAL_VERTEXCOLOR) {
71                                 dataMask |= (1 << CD_MCOL);
72                         }
73                         /* CD_MDEFORMVERT */
74                         if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) {
75                                 dataMask |= (1 << CD_MDEFORMVERT);
76                         }
77                 }
78         }
79
80         if (pmd->brush) {
81                 if (pmd->brush->mat) {
82                         dataMask |= (1 << CD_MTFACE);
83                 }
84         }
85         return dataMask;
86 }
87
88 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, 
89                                                 DerivedMesh *dm,
90                                                 int useRenderParams,
91                                                 int isFinalCalc)
92 {
93         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
94
95         return dynamicPaint_Modifier_do(pmd, md->scene, ob, dm);
96 }
97
98 static void updateDepgraph(ModifierData *md, DagForest *forest,
99                                                 struct Scene *scene,
100                                                 Object *ob,
101                                                 DagNode *obNode)
102 {
103         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
104
105         /* add relation from canvases to all brush objects */
106         if(pmd && pmd->canvas)
107         {
108                 Base *base = scene->base.first;
109
110                 for(; base; base = base->next) {
111                         DynamicPaintModifierData *pmd2 = (DynamicPaintModifierData *)modifiers_findByType(base->object, eModifierType_DynamicPaint);
112
113                         if(pmd2 && pmd2->brush && ob!=base->object)
114                         {
115                                 DagNode *brushNode = dag_get_node(forest, base->object);
116                                 dag_add_relation(forest, brushNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Dynamic Paint Brush");
117                         }
118                 }
119         }
120 }
121
122 static int dependsOnTime(ModifierData *md)
123 {
124         return 1;
125 }
126
127 static void foreachIDLink(ModifierData *md, Object *ob,
128                                            IDWalkFunc walk, void *userData)
129 {
130         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
131
132         if(pmd->canvas) {
133                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
134
135                 for(; surface; surface=surface->next) {
136                         walk(userData, ob, (ID **)&surface->brush_group);
137                         walk(userData, ob, (ID **)&surface->init_texture);
138                 }
139         }
140         if (pmd->brush) {
141                 walk(userData, ob, (ID **)&pmd->brush->mat);
142         }
143 }
144
145 static void foreachTexLink(ModifierData *md, Object *ob,
146                                            TexWalkFunc walk, void *userData)
147 {
148         walk(userData, ob, md, ""); /* property name isn't used */
149 }
150
151 ModifierTypeInfo modifierType_DynamicPaint = {
152         /* name */              "Dynamic Paint",
153         /* structName */        "DynamicPaintModifierData",
154         /* structSize */        sizeof(DynamicPaintModifierData),
155         /* type */              eModifierTypeType_Constructive,
156         /* flags */             eModifierTypeFlag_AcceptsMesh
157                                                         | eModifierTypeFlag_UsesPointCache
158                                                         | eModifierTypeFlag_Single,
159
160         /* copyData */          copyData,
161         /* deformVerts */       0,
162         /* deformMatrices */    0,
163         /* deformVertsEM */     0,
164         /* deformMatricesEM */  0,
165         /* applyModifier */     applyModifier,
166         /* applyModifierEM */   0,
167         /* initData */          initData,
168         /* requiredDataMask */  requiredDataMask,
169         /* freeData */          freeData,
170         /* isDisabled */        0,
171         /* updateDepgraph */    updateDepgraph,
172         /* dependsOnTime */     dependsOnTime,
173         /* dependsOnNormals */  0,
174         /* foreachObjectLink */ 0,
175         /* foreachIDLink */     foreachIDLink,
176         /* foreachTexLink */    foreachTexLink,
177 };