Fixed typo in a tooltip
[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 static 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 static 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 static 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 static 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 static 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 static 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:
655                         return mcol_blend(col, paintcol, alpha_i);
656                 case PAINT_BLEND_ADD:
657                         return mcol_add(col, paintcol, alpha_i);
658                 case PAINT_BLEND_SUB:
659                         return mcol_sub(col, paintcol, alpha_i);
660                 case PAINT_BLEND_MUL:
661                         return mcol_mul(col, paintcol, alpha_i);
662                 case PAINT_BLEND_LIGHTEN:
663                         return mcol_lighten(col, paintcol, alpha_i);
664                 case PAINT_BLEND_DARKEN:
665                         return mcol_darken(col, paintcol, alpha_i);
666                 default:
667                         BLI_assert(0);
668                         return 0;
669         }
670 }
671
672 /* wpaint has 'wpaint_blend' */
673 static unsigned int vpaint_blend(VPaint *vp, unsigned int col, unsigned int colorig, const
674                                  unsigned int paintcol, const int alpha_i,
675                                  /* pre scaled from [0-1] --> [0-255] */
676                                  const int brush_alpha_value_i)
677 {
678         Brush *brush = paint_brush(&vp->paint);
679         const int tool = brush->vertexpaint_tool;
680
681         col = vpaint_blend_tool(tool, col, paintcol, alpha_i);
682
683         /* if no spray, clip color adding with colorig & orig alpha */
684         if((vp->flag & VP_SPRAY)==0) {
685                 unsigned int testcol, a;
686                 char *cp, *ct, *co;
687                 
688                 testcol = vpaint_blend_tool(tool, colorig, paintcol, brush_alpha_value_i);
689                 
690                 cp= (char *)&col;
691                 ct= (char *)&testcol;
692                 co= (char *)&colorig;
693                 
694                 for(a=0; a<4; a++) {
695                         if( ct[a]<co[a] ) {
696                                 if( cp[a]<ct[a] ) cp[a]= ct[a];
697                                 else if( cp[a]>co[a] ) cp[a]= co[a];
698                         }
699                         else {
700                                 if( cp[a]<co[a] ) cp[a]= co[a];
701                                 else if( cp[a]>ct[a] ) cp[a]= ct[a];
702                         }
703                 }
704         }
705
706         return col;
707 }
708
709
710 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
711 {
712         struct ImBuf *ibuf;
713         int a, tot=0, index;
714         
715         /* brecht: disabled this because it obviously failes for
716            brushes with size > 64, why is this here? */
717         /*if(size>64.0) size= 64.0;*/
718         
719         ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
720         if(ibuf) {
721                 unsigned int *rt= ibuf->rect;
722
723                 memset(indexar, 0, sizeof(int)*(totface+1));
724                 
725                 size= ibuf->x*ibuf->y;
726                 while(size--) {
727                                 
728                         if(*rt) {
729                                 index= WM_framebuffer_to_index(*rt);
730                                 if(index>0 && index<=totface)
731                                         indexar[index] = 1;
732                         }
733                 
734                         rt++;
735                 }
736                 
737                 for(a=1; a<=totface; a++) {
738                         if(indexar[a]) indexar[tot++]= a;
739                 }
740
741                 IMB_freeImBuf(ibuf);
742         }
743         
744         return tot;
745 }
746
747 /* whats _dl mean? */
748 static float calc_vp_strength_dl(VPaint *vp, ViewContext *vc, const float *vert_nor,
749                               const float mval[2], const float brush_size_pressure)
750 {
751         Brush *brush = paint_brush(&vp->paint);
752         float dist_squared;
753         float vertco[2], delta[2];
754
755         project_float_noclip(vc->ar, vert_nor, vertco);
756         sub_v2_v2v2(delta, mval, vertco);
757         dist_squared= dot_v2v2(delta, delta); /* len squared */
758         if (dist_squared > brush_size_pressure * brush_size_pressure) {
759                 return 0.0f;
760         }
761         else {
762                 const float dist = sqrtf(dist_squared);
763                 return brush_curve_strength_clamp(brush, dist, brush_size_pressure);
764         }
765 }
766
767 static float calc_vp_alpha_dl(VPaint *vp, ViewContext *vc,
768                               float vpimat[][3], const float *vert_nor,
769                               const float mval[2],
770                               const float brush_size_pressure, const float brush_alpha_pressure)
771 {
772         float strength = calc_vp_strength_dl(vp, vc, vert_nor, mval, brush_size_pressure);
773
774         if (strength > 0.0f) {
775                 float alpha= brush_alpha_pressure * strength;
776
777                 if(vp->flag & VP_NORMALS) {
778                         float dvec[3];
779                         const float *no= vert_nor + 3;
780
781                         /* transpose ! */
782                         dvec[2] = dot_v3v3(vpimat[2], no);
783                         if (dvec[2] > 0.0f) {
784                                 dvec[0] = dot_v3v3(vpimat[0], no);
785                                 dvec[1] = dot_v3v3(vpimat[1], no);
786
787                                 alpha *= dvec[2] / len_v3(dvec);
788                         }
789                         else {
790                                 return 0.0f;
791                         }
792                 }
793
794                 return alpha;
795         }
796
797         return 0.0f;
798 }
799
800 /* vpaint has 'vpaint_blend_tool' */
801 /* result is not clamped from [0-1] */
802 static float wpaint_blend_tool(const int tool,
803                                /* dw->weight */
804                                const float weight,
805                                const float paintval, const float alpha)
806 {
807         switch (tool) {
808                 case PAINT_BLEND_MIX:
809                 case PAINT_BLEND_BLUR:
810                         return (paintval * alpha) + (weight * (1.0f - alpha));
811                 case PAINT_BLEND_ADD:
812                         return (paintval * alpha) + weight;
813                 case PAINT_BLEND_SUB:
814                         return (paintval * alpha) - weight;
815                 case PAINT_BLEND_MUL:
816                         /* first mul, then blend the fac */
817                         return ((1.0f - alpha) + alpha * paintval) * weight;
818                 case PAINT_BLEND_LIGHTEN:
819                         return (weight < paintval) ? (paintval * alpha) + (weight * (1.0f - alpha)) : weight;
820                 case PAINT_BLEND_DARKEN:
821                         return (weight > paintval) ? (paintval * alpha) + (weight * (1.0f - alpha)) : weight;
822                 default:
823                         BLI_assert(0);
824                         return 0.0f;
825         }
826 }
827
828 /* vpaint has 'vpaint_blend' */
829 static float wpaint_blend(VPaint *wp, float weight, float weight_prev,
830                           const float alpha, float paintval,
831                           const float brush_alpha_value,
832                           const short do_flip, const short do_multipaint_totsel)
833 {
834         Brush *brush = paint_brush(&wp->paint);
835         int tool = brush->vertexpaint_tool;
836
837         if (do_flip) {
838                 switch(tool) {
839                         case PAINT_BLEND_MIX:
840                                 paintval = 1.f - paintval; break;
841                         case PAINT_BLEND_ADD:
842                                 tool= PAINT_BLEND_SUB; break;
843                         case PAINT_BLEND_SUB:
844                                 tool= PAINT_BLEND_ADD; break;
845                         case PAINT_BLEND_LIGHTEN:
846                                 tool= PAINT_BLEND_DARKEN; break;
847                         case PAINT_BLEND_DARKEN:
848                                 tool= PAINT_BLEND_LIGHTEN; break;
849                 }
850         }
851         
852         weight = wpaint_blend_tool(tool, weight, paintval, alpha);
853
854         /* delay clamping until the end so multi-paint can function when the active group is at the limits */
855         if(do_multipaint_totsel == FALSE) {
856                 CLAMP(weight, 0.0f, 1.0f);
857         }
858         
859         /* if no spray, clip result with orig weight & orig alpha */
860         if ((wp->flag & VP_SPRAY) == 0) {
861                 if(do_multipaint_totsel == FALSE) {
862                         float testw = wpaint_blend_tool(tool, weight_prev, paintval, brush_alpha_value);
863
864                         CLAMP(testw, 0.0f, 1.0f);
865                         if (testw < weight_prev) {
866                                 if(weight < testw) weight = testw;
867                                 else if(weight > weight_prev) weight = weight_prev;
868                         }
869                         else {
870                                 if (weight > testw) weight = testw;
871                                 else if (weight < weight_prev) weight = weight_prev;
872                         }
873                 }
874         }
875
876         return weight;
877 }
878
879 /* ----------------------------------------------------- */
880
881
882 /* sets wp->weight to the closest weight value to vertex */
883 /* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
884 static int weight_sample_invoke(bContext *C, wmOperator *op, wmEvent *event)
885 {
886         ViewContext vc;
887         Mesh *me;
888         short change= FALSE;
889
890         view3d_set_viewcontext(C, &vc);
891         me= get_mesh(vc.obact);
892
893         if (me && me->dvert && vc.v3d && vc.rv3d) {
894                 int index;
895
896                 view3d_operator_needs_opengl(C);
897
898                 index= view3d_sample_backbuf(&vc, event->mval[0], event->mval[1]);
899
900                 if(index && index<=me->totface) {
901                         DerivedMesh *dm= mesh_get_derived_final(vc.scene, vc.obact, CD_MASK_BAREMESH);
902
903                         if(dm->getVertCo==NULL) {
904                                 BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
905                         }
906                         else {
907                                 MFace *mf= ((MFace *)me->mface) + index-1;
908                                 const int vgroup_active= vc.obact->actdef - 1;
909                                 ToolSettings *ts= vc.scene->toolsettings;
910                                 float mval_f[2];
911                                 int v_idx_best= -1;
912                                 int fidx;
913                                 float len_best= FLT_MAX;
914
915                                 mval_f[0]= (float)event->mval[0];
916                                 mval_f[1]= (float)event->mval[1];
917
918                                 fidx= mf->v4 ? 3:2;
919                                 do {
920                                         float co[3], sco[3], len;
921                                         const int v_idx= (*(&mf->v1 + fidx));
922                                         dm->getVertCo(dm, v_idx, co);
923                                         project_float_noclip(vc.ar, co, sco);
924                                         len= len_squared_v2v2(mval_f, sco);
925                                         if(len < len_best) {
926                                                 len_best= len;
927                                                 v_idx_best= v_idx;
928                                         }
929                                 } while (fidx--);
930
931                                 if(v_idx_best != -1) { /* should always be valid */
932                                         ts->vgroup_weight= defvert_find_weight(&me->dvert[v_idx_best], vgroup_active);
933                                         change= TRUE;
934                                 }
935                         }
936                         dm->release(dm);
937                 }
938         }
939
940         if(change) {
941                 /* not really correct since the brush didnt change, but redraws the toolbar */
942                 WM_main_add_notifier(NC_BRUSH|NA_EDITED, NULL); /* ts->wpaint->paint.brush */
943
944                 return OPERATOR_FINISHED;
945         }
946         else {
947                 return OPERATOR_CANCELLED;
948         }
949 }
950
951 void PAINT_OT_weight_sample(wmOperatorType *ot)
952 {
953         /* identifiers */
954         ot->name= "Weight Paint Sample Weight";
955         ot->idname= "PAINT_OT_weight_sample";
956
957         /* api callbacks */
958         ot->invoke= weight_sample_invoke;
959         ot->poll= weight_paint_mode_poll;
960
961         /* flags */
962         ot->flag= OPTYPE_UNDO;
963 }
964
965 /* samples cursor location, and gives menu with vertex groups to activate */
966 static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
967 {
968         if (C) {
969                 wmWindow *win= CTX_wm_window(C);
970                 if(win && win->eventstate) {
971                         ViewContext vc;
972                         Mesh *me;
973
974                         view3d_set_viewcontext(C, &vc);
975                         me= get_mesh(vc.obact);
976
977                         if (me && me->dvert && vc.v3d && vc.rv3d) {
978                                 int index;
979
980                                 view3d_operator_needs_opengl(C);
981
982                                 index= view3d_sample_backbuf(&vc, win->eventstate->x - vc.ar->winrct.xmin, win->eventstate->y - vc.ar->winrct.ymin);
983
984                                 if(index && index<=me->totface) {
985                                         const int defbase_tot= BLI_countlist(&vc.obact->defbase);
986                                         if(defbase_tot) {
987                                                 MFace *mf= ((MFace *)me->mface) + index-1;
988                                                 unsigned int fidx= mf->v4 ? 3:2;
989                                                 int *groups= MEM_callocN(defbase_tot*sizeof(int), "groups");
990                                                 int found= FALSE;
991
992                                                 do {
993                                                         MDeformVert *dvert= me->dvert + (*(&mf->v1 + fidx));
994                                                         int i= dvert->totweight;
995                                                         MDeformWeight *dw;
996                                                         for(dw= dvert->dw; i > 0; dw++, i--) {
997                                                                 if (dw->def_nr < defbase_tot) {
998                                                                         groups[dw->def_nr]= TRUE;
999                                                                         found= TRUE;
1000                                                                 }
1001                                                         }
1002                                                 } while (fidx--);
1003
1004                                                 if(found==FALSE) {
1005                                                         MEM_freeN(groups);
1006                                                 }
1007                                                 else {
1008                                                         EnumPropertyItem *item= NULL, item_tmp= {0};
1009                                                         int totitem= 0;
1010                                                         int i= 0;
1011                                                         bDeformGroup *dg;
1012                                                         for(dg= vc.obact->defbase.first; dg && i<defbase_tot; i++, dg= dg->next) {
1013                                                                 if(groups[i]) {
1014                                                                         item_tmp.identifier= item_tmp.name= dg->name;
1015                                                                         item_tmp.value= i;
1016                                                                         RNA_enum_item_add(&item, &totitem, &item_tmp);
1017                                                                 }
1018                                                         }
1019
1020                                                         RNA_enum_item_end(&item, &totitem);
1021                                                         *free= 1;
1022
1023                                                         MEM_freeN(groups);
1024                                                         return item;
1025                                                 }
1026                                         }
1027                                 }
1028                         }
1029                 }
1030         }
1031
1032         return DummyRNA_NULL_items;
1033 }
1034
1035 static int weight_sample_group_exec(bContext *C, wmOperator *op)
1036 {
1037         int type= RNA_enum_get(op->ptr, "group");
1038         ViewContext vc;
1039         view3d_set_viewcontext(C, &vc);
1040
1041         BLI_assert(type + 1 >= 0);
1042         vc.obact->actdef= type + 1;
1043
1044         DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA);
1045         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, vc.obact);
1046         return OPERATOR_FINISHED;
1047 }
1048
1049 /* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */
1050 void PAINT_OT_weight_sample_group(wmOperatorType *ot)
1051 {
1052         PropertyRNA *prop= NULL;
1053
1054         /* identifiers */
1055         ot->name= "Weight Paint Sample Group";
1056         ot->idname= "PAINT_OT_weight_sample_group";
1057
1058         /* api callbacks */
1059         ot->exec= weight_sample_group_exec;
1060         ot->invoke= WM_menu_invoke;
1061         ot->poll= weight_paint_mode_poll;
1062
1063         /* flags */
1064         ot->flag= OPTYPE_UNDO;
1065
1066         /* keyingset to use (dynamic enum) */
1067         prop= RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
1068         RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf);
1069         ot->prop= prop;
1070 }
1071
1072 static void do_weight_paint_normalize_all(MDeformVert *dvert, const int defbase_tot, const char *vgroup_validmap)
1073 {
1074         float sum= 0.0f, fac;
1075         unsigned int i, tot=0;
1076         MDeformWeight *dw;
1077
1078         for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1079                 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1080                         tot++;
1081                         sum += dw->weight;
1082                 }
1083         }
1084
1085         if ((tot == 0) || (sum == 1.0f)) {
1086                 return;
1087         }
1088
1089         if (sum != 0.0f) {
1090                 fac= 1.0f / sum;
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                                         dw->weight *= fac;
1095                         }
1096                 }
1097         }
1098         else {
1099                 /* hrmf, not a factor in this case */
1100                 fac = 1.0f / tot;
1101
1102                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1103                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1104                                 dw->weight = fac;
1105                         }
1106                 }
1107         }
1108 }
1109
1110 /* same as function above except it normalizes against the active vgroup which remains unchanged
1111  *
1112  * note that the active is just the group which is unchanged, it can be any,
1113  * can also be -1 to normalize all but in that case call 'do_weight_paint_normalize_all' */
1114 static void do_weight_paint_normalize_all_active(MDeformVert *dvert, const int defbase_tot, const char *vgroup_validmap,
1115                                                  const int vgroup_active)
1116 {
1117         float sum= 0.0f, fac;
1118         unsigned int i, tot=0;
1119         MDeformWeight *dw;
1120         float act_weight = 0.0f;
1121
1122         for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1123                 if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1124                         if (dw->def_nr != vgroup_active) {
1125                                 sum += dw->weight;
1126                                 tot++;
1127                         }
1128                         else {
1129                                 act_weight = dw->weight;
1130                         }
1131                 }
1132         }
1133
1134         if ((tot == 0) || (sum + act_weight == 1.0f)) {
1135                 return;
1136         }
1137
1138         if (sum != 0.0f) {
1139                 fac = (1.0f / sum) * (1.0f - act_weight);
1140
1141                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1142                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1143                                 if (dw->def_nr != vgroup_active) {
1144                                         dw->weight *= fac;
1145
1146                                         /* paranoid but possibly with float error */
1147                                         CLAMP(dw->weight, 0.0f, 1.0f);
1148                                 }
1149                         }
1150                 }
1151         }
1152         else {
1153                 /* corner case where we need to scale all weights evenly because they're all zero */
1154
1155                 /* hrmf, not a factor in this case */
1156                 fac = (1.0f - act_weight) / tot;
1157
1158                 /* paranoid but possibly with float error */
1159                 CLAMP(fac, 0.0f, 1.0f);
1160
1161                 for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1162                         if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
1163                                 if (dw->def_nr != vgroup_active) {
1164                                         dw->weight = fac;
1165                                 }
1166                         }
1167                 }
1168         }
1169 }
1170
1171 /*
1172 See if the current deform vertex has a locked group
1173 */
1174 static char has_locked_group(MDeformVert *dvert, const int defbase_tot,
1175                              const char *bone_groups, const char *lock_flags)
1176 {
1177         int i;
1178         MDeformWeight *dw;
1179
1180         for (i= dvert->totweight, dw= dvert->dw; i != 0; i--, dw++) {
1181                 if (dw->def_nr < defbase_tot) {
1182                         if (bone_groups[dw->def_nr] && lock_flags[dw->def_nr] && dw->weight > 0.0f) {
1183                                 return TRUE;
1184                         }
1185                 }
1186         }
1187         return FALSE;
1188 }
1189 /* 
1190  * gen_lck_flags gets the status of "flag" for each bDeformGroup
1191  *in ob->defbase and returns an array containing them
1192  */
1193 static char *gen_lock_flags(Object* ob, int defbase_tot)
1194 {
1195         char is_locked = FALSE;
1196         int i;
1197         //int defbase_tot = BLI_countlist(&ob->defbase);
1198         char *lock_flags = MEM_mallocN(defbase_tot*sizeof(char), "defflags");
1199         bDeformGroup *defgroup;
1200
1201         for(i = 0, defgroup = ob->defbase.first; i < defbase_tot && defgroup; defgroup = defgroup->next, i++) {
1202                 lock_flags[i] = ((defgroup->flag & DG_LOCK_WEIGHT) != 0);
1203                 is_locked |= lock_flags[i];
1204         }
1205         if (is_locked) {
1206                 return lock_flags;
1207         }
1208
1209         MEM_freeN(lock_flags);
1210         return NULL;
1211 }
1212
1213 static int has_locked_group_selected(int defbase_tot, const char *defbase_sel, const char *lock_flags)
1214 {
1215         int i;
1216         for(i = 0; i < defbase_tot; i++) {
1217                 if(defbase_sel[i] && lock_flags[i]) {
1218                         return TRUE;
1219                 }
1220         }
1221         return FALSE;
1222 }
1223
1224
1225 #if 0 /* UNUSED */
1226 static int has_unselected_unlocked_bone_group(int defbase_tot, char *defbase_sel, int selected, char *lock_flags, char *vgroup_validmap)
1227 {
1228         int i;
1229         if(defbase_tot == selected) {
1230                 return FALSE;
1231         }
1232         for(i = 0; i < defbase_tot; i++) {
1233                 if(vgroup_validmap[i] && !defbase_sel[i] && !lock_flags[i]) {
1234                         return TRUE;
1235                 }
1236         }
1237         return FALSE;
1238 }
1239 #endif
1240
1241
1242 static void multipaint_selection(MDeformVert *dvert, const int defbase_tot, float change, const char *defbase_sel)
1243 {
1244         int i;
1245         MDeformWeight *dw;
1246         float val;
1247         /* make sure they are all at most 1 after the change */
1248         for(i = 0; i < defbase_tot; i++) {
1249                 if(defbase_sel[i]) {
1250                         dw = defvert_find_index(dvert, i);
1251                         if(dw && dw->weight) {
1252                                 val = dw->weight * change;
1253                                 if(val > 1) {
1254                                         /* TODO: when the change is reduced, you need to recheck
1255                                          * the earlier values to make sure they are not 0
1256                                          * (precision error) */
1257                                         change = 1.0f/dw->weight;
1258                                 }
1259                                 /* the value should never reach zero while multi-painting if it
1260                                  * was nonzero beforehand */
1261                                 if(val <= 0) {
1262                                         return;
1263                                 }
1264                         }
1265                 }
1266         }
1267         /* apply the valid change */
1268         for(i = 0; i < defbase_tot; i++) {
1269                 if(defbase_sel[i]) {
1270                         dw = defvert_find_index(dvert, i);
1271                         if(dw && dw->weight) {
1272                                 dw->weight = dw->weight * change;
1273                         }
1274                 }
1275         }
1276 }
1277
1278 /* move all change onto valid, unchanged groups.  If there is change left over,
1279  * then return it.
1280  * assumes there are valid groups to shift weight onto */
1281 static float redistribute_change(MDeformVert *ndv, const int defbase_tot,
1282                                  char *change_status, const char change_me, int changeto,
1283                                  float totchange, float total_valid,
1284                                  char do_auto_normalize)
1285 {
1286         float was_change;
1287         float change;
1288         float oldval;
1289         MDeformWeight *ndw;
1290         int i;
1291         do {
1292                 /* assume there is no change until you see one */
1293                 was_change = FALSE;
1294                 /* change each group by the same amount each time */
1295                 change = totchange/total_valid;
1296                 for(i = 0; i < ndv->totweight && total_valid && totchange; i++) {
1297                         ndw = (ndv->dw+i);
1298
1299                         /* ignore anything outside the value range */
1300                         if (ndw->def_nr < defbase_tot) {
1301
1302                                 /* change only the groups with a valid status */
1303                                 if(change_status[ndw->def_nr] == change_me) {
1304                                         oldval = ndw->weight;
1305                                         /* if auto normalize is active, don't worry about upper bounds */
1306                                         if(do_auto_normalize == FALSE && ndw->weight + change > 1) {
1307                                                 totchange -= 1-ndw->weight;
1308                                                 ndw->weight = 1;
1309                                                 /* stop the changes to this group */
1310                                                 change_status[ndw->def_nr] = changeto;
1311                                                 total_valid--;
1312                                         }
1313                                         else if(ndw->weight + change < 0) { /* check the lower bound */
1314                                                 totchange -= ndw->weight;
1315                                                 ndw->weight = 0;
1316                                                 change_status[ndw->def_nr] = changeto;
1317                                                 total_valid--;
1318                                         }
1319                                         else {/* a perfectly valid change occurred to ndw->weight */
1320                                                 totchange -= change;
1321                                                 ndw->weight += change;
1322                                         }
1323                                         /* see if there was a change */
1324                                         if(oldval != ndw->weight) {
1325                                                 was_change = TRUE;
1326                                         }
1327                                 }
1328                         }
1329                 }
1330                 /* don't go again if there was no change, if there is no valid group,
1331                  * or there is no change left */
1332         } while(was_change && total_valid && totchange);
1333         /* left overs */
1334         return totchange;
1335 }
1336 static float get_mp_change(MDeformVert *odv, const int defbase_tot, const char *defbase_sel, float brush_change);
1337 /* observe the changes made to the weights of groups.
1338  * make sure all locked groups on the vertex have the same deformation
1339  * by moving the changes made to groups onto other unlocked groups */
1340 static void enforce_locks(MDeformVert *odv, MDeformVert *ndv,
1341                           const int defbase_tot, const char *defbase_sel,
1342                           const char *lock_flags, const char *vgroup_validmap,
1343                           char do_auto_normalize, char do_multipaint)
1344 {
1345         float totchange = 0.0f;
1346         float totchange_allowed = 0.0f;
1347         float left_over;
1348
1349         int total_valid = 0;
1350         int total_changed = 0;
1351         unsigned int i;
1352         MDeformWeight *ndw;
1353         MDeformWeight *odw;
1354
1355         float changed_sum = 0.0f;
1356
1357         char *change_status;
1358
1359         if(!lock_flags || !has_locked_group(ndv, defbase_tot, vgroup_validmap, lock_flags)) {
1360                 return;
1361         }
1362         /* record if a group was changed, unlocked and not changed, or locked */
1363         change_status = MEM_callocN(sizeof(char)*defbase_tot, "unlocked_unchanged");
1364
1365         for(i = 0; i < defbase_tot; i++) {
1366                 ndw = defvert_find_index(ndv, i);
1367                 odw = defvert_find_index(odv, i);
1368                 /* the weights are zero, so we can assume a lot */
1369                 if(!ndw || !odw) {
1370                         if (!lock_flags[i] && vgroup_validmap[i]) {
1371                                 defvert_verify_index(odv, i);
1372                                 defvert_verify_index(ndv, i);
1373                                 total_valid++;
1374                                 change_status[i] = 1; /* can be altered while redistributing */
1375                         }
1376                         continue;
1377                 }
1378                 /* locked groups should not be changed */
1379                 if(lock_flags[i]) {
1380                         ndw->weight = odw->weight;
1381                 }
1382                 else if(ndw->weight != odw->weight) { /* changed groups are handled here */
1383                         totchange += ndw->weight - odw->weight;
1384                         changed_sum += ndw->weight;
1385                         change_status[i] = 2; /* was altered already */
1386                         total_changed++;
1387                 } /* unchanged, unlocked bone groups are handled here */
1388                 else if (vgroup_validmap[i]) {
1389                         totchange_allowed += ndw->weight;
1390                         total_valid++;
1391                         change_status[i] = 1; /* can be altered while redistributing */
1392                 }
1393         }
1394         /* if there was any change, redistribute it */
1395         if(total_changed) {
1396                 /* auto normalize will allow weights to temporarily go above 1 in redistribution */
1397                 if(vgroup_validmap && total_changed < 0 && total_valid) {
1398                         totchange_allowed = total_valid;
1399                 }
1400                 /* the way you modify the unlocked+unchanged groups is different depending
1401                  * on whether or not you are painting the weight(s) up or down */
1402                 if(totchange < 0) {
1403                         totchange_allowed = total_valid - totchange_allowed;
1404                 }
1405                 else {
1406                         totchange_allowed *= -1;
1407                 }
1408                 /* there needs to be change allowed, or you should not bother */
1409                 if(totchange_allowed) {
1410                         left_over = 0;
1411                         if(fabsf(totchange_allowed) < fabsf(totchange)) {
1412                                 /* this amount goes back onto the changed, unlocked weights */
1413                                 left_over = fabsf(fabsf(totchange) - fabsf(totchange_allowed));
1414                                 if(totchange > 0) {
1415                                         left_over *= -1;
1416                                 }
1417                         }
1418                         else {
1419                                 /* all of the change will be permitted */
1420                                 totchange_allowed = -totchange;
1421                         }
1422                         /* move the weight evenly between the allowed groups, move excess back onto the used groups based on the change */
1423                         totchange_allowed = redistribute_change(ndv, defbase_tot, change_status, 1, -1, totchange_allowed, total_valid, do_auto_normalize);
1424                         left_over += totchange_allowed;
1425                         if(left_over) {
1426                                 /* more than one nonzero weights were changed with the same ratio with multipaint, so keep them changed that way! */
1427                                 if(total_changed > 1 && do_multipaint) {
1428                                         float undo_change = get_mp_change(ndv, defbase_tot, defbase_sel, left_over);
1429                                         multipaint_selection(ndv, defbase_tot, undo_change, defbase_sel);
1430                                 }       
1431                                 /* or designatedw is still -1 put weight back as evenly as possible */
1432                                 else {
1433                                         redistribute_change(ndv, defbase_tot, change_status, 2, -2, left_over, total_changed, do_auto_normalize);
1434                                 }
1435                         }
1436                 }
1437                 else {
1438                         /* reset the weights */
1439                         unsigned int i;
1440                         MDeformWeight *dw_old= odv->dw;
1441                         MDeformWeight *dw_new= ndv->dw;
1442
1443                         for (i= odv->totweight; i != 0; i--, dw_old++, dw_new++) {
1444                                 dw_new->weight= dw_old->weight;
1445                         }
1446                 }
1447         }
1448
1449         MEM_freeN(change_status);
1450 }
1451
1452 /* multi-paint's initial, potential change is computed here based on the user's stroke */
1453 static float get_mp_change(MDeformVert *odv, const int defbase_tot, const char *defbase_sel, float brush_change)
1454 {
1455         float selwsum = 0.0f;
1456         unsigned int i;
1457         MDeformWeight *dw= odv->dw;
1458
1459         for (i= odv->totweight; i != 0; i--, dw++) {
1460                 if (dw->def_nr < defbase_tot) {
1461                         if(defbase_sel[dw->def_nr]) {
1462                                 selwsum += dw->weight;
1463                         }
1464                 }
1465         }
1466         if(selwsum && selwsum+brush_change > 0) {
1467                 return (selwsum+brush_change)/selwsum;
1468         }
1469         return 0.0f;
1470 }
1471
1472 /* change the weights back to the wv's weights
1473  * it assumes you already have the correct pointer index */
1474 static void defvert_reset_to_prev(MDeformVert *dv_prev, MDeformVert *dv)
1475 {
1476         MDeformWeight *dw= dv->dw;
1477         MDeformWeight *dw_prev;
1478         unsigned int i;
1479         for (i= dv->totweight; i != 0; i--, dw++) {
1480                 dw_prev= defvert_find_index(dv_prev, dw->def_nr);
1481                 /* if there was no w when there is a d, then the old weight was 0 */
1482                 dw->weight = dw_prev ? dw_prev->weight : 0.0f;
1483         }
1484 }
1485
1486 static void clamp_weights(MDeformVert *dvert)
1487 {
1488         MDeformWeight *dw= dvert->dw;
1489         unsigned int i;
1490         for (i= dvert->totweight; i != 0; i--, dw++) {
1491                 CLAMP(dw->weight, 0.0f, 1.0f);
1492         }
1493 }
1494
1495 /* struct to avoid passing many args each call to do_weight_paint_vertex()
1496  * this _could_ be made a part of the operators 'WPaintData' struct, or at
1497  * least a member, but for now keep its own struct, initialized on every
1498  * paint stroke update - campbell */
1499 typedef struct WeightPaintInfo {
1500
1501         int defbase_tot;
1502
1503         /* both must add up to 'defbase_tot' */
1504         int defbase_tot_sel;
1505         int defbase_tot_unsel;
1506
1507         int vgroup_active; /* (ob->actdef - 1) */
1508         int vgroup_mirror; /* mirror group or -1 */
1509
1510         const char *lock_flags;  /* boolean array for locked bones,
1511                                   * length of defbase_tot */
1512         const char *defbase_sel; /* boolean array for selected bones,
1513                                   * length of defbase_tot, cant be const because of how its passed */
1514
1515         const char *vgroup_validmap; /* same as WeightPaintData.vgroup_validmap,
1516                                       * only added here for convenience */
1517
1518         char do_flip;
1519         char do_multipaint;
1520         char do_auto_normalize;
1521
1522         float brush_alpha_value;  /* result of brush_alpha() */
1523 } WeightPaintInfo;
1524
1525 /* fresh start to make multi-paint and locking modular */
1526 /* returns TRUE if it thinks you need to reset the weights due to
1527  * normalizing while multi-painting
1528  *
1529  * note: this assumes dw->def_nr range has been checked by the caller
1530  */
1531 static int apply_mp_locks_normalize(Mesh *me, const WeightPaintInfo *wpi,
1532                                     const unsigned int index,
1533                                     MDeformWeight *dw, MDeformWeight *tdw,
1534                                     float change, float oldChange,
1535                                     float oldw, float neww)
1536 {
1537         MDeformVert *dv= &me->dvert[index];
1538         MDeformVert dv_test= {NULL};
1539
1540         dv_test.dw= MEM_dupallocN(dv->dw);
1541         dv_test.flag = dv->flag;
1542         dv_test.totweight = dv->totweight;
1543         /* do not multi-paint if a locked group is selected or the active group is locked
1544          * !lock_flags[dw->def_nr] helps if nothing is selected, but active group is locked */
1545         if ( (wpi->lock_flags == NULL) ||
1546              ((wpi->lock_flags[dw->def_nr] == FALSE) && /* def_nr range has to be checked for by caller */
1547               has_locked_group_selected(wpi->defbase_tot, wpi->defbase_sel, wpi->lock_flags) == FALSE))
1548         {
1549                 if(wpi->do_multipaint && wpi->defbase_tot_sel > 1) {
1550                         if(change && change!=1) {
1551                                 multipaint_selection(dv, wpi->defbase_tot, change, wpi->defbase_sel);
1552                         }
1553                 }
1554                 else { /* this lets users paint normally, but don't let them paint locked groups */
1555                         dw->weight = neww;
1556                 }
1557         }
1558         clamp_weights(dv);
1559
1560         enforce_locks(&dv_test, dv, wpi->defbase_tot, wpi->defbase_sel, wpi->lock_flags, wpi->vgroup_validmap, wpi->do_auto_normalize, wpi->do_multipaint);
1561
1562         if (wpi->do_auto_normalize) {
1563                 /* XXX - should we pass the active group? - currently '-1' */
1564                 do_weight_paint_normalize_all(dv, wpi->defbase_tot, wpi->vgroup_validmap);
1565         }
1566
1567         if(oldChange && wpi->do_multipaint && wpi->defbase_tot_sel > 1) {
1568                 if(tdw->weight != oldw) {
1569                         if(neww > oldw) {
1570                                 if(tdw->weight <= oldw) {
1571                                         MEM_freeN(dv_test.dw);
1572                                         return TRUE;
1573                                 }
1574                         }
1575                         else {
1576                                 if(tdw->weight >= oldw) {
1577                                         MEM_freeN(dv_test.dw);
1578                                         return TRUE;
1579                                 }
1580                         }
1581                 }
1582         }
1583         MEM_freeN(dv_test.dw);
1584         return FALSE;
1585 }
1586
1587 /* within the current dvert index, get the dw that is selected and has a weight
1588  * above 0, this helps multi-paint */
1589 static int get_first_selected_nonzero_weight(MDeformVert *dvert, const int defbase_tot, const char *defbase_sel)
1590 {
1591         int i;
1592         MDeformWeight *dw= dvert->dw;
1593         for(i=0; i< dvert->totweight; i++, dw++) {
1594                 if (dw->def_nr < defbase_tot) {
1595                         if (defbase_sel[dw->def_nr] && dw->weight > 0.0f) {
1596                                 return i;
1597                         }
1598                 }
1599         }
1600         return -1;
1601 }
1602
1603
1604 static char *wpaint_make_validmap(Object *ob);
1605
1606
1607 static void do_weight_paint_vertex(/* vars which remain the same for every vert */
1608                                    VPaint *wp, Object *ob, const WeightPaintInfo *wpi,
1609                                    /* vars which change on each stroke */
1610                                    const unsigned int index, float alpha, float paintweight
1611                                    )
1612 {
1613         Mesh *me= ob->data;
1614         MDeformVert *dv= &me->dvert[index];
1615         
1616         MDeformWeight *dw, *dw_prev;
1617
1618         /* mirror vars */
1619         int index_mirr;
1620         int vgroup_mirr;
1621
1622         MDeformVert *dv_mirr;
1623         MDeformWeight *dw_mirr;
1624
1625         const short do_multipaint_totsel = (wpi->do_multipaint && wpi->defbase_tot_sel > 1);
1626
1627         if(wp->flag & VP_ONLYVGROUP) {
1628                 dw= defvert_find_index(dv, wpi->vgroup_active);
1629                 dw_prev= defvert_find_index(wp->wpaint_prev+index, wpi->vgroup_active);
1630         }
1631         else {
1632                 dw= defvert_verify_index(dv, wpi->vgroup_active);
1633                 dw_prev= defvert_verify_index(wp->wpaint_prev+index, wpi->vgroup_active);
1634         }
1635
1636         if(dw==NULL || dw_prev==NULL) {
1637                 return;
1638         }
1639
1640
1641         /* from now on we can check if mirrors enabled if this var is -1 and not bother with the flag */
1642         if (me->editflag & ME_EDIT_MIRROR_X) {
1643                 index_mirr = mesh_get_x_mirror_vert(ob, index);
1644                 vgroup_mirr = (wpi->vgroup_mirror != -1) ? wpi->vgroup_mirror : wpi->vgroup_active;
1645
1646                 /* another possible error - mirror group _and_ active group are the same (which is fine),
1647                  * but we also are painting onto a center vertex - this would paint the same weight twice */
1648                 if (index_mirr == index && vgroup_mirr == wpi->vgroup_active) {
1649                         index_mirr = vgroup_mirr = -1;
1650                 }
1651         }
1652         else {
1653                 index_mirr = vgroup_mirr = -1;
1654         }
1655
1656
1657         /* get the mirror def vars */
1658         if (index_mirr != -1) {
1659                 dv_mirr = &me->dvert[index_mirr];
1660                 if (wp->flag & VP_ONLYVGROUP) {
1661                         dw_mirr = defvert_find_index(dv_mirr, vgroup_mirr);
1662
1663                         if (dw_mirr == NULL) {
1664                                 index_mirr = vgroup_mirr = -1;
1665                                 dv_mirr = NULL;
1666                         }
1667                 }
1668                 else {
1669                         if (index != index_mirr) {
1670                                 dw_mirr = defvert_verify_index(dv_mirr, vgroup_mirr);
1671                         }
1672                         else {
1673                                 /* dv and dv_mirr are the same */
1674                                 int totweight_prev = dv_mirr->totweight;
1675                                 int dw_offset = (int)(dw - dv_mirr->dw);
1676                                 dw_mirr = defvert_verify_index(dv_mirr, vgroup_mirr);
1677
1678                                 /* if we added another, get our old one back */
1679                                 if (totweight_prev != dv_mirr->totweight) {
1680                                         dw = &dv_mirr->dw[dw_offset];
1681                                 }
1682                         }
1683                 }
1684         }
1685         else {
1686                 dv_mirr = NULL;
1687                 dw_mirr = NULL;
1688         }
1689
1690
1691         /* TODO: De-duplicate the simple weight paint - jason */
1692         /* ... or not, since its <10 SLOC - campbell */
1693
1694         /* If there are no locks or multipaint,
1695          * then there is no need to run the more complicated checks */
1696         if ( (do_multipaint_totsel == FALSE) &&
1697              (wpi->lock_flags == NULL || has_locked_group(dv, wpi->defbase_tot, wpi->vgroup_validmap, wpi->lock_flags) == FALSE))
1698         {
1699                 dw->weight = wpaint_blend(wp, dw->weight, dw_prev->weight, alpha, paintweight,
1700                                           wpi->brush_alpha_value, wpi->do_flip, FALSE);
1701
1702                 /* WATCH IT: take care of the ordering of applying mirror -> normalize,
1703                  * can give wrong results [#26193], least confusing if normalize is done last */
1704
1705                 /* apply mirror */
1706                 if(index_mirr != -1) {
1707                         /* copy, not paint again */
1708                         dw_mirr->weight = dw->weight;
1709                 }
1710
1711                 /* apply normalize */
1712                 if (wpi->do_auto_normalize) {
1713                         /* note on normalize - this used to be applied after painting and normalize all weights,
1714                          * in some ways this is good because there is feedback where the more weights involved would
1715                          * 'risist' so you couldn't instantly zero out other weights by painting 1.0 on the active.
1716                          *
1717                          * However this gave a problem since applying mirror, then normalize both verts
1718                          * the resulting weight wont match on both sides.
1719                          *
1720                          * If this 'resisting', slower normalize is nicer, we could call
1721                          * do_weight_paint_normalize_all() and only use...
1722                          * do_weight_paint_normalize_all_active() when normalizing the mirror vertex.
1723                          * - campbell
1724                          */
1725                         do_weight_paint_normalize_all_active(dv, wpi->defbase_tot, wpi->vgroup_validmap, wpi->vgroup_active);
1726
1727                         if (index_mirr != -1) {
1728                                 /* only normalize if this is not a center vertex, else we get a conflict, normalizing twice */
1729                                 if (index != index_mirr) {
1730                                         do_weight_paint_normalize_all_active(dv_mirr, wpi->defbase_tot, wpi->vgroup_validmap, vgroup_mirr);
1731                                 }
1732                                 else {
1733                                         /* this case accounts for...
1734                                          * - painting onto a center vertex of a mesh
1735                                          * - x mirror is enabled
1736                                          * - auto normalize is enabled
1737                                          * - the group you are painting onto has a L / R version
1738                                          *
1739                                          * We wan't L/R vgroups to have the same weight but this cant be if both are over 0.5,
1740                                          * We _could_ have special check for that, but this would need its own normalize function which
1741                                          * holds 2 groups from changing at once.
1742                                          *
1743                                          * So! just balance out the 2 weights, it keeps them equal and everything normalized.
1744                                          *
1745                                          * While it wont hit the desired weight immediatelty as the user waggles their mouse,
1746                                          * constant painting and re-normalizing will get there. this is also just simpler logic.
1747                                          * - campbell */
1748                                         dw_mirr->weight = dw->weight = (dw_mirr->weight + dw->weight) * 0.5f;
1749                                 }
1750                         }
1751                 }
1752         }
1753         else {
1754                 /* use locks and/or multipaint */
1755                 float oldw;
1756                 float neww;
1757                 float testw=0;
1758                 float change = 0;
1759                 float oldChange = 0;
1760                 int i;
1761                 MDeformWeight *tdw = NULL, *tdw_prev;
1762                 MDeformVert dv_copy= {NULL};
1763
1764                 oldw = dw->weight;
1765                 neww = wpaint_blend(wp, dw->weight, dw_prev->weight, alpha, paintweight,
1766                                     wpi->brush_alpha_value, wpi->do_flip, do_multipaint_totsel);
1767                 
1768                 /* setup multi-paint */
1769                 if (do_multipaint_totsel) {
1770                         dv_copy.dw= MEM_dupallocN(dv->dw);
1771                         dv_copy.flag = dv->flag;
1772                         dv_copy.totweight = dv->totweight;
1773                         tdw = dw;
1774                         tdw_prev = dw_prev;
1775                         change = get_mp_change(&wp->wpaint_prev[index], wpi->defbase_tot, wpi->defbase_sel, neww - oldw);
1776                         if(change) {
1777                                 if(!tdw->weight) {
1778                                         i = get_first_selected_nonzero_weight(dv, wpi->defbase_tot, wpi->defbase_sel);
1779                                         if(i>=0) {
1780                                                 tdw = &(dv->dw[i]);
1781                                                 tdw_prev = defvert_verify_index(&wp->wpaint_prev[index], tdw->def_nr);
1782                                         }
1783                                         else {
1784                                                 change = 0;
1785                                         }
1786                                 }
1787                                 if(change && tdw_prev->weight && tdw_prev->weight * change) {
1788                                         if(tdw->weight != tdw_prev->weight) {
1789                                                 oldChange = tdw->weight/tdw_prev->weight;
1790                                                 testw = tdw_prev->weight*change;
1791                                                 if( testw > tdw_prev->weight ) {
1792                                                         if(change > oldChange) {
1793                                                                 /* reset the weights and use the new change */
1794                                                                 defvert_reset_to_prev(wp->wpaint_prev+index, dv);
1795                                                         }
1796                                                         else {
1797                                                                 /* the old change was more significant, so set
1798                                                                  * the change to 0 so that it will not do another multi-paint */
1799                                                                 change = 0;
1800                                                         }
1801                                                 }
1802                                                 else {
1803                                                         if(change < oldChange) {
1804                                                                 defvert_reset_to_prev(wp->wpaint_prev+index, dv);
1805                                                         }
1806                                                         else {
1807                                                                 change = 0;
1808                                                         }
1809                                                 }
1810                                         }
1811                                 }
1812                                 else {
1813                                         change = 0;
1814                                 }
1815                         }
1816                 }
1817                 
1818                 if(apply_mp_locks_normalize(me, wpi, index, dw, tdw, change, oldChange, oldw, neww)) {
1819                         defvert_reset_to_prev(&dv_copy, dv);
1820                         change = 0;
1821                         oldChange = 0;
1822                 }
1823                 if(dv_copy.dw) {
1824                         MEM_freeN(dv_copy.dw);
1825                 }
1826 #if 0
1827                 /* dv may have been altered greatly */
1828                 dw = defvert_find_index(dv, vgroup);
1829 #else
1830                 dw = NULL; /* UNUSED after assignment, set to NULL to ensuyre we don't
1831                                 * use again, we thats needed un-ifdef the line above */
1832                 (void)dw;  /* quiet warnigns */
1833 #endif
1834
1835                 /* x mirror painting */
1836                 if(index_mirr != -1) {
1837                         /* copy, not paint again */
1838
1839                         /* dw_mirr->weight = dw->weight; */  /* TODO, explain the logic in not assigning weight! - campbell */
1840                         apply_mp_locks_normalize(me, wpi, index_mirr, dw_mirr, tdw, change, oldChange, oldw, neww);
1841                 }
1842         }
1843 }
1844
1845
1846 /* *************** set wpaint operator ****************** */
1847
1848 static int set_wpaint(bContext *C, wmOperator *UNUSED(op))              /* toggle */
1849 {               
1850         Object *ob= CTX_data_active_object(C);
1851         Scene *scene= CTX_data_scene(C);
1852         VPaint *wp= scene->toolsettings->wpaint;
1853         Mesh *me;
1854         
1855         me= get_mesh(ob);
1856         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1857         
1858         if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
1859         else ob->mode |= OB_MODE_WEIGHT_PAINT;
1860         
1861         
1862         /* Weightpaint works by overriding colors in mesh,
1863                 * so need to make sure we recalc on enter and
1864                 * exit (exit needs doing regardless because we
1865                                 * should redeform).
1866                 */
1867         DAG_id_tag_update(&me->id, 0);
1868         
1869         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1870                 Object *par;
1871                 
1872                 if(wp==NULL)
1873                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1874
1875                 paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
1876                 paint_cursor_start(C, weight_paint_poll);
1877                 
1878                 mesh_octree_table(ob, NULL, NULL, 's');
1879                 
1880                 /* verify if active weight group is also active bone */
1881                 par= modifiers_isDeformedByArmature(ob);
1882                 if(par && (par->mode & OB_MODE_POSE)) {
1883                         bArmature *arm= par->data;
1884
1885                         if(arm->act_bone)
1886                                 ED_vgroup_select_by_name(ob, arm->act_bone->name);
1887                 }
1888         }
1889         else {
1890                 mesh_octree_table(NULL, NULL, NULL, 'e');
1891                 mesh_mirrtopo_table(NULL, 'e');
1892         }
1893         
1894         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1895         
1896         return OPERATOR_FINISHED;
1897 }
1898
1899 /* for switching to/from mode */
1900 static int paint_poll_test(bContext *C)
1901 {
1902         Object *ob= CTX_data_active_object(C);
1903         if(CTX_data_edit_object(C))
1904                 return 0;
1905         if(CTX_data_active_object(C)==NULL)
1906                 return 0;
1907         if(!ob->data || ((ID *)ob->data)->lib)
1908                 return 0;
1909         return 1;
1910 }
1911
1912 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1913 {
1914         
1915         /* identifiers */
1916         ot->name= "Weight Paint Mode";
1917         ot->idname= "PAINT_OT_weight_paint_toggle";
1918         
1919         /* api callbacks */
1920         ot->exec= set_wpaint;
1921         ot->poll= paint_poll_test;
1922         
1923         /* flags */
1924         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1925         
1926 }
1927
1928 /* ************ weight paint operator ********** */
1929
1930 struct WPaintData {
1931         ViewContext vc;
1932         int *indexar;
1933         int vgroup_active;
1934         int vgroup_mirror;
1935         float *vertexcosnos;
1936         float wpimat[3][3];
1937         
1938         /*variables for auto normalize*/
1939         const char *vgroup_validmap; /*stores if vgroups tie to deforming bones or not*/
1940         const char *lock_flags;
1941         int defbase_tot;
1942 };
1943
1944 static char *wpaint_make_validmap(Object *ob)
1945 {
1946         bDeformGroup *dg;
1947         ModifierData *md;
1948         char *vgroup_validmap;
1949         GHash *gh;
1950         int i, step1=1;
1951
1952         if(ob->defbase.first == NULL) {
1953                 return NULL;
1954         }
1955
1956         gh= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
1957
1958         /*add all names to a hash table*/
1959         for (dg=ob->defbase.first; dg; dg=dg->next) {
1960                 BLI_ghash_insert(gh, dg->name, NULL);
1961         }
1962
1963         /*now loop through the armature modifiers and identify deform bones*/
1964         for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
1965                 if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
1966                         continue;
1967
1968                 if (md->type == eModifierType_Armature) {
1969                         ArmatureModifierData *amd= (ArmatureModifierData*) md;
1970
1971                         if(amd->object && amd->object->pose) {
1972                                 bPose *pose= amd->object->pose;
1973                                 bPoseChannel *chan;
1974                                 
1975                                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1976                                         if (chan->bone->flag & BONE_NO_DEFORM)
1977                                                 continue;
1978
1979                                         if (BLI_ghash_haskey(gh, chan->name)) {
1980                                                 BLI_ghash_remove(gh, chan->name, NULL, NULL);
1981                                                 BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
1982                                         }
1983                                 }
1984                         }
1985                 }
1986         }
1987
1988         vgroup_validmap= MEM_mallocN(BLI_ghash_size(gh), "wpaint valid map");
1989
1990         /*add all names to a hash table*/
1991         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1992                 vgroup_validmap[i]= (BLI_ghash_lookup(gh, dg->name) != NULL);
1993         }
1994
1995         BLI_assert(i == BLI_ghash_size(gh));
1996
1997         BLI_ghash_free(gh, NULL, NULL);
1998
1999         return vgroup_validmap;
2000 }
2001
2002 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2003 {
2004         Scene *scene= CTX_data_scene(C);
2005         struct PaintStroke *stroke = op->customdata;
2006         ToolSettings *ts= scene->toolsettings;
2007         VPaint *wp= ts->wpaint;
2008         Object *ob= CTX_data_active_object(C);
2009         struct WPaintData *wpd;
2010         Mesh *me;
2011         bDeformGroup *dg;
2012
2013         float mat[4][4], imat[4][4];
2014         
2015         if(scene->obedit) {
2016                 return FALSE;
2017         }
2018         
2019         me= get_mesh(ob);
2020         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
2021         
2022         /* if nothing was added yet, we make dverts and a vertex deform group */
2023         if (!me->dvert) {
2024                 ED_vgroup_data_create(&me->id);
2025                 WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
2026         }
2027
2028         /* this happens on a Bone select, when no vgroup existed yet */
2029         if (ob->actdef <= 0) {
2030                 Object *modob;
2031                 if((modob = modifiers_isDeformedByArmature(ob))) {
2032                         Bone *actbone= ((bArmature *)modob->data)->act_bone;
2033                         if(actbone) {
2034                                 bPoseChannel *pchan= get_pose_channel(modob->pose, actbone->name);
2035
2036                                 if(pchan) {
2037                                         bDeformGroup *dg= defgroup_find_name(ob, pchan->name);
2038                                         if(dg==NULL) {
2039                                                 dg= ED_vgroup_add_name(ob, pchan->name);        /* sets actdef */
2040                                         }
2041                                         else {
2042                                                 int actdef = 1 + BLI_findindex(&ob->defbase, dg);
2043                                                 BLI_assert(actdef >= 0);
2044                                                 ob->actdef= actdef;
2045                                         }
2046                                 }
2047                         }
2048                 }
2049         }
2050         if(ob->defbase.first==NULL) {
2051                 ED_vgroup_add(ob);
2052         }
2053
2054         /* ensure we dont try paint onto an invalid group */
2055         if (ob->actdef <= 0) {
2056                 BKE_report(op->reports, RPT_WARNING, "No active vertex group for painting, aborting");
2057                 return FALSE;
2058         }
2059
2060         /* check if we are attempting to paint onto a locked vertex group,
2061          * and other options disallow it from doing anything useful */
2062         dg = BLI_findlink(&ob->defbase, (ob->actdef-1));
2063         if (dg->flag & DG_LOCK_WEIGHT) {
2064                 BKE_report(op->reports, RPT_WARNING, "Active group is locked, aborting");
2065                 return FALSE;
2066         }
2067
2068         /* ALLOCATIONS! no return after this line */
2069         /* make mode data storage */
2070         wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
2071         paint_stroke_set_mode_data(stroke, wpd);
2072         view3d_set_viewcontext(C, &wpd->vc);
2073
2074         wpd->vgroup_active = ob->actdef - 1;
2075         wpd->vgroup_mirror = -1;
2076
2077         /* set up auto-normalize, and generate map for detecting which
2078          * vgroups affect deform bones*/
2079         wpd->defbase_tot = BLI_countlist(&ob->defbase);
2080         wpd->lock_flags = gen_lock_flags(ob, wpd->defbase_tot);
2081         if (ts->auto_normalize || ts->multipaint || wpd->lock_flags) {
2082                 wpd->vgroup_validmap = wpaint_make_validmap(ob);
2083         }
2084
2085         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
2086         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
2087         wpd->indexar= get_indexarray(me);
2088         copy_wpaint_prev(wp, me->dvert, me->totvert);
2089
2090         /* imat for normals */
2091         mult_m4_m4m4(mat, wpd->vc.rv3d->viewmat, ob->obmat);
2092         invert_m4_m4(imat, mat);
2093         copy_m3_m4(wpd->wpimat, imat);
2094
2095         /* if mirror painting, find the other group */
2096         if(me->editflag & ME_EDIT_MIRROR_X) {
2097                 wpd->vgroup_mirror = wpaint_mirror_vgroup_ensure(ob, wpd->vgroup_active);
2098         }
2099         
2100         return TRUE;
2101 }
2102
2103 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
2104 {
2105         Scene *scene= CTX_data_scene(C);
2106         ToolSettings *ts= CTX_data_tool_settings(C);
2107         VPaint *wp= ts->wpaint;
2108         Brush *brush = paint_brush(&wp->paint);
2109         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
2110         ViewContext *vc;
2111         Object *ob;
2112         Mesh *me;
2113         float mat[4][4];
2114         float paintweight;
2115         int *indexar;
2116         float totw;
2117         unsigned int index, totindex;
2118         float alpha;
2119         float mval[2];
2120         int use_vert_sel;
2121         char *defbase_sel;
2122
2123         const float pressure = RNA_float_get(itemptr, "pressure");
2124         const float brush_size_pressure = brush_size(scene, brush) * (brush_use_size_pressure(scene, brush) ? pressure : 1.0f);
2125         const float brush_alpha_value = brush_alpha(scene, brush);
2126         const float brush_alpha_pressure = brush_alpha_value * (brush_use_alpha_pressure(scene, brush) ? pressure : 1.0f);
2127
2128         /* intentionally dont initialize as NULL, make sure we initialize all members below */
2129         WeightPaintInfo wpi;
2130
2131         /* cannot paint if there is no stroke data */
2132         if (wpd == NULL) {
2133                 /* XXX: force a redraw here, since even though we can't paint,
2134                  * at least view won't freeze until stroke ends */
2135                 ED_region_tag_redraw(CTX_wm_region(C));
2136                 return;
2137         }
2138                 
2139         vc= &wpd->vc;
2140         ob= vc->obact;
2141         me= ob->data;
2142         indexar= wpd->indexar;
2143         
2144         view3d_operator_needs_opengl(C);
2145                 
2146         /* load projection matrix */
2147         mult_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat);
2148
2149         RNA_float_get_array(itemptr, "mouse", mval);
2150         mval[0]-= vc->ar->winrct.xmin;
2151         mval[1]-= vc->ar->winrct.ymin;
2152
2153
2154
2155         /* *** setup WeightPaintInfo - pass onto do_weight_paint_vertex *** */
2156         wpi.defbase_tot=        wpd->defbase_tot;
2157         defbase_sel=            MEM_mallocN(wpi.defbase_tot*sizeof(char), "wpi.defbase_sel");
2158         wpi.defbase_tot_sel=    get_selected_defgroups(ob, defbase_sel, wpi.defbase_tot);
2159         wpi.defbase_sel=        defbase_sel; /* so we can stay const */
2160         if(wpi.defbase_tot_sel == 0 && ob->actdef > 0) wpi.defbase_tot_sel = 1;
2161
2162         wpi.defbase_tot_unsel=  wpi.defbase_tot - wpi.defbase_tot_sel;
2163         wpi.vgroup_active=      wpd->vgroup_active;
2164         wpi.vgroup_mirror=      wpd->vgroup_mirror;
2165         wpi.lock_flags=         wpd->lock_flags;
2166         wpi.vgroup_validmap=    wpd->vgroup_validmap;
2167         wpi.do_flip=            RNA_boolean_get(itemptr, "pen_flip");
2168         wpi.do_multipaint=      (ts->multipaint != 0);
2169         wpi.do_auto_normalize=  ((ts->auto_normalize != 0) && (wpi.vgroup_validmap != NULL));
2170         wpi.brush_alpha_value=  brush_alpha_value;
2171         /* *** done setting up WeightPaintInfo *** */
2172
2173
2174
2175         swap_m4m4(wpd->vc.rv3d->persmat, mat);
2176
2177         use_vert_sel= (me->editflag & ME_EDIT_VERT_SEL) != 0;
2178
2179         /* which faces are involved */
2180         if(wp->flag & VP_AREA) {
2181                 /* Ugly hack, to avoid drawing vertex index when getting the face index buffer - campbell */
2182                 me->editflag &= ~ME_EDIT_VERT_SEL;
2183                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size_pressure);
2184                 me->editflag |= use_vert_sel ? ME_EDIT_VERT_SEL : 0;
2185         }
2186         else {
2187                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
2188                 if(indexar[0]) totindex= 1;
2189                 else totindex= 0;
2190         }
2191                         
2192         if(wp->flag & VP_COLINDEX) {
2193                 for(index=0; index<totindex; index++) {
2194                         if(indexar[index] && indexar[index]<=me->totface) {
2195                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
2196                                                 
2197                                 if(mface->mat_nr!=ob->actcol-1) {
2198                                         indexar[index]= 0;
2199                                 }
2200                         }
2201                 }
2202         }
2203                         
2204         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
2205                 for(index=0; index<totindex; index++) {
2206                         if(indexar[index] && indexar[index]<=me->totface) {
2207                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
2208                                                 
2209                                 if((mface->flag & ME_FACE_SEL)==0) {
2210                                         indexar[index]= 0;
2211                                 }
2212                         }                                       
2213                 }
2214         }
2215                         
2216         /* make sure each vertex gets treated only once */
2217         /* and calculate filter weight */
2218         totw= 0.0f;
2219         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR)
2220                 paintweight= 0.0f;
2221         else
2222                 paintweight= ts->vgroup_weight;
2223                         
2224         for(index=0; index<totindex; index++) {
2225                 if(indexar[index] && indexar[index]<=me->totface) {
2226                         MFace *mface= me->mface + (indexar[index]-1);
2227
2228                         if(use_vert_sel) {
2229                                 me->dvert[mface->v1].flag = (me->mvert[mface->v1].flag & SELECT);
2230                                 me->dvert[mface->v2].flag = (me->mvert[mface->v2].flag & SELECT);
2231                                 me->dvert[mface->v3].flag = (me->mvert[mface->v3].flag & SELECT);
2232                                 if(mface->v4) me->dvert[mface->v4].flag = (me->mvert[mface->v4].flag & SELECT);
2233                         }
2234                         else {
2235                                 me->dvert[mface->v1].flag= 1;
2236                                 me->dvert[mface->v2].flag= 1;
2237                                 me->dvert[mface->v3].flag= 1;
2238                                 if(mface->v4) me->dvert[mface->v4].flag= 1;
2239                         }
2240                                         
2241                         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
2242                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, const int);
2243                                 unsigned int fidx= mface->v4 ? 3:2;
2244                                                 
2245                                 if(wp->flag & VP_ONLYVGROUP)
2246                                         dw_func= (MDeformWeight *(*)(MDeformVert *, const int))defvert_find_index;
2247                                 else
2248                                         dw_func= defvert_verify_index;
2249
2250                                 do {
2251                                         unsigned int vidx= *(&mface->v1 + fidx);
2252                                         const float fac = calc_vp_strength_dl(wp, vc, wpd->vertexcosnos+6*vidx, mval, brush_size_pressure);
2253                                         if (fac > 0.0f) {
2254                                                 dw = dw_func(&me->dvert[vidx], wpi.vgroup_active);
2255                                                 paintweight += dw ? (dw->weight * fac) : 0.0f;
2256                                                 totw += fac;
2257                                         }
2258
2259                                 } while (fidx--);
2260
2261                         }
2262                 }
2263         }
2264                         
2265         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
2266                 paintweight /= totw;
2267         }
2268
2269         for(index=0; index<totindex; index++) {
2270                                 
2271                 if(indexar[index] && indexar[index]<=me->totface) {
2272                         MFace *mf= me->mface + (indexar[index]-1);
2273                         unsigned int fidx= mf->v4 ? 3:2;
2274                         do {
2275                                 unsigned int vidx= *(&mf->v1 + fidx);
2276
2277                                 if(me->dvert[vidx].flag) {
2278                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*vidx,
2279                                                                 mval, brush_size_pressure, brush_alpha_pressure);
2280                                         if(alpha) {
2281                                                 do_weight_paint_vertex(wp, ob, &wpi, vidx, alpha, paintweight);
2282                                         }
2283                                         me->dvert[vidx].flag= 0;
2284                                 }
2285                         } while (fidx--);
2286                 }
2287         }
2288
2289
2290         /* *** free wpi members */
2291         MEM_freeN((void *)wpi.defbase_sel);
2292         /* *** dont freeing wpi members */
2293
2294
2295         swap_m4m4(vc->rv3d->persmat, mat);
2296                         
2297         DAG_id_tag_update(ob->data, 0);
2298         ED_region_tag_redraw(vc->ar);
2299 }
2300
2301 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2302 {
2303         ToolSettings *ts= CTX_data_tool_settings(C);
2304         Object *ob= CTX_data_active_object(C);
2305         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
2306         
2307         if(wpd) {
2308                 if(wpd->vertexcosnos)
2309                         MEM_freeN(wpd->vertexcosnos);
2310                 MEM_freeN(wpd->indexar);
2311                 
2312                 if (wpd->vgroup_validmap)
2313                         MEM_freeN((void *)wpd->vgroup_validmap);
2314                 if(wpd->lock_flags)
2315                         MEM_freeN((void *)wpd->lock_flags);
2316
2317                 MEM_freeN(wpd);
2318         }
2319         
2320         /* frees prev buffer */
2321         copy_wpaint_prev(ts->wpaint, NULL, 0);
2322         
2323         /* and particles too */
2324         if(ob->particlesystem.first) {
2325                 ParticleSystem *psys;
2326                 int i;
2327                 
2328                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
2329                         for(i=0; i<PSYS_TOT_VG; i++) {
2330                                 if(psys->vgroup[i]==ob->actdef) {
2331                                         psys->recalc |= PSYS_RECALC_RESET;
2332                                         break;
2333                                 }
2334                         }
2335                 }
2336         }
2337         
2338         DAG_id_tag_update(ob->data, 0);
2339 }
2340
2341
2342 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2343 {
2344         
2345         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
2346                                           wpaint_stroke_update_step,
2347                                           wpaint_stroke_done, event->type);
2348         
2349         /* add modal handler */
2350         WM_event_add_modal_handler(C, op);
2351
2352         op->type->modal(C, op, event);
2353         
2354         return OPERATOR_RUNNING_MODAL;
2355 }
2356
2357 static int wpaint_cancel(bContext *C, wmOperator *op)
2358 {
2359         paint_stroke_cancel(C, op);
2360
2361         return OPERATOR_CANCELLED;
2362 }
2363
2364 void PAINT_OT_weight_paint(wmOperatorType *ot)
2365 {
2366         
2367         /* identifiers */
2368         ot->name= "Weight Paint";
2369         ot->idname= "PAINT_OT_weight_paint";
2370         
2371         /* api callbacks */
2372         ot->invoke= wpaint_invoke;
2373         ot->modal= paint_stroke_modal;
2374         /* ot->exec= vpaint_exec; <-- needs stroke property */
2375         ot->poll= weight_paint_poll;
2376         ot->cancel= wpaint_cancel;
2377         
2378         /* flags */
2379         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2380
2381         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2382 }
2383
2384 static int weight_paint_set_exec(bContext *C, wmOperator *UNUSED(op))
2385 {
2386         struct Scene *scene= CTX_data_scene(C);
2387         Object *obact = CTX_data_active_object(C);
2388
2389         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
2390         ED_region_tag_redraw(CTX_wm_region(C)); /* XXX - should redraw all 3D views */
2391         return OPERATOR_FINISHED;
2392 }
2393
2394 void PAINT_OT_weight_set(wmOperatorType *ot)
2395 {
2396         /* identifiers */
2397         ot->name= "Set Weight";
2398         ot->idname= "PAINT_OT_weight_set";
2399
2400         /* api callbacks */
2401         ot->exec= weight_paint_set_exec;
2402         ot->poll= mask_paint_poll; /* it was facemask_paint_poll */
2403
2404         /* flags */
2405         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2406 }
2407
2408 /* ************ set / clear vertex paint mode ********** */
2409
2410
2411 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
2412 {       
2413         Object *ob= CTX_data_active_object(C);
2414         Scene *scene= CTX_data_scene(C);
2415         VPaint *vp= scene->toolsettings->vpaint;
2416         Mesh *me;
2417         
2418         me= get_mesh(ob);
2419         
2420         if(me==NULL || object_data_is_libdata(ob)) {
2421                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2422                 return OPERATOR_PASS_THROUGH;
2423         }
2424         
2425         if(me && me->mcol==NULL) make_vertexcol(ob);
2426         
2427         /* toggle: end vpaint */
2428         if(ob->mode & OB_MODE_VERTEX_PAINT) {
2429                 
2430                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
2431         }
2432         else {
2433                 ob->mode |= OB_MODE_VERTEX_PAINT;
2434                 /* Turn off weight painting */
2435                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
2436                         set_wpaint(C, op);
2437                 
2438                 if(vp==NULL)
2439                         vp= scene->toolsettings->vpaint= new_vpaint(0);
2440                 
2441                 paint_cursor_start(C, vertex_paint_poll);
2442
2443                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
2444         }
2445         
2446         if (me)
2447                 /* update modifier stack for mapping requirements */
2448                 DAG_id_tag_update(&me->id, 0);
2449         
2450         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
2451         
2452         return OPERATOR_FINISHED;
2453 }
2454
2455 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
2456 {
2457         
2458         /* identifiers */
2459         ot->name= "Vertex Paint Mode";
2460         ot->idname= "PAINT_OT_vertex_paint_toggle";
2461         
2462         /* api callbacks */
2463         ot->exec= set_vpaint;
2464         ot->poll= paint_poll_test;
2465         
2466         /* flags */
2467         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2468 }
2469
2470
2471
2472 /* ********************** vertex paint operator ******************* */
2473
2474 /* Implementation notes:
2475
2476 Operator->invoke()
2477   - validate context (add mcol)
2478   - create customdata storage
2479   - call paint once (mouse click)
2480   - add modal handler 
2481
2482 Operator->modal()
2483   - for every mousemove, apply vertex paint
2484   - exit on mouse release, free customdata
2485         (return OPERATOR_FINISHED also removes handler and operator)
2486
2487 For future:
2488   - implement a stroke event (or mousemove with past positons)
2489   - revise whether op->customdata should be added in object, in set_vpaint
2490
2491 */
2492
2493 typedef struct VPaintData {
2494         ViewContext vc;
2495         unsigned int paintcol;
2496         int *indexar;
2497         float *vertexcosnos;
2498         float vpimat[3][3];
2499 } VPaintData;
2500
2501 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *UNUSED(event))
2502 {
2503         ToolSettings *ts= CTX_data_tool_settings(C);
2504         struct PaintStroke *stroke = op->customdata;
2505         VPaint *vp= ts->vpaint;
2506         struct VPaintData *vpd;
2507         Object *ob= CTX_data_active_object(C);
2508         Mesh *me;
2509         float mat[4][4], imat[4][4];
2510
2511         /* context checks could be a poll() */
2512         me= get_mesh(ob);
2513         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
2514         
2515         if(me->mcol==NULL) make_vertexcol(ob);
2516         if(me->mcol==NULL) return OPERATOR_CANCELLED;
2517         
2518         /* make mode data storage */
2519         vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
2520         paint_stroke_set_mode_data(stroke, vpd);
2521         view3d_set_viewcontext(C, &vpd->vc);
2522         
2523         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
2524         vpd->indexar= get_indexarray(me);
2525         vpd->paintcol= vpaint_get_current_col(vp);
2526         
2527         /* for filtering */
2528         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
2529         
2530         /* some old cruft to sort out later */
2531         mult_m4_m4m4(mat, vpd->vc.rv3d->viewmat, ob->obmat);
2532         invert_m4_m4(imat, mat);
2533         copy_m3_m4(vpd->vpimat, imat);
2534
2535         return 1;
2536 }
2537
2538 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob,
2539                               const unsigned int index, const float mval[2],
2540                               const float brush_size_pressure, const float brush_alpha_pressure,
2541                               int UNUSED(flip))
2542 {
2543         ViewContext *vc = &vpd->vc;
2544         Brush *brush = paint_brush(&vp->paint);
2545         Mesh *me = get_mesh(ob);
2546         MFace *mface= &me->mface[index];
2547         unsigned int *mcol= ((unsigned int*)me->mcol) + 4*index;
2548         unsigned int *mcolorig= ((unsigned int*)vp->vpaint_prev) + 4*index;
2549         float alpha;
2550         int i;
2551
2552         int brush_alpha_pressure_i;
2553         
2554         if((vp->flag & VP_COLINDEX && mface->mat_nr!=ob->actcol-1) ||
2555            ((me->editflag & ME_EDIT_PAINT_MASK) && !(mface->flag & ME_FACE_SEL)))
2556                 return;
2557
2558         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
2559                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
2560                 if(mface->v4) {
2561                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
2562                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
2563                 }
2564                 else {
2565                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
2566                 }
2567         }
2568
2569         brush_alpha_pressure_i = (int)(brush_alpha_pressure*255.0f);
2570
2571         for(i = 0; i < (mface->v4 ? 4 : 3); ++i) {
2572                 alpha = calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*(&mface->v1)[i],
2573                                          mval, brush_size_pressure, brush_alpha_pressure);
2574                 if (alpha) {
2575                         const int alpha_i = (int)(alpha*255.0f);
2576                         mcol[i] = vpaint_blend(vp, mcol[i], mcolorig[i], vpd->paintcol, alpha_i, brush_alpha_pressure_i);
2577                 }
2578         }
2579 }
2580
2581 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
2582 {
2583         Scene *scene= CTX_data_scene(C);
2584         ToolSettings *ts= CTX_data_tool_settings(C);
2585         struct VPaintData *vpd = paint_stroke_mode_data(stroke);
2586         VPaint *vp= ts->vpaint;
2587         Brush *brush = paint_brush(&vp->paint);
2588         ViewContext *vc= &vpd->vc;
2589         Object *ob= vc->obact;
2590         Mesh *me= ob->data;
2591         float mat[4][4];
2592         int *indexar= vpd->indexar;
2593         int totindex, index, flip;
2594         float mval[2];
2595
2596         const float pressure = RNA_float_get(itemptr, "pressure");
2597         const float brush_size_pressure = brush_size(scene, brush) * (brush_use_size_pressure(scene, brush) ? pressure : 1.0f);
2598         const float brush_alpha_pressure = brush_alpha(scene, brush) * (brush_use_alpha_pressure(scene, brush) ? pressure : 1.0f);
2599
2600         RNA_float_get_array(itemptr, "mouse", mval);
2601         flip = RNA_boolean_get(itemptr, "pen_flip");
2602                         
2603         view3d_operator_needs_opengl(C);
2604                         
2605         /* load projection matrix */
2606         mult_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat);
2607
2608         mval[0]-= vc->ar->winrct.xmin;
2609         mval[1]-= vc->ar->winrct.ymin;
2610
2611                         
2612         /* which faces are involved */
2613         if(vp->flag & VP_AREA) {
2614                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size_pressure);
2615         }
2616         else {
2617                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
2618                 if(indexar[0]) totindex= 1;
2619                 else totindex= 0;
2620         }
2621                         
2622         swap_m4m4(vc->rv3d->persmat, mat);
2623                         
2624         for(index=0; index<totindex; index++) {                         
2625                 if (indexar[index] && indexar[index]<=me->totface) {
2626                         vpaint_paint_face(vp, vpd, ob, indexar[index]-1, mval, brush_size_pressure, brush_alpha_pressure, flip);
2627                 }
2628         }
2629                                                 
2630         swap_m4m4(vc->rv3d->persmat, mat);
2631
2632         /* was disabled because it is slow, but necessary for blur */
2633         if (brush->vertexpaint_tool == PAINT_BLEND_BLUR)
2634                 do_shared_vertexcol(me);
2635                         
2636         ED_region_tag_redraw(vc->ar);
2637                         
2638         DAG_id_tag_update(ob->data, 0);
2639 }
2640
2641 static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
2642 {
2643         ToolSettings *ts= CTX_data_tool_settings(C);
2644         struct VPaintData *vpd= paint_stroke_mode_data(stroke);
2645         
2646         if(vpd->vertexcosnos)
2647                 MEM_freeN(vpd->vertexcosnos);
2648         MEM_freeN(vpd->indexar);
2649         
2650         /* frees prev buffer */
2651         copy_vpaint_prev(ts->vpaint, NULL, 0);
2652         
2653         MEM_freeN(vpd);
2654 }
2655
2656 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
2657 {
2658         
2659         op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
2660                                           vpaint_stroke_update_step,
2661                                           vpaint_stroke_done, event->type);
2662         
2663         /* add modal handler */
2664         WM_event_add_modal_handler(C, op);
2665
2666         op->type->modal(C, op, event);
2667         
2668         return OPERATOR_RUNNING_MODAL;
2669 }
2670
2671 static int vpaint_cancel(bContext *C, wmOperator *op)
2672 {
2673         paint_stroke_cancel(C, op);
2674
2675         return OPERATOR_CANCELLED;
2676 }
2677
2678 void PAINT_OT_vertex_paint(wmOperatorType *ot)
2679 {
2680         /* identifiers */
2681         ot->name= "Vertex Paint";
2682         ot->idname= "PAINT_OT_vertex_paint";
2683         
2684         /* api callbacks */
2685         ot->invoke= vpaint_invoke;
2686         ot->modal= paint_stroke_modal;
2687         /* ot->exec= vpaint_exec; <-- needs stroke property */
2688         ot->poll= vertex_paint_poll;
2689         ot->cancel= vpaint_cancel;
2690         
2691         /* flags */
2692         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
2693
2694         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
2695 }
2696
2697 /* ********************** weight from bones operator ******************* */
2698
2699 static int weight_from_bones_poll(bContext *C)
2700 {
2701         Object *ob= CTX_data_active_object(C);
2702
2703         return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob));
2704 }
2705
2706 static int weight_from_bones_exec(bContext *C, wmOperator *op)
2707 {
2708         Scene *scene= CTX_data_scene(C);
2709         Object *ob= CTX_data_active_object(C);
2710         Object *armob= modifiers_isDeformedByArmature(ob);
2711         Mesh *me= ob->data;
2712         int type= RNA_enum_get(op->ptr, "type");
2713
2714         create_vgroups_from_armature(op->reports, scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X));
2715
2716         DAG_id_tag_update(&me->id, 0);
2717         WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
2718
2719         return OPERATOR_FINISHED;
2720 }
2721
2722 void PAINT_OT_weight_from_bones(wmOperatorType *ot)
2723 {
2724         static EnumPropertyItem type_items[]= {
2725                 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights froms bones"},
2726                 {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"},
2727                 {0, NULL, 0, NULL, NULL}};
2728
2729         /* identifiers */
2730         ot->name= "Weight from Bones";
2731         ot->idname= "PAINT_OT_weight_from_bones";
2732         
2733         /* api callbacks */
2734         ot->exec= weight_from_bones_exec;
2735         ot->invoke= WM_menu_invoke;
2736         ot->poll= weight_from_bones_poll;
2737         
2738         /* flags */
2739         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2740
2741         /* properties */
2742         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights");
2743 }
2744