Fix modifier freeing code re. ID refcounting.
[blender.git] / source / blender / modifiers / intern / MOD_wave.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_wave.c
32  *  \ingroup modifiers
33  */
34
35
36 #include "BLI_math.h"
37
38 #include "DNA_meshdata_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_object_types.h"
41
42 #include "BLI_utildefines.h"
43
44
45 #include "BKE_deform.h"
46 #include "BKE_DerivedMesh.h"
47 #include "BKE_library.h"
48 #include "BKE_library_query.h"
49 #include "BKE_scene.h"
50 #include "BKE_texture.h"
51
52 #include "depsgraph_private.h"
53
54 #include "MEM_guardedalloc.h"
55 #include "RE_shader_ext.h"
56
57 #include "MOD_modifiertypes.h"
58 #include "MOD_util.h"
59
60 static void initData(ModifierData *md)
61 {
62         WaveModifierData *wmd = (WaveModifierData *) md; // whadya know, moved here from Iraq
63
64         wmd->flag |= (MOD_WAVE_X | MOD_WAVE_Y | MOD_WAVE_CYCL |
65                       MOD_WAVE_NORM_X | MOD_WAVE_NORM_Y | MOD_WAVE_NORM_Z);
66
67         wmd->objectcenter = NULL;
68         wmd->texture = NULL;
69         wmd->map_object = NULL;
70         wmd->height = 0.5f;
71         wmd->width = 1.5f;
72         wmd->speed = 0.25f;
73         wmd->narrow = 1.5f;
74         wmd->lifetime = 0.0f;
75         wmd->damp = 10.0f;
76         wmd->falloff = 0.0f;
77         wmd->texmapping = MOD_DISP_MAP_LOCAL;
78         wmd->defgrp_name[0] = 0;
79 }
80
81 static void copyData(ModifierData *md, ModifierData *target)
82 {
83 #if 0
84         WaveModifierData *wmd = (WaveModifierData *) md;
85         WaveModifierData *twmd = (WaveModifierData *) target;
86 #endif
87
88         modifier_copyData_generic(md, target);
89 }
90
91 static bool dependsOnTime(ModifierData *UNUSED(md))
92 {
93         return true;
94 }
95
96 static void foreachObjectLink(
97         ModifierData *md, Object *ob,
98         ObjectWalkFunc walk, void *userData)
99 {
100         WaveModifierData *wmd = (WaveModifierData *) md;
101
102         walk(userData, ob, &wmd->objectcenter, IDWALK_CB_NOP);
103         walk(userData, ob, &wmd->map_object, IDWALK_CB_NOP);
104 }
105
106 static void foreachIDLink(ModifierData *md, Object *ob,
107                           IDWalkFunc walk, void *userData)
108 {
109         WaveModifierData *wmd = (WaveModifierData *) md;
110
111         walk(userData, ob, (ID **)&wmd->texture, IDWALK_CB_USER);
112
113         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
114 }
115
116 static void foreachTexLink(ModifierData *md, Object *ob,
117                            TexWalkFunc walk, void *userData)
118 {
119         walk(userData, ob, md, "texture");
120 }
121
122 static void updateDepgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
123 {
124         WaveModifierData *wmd = (WaveModifierData *) md;
125
126         if (wmd->objectcenter) {
127                 DagNode *curNode = dag_get_node(ctx->forest, wmd->objectcenter);
128
129                 dag_add_relation(ctx->forest, curNode, ctx->obNode, DAG_RL_OB_DATA,
130                                  "Wave Modifier");
131         }
132
133         if (wmd->map_object) {
134                 DagNode *curNode = dag_get_node(ctx->forest, wmd->map_object);
135
136                 dag_add_relation(ctx->forest, curNode, ctx->obNode, DAG_RL_OB_DATA,
137                                  "Wave Modifer");
138         }
139 }
140
141 static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
142 {
143         WaveModifierData *wmd = (WaveModifierData *)md;
144         if (wmd->objectcenter != NULL) {
145                 DEG_add_object_relation(ctx->node, wmd->objectcenter, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
146         }
147         if (wmd->map_object != NULL) {
148                 DEG_add_object_relation(ctx->node, wmd->map_object, DEG_OB_COMP_TRANSFORM, "Wave Modifier");
149         }
150 }
151
152 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
153 {
154         WaveModifierData *wmd = (WaveModifierData *)md;
155         CustomDataMask dataMask = 0;
156
157
158         /* ask for UV coordinates if we need them */
159         if (wmd->texture && wmd->texmapping == MOD_DISP_MAP_UV)
160                 dataMask |= CD_MASK_MTFACE;
161
162         /* ask for vertexgroups if we need them */
163         if (wmd->defgrp_name[0])
164                 dataMask |= CD_MASK_MDEFORMVERT;
165
166         return dataMask;
167 }
168
169 static void waveModifier_do(WaveModifierData *md, 
170                             Scene *scene, Object *ob, DerivedMesh *dm,
171                             float (*vertexCos)[3], int numVerts)
172 {
173         WaveModifierData *wmd = (WaveModifierData *) md;
174         MVert *mvert = NULL;
175         MDeformVert *dvert;
176         int defgrp_index;
177         float ctime = BKE_scene_frame_get(scene);
178         float minfac = (float)(1.0 / exp(wmd->width * wmd->narrow * wmd->width * wmd->narrow));
179         float lifefac = wmd->height;
180         float (*tex_co)[3] = NULL;
181         const int wmd_axis = wmd->flag & (MOD_WAVE_X | MOD_WAVE_Y);
182         const float falloff = wmd->falloff;
183         float falloff_fac = 1.0f; /* when falloff == 0.0f this stays at 1.0f */
184
185         if ((wmd->flag & MOD_WAVE_NORM) && (ob->type == OB_MESH))
186                 mvert = dm->getVertArray(dm);
187
188         if (wmd->objectcenter) {
189                 float mat[4][4];
190                 /* get the control object's location in local coordinates */
191                 invert_m4_m4(ob->imat, ob->obmat);
192                 mul_m4_m4m4(mat, ob->imat, wmd->objectcenter->obmat);
193
194                 wmd->startx = mat[3][0];
195                 wmd->starty = mat[3][1];
196         }
197
198         /* get the index of the deform group */
199         modifier_get_vgroup(ob, dm, wmd->defgrp_name, &dvert, &defgrp_index);
200
201         if (wmd->damp == 0) wmd->damp = 10.0f;
202
203         if (wmd->lifetime != 0.0f) {
204                 float x = ctime - wmd->timeoffs;
205
206                 if (x > wmd->lifetime) {
207                         lifefac = x - wmd->lifetime;
208
209                         if (lifefac > wmd->damp) lifefac = 0.0;
210                         else lifefac = (float)(wmd->height * (1.0f - sqrtf(lifefac / wmd->damp)));
211                 }
212         }
213
214         if (wmd->texture) {
215                 tex_co = MEM_malloc_arrayN(numVerts, sizeof(*tex_co),
216                                      "waveModifier_do tex_co");
217                 get_texture_coords((MappingInfoModifierData *)wmd, ob, dm, vertexCos, tex_co, numVerts);
218
219                 modifier_init_texture(wmd->modifier.scene, wmd->texture);
220         }
221
222         if (lifefac != 0.0f) {
223                 /* avoid divide by zero checks within the loop */
224                 float falloff_inv = falloff ? 1.0f / falloff : 1.0f;
225                 int i;
226
227                 for (i = 0; i < numVerts; i++) {
228                         float *co = vertexCos[i];
229                         float x = co[0] - wmd->startx;
230                         float y = co[1] - wmd->starty;
231                         float amplit = 0.0f;
232                         float def_weight = 1.0f;
233
234                         /* get weights */
235                         if (dvert) {
236                                 def_weight = defvert_find_weight(&dvert[i], defgrp_index);
237
238                                 /* if this vert isn't in the vgroup, don't deform it */
239                                 if (def_weight == 0.0f) {
240                                         continue;
241                                 }
242                         }
243
244                         switch (wmd_axis) {
245                                 case MOD_WAVE_X | MOD_WAVE_Y:
246                                         amplit = sqrtf(x * x + y * y);
247                                         break;
248                                 case MOD_WAVE_X:
249                                         amplit = x;
250                                         break;
251                                 case MOD_WAVE_Y:
252                                         amplit = y;
253                                         break;
254                         }
255
256                         /* this way it makes nice circles */
257                         amplit -= (ctime - wmd->timeoffs) * wmd->speed;
258
259                         if (wmd->flag & MOD_WAVE_CYCL) {
260                                 amplit = (float)fmodf(amplit - wmd->width, 2.0f * wmd->width) +
261                                          wmd->width;
262                         }
263
264                         if (falloff != 0.0f) {
265                                 float dist = 0.0f;
266
267                                 switch (wmd_axis) {
268                                         case MOD_WAVE_X | MOD_WAVE_Y:
269                                                 dist = sqrtf(x * x + y * y);
270                                                 break;
271                                         case MOD_WAVE_X:
272                                                 dist = fabsf(x);
273                                                 break;
274                                         case MOD_WAVE_Y:
275                                                 dist = fabsf(y);
276                                                 break;
277                                 }
278
279                                 falloff_fac = (1.0f - (dist * falloff_inv));
280                                 CLAMP(falloff_fac, 0.0f, 1.0f);
281                         }
282
283                         /* GAUSSIAN */
284                         if ((falloff_fac != 0.0f) && (amplit > -wmd->width) && (amplit < wmd->width)) {
285                                 amplit = amplit * wmd->narrow;
286                                 amplit = (float)(1.0f / expf(amplit * amplit) - minfac);
287
288                                 /*apply texture*/
289                                 if (wmd->texture) {
290                                         TexResult texres;
291                                         texres.nor = NULL;
292                                         BKE_texture_get_value(wmd->modifier.scene, wmd->texture, tex_co[i], &texres, false);
293                                         amplit *= texres.tin;
294                                 }
295
296                                 /*apply weight & falloff */
297                                 amplit *= def_weight * falloff_fac;
298
299                                 if (mvert) {
300                                         /* move along normals */
301                                         if (wmd->flag & MOD_WAVE_NORM_X) {
302                                                 co[0] += (lifefac * amplit) * mvert[i].no[0] / 32767.0f;
303                                         }
304                                         if (wmd->flag & MOD_WAVE_NORM_Y) {
305                                                 co[1] += (lifefac * amplit) * mvert[i].no[1] / 32767.0f;
306                                         }
307                                         if (wmd->flag & MOD_WAVE_NORM_Z) {
308                                                 co[2] += (lifefac * amplit) * mvert[i].no[2] / 32767.0f;
309                                         }
310                                 }
311                                 else {
312                                         /* move along local z axis */
313                                         co[2] += lifefac * amplit;
314                                 }
315                         }
316                 }
317         }
318
319         if (wmd->texture) MEM_freeN(tex_co);
320 }
321
322 static void deformVerts(ModifierData *md, Object *ob,
323                         DerivedMesh *derivedData,
324                         float (*vertexCos)[3],
325                         int numVerts,
326                         ModifierApplyFlag UNUSED(flag))
327 {
328         DerivedMesh *dm = derivedData;
329         WaveModifierData *wmd = (WaveModifierData *)md;
330
331         if (wmd->flag & MOD_WAVE_NORM)
332                 dm = get_cddm(ob, NULL, dm, vertexCos, false);
333         else if (wmd->texture || wmd->defgrp_name[0])
334                 dm = get_dm(ob, NULL, dm, NULL, false, false);
335
336         waveModifier_do(wmd, md->scene, ob, dm, vertexCos, numVerts);
337
338         if (dm != derivedData)
339                 dm->release(dm);
340 }
341
342 static void deformVertsEM(
343         ModifierData *md, Object *ob, struct BMEditMesh *editData,
344         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
345 {
346         DerivedMesh *dm = derivedData;
347         WaveModifierData *wmd = (WaveModifierData *)md;
348
349         if (wmd->flag & MOD_WAVE_NORM)
350                 dm = get_cddm(ob, editData, dm, vertexCos, false);
351         else if (wmd->texture || wmd->defgrp_name[0])
352                 dm = get_dm(ob, editData, dm, NULL, false, false);
353
354         waveModifier_do(wmd, md->scene, ob, dm, vertexCos, numVerts);
355
356         if (dm != derivedData)
357                 dm->release(dm);
358 }
359
360
361 ModifierTypeInfo modifierType_Wave = {
362         /* name */              "Wave",
363         /* structName */        "WaveModifierData",
364         /* structSize */        sizeof(WaveModifierData),
365         /* type */              eModifierTypeType_OnlyDeform,
366         /* flags */             eModifierTypeFlag_AcceptsCVs |
367                                 eModifierTypeFlag_AcceptsLattice |
368                                 eModifierTypeFlag_SupportsEditmode,
369         /* copyData */          copyData,
370         /* deformVerts */       deformVerts,
371         /* deformMatrices */    NULL,
372         /* deformVertsEM */     deformVertsEM,
373         /* deformMatricesEM */  NULL,
374         /* applyModifier */     NULL,
375         /* applyModifierEM */   NULL,
376         /* initData */          initData,
377         /* requiredDataMask */  requiredDataMask,
378         /* freeData */          NULL,
379         /* isDisabled */        NULL,
380         /* updateDepgraph */    updateDepgraph,
381         /* updateDepsgraph */   updateDepsgraph,
382         /* dependsOnTime */     dependsOnTime,
383         /* dependsOnNormals */  NULL,
384         /* foreachObjectLink */ foreachObjectLink,
385         /* foreachIDLink */     foreachIDLink,
386         /* foreachTexLink */    foreachTexLink,
387 };