9b03816ed0469893a45ce2479b78c10b877eaacd
[blender.git] / source / blender / modifiers / intern / MOD_smoke.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  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_smoke.c
32  *  \ingroup modifiers
33  */
34
35
36 #include <stddef.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_group_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_smoke_types.h"
44 #include "DNA_object_force.h"
45
46 #include "BLI_utildefines.h"
47
48
49 #include "BKE_cdderivedmesh.h"
50 #include "BKE_modifier.h"
51 #include "BKE_smoke.h"
52
53 #include "depsgraph_private.h"
54
55 #include "MOD_util.h"
56
57
58 static void initData(ModifierData *md) 
59 {
60         SmokeModifierData *smd = (SmokeModifierData*) md;
61         
62         smd->domain = NULL;
63         smd->flow = NULL;
64         smd->coll = NULL;
65         smd->type = 0;
66         smd->time = -1;
67 }
68
69 static void copyData(ModifierData *md, ModifierData *target)
70 {
71         SmokeModifierData *smd  = (SmokeModifierData*)md;
72         SmokeModifierData *tsmd = (SmokeModifierData*)target;
73         
74         smokeModifier_copy(smd, tsmd);
75 }
76
77 static void freeData(ModifierData *md)
78 {
79         SmokeModifierData *smd = (SmokeModifierData*) md;
80         
81         smokeModifier_free (smd);
82 }
83
84 static void deformVerts(ModifierData *md, Object *ob,
85                                                 DerivedMesh *derivedData,
86                                                 float (*vertexCos)[3],
87                                                 int UNUSED(numVerts),
88                                                 int UNUSED(useRenderParams),
89                                                 int UNUSED(isFinalCalc))
90 {
91         SmokeModifierData *smd = (SmokeModifierData*) md;
92         DerivedMesh *dm = get_cddm(ob, NULL, derivedData, vertexCos);
93
94         smokeModifier_do(smd, md->scene, ob, dm);
95
96         if(dm != derivedData)
97                 dm->release(dm);
98 }
99
100 static int dependsOnTime(ModifierData *UNUSED(md))
101 {
102         return 1;
103 }
104
105 static void updateDepgraph(ModifierData *md, DagForest *forest,
106                                                 struct Scene *scene,
107                                                 Object *UNUSED(ob),
108                                                 DagNode *obNode)
109 {
110         SmokeModifierData *smd = (SmokeModifierData *) md;
111
112         if(smd && (smd->type & MOD_SMOKE_TYPE_DOMAIN) && smd->domain)
113         {
114                 if(smd->domain->fluid_group)
115                 {
116                         GroupObject *go = NULL;
117                         
118                         for(go = smd->domain->fluid_group->gobject.first; go; go = go->next) 
119                         {
120                                 if(go->ob)
121                                 {
122                                         SmokeModifierData *smd2 = (SmokeModifierData *)modifiers_findByType(go->ob, eModifierType_Smoke);
123                                         
124                                         // check for initialized smoke object
125                                         if(smd2 && (((smd2->type & MOD_SMOKE_TYPE_FLOW) && smd2->flow) || ((smd->type & MOD_SMOKE_TYPE_COLL) && smd2->coll)))
126                                         {
127                                                 DagNode *curNode = dag_get_node(forest, go->ob);
128                                                 dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Smoke Flow");
129                                         }
130                                 }
131                         }
132                 }
133                 else {
134                         Base *base = scene->base.first;
135
136                         for(; base; base = base->next) {
137                                 SmokeModifierData *smd2 = (SmokeModifierData *)modifiers_findByType(base->object, eModifierType_Smoke);
138
139                                 if(smd2 && (((smd2->type & MOD_SMOKE_TYPE_FLOW) && smd2->flow) || ((smd->type & MOD_SMOKE_TYPE_COLL) && smd2->coll)))
140                                 {
141                                         DagNode *curNode = dag_get_node(forest, base->object);
142                                         dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA, "Smoke Flow");
143                                 }
144                         }
145                 }
146         }
147 }
148
149 static void foreachIDLink(ModifierData *md, Object *ob,
150                                            IDWalkFunc walk, void *userData)
151 {
152         SmokeModifierData *smd = (SmokeModifierData*) md;
153
154         if(smd->type==MOD_SMOKE_TYPE_DOMAIN && smd->domain) {
155                 walk(userData, ob, (ID **)&smd->domain->coll_group);
156                 walk(userData, ob, (ID **)&smd->domain->fluid_group);
157                 walk(userData, ob, (ID **)&smd->domain->eff_group);
158
159                 if(smd->domain->effector_weights) {
160                         walk(userData, ob, (ID **)&smd->domain->effector_weights->group);
161                 }
162         }
163 }
164
165 ModifierTypeInfo modifierType_Smoke = {
166         /* name */              "Smoke",
167         /* structName */        "SmokeModifierData",
168         /* structSize */        sizeof(SmokeModifierData),
169         /* type */              eModifierTypeType_OnlyDeform,
170         /* flags */             eModifierTypeFlag_AcceptsMesh
171                                                         | eModifierTypeFlag_UsesPointCache
172                                                         | eModifierTypeFlag_Single,
173
174         /* copyData */          copyData,
175         /* deformVerts */       deformVerts,
176         /* deformMatrices */    NULL,
177         /* deformVertsEM */     NULL,
178         /* deformMatricesEM */  NULL,
179         /* applyModifier */     NULL,
180         /* applyModifierEM */   NULL,
181         /* initData */          initData,
182         /* requiredDataMask */  NULL,
183         /* freeData */          freeData,
184         /* isDisabled */        NULL,
185         /* updateDepgraph */    updateDepgraph,
186         /* dependsOnTime */     dependsOnTime,
187         /* dependsOnNormals */  NULL,
188         /* foreachObjectLink */ NULL,
189         /* foreachIDLink */     foreachIDLink,
190         /* foreachTexLink */    NULL
191 };