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