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