Merge with trunk r37849
[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 = 0;
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 void deformVerts(
67                                          ModifierData *md, Object *ob, DerivedMesh *derivedData,
68           float (*vertexCos)[3], int numVerts, int useRenderParams, int isFinalCalc)*/
69
70 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, 
71                                                 DerivedMesh *dm,
72                                                 int useRenderParams,
73                                                 int isFinalCalc)
74 {
75         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
76         //DerivedMesh *dm = get_cddm(ob, NULL, derivedData, vertexCos);
77         DerivedMesh *result;
78
79
80         result = dynamicPaint_Modifier_do(pmd, md->scene, ob, dm);
81
82         /*if(dm != derivedData)
83                 dm->release(dm);*/
84
85         return result;
86 }
87
88 static void updateDepgraph(ModifierData *md, DagForest *forest,
89                                                 struct Scene *scene,
90                                                 Object *ob,
91                                                 DagNode *obNode)
92 {
93         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
94
95         /* add relation from canvases to all brush objects */
96         if(pmd && (pmd->type & MOD_DYNAMICPAINT_TYPE_CANVAS) && pmd->canvas)
97         {
98                 Base *base = scene->base.first;
99
100                 for(; base; base = base->next) {
101                         DynamicPaintModifierData *pmd2 = (DynamicPaintModifierData *)modifiers_findByType(base->object, eModifierType_DynamicPaint);
102
103                         if(pmd2 && (pmd2->type & MOD_DYNAMICPAINT_TYPE_BRUSH) && pmd2->brush)
104                         {
105                                 DagNode *brushNode = dag_get_node(forest, base->object);
106                                 dag_add_relation(forest, brushNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Dynamic Paint Brush");
107                         }
108                 }
109         }
110 }
111
112 static int dependsOnTime(ModifierData *md)
113 {
114         return 1;
115 }
116
117 static void foreachIDLink(ModifierData *md, Object *ob,
118                                            IDWalkFunc walk, void *userData)
119 {
120         DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
121
122         if(pmd->type==MOD_DYNAMICPAINT_TYPE_CANVAS && pmd->canvas) {
123                 DynamicPaintSurface *surface = pmd->canvas->surfaces.first;
124
125                 for(; surface; surface=surface->next) {
126                         walk(userData, ob, (ID **)&surface->brush_group);
127                 }
128         }
129 }
130
131 ModifierTypeInfo modifierType_DynamicPaint = {
132         /* name */              "Dynamic Paint",
133         /* structName */        "DynamicPaintModifierData",
134         /* structSize */        sizeof(DynamicPaintModifierData),
135         /* type */              eModifierTypeType_Constructive,
136         /* flags */             eModifierTypeFlag_AcceptsMesh
137                                                         | eModifierTypeFlag_UsesPointCache
138                                                         | eModifierTypeFlag_Single,
139
140         /* copyData */          copyData,
141         /* deformVerts */       0,
142         /* deformMatrices */    0,
143         /* deformVertsEM */     0,
144         /* deformMatricesEM */  0,
145         /* applyModifier */     applyModifier,
146         /* applyModifierEM */   0,
147         /* initData */          initData,
148         /* requiredDataMask */  requiredDataMask,
149         /* freeData */          freeData,
150         /* isDisabled */        0,
151         /* updateDepgraph */    updateDepgraph,
152         /* dependsOnTime */     dependsOnTime,
153         /* dependsOnNormals */  0,
154         /* foreachObjectLink */ 0,
155         /* foreachIDLink */     foreachIDLink,
156 };