fix for own error in recent paint refactor, subtract mode was broken.
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.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) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/sculpt_paint/paint_vertex.c
29  *  \ingroup edsculpt
30  */
31
32
33 #include <math.h>
34 #include <string.h>
35
36 #ifdef WIN32
37 #include <io.h>
38 #else
39 #include <unistd.h>
40 #endif   
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_utildefines.h"
47 #include "BLI_ghash.h"
48
49 #include "IMB_imbuf.h"
50 #include "IMB_imbuf_types.h"
51
52 #include "DNA_armature_types.h"
53 #include "DNA_mesh_types.h"
54 #include "DNA_particle_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_brush_types.h"
57 #include "DNA_object_types.h"
58 #include "DNA_meshdata_types.h"
59
60 #include "RNA_access.h"
61 #include "RNA_define.h"
62 #include "RNA_enum_types.h"
63
64 #include "BKE_DerivedMesh.h"
65 #include "BKE_armature.h"
66 #include "BKE_action.h"
67 #include "BKE_brush.h"
68 #include "BKE_context.h"
69 #include "BKE_depsgraph.h"
70 #include "BKE_deform.h"
71 #include "BKE_mesh.h"
72 #include "BKE_modifier.h"
73 #include "BKE_object.h"
74 #include "BKE_paint.h"
75 #include "BKE_report.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80
81 #include "ED_armature.h"
82 #include "ED_mesh.h"
83 #include "ED_screen.h"
84 #include "ED_view3d.h"
85
86 #include "paint_intern.h"
87
88 /* polling - retrieve whether cursor should be set or operator should be done */
89
90
91 /* Returns true if vertex paint mode is active */
92 int vertex_paint_mode_poll(bContext *C)
93 {
94         Object *ob = CTX_data_active_object(C);
95
96         return ob && ob->mode == OB_MODE_VERTEX_PAINT && ((Mesh *)ob->data)->totface;
97 }
98
99 int vertex_paint_poll(bContext *C)
100 {
101         if(vertex_paint_mode_poll(C) && 
102            paint_brush(&CTX_data_tool_settings(C)->vpaint->paint)) {
103                 ScrArea *sa= CTX_wm_area(C);
104                 if(sa->spacetype==SPACE_VIEW3D) {
105                         ARegion *ar= CTX_wm_region(C);
106                         if(ar->regiontype==RGN_TYPE_WINDOW)
107                                 return 1;
108                         }
109                 }
110         return 0;
111 }
112
113 int weight_paint_mode_poll(bContext *C)
114 {
115         Object *ob = CTX_data_active_object(C);
116
117         return ob && ob->mode == OB_MODE_WEIGHT_PAINT && ((Mesh *)ob->data)->totface;
118 }
119
120 int weight_paint_poll(bContext *C)
121 {
122         Object *ob= CTX_data_active_object(C);
123         ScrArea *sa;
124
125         if(     (ob != NULL) &&
126                 (ob->mode & OB_MODE_WEIGHT_PAINT) &&
127                 (paint_brush(&CTX_data_tool_settings(C)->wpaint->paint) != NULL) &&
128                 (sa= CTX_wm_area(C)) &&
129                 (sa->spacetype == SPACE_VIEW3D)
130         ) {
131                 ARegion *ar= CTX_wm_region(C);
132                 if(ar->regiontype==RGN_TYPE_WINDOW) {
133                         return 1;
134                 }
135         }
136         return 0;
137 }
138
139 static VPaint *new_vpaint(int wpaint)
140 {
141         VPaint *vp= MEM_callocN(sizeof(VPaint), "VPaint");
142         
143         vp->flag= VP_AREA+VP_SPRAY;
144         
145         if(wpaint)
146                 vp->flag= VP_AREA;
147
148         return vp;
149 }
150
151 static int *get_indexarray(Mesh *me)
152 {
153         return MEM_mallocN(sizeof(int)*(me->totface+1), "vertexpaint");
154 }
155
156
157 /* in contradiction to cpack drawing colors, the MCOL colors (vpaint colors) are per byte! 
158    so not endian sensitive. Mcol = ABGR!!! so be cautious with cpack calls */
159
160 static unsigned int rgba_to_mcol(float r, float g, float b, float a)
161 {
162         int ir, ig, ib, ia;
163         unsigned int col;
164         char *cp;
165         
166         ir= floor(255.0f * r);
167         if(ir<0) ir= 0; else if(ir>255) ir= 255;
168         ig= floor(255.0f * g);
169         if(ig<0) ig= 0; else if(ig>255) ig= 255;
170         ib= floor(255.0f * b);
171         if(ib<0) ib= 0; else if(ib>255) ib= 255;
172         ia= floor(255.0f * a);
173         if(ia<0) ia= 0; else if(ia>255) ia= 255;
174         
175         cp= (char *)&col;
176         cp[0]= ia;
177         cp[1]= ib;
178         cp[2]= ig;
179         cp[3]= ir;
180         
181         return col;
182         
183 }
184
185 unsigned int vpaint_get_current_col(VPaint *vp)
186 {
187         Brush *brush = paint_brush(&vp->paint);
188         return rgba_to_mcol(brush->rgb[0], brush->rgb[1], brush->rgb[2], 1.0f);
189 }
190
191 static void do_shared_vertexcol(Mesh *me)
192 {
193         /* if no mcol: do not do */
194         /* if tface: only the involved faces, otherwise all */
195         MFace *mface;
196         MTFace *tface;
197         int a;
198         short *scolmain, *scol;
199         char *mcol;
200         
201         if(me->mcol==NULL || me->totvert==0 || me->totface==0) return;
202         
203         scolmain= MEM_callocN(4*sizeof(short)*me->totvert, "colmain");
204         
205         tface= me->mtface;
206         mface= me->mface;
207         mcol= (char *)me->mcol;
208         for(a=me->totface; a>0; a--, mface++, mcol+=16) {
209                 if((tface && tface->mode & TF_SHAREDCOL) || (me->editflag & ME_EDIT_PAINT_MASK)==0) {
210                         scol= scolmain+4*mface->v1;
211                         scol[0]++; scol[1]+= mcol[1]; scol[2]+= mcol[2]; scol[3]+= mcol[3];
212                         scol= scolmain+4*mface->v2;
213                         scol[0]++; scol[1]+= mcol[5]; scol[2]+= mcol[6]; scol[3]+= mcol[7];
214                         scol= scolmain+4*mface->v3;
215                         scol[0]++; scol[1]+= mcol[9]; scol[2]+= mcol[10]; scol[3]+= mcol[11];
216                         if(mface->v4) {
217                                 scol= scolmain+4*mface->v4;
218                                 scol[0]++; scol[1]+= mcol[13]; scol[2]+= mcol[14]; scol[3]+= mcol[15];
219                         }
220                 }
221                 if(tface) tface++;
222         }
223         
224         a= me->totvert;
225         scol= scolmain;
226         while(a--) {
227                 if(scol[0]>1) {
228                         scol[1]/= scol[0];
229                         scol[2]/= scol[0];
230                         scol[3]/= scol[0];
231                 }
232                 scol+= 4;
233         }
234         
235         tface= me->mtface;
236         mface= me->mface;
237         mcol= (char *)me->mcol;
238         for(a=me->totface; a>0; a--, mface++, mcol+=16) {
239                 if((tface && tface->mode & TF_SHAREDCOL) || (me->editflag & ME_EDIT_PAINT_MASK)==0) {
240                         scol= scolmain+4*mface->v1;
241                         mcol[1]= scol[1]; mcol[2]= scol[2]; mcol[3]= scol[3];
242                         scol= scolmain+4*mface->v2;
243                         mcol[5]= scol[1]; mcol[6]= scol[2]; mcol[7]= scol[3];
244                         scol= scolmain+4*mface->v3;
245                         mcol[9]= scol[1]; mcol[10]= scol[2]; mcol[11]= scol[3];
246                         if(mface->v4) {
247                                 scol= scolmain+4*mface->v4;
248                                 mcol[13]= scol[1]; mcol[14]= scol[2]; mcol[15]= scol[3];
249                         }
250                 }
251                 if(tface) tface++;
252         }
253
254         MEM_freeN(scolmain);
255 }
256
257 static void make_vertexcol(Object *ob)  /* single ob */
258 {
259         Mesh *me;
260         if(!ob || ob->id.lib) return;
261         me= get_mesh(ob);
262         if(me==NULL) return;
263         if(me->edit_mesh) return;
264
265         /* copies from shadedisplist to mcol */
266         if(!me->mcol) {
267                 CustomData_add_layer(&me->fdata, CD_MCOL, CD_CALLOC, NULL, me->totface);
268                 mesh_update_customdata_pointers(me);
269         }
270
271         //if(shade)
272         //      shadeMeshMCol(scene, ob, me);
273         //else
274
275         memset(me->mcol, 255, 4*sizeof(MCol)*me->totface);
276         
277         DAG_id_tag_update(&me->id, 0);
278         
279 }
280
281 /* mirror_vgroup is set to -1 when invalid */
282 static int wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active)
283 {
284         bDeformGroup *defgroup= BLI_findlink(&ob->defbase, vgroup_active);
285
286         if(defgroup) {
287                 bDeformGroup *curdef;
288                 int mirrdef;
289                 char name[MAXBONENAME];
290
291                 flip_side_name(name, defgroup->name, FALSE);
292
293                 if(strcmp(name, defgroup->name) != 0) {
294                         for (curdef= ob->defbase.first, mirrdef= 0; curdef; curdef=curdef->next, mirrdef++) {
295                                 if (!strcmp(curdef->name, name)) {
296                                         break;
297                                 }
298                         }
299
300                         if(curdef==NULL) {
301                                 int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
302                                 curdef= ED_vgroup_add_name(ob, name);
303                                 ob->actdef= olddef;
304                         }
305
306                         /* curdef should never be NULL unless this is
307                          * a  lamp and ED_vgroup_add_name fails */
308                         if(curdef) {
309                                 return mirrdef;
310                         }
311                 }
312         }
313
314         return -1;
315 }
316
317 static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
318 {
319         if(vp->vpaint_prev) {
320                 MEM_freeN(vp->vpaint_prev);
321                 vp->vpaint_prev= NULL;
322         }
323         vp->tot= tot;   
324         
325         if(mcol==NULL || tot==0) return;
326         
327         vp->vpaint_prev= MEM_mallocN(4*sizeof(int)*tot, "vpaint_prev");
328         memcpy(vp->vpaint_prev, mcol, 4*sizeof(int)*tot);
329         
330 }
331
332 static void copy_wpaint_prev (VPaint *wp, MDeformVert *dverts, int dcount)
333 {
334         if (wp->wpaint_prev) {
335                 free_dverts(wp->wpaint_prev, wp->tot);
336                 wp->wpaint_prev= NULL;
337         }
338         
339         if(dverts && dcount) {
340                 
341                 wp->wpaint_prev = MEM_mallocN (sizeof(MDeformVert)*dcount, "wpaint prev");
342                 wp->tot = dcount;
343                 copy_dverts (wp->wpaint_prev, dverts, dcount);
344         }
345 }
346
347
348 void vpaint_fill(Object *ob, unsigned int paintcol)
349 {
350         Mesh *me;
351         MFace *mf;
352         unsigned int *mcol;
353         int i, selected;
354
355         me= get_mesh(ob);
356         if(me==NULL || me->totface==0) return;
357
358         if(!me->mcol) make_vertexcol(ob);
359         if(!me->mcol) return; /* possible we can't make mcol's */
360
361
362         selected= (me->editflag & ME_EDIT_PAINT_MASK);
363
364         mf = me->mface;
365         mcol = (unsigned int*)me->mcol;
366         for (i = 0; i < me->totface; i++, mf++, mcol+=4) {
367                 if (!selected || mf->flag & ME_FACE_SEL) {
368                         mcol[0] = paintcol;
369                         mcol[1] = paintcol;
370                         mcol[2] = paintcol;
371                         mcol[3] = paintcol;
372                 }
373         }
374         
375         DAG_id_tag_update(&me->id, 0);
376 }
377
378
379 /* fills in the selected faces with the current weight and vertex group */
380 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
381 {
382         Mesh *me= ob->data;
383         MFace *mf;
384         MDeformWeight *dw, *dw_prev;
385         int vgroup_active, vgroup_mirror= -1;
386         unsigned int index;
387
388         /* mutually exclusive, could be made into a */
389         const short paint_selmode= ME_EDIT_PAINT_SEL_MODE(me);
390
391         if(me->totface==0 || me->dvert==NULL || !me->mface) return;
392         
393         vgroup_active = ob->actdef - 1;
394
395         /* if mirror painting, find the other group */
396         if(me->editflag & ME_EDIT_MIRROR_X) {
397                 vgroup_mirror= wpaint_mirror_vgroup_ensure(ob, vgroup_active);
398         }
399         
400         copy_wpaint_prev(wp, me->dvert, me->totvert);
401         
402         for(index=0, mf= me->mface; index < me->totface; index++, mf++) {
403                 unsigned int fidx= mf->v4 ? 3:2;
404
405                 if ((paint_selmode == SCE_SELECT_FACE) && !(mf->flag & ME_FACE_SEL)) {
406                         continue;
407                 }
408
409                 do {
410                         unsigned int vidx= *(&mf->v1 + fidx);
411
412                         if(!me->dvert[vidx].flag) {
413                                 if((paint_selmode == SCE_SELECT_VERTEX) && !(me->mvert[vidx].flag & SELECT)) {
414                                         continue;
415                                 }
416
417                                 dw= defvert_verify_index(&me->dvert[vidx], vgroup_active);
418                                 if(dw) {
419                                         dw_prev= defvert_verify_index(wp->wpaint_prev+vidx, vgroup_active);
420                                         dw_prev->weight= dw->weight; /* set the undo weight */
421                                         dw->weight= paintweight;
422
423                                         if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
424                                                 int j= mesh_get_x_mirror_vert(ob, vidx);
425                                                 if(j>=0) {
426                                                         /* copy, not paint again */
427                                                         if(vgroup_mirror != -1) {
428                                                                 dw= defvert_verify_index(me->dvert+j, vgroup_mirror);
429                                                                 dw_prev= defvert_verify_index(wp->wpaint_prev+j, vgroup_mirror);
430                                                         } else {
431                                                                 dw= defvert_verify_index(me->dvert+j, vgroup_active);
432                                                                 dw_prev= defvert_verify_index(wp->wpaint_prev+j, vgroup_active);
433                                                         }
434                                                         dw_prev->weight= dw->weight; /* set the undo weight */
435                                                         dw->weight= paintweight;
436                                                 }
437                                         }
438                                 }
439                                 me->dvert[vidx].flag= 1;
440                         }
441
442                 } while (fidx--);
443         }
444
445         {
446                 MDeformVert *dv= me->dvert;
447                 for(index= me->totvert; index != 0; index--, dv++) {
448                         dv->flag= 0;
449                 }
450         }
451
452         copy_wpaint_prev(wp, NULL, 0);
453
454         DAG_id_tag_update(&me->id, 0);
455 }
456
457 /* XXX: should be re-implemented as a vertex/weight paint 'color correct' operator
458  
459 void vpaint_dogamma(Scene *scene)
460 {
461         VPaint *vp= scene->toolsettings->vpaint;
462         Mesh *me;
463         Object *ob;
464         float igam, fac;
465         int a, temp;
466         unsigned char *cp, gamtab[256];
467
468         ob= OBACT;
469         me= get_mesh(ob);
470
471         if(!(ob->mode & OB_MODE_VERTEX_PAINT)) return;
472         if(me==0 || me->mcol==0 || me->totface==0) return;
473
474         igam= 1.0/vp->gamma;
475         for(a=0; a<256; a++) {
476                 
477                 fac= ((float)a)/255.0;
478                 fac= vp->mul*pow( fac, igam);
479                 
480                 temp= 255.9*fac;
481                 
482                 if(temp<=0) gamtab[a]= 0;
483                 else if(temp>=255) gamtab[a]= 255;
484                 else gamtab[a]= temp;
485         }
486
487         a= 4*me->totface;
488         cp= (unsigned char *)me->mcol;
489         while(a--) {
490                 
491                 cp[1]= gamtab[ cp[1] ];
492                 cp[2]= gamtab[ cp[2] ];
493                 cp[3]= gamtab[ cp[3] ];
494                 
495                 cp+= 4;
496         }
497 }
498  */
499
500 BM_INLINE unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
501 {
502         char *cp1, *cp2, *cp;
503         int mfac;
504         unsigned int col=0;
505         
506         if(fac==0) return col1;
507         if(fac>=255) return col2;
508
509         mfac= 255-fac;
510         
511         cp1= (char *)&col1;
512         cp2= (char *)&col2;
513         cp=  (char *)&col;
514         
515         cp[0]= 255;
516         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
517         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
518         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
519         
520         return col;
521 }
522
523 BM_INLINE unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
524 {
525         char *cp1, *cp2, *cp;
526         int temp;
527         unsigned int col=0;
528         
529         if(fac==0) return col1;
530         
531         cp1= (char *)&col1;
532         cp2= (char *)&col2;
533         cp=  (char *)&col;
534         
535         cp[0]= 255;
536         temp= cp1[1] + ((fac*cp2[1])/255);
537         if(temp>254) cp[1]= 255; else cp[1]= temp;
538         temp= cp1[2] + ((fac*cp2[2])/255);
539         if(temp>254) cp[2]= 255; else cp[2]= temp;
540         temp= cp1[3] + ((fac*cp2[3])/255);
541         if(temp>254) cp[3]= 255; else cp[3]= temp;
542         
543         return col;
544 }
545
546 BM_INLINE unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
547 {
548         char *cp1, *cp2, *cp;
549         int temp;
550         unsigned int col=0;
551         
552         if(fac==0) return col1;
553         
554         cp1= (char *)&col1;
555         cp2= (char *)&col2;
556         cp=  (char *)&col;
557         
558         cp[0]= 255;
559         temp= cp1[1] - ((fac*cp2[1])/255);
560         if(temp<0) cp[1]= 0; else cp[1]= temp;
561         temp= cp1[2] - ((fac*cp2[2])/255);
562         if(temp<0) cp[2]= 0; else cp[2]= temp;
563         temp= cp1[3] - ((fac*cp2[3])/255);
564         if(temp<0) cp[3]= 0; else cp[3]= temp;
565         
566         return col;
567 }
568
569 BM_INLINE unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac)
570 {
571         char *cp1, *cp2, *cp;
572         int mfac;
573         unsigned int col=0;
574         
575         if(fac==0) return col1;
576
577         mfac= 255-fac;
578         
579         cp1= (char *)&col1;
580         cp2= (char *)&col2;
581         cp=  (char *)&col;
582         
583         /* first mul, then blend the fac */
584         cp[0]= 255;
585         cp[1]= (mfac*cp1[1] + fac*((cp2[1]*cp1[1])/255)  )/255;
586         cp[2]= (mfac*cp1[2] + fac*((cp2[2]*cp1[2])/255)  )/255;
587         cp[3]= (mfac*cp1[3] + fac*((cp2[3]*cp1[3])/255)  )/255;
588
589         
590         return col;
591 }
592
593 BM_INLINE unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
594 {
595         char *cp1, *cp2, *cp;
596         int mfac;
597         unsigned int col=0;
598         
599         if(fac==0) return col1;
600         if(fac>=255) return col2;
601
602         mfac= 255-fac;
603         
604         cp1= (char *)&col1;
605         cp2= (char *)&col2;
606         cp=  (char *)&col;
607         
608         /* See if are lighter, if so mix, else dont do anything.
609         if the paint col is darker then the original, then ignore */
610         if (cp1[1]+cp1[2]+cp1[3] > cp2[1]+cp2[2]+cp2[3])
611                 return col1;
612         
613         cp[0]= 255;
614         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
615         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
616         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
617         
618         return col;
619 }
620
621 BM_INLINE unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
622 {
623         char *cp1, *cp2, *cp;
624         int mfac;
625         unsigned int col=0;
626         
627         if(fac==0) return col1;
628         if(fac>=255) return col2;
629
630         mfac= 255-fac;
631         
632         cp1= (char *)&col1;
633         cp2= (char *)&col2;
634         cp=  (char *)&col;
635         
636         /* See if were darker, if so mix, else dont do anything.
637         if the paint col is brighter then the original, then ignore */
638         if (cp1[1]+cp1[2]+cp1[3] < cp2[1]+cp2[2]+cp2[3])
639                 return col1;
640         
641         cp[0]= 255;
642         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
643         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
644         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
645         return col;
646 }
647
648 /* wpaint has 'wpaint_blend_tool' */
649 static unsigned int vpaint_blend_tool(const int tool, const unsigned int col,
650                                       const unsigned int paintcol, const int alpha_i)
651 {
652         switch (tool) {
653                 case PAINT_BLEND_MIX:
654                 case PAINT_BLEND_BLUR:     return mcol_blend(col, paintcol, alpha_i);
655                 case PAINT_BLEND_ADD:      return mcol_add(col, paintcol, alpha_i);
656                 case PAINT_BLEND_SUB:      return mcol_sub(col, paintcol, alpha_i);
657                 case PAINT_BLEND_MUL:      return mcol_mul(col, paintcol, alpha_i);
658                 case PAINT_BLEND_LIGHTEN:  return mcol_lighten(col, paintcol, alpha_i);
659                 case PAINT_BLEND_DARKEN:   return mcol_darken(col, paintcol, alpha_i);
660                 default:
661                         BLI_assert(0);
662                         return 0;
663         }
664 }
665
666 /* wpaint has 'wpaint_blend' */
667 static unsigned int vpaint_blend(VPaint *vp, unsigned int col, unsigned int colorig, const
668                                  unsigned int paintcol, const int alpha_i,
669                                  /* pre scaled from [0-1] --> [0-255] */
670                                  const int brush_alpha_value_i)
671 {
672         Brush *brush = paint_brush(&vp->paint);
673         const int tool = brush->vertexpaint_tool;
674
675         col = vpaint_blend_tool(tool, col, paintcol, alpha_i);
676
677         /* if no spray, clip color adding with colorig & orig alpha */
678         if((vp->flag & VP_SPRAY)==0) {
679                 unsigned int testcol, a;
680                 char *cp, *ct, *co;
681                 
682                 testcol = vpaint_blend_tool(tool, colorig, paintcol, brush_alpha_value_i);
683                 
684                 cp= (char *)&col;
685                 ct= (char *)&testcol;
686                 co= (char *)&colorig;
687                 
688                 for(a=0; a<4; a++) {
689                         if( ct[a]<co[a] ) {
690                                 if( cp[a]<ct[a] ) cp[a]= ct[a];
691                                 else if( cp[a]>co[a] ) cp[a]= co[a];
692                         }
693                         else {
694                                 if( cp[a]<co[a] ) cp[a]= co[a];
695                                 else if( cp[a]>ct[a] ) cp[a]= ct[a];
696                         }
697                 }
698         }
699
700         return col;
701 }
702
703
704 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
705 {
706         struct ImBuf *ibuf;
707         int a, tot=0, index;
708         
709         /* brecht: disabled this because it obviously failes for
710            brushes with size > 64, why is this here? */
711         /*if(size>64.0) size= 64.0;*/
712         
713         ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
714         if(ibuf) {
715                 unsigned int *rt= ibuf->rect;
716
717                 memset(indexar, 0, sizeof(int)*(totface+1));
718                 
719                 size= ibuf->x*ibuf->y;
720                 while(size--) {
721                                 
722                         if(*rt) {
723                                 index= WM_framebuffer_to_index(*rt);
724                                 if(index>0 && index<=totface)
725                                         indexar[index] = 1;
726                         }
727                 
728                         rt++;
729                 }
730                 
731                 for(a=1; a<=totface; a++) {
732                         if(indexar[a]) indexar[tot++]= a;
733                 }
734
735                 IMB_freeImBuf(ibuf);
736         }
737         
738         return tot;
739 }
740
741 /* whats _dl mean? */
742 static float calc_vp_strength_dl(VPaint *vp, ViewContext *vc, const float *vert_nor,
743                               const float mval[2], const float brush_size_pressure)
744 {
745         Brush *brush = paint_brush(&vp->paint);
746         float dist_squared;
747         float vertco[2], delta[2];
748
749         project_float_noclip(vc->ar, vert_nor, vertco);
750         sub_v2_v2v2(delta, mval, vertco);
751         dist_squared= dot_v2v2(delta, delta); /* len squared */
752         if (dist_squared > brush_size_pressure * brush_size_pressure) {
753                 return 0.0f;
754         }
755         else {
756                 const float dist = sqrtf(dist_squared);
757                 return brush_curve_strength_clamp(brush, dist, brush_size_pressure);
758         }
759 }
760
761 static float calc_vp_alpha_dl(VPaint *vp, ViewContext *vc,
762                               float vpimat[][3], const float *vert_nor,
763                               const float mval[2],
764                               const float brush_size_pressure, const float brush_alpha_pressure)
765 {
766         float strength = calc_vp_strength_dl(vp, vc, vert_nor, mval, brush_size_pressure);
767
768         if (strength > 0.0f) {
769                 float alpha= brush_alpha_pressure * strength;
770
771                 if(vp->flag & VP_NORMALS) {
772                         float dvec[3];
773                         const float *no= vert_nor + 3;
774
775                         /* transpose ! */
776                         dvec[2] = dot_v3v3(vpimat[2], no);
777                         if (dvec[2] > 0.0f) {
778                                 dvec[0] = dot_v3v3(vpimat[0], no);
779                                 dvec[1] = dot_v3v3(vpimat[1], no);
780
781                                 alpha *= dvec[2] / len_v3(dvec);
782                         }
783                         else {
784                                 return 0.0f;
785                         }
786                 }
787
788                 return alpha;
789         }
790
791         return 0.0f;
792 }
793
794
795 BM_INLINE float wval_blend(const float weight, const float paintval, const float alpha)
796 {
797         return (paintval * alpha) + (weight * (1.0f - alpha));
798 }
799 BM_INLINE float wval_add(const float weight, const float paintval, const float alpha)
800 {
801         return weight + (paintval * alpha);
802 }
803 BM_INLINE float wval_sub(const float weight, const float paintval, const float alpha)
804 {
805         return weight - (paintval * alpha);
806 }
807 BM_INLINE float wval_mul(const float weight, const float paintval, const float alpha)
808 {       /* first mul, then blend the fac */
809         return ((1.0f - alpha) + (alpha * paintval)) * weight;
810 }
811 BM_INLINE float wval_lighten(const float weight, const float paintval, const float alpha)
812 {
813         return (weight < paintval) ? wval_blend(weight, paintval, alpha) : weight;
814 }
815 BM_INLINE float wval_darken(const float weight, const float paintval, const float alpha)
816 {
817         return (weight > paintval) ? wval_blend(weight, paintval, alpha) : weight;
818 }
819
820
821 /* vpaint has 'vpaint_blend_tool' */
822 /* result is not clamped from [0-1] */
823 static float wpaint_blend_tool(const int tool,
824                                /* dw->weight */
825                                const float weight,
826                                const float paintval, const float alpha)
827 {
828         switch (tool) {
829                 case PAINT_BLEND_MIX:
830                 case PAINT_BLEND_BLUR:     return wval_blend(weight, paintval, alpha);
831                 case PAINT_BLEND_ADD:      return wval_add(weight, paintval, alpha);
832                 case PAINT_BLEND_SUB:      return wval_sub(weight, paintval, alpha);
833                 case PAINT_BLEND_MUL:      return wval_mul(weight, paintval, alpha);
834                 case PAINT_BLEND_LIGHTEN:  return wval_lighten(weight, paintval, alpha);
835                 case PAINT_BLEND_DARKEN:   return wval_darken(weight, paintval, alpha);
836                 default:
837                         BLI_assert(0);
838                         return 0.0f;
839         }
840 }
841
842 /* vpaint has 'vpaint_blend' */
843 static float wpaint_blend(VPaint *wp, float weight, float weight_prev,
844                           const float alpha, float paintval,
845                           const float brush_alpha_value,
846                           const short do_flip, const short do_multipaint_totsel)
847 {
848         Brush *brush = paint_brush(&wp->paint);
849         int tool = brush->vertexpaint_tool;
850
851         if (do_flip) {
852                 switch(tool) {
853                         case PAINT_BLEND_MIX:
854                                 paintval = 1.f - paintval; break;
855                         case PAINT_BLEND_ADD:
856                                 tool= PAINT_BLEND_SUB; break;
857                         case PAINT_BLEND_SUB:
858                                 tool= PAINT_BLEND_ADD; break;
859                         case PAINT_BLEND_LIGHTEN:
860                                 tool= PAINT_BLEND_DARKEN; break;
861                         case PAINT_BLEND_DARKEN:
862                                 tool= PAINT_BLEND_LIGHTEN; break;
863                 }
864         }
865         
866         weight = wpaint_blend_tool(tool, weight, paintval, alpha);
867
868         /* delay clamping until the end so multi-paint can function when the active group is at the limits */
869         if(do_multipaint_totsel == FALSE) {
870                 CLAMP(weight, 0.0f, 1.0f);
871         }
872         
873         /* if no spray, clip result with orig weight & orig alpha */
874         if ((wp->flag & VP_SPRAY) == 0) {
875                 if(do_multipaint_totsel == FALSE) {
876                         float testw = wpaint_blend_tool(tool, weight_prev, paintval, brush_alpha_value);
877
878                         CLAMP(testw, 0.0f, 1.0f);
879                         if (testw < weight_prev) {
880                                 if(weight < testw) weight = testw;
881                                 else if(weight > weight_prev) weight = weight_prev;
882                         }
883                         else {
884                                 if (weight > testw) weight = testw;
885                                 else if (weight < weight_prev) weight = weight_prev;
886                         }
887                 }
888         }
889
890         return weight;
891 }
892
893 /* ----------------------------------------------------- */
894
895
896 /* sets wp->weight to the closest weight value to vertex */
897 /* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
898 static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
899 {
900         ViewContext vc;
901         Mesh *me;
902         short change= FALSE;
903
904         view3d_set_viewcontext(C, &vc);
905         me= get_mesh(vc.obact);
906
907         if (me && me->dvert && vc.v3d && vc.rv3d) {
908                 int index;
909
910                 view3d_operator_needs_opengl(C);
911
912                 index= view3d_sample_backbuf(&vc, event->mval[0], event->mval[1]);
913
914                 if(index && index<=me->totface) {
915                         DerivedMesh *dm= mesh_get_derived_final(vc.scene, vc.obact, CD_MASK_BAREMESH);
916
917                         if(dm->getVertCo==NULL) {
918                                 BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
919                         }
920                         else {
921                                 MFace *mf= ((MFace *)me->mface) + index-1;
922                                 const int vgroup_active= vc.obact->actdef - 1;
923                                 ToolSettings *ts= vc.scene->toolsettings;
924                                 float mval_f[2];
925                                 int v_idx_best= -1;
926                                 int fidx;
927                                 float len_best= FLT_MAX;
928
929                                 mval_f[0]= (float)event->mval[0];
930                                 mval_f[1]= (float)event->mval[1];
931
932                                 fidx= mf->v4 ? 3:2;
933                                 do {
934                                         float co[3], sco[3], len;
935                                         const int v_idx= (*(&mf->v1 + fidx));
936                                         dm->getVertCo(dm, v_idx, co);
937                                         project_float_noclip(vc.ar, co, sco);
938                                         len= len_squared_v2v2(mval_f, sco);
939                                         if(len < len_best) {
940                                                 len_best= len;
941                                                 v_idx_best= v_idx;
942                                         }
943                                 } while (fidx--);
944
945                                 if(v_idx_best != -1) { /* should always be valid */
946                                         ts->vgroup_weight= defvert_find_weight(&me->dvert[v_idx_best], vgroup_active);
947                                         change= TRUE;
948                                 }
949                         }
950                         dm->release(dm);
951                 }
952         }
953
954         if(change) {
955                 /* not really correct since the brush didnt change, but redraws the toolbar */
956                 WM_main_add_notifier(NC_BRUSH|NA_EDITED, NULL); /* ts->wpaint->paint.brush */
957
958                 return OPERATOR_FINISHED;
959         }
960         else {
961                 return OPERATOR_CANCELLED;
962         }
963 }
964
965 void PAINT_OT_weight_sample(wmOperatorType *ot)
966 {
967         /* identifiers */
968         ot->name= "Weight Paint Sample Weight";
969         ot->idname= "PAINT_OT_weight_sample";
970
971         /* api callbacks */
972         ot->invoke= weight_sample_invoke;
973         ot->poll= weight_paint_mode_poll;
974
975         /* flags */
976         ot->flag= OPTYPE_UNDO;
977 }
978
979 /* samples cursor location, and gives menu with vertex groups to activate */
980 static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
981 {
982         if (C) {
983                 wmWindow *win= CTX_wm_window(C);
984                 if(win && win->eventstate) {
985                         ViewContext vc;
986                         Mesh *me;
987
988                         view3d_set_viewcontext(C, &vc);
989                         me= get_mesh(vc.obact);
990
991                         if (me && me->dvert && vc.v3d && vc.rv3d) {
992                                 int index;
993
994                                 view3d_operator_needs_opengl(C);
995
996                                 index= view3d_sample_backbuf(&vc, win->eventstate->x - vc.ar->winrct.xmin, win->eventstate->y - vc.ar->winrct.ymin);
997
998                                 if(index && index<=me->totface) {
999                                         const int defbase_tot= BLI_countlist(&vc.obact->defbase);
1000                                         if(defbase_tot) {
1001                                                 MFace *mf= ((MFace *)me->mface) + index-1;
1002                                                 unsigned int fidx= mf->v4 ? 3:2;
1003                                                 int *groups= MEM_callocN(defbase_tot*sizeof(int), "groups");
1004                                                 int found= FALSE;
1005
1006                                                 do {
1007                                                         MDeformVert *dvert= me->dvert + (*(&mf->v1 + fidx));
1008                                                         int i= dvert->totweight;
1009                                                         MDeformWeight *dw;
1010                                                         for(dw= dvert->dw; i > 0; dw++, i--) {
1011                                                                 if (dw->def_nr < defbase_tot) {
1012                                                                         groups[dw->def_nr]= TRUE;
1013                                                                         found= TRUE;
1014                                                                 }
1015                                                         }
1016                                                 } while (fidx--);
1017
1018                                                 if(found==FALSE) {
1019                                                         MEM_freeN(groups);
1020                                                 }
1021                                                 else {
1022                                                         EnumPropertyItem *item= NULL, item_tmp= {0};
1023                                                         int totitem= 0;
1024                                                         int i= 0;
1025                                                         bDeformGroup *dg;
1026                                                         for(dg= vc.obact->defbase.first; dg && i<defbase_tot; i++, dg= dg->next) {
1027                                                                 if(groups[i]) {
1028                                                                         item_tmp.identifier= item_tmp.name= dg->name;
1029                                                                         item_tmp.value= i;
1030                                                                         RNA_enum_item_add(&item, &totitem, &item_tmp);
1031                                                                 }
1032                                                         }
1033
1034                                                         RNA_enum_item_end(&item, &totitem);
1035                                                         *free= 1;
1036
1037                                                         MEM_freeN(groups);
1038                                                         return item;
1039                                                 }
1040                                         }
1041                                 }
1042                         }
1043                 }
1044         }
1045
1046         return DummyRNA_NULL_items;
1047 }
1048
1049 static int weight_sample_group_exec(bContext *C, wmOperator *op)
1050 {
1051         int type= RNA_enum_get(op->ptr, "group");
1052         ViewContext vc;
1053         view3d_set_viewcontext(C, &vc);
1054
1055         BLI_assert(type + 1 >= 0);
1056         vc.obact->actdef= type + 1;
1057
1058         DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA);
1059         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, vc.obact);
1060         return OPERATOR_FINISHED;
1061 }
1062
1063 /* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */
1064 void PAINT_OT_weight_sample_group(wmOperatorType *ot)
1065 {
1066         PropertyRNA *prop= NULL;
1067
1068         /* identifiers */
1069         ot->name= "Weight Paint Sample Group";
1070         ot->idname= "PAINT_OT_weight_sample_group";
1071
1072         /* api callbacks */
1073         ot->exec= weight_sample_group_exec;
1074         ot->invoke= WM_menu_invoke;
1075         ot->poll= weight_paint_mode_poll;
1076
1077         /* flags */
1078         ot->flag= OPTYPE_UNDO;
1079
1080         /* keyingset to use (dynamic enum) */
1081         prop= RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
1082         RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf);
1083         ot->prop= prop;
1084 }
1085
1086 static void do_weight_paint_normalize_all(MDeformVert *dvert, const int defbase_tot, const char *vgroup_validmap)
1087 {
1088         float sum= 0.0f, fac;
1089         unsigned int i, tot=0;
1090         MDeformWeight *dw;
1091
1092         for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1093                 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1094                         tot++;
1095                         sum += dw->weight;
1096                 }
1097         }
1098
1099         if ((tot == 0) || (sum == 1.0f)) {
1100                 return;
1101         }
1102
1103         if (sum != 0.0f) {
1104                 fac= 1.0f / sum;
1105
1106                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1107                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1108                                         dw->weight *= fac;
1109                         }
1110                 }
1111         }
1112         else {
1113                 /* hrmf, not a factor in this case */
1114                 fac = 1.0f / tot;
1115
1116                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1117                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1118                                 dw->weight = fac;
1119                         }
1120                 }
1121         }
1122 }
1123
1124 /* same as function above except it normalizes against the active vgroup which remains unchanged
1125  *
1126  * note that the active is just the group which is unchanged, it can be any,
1127  * can also be -1 to normalize all but in that case call 'do_weight_paint_normalize_all' */
1128 static void do_weight_paint_normalize_all_active(MDeformVert *dvert, const int defbase_tot, const char *vgroup_validmap,
1129                                                  const int vgroup_active)
1130 {
1131         float sum= 0.0f, fac;
1132         unsigned int i, tot=0;
1133         MDeformWeight *dw;
1134         float act_weight = 0.0f;
1135
1136         for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1137                 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1138                         if (dw->def_nr != vgroup_active) {
1139                                 sum += dw->weight;
1140                                 tot++;
1141                         }
1142                         else {
1143                                 act_weight = dw->weight;
1144                         }
1145                 }
1146         }
1147
1148         if ((tot == 0) || (sum + act_weight == 1.0f)) {
1149                 return;
1150         }
1151
1152         if (sum != 0.0f) {
1153                 fac = (1.0f / sum) * (1.0f - act_weight);
1154
1155                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1156                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1157                                 if (dw->def_nr != vgroup_active) {
1158                                         dw->weight *= fac;
1159
1160                                         /* paranoid but possibly with float error */
1161                                         CLAMP(dw->weight, 0.0f, 1.0f);
1162                                 }
1163                         }
1164                 }
1165         }
1166         else {
1167                 /* corner case where we need to scale all weights evenly because they're all zero */
1168
1169                 /* hrmf, not a factor in this case */
1170                 fac = (1.0f - act_weight) / tot;
1171
1172                 /* paranoid but possibly with float error */
1173                 CLAMP(fac, 0.0f, 1.0f);
1174
1175                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1176                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1177                                 if (dw->def_nr != vgroup_active) {
1178                                         dw->weight = fac;
1179                                 }
1180                         }
1181                 }
1182         }
1183 }
1184
1185 /*
1186 See if the current deform vertex has a locked group
1187 */
1188 static char has_locked_group(MDeformVert *dvert, const int defbase_tot,
1189                              const char *bone_groups, const char *lock_flags)
1190 {
1191         int i;
1192         MDeformWeight *dw;
1193
1194         for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1195                 if (dw->def_nr < defbase_tot) {
1196                         if (bone_groups[dw->def_nr] && lock_flags[dw->def_nr] && dw->weight > 0.0f) {
1197                                 return TRUE;
1198                         }
1199                 }
1200         }
1201         return FALSE;
1202 }
1203 /* 
1204  * gen_lck_flags gets the status of "flag" for each bDeformGroup
1205  *in ob->defbase and returns an array containing them
1206  */
1207 static char *gen_lock_flags(Object* ob, int defbase_tot)
1208 {
1209         char is_locked = FALSE;
1210         int i;
1211         //int defbase_tot = BLI_countlist(&ob->defbase);
1212         char *lock_flags = MEM_mallocN(defbase_tot*sizeof(char), "defflags");
1213         bDeformGroup *defgroup;
1214
1215         for(i = 0, defgroup = ob->defbase.first; i < defbase_tot && defgroup; defgroup = defgroup->next, i++) {
1216                 lock_flags[i] = ((defgroup->flag & DG_LOCK_WEIGHT) != 0);
1217                 is_locked |= lock_flags[i];
1218         }
1219         if (is_locked) {
1220                 return lock_flags;
1221         }
1222
1223         MEM_freeN(lock_flags);
1224         return NULL;
1225 }
1226
1227 static int has_locked_group_selected(int defbase_tot, const char *defbase_sel, const char *lock_flags)
1228 {
1229         int i;
1230         for(i = 0; i < defbase_tot; i++) {
1231                 if(defbase_sel[i] && lock_flags[i]) {
1232                         return TRUE;
1233                 }
1234         }
1235         return FALSE;
1236 }
1237
1238
1239 #if 0 /* UNUSED */
1240 static int has_unselected_unlocked_bone_group(int defbase_tot, char *defbase_sel, int selected, char *lock_flags, char *vgroup_validmap)
1241 {
1242         int i;
1243         if(defbase_tot == selected) {
1244                 return FALSE;
1245         }
1246         for(i = 0; i < defbase_tot; i++) {
1247                 if(vgroup_validmap[i] && !defbase_sel[i] && !lock_flags[i]) {
1248                         return TRUE;
1249                 }
1250         }
1251         return FALSE;
1252 }
1253 #endif
1254
1255
1256 static void multipaint_selection(MDeformVert *dvert, const int defbase_tot, float change, const char *defbase_sel)
1257 {
1258         int i;
1259         MDeformWeight *dw;
1260         float val;
1261         /* make sure they are all at most 1 after the change */
1262         for(i = 0; i < defbase_tot; i++) {
1263                 if(defbase_sel[i]) {
1264                         dw = defvert_find_index(dvert, i);
1265                         if(dw && dw->weight) {
1266                                 val = dw->weight * change;
1267                                 if(val > 1) {
1268                                         /* TODO: when the change is reduced, you need to recheck
1269                                          * the earlier values to make sure they are not 0
1270                                          * (precision error) */
1271                                         change = 1.0f/dw->weight;
1272                                 }
1273                                 /* the value should never reach zero while multi-painting if it
1274                                  * was nonzero beforehand */
1275                                 if(val <= 0) {
1276                                         return;
1277                                 }
1278                         }
1279                 }
1280         }
1281         /* apply the valid change */
1282         for(i = 0; i < defbase_tot; i++) {
1283                 if(defbase_sel[i]) {
1284                         dw = defvert_find_index(dvert, i);
1285                         if(dw && dw->weight) {
1286                                 dw->weight = dw->weight * change;
1287                         }
1288                 }
1289         }
1290 }
1291
1292 /* move all change onto valid, unchanged groups.  If there is change left over,
1293  * then return it.
1294  * assumes there are valid groups to shift weight onto */
1295 static float redistribute_change(MDeformVert *ndv, const int defbase_tot,
1296                                  char *change_status, const char change_me, int changeto,
1297                                  float totchange, float total_valid,
1298                                  char do_auto_normalize)
1299 {
1300         float was_change;
1301         float change;
1302         float oldval;
1303         MDeformWeight *ndw;
1304         int i;
1305         do {
1306                 /* assume there is no change until you see one */
1307                 was_change = FALSE;
1308                 /* change each group by the same amount each time */
1309                 change = totchange/total_valid;
1310                 for(i = 0; i < ndv->totweight && total_valid && totchange; i++) {
1311                         ndw = (ndv->dw+i);
1312
1313                         /* ignore anything outside the value range */
1314                         if (ndw->def_nr < defbase_tot) {
1315
1316                                 /* change only the groups with a valid status */
1317                                 if(change_status[ndw->def_nr] == change_me) {
1318                                         oldval = ndw->weight;
1319                                         /* if auto normalize is active, don't worry about upper bounds */
1320                                         if(do_auto_normalize == FALSE && ndw->weight + change > 1) {
1321                                                 totchange -= 1-ndw->weight;
1322                                                 ndw->weight = 1;
1323                                                 /* stop the changes to this group */
1324                                                 change_status[ndw->def_nr] = changeto;
1325                                                 total_valid--;
1326                                         }
1327                                         else if(ndw->weight + change < 0) { /* check the lower bound */
1328                                                 totchange -= ndw->weight;
1329                                                 ndw->weight = 0;
1330                                                 change_status[ndw->def_nr] = changeto;
1331                                                 total_valid--;
1332                                         }
1333                                         else {/* a perfectly valid change occurred to ndw->weight */
1334                                                 totchange -= change;
1335                                                 ndw->weight += change;
1336                                         }
1337                                         /* see if there was a change */
1338                                         if(oldval != ndw->weight) {
1339                                                 was_change = TRUE;
1340                                         }
1341                                 }
1342                         }
1343                 }
1344                 /* don't go again if there was no change, if there is no valid group,
1345                  * or there is no change left */
1346         } while(was_change && total_valid && totchange);
1347         /* left overs */
1348         return totchange;
1349 }
1350 static float get_mp_change(MDeformVert *odv, const int defbase_tot, const char *defbase_sel, float brush_change);
1351 /* observe the changes made to the weights of groups.
1352  * make sure all locked groups on the vertex have the same deformation
1353  * by moving the changes made to groups onto other unlocked groups */
1354 static void enforce_locks(MDeformVert *odv, MDeformVert *ndv,
1355                           const int defbase_tot, const char *defbase_sel,
1356                           const char *lock_flags, const char *vgroup_validmap,
1357                           char do_auto_normalize, char do_multipaint)
1358 {
1359         float totchange = 0.0f;
1360         float totchange_allowed = 0.0f;
1361         float left_over;
1362
1363         int total_valid = 0;
1364         int total_changed = 0;
1365         unsigned int i;
1366         MDeformWeight *ndw;
1367         MDeformWeight *odw;
1368
1369         float changed_sum = 0.0f;
1370
1371         char *change_status;
1372
1373         if(!lock_flags || !has_locked_group(ndv, defbase_tot, vgroup_validmap, lock_flags)) {
1374                 return;
1375         }
1376         /* record if a group was changed, unlocked and not changed, or locked */
1377         change_status = MEM_callocN(sizeof(char)*defbase_tot, "unlocked_unchanged");
1378
1379         for(i = 0; i < defbase_tot; i++) {
1380                 ndw = defvert_find_index(ndv, i);
1381                 odw = defvert_find_index(odv, i);
1382                 /* the weights are zero, so we can assume a lot */
1383                 if(!ndw || !odw) {
1384                         if (!lock_flags[i] && vgroup_validmap[i]) {
1385                                 defvert_verify_index(odv, i);
1386                                 defvert_verify_index(ndv, i);
1387                                 total_valid++;
1388                                 change_status[i] = 1; /* can be altered while redistributing */
1389                         }
1390                         continue;
1391                 }
1392                 /* locked groups should not be changed */
1393                 if(lock_flags[i]) {
1394                         ndw->weight = odw->weight;
1395                 }
1396                 else if(ndw->weight != odw->weight) { /* changed groups are handled here */
1397                         totchange += ndw->weight - odw->weight;
1398                         changed_sum += ndw->weight;
1399                         change_status[i] = 2; /* was altered already */
1400                         total_changed++;
1401                 } /* unchanged, unlocked bone groups are handled here */
1402                 else if (vgroup_validmap[i]) {
1403                         totchange_allowed += ndw->weight;
1404                         total_valid++;
1405                         change_status[i] = 1; /* can be altered while redistributing */
1406                 }
1407         }
1408         /* if there was any change, redistribute it */
1409         if(total_changed) {
1410                 /* auto normalize will allow weights to temporarily go above 1 in redistribution */
1411                 if(vgroup_validmap && total_changed < 0 && total_valid) {
1412                         totchange_allowed = total_valid;
1413                 }
1414                 /* the way you modify the unlocked+unchanged groups is different depending
1415                  * on whether or not you are painting the weight(s) up or down */
1416                 if(totchange < 0) {
1417                         totchange_allowed = total_valid - totchange_allowed;
1418                 }
1419                 else {
1420                         totchange_allowed *= -1;
1421                 }
1422                 /* there needs to be change allowed, or you should not bother */
1423                 if(totchange_allowed) {
1424                         left_over = 0;
1425                         if(fabsf(totchange_allowed) < fabsf(totchange)) {
1426                                 /* this amount goes back onto the changed, unlocked weights */
1427                                 left_over = fabsf(fabsf(totchange) - fabsf(totchange_allowed));
1428                                 if(totchange > 0) {
1429                                         left_over *= -1;
1430                                 }
1431                         }
1432                         else {
1433                                 /* all of the change will be permitted */
1434                                 totchange_allowed = -totchange;
1435                         }
1436                         /* move the weight evenly between the allowed groups, move excess back onto the used groups based on the change */
1437                         totchange_allowed = redistribute_change(ndv, defbase_tot, change_status, 1, -1, totchange_allowed, total_valid, do_auto_normalize);
1438                         left_over += totchange_allowed;
1439                         if(left_over) {
1440                                 /* more than one nonzero weights were changed with the same ratio with multipaint, so keep them changed that way! */
1441                                 if(total_changed > 1 && do_multipaint) {
1442                                         float undo_change = get_mp_change(ndv, defbase_tot, defbase_sel, left_over);
1443                                         multipaint_selection(ndv, defbase_tot, undo_change, defbase_sel);
1444                                 }       
1445                                 /* or designatedw is still -1 put weight back as evenly as possible */
1446                                 else {
1447                                         redistribute_change(ndv, defbase_tot, change_status, 2, -2, left_over, total_changed, do_auto_normalize);
1448                                 }
1449                         }
1450                 }
1451                 else {
1452                         /* reset the weights */
1453                         unsigned int i;
1454                         MDeformWeight *dw_old= odv->dw;
1455                         MDeformWeight *dw_new= ndv->dw;
1456
1457                         for (i= odv->totweight; i != 0; i--, dw_old++, dw_new++) {
1458                                 dw_new->weight= dw_old->weight;
1459                         }
1460                 }
1461         }
1462
1463         MEM_freeN(change_status);
1464 }
1465
1466 /* multi-paint's initial, potential change is computed here based on the user's stroke */
1467 static float get_mp_change(MDeformVert *odv, const int defbase_tot, const char *defbase_sel, float brush_change)
1468 {
1469         float selwsum = 0.0f;
1470         unsigned int i;
1471         MDeformWeight *dw= odv->dw;
1472
1473         for (i= odv->totweight; i != 0; i--, dw++) {
1474                 if (dw->def_nr < defbase_tot) {
1475                         if(defbase_sel[dw->def_nr]) {
1476                                 selwsum += dw->weight;
1477                         }
1478                 }
1479         }
1480         if(selwsum && selwsum+brush_change > 0) {
1481                 return (selwsum+brush_change)/selwsum;
1482         }
1483         return 0.0f;
1484 }
1485
1486 /* change the weights back to the wv's weights
1487  * it assumes you already have the correct pointer index */
1488 static void defvert_reset_to_prev(MDeformVert *dv_prev, MDeformVert *dv)
1489 {
1490         MDeformWeight *dw= dv->dw;
1491         MDeformWeight *dw_prev;
1492         unsigned int i;
1493         for (i= dv->totweight; i != 0; i--, dw++) {
1494                 dw_prev= defvert_find_index(dv_prev, dw->def_nr);
1495                 /* if there was no w when there is a d, then the old weight was 0 */
1496                 dw->weight = dw_prev ? dw_prev->weight : 0.0f;
1497         }
1498 }
1499
1500 static void clamp_weights(MDeformVert *dvert)
1501 {
1502         MDeformWeight *dw= dvert->dw;
1503         unsigned int i;
1504         for (i= dvert->totweight; i != 0; i--, dw++) {
1505                 CLAMP(dw->weight, 0.0f, 1.0f);
1506         }
1507 }
1508
1509 /* struct to avoid passing many args each call to do_weight_paint_vertex()
1510  * this _could_ be made a part of the operators 'WPaintData' struct, or at
1511  * least a member, but for now keep its own struct, initialized on every
1512  * paint stroke update - campbell */
1513 typedef struct WeightPaintInfo {
1514
1515         int defbase_tot;
1516
1517         /* both must add up to 'defbase_tot' */
1518         int defbase_tot_sel;
1519         int defbase_tot_unsel;
1520
1521         int vgroup_active; /* (ob->actdef - 1) */
1522         int vgroup_mirror; /* mirror group or -1 */
1523
1524         const char *lock_flags;  /* boolean array for locked bones,
1525                                   * length of defbase_tot */
1526         const char *defbase_sel; /* boolean array for selected bones,
1527                                   * length of defbase_tot, cant be const because of how its passed */
1528
1529         const char *vgroup_validmap; /* same as WeightPaintData.vgroup_validmap,
1530                                       * only added here for convenience */
1531
1532         char do_flip;
1533         char do_multipaint;
1534         char do_auto_normalize;
1535
1536         float brush_alpha_value;  /* result of brush_alpha() */
1537 } WeightPaintInfo;
1538
1539 /* fresh start to make multi-paint and locking modular */
1540 /* returns TRUE if it thinks you need to reset the weights due to
1541  * normalizing while multi-painting
1542  *
1543  * note: this assumes dw->def_nr range has been checked by the caller
1544  */
1545 static int apply_mp_locks_normalize(Mesh *me, const WeightPaintInfo *wpi,
1546                                     const unsigned int index,
1547                                     MDeformWeight *dw, MDeformWeight *tdw,
1548                                     float change, float oldChange,
1549                                     float oldw, float neww)
1550 {
1551         MDeformVert *dv= &me->dvert[index];
1552         MDeformVert dv_test= {NULL};
1553
1554         dv_test.dw= MEM_dupallocN(dv->dw);
1555         dv_test.flag = dv->flag;
1556         dv_test.totweight = dv->totweight;
1557         /* do not multi-paint if a locked group is selected or the active group is locked
1558          * !lock_flags[dw->def_nr] helps if nothing is selected, but active group is locked */
1559         if ( (wpi->lock_flags == NULL) ||
1560              ((wpi->lock_flags[dw->def_nr] == FALSE) && /* def_nr range has to be checked for by caller */
1561               has_locked_group_selected(wpi->defbase_tot, wpi->defbase_sel, wpi->lock_flags) == FALSE))
1562         {
1563                 if(wpi->do_multipaint && wpi->defbase_tot_sel > 1) {
1564                         if(change && change!=1) {
1565                                 multipaint_selection(dv, wpi->defbase_tot, change, wpi->defbase_sel);
1566                         }
1567                 }
1568                 else { /* this lets users paint normally, but don't let them paint locked groups */
1569                         dw->weight = neww;
1570                 }
1571         }
1572         clamp_weights(dv);
1573
1574         enforce_locks(&dv_test, dv, wpi->defbase_tot, wpi->defbase_sel, wpi->lock_flags, wpi->vgroup_validmap, wpi->do_auto_normalize, wpi->do_multipaint);
1575
1576         if (wpi->do_auto_normalize) {
1577                 /* XXX - should we pass the active group? - currently '-1' */
1578                 do_weight_paint_normalize_all(dv, wpi->defbase_tot, wpi->vgroup_validmap);
1579         }
1580
1581         if(oldChange && wpi->do_multipaint && wpi->defbase_tot_sel > 1) {
1582                 if(tdw->weight != oldw) {
1583                         if(neww > oldw) {
1584                                 if(tdw->weight <= oldw) {
1585                                         MEM_freeN(dv_test.dw);
1586                                         return TRUE;
1587                                 }
1588                         }
1589                         else {
1590                                 if(tdw->weight >= oldw) {
1591                                         MEM_freeN(dv_test.dw);
1592                                         return TRUE;
1593                                 }
1594                         }
1595                 }
1596         }
1597         MEM_freeN(dv_test.dw);
1598         return FALSE;
1599 }
1600
1601 /* within the current dvert index, get the dw that is selected and has a weight
1602  * above 0, this helps multi-paint */
1603 static int get_first_selected_nonzero_weight(MDeformVert *dvert, const int defbase_tot, const char *defbase_sel)
1604 {
1605         int i;
1606         MDeformWeight *dw= dvert->dw;
1607         for(i=0; i< dvert->totweight; i++, dw++) {
1608                 if (dw->def_nr < defbase_tot) {
1609                         if (defbase_sel[dw->def_nr] && dw->weight > 0.0f) {
1610                                 return i;
1611                         }
1612                 }
1613         }
1614         return -1;
1615 }
1616
1617
1618 static char *wpaint_make_validmap(Object *ob);
1619
1620
1621 static void do_weight_paint_vertex(/* vars which remain the same for every vert */
1622                                    VPaint *wp, Object *ob, const WeightPaintInfo *wpi,
1623                                    /* vars which change on each stroke */
1624                                    const unsigned int index, float alpha, float paintweight
1625                                    )
1626 {
1627         Mesh *me= ob->data;
1628         MDeformVert *dv= &me->dvert[index];
1629         
1630         MDeformWeight *dw, *dw_prev;
1631
1632         /* mirror vars */
1633         int index_mirr;
1634         int vgroup_mirr;
1635
1636         MDeformVert *dv_mirr;
1637         MDeformWeight *dw_mirr;
1638
1639         const short do_multipaint_totsel = (wpi->do_multipaint && wpi->defbase_tot_sel > 1);
1640
1641         if(wp->flag & VP_ONLYVGROUP) {
1642                 dw= defvert_find_index(dv, wpi->vgroup_active);
1643                 dw_prev= defvert_find_index(wp->wpaint_prev+index, wpi->vgroup_active);
1644         }
1645         else {
1646                 dw= defvert_verify_index(dv, wpi->vgroup_active);
1647                 dw_prev= defvert_verify_index(wp->wpaint_prev+index, wpi->vgroup_active);
1648         }
1649
1650         if(dw==NULL || dw_prev==NULL) {
1651                 return;
1652         }
1653
1654
1655         /* from now on we can check if mirrors enabled if this var is -1 and not bother with the flag */
1656         if (me->editflag & ME_EDIT_MIRROR_X) {
1657                 index_mirr = mesh_get_x_mirror_vert(ob, index);
1658                 vgroup_mirr = (wpi->vgroup_mirror != -1) ? wpi->vgroup_mirror : wpi->vgroup_active;
1659
1660                 /* another possible error - mirror group _and_ active group are the same (which is fine),
1661                  * but we also are painting onto a center vertex - this would paint the same weight twice */
1662                 if (index_mirr == index && vgroup_mirr == wpi->vgroup_active) {
1663                         index_mirr = vgroup_mirr = -1;
1664                 }
1665         }
1666         else {
1667                 index_mirr = vgroup_mirr = -1;
1668         }
1669
1670
1671         /* get the mirror def vars */
1672         if (index_mirr != -1) {
1673                 dv_mirr = &me->dvert[index_mirr];
1674                 if (wp->flag & VP_ONLYVGROUP) {
1675                         dw_mirr = defvert_find_index(dv_mirr, vgroup_mirr);
1676
1677                         if (dw_mirr == NULL) {
1678                                 index_mirr = vgroup_mirr = -1;
1679                                 dv_mirr = NULL;
1680                         }
1681                 }
1682                 else {
1683                         if (index != index_mirr) {
1684                                 dw_mirr = defvert_verify_index(dv_mirr, vgroup_mirr);
1685                         }
1686                         else {
1687                                 /* dv and dv_mirr are the same */
1688                                 int totweight_prev = dv_mirr->totweight;
1689                                 int dw_offset = (int)(dw - dv_mirr->dw);
1690                                 dw_mirr = defvert_verify_index(dv_mirr, vgroup_mirr);
1691
1692                                 /* if we added another, get our old one back */
1693                                 if (totweight_prev != dv_mirr->totweight) {
1694                                         dw = &dv_mirr->dw[dw_offset];
1695                                 }
1696                         }
1697                 }
1698         }
1699         else {
1700                 dv_mirr = NULL;
1701                 dw_mirr = NULL;
1702         }
1703
1704
1705         /* TODO: De-duplicate the simple weight paint - jason */
1706         /* ... or not, since its <10 SLOC - campbell */
1707
1708         /* If there are no locks or multipaint,
1709          * then there is no need to run the more complicated checks */
1710         if ( (do_multipaint_totsel == FALSE) &&
1711              (wpi->lock_flags == NULL || has_locked_group(dv, wpi->defbase_tot, wpi->vgroup_validmap, wpi->lock_flags) == FALSE))
1712         {
1713                 dw->weight = wpaint_blend(wp, dw->weight, dw_prev->weight, alpha, paintweight,
1714                                           wpi->brush_alpha_value, wpi->do_flip, FALSE);
1715
1716                 /* WATCH IT: take care of the ordering of applying mirror -> normalize,
1717                  * can give wrong results [#26193], least confusing if normalize is done last */
1718
1719                 /* apply mirror */
1720                 if(index_mirr != -1) {
1721                         /* copy, not paint again */
1722                         dw_mirr->weight = dw->weight;
1723                 }
1724
1725                 /* apply normalize */
1726                 if (wpi->do_auto_normalize) {
1727                         /* note on normalize - this used to be applied after painting and normalize all weights,
1728                          * in some ways this is good because there is feedback where the more weights involved would
1729                          * 'risist' so you couldn't instantly zero out other weights by painting 1.0 on the active.
1730                          *
1731                          * However this gave a problem since applying mirror, then normalize both verts
1732                          * the resulting weight wont match on both sides.
1733                          *
1734                          * If this 'resisting', slower normalize is nicer, we could call
1735                          * do_weight_paint_normalize_all() and only use...
1736                          * do_weight_paint_normalize_all_active() when normalizing the mirror vertex.
1737                          * - campbell
1738                          */
1739                         do_weight_paint_normalize_all_active(dv, wpi->defbase_tot, wpi->vgroup_validmap, wpi->vgroup_active);
1740
1741                         if (index_mirr != -1) {
1742                                 /* only normalize if this is not a center vertex, else we get a conflict, normalizing twice */
1743                                 if (index != index_mirr) {
1744                                         do_weight_paint_normalize_all_active(dv_mirr, wpi->defbase_tot, wpi->vgroup_validmap, vgroup_mirr);
1745                                 }
1746                                 else {
1747                                         /* this case accounts for...
1748                                          * - painting onto a center vertex of a mesh
1749                                          * - x mirror is enabled
1750                                          * - auto normalize is enabled
1751                                          * - the group you are painting onto has a L / R version
1752                                          *
1753                                          * We wan't L/R vgroups to have the same weight but this cant be if both are over 0.5,
1754                                          * We _could_ have special check for that, but this would need its own normalize function which
1755                                          * holds 2 groups from changing at once.
1756                                          *
1757                                          * So! just balance out the 2 weights, it keeps them equal and everything normalized.
1758                                          *
1759                                          * While it wont hit the desired weight immediatelty as the user waggles their mouse,
1760                                          * constant painting and re-normalizing will get there. this is also just simpler logic.
1761                                          * - campbell */
1762                                         dw_mirr->weight = dw->weight = (dw_mirr->weight + dw->weight) * 0.5f;
1763                                 }
1764                         }
1765                 }
1766         }
1767         else {
1768                 /* use locks and/or multipaint */
1769                 float oldw;
1770                 float neww;
1771                 float testw=0;
1772                 float change = 0;
1773                 float oldChange = 0;
1774                 int i;
1775                 MDeformWeight *tdw = NULL, *tdw_prev;
1776                 MDeformVert dv_copy= {NULL};
1777
1778                 oldw = dw->weight;
1779                 neww = wpaint_blend(wp, dw->weight, dw_prev->weight, alpha, paintweight,
1780                                     wpi->brush_alpha_value, wpi->do_flip, do_multipaint_totsel);
1781                 
1782                 /* setup multi-paint */
1783                 if (do_multipaint_totsel) {
1784                         dv_copy.dw= MEM_dupallocN(dv->dw);
1785                         dv_copy.flag = dv->flag;
1786                         dv_copy.totweight = dv->totweight;
1787                         tdw = dw;
1788                         tdw_prev = dw_prev;
1789                         change = get_mp_change(&wp->wpaint_prev[index], wpi->defbase_tot, wpi->defbase_sel, neww - oldw);
1790                         if(change) {
1791                                 if(!tdw->weight) {
1792                                         i = get_first_selected_nonzero_weight(dv, wpi->defbase_tot, wpi->defbase_sel);
1793                                         if(i>=0) {
1794                                                 tdw = &(dv->dw[i]);
1795                                                 tdw_prev = defvert_verify_index(&wp->wpaint_prev[index], tdw->def_nr);
1796                                         }
1797                                         else {
1798                                                 change = 0;
1799                                         }
1800                                 }
1801                                 if(change && tdw_prev->weight && tdw_prev->weight * change) {
1802                                         if(tdw->weight != tdw_prev->weight) {
1803                                                 oldChange = tdw->weight/tdw_prev->weight;
1804                                                 testw = tdw_prev->weight*change;
1805                                                 if( testw > tdw_prev->weight ) {
1806                                                         if(change > oldChange) {
1807                                                                 /* reset the weights and use the new change */
1808                                                                 defvert_reset_to_prev(wp->wpaint_prev+index, dv);
1809                                                         }
1810                                                         else {
1811                                                                 /* the old change was more significant, so set
1812                                                                  * the change to 0 so that it will not do another multi-paint */
1813                                                                 change = 0;
1814                                                         }
1815                                                 }
1816                                                 else {
1817                                                         if(change < oldChange) {
1818                                                                 defvert_reset_to_prev(wp->wpaint_prev+index, dv);
1819                                                         }
1820                                                         else {
1821                                                                 change = 0;
1822                                                         }
1823                                                 }
1824                                         }
1825                                 }
1826                                 else {
1827                                         change = 0;
1828                                 }
1829                         }
1830                 }
1831                 
1832                 if(apply_mp_locks_normalize(me, wpi, index, dw, tdw, change, oldChange, oldw, neww)) {
1833                         defvert_reset_to_prev(&dv_copy, dv);
1834                         change = 0;
1835                         oldChange = 0;
1836                 }
1837                 if(dv_copy.dw) {
1838                         MEM_freeN(dv_copy.dw);
1839                 }
1840 #if 0
1841                 /* dv may have been altered greatly */
1842                 dw = defvert_find_index(dv, vgroup);
1843 #else
1844                 dw = NULL; /* UNUSED after assignment, set to NULL to ensuyre we don't
1845                                 * use again, we thats needed un-ifdef the line above */
1846                 (void)dw;  /* quiet warnigns */
1847 #endif
1848
1849                 /* x mirror painting */
1850                 if(index_mirr != -1) {
1851                         /* copy, not paint again */
1852
1853                         /* dw_mirr->weight = dw->weight; */  /* TODO, explain the logic in not assigning weight! - campbell */
1854                         apply_mp_locks_normalize(me, wpi, index_mirr, dw_mirr, tdw, change, oldChange, oldw, neww);
1855                 }
1856         }
1857 }
1858
1859
1860 /* *************** set wpaint operator ****************** */
1861
1862 static int set_wpaint(bContext *C, wmOperator *UNUSED(op))              /* toggle */
1863 {               
1864         Object *ob= CTX_data_active_object(C);
1865         Scene *scene= CTX_data_scene(C);
1866         VPaint *wp= scene->toolsettings->wpaint;
1867         Mesh *me;
1868         
1869         me= get_mesh(ob);
1870         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1871         
1872         if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
1873         else ob->mode |= OB_MODE_WEIGHT_PAINT;
1874         
1875         
1876         /* Weightpaint works by overriding colors in mesh,
1877                 * so need to make sure we recalc on enter and
1878                 * exit (exit needs doing regardless because we
1879                                 * should redeform).
1880                 */
1881         DAG_id_tag_update(&me->id, 0);
1882         
1883         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1884                 Object *par;
1885                 
1886                 if(wp==NULL)
1887                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1888
1889                 paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
1890                 paint_cursor_start(C, weight_paint_poll);
1891                 
1892                 mesh_octree_table(ob, NULL, NULL, 's');
1893                 
1894                 /* verify if active weight group is also active bone */
1895                 par= modifiers_isDeformedByArmature(ob);
1896                 if(par && (par->mode & OB_MODE_POSE)) {
1897                         bArmature *arm= par->data;
1898
1899                         if(arm->act_bone)
1900                                 ED_vgroup_select_by_name(ob, arm->act_bone->name);
1901                 }
1902         }
1903         else {
1904                 mesh_octree_table(NULL, NULL, NULL, 'e');
1905                 mesh_mirrtopo_table(NULL, 'e');
1906         }
1907         
1908         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1909         
1910         return OPERATOR_FINISHED;
1911 }
1912
1913 /* for switching to/from mode */
1914 static int paint_poll_test(bContext *C)
1915 {
1916         Object *ob= CTX_data_active_object(C);
1917         if(CTX_data_edit_object(C))
1918                 return 0;
1919         if(CTX_data_active_object(C)==NULL)
1920                 return 0;
1921         if(!ob->data || ((ID *)ob->data)->lib)
1922                 return 0;
1923         return 1;
1924 }
1925
1926 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1927 {
1928         
1929         /* identifiers */
1930         ot->name= "Weight Paint Mode";
1931         ot->idname= "PAINT_OT_weight_paint_toggle";
1932         
1933         /* api callbacks */
1934         ot->exec= set_wpaint;
1935         ot->poll= paint_poll_test;
1936         
1937         /* flags */
1938         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1939         
1940 }
1941
1942 /* ************ weight paint operator ********** */
1943
1944 struct WPaintData {
1945         ViewContext vc;
1946         int *indexar;
1947         int vgroup_active;
1948         int vgroup_mirror;
1949         float *vertexcosnos;
1950         float wpimat[3][3];
1951         
1952         /*variables for auto normalize*/
1953         const char *vgroup_validmap; /*stores if vgroups tie to deforming bones or not*/
1954         const char *lock_flags;
1955         int defbase_tot;
1956 };
1957
1958 static char *wpaint_make_validmap(Object *ob)
1959 {
1960         bDeformGroup *dg;
1961         ModifierData *md;
1962         char *vgroup_validmap;
1963         GHash *gh;
1964         int i, step1=1;
1965
1966         if(ob->defbase.first == NULL) {
1967                 return NULL;
1968         }
1969
1970         gh= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
1971
1972         /*add all names to a hash table*/
1973         for (dg=ob->defbase.first; dg; dg=dg->next) {
1974                 BLI_ghash_insert(gh, dg->name, NULL);
1975         }
1976
1977         /*now loop through the armature modifiers and identify deform bones*/
1978         for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
1979                 if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
1980                         continue;
1981
1982                 if (md->type == eModifierType_Armature) {
1983                         ArmatureModifierData *amd= (ArmatureModifierData*) md;
1984
1985                         if(amd->object && amd->object->pose) {
1986                                 bPose *pose= amd->object->pose;
1987                                 bPoseChannel *chan;
1988                                 
1989                                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1990                                         if (chan->bone->flag & BONE_NO_DEFORM)
1991                                                 continue;
1992
1993                                         if (BLI_ghash_haskey(gh, chan->name)) {
1994                                                 BLI_ghash_remove(gh, chan->name, NULL, NULL);
1995                                                 BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
1996                                         }
1997                                 }
1998                         }
1999                 }
2000         }
2001
2002         vgroup_validmap= MEM_mallocN(BLI_ghash_size(gh), "wpaint valid map");
2003
2004         /*add all names to a hash table*/
2005         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
2006                 vgroup_validmap[i]= (BLI_ghash_lookup(gh, dg->name) != NULL);
2007         }
2008
2009         BLI_assert(i == BLI_ghash_size(gh));
2010
2011         BLI_ghash_free(gh, NULL, NULL);
2012
2013         return vgroup_validmap;
2014 }
2015
2016 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2017 {
2018         Scene *scene= CTX_data_scene(C);
2019         struct PaintStroke *stroke = op->customdata;
2020         ToolSettings *ts= scene->toolsettings;
2021         VPaint *wp= ts->wpaint;
2022         Object *ob= CTX_data_active_object(C);
2023         struct WPaintData *wpd;
2024         Mesh *me;
2025         bDeformGroup *dg;
2026
2027         float mat[4][4], imat[4][4];
2028         
2029         if(scene->obedit) {
2030                 return FALSE;
2031         }
2032         
2033         me= get_mesh(ob);
2034         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
2035         
2036         /* if nothing was added yet, we make dverts and a vertex deform group */
2037         if (!me->dvert) {
2038                 ED_vgroup_data_create(&me->id);
2039                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
2040         }
2041
2042         /* this happens on a Bone select, when no vgroup existed yet */
2043         if (ob->actdef <= 0) {
2044                 Object *modob;
2045                 if((modob = modifiers_isDeformedByArmature(ob))) {
2046                         Bone *actbone= ((bArmature *)modob->data)->act_bone;
2047                         if(actbone) {
2048                                 bPoseChannel *pchan= get_pose_channel(modob->pose, actbone->name);
2049
2050                                 if(pchan) {
2051                                         bDeformGroup *dg= defgroup_find_name(ob, pchan->name);
2052                                         if(dg==NULL) {
2053                                                 dg= ED_vgroup_add_name(ob, pchan->name);        /* sets actdef */
2054                                         }
2055                                         else {
2056                                                 int actdef = 1 + BLI_findindex(&ob->defbase, dg);
2057                                                 BLI_assert(actdef >= 0);
2058                                                 ob->actdef= actdef;
2059                                         }
2060                                 }
2061                         }
2062                 }
2063         }
2064         if(ob->defbase.first==NULL) {
2065                 ED_vgroup_add(ob);
2066         }
2067
2068         /* ensure we dont try paint onto an invalid group */
2069         if (ob->actdef <= 0) {
2070                 BKE_report(op->reports, RPT_WARNING, "No active vertex group for painting, aborting");
2071                 return FALSE;
2072         }
2073
2074         /* check if we are attempting to paint onto a locked vertex group,
2075          * and other options disallow it from doing anything useful */
2076         dg = BLI_findlink(&ob->defbase, (ob->actdef-1));
2077         if (dg->flag & DG_LOCK_WEIGHT) {
2078                 BKE_report(op->reports, RPT_WARNING, "Active group is locked, aborting");
2079                 return FALSE;
2080         }
2081
2082         /* ALLOCATIONS! no return after this line */
2083         /* make mode data storage */
2084         wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
2085         paint_stroke_set_mode_data(stroke, wpd);
2086         view3d_set_viewcontext(C, &wpd->vc);
2087
2088         wpd->vgroup_active = ob->actdef - 1;
2089         wpd->vgroup_mirror = -1;
2090
2091         /* set up auto-normalize, and generate map for detecting which
2092          * vgroups affect deform bones*/
2093         wpd->defbase_tot = BLI_countlist(&ob->defbase);
2094         wpd->lock_flags = gen_lock_flags(ob, wpd->defbase_tot);
2095         if (ts->auto_normalize || ts->multipaint || wpd->lock_flags) {
2096                 wpd->vgroup_validmap = wpaint_make_validmap(ob);
2097         }
2098
2099         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
2100         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
2101         wpd->indexar= get_indexarray(me);
2102         copy_wpaint_prev(wp, me->dvert, me->totvert);
2103
2104         /* imat for normals */
2105         mult_m4_m4m4(mat, wpd->vc.rv3d->viewmat, ob->obmat);
2106         invert_m4_m4(imat, mat);
2107         copy_m3_m4(wpd->wpimat, imat);
2108
2109         /* if mirror painting, find the other group */
2110         if(me->editflag & ME_EDIT_MIRROR_X) {
2111                 wpd->vgroup_mirror = wpaint_mirror_vgroup_ensure(ob, wpd->vgroup_active);
2112         }
2113         
2114         return TRUE;
2115 }
2116
2117 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
2118 {
2119         Scene *scene= CTX_data_scene(C);
2120         ToolSettings *ts= CTX_data_tool_settings(C);
2121         VPaint *wp= ts->wpaint;
2122         Brush *brush = paint_brush(&wp->paint);
2123         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
2124         ViewContext *vc;
2125         Object *ob;
2126         Mesh *me;
2127         float mat[4][4];
2128         float paintweight;
2129         int *indexar;
2130         float totw;
2131         unsigned int index, totindex;
2132         float alpha;
2133         float mval[2];
2134         int use_vert_sel;
2135         char *defbase_sel;
2136
2137         const float pressure = RNA_float_get(itemptr, "pressure");
2138         const float brush_size_pressure = brush_size(scene, brush) * (brush_use_size_pressure(scene, brush) ? pressure : 1.0f);
2139         const float brush_alpha_value = brush_alpha(scene, brush);
2140         const float brush_alpha_pressure = brush_alpha_value * (brush_use_alpha_pressure(scene, brush) ? pressure : 1.0f);
2141
2142         /* intentionally dont initialize as NULL, make sure we initialize all members below */
2143         WeightPaintInfo wpi;
2144
2145         /* cannot paint if there is no stroke data */
2146         if (wpd == NULL) {
2147                 /* XXX: force a redraw here, since even though we can't paint,
2148                  * at least view won't freeze until stroke ends */
2149                 ED_region_tag_redraw(CTX_wm_region(C));
2150                 return;
2151         }
2152                 
2153         vc= &wpd->vc;
2154         ob= vc->obact;
2155         me= ob->data;
2156         indexar= wpd->indexar;
2157         
2158         view3d_operator_needs_opengl(C);
2159                 
2160         /* load projection matrix */
2161         mult_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat);
2162
2163         RNA_float_get_array(itemptr, "mouse", mval);
2164         mval[0]-= vc->ar->winrct.xmin;
2165         mval[1]-= vc->ar->winrct.ymin;
2166
2167
2168
2169         /* *** setup WeightPaintInfo - pass onto do_weight_paint_vertex *** */
2170         wpi.defbase_tot=        wpd->defbase_tot;
2171         defbase_sel=            MEM_mallocN(wpi.defbase_tot*sizeof(char), "wpi.defbase_sel");
2172         wpi.defbase_tot_sel=    get_selected_defgroups(ob, defbase_sel, wpi.defbase_tot);
2173         wpi.defbase_sel=        defbase_sel; /* so we can stay const */
2174         if(wpi.defbase_tot_sel == 0 && ob->actdef > 0) wpi.defbase_tot_sel = 1;
2175
2176         wpi.defbase_tot_unsel=  wpi.defbase_tot - wpi.defbase_tot_sel;
2177         wpi.vgroup_active=      wpd->vgroup_active;
2178         wpi.vgroup_mirror=      wpd->vgroup_mirror;
2179         wpi.lock_flags=         wpd->lock_flags;
2180         wpi.vgroup_validmap=    wpd->vgroup_validmap;
2181         wpi.do_flip=            RNA_boolean_get(itemptr, "pen_flip");
2182         wpi.do_multipaint=      (ts->multipaint != 0);
2183         wpi.do_auto_normalize=  ((ts->auto_normalize != 0) && (wpi.vgroup_validmap != NULL));
2184         wpi.brush_alpha_value=  brush_alpha_value;
2185         /* *** done setting up WeightPaintInfo *** */
2186
2187
2188
2189         swap_m4m4(wpd->vc.rv3d->persmat, mat);
2190
2191         use_vert_sel= (me->editflag & ME_EDIT_VERT_SEL) != 0;
2192
2193         /* which faces are involved */
2194         if(wp->flag & VP_AREA) {
2195                 /* Ugly hack, to avoid drawing vertex index when getting the face index buffer - campbell */
2196                 me->editflag &= ~ME_EDIT_VERT_SEL;
2197                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size_pressure);
2198                 me->editflag |= use_vert_sel ? ME_EDIT_VERT_SEL : 0;
2199         }
2200         else {
2201                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
2202                 if(indexar[0]) totindex= 1;
2203                 else totindex= 0;
2204         }
2205                         
2206         if(wp->flag & VP_COLINDEX) {
2207                 for(index=0; index<totindex; index++) {
2208                         if(indexar[index] && indexar[index]<=me->totface) {
2209                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
2210                                                 
2211                                 if(mface->mat_nr!=ob->actcol-1) {
2212                                         indexar[index]= 0;
2213                                 }
2214                         }
2215                 }
2216         }
2217                         
2218         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
2219                 for(index=0; index<totindex; index++) {
2220                         if(indexar[index] && indexar[index]<=me->totface) {
2221                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
2222                                                 
2223                                 if((mface->flag & ME_FACE_SEL)==0) {
2224                                         indexar[index]= 0;
2225                                 }
2226                         }                                       
2227                 }
2228         }
2229                         
2230         /* make sure each vertex gets treated only once */
2231         /* and calculate filter weight */
2232         totw= 0.0f;
2233         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR)
2234                 paintweight= 0.0f;
2235         else
2236                 paintweight= ts->vgroup_weight;
2237                         
2238         for(index=0; index<totindex; index++) {
2239                 if(indexar[index] && indexar[index]<=me->totface) {
2240                         MFace *mface= me->mface + (indexar[index]-1);
2241
2242                         if(use_vert_sel) {
2243                                 me->dvert[mface->v1].flag = (me->mvert[mface->v1].flag & SELECT);
2244                                 me->dvert[mface->v2].flag = (me->mvert[mface->v2].flag & SELECT);
2245                                 me->dvert[mface->v3].flag = (me->mvert[mface->v3].flag & SELECT);
2246                                 if(mface->v4) me->dvert[mface->v4].flag = (me->mvert[mface->v4].flag & SELECT);
2247                         }
2248                         else {
2249                                 me->dvert[mface->v1].flag= 1;
2250                                 me->dvert[mface->v2].flag= 1;
2251                                 me->dvert[mface->v3].flag= 1;
2252                                 if(mface->v4) me->dvert[mface->v4].flag= 1;
2253                         }
2254                                         
2255                         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
2256                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
2257                                 unsigned int fidx= mface->v4 ? 3:2;
2258                                                 
2259                                 if(wp->flag & VP_ONLYVGROUP)
2260                                         dw_func= (MDeformWeight *(*)(MDeformVert *, const int))defvert_find_index;
2261                                 else
2262                                         dw_func= defvert_verify_index;
2263
2264                                 do {
2265                                         unsigned int vidx= *(&mface->v1 + fidx);
2266                                         const float fac = calc_vp_strength_dl(wp, vc, wpd->vertexcosnos+6*vidx, mval, brush_size_pressure);
2267                                         if (fac > 0.0f) {
2268                                                 dw = dw_func(&me->dvert[vidx], wpi.vgroup_active);
2269                                                 paintweight += dw ? (dw->weight * fac) : 0.0f;
2270                                                 totw += fac;
2271                                         }
2272
2273                                 } while (fidx--);
2274
2275                         }
2276                 }
2277         }
2278                         
2279         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
2280                 paintweight /= totw;
2281         }
2282
2283         for(index=0; index<totindex; index++) {
2284                                 
2285                 if(indexar[index] && indexar[index]<=me->totface) {
2286                         MFace *mf= me->mface + (indexar[index]-1);
2287                         unsigned int fidx= mf->v4 ? 3:2;
2288                         do {
2289                                 unsigned int vidx= *(&mf->v1 + fidx);
2290
2291                                 if(me->dvert[vidx].flag) {
2292                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*vidx,
2293                                                                 mval, brush_size_pressure, brush_alpha_pressure);
2294                                         if(alpha) {
2295                                                 do_weight_paint_vertex(wp, ob, &wpi, vidx, alpha, paintweight);
2296                                         }
2297                                         me->dvert[vidx].flag= 0;
2298                                 }
2299                         } while (fidx--);
2300                 }
2301         }
2302
2303
2304         /* *** free wpi members */
2305         MEM_freeN((void *)wpi.defbase_sel);
2306         /* *** dont freeing wpi members */
2307
2308
2309         swap_m4m4(vc->rv3d->persmat, mat);
2310                         
2311         DAG_id_tag_update(ob->data, 0);
2312         ED_region_tag_redraw(vc->ar);
2313 }
2314
2315 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2316 {
2317         ToolSettings *ts= CTX_data_tool_settings(C);
2318         Object *ob= CTX_data_active_object(C);
2319         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
2320         
2321         if(wpd) {
2322                 if(wpd->vertexcosnos)
2323                         MEM_freeN(wpd->vertexcosnos);
2324                 MEM_freeN(wpd->indexar);
2325                 
2326                 if (wpd->vgroup_validmap)
2327                         MEM_freeN((void *)wpd->vgroup_validmap);
2328                 if(wpd->lock_flags)
2329                         MEM_freeN((void *)wpd->lock_flags);
2330
2331                 MEM_freeN(wpd);
2332         }
2333         
2334         /* frees prev buffer */
2335         copy_wpaint_prev(ts->wpaint, NULL, 0);
2336         
2337         /* and particles too */
2338         if(ob->particlesystem.first) {
2339                 ParticleSystem *psys;
2340                 int i;
2341                 
2342                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
2343                         for(i=0; i<PSYS_TOT_VG; i++) {
2344                                 if(psys->vgroup[i]==ob->actdef) {
2345                                         psys->recalc |= PSYS_RECALC_RESET;
2346                                         break;
2347                                 }
2348                         }
2349                 }
2350         }
2351         
2352         DAG_id_tag_update(ob->data, 0);
2353 }
2354
2355
2356 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2357 {
2358         
2359         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
2360                                           wpaint_stroke_update_step,
2361                                           wpaint_stroke_done, event->type);
2362         
2363         /* add modal handler */
2364         WM_event_add_modal_handler(C, op);
2365
2366         op->type->modal(C, op, event);
2367         
2368         return OPERATOR_RUNNING_MODAL;
2369 }
2370
2371 static int wpaint_cancel(bContext *C, wmOperator *op)
2372 {
2373         paint_stroke_cancel(C, op);
2374
2375         return OPERATOR_CANCELLED;
2376 }
2377
2378 void PAINT_OT_weight_paint(wmOperatorType *ot)
2379 {
2380         
2381         /* identifiers */
2382         ot->name= "Weight Paint";
2383         ot->idname= "PAINT_OT_weight_paint";
2384         
2385         /* api callbacks */
2386         ot->invoke= wpaint_invoke;
2387         ot->modal= paint_stroke_modal;
2388         /* ot->exec= vpaint_exec; <-- needs stroke property */
2389         ot->poll= weight_paint_poll;
2390         ot->cancel= wpaint_cancel;
2391         
2392         /* flags */
2393         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2394
2395         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2396 }
2397
2398 static int weight_paint_set_exec(bContext *C, wmOperator *UNUSED(op))
2399 {
2400         struct Scene *scene= CTX_data_scene(C);
2401         Object *obact = CTX_data_active_object(C);
2402
2403         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
2404         ED_region_tag_redraw(CTX_wm_region(C)); /* XXX - should redraw all 3D views */
2405         return OPERATOR_FINISHED;
2406 }
2407
2408 void PAINT_OT_weight_set(wmOperatorType *ot)
2409 {
2410         /* identifiers */
2411         ot->name= "Set Weight";
2412         ot->idname= "PAINT_OT_weight_set";
2413
2414         /* api callbacks */
2415         ot->exec= weight_paint_set_exec;
2416         ot->poll= mask_paint_poll; /* it was facemask_paint_poll */
2417
2418         /* flags */
2419         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2420 }
2421
2422 /* ************ set / clear vertex paint mode ********** */
2423
2424
2425 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
2426 {       
2427         Object *ob= CTX_data_active_object(C);
2428         Scene *scene= CTX_data_scene(C);
2429         VPaint *vp= scene->toolsettings->vpaint;
2430         Mesh *me;
2431         
2432         me= get_mesh(ob);
2433         
2434         if(me==NULL || object_data_is_libdata(ob)) {
2435                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2436                 return OPERATOR_PASS_THROUGH;
2437         }
2438         
2439         if(me && me->mcol==NULL) make_vertexcol(ob);
2440         
2441         /* toggle: end vpaint */
2442         if(ob->mode & OB_MODE_VERTEX_PAINT) {
2443                 
2444                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2445         }
2446         else {
2447                 ob->mode |= OB_MODE_VERTEX_PAINT;
2448                 /* Turn off weight painting */
2449                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
2450                         set_wpaint(C, op);
2451                 
2452                 if(vp==NULL)
2453                         vp= scene->toolsettings->vpaint= new_vpaint(0);
2454                 
2455                 paint_cursor_start(C, vertex_paint_poll);
2456
2457                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
2458         }
2459         
2460         if (me)
2461                 /* update modifier stack for mapping requirements */
2462                 DAG_id_tag_update(&me->id, 0);
2463         
2464         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
2465         
2466         return OPERATOR_FINISHED;
2467 }
2468
2469 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
2470 {
2471         
2472         /* identifiers */
2473         ot->name= "Vertex Paint Mode";
2474         ot->idname= "PAINT_OT_vertex_paint_toggle";
2475         
2476         /* api callbacks */
2477         ot->exec= set_vpaint;
2478         ot->poll= paint_poll_test;
2479         
2480         /* flags */
2481         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2482 }
2483
2484
2485
2486 /* ********************** vertex paint operator ******************* */
2487
2488 /* Implementation notes:
2489
2490 Operator->invoke()
2491   - validate context (add mcol)
2492   - create customdata storage
2493   - call paint once (mouse click)
2494   - add modal handler 
2495
2496 Operator->modal()
2497   - for every mousemove, apply vertex paint
2498   - exit on mouse release, free customdata
2499         (return OPERATOR_FINISHED also removes handler and operator)
2500
2501 For future:
2502   - implement a stroke event (or mousemove with past positons)
2503   - revise whether op->customdata should be added in object, in set_vpaint
2504
2505 */
2506
2507 typedef struct VPaintData {
2508         ViewContext vc;
2509         unsigned int paintcol;
2510         int *indexar;
2511         float *vertexcosnos;
2512         float vpimat[3][3];
2513 } VPaintData;
2514
2515 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *UNUSED(event))
2516 {
2517         ToolSettings *ts= CTX_data_tool_settings(C);
2518         struct PaintStroke *stroke = op->customdata;
2519         VPaint *vp= ts->vpaint;
2520         struct VPaintData *vpd;
2521         Object *ob= CTX_data_active_object(C);
2522         Mesh *me;
2523         float mat[4][4], imat[4][4];
2524
2525         /* context checks could be a poll() */
2526         me= get_mesh(ob);
2527         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
2528         
2529         if(me->mcol==NULL) make_vertexcol(ob);
2530         if(me->mcol==NULL) return OPERATOR_CANCELLED;
2531         
2532         /* make mode data storage */
2533         vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
2534         paint_stroke_set_mode_data(stroke, vpd);
2535         view3d_set_viewcontext(C, &vpd->vc);
2536         
2537         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
2538         vpd->indexar= get_indexarray(me);
2539         vpd->paintcol= vpaint_get_current_col(vp);
2540         
2541         /* for filtering */
2542         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
2543         
2544         /* some old cruft to sort out later */
2545         mult_m4_m4m4(mat, vpd->vc.rv3d->viewmat, ob->obmat);
2546         invert_m4_m4(imat, mat);
2547         copy_m3_m4(vpd->vpimat, imat);
2548
2549         return 1;
2550 }
2551
2552 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob,
2553                               const unsigned int index, const float mval[2],
2554                               const float brush_size_pressure, const float brush_alpha_pressure,
2555                               int UNUSED(flip))
2556 {
2557         ViewContext *vc = &vpd->vc;
2558         Brush *brush = paint_brush(&vp->paint);
2559         Mesh *me = get_mesh(ob);
2560         MFace *mface= &me->mface[index];
2561         unsigned int *mcol= ((unsigned int*)me->mcol) + 4*index;
2562         unsigned int *mcolorig= ((unsigned int*)vp->vpaint_prev) + 4*index;
2563         float alpha;
2564         int i;
2565
2566         int brush_alpha_pressure_i;
2567         
2568         if((vp->flag & VP_COLINDEX && mface->mat_nr!=ob->actcol-1) ||
2569            ((me->editflag & ME_EDIT_PAINT_MASK) && !(mface->flag & ME_FACE_SEL)))
2570                 return;
2571
2572         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
2573                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
2574                 if(mface->v4) {
2575                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
2576                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
2577                 }
2578                 else {
2579                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
2580                 }
2581         }
2582
2583         brush_alpha_pressure_i = (int)(brush_alpha_pressure*255.0f);
2584
2585         for(i = 0; i < (mface->v4 ? 4 : 3); ++i) {
2586                 alpha = calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*(&mface->v1)[i],
2587                                          mval, brush_size_pressure, brush_alpha_pressure);
2588                 if (alpha) {
2589                         const int alpha_i = (int)(alpha*255.0f);
2590                         mcol[i] = vpaint_blend(vp, mcol[i], mcolorig[i], vpd->paintcol, alpha_i, brush_alpha_pressure_i);
2591                 }
2592         }
2593 }
2594
2595 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
2596 {
2597         Scene *scene= CTX_data_scene(C);
2598         ToolSettings *ts= CTX_data_tool_settings(C);
2599         struct VPaintData *vpd = paint_stroke_mode_data(stroke);
2600         VPaint *vp= ts->vpaint;
2601         Brush *brush = paint_brush(&vp->paint);
2602         ViewContext *vc= &vpd->vc;
2603         Object *ob= vc->obact;
2604         Mesh *me= ob->data;
2605         float mat[4][4];
2606         int *indexar= vpd->indexar;
2607         int totindex, index, flip;
2608         float mval[2];
2609
2610         const float pressure = RNA_float_get(itemptr, "pressure");
2611         const float brush_size_pressure = brush_size(scene, brush) * (brush_use_size_pressure(scene, brush) ? pressure : 1.0f);
2612         const float brush_alpha_pressure = brush_alpha(scene, brush) * (brush_use_alpha_pressure(scene, brush) ? pressure : 1.0f);
2613
2614         RNA_float_get_array(itemptr, "mouse", mval);
2615         flip = RNA_boolean_get(itemptr, "pen_flip");
2616                         
2617         view3d_operator_needs_opengl(C);
2618                         
2619         /* load projection matrix */
2620         mult_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat);
2621
2622         mval[0]-= vc->ar->winrct.xmin;
2623         mval[1]-= vc->ar->winrct.ymin;
2624
2625                         
2626         /* which faces are involved */
2627         if(vp->flag & VP_AREA) {
2628                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size_pressure);
2629         }
2630         else {
2631                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
2632                 if(indexar[0]) totindex= 1;
2633                 else totindex= 0;
2634         }
2635                         
2636         swap_m4m4(vc->rv3d->persmat, mat);
2637                         
2638         for(index=0; index<totindex; index++) {                         
2639                 if (indexar[index] && indexar[index]<=me->totface) {
2640                         vpaint_paint_face(vp, vpd, ob, indexar[index]-1, mval, brush_size_pressure, brush_alpha_pressure, flip);
2641                 }
2642         }
2643                                                 
2644         swap_m4m4(vc->rv3d->persmat, mat);
2645
2646         /* was disabled because it is slow, but necessary for blur */
2647         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR)
2648                 do_shared_vertexcol(me);
2649                         
2650         ED_region_tag_redraw(vc->ar);
2651                         
2652         DAG_id_tag_update(ob->data, 0);
2653 }
2654
2655 static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2656 {
2657         ToolSettings *ts= CTX_data_tool_settings(C);
2658         struct VPaintData *vpd= paint_stroke_mode_data(stroke);
2659         
2660         if(vpd->vertexcosnos)
2661                 MEM_freeN(vpd->vertexcosnos);
2662         MEM_freeN(vpd->indexar);
2663         
2664         /* frees prev buffer */
2665         copy_vpaint_prev(ts->vpaint, NULL, 0);
2666         
2667         MEM_freeN(vpd);
2668 }
2669
2670 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2671 {
2672         
2673         op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
2674                                           vpaint_stroke_update_step,
2675                                           vpaint_stroke_done, event->type);
2676         
2677         /* add modal handler */
2678         WM_event_add_modal_handler(C, op);
2679
2680         op->type->modal(C, op, event);
2681         
2682         return OPERATOR_RUNNING_MODAL;
2683 }
2684
2685 static int vpaint_cancel(bContext *C, wmOperator *op)
2686 {
2687         paint_stroke_cancel(C, op);
2688
2689         return OPERATOR_CANCELLED;
2690 }
2691
2692 void PAINT_OT_vertex_paint(wmOperatorType *ot)
2693 {
2694         /* identifiers */
2695         ot->name= "Vertex Paint";
2696         ot->idname= "PAINT_OT_vertex_paint";
2697         
2698         /* api callbacks */
2699         ot->invoke= vpaint_invoke;
2700         ot->modal= paint_stroke_modal;
2701         /* ot->exec= vpaint_exec; <-- needs stroke property */
2702         ot->poll= vertex_paint_poll;
2703         ot->cancel= vpaint_cancel;
2704         
2705         /* flags */
2706         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2707
2708         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2709 }
2710
2711 /* ********************** weight from bones operator ******************* */
2712
2713 static int weight_from_bones_poll(bContext *C)
2714 {
2715         Object *ob= CTX_data_active_object(C);
2716
2717         return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob));
2718 }
2719
2720 static int weight_from_bones_exec(bContext *C, wmOperator *op)
2721 {
2722         Scene *scene= CTX_data_scene(C);
2723         Object *ob= CTX_data_active_object(C);
2724         Object *armob= modifiers_isDeformedByArmature(ob);
2725         Mesh *me= ob->data;
2726         int type= RNA_enum_get(op->ptr, "type");
2727
2728         create_vgroups_from_armature(op->reports, scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X));
2729
2730         DAG_id_tag_update(&me->id, 0);
2731         WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
2732
2733         return OPERATOR_FINISHED;
2734 }
2735
2736 void PAINT_OT_weight_from_bones(wmOperatorType *ot)
2737 {
2738         static EnumPropertyItem type_items[]= {
2739                 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights froms bones"},
2740                 {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"},
2741                 {0, NULL, 0, NULL, NULL}};
2742
2743         /* identifiers */
2744         ot->name= "Weight from Bones";
2745         ot->idname= "PAINT_OT_weight_from_bones";
2746         
2747         /* api callbacks */
2748         ot->exec= weight_from_bones_exec;
2749         ot->invoke= WM_menu_invoke;
2750         ot->poll= weight_from_bones_poll;
2751         
2752         /* flags */
2753         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2754
2755         /* properties */
2756         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights");
2757 }
2758