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