svn merge -r 30954:31059 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / editors / sculpt_paint / paint_vertex.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <string.h>
32
33 #ifdef WIN32
34 #include <io.h>
35 #else
36 #include <unistd.h>
37 #endif   
38
39 #include "MEM_guardedalloc.h"
40
41 #include "IMB_imbuf.h"
42 #include "IMB_imbuf_types.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_ghash.h"
47
48 #include "DNA_armature_types.h"
49 #include "DNA_mesh_types.h"
50 #include "DNA_particle_types.h"
51 #include "DNA_brush_types.h"
52
53 #include "RNA_access.h"
54 #include "RNA_define.h"
55
56 #include "BKE_armature.h"
57 #include "BKE_action.h"
58 #include "BKE_brush.h"
59 #include "BKE_DerivedMesh.h"
60 #include "BKE_cloth.h"
61 #include "BKE_context.h"
62 #include "BKE_customdata.h"
63 #include "BKE_depsgraph.h"
64 #include "BKE_deform.h"
65 #include "BKE_displist.h"
66 #include "BKE_global.h"
67 #include "BKE_mesh.h"
68 #include "BKE_modifier.h"
69 #include "BKE_object.h"
70 #include "BKE_paint.h"
71 #include "BKE_utildefines.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76 #include "BIF_gl.h"
77 #include "BIF_glutil.h"
78
79 #include "ED_armature.h"
80 #include "ED_mesh.h"
81 #include "ED_screen.h"
82 #include "ED_view3d.h"
83
84 #include "paint_intern.h"
85
86 /* brush->vertexpaint_tool */
87 #define VP_MIX  0
88 #define VP_ADD  1
89 #define VP_SUB  2
90 #define VP_MUL  3
91 #define VP_BLUR 4
92 #define VP_LIGHTEN      5
93 #define VP_DARKEN       6
94
95 /* polling - retrieve whether cursor should be set or operator should be done */
96
97
98 /* Returns true if vertex paint mode is active */
99 int vertex_paint_mode_poll(bContext *C)
100 {
101         Object *ob = CTX_data_active_object(C);
102
103         return ob && ob->mode == OB_MODE_VERTEX_PAINT;
104 }
105
106 int vertex_paint_poll(bContext *C)
107 {
108         if(vertex_paint_mode_poll(C) && 
109            paint_brush(&CTX_data_tool_settings(C)->vpaint->paint)) {
110                 ScrArea *sa= CTX_wm_area(C);
111                 if(sa->spacetype==SPACE_VIEW3D) {
112                         ARegion *ar= CTX_wm_region(C);
113                         if(ar->regiontype==RGN_TYPE_WINDOW)
114                                 return 1;
115                 }
116         }
117         return 0;
118 }
119
120 int weight_paint_mode_poll(bContext *C)
121 {
122         Object *ob = CTX_data_active_object(C);
123
124         return ob && ob->mode == OB_MODE_WEIGHT_PAINT;
125 }
126
127 int weight_paint_poll(bContext *C)
128 {
129         Object *ob = CTX_data_active_object(C);
130
131         if(ob && ob->mode & OB_MODE_WEIGHT_PAINT &&
132            paint_brush(&CTX_data_tool_settings(C)->wpaint->paint)) {
133                 ScrArea *sa= CTX_wm_area(C);
134                 if(sa->spacetype==SPACE_VIEW3D) {
135                         ARegion *ar= CTX_wm_region(C);
136                         if(ar->regiontype==RGN_TYPE_WINDOW)
137                                 return 1;
138                 }
139         }
140         return 0;
141 }
142
143 static VPaint *new_vpaint(int wpaint)
144 {
145         VPaint *vp= MEM_callocN(sizeof(VPaint), "VPaint");
146         
147         vp->flag= VP_AREA+VP_SPRAY;
148         
149         if(wpaint)
150                 vp->flag= VP_AREA;
151
152         return vp;
153 }
154
155 static int *get_indexarray(Mesh *me)
156 {
157         return MEM_mallocN(sizeof(int)*(me->totface+1), "vertexpaint");
158 }
159
160
161 /* in contradiction to cpack drawing colors, the MCOL colors (vpaint colors) are per byte! 
162    so not endian sensitive. Mcol = ABGR!!! so be cautious with cpack calls */
163
164 unsigned int rgba_to_mcol(float r, float g, float b, float a)
165 {
166         int ir, ig, ib, ia;
167         unsigned int col;
168         char *cp;
169         
170         ir= floor(255.0*r);
171         if(ir<0) ir= 0; else if(ir>255) ir= 255;
172         ig= floor(255.0*g);
173         if(ig<0) ig= 0; else if(ig>255) ig= 255;
174         ib= floor(255.0*b);
175         if(ib<0) ib= 0; else if(ib>255) ib= 255;
176         ia= floor(255.0*a);
177         if(ia<0) ia= 0; else if(ia>255) ia= 255;
178         
179         cp= (char *)&col;
180         cp[0]= ia;
181         cp[1]= ib;
182         cp[2]= ig;
183         cp[3]= ir;
184         
185         return col;
186         
187 }
188
189 unsigned int vpaint_get_current_col(VPaint *vp)
190 {
191         Brush *brush = paint_brush(&vp->paint);
192         return rgba_to_mcol(brush->rgb[0], brush->rgb[1], brush->rgb[2], 1.0f);
193 }
194
195 static void do_shared_vertexcol(Mesh *me)
196 {
197         /* if no mcol: do not do */
198         /* if tface: only the involved faces, otherwise all */
199         MFace *mface;
200         MTFace *tface;
201         int a;
202         short *scolmain, *scol;
203         char *mcol;
204         
205         if(me->mcol==0 || me->totvert==0 || me->totface==0) return;
206         
207         scolmain= MEM_callocN(4*sizeof(short)*me->totvert, "colmain");
208         
209         tface= me->mtface;
210         mface= me->mface;
211         mcol= (char *)me->mcol;
212         for(a=me->totface; a>0; a--, mface++, mcol+=16) {
213                 if((tface && tface->mode & TF_SHAREDCOL) || (me->editflag & ME_EDIT_PAINT_MASK)==0) {
214                         scol= scolmain+4*mface->v1;
215                         scol[0]++; scol[1]+= mcol[1]; scol[2]+= mcol[2]; scol[3]+= mcol[3];
216                         scol= scolmain+4*mface->v2;
217                         scol[0]++; scol[1]+= mcol[5]; scol[2]+= mcol[6]; scol[3]+= mcol[7];
218                         scol= scolmain+4*mface->v3;
219                         scol[0]++; scol[1]+= mcol[9]; scol[2]+= mcol[10]; scol[3]+= mcol[11];
220                         if(mface->v4) {
221                                 scol= scolmain+4*mface->v4;
222                                 scol[0]++; scol[1]+= mcol[13]; scol[2]+= mcol[14]; scol[3]+= mcol[15];
223                         }
224                 }
225                 if(tface) tface++;
226         }
227         
228         a= me->totvert;
229         scol= scolmain;
230         while(a--) {
231                 if(scol[0]>1) {
232                         scol[1]/= scol[0];
233                         scol[2]/= scol[0];
234                         scol[3]/= scol[0];
235                 }
236                 scol+= 4;
237         }
238         
239         tface= me->mtface;
240         mface= me->mface;
241         mcol= (char *)me->mcol;
242         for(a=me->totface; a>0; a--, mface++, mcol+=16) {
243                 if((tface && tface->mode & TF_SHAREDCOL) || (me->editflag & ME_EDIT_PAINT_MASK)==0) {
244                         scol= scolmain+4*mface->v1;
245                         mcol[1]= scol[1]; mcol[2]= scol[2]; mcol[3]= scol[3];
246                         scol= scolmain+4*mface->v2;
247                         mcol[5]= scol[1]; mcol[6]= scol[2]; mcol[7]= scol[3];
248                         scol= scolmain+4*mface->v3;
249                         mcol[9]= scol[1]; mcol[10]= scol[2]; mcol[11]= scol[3];
250                         if(mface->v4) {
251                                 scol= scolmain+4*mface->v4;
252                                 mcol[13]= scol[1]; mcol[14]= scol[2]; mcol[15]= scol[3];
253                         }
254                 }
255                 if(tface) tface++;
256         }
257
258         MEM_freeN(scolmain);
259 }
260
261 static void make_vertexcol(Object *ob)  /* single ob */
262 {
263         Mesh *me;
264         if(!ob || ob->id.lib) return;
265         me= get_mesh(ob);
266         if(me==0) return;
267         if(me->edit_mesh) return;
268
269         /* copies from shadedisplist to mcol */
270         if(!me->mcol) {
271                 CustomData_add_layer(&me->fdata, CD_MCOL, CD_CALLOC, NULL, me->totface);
272                 mesh_update_customdata_pointers(me);
273         }
274
275         //if(shade)
276         //      shadeMeshMCol(scene, ob, me);
277         //else
278
279         memset(me->mcol, 255, 4*sizeof(MCol)*me->totface);
280         
281         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
282         
283 }
284
285 static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
286 {
287         if(vp->vpaint_prev) {
288                 MEM_freeN(vp->vpaint_prev);
289                 vp->vpaint_prev= NULL;
290         }
291         vp->tot= tot;   
292         
293         if(mcol==NULL || tot==0) return;
294         
295         vp->vpaint_prev= MEM_mallocN(4*sizeof(int)*tot, "vpaint_prev");
296         memcpy(vp->vpaint_prev, mcol, 4*sizeof(int)*tot);
297         
298 }
299
300 static void copy_wpaint_prev (VPaint *wp, MDeformVert *dverts, int dcount)
301 {
302         if (wp->wpaint_prev) {
303                 free_dverts(wp->wpaint_prev, wp->tot);
304                 wp->wpaint_prev= NULL;
305         }
306         
307         if(dverts && dcount) {
308                 
309                 wp->wpaint_prev = MEM_mallocN (sizeof(MDeformVert)*dcount, "wpaint prev");
310                 wp->tot = dcount;
311                 copy_dverts (wp->wpaint_prev, dverts, dcount);
312         }
313 }
314
315
316 void vpaint_fill(Object *ob, unsigned int paintcol)
317 {
318         Mesh *me;
319         MFace *mf;
320         unsigned int *mcol;
321         int i, selected;
322
323         me= get_mesh(ob);
324         if(me==0 || me->totface==0) return;
325
326         if(!me->mcol)
327                 make_vertexcol(ob);
328
329         selected= (me->editflag & ME_EDIT_PAINT_MASK);
330
331         mf = me->mface;
332         mcol = (unsigned int*)me->mcol;
333         for (i = 0; i < me->totface; i++, mf++, mcol+=4) {
334                 if (!selected || mf->flag & ME_FACE_SEL) {
335                         mcol[0] = paintcol;
336                         mcol[1] = paintcol;
337                         mcol[2] = paintcol;
338                         mcol[3] = paintcol;
339                 }
340         }
341         
342         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
343 }
344
345
346 /* fills in the selected faces with the current weight and vertex group */
347 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
348 {
349         Mesh *me;
350         MFace *mface;
351         MDeformWeight *dw, *uw;
352         int *indexar;
353         int index, vgroup;
354         unsigned int faceverts[5]={0,0,0,0,0};
355         unsigned char i;
356         int vgroup_mirror= -1;
357         int selected;
358         
359         me= ob->data;
360         if(me==0 || me->totface==0 || me->dvert==0 || !me->mface) return;
361         
362         selected= (me->editflag & ME_EDIT_PAINT_MASK);
363
364         indexar= get_indexarray(me);
365
366         if(selected) {
367                 for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
368                         if((mface->flag & ME_FACE_SEL)==0)
369                                 indexar[index]= 0;
370                         else
371                                 indexar[index]= index+1;
372                 }
373         }
374         else {
375                 for(index=0; index<me->totface; index++)
376                         indexar[index]= index+1;
377         }
378         
379         vgroup= ob->actdef-1;
380         
381         /* directly copied from weight_paint, should probaby split into a separate function */
382         /* if mirror painting, find the other group */          
383         if(me->editflag & ME_EDIT_MIRROR_X) {
384                 bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
385                 if(defgroup) {
386                         bDeformGroup *curdef;
387                         int actdef= 0;
388                         char name[32];
389
390                         flip_side_name(name, defgroup->name, FALSE);
391
392                         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
393                                 if (!strcmp(curdef->name, name))
394                                         break;
395                         if(curdef==NULL) {
396                                 int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
397                                 curdef= ED_vgroup_add_name (ob, name);
398                                 ob->actdef= olddef;
399                         }
400                         
401                         if(curdef && curdef!=defgroup)
402                                 vgroup_mirror= actdef;
403                 }
404         }
405         /* end copy from weight_paint*/
406         
407         copy_wpaint_prev(wp, me->dvert, me->totvert);
408         
409         for(index=0; index<me->totface; index++) {
410                 if(indexar[index] && indexar[index]<=me->totface) {
411                         mface= me->mface + (indexar[index]-1);
412                         /* just so we can loop through the verts */
413                         faceverts[0]= mface->v1;
414                         faceverts[1]= mface->v2;
415                         faceverts[2]= mface->v3;
416                         faceverts[3]= mface->v4;
417                         for (i=0; i<3 || faceverts[i]; i++) {
418                                 if(!((me->dvert+faceverts[i])->flag)) {
419                                         dw= defvert_verify_index(me->dvert+faceverts[i], vgroup);
420                                         if(dw) {
421                                                 uw= defvert_verify_index(wp->wpaint_prev+faceverts[i], vgroup);
422                                                 uw->weight= dw->weight; /* set the undo weight */
423                                                 dw->weight= paintweight;
424                                                 
425                                                 if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
426                                                         int j= mesh_get_x_mirror_vert(ob, faceverts[i]);
427                                                         if(j>=0) {
428                                                                 /* copy, not paint again */
429                                                                 if(vgroup_mirror != -1) {
430                                                                         dw= defvert_verify_index(me->dvert+j, vgroup_mirror);
431                                                                         uw= defvert_verify_index(wp->wpaint_prev+j, vgroup_mirror);
432                                                                 } else {
433                                                                         dw= defvert_verify_index(me->dvert+j, vgroup);
434                                                                         uw= defvert_verify_index(wp->wpaint_prev+j, vgroup);
435                                                                 }
436                                                                 uw->weight= dw->weight; /* set the undo weight */
437                                                                 dw->weight= paintweight;
438                                                         }
439                                                 }
440                                         }
441                                         (me->dvert+faceverts[i])->flag= 1;
442                                 }
443                         }
444                 }
445         }
446         
447         index=0;
448         while (index<me->totvert) {
449                 (me->dvert+index)->flag= 0;
450                 index++;
451         }
452         
453         MEM_freeN(indexar);
454         copy_wpaint_prev(wp, NULL, 0);
455
456         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
457 }
458
459 /* XXX: should be re-implemented as a vertex/weight paint 'colour correct' operator
460  
461 void vpaint_dogamma(Scene *scene)
462 {
463         VPaint *vp= scene->toolsettings->vpaint;
464         Mesh *me;
465         Object *ob;
466         float igam, fac;
467         int a, temp;
468         unsigned char *cp, gamtab[256];
469
470         ob= OBACT;
471         me= get_mesh(ob);
472
473         if(!(ob->mode & OB_MODE_VERTEX_PAINT)) return;
474         if(me==0 || me->mcol==0 || me->totface==0) return;
475
476         igam= 1.0/vp->gamma;
477         for(a=0; a<256; a++) {
478                 
479                 fac= ((float)a)/255.0;
480                 fac= vp->mul*pow( fac, igam);
481                 
482                 temp= 255.9*fac;
483                 
484                 if(temp<=0) gamtab[a]= 0;
485                 else if(temp>=255) gamtab[a]= 255;
486                 else gamtab[a]= temp;
487         }
488
489         a= 4*me->totface;
490         cp= (unsigned char *)me->mcol;
491         while(a--) {
492                 
493                 cp[1]= gamtab[ cp[1] ];
494                 cp[2]= gamtab[ cp[2] ];
495                 cp[3]= gamtab[ cp[3] ];
496                 
497                 cp+= 4;
498         }
499 }
500  */
501
502 static unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
503 {
504         char *cp1, *cp2, *cp;
505         int mfac;
506         unsigned int col=0;
507         
508         if(fac==0) return col1;
509         if(fac>=255) return col2;
510
511         mfac= 255-fac;
512         
513         cp1= (char *)&col1;
514         cp2= (char *)&col2;
515         cp=  (char *)&col;
516         
517         cp[0]= 255;
518         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
519         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
520         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
521         
522         return col;
523 }
524
525 static unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
526 {
527         char *cp1, *cp2, *cp;
528         int temp;
529         unsigned int col=0;
530         
531         if(fac==0) return col1;
532         
533         cp1= (char *)&col1;
534         cp2= (char *)&col2;
535         cp=  (char *)&col;
536         
537         cp[0]= 255;
538         temp= cp1[1] + ((fac*cp2[1])/255);
539         if(temp>254) cp[1]= 255; else cp[1]= temp;
540         temp= cp1[2] + ((fac*cp2[2])/255);
541         if(temp>254) cp[2]= 255; else cp[2]= temp;
542         temp= cp1[3] + ((fac*cp2[3])/255);
543         if(temp>254) cp[3]= 255; else cp[3]= temp;
544         
545         return col;
546 }
547
548 static unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
549 {
550         char *cp1, *cp2, *cp;
551         int temp;
552         unsigned int col=0;
553         
554         if(fac==0) return col1;
555         
556         cp1= (char *)&col1;
557         cp2= (char *)&col2;
558         cp=  (char *)&col;
559         
560         cp[0]= 255;
561         temp= cp1[1] - ((fac*cp2[1])/255);
562         if(temp<0) cp[1]= 0; else cp[1]= temp;
563         temp= cp1[2] - ((fac*cp2[2])/255);
564         if(temp<0) cp[2]= 0; else cp[2]= temp;
565         temp= cp1[3] - ((fac*cp2[3])/255);
566         if(temp<0) cp[3]= 0; else cp[3]= temp;
567         
568         return col;
569 }
570
571 static unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac)
572 {
573         char *cp1, *cp2, *cp;
574         int mfac;
575         unsigned int col=0;
576         
577         if(fac==0) return col1;
578
579         mfac= 255-fac;
580         
581         cp1= (char *)&col1;
582         cp2= (char *)&col2;
583         cp=  (char *)&col;
584         
585         /* first mul, then blend the fac */
586         cp[0]= 255;
587         cp[1]= (mfac*cp1[1] + fac*((cp2[1]*cp1[1])/255)  )/255;
588         cp[2]= (mfac*cp1[2] + fac*((cp2[2]*cp1[2])/255)  )/255;
589         cp[3]= (mfac*cp1[3] + fac*((cp2[3]*cp1[3])/255)  )/255;
590
591         
592         return col;
593 }
594
595 static unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
596 {
597         char *cp1, *cp2, *cp;
598         int mfac;
599         unsigned int col=0;
600         
601         if(fac==0) return col1;
602         if(fac>=255) return col2;
603
604         mfac= 255-fac;
605         
606         cp1= (char *)&col1;
607         cp2= (char *)&col2;
608         cp=  (char *)&col;
609         
610         /* See if are lighter, if so mix, else dont do anything.
611         if the paint col is darker then the original, then ignore */
612         if (cp1[1]+cp1[2]+cp1[3] > cp2[1]+cp2[2]+cp2[3])
613                 return col1;
614         
615         cp[0]= 255;
616         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
617         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
618         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
619         
620         return col;
621 }
622
623 static unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
624 {
625         char *cp1, *cp2, *cp;
626         int mfac;
627         unsigned int col=0;
628         
629         if(fac==0) return col1;
630         if(fac>=255) return col2;
631
632         mfac= 255-fac;
633         
634         cp1= (char *)&col1;
635         cp2= (char *)&col2;
636         cp=  (char *)&col;
637         
638         /* See if were darker, if so mix, else dont do anything.
639         if the paint col is brighter then the original, then ignore */
640         if (cp1[1]+cp1[2]+cp1[3] < cp2[1]+cp2[2]+cp2[3])
641                 return col1;
642         
643         cp[0]= 255;
644         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
645         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
646         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
647         return col;
648 }
649
650 static void vpaint_blend(VPaint *vp, unsigned int *col, unsigned int *colorig, unsigned int paintcol, int alpha)
651 {
652         Brush *brush = paint_brush(&vp->paint);
653
654         if(brush->vertexpaint_tool==VP_MIX || brush->vertexpaint_tool==VP_BLUR) *col= mcol_blend( *col, paintcol, alpha);
655         else if(brush->vertexpaint_tool==VP_ADD) *col= mcol_add( *col, paintcol, alpha);
656         else if(brush->vertexpaint_tool==VP_SUB) *col= mcol_sub( *col, paintcol, alpha);
657         else if(brush->vertexpaint_tool==VP_MUL) *col= mcol_mul( *col, paintcol, alpha);
658         else if(brush->vertexpaint_tool==VP_LIGHTEN) *col= mcol_lighten( *col, paintcol, alpha);
659         else if(brush->vertexpaint_tool==VP_DARKEN) *col= mcol_darken( *col, paintcol, alpha);
660         
661         /* if no spray, clip color adding with colorig & orig alpha */
662         if((vp->flag & VP_SPRAY)==0) {
663                 unsigned int testcol=0, a;
664                 char *cp, *ct, *co;
665                 
666                 alpha= (int)(255.0*brush_alpha(brush));
667                 
668                 if(brush->vertexpaint_tool==VP_MIX || brush->vertexpaint_tool==VP_BLUR) testcol= mcol_blend( *colorig, paintcol, alpha);
669                 else if(brush->vertexpaint_tool==VP_ADD) testcol= mcol_add( *colorig, paintcol, alpha);
670                 else if(brush->vertexpaint_tool==VP_SUB) testcol= mcol_sub( *colorig, paintcol, alpha);
671                 else if(brush->vertexpaint_tool==VP_MUL) testcol= mcol_mul( *colorig, paintcol, alpha);
672                 else if(brush->vertexpaint_tool==VP_LIGHTEN)  testcol= mcol_lighten( *colorig, paintcol, alpha);
673                 else if(brush->vertexpaint_tool==VP_DARKEN)   testcol= mcol_darken( *colorig, paintcol, alpha);
674                 
675                 cp= (char *)col;
676                 ct= (char *)&testcol;
677                 co= (char *)colorig;
678                 
679                 for(a=0; a<4; a++) {
680                         if( ct[a]<co[a] ) {
681                                 if( cp[a]<ct[a] ) cp[a]= ct[a];
682                                 else if( cp[a]>co[a] ) cp[a]= co[a];
683                         }
684                         else {
685                                 if( cp[a]<co[a] ) cp[a]= co[a];
686                                 else if( cp[a]>ct[a] ) cp[a]= ct[a];
687                         }
688                 }
689         }
690 }
691
692
693 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
694 {
695         struct ImBuf *ibuf;
696         int a, tot=0, index;
697         
698         /* brecht: disabled this because it obviously failes for
699            brushes with size > 64, why is this here? */
700         /*if(size>64.0) size= 64.0;*/
701         
702         ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
703         if(ibuf) {
704                 unsigned int *rt= ibuf->rect;
705
706                 memset(indexar, 0, sizeof(int)*(totface+1));
707                 
708                 size= ibuf->x*ibuf->y;
709                 while(size--) {
710                                 
711                         if(*rt) {
712                                 index= WM_framebuffer_to_index(*rt);
713                                 if(index>0 && index<=totface)
714                                         indexar[index] = 1;
715                         }
716                 
717                         rt++;
718                 }
719                 
720                 for(a=1; a<=totface; a++) {
721                         if(indexar[a]) indexar[tot++]= a;
722                 }
723
724                 IMB_freeImBuf(ibuf);
725         }
726         
727         return tot;
728 }
729
730 static float calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], float *vert_nor, float *mval, float pressure)
731 {
732         Brush *brush = paint_brush(&vp->paint);
733         float fac, fac_2, size, dx, dy;
734         float alpha;
735         short vertco[2];
736         const int radius= brush_size(brush);
737
738         project_short_noclip(vc->ar, vert_nor, vertco);
739         dx= mval[0]-vertco[0];
740         dy= mval[1]-vertco[1];
741         
742         if (brush_use_size_pressure(brush))
743                 size = pressure * radius;
744         else
745                 size = radius;
746         
747         fac_2= dx*dx + dy*dy;
748         if(fac_2 > size*size) return 0.f;
749         fac = sqrtf(fac_2);
750         
751         alpha= brush_alpha(brush) * brush_curve_strength_clamp(brush, fac, size);
752         
753         if (brush_use_alpha_pressure(brush))
754                 alpha *= pressure;
755                 
756         if(vp->flag & VP_NORMALS) {
757                 float *no= vert_nor+3;
758                 
759                 /* transpose ! */
760                 fac= vpimat[2][0]*no[0]+vpimat[2][1]*no[1]+vpimat[2][2]*no[2];
761                 if(fac>0.0) {
762                         dx= vpimat[0][0]*no[0]+vpimat[0][1]*no[1]+vpimat[0][2]*no[2];
763                         dy= vpimat[1][0]*no[0]+vpimat[1][1]*no[1]+vpimat[1][2]*no[2];
764                         
765                         alpha*= fac/sqrtf(dx*dx + dy*dy + fac*fac);
766                 }
767                 else return 0.f;
768         }
769         
770         return alpha;
771 }
772
773 static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval, int flip)
774 {
775         Brush *brush = paint_brush(&wp->paint);
776         int tool = brush->vertexpaint_tool;
777         
778         if(dw==NULL || uw==NULL) return;
779         
780         if (flip) {
781                 switch(tool) {
782                         case VP_MIX:
783                                 paintval = 1.f - paintval; break;
784                         case VP_ADD:
785                                 tool= VP_SUB; break;
786                         case VP_SUB:
787                                 tool= VP_ADD; break;
788                         case VP_LIGHTEN:
789                                 tool= VP_DARKEN; break;
790                         case VP_DARKEN:
791                                 tool= VP_LIGHTEN; break;
792                 }
793         }
794         
795         if(tool==VP_MIX || tool==VP_BLUR)
796                 dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
797         else if(tool==VP_ADD)
798                 dw->weight += paintval*alpha;
799         else if(tool==VP_SUB) 
800                 dw->weight -= paintval*alpha;
801         else if(tool==VP_MUL) 
802                 /* first mul, then blend the fac */
803                 dw->weight = ((1.0-alpha) + alpha*paintval)*dw->weight;
804         else if(tool==VP_LIGHTEN) {
805                 if (dw->weight < paintval)
806                         dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
807         } else if(tool==VP_DARKEN) {
808                 if (dw->weight > paintval)
809                         dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
810         }
811         CLAMP(dw->weight, 0.0f, 1.0f);
812         
813         /* if no spray, clip result with orig weight & orig alpha */
814         if((wp->flag & VP_SPRAY)==0) {
815                 float testw=0.0f;
816                 
817                 alpha= brush_alpha(brush);
818                 if(tool==VP_MIX || tool==VP_BLUR)
819                         testw = paintval*alpha + uw->weight*(1.0-alpha);
820                 else if(tool==VP_ADD)
821                         testw = uw->weight + paintval*alpha;
822                 else if(tool==VP_SUB) 
823                         testw = uw->weight - paintval*alpha;
824                 else if(tool==VP_MUL) 
825                         /* first mul, then blend the fac */
826                         testw = ((1.0-alpha) + alpha*paintval)*uw->weight;              
827                 else if(tool==VP_LIGHTEN) {
828                         if (uw->weight < paintval)
829                                 testw = paintval*alpha + uw->weight*(1.0-alpha);
830                         else
831                                 testw = uw->weight;
832                 } else if(tool==VP_DARKEN) {
833                         if (uw->weight > paintval)
834                                 testw = paintval*alpha + uw->weight*(1.0-alpha);
835                         else
836                                 testw = uw->weight;
837                 }
838                 CLAMP(testw, 0.0f, 1.0f);
839                 
840                 if( testw<uw->weight ) {
841                         if(dw->weight < testw) dw->weight= testw;
842                         else if(dw->weight > uw->weight) dw->weight= uw->weight;
843                 }
844                 else {
845                         if(dw->weight > testw) dw->weight= testw;
846                         else if(dw->weight < uw->weight) dw->weight= uw->weight;
847                 }
848         }
849         
850 }
851
852 /* ----------------------------------------------------- */
853
854 /* used for 3d view, on active object, assumes me->dvert exists */
855 /* if mode==1: */
856 /*     samples cursor location, and gives menu with vertex groups to activate */
857 /* else */
858 /*     sets wp->weight to the closest weight value to vertex */
859 /*     note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
860 void sample_wpaint(Scene *scene, ARegion *ar, View3D *v3d, int mode)
861 {
862         ViewContext vc;
863         ToolSettings *ts= scene->toolsettings;
864         Object *ob= OBACT;
865         Mesh *me= get_mesh(ob);
866         int index;
867         short mval[2] = {0, 0}, sco[2];
868         int vgroup= ob->actdef-1;
869
870         if (!me) return;
871         
872 //      getmouseco_areawin(mval);
873         index= view3d_sample_backbuf(&vc, mval[0], mval[1]);
874         
875         if(index && index<=me->totface) {
876                 MFace *mface;
877                 
878                 mface= ((MFace *)me->mface) + index-1;
879                 
880                 if(mode==1) {   /* sampe which groups are in here */
881                         MDeformVert *dv;
882                         int a, totgroup;
883                         
884                         totgroup= BLI_countlist(&ob->defbase);
885                         if(totgroup) {
886                                 int totmenu=0;
887                                 int *groups=MEM_callocN(totgroup*sizeof(int), "groups");
888                                 
889                                 dv= me->dvert+mface->v1;
890                                 for(a=0; a<dv->totweight; a++) {
891                                         if (dv->dw[a].def_nr<totgroup)
892                                                 groups[dv->dw[a].def_nr]= 1;
893                                 }
894                                 dv= me->dvert+mface->v2;
895                                 for(a=0; a<dv->totweight; a++) {
896                                         if (dv->dw[a].def_nr<totgroup)
897                                                 groups[dv->dw[a].def_nr]= 1;
898                                 }
899                                 dv= me->dvert+mface->v3;
900                                 for(a=0; a<dv->totweight; a++) {
901                                         if (dv->dw[a].def_nr<totgroup)
902                                                 groups[dv->dw[a].def_nr]= 1;
903                                 }
904                                 if(mface->v4) {
905                                         dv= me->dvert+mface->v4;
906                                         for(a=0; a<dv->totweight; a++) {
907                                                 if (dv->dw[a].def_nr<totgroup)
908                                                         groups[dv->dw[a].def_nr]= 1;
909                                         }
910                                 }
911                                 for(a=0; a<totgroup; a++)
912                                         if(groups[a]) totmenu++;
913                                 
914                                 if(totmenu==0) {
915                                         //notice("No Vertex Group Selected");
916                                 }
917                                 else {
918                                         bDeformGroup *dg;
919                                         short val;
920                                         char item[40], *str= MEM_mallocN(40*totmenu+40, "menu");
921                                         
922                                         strcpy(str, "Vertex Groups %t");
923                                         for(a=0, dg=ob->defbase.first; dg && a<totgroup; a++, dg= dg->next) {
924                                                 if(groups[a]) {
925                                                         sprintf(item, "|%s %%x%d", dg->name, a);
926                                                         strcat(str, item);
927                                                 }
928                                         }
929                                         
930                                         val= 0; // XXX pupmenu(str);
931                                         if(val>=0) {
932                                                 ob->actdef= val+1;
933                                                 DAG_id_flush_update(&me->id, OB_RECALC_DATA);
934                                         }
935                                         MEM_freeN(str);
936                                 }
937                                 MEM_freeN(groups);
938                         }
939 //                      else notice("No Vertex Groups in Object");
940                 }
941                 else {
942                         DerivedMesh *dm;
943                         float w1, w2, w3, w4, co[3], fac;
944                         
945                         dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
946                         if(dm->getVertCo==NULL) {
947                                 //notice("Not supported yet");
948                         }
949                         else {
950                                 /* calc 3 or 4 corner weights */
951                                 dm->getVertCo(dm, mface->v1, co);
952                                 project_short_noclip(ar, co, sco);
953                                 w1= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
954                                 
955                                 dm->getVertCo(dm, mface->v2, co);
956                                 project_short_noclip(ar, co, sco);
957                                 w2= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
958                                 
959                                 dm->getVertCo(dm, mface->v3, co);
960                                 project_short_noclip(ar, co, sco);
961                                 w3= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
962                                 
963                                 if(mface->v4) {
964                                         dm->getVertCo(dm, mface->v4, co);
965                                         project_short_noclip(ar, co, sco);
966                                         w4= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
967                                 }
968                                 else w4= 1.0e10;
969                                 
970                                 fac= MIN4(w1, w2, w3, w4);
971                                 if(w1==fac) {
972                                         ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v1, vgroup);
973                                 }
974                                 else if(w2==fac) {
975                                         ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v2, vgroup);
976                                 }
977                                 else if(w3==fac) {
978                                         ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v3, vgroup);
979                                 }
980                                 else if(w4==fac) {
981                                         if(mface->v4) {
982                                                 ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v4, vgroup);
983                                         }
984                                 }
985                         }
986                         dm->release(dm);
987                 }               
988                 
989         }
990         
991 }
992
993 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
994                                            int paint_nr, char *map)
995 {
996 //      MDeformWeight *dw = dvert->dw;
997         float sum=0.0f, fac=0.0f, paintw=0.0f;
998         int i, tot=0;
999
1000         if (!map)
1001                 return;
1002
1003         for (i=0; i<dvert->totweight; i++) {
1004                 if (dvert->dw[i].def_nr == paint_nr)
1005                         paintw = dvert->dw[i].weight;
1006
1007                 if (map[dvert->dw[i].def_nr]) {
1008                         tot += 1;
1009                         if (dvert->dw[i].def_nr != paint_nr)
1010                                 sum += dvert->dw[i].weight;
1011                 }
1012         }
1013         
1014         if (!tot || sum <= (1.0f - paintw))
1015                 return;
1016
1017         fac = sum / (1.0f - paintw);
1018         fac = fac==0.0f ? 1.0f : 1.0f / fac;
1019
1020         for (i=0; i<dvert->totweight; i++) {
1021                 if (map[dvert->dw[i].def_nr]) {
1022                         if (dvert->dw[i].def_nr != paint_nr)
1023                                 dvert->dw[i].weight *= fac;
1024                 }
1025         }
1026 }
1027
1028 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, 
1029                                    float alpha, float paintweight, int flip, 
1030                                    int vgroup_mirror, char *validmap)
1031 {
1032         Mesh *me= ob->data;
1033         MDeformWeight *dw, *uw;
1034         int vgroup= ob->actdef-1;
1035         
1036         if(wp->flag & VP_ONLYVGROUP) {
1037                 dw= defvert_find_index(me->dvert+index, vgroup);
1038                 uw= defvert_find_index(wp->wpaint_prev+index, vgroup);
1039         }
1040         else {
1041                 dw= defvert_verify_index(me->dvert+index, vgroup);
1042                 uw= defvert_verify_index(wp->wpaint_prev+index, vgroup);
1043         }
1044         if(dw==NULL || uw==NULL)
1045                 return;
1046         
1047         wpaint_blend(wp, dw, uw, alpha, paintweight, flip);
1048         do_weight_paint_auto_normalize(me->dvert+index, vgroup, validmap);
1049
1050         if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
1051                 int j= mesh_get_x_mirror_vert(ob, index);
1052                 if(j>=0) {
1053                         /* copy, not paint again */
1054                         if(vgroup_mirror != -1)
1055                                 uw= defvert_verify_index(me->dvert+j, vgroup_mirror);
1056                         else
1057                                 uw= defvert_verify_index(me->dvert+j, vgroup);
1058                                 
1059                         uw->weight= dw->weight;
1060
1061                         do_weight_paint_auto_normalize(me->dvert+j, vgroup, validmap);
1062                 }
1063         }
1064 }
1065
1066
1067 /* *************** set wpaint operator ****************** */
1068
1069 static int set_wpaint(bContext *C, wmOperator *op)              /* toggle */
1070 {               
1071         Object *ob= CTX_data_active_object(C);
1072         Scene *scene= CTX_data_scene(C);
1073         VPaint *wp= scene->toolsettings->wpaint;
1074         Mesh *me;
1075         
1076         me= get_mesh(ob);
1077         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1078         
1079         if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
1080         else ob->mode |= OB_MODE_WEIGHT_PAINT;
1081         
1082         
1083         /* Weightpaint works by overriding colors in mesh,
1084                 * so need to make sure we recalc on enter and
1085                 * exit (exit needs doing regardless because we
1086                                 * should redeform).
1087                 */
1088         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1089         
1090         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1091                 Object *par;
1092                 
1093                 if(wp==NULL)
1094                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1095
1096                 paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
1097                 paint_cursor_start(C, weight_paint_poll);
1098                 
1099                 mesh_octree_table(ob, NULL, NULL, 's');
1100                 
1101                 /* verify if active weight group is also active bone */
1102                 par= modifiers_isDeformedByArmature(ob);
1103                 if(par && (par->mode & OB_MODE_POSE)) {
1104                         bArmature *arm= par->data;
1105
1106                         if(arm->act_bone)
1107                                 ED_vgroup_select_by_name(ob, arm->act_bone->name);
1108                 }
1109         }
1110         else {
1111                 mesh_octree_table(NULL, NULL, NULL, 'e');
1112                 mesh_mirrtopo_table(NULL, 'e');
1113         }
1114         
1115         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1116         
1117         return OPERATOR_FINISHED;
1118 }
1119
1120 /* for switching to/from mode */
1121 static int paint_poll_test(bContext *C)
1122 {
1123         if(CTX_data_edit_object(C))
1124                 return 0;
1125         if(CTX_data_active_object(C)==NULL)
1126                 return 0;
1127         return 1;
1128 }
1129
1130 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1131 {
1132         
1133         /* identifiers */
1134         ot->name= "Weight Paint Mode";
1135         ot->idname= "PAINT_OT_weight_paint_toggle";
1136         
1137         /* api callbacks */
1138         ot->exec= set_wpaint;
1139         ot->poll= paint_poll_test;
1140         
1141         /* flags */
1142         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1143         
1144 }
1145
1146 /* ************ paint radial controls *************/
1147
1148 static int vpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1149 {
1150         Paint *p = paint_get_active(CTX_data_scene(C));
1151         Brush *brush = paint_brush(p);
1152         
1153         WM_paint_cursor_end(CTX_wm_manager(C), p->paint_cursor);
1154         p->paint_cursor = NULL;
1155         brush_radial_control_invoke(op, brush, 1);
1156         return WM_radial_control_invoke(C, op, event);
1157 }
1158
1159 static int vpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1160 {
1161         int ret = WM_radial_control_modal(C, op, event);
1162         if(ret != OPERATOR_RUNNING_MODAL)
1163                 paint_cursor_start(C, vertex_paint_poll);
1164         return ret;
1165 }
1166
1167 static int vpaint_radial_control_exec(bContext *C, wmOperator *op)
1168 {
1169         Brush *brush = paint_brush(&CTX_data_scene(C)->toolsettings->vpaint->paint);
1170         int ret = brush_radial_control_exec(op, brush, 1);
1171         
1172         WM_event_add_notifier(C, NC_BRUSH|NA_EDITED, brush);
1173         
1174         return ret;
1175 }
1176
1177 static int wpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1178 {
1179         Paint *p = paint_get_active(CTX_data_scene(C));
1180         Brush *brush = paint_brush(p);
1181         
1182         WM_paint_cursor_end(CTX_wm_manager(C), p->paint_cursor);
1183         p->paint_cursor = NULL;
1184         brush_radial_control_invoke(op, brush, 1);
1185         return WM_radial_control_invoke(C, op, event);
1186 }
1187
1188 static int wpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1189 {
1190         int ret = WM_radial_control_modal(C, op, event);
1191         if(ret != OPERATOR_RUNNING_MODAL)
1192                 paint_cursor_start(C, weight_paint_poll);
1193         return ret;
1194 }
1195
1196 static int wpaint_radial_control_exec(bContext *C, wmOperator *op)
1197 {
1198         Brush *brush = paint_brush(&CTX_data_scene(C)->toolsettings->wpaint->paint);
1199         int ret = brush_radial_control_exec(op, brush, 1);
1200         
1201         WM_event_add_notifier(C, NC_BRUSH|NA_EDITED, brush);
1202         
1203         return ret;
1204 }
1205
1206 void PAINT_OT_weight_paint_radial_control(wmOperatorType *ot)
1207 {
1208         WM_OT_radial_control_partial(ot);
1209
1210         ot->name= "Weight Paint Radial Control";
1211         ot->idname= "PAINT_OT_weight_paint_radial_control";
1212
1213         ot->invoke= wpaint_radial_control_invoke;
1214         ot->modal= wpaint_radial_control_modal;
1215         ot->exec= wpaint_radial_control_exec;
1216         ot->poll= weight_paint_poll;
1217         
1218         /* flags */
1219         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1220 }
1221
1222 void PAINT_OT_vertex_paint_radial_control(wmOperatorType *ot)
1223 {
1224         WM_OT_radial_control_partial(ot);
1225
1226         ot->name= "Vertex Paint Radial Control";
1227         ot->idname= "PAINT_OT_vertex_paint_radial_control";
1228
1229         ot->invoke= vpaint_radial_control_invoke;
1230         ot->modal= vpaint_radial_control_modal;
1231         ot->exec= vpaint_radial_control_exec;
1232         ot->poll= vertex_paint_poll;
1233         
1234         /* flags */
1235         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1236 }
1237
1238 /* ************ weight paint operator ********** */
1239
1240 struct WPaintData {
1241         ViewContext vc;
1242         int *indexar;
1243         int vgroup_mirror;
1244         float *vertexcosnos;
1245         float wpimat[3][3];
1246         
1247         /*variables for auto normalize*/
1248         int auto_normalize;
1249         char *vgroup_validmap; /*stores if vgroups tie to deforming bones or not*/
1250 };
1251
1252 static char *wpaint_make_validmap(Mesh *me, Object *ob)
1253 {
1254         bDeformGroup *dg;
1255         ModifierData *md;
1256         char *validmap;
1257         bPose *pose;
1258         bPoseChannel *chan;
1259         ArmatureModifierData *amd;
1260         GHash *gh = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
1261         int i = 0, step1=1;
1262
1263         /*add all names to a hash table*/
1264         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1265                 BLI_ghash_insert(gh, dg->name, NULL);
1266         }
1267
1268         if (!i)
1269                 return NULL;
1270
1271         validmap = MEM_callocN(i, "wpaint valid map");
1272
1273         /*now loop through the armature modifiers and identify deform bones*/
1274         for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
1275                 if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
1276                         continue;
1277
1278                 if (md->type == eModifierType_Armature) 
1279                 {
1280                         amd = (ArmatureModifierData*) md;
1281
1282                         if(amd->object && amd->object->pose) {
1283                                 pose = amd->object->pose;
1284                                 
1285                                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1286                                         if (chan->bone->flag & BONE_NO_DEFORM)
1287                                                 continue;
1288
1289                                         if (BLI_ghash_haskey(gh, chan->name)) {
1290                                                 BLI_ghash_remove(gh, chan->name, NULL, NULL);
1291                                                 BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
1292                                         }
1293                                 }
1294                         }
1295                 }
1296         }
1297         
1298         /*add all names to a hash table*/
1299         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1300                 if (BLI_ghash_lookup(gh, dg->name) != NULL) {
1301                         validmap[i] = 1;
1302                 }
1303         }
1304
1305         BLI_ghash_free(gh, NULL, NULL);
1306
1307         return validmap;
1308 }
1309
1310 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *event)
1311 {
1312         Scene *scene= CTX_data_scene(C);
1313         struct PaintStroke *stroke = op->customdata;
1314         ToolSettings *ts= CTX_data_tool_settings(C);
1315         VPaint *wp= ts->wpaint;
1316         Object *ob= CTX_data_active_object(C);
1317         struct WPaintData *wpd;
1318         Mesh *me;
1319         float mat[4][4], imat[4][4];
1320         
1321         if(scene->obedit) return OPERATOR_CANCELLED;
1322         
1323         me= get_mesh(ob);
1324         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1325         
1326         /* if nothing was added yet, we make dverts and a vertex deform group */
1327         if (!me->dvert)
1328                 ED_vgroup_data_create(&me->id);
1329         
1330         /* make mode data storage */
1331         wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1332         paint_stroke_set_mode_data(stroke, wpd);
1333         view3d_set_viewcontext(C, &wpd->vc);
1334         wpd->vgroup_mirror= -1;
1335         
1336         /*set up auto-normalize, and generate map for detecting which
1337           vgroups affect deform bones*/
1338         wpd->auto_normalize = ts->auto_normalize;
1339         if (wpd->auto_normalize)
1340                 wpd->vgroup_validmap = wpaint_make_validmap(me, ob);
1341         
1342         //      if(qual & LR_CTRLKEY) {
1343         //              sample_wpaint(scene, ar, v3d, 0);
1344         //              return;
1345         //      }
1346         //      if(qual & LR_SHIFTKEY) {
1347         //              sample_wpaint(scene, ar, v3d, 1);
1348         //              return;
1349         //      }
1350         
1351         /* ALLOCATIONS! no return after this line */
1352         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
1353         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
1354         wpd->indexar= get_indexarray(me);
1355         copy_wpaint_prev(wp, me->dvert, me->totvert);
1356         
1357         /* this happens on a Bone select, when no vgroup existed yet */
1358         if(ob->actdef<=0) {
1359                 Object *modob;
1360                 if((modob = modifiers_isDeformedByArmature(ob))) {
1361                         Bone *actbone= ((bArmature *)modob->data)->act_bone;
1362                         if(actbone) {
1363                                 bPoseChannel *pchan= get_pose_channel(modob->pose, actbone->name);
1364
1365                                 if(pchan) {
1366                                         bDeformGroup *dg= defgroup_find_name(ob, pchan->name);
1367                                         if(dg==NULL)
1368                                                 dg= ED_vgroup_add_name(ob, pchan->name);        /* sets actdef */
1369                                         else
1370                                                 ob->actdef= 1 + defgroup_find_index(ob, dg);
1371                                 }
1372                         }
1373                 }
1374         }
1375         if(ob->defbase.first==NULL) {
1376                 ED_vgroup_add(ob);
1377         }
1378         
1379         //      if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1380         
1381         /* imat for normals */
1382         mul_m4_m4m4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1383         invert_m4_m4(imat, mat);
1384         copy_m3_m4(wpd->wpimat, imat);
1385         
1386         /* if mirror painting, find the other group */
1387         if(me->editflag & ME_EDIT_MIRROR_X) {
1388                 bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
1389                 if(defgroup) {
1390                         bDeformGroup *curdef;
1391                         int actdef= 0;
1392                         char name[32];
1393
1394                         flip_side_name(name, defgroup->name, FALSE);
1395
1396                         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
1397                                 if (!strcmp(curdef->name, name))
1398                                         break;
1399                         if(curdef==NULL) {
1400                                 int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
1401                                 curdef= ED_vgroup_add_name (ob, name);
1402                                 ob->actdef= olddef;
1403                         }
1404                         
1405                         if(curdef && curdef!=defgroup)
1406                                 wpd->vgroup_mirror= actdef;
1407                 }
1408         }
1409         
1410         return 1;
1411 }
1412
1413 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1414 {
1415         ToolSettings *ts= CTX_data_tool_settings(C);
1416         VPaint *wp= ts->wpaint;
1417         Brush *brush = paint_brush(&wp->paint);
1418         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1419         ViewContext *vc;
1420         Object *ob;
1421         Mesh *me;
1422         float mat[4][4];
1423         float paintweight= ts->vgroup_weight;
1424         int *indexar;
1425         int totindex, index, totw, flip;
1426         float alpha;
1427         float mval[2], pressure;
1428         
1429         /* cannot paint if there is no stroke data */
1430         if (wpd == NULL) {
1431                 // XXX: force a redraw here, since even though we can't paint, 
1432                 // at least view won't freeze until stroke ends
1433                 ED_region_tag_redraw(CTX_wm_region(C));
1434                 return;
1435         }
1436                 
1437         vc= &wpd->vc;
1438         ob= vc->obact;
1439         me= ob->data;
1440         indexar= wpd->indexar;
1441         
1442         view3d_operator_needs_opengl(C);
1443                 
1444         /* load projection matrix */
1445         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1446
1447         flip = RNA_boolean_get(itemptr, "pen_flip");
1448         pressure = RNA_float_get(itemptr, "pressure");
1449         RNA_float_get_array(itemptr, "mouse", mval);
1450         mval[0]-= vc->ar->winrct.xmin;
1451         mval[1]-= vc->ar->winrct.ymin;
1452                         
1453         swap_m4m4(wpd->vc.rv3d->persmat, mat);
1454                         
1455         /* which faces are involved */
1456         if(wp->flag & VP_AREA) {
1457                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1458         }
1459         else {
1460                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1461                 if(indexar[0]) totindex= 1;
1462                 else totindex= 0;
1463         }
1464                         
1465         if(wp->flag & VP_COLINDEX) {
1466                 for(index=0; index<totindex; index++) {
1467                         if(indexar[index] && indexar[index]<=me->totface) {
1468                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1469                                                 
1470                                 if(mface->mat_nr!=ob->actcol-1) {
1471                                         indexar[index]= 0;
1472                                 }
1473                         }
1474                 }
1475         }
1476                         
1477         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
1478                 for(index=0; index<totindex; index++) {
1479                         if(indexar[index] && indexar[index]<=me->totface) {
1480                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1481                                                 
1482                                 if((mface->flag & ME_FACE_SEL)==0) {
1483                                         indexar[index]= 0;
1484                                 }
1485                         }                                       
1486                 }
1487         }
1488                         
1489         /* make sure each vertex gets treated only once */
1490         /* and calculate filter weight */
1491         totw= 0;
1492         if(brush->vertexpaint_tool==VP_BLUR) 
1493                 paintweight= 0.0f;
1494         else
1495                 paintweight= ts->vgroup_weight;
1496                         
1497         for(index=0; index<totindex; index++) {
1498                 if(indexar[index] && indexar[index]<=me->totface) {
1499                         MFace *mface= me->mface + (indexar[index]-1);
1500                                         
1501                         (me->dvert+mface->v1)->flag= 1;
1502                         (me->dvert+mface->v2)->flag= 1;
1503                         (me->dvert+mface->v3)->flag= 1;
1504                         if(mface->v4) (me->dvert+mface->v4)->flag= 1;
1505                                         
1506                         if(brush->vertexpaint_tool==VP_BLUR) {
1507                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, int);
1508                                                 
1509                                 if(wp->flag & VP_ONLYVGROUP)
1510                                         dw_func= (void *)defvert_find_index; /* uses a const, cast to quiet warning */
1511                                 else
1512                                         dw_func= defvert_verify_index;
1513                                                 
1514                                 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
1515                                 if(dw) {paintweight+= dw->weight; totw++;}
1516                                 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
1517                                 if(dw) {paintweight+= dw->weight; totw++;}
1518                                 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
1519                                 if(dw) {paintweight+= dw->weight; totw++;}
1520                                 if(mface->v4) {
1521                                         dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
1522                                         if(dw) {paintweight+= dw->weight; totw++;}
1523                                 }
1524                         }
1525                 }
1526         }
1527                         
1528         if(brush->vertexpaint_tool==VP_BLUR) 
1529                 paintweight/= (float)totw;
1530                         
1531         for(index=0; index<totindex; index++) {
1532                                 
1533                 if(indexar[index] && indexar[index]<=me->totface) {
1534                         MFace *mface= me->mface + (indexar[index]-1);
1535                                         
1536                         if((me->dvert+mface->v1)->flag) {
1537                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
1538                                 if(alpha) {
1539                                         do_weight_paint_vertex(wp, ob, mface->v1, 
1540                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1541                                                 wpd->vgroup_validmap);
1542                                 }
1543                                 (me->dvert+mface->v1)->flag= 0;
1544                         }
1545                                         
1546                         if((me->dvert+mface->v2)->flag) {
1547                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
1548                                 if(alpha) {
1549                                         do_weight_paint_vertex(wp, ob, mface->v2, 
1550                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1551                                                 wpd->vgroup_validmap);
1552                                 }
1553                                 (me->dvert+mface->v2)->flag= 0;
1554                         }
1555                                         
1556                         if((me->dvert+mface->v3)->flag) {
1557                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
1558                                 if(alpha) {
1559                                         do_weight_paint_vertex(wp, ob, mface->v3, 
1560                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1561                                                 wpd->vgroup_validmap);
1562                                 }
1563                                 (me->dvert+mface->v3)->flag= 0;
1564                         }
1565                                         
1566                         if((me->dvert+mface->v4)->flag) {
1567                                 if(mface->v4) {
1568                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
1569                                         if(alpha) {
1570                                                 do_weight_paint_vertex(wp, ob, mface->v4, 
1571                                                         alpha, paintweight, flip, wpd->vgroup_mirror,
1572                                                         wpd->vgroup_validmap);
1573                                         }
1574                                         (me->dvert+mface->v4)->flag= 0;
1575                                 }
1576                         }
1577                 }
1578         }
1579                         
1580         swap_m4m4(vc->rv3d->persmat, mat);
1581                         
1582         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
1583         ED_region_tag_redraw(vc->ar);
1584 }
1585
1586 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
1587 {
1588         ToolSettings *ts= CTX_data_tool_settings(C);
1589         Object *ob= CTX_data_active_object(C);
1590         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1591         
1592         if(wpd) {
1593                 if(wpd->vertexcosnos)
1594                         MEM_freeN(wpd->vertexcosnos);
1595                 MEM_freeN(wpd->indexar);
1596                 
1597                 if (wpd->vgroup_validmap)
1598                         MEM_freeN(wpd->vgroup_validmap);
1599                 
1600                 MEM_freeN(wpd);
1601         }
1602         
1603         /* frees prev buffer */
1604         copy_wpaint_prev(ts->wpaint, NULL, 0);
1605         
1606         /* and particles too */
1607         if(ob->particlesystem.first) {
1608                 ParticleSystem *psys;
1609                 int i;
1610                 
1611                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
1612                         for(i=0; i<PSYS_TOT_VG; i++) {
1613                                 if(psys->vgroup[i]==ob->actdef) {
1614                                         psys->recalc |= PSYS_RECALC_RESET;
1615                                         break;
1616                                 }
1617                         }
1618                 }
1619         }
1620         
1621         DAG_id_flush_update(ob->data, OB_RECALC_DATA);  
1622 }
1623
1624
1625 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1626 {
1627         
1628         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
1629                                           wpaint_stroke_update_step,
1630                                           wpaint_stroke_done);
1631         
1632         /* add modal handler */
1633         WM_event_add_modal_handler(C, op);
1634
1635         op->type->modal(C, op, event);
1636         
1637         return OPERATOR_RUNNING_MODAL;
1638 }
1639
1640 void PAINT_OT_weight_paint(wmOperatorType *ot)
1641 {
1642         
1643         /* identifiers */
1644         ot->name= "Weight Paint";
1645         ot->idname= "PAINT_OT_weight_paint";
1646         
1647         /* api callbacks */
1648         ot->invoke= wpaint_invoke;
1649         ot->modal= paint_stroke_modal;
1650         /* ot->exec= vpaint_exec; <-- needs stroke property */
1651         ot->poll= weight_paint_poll;
1652         
1653         /* flags */
1654         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1655
1656         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1657 }
1658
1659 static int weight_paint_set_exec(bContext *C, wmOperator *op)
1660 {
1661         struct Scene *scene= CTX_data_scene(C);
1662         Object *obact = CTX_data_active_object(C);
1663
1664         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
1665         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
1666         return OPERATOR_FINISHED;
1667 }
1668
1669 void PAINT_OT_weight_set(wmOperatorType *ot)
1670 {
1671         /* identifiers */
1672         ot->name= "Weight Set";
1673         ot->idname= "PAINT_OT_weight_set";
1674
1675         /* api callbacks */
1676         ot->exec= weight_paint_set_exec;
1677         ot->poll= facemask_paint_poll;
1678
1679         /* flags */
1680         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1681 }
1682
1683 /* ************ set / clear vertex paint mode ********** */
1684
1685
1686 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
1687 {       
1688         Object *ob= CTX_data_active_object(C);
1689         Scene *scene= CTX_data_scene(C);
1690         VPaint *vp= scene->toolsettings->vpaint;
1691         Mesh *me;
1692         
1693         me= get_mesh(ob);
1694         
1695         if(me==NULL || object_data_is_libdata(ob)) {
1696                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
1697                 return OPERATOR_PASS_THROUGH;
1698         }
1699         
1700         if(me && me->mcol==NULL) make_vertexcol(ob);
1701         
1702         /* toggle: end vpaint */
1703         if(ob->mode & OB_MODE_VERTEX_PAINT) {
1704                 
1705                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
1706         }
1707         else {
1708                 ob->mode |= OB_MODE_VERTEX_PAINT;
1709                 /* Turn off weight painting */
1710                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
1711                         set_wpaint(C, op);
1712                 
1713                 if(vp==NULL)
1714                         vp= scene->toolsettings->vpaint= new_vpaint(0);
1715                 
1716                 paint_cursor_start(C, vertex_paint_poll);
1717
1718                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
1719         }
1720         
1721         if (me)
1722                 /* update modifier stack for mapping requirements */
1723                 DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1724         
1725         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1726         
1727         return OPERATOR_FINISHED;
1728 }
1729
1730 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
1731 {
1732         
1733         /* identifiers */
1734         ot->name= "Vertex Paint Mode";
1735         ot->idname= "PAINT_OT_vertex_paint_toggle";
1736         
1737         /* api callbacks */
1738         ot->exec= set_vpaint;
1739         ot->poll= paint_poll_test;
1740         
1741         /* flags */
1742         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1743 }
1744
1745
1746
1747 /* ********************** vertex paint operator ******************* */
1748
1749 /* Implementation notes:
1750
1751 Operator->invoke()
1752   - validate context (add mcol)
1753   - create customdata storage
1754   - call paint once (mouse click)
1755   - add modal handler 
1756
1757 Operator->modal()
1758   - for every mousemove, apply vertex paint
1759   - exit on mouse release, free customdata
1760         (return OPERATOR_FINISHED also removes handler and operator)
1761
1762 For future:
1763   - implement a stroke event (or mousemove with past positons)
1764   - revise whether op->customdata should be added in object, in set_vpaint
1765
1766 */
1767
1768 typedef struct VPaintData {
1769         ViewContext vc;
1770         unsigned int paintcol;
1771         int *indexar;
1772         float *vertexcosnos;
1773         float vpimat[3][3];
1774 } VPaintData;
1775
1776 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *event)
1777 {
1778         ToolSettings *ts= CTX_data_tool_settings(C);
1779         struct PaintStroke *stroke = op->customdata;
1780         VPaint *vp= ts->vpaint;
1781         struct VPaintData *vpd;
1782         Object *ob= CTX_data_active_object(C);
1783         Mesh *me;
1784         float mat[4][4], imat[4][4];
1785
1786         /* context checks could be a poll() */
1787         me= get_mesh(ob);
1788         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1789         
1790         if(me->mcol==NULL) make_vertexcol(ob);
1791         if(me->mcol==NULL) return OPERATOR_CANCELLED;
1792         
1793         /* make mode data storage */
1794         vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
1795         paint_stroke_set_mode_data(stroke, vpd);
1796         view3d_set_viewcontext(C, &vpd->vc);
1797         
1798         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
1799         vpd->indexar= get_indexarray(me);
1800         vpd->paintcol= vpaint_get_current_col(vp);
1801         
1802         /* for filtering */
1803         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
1804         
1805         /* some old cruft to sort out later */
1806         mul_m4_m4m4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
1807         invert_m4_m4(imat, mat);
1808         copy_m3_m4(vpd->vpimat, imat);
1809
1810         return 1;
1811 }
1812
1813 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index, float mval[2], float pressure, int flip)
1814 {
1815         ViewContext *vc = &vpd->vc;
1816         Brush *brush = paint_brush(&vp->paint);
1817         Mesh *me = get_mesh(ob);
1818         MFace *mface= ((MFace*)me->mface) + index;
1819         unsigned int *mcol= ((unsigned int*)me->mcol) + 4*index;
1820         unsigned int *mcolorig= ((unsigned int*)vp->vpaint_prev) + 4*index;
1821         float alpha;
1822         int i;
1823         
1824         if((vp->flag & VP_COLINDEX && mface->mat_nr!=ob->actcol-1) ||
1825            ((me->editflag & ME_EDIT_PAINT_MASK) && !(mface->flag & ME_FACE_SEL)))
1826                 return;
1827
1828         if(brush->vertexpaint_tool==VP_BLUR) {
1829                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
1830                 if(mface->v4) {
1831                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
1832                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
1833                 }
1834                 else {
1835                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
1836                 }
1837                 
1838         }
1839
1840         for(i = 0; i < (mface->v4 ? 4 : 3); ++i) {
1841                 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*(&mface->v1)[i], mval, pressure);
1842                 if(alpha)
1843                         vpaint_blend(vp, mcol+i, mcolorig+i, vpd->paintcol, (int)(alpha*255.0));
1844         }
1845 }
1846
1847 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1848 {
1849         ToolSettings *ts= CTX_data_tool_settings(C);
1850         struct VPaintData *vpd = paint_stroke_mode_data(stroke);
1851         VPaint *vp= ts->vpaint;
1852         Brush *brush = paint_brush(&vp->paint);
1853         ViewContext *vc= &vpd->vc;
1854         Object *ob= vc->obact;
1855         Mesh *me= ob->data;
1856         float mat[4][4];
1857         int *indexar= vpd->indexar;
1858         int totindex, index, flip;
1859         float pressure, mval[2];
1860
1861         RNA_float_get_array(itemptr, "mouse", mval);
1862         flip = RNA_boolean_get(itemptr, "pen_flip");
1863         pressure = RNA_float_get(itemptr, "pressure");
1864                         
1865         view3d_operator_needs_opengl(C);
1866                         
1867         /* load projection matrix */
1868         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1869
1870         mval[0]-= vc->ar->winrct.xmin;
1871         mval[1]-= vc->ar->winrct.ymin;
1872
1873                         
1874         /* which faces are involved */
1875         if(vp->flag & VP_AREA) {
1876                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1877         }
1878         else {
1879                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1880                 if(indexar[0]) totindex= 1;
1881                 else totindex= 0;
1882         }
1883                         
1884         swap_m4m4(vc->rv3d->persmat, mat);
1885                         
1886         for(index=0; index<totindex; index++) {                         
1887                 if(indexar[index] && indexar[index]<=me->totface)
1888                         vpaint_paint_face(vp, vpd, ob, indexar[index]-1, mval, pressure, flip);
1889         }
1890                                                 
1891         swap_m4m4(vc->rv3d->persmat, mat);
1892
1893         /* was disabled because it is slow, but necessary for blur */
1894         if(brush->vertexpaint_tool == VP_BLUR)
1895                 do_shared_vertexcol(me);
1896                         
1897         ED_region_tag_redraw(vc->ar);
1898                         
1899         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
1900 }
1901
1902 static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
1903 {
1904         ToolSettings *ts= CTX_data_tool_settings(C);
1905         struct VPaintData *vpd= paint_stroke_mode_data(stroke);
1906         
1907         if(vpd->vertexcosnos)
1908                 MEM_freeN(vpd->vertexcosnos);
1909         MEM_freeN(vpd->indexar);
1910         
1911         /* frees prev buffer */
1912         copy_vpaint_prev(ts->vpaint, NULL, 0);
1913         
1914         MEM_freeN(vpd);
1915 }
1916
1917 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1918 {
1919         
1920         op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
1921                                           vpaint_stroke_update_step,
1922                                           vpaint_stroke_done);
1923         
1924         /* add modal handler */
1925         WM_event_add_modal_handler(C, op);
1926
1927         op->type->modal(C, op, event);
1928         
1929         return OPERATOR_RUNNING_MODAL;
1930 }
1931
1932 void PAINT_OT_vertex_paint(wmOperatorType *ot)
1933 {
1934         /* identifiers */
1935         ot->name= "Vertex Paint";
1936         ot->idname= "PAINT_OT_vertex_paint";
1937         
1938         /* api callbacks */
1939         ot->invoke= vpaint_invoke;
1940         ot->modal= paint_stroke_modal;
1941         /* ot->exec= vpaint_exec; <-- needs stroke property */
1942         ot->poll= vertex_paint_poll;
1943         
1944         /* flags */
1945         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1946
1947         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1948 }
1949
1950 /* ********************** weight from bones operator ******************* */
1951
1952 static int weight_from_bones_poll(bContext *C)
1953 {
1954         Object *ob= CTX_data_active_object(C);
1955
1956         return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob));
1957 }
1958
1959 static int weight_from_bones_exec(bContext *C, wmOperator *op)
1960 {
1961         Scene *scene= CTX_data_scene(C);
1962         Object *ob= CTX_data_active_object(C);
1963         Object *armob= modifiers_isDeformedByArmature(ob);
1964         Mesh *me= ob->data;
1965         int type= RNA_enum_get(op->ptr, "type");
1966
1967         create_vgroups_from_armature(scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X));
1968
1969         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1970         WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
1971
1972         return OPERATOR_FINISHED;
1973 }
1974
1975 void PAINT_OT_weight_from_bones(wmOperatorType *ot)
1976 {
1977         static EnumPropertyItem type_items[]= {
1978                 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights froms bones"},
1979                 {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"},
1980                 {0, NULL, 0, NULL, NULL}};
1981
1982         /* identifiers */
1983         ot->name= "Weight from Bones";
1984         ot->idname= "PAINT_OT_weight_from_bones";
1985         
1986         /* api callbacks */
1987         ot->exec= weight_from_bones_exec;
1988         ot->invoke= WM_menu_invoke;
1989         ot->poll= weight_from_bones_poll;
1990         
1991         /* flags */
1992         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1993
1994         /* properties */
1995         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights.");
1996 }
1997