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