svn merge ^/trunk/blender -r41226:41227 .
[blender.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 "BLI_math.h"
33 #include "BLI_utildefines.h"
34 #include "BLI_string.h"
35
36 #include "BKE_cdderivedmesh.h"
37 #include "BKE_modifier.h"
38 #include "BKE_deform.h"
39 #include "BKE_texture.h"
40 #include "BKE_colortools.h"
41
42 #include "DNA_object_types.h"
43 #include "DNA_meshdata_types.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         invert_m4_m4(obinv, ob->obmat);
199
200         mul_m4_m4m4(mat_from, wmd->object_from->obmat, obinv);
201         mul_m4_m4m4(mat_to, wmd->object_to->obmat, obinv);
202
203         invert_m4_m4(tmat, mat_from); // swap?
204         mul_m4_m4m4(mat_final, mat_to, tmat);
205
206         invert_m4_m4(mat_from_inv, mat_from);
207
208         unit_m4(mat_unit);
209
210         if(strength < 0.0f) {
211                 float loc[3];
212                 strength = -strength;
213
214                 /* inverted location is not useful, just use the negative */
215                 copy_v3_v3(loc, mat_final[3]);
216                 invert_m4(mat_final);
217                 negate_v3_v3(mat_final[3], loc);
218
219         }
220         weight= strength;
221
222         if(wmd->texture) {
223                 tex_co = MEM_mallocN(sizeof(*tex_co) * numVerts, "warpModifier_do tex_co");
224                 get_texture_coords((MappingInfoModifierData *)wmd, ob, dm, vertexCos, tex_co, numVerts);
225         }
226
227         for(i = 0; i < numVerts; i++) {
228                 float *co = vertexCos[i];
229
230                 if(wmd->falloff_type==eWarp_Falloff_None ||
231                         ((fac=len_v3v3(co, mat_from[3])) < wmd->falloff_radius && (fac=(wmd->falloff_radius-fac)/wmd->falloff_radius)) ) {
232
233                         /* skip if no vert group found */
234                         if(dvert && defgrp_index >= 0) {
235                                 dv = &dvert[i];
236
237                                 if(dv) {
238                                         weight = defvert_find_weight(dv, defgrp_index) * strength;
239                                         if(weight <= 0.0f) /* Should never occure... */
240                                                 continue;
241                                 }
242                         }
243
244
245                         /* closely match PROP_SMOOTH and similar */
246                         switch(wmd->falloff_type) {
247                         case eWarp_Falloff_None:
248                                 fac = 1.0f;
249                                 break;
250                         case eWarp_Falloff_Curve:
251                                 fac = curvemapping_evaluateF(wmd->curfalloff, 0, fac);
252                                 break;
253                         case eWarp_Falloff_Sharp:
254                                 fac = fac*fac;
255                                 break;
256                         case eWarp_Falloff_Smooth:
257                                 fac = 3.0f*fac*fac - 2.0f*fac*fac*fac;
258                                 break;
259                         case eWarp_Falloff_Root:
260                                 fac = (float)sqrt(fac);
261                                 break;
262                         case eWarp_Falloff_Linear:
263                                 /* pass */
264                                 break;
265                         case eWarp_Falloff_Const:
266                                 fac = 1.0f;
267                                 break;
268                         case eWarp_Falloff_Sphere:
269                                 fac = (float)sqrt(2*fac - fac * fac);
270                                 break;
271                         }
272
273                         fac *= weight;
274
275                         if(tex_co) {
276                                 TexResult texres;
277                                 texres.nor = NULL;
278                                 get_texture_value(wmd->texture, tex_co[i], &texres);
279                                 fac *= texres.tin;
280                         }
281
282                         /* into the 'from' objects space */
283                         mul_m4_v3(mat_from_inv, co);
284
285                         if(fac >= 1.0f) {
286                                 mul_m4_v3(mat_final, co);
287                         }
288                         else if(fac > 0.0f) {
289                                 if(wmd->flag & MOD_WARP_VOLUME_PRESERVE) {
290                                         /* interpolate the matrix for nicer locations */
291                                         blend_m4_m4m4(tmat, mat_unit, mat_final, fac);
292                                         mul_m4_v3(tmat, co);
293                                 }
294                                 else {
295                                         float tvec[3];
296                                         mul_v3_m4v3(tvec, mat_final, co);
297                                         interp_v3_v3v3(co, co, tvec, fac);
298                                 }
299                         }
300
301                         /* out of the 'from' objects space */
302                         mul_m4_v3(mat_from, co);
303                 }
304         }
305
306         if(tex_co)
307                 MEM_freeN(tex_co);
308
309 }
310
311 static int warp_needs_dm(WarpModifierData *wmd)
312 {
313         return wmd->texture || wmd->defgrp_name[0];
314 }
315
316 static void deformVerts(ModifierData *md, Object *ob, DerivedMesh *derivedData,
317                         float (*vertexCos)[3], int numVerts, int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
318 {
319         DerivedMesh *dm= NULL;
320         int use_dm= warp_needs_dm((WarpModifierData *)md);
321
322         if(use_dm) {
323                 dm= get_cddm(ob, NULL, derivedData, vertexCos);
324         }
325
326         warpModifier_do((WarpModifierData *)md, ob, dm, vertexCos, numVerts);
327
328         if(use_dm) {
329                 if(dm != derivedData) dm->release(dm);
330         }
331 }
332
333 static void deformVertsEM(ModifierData *md, Object *ob, struct BMEditMesh *editData,
334                           DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
335 {
336         DerivedMesh *dm = derivedData;
337         int use_dm= warp_needs_dm((WarpModifierData *)md);
338
339         if(use_dm) {
340                 if(!derivedData)
341                         dm = CDDM_from_BMEditMesh(editData, ob->data, 0);
342         }
343
344         deformVerts(md, ob, dm, vertexCos, numVerts, 0, 0);
345
346         if(use_dm) {
347                 if(!derivedData) dm->release(dm);
348         }
349 }
350
351
352 ModifierTypeInfo modifierType_Warp = {
353         /* name */              "Warp",
354         /* structName */        "WarpModifierData",
355         /* structSize */        sizeof(WarpModifierData),
356         /* type */              eModifierTypeType_OnlyDeform,
357         /* flags */             eModifierTypeFlag_AcceptsCVs
358                                                         | eModifierTypeFlag_SupportsEditmode,
359         /* copyData */          copyData,
360         /* deformVerts */       deformVerts,
361         /* deformMatrices */    NULL,
362         /* deformVertsEM */     deformVertsEM,
363         /* deformMatricesEM */  NULL,
364         /* applyModifier */     0,
365         /* applyModifierEM */   0,
366         /* initData */          initData,
367         /* requiredDataMask */  requiredDataMask,
368         /* freeData */          freeData,
369         /* isDisabled */        isDisabled,
370         /* updateDepgraph */    updateDepgraph,
371         /* dependsOnTime */     dependsOnTime,
372         /* dependsOnNormals */  NULL,
373         /* foreachObjectLink */ foreachObjectLink,
374         /* foreachIDLink */     foreachIDLink,
375         /* foreachTexLink */    foreachTexLink,
376 };