Fix #35174: dynamic paint displacement missing in render.
[blender-staging.git] / source / blender / modifiers / intern / MOD_weightvgmix.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) 2011 by Bastien Montagne.
19  * All rights reserved.
20  *
21  * Contributor(s): None yet.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  */
26
27 /** \file blender/modifiers/intern/MOD_weightvgmix.c
28  *  \ingroup modifiers
29  */
30
31 #include "BLI_utildefines.h"
32 #include "BLI_math.h"
33 #include "BLI_string.h"
34
35 #include "DNA_mesh_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_modifier_types.h"
38 #include "DNA_object_types.h"
39
40 #include "BKE_cdderivedmesh.h"
41 #include "BKE_deform.h"
42 #include "BKE_mesh.h"
43 #include "BKE_modifier.h"
44 #include "BKE_texture.h"          /* Texture masking. */
45
46 #include "depsgraph_private.h"
47 #include "MEM_guardedalloc.h"
48 #include "MOD_util.h"
49 #include "MOD_weightvg_util.h"
50
51
52 /**
53  * This mixes the old weight with the new weight factor.
54  */
55 static float mix_weight(float weight, float weight2, char mix_mode)
56 {
57 #if 0
58         /*
59          * XXX Don't know why, but the switch version takes many CPU time,
60          *     and produces lag in realtime playback...
61          */
62         switch (mix_mode)
63         {
64                 case MOD_WVG_MIX_ADD:
65                         return (weight + weight2);
66                 case MOD_WVG_MIX_SUB:
67                         return (weight - weight2);
68                 case MOD_WVG_MIX_MUL:
69                         return (weight * weight2);
70                 case MOD_WVG_MIX_DIV:
71                         /* Avoid dividing by zero (or really small values). */
72                         if (0.0 <= weight2 < MOD_WVG_ZEROFLOOR)
73                                 weight2 = MOD_WVG_ZEROFLOOR;
74                         else if (-MOD_WVG_ZEROFLOOR < weight2)
75                                 weight2 = -MOD_WVG_ZEROFLOOR;
76                         return (weight / weight2);
77                 case MOD_WVG_MIX_DIF:
78                         return (weight < weight2 ? weight2 - weight : weight - weight2);
79                 case MOD_WVG_MIX_AVG:
80                         return (weight + weight2) / 2.0;
81                 case MOD_WVG_MIX_SET:
82                 default:
83                         return weight2;
84         }
85 #endif
86         if (mix_mode == MOD_WVG_MIX_SET)
87                 return weight2;
88         else if (mix_mode == MOD_WVG_MIX_ADD)
89                 return (weight + weight2);
90         else if (mix_mode == MOD_WVG_MIX_SUB)
91                 return (weight - weight2);
92         else if (mix_mode == MOD_WVG_MIX_MUL)
93                 return (weight * weight2);
94         else if (mix_mode == MOD_WVG_MIX_DIV) {
95                 /* Avoid dividing by zero (or really small values). */
96                 if (weight2 < 0.0f && weight2 > -MOD_WVG_ZEROFLOOR)
97                         weight2 = -MOD_WVG_ZEROFLOOR;
98                 else if (weight2 >= 0.0f && weight2 < MOD_WVG_ZEROFLOOR)
99                         weight2 = MOD_WVG_ZEROFLOOR;
100                 return (weight / weight2);
101         }
102         else if (mix_mode == MOD_WVG_MIX_DIF)
103                 return (weight < weight2 ? weight2 - weight : weight - weight2);
104         else if (mix_mode == MOD_WVG_MIX_AVG)
105                 return (weight + weight2) * 0.5f;
106         else return weight2;
107 }
108
109 /**************************************
110  * Modifiers functions.               *
111  **************************************/
112 static void initData(ModifierData *md)
113 {
114         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
115
116         wmd->default_weight_a       = 0.0f;
117         wmd->default_weight_b       = 0.0f;
118         wmd->mix_mode               = MOD_WVG_MIX_SET;
119         wmd->mix_set                = MOD_WVG_SET_AND;
120
121         wmd->mask_constant          = 1.0f;
122         wmd->mask_tex_use_channel   = MOD_WVG_MASK_TEX_USE_INT; /* Use intensity by default. */
123         wmd->mask_tex_mapping       = MOD_DISP_MAP_LOCAL;
124 }
125
126 static void copyData(ModifierData *md, ModifierData *target)
127 {
128         WeightVGMixModifierData *wmd  = (WeightVGMixModifierData *) md;
129         WeightVGMixModifierData *twmd = (WeightVGMixModifierData *) target;
130
131         BLI_strncpy(twmd->defgrp_name_a, wmd->defgrp_name_a, sizeof(twmd->defgrp_name_a));
132         BLI_strncpy(twmd->defgrp_name_b, wmd->defgrp_name_b, sizeof(twmd->defgrp_name_b));
133         twmd->default_weight_a       = wmd->default_weight_a;
134         twmd->default_weight_b       = wmd->default_weight_b;
135         twmd->mix_mode               = wmd->mix_mode;
136         twmd->mix_set                = wmd->mix_set;
137
138         twmd->mask_constant          = wmd->mask_constant;
139         BLI_strncpy(twmd->mask_defgrp_name, wmd->mask_defgrp_name, sizeof(twmd->mask_defgrp_name));
140         twmd->mask_texture           = wmd->mask_texture;
141         twmd->mask_tex_use_channel   = wmd->mask_tex_use_channel;
142         twmd->mask_tex_mapping       = wmd->mask_tex_mapping;
143         twmd->mask_tex_map_obj       = wmd->mask_tex_map_obj;
144         BLI_strncpy(twmd->mask_tex_uvlayer_name, wmd->mask_tex_uvlayer_name, sizeof(twmd->mask_tex_uvlayer_name));
145 }
146
147 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
148 {
149         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
150         CustomDataMask dataMask = 0;
151
152         /* We need vertex groups! */
153         dataMask |= CD_MASK_MDEFORMVERT;
154
155         /* Ask for UV coordinates if we need them. */
156         if (wmd->mask_tex_mapping == MOD_DISP_MAP_UV)
157                 dataMask |= CD_MASK_MTFACE;
158
159         /* No need to ask for CD_PREVIEW_MLOOPCOL... */
160
161         return dataMask;
162 }
163
164 static int dependsOnTime(ModifierData *md)
165 {
166         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
167
168         if (wmd->mask_texture)
169                 return BKE_texture_dependsOnTime(wmd->mask_texture);
170         return 0;
171 }
172
173 static void foreachObjectLink(ModifierData *md, Object *ob,
174                               void (*walk)(void *userData, Object *ob, Object **obpoin),
175                               void *userData)
176 {
177         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
178         walk(userData, ob, &wmd->mask_tex_map_obj);
179 }
180
181 static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
182 {
183         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
184
185         walk(userData, ob, (ID **)&wmd->mask_texture);
186
187         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
188 }
189
190 static void foreachTexLink(ModifierData *md, Object *ob, TexWalkFunc walk, void *userData)
191 {
192         walk(userData, ob, md, "mask_texture");
193 }
194
195 static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *UNUSED(scene),
196                            Object *UNUSED(ob), DagNode *obNode)
197 {
198         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
199         DagNode *curNode;
200
201         if (wmd->mask_tex_map_obj && wmd->mask_tex_mapping == MOD_DISP_MAP_OBJECT) {
202                 curNode = dag_get_node(forest, wmd->mask_tex_map_obj);
203
204                 dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA | DAG_RL_OB_DATA,
205                                  "WeightVGMix Modifier");
206         }
207
208         if (wmd->mask_tex_mapping == MOD_DISP_MAP_GLOBAL)
209                 dag_add_relation(forest, obNode, obNode, DAG_RL_DATA_DATA | DAG_RL_OB_DATA,
210                                  "WeightVGMix Modifier");
211 }
212
213 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
214 {
215         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
216         /* If no vertex group, bypass. */
217         return (wmd->defgrp_name_a[0] == '\0');
218 }
219
220 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
221                                   ModifierApplyFlag UNUSED(flag))
222 {
223         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
224         DerivedMesh *dm = derivedData;
225         MDeformVert *dvert = NULL;
226         MDeformWeight **dw1, **tdw1, **dw2, **tdw2;
227         int numVerts;
228         int defgrp_index, defgrp_index_other = -1;
229         float *org_w;
230         float *new_w;
231         int *tidx, *indices = NULL;
232         int numIdx = 0;
233         int i;
234         /* Flags. */
235 #if 0
236         int do_prev = (wmd->modifier.mode & eModifierMode_DoWeightPreview);
237 #endif
238
239         /* Get number of verts. */
240         numVerts = dm->getNumVerts(dm);
241
242         /* Check if we can just return the original mesh.
243          * Must have verts and therefore verts assigned to vgroups to do anything useful!
244          */
245         if ((numVerts == 0) || (ob->defbase.first == NULL))
246                 return dm;
247
248         /* Get vgroup idx from its name. */
249         defgrp_index = defgroup_name_index(ob, wmd->defgrp_name_a);
250         if (defgrp_index == -1)
251                 return dm;
252         /* Get seconf vgroup idx from its name, if given. */
253         if (wmd->defgrp_name_b[0] != (char)0) {
254                 defgrp_index_other = defgroup_name_index(ob, wmd->defgrp_name_b);
255                 if (defgrp_index_other == -1)
256                         return dm;
257         }
258
259         dvert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MDEFORMVERT, numVerts);
260         /* If no vertices were ever added to an object's vgroup, dvert might be NULL. */
261         if (!dvert) {
262                 /* If not affecting all vertices, just return. */
263                 if (wmd->mix_set != MOD_WVG_SET_ALL)
264                         return dm;
265                 /* Else, add a valid data layer! */
266                 dvert = CustomData_add_layer_named(&dm->vertData, CD_MDEFORMVERT, CD_CALLOC,
267                                                    NULL, numVerts, wmd->defgrp_name_a);
268                 /* Ultimate security check. */
269                 if (!dvert)
270                         return dm;
271         }
272         /* Find out which vertices to work on. */
273         tidx = MEM_mallocN(sizeof(int) * numVerts, "WeightVGMix Modifier, tidx");
274         tdw1 = MEM_mallocN(sizeof(MDeformWeight *) * numVerts, "WeightVGMix Modifier, tdw1");
275         tdw2 = MEM_mallocN(sizeof(MDeformWeight *) * numVerts, "WeightVGMix Modifier, tdw2");
276         switch (wmd->mix_set) {
277                 case MOD_WVG_SET_A:
278                         /* All vertices in first vgroup. */
279                         for (i = 0; i < numVerts; i++) {
280                                 MDeformWeight *dw = defvert_find_index(&dvert[i], defgrp_index);
281                                 if (dw) {
282                                         tdw1[numIdx] = dw;
283                                         tdw2[numIdx] = defvert_find_index(&dvert[i], defgrp_index_other);
284                                         tidx[numIdx++] = i;
285                                 }
286                         }
287                         break;
288                 case MOD_WVG_SET_B:
289                         /* All vertices in second vgroup. */
290                         for (i = 0; i < numVerts; i++) {
291                                 MDeformWeight *dw = defvert_find_index(&dvert[i], defgrp_index_other);
292                                 if (dw) {
293                                         tdw1[numIdx] = defvert_find_index(&dvert[i], defgrp_index);
294                                         tdw2[numIdx] = dw;
295                                         tidx[numIdx++] = i;
296                                 }
297                         }
298                         break;
299                 case MOD_WVG_SET_OR:
300                         /* All vertices in one vgroup or the other. */
301                         for (i = 0; i < numVerts; i++) {
302                                 MDeformWeight *adw = defvert_find_index(&dvert[i], defgrp_index);
303                                 MDeformWeight *bdw = defvert_find_index(&dvert[i], defgrp_index_other);
304                                 if (adw || bdw) {
305                                         tdw1[numIdx] = adw;
306                                         tdw2[numIdx] = bdw;
307                                         tidx[numIdx++] = i;
308                                 }
309                         }
310                         break;
311                 case MOD_WVG_SET_AND:
312                         /* All vertices in both vgroups. */
313                         for (i = 0; i < numVerts; i++) {
314                                 MDeformWeight *adw = defvert_find_index(&dvert[i], defgrp_index);
315                                 MDeformWeight *bdw = defvert_find_index(&dvert[i], defgrp_index_other);
316                                 if (adw && bdw) {
317                                         tdw1[numIdx] = adw;
318                                         tdw2[numIdx] = bdw;
319                                         tidx[numIdx++] = i;
320                                 }
321                         }
322                         break;
323                 case MOD_WVG_SET_ALL:
324                 default:
325                         /* Use all vertices. */
326                         for (i = 0; i < numVerts; i++) {
327                                 tdw1[i] = defvert_find_index(&dvert[i], defgrp_index);
328                                 tdw2[i] = defvert_find_index(&dvert[i], defgrp_index_other);
329                         }
330                         numIdx = -1;
331                         break;
332         }
333         if (numIdx == 0) {
334                 /* Use no vertices! Hence, return org data. */
335                 MEM_freeN(tdw1);
336                 MEM_freeN(tdw2);
337                 MEM_freeN(tidx);
338                 return dm;
339         }
340         if (numIdx != -1) {
341                 indices = MEM_mallocN(sizeof(int) * numIdx, "WeightVGMix Modifier, indices");
342                 memcpy(indices, tidx, sizeof(int) * numIdx);
343                 dw1 = MEM_mallocN(sizeof(MDeformWeight *) * numIdx, "WeightVGMix Modifier, dw1");
344                 memcpy(dw1, tdw1, sizeof(MDeformWeight *) * numIdx);
345                 MEM_freeN(tdw1);
346                 dw2 = MEM_mallocN(sizeof(MDeformWeight *) * numIdx, "WeightVGMix Modifier, dw2");
347                 memcpy(dw2, tdw2, sizeof(MDeformWeight *) * numIdx);
348                 MEM_freeN(tdw2);
349         }
350         else {
351                 /* Use all vertices. */
352                 numIdx = numVerts;
353                 /* Just copy MDeformWeight pointers arrays, they will be freed at the end. */
354                 dw1 = tdw1;
355                 dw2 = tdw2;
356         }
357         MEM_freeN(tidx);
358
359         org_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, org_w");
360         new_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, new_w");
361
362         /* Mix weights. */
363         for (i = 0; i < numIdx; i++) {
364                 float weight2;
365                 org_w[i] = dw1[i] ? dw1[i]->weight : wmd->default_weight_a;
366                 weight2  = dw2[i] ? dw2[i]->weight : wmd->default_weight_b;
367
368                 new_w[i] = mix_weight(org_w[i], weight2, wmd->mix_mode);
369         }
370
371         /* Do masking. */
372         weightvg_do_mask(numIdx, indices, org_w, new_w, ob, dm, wmd->mask_constant,
373                          wmd->mask_defgrp_name, wmd->modifier.scene, wmd->mask_texture,
374                          wmd->mask_tex_use_channel, wmd->mask_tex_mapping,
375                          wmd->mask_tex_map_obj, wmd->mask_tex_uvlayer_name);
376
377         /* Update (add to) vgroup.
378          * XXX Depending on the MOD_WVG_SET_xxx option chosen, we might have to add vertices to vgroup.
379          */
380         weightvg_update_vg(dvert, defgrp_index, dw1, numIdx, indices, org_w, TRUE, -FLT_MAX, FALSE, 0.0f);
381
382         /* If weight preview enabled... */
383 #if 0 /* XXX Currently done in mod stack :/ */
384         if (do_prev)
385                 DM_update_weight_mcol(ob, dm, 0, org_w, numIdx, indices);
386 #endif
387
388         /* Freeing stuff. */
389         MEM_freeN(org_w);
390         MEM_freeN(new_w);
391         MEM_freeN(dw1);
392         MEM_freeN(dw2);
393
394         if (indices)
395                 MEM_freeN(indices);
396
397         /* Return the vgroup-modified mesh. */
398         return dm;
399 }
400
401
402 ModifierTypeInfo modifierType_WeightVGMix = {
403         /* name */              "VertexWeightMix",
404         /* structName */        "WeightVGMixModifierData",
405         /* structSize */        sizeof(WeightVGMixModifierData),
406         /* type */              eModifierTypeType_NonGeometrical,
407         /* flags */             eModifierTypeFlag_AcceptsMesh |
408                                 eModifierTypeFlag_SupportsMapping |
409                                 eModifierTypeFlag_SupportsEditmode |
410                                 eModifierTypeFlag_UsesPreview,
411
412         /* copyData */          copyData,
413         /* deformVerts */       NULL,
414         /* deformMatrices */    NULL,
415         /* deformVertsEM */     NULL,
416         /* deformMatricesEM */  NULL,
417         /* applyModifier */     applyModifier,
418         /* applyModifierEM */   NULL,
419         /* initData */          initData,
420         /* requiredDataMask */  requiredDataMask,
421         /* freeData */          NULL,
422         /* isDisabled */        isDisabled,
423         /* updateDepgraph */    updateDepgraph,
424         /* dependsOnTime */     dependsOnTime,
425         /* dependsOnNormals */  NULL,
426         /* foreachObjectLink */ foreachObjectLink,
427         /* foreachIDLink */     foreachIDLink,
428         /* foreachTexLink */    foreachTexLink,
429 };