change curve evaluation functions never to modify curve data (ensures thread safety...
[blender-staging.git] / source / blender / modifiers / intern / MOD_warp.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  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  *
22  */
23
24 /** \file blender/modifiers/intern/MOD_warp.c
25  *  \ingroup modifiers
26  */
27
28 #include <string.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_object_types.h"
33 #include "DNA_meshdata_types.h"
34
35 #include "BLI_math.h"
36 #include "BLI_utildefines.h"
37 #include "BLI_string.h"
38
39 #include "BKE_cdderivedmesh.h"
40 #include "BKE_modifier.h"
41 #include "BKE_deform.h"
42 #include "BKE_texture.h"
43 #include "BKE_colortools.h"
44
45 #include "depsgraph_private.h"
46
47 #include "RE_shader_ext.h"
48
49 #include "MOD_util.h"
50
51
52 static void initData(ModifierData *md)
53 {
54         WarpModifierData *wmd = (WarpModifierData *) md;
55
56         wmd->curfalloff = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
57         wmd->texture = NULL;
58         wmd->strength = 1.0f;
59         wmd->falloff_radius = 1.0f;
60         wmd->falloff_type = eWarp_Falloff_Smooth;
61         wmd->flag = 0;
62 }
63
64 static void copyData(ModifierData *md, ModifierData *target)
65 {
66         WarpModifierData *wmd = (WarpModifierData *) md;
67         WarpModifierData *twmd = (WarpModifierData *) target;
68
69         twmd->object_from = wmd->object_from;
70         twmd->object_to = wmd->object_to;
71
72         twmd->strength = wmd->strength;
73         twmd->falloff_radius = wmd->falloff_radius;
74         twmd->falloff_type = wmd->falloff_type;
75         BLI_strncpy(twmd->defgrp_name, wmd->defgrp_name, sizeof(twmd->defgrp_name));
76         twmd->curfalloff = curvemapping_copy(wmd->curfalloff);
77
78         /* map info */
79         twmd->texture = wmd->texture;
80         twmd->map_object = wmd->map_object;
81         BLI_strncpy(twmd->uvlayer_name, wmd->uvlayer_name, sizeof(twmd->uvlayer_name));
82         twmd->texmapping = wmd->texmapping;
83 }
84
85 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
86 {
87         WarpModifierData *wmd = (WarpModifierData *)md;
88         CustomDataMask dataMask = 0;
89
90         /* ask for vertexgroups if we need them */
91         if (wmd->defgrp_name[0]) dataMask |= (CD_MASK_MDEFORMVERT);
92         dataMask |= (CD_MASK_MDEFORMVERT);
93
94         /* ask for UV coordinates if we need them */
95         if (wmd->texmapping == MOD_DISP_MAP_UV) dataMask |= (1 << CD_MTFACE);
96
97         return dataMask;
98 }
99
100 static int dependsOnTime(ModifierData *md)
101 {
102         WarpModifierData *wmd = (WarpModifierData *)md;
103
104         if (wmd->texture) {
105                 return BKE_texture_dependsOnTime(wmd->texture);
106         }
107         else {
108                 return 0;
109         }
110 }
111
112 static void freeData(ModifierData *md)
113 {
114         WarpModifierData *wmd = (WarpModifierData *) md;
115         curvemapping_free(wmd->curfalloff);
116 }
117
118
119 static int isDisabled(ModifierData *md, int UNUSED(userRenderParams))
120 {
121         WarpModifierData *wmd = (WarpModifierData *) md;
122
123         return !(wmd->object_from && wmd->object_to);
124 }
125
126 static void foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk, void *userData)
127 {
128         WarpModifierData *wmd = (WarpModifierData *) md;
129
130         walk(userData, ob, &wmd->object_from);
131         walk(userData, ob, &wmd->object_to);
132         walk(userData, ob, &wmd->map_object);
133 }
134
135 static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
136 {
137         WarpModifierData *wmd = (WarpModifierData *) md;
138
139         walk(userData, ob, (ID **)&wmd->texture);
140
141         walk(userData, ob, (ID **)&wmd->object_from);
142         walk(userData, ob, (ID **)&wmd->object_to);
143         walk(userData, ob, (ID **)&wmd->map_object);
144 }
145
146 static void foreachTexLink(ModifierData *md, Object *ob, TexWalkFunc walk, void *userData)
147 {
148         walk(userData, ob, md, "texture");
149 }
150
151 static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *UNUSED(scene),
152                            Object *UNUSED(ob), DagNode *obNode)
153 {
154         WarpModifierData *wmd = (WarpModifierData *) md;
155
156         if (wmd->object_from && wmd->object_to) {
157                 DagNode *fromNode = dag_get_node(forest, wmd->object_from);
158                 DagNode *toNode = dag_get_node(forest, wmd->object_to);
159
160                 dag_add_relation(forest, fromNode, obNode, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Warp Modifier1");
161                 dag_add_relation(forest, toNode, obNode, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Warp Modifier2");
162         }
163
164         if ((wmd->texmapping == MOD_DISP_MAP_OBJECT) && wmd->map_object) {
165                 DagNode *curNode = dag_get_node(forest, wmd->map_object);
166                 dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Warp Modifier3");
167         }
168 }
169
170 static void warpModifier_do(WarpModifierData *wmd, Object *ob,
171                             DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
172 {
173         float obinv[4][4];
174         float mat_from[4][4];
175         float mat_from_inv[4][4];
176         float mat_to[4][4];
177         float mat_unit[4][4];
178         float mat_final[4][4];
179
180         float tmat[4][4];
181
182         float strength = wmd->strength;
183         float fac = 1.0f, weight;
184         int i;
185         int defgrp_index;
186         MDeformVert *dvert, *dv = NULL;
187
188         float (*tex_co)[3] = NULL;
189
190         if (!(wmd->object_from && wmd->object_to))
191                 return;
192
193         modifier_get_vgroup(ob, dm, wmd->defgrp_name, &dvert, &defgrp_index);
194
195         if (wmd->curfalloff == NULL) /* should never happen, but bad lib linking could cause it */
196                 wmd->curfalloff = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
197
198         if (wmd->curfalloff) {
199                 curvemapping_initialize(wmd->curfalloff);
200         }
201
202         invert_m4_m4(obinv, ob->obmat);
203
204         mult_m4_m4m4(mat_from, obinv, wmd->object_from->obmat);
205         mult_m4_m4m4(mat_to, obinv, wmd->object_to->obmat);
206
207         invert_m4_m4(tmat, mat_from); // swap?
208         mult_m4_m4m4(mat_final, tmat, mat_to);
209
210         invert_m4_m4(mat_from_inv, mat_from);
211
212         unit_m4(mat_unit);
213
214         if (strength < 0.0f) {
215                 float loc[3];
216                 strength = -strength;
217
218                 /* inverted location is not useful, just use the negative */
219                 copy_v3_v3(loc, mat_final[3]);
220                 invert_m4(mat_final);
221                 negate_v3_v3(mat_final[3], loc);
222
223         }
224         weight = strength;
225
226         if (wmd->texture) {
227                 tex_co = MEM_mallocN(sizeof(*tex_co) * numVerts, "warpModifier_do tex_co");
228                 get_texture_coords((MappingInfoModifierData *)wmd, ob, dm, vertexCos, tex_co, numVerts);
229
230                 modifier_init_texture(wmd->modifier.scene, wmd->texture);
231         }
232
233         for (i = 0; i < numVerts; i++) {
234                 float *co = vertexCos[i];
235
236                 if (wmd->falloff_type == eWarp_Falloff_None ||
237                     ((fac = len_v3v3(co, mat_from[3])) < wmd->falloff_radius &&
238                      (fac = (wmd->falloff_radius - fac) / wmd->falloff_radius)))
239                 {
240                         /* skip if no vert group found */
241                         if (dvert && defgrp_index >= 0) {
242                                 dv = &dvert[i];
243
244                                 if (dv) {
245                                         weight = defvert_find_weight(dv, defgrp_index) * strength;
246                                         if (weight <= 0.0f) /* Should never occure... */
247                                                 continue;
248                                 }
249                         }
250
251
252                         /* closely match PROP_SMOOTH and similar */
253                         switch (wmd->falloff_type) {
254                                 case eWarp_Falloff_None:
255                                         fac = 1.0f;
256                                         break;
257                                 case eWarp_Falloff_Curve:
258                                         fac = curvemapping_evaluateF(wmd->curfalloff, 0, fac);
259                                         break;
260                                 case eWarp_Falloff_Sharp:
261                                         fac = fac * fac;
262                                         break;
263                                 case eWarp_Falloff_Smooth:
264                                         fac = 3.0f * fac * fac - 2.0f * fac * fac * fac;
265                                         break;
266                                 case eWarp_Falloff_Root:
267                                         fac = (float)sqrt(fac);
268                                         break;
269                                 case eWarp_Falloff_Linear:
270                                         /* pass */
271                                         break;
272                                 case eWarp_Falloff_Const:
273                                         fac = 1.0f;
274                                         break;
275                                 case eWarp_Falloff_Sphere:
276                                         fac = (float)sqrt(2 * fac - fac * fac);
277                                         break;
278                         }
279
280                         fac *= weight;
281
282                         if (tex_co) {
283                                 TexResult texres;
284                                 texres.nor = NULL;
285                                 get_texture_value(wmd->texture, tex_co[i], &texres);
286                                 fac *= texres.tin;
287                         }
288
289                         /* into the 'from' objects space */
290                         mul_m4_v3(mat_from_inv, co);
291
292                         if (fac >= 1.0f) {
293                                 mul_m4_v3(mat_final, co);
294                         }
295                         else if (fac > 0.0f) {
296                                 if (wmd->flag & MOD_WARP_VOLUME_PRESERVE) {
297                                         /* interpolate the matrix for nicer locations */
298                                         blend_m4_m4m4(tmat, mat_unit, mat_final, fac);
299                                         mul_m4_v3(tmat, co);
300                                 }
301                                 else {
302                                         float tvec[3];
303                                         mul_v3_m4v3(tvec, mat_final, co);
304                                         interp_v3_v3v3(co, co, tvec, fac);
305                                 }
306                         }
307
308                         /* out of the 'from' objects space */
309                         mul_m4_v3(mat_from, co);
310                 }
311         }
312
313         if (tex_co)
314                 MEM_freeN(tex_co);
315
316 }
317
318 static int warp_needs_dm(WarpModifierData *wmd)
319 {
320         return wmd->texture || wmd->defgrp_name[0];
321 }
322
323 static void deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData,
324                         float (*vertexCos)[3], int numVerts, ModifierApplyFlag UNUSED(flag))
325 {
326         DerivedMesh *dm = NULL;
327         int use_dm = warp_needs_dm((WarpModifierData *)md);
328
329         if (use_dm) {
330                 dm = get_cddm(ob, NULL, derivedData, vertexCos);
331         }
332
333         warpModifier_do((WarpModifierData *)md, ob, dm, vertexCos, numVerts);
334
335         if (use_dm) {
336                 if (dm != derivedData) dm->release(dm);
337         }
338 }
339
340 static void deformVertsEM(ModifierData *md, Object *ob, struct BMEditMesh *editData,
341                           DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
342 {
343         DerivedMesh *dm = derivedData;
344         int use_dm = warp_needs_dm((WarpModifierData *)md);
345
346         if (use_dm) {
347                 if (!derivedData)
348                         dm = CDDM_from_BMEditMesh(editData, ob->data, FALSE, FALSE);
349         }
350
351         deformVerts(md, ob, dm, vertexCos, numVerts, 0);
352
353         if (use_dm) {
354                 if (!derivedData) dm->release(dm);
355         }
356 }
357
358
359 ModifierTypeInfo modifierType_Warp = {
360         /* name */ "Warp",
361         /* structName */ "WarpModifierData",
362         /* structSize */ sizeof(WarpModifierData),
363         /* type */ eModifierTypeType_OnlyDeform,
364         /* flags */ eModifierTypeFlag_AcceptsCVs |
365         eModifierTypeFlag_SupportsEditmode,
366         /* copyData */ copyData,
367         /* deformVerts */ deformVerts,
368         /* deformMatrices */ NULL,
369         /* deformVertsEM */ deformVertsEM,
370         /* deformMatricesEM */ NULL,
371         /* applyModifier */ 0,
372         /* applyModifierEM */ 0,
373         /* initData */ initData,
374         /* requiredDataMask */ requiredDataMask,
375         /* freeData */ freeData,
376         /* isDisabled */ isDisabled,
377         /* updateDepgraph */ updateDepgraph,
378         /* dependsOnTime */ dependsOnTime,
379         /* dependsOnNormals */ NULL,
380         /* foreachObjectLink */ foreachObjectLink,
381         /* foreachIDLink */ foreachIDLink,
382         /* foreachTexLink */ foreachTexLink,
383 };