vgroup_modifiers: Fixed last problems with WP mode, plus a small fix in weightvg_util.c.
[blender.git] / source / blender / modifiers / intern / MOD_weightvg_util.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2011 by Bastien Montagne.
21 * All rights reserved.
22 *
23 * Contributor(s): None yet.
24 *
25 * ***** END GPL LICENSE BLOCK *****
26 *
27 */
28
29 /*
30  * XXX I’d like to make modified weights visible in WeightPaint mode,
31  *     but couldn’t figure a way to do this…
32  *     Maybe this will need changes in mesh_calc_modifiers (DerivedMesh.c)?
33  *     Or the WeightPaint mode code itself?
34  */
35
36 #include "BLI_utildefines.h"
37 #include "BLI_math.h"
38 #include "BLI_string.h"
39
40 #include "DNA_mesh_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_modifier_types.h"
43 #include "DNA_object_types.h"
44
45 #include "BKE_cdderivedmesh.h"
46 #include "BKE_deform.h"
47 #include "BKE_mesh.h"
48 #include "BKE_modifier.h"
49 #include "BKE_texture.h"          /* Texture masking. */
50
51 #include "depsgraph_private.h"
52 #include "MEM_guardedalloc.h"
53 #include "MOD_util.h"
54 #include "MOD_weightvg_util.h"
55 #include "RE_shader_ext.h"        /* Texture masking. */
56
57 /* Applies new_w weights to org_w ones, using either a texture, vgroup or constant value as factor.
58  * Return values are in org_w.
59  * If indices is not NULL, it must be a table of same length as org_w and new_w, mapping to the real
60  * vertex index (in case the weight tables do not cover the whole vertices...).
61  * XXX The standard “factor” value is assumed in [0.0, 1.0] range. Else, weird results might appear.
62  */
63 void weightvg_do_mask(int num, int *indices, float *org_w, float *new_w, Object *ob,
64                       DerivedMesh *dm, float fact, const char *defgrp_name, Tex *texture,
65                       int tex_use_channel, int tex_mapping, Object *tex_map_object,
66                       const char *tex_uvlayer_name) {
67         int ref_didx;
68         MDeformVert *dvert = NULL;
69         int i;
70
71         /* If influence factor is null, nothing to do! */
72         if (fact == 0.0) return;
73
74         /* If we want to mask vgroup weights from a texture. */
75         if (texture) {
76                 /* The texture coordinates. */
77                 float (*tex_co)[3];
78                 /* See mapping note below… */
79                 MappingInfoModifierData t_map;
80                 float (*v_co)[3];
81
82                 /* Use new generic get_texture_coords, but do not modify our DNA struct for it…
83                  * XXX Why use a ModifierData stuff here ? Why not a simple, generic struct for parameters ?
84                  *     What e.g. if a modifier wants to use several textures ?
85                  *     Why use only v_co, and not MVert (or both) ?
86                  */
87                 t_map.texture = texture;
88                 t_map.map_object = tex_map_object;
89                 BLI_strncpy(t_map.uvlayer_name, tex_uvlayer_name, sizeof(t_map.uvlayer_name));
90                 t_map.texmapping = tex_mapping;
91                 v_co = MEM_mallocN(sizeof(*v_co) * num, "WeightVG Modifier, TEX mode, v_co");
92                 dm->getVertCos(dm, v_co);
93                 tex_co = MEM_callocN(sizeof(*tex_co) * num, "WeightVG Modifier, TEX mode, tex_co");
94                 get_texture_coords(&t_map, ob, dm, v_co, tex_co, num);
95                 MEM_freeN(v_co);
96
97                 /* For each weight (vertex), make the mix between org and new weights. */
98                 for(i = 0; i < num; ++i) {
99                         int idx = indices ? indices[i] : i;
100                         TexResult texres;
101                         float h, s, v; /* For HSV color space. */
102
103                         texres.nor = NULL;
104                         get_texture_value(texture, tex_co[idx], &texres);
105                         /* Get the good channel value… */
106                         switch(tex_use_channel) {
107                         case MOD_WVG_MASK_TEX_USE_INT:
108                                 org_w[i] = (new_w[i] * texres.tin * fact) + (org_w[i] * (1.0 - (texres.tin*fact)));
109                                 break;
110                         case MOD_WVG_MASK_TEX_USE_RED:
111                                 org_w[i] = (new_w[i] * texres.tr * fact) + (org_w[i] * (1.0 - (texres.tr*fact)));
112                                 break;
113                         case MOD_WVG_MASK_TEX_USE_GREEN:
114                                 org_w[i] = (new_w[i] * texres.tg * fact) + (org_w[i] * (1.0 - (texres.tg*fact)));
115                                 break;
116                         case MOD_WVG_MASK_TEX_USE_BLUE:
117                                 org_w[i] = (new_w[i] * texres.tb * fact) + (org_w[i] * (1.0 - (texres.tb*fact)));
118                                 break;
119                         case MOD_WVG_MASK_TEX_USE_HUE:
120                                 rgb_to_hsv(texres.tr, texres.tg, texres.tb, &h, &s, &v);
121                                 org_w[i] = (new_w[i] * h * fact) + (org_w[i] * (1.0 - (h*fact)));
122                                 break;
123                         case MOD_WVG_MASK_TEX_USE_SAT:
124                                 rgb_to_hsv(texres.tr, texres.tg, texres.tb, &h, &s, &v);
125                                 org_w[i] = (new_w[i] * s * fact) + (org_w[i] * (1.0 - (s*fact)));
126                                 break;
127                         case MOD_WVG_MASK_TEX_USE_VAL:
128                                 rgb_to_hsv(texres.tr, texres.tg, texres.tb, &h, &s, &v);
129                                 org_w[i] = (new_w[i] * v * fact) + (org_w[i] * (1.0 - (v*fact)));
130                                 break;
131                         case MOD_WVG_MASK_TEX_USE_ALPHA:
132                                 org_w[i] = (new_w[i] * texres.ta * fact) + (org_w[i] * (1.0 - (texres.ta*fact)));
133                                 break;
134                         default:
135                                 org_w[i] = (new_w[i] * texres.tin * fact) + (org_w[i] * (1.0 - (texres.tin*fact)));
136                                 break;
137                         }
138                 }
139
140                 MEM_freeN(tex_co);
141                 return;
142         }
143
144         /* Check whether we want to set vgroup weights from a constant weight factor or a vertex
145          * group.
146          */
147         /* Get vgroup idx from its name. */
148         ref_didx = defgroup_name_index(ob, defgrp_name);
149         /* Proceed only if vgroup is valid, else use constant factor. */
150         if (ref_didx >= 0) {
151                 /* Get actual dverts (ie vertex group data). */
152                 dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
153                 /* Proceed only if vgroup is valid, else assume factor = O. */
154                 if (dvert == NULL) return;
155
156                 /* For each weight (vertex), make the mix between org and new weights. */
157                 for (i = 0; i < num; i++) {
158                         int idx = indices ? indices[i] : i;
159                         int j;
160                         for (j = 0; j < dvert[idx].totweight; j++) {
161                                 if(dvert[idx].dw[j].def_nr == ref_didx) {
162                                         float f = dvert[idx].dw[j].weight * fact;
163                                         org_w[i] = (new_w[i] * f) + (org_w[i] * (1.0-f));
164                                         break;
165                                 }
166                         }
167                         /* If that vertex is not in ref vgroup, assume null factor, and hence do nothing! */
168                 }
169                 return;
170         }
171
172         /* Default "influence" behavior. */
173         /* For each weight (vertex), make the mix between org and new weights. */
174         for (i = 0; i < num; i++) {
175                 org_w[i] = (new_w[i] * fact) + (org_w[i] * (1.0-fact));
176         }
177 }
178
179 /* Applies weights to given vgroup (defgroup), and optionnaly add/remove vertices from the group.
180  * If indices is not NULL, it must be a table of same length as weights, mapping to the real
181  * vertex index (in case the weight table does not cover the whole vertices...).
182  */
183 void weightvg_update_vg(MDeformVert *dvert, int defgrp_idx, int num, int *indices,
184                         float *weights, int do_add, float add_thresh, int do_rem,
185                         float rem_thresh){
186         int i;
187
188         for (i = 0; i < num; i++) {
189                 int j;
190                 char add2vg = do_add;
191                 float w = weights[i];
192                 MDeformVert *dv = &dvert[indices ? indices[i] : i];
193                 MDeformWeight *newdw;
194
195                 /* Let’s first check to see if this vert is already in the weight group – if so
196                  * let’s update it, or remove it if needed.
197                  */
198                 for (j = 0; j < dv->totweight; j++) {
199                         /* If this weight corresponds to the deform group, update the value or,
200                          * if lower than rem_threshold, remove the vertex from the vgroup.
201                          */
202                         if (dv->dw[j].def_nr == defgrp_idx) {
203                                 /* Remove the vertex from this vgroup if needed. */
204                                 if (do_rem && w < rem_thresh) {
205                                         dv->totweight--;
206                                         /* If there are still other deform weights attached to this vert then remove
207                                          * this deform weight, and reshuffle the others.
208                                          */
209                                         if(dv->totweight) {
210                                                 newdw = MEM_mallocN(sizeof(MDeformWeight)*(dv->totweight), "deformWeight");
211                                                 if(dv->dw){
212                                                         memcpy(newdw, dv->dw, sizeof(MDeformWeight)*j);
213                                                         memcpy(newdw+j, dv->dw+j+1, sizeof(MDeformWeight)*(dv->totweight-j));
214                                                         MEM_freeN(dv->dw);
215                                                 }
216                                                 dv->dw = newdw;
217                                         }
218                                         /* If there are no other deform weights left then just remove this one. */
219                                         else {
220                                                 MEM_freeN(dv->dw);
221                                                 dv->dw = NULL;
222                                         }
223                                 }
224                                 /* Else, just set the new computed weight. */
225                                 else {
226                                         dv->dw[j].weight = w;
227                                 }
228                                 add2vg = 0;
229                                 break;
230                         }
231                 }
232
233                 /* If the vert wasn’t in the deform group, add it if needed!
234                  */
235                 if (add2vg && w > add_thresh) {
236                         newdw = MEM_callocN(sizeof(MDeformWeight)*(dv->totweight+1), "WeightVGEdit Modifier, deformWeight");
237                         if(dv->dw) {
238                                 memcpy(newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
239                                 MEM_freeN(dv->dw);
240                         }
241                         dv->dw = newdw;
242                         dv->dw[dv->totweight].weight = w;
243                         dv->dw[dv->totweight].def_nr = defgrp_idx;
244                         dv->totweight++;
245                 }
246         }
247 }
248