== Sculpt/Paint Fixes ==
[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
52 #include "RNA_access.h"
53 #include "RNA_define.h"
54
55 #include "BKE_armature.h"
56 #include "BKE_action.h"
57 #include "BKE_brush.h"
58 #include "BKE_DerivedMesh.h"
59 #include "BKE_cloth.h"
60 #include "BKE_context.h"
61 #include "BKE_customdata.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_deform.h"
64 #include "BKE_displist.h"
65 #include "BKE_global.h"
66 #include "BKE_mesh.h"
67 #include "BKE_modifier.h"
68 #include "BKE_object.h"
69 #include "BKE_paint.h"
70 #include "BKE_utildefines.h"
71
72 #include "WM_api.h"
73 #include "WM_types.h"
74
75 #include "BIF_gl.h"
76 #include "BIF_glutil.h"
77
78 #include "ED_armature.h"
79 #include "ED_mesh.h"
80 #include "ED_screen.h"
81 #include "ED_view3d.h"
82
83 #include "paint_intern.h"
84
85 /* brush->vertexpaint_tool */
86 #define VP_MIX  0
87 #define VP_ADD  1
88 #define VP_SUB  2
89 #define VP_MUL  3
90 #define VP_BLUR 4
91 #define VP_LIGHTEN      5
92 #define VP_DARKEN       6
93
94 /* polling - retrieve whether cursor should be set or operator should be done */
95
96
97 /* Returns true if vertex paint mode is active */
98 int vertex_paint_mode_poll(bContext *C)
99 {
100         Object *ob = CTX_data_active_object(C);
101
102         return ob && ob->mode == OB_MODE_VERTEX_PAINT;
103 }
104
105 int vertex_paint_poll(bContext *C)
106 {
107         if(vertex_paint_mode_poll(C) && 
108            paint_brush(&CTX_data_tool_settings(C)->vpaint->paint)) {
109                 ScrArea *sa= CTX_wm_area(C);
110                 if(sa->spacetype==SPACE_VIEW3D) {
111                         ARegion *ar= CTX_wm_region(C);
112                         if(ar->regiontype==RGN_TYPE_WINDOW)
113                                 return 1;
114                 }
115         }
116         return 0;
117 }
118
119 int weight_paint_mode_poll(bContext *C)
120 {
121         Object *ob = CTX_data_active_object(C);
122
123         return ob && ob->mode == OB_MODE_WEIGHT_PAINT;
124 }
125
126 int weight_paint_poll(bContext *C)
127 {
128         Object *ob = CTX_data_active_object(C);
129
130         if(ob && ob->mode & OB_MODE_WEIGHT_PAINT &&
131            paint_brush(&CTX_data_tool_settings(C)->wpaint->paint)) {
132                 ScrArea *sa= CTX_wm_area(C);
133                 if(sa->spacetype==SPACE_VIEW3D) {
134                         ARegion *ar= CTX_wm_region(C);
135                         if(ar->regiontype==RGN_TYPE_WINDOW)
136                                 return 1;
137                 }
138         }
139         return 0;
140 }
141
142 static VPaint *new_vpaint(int wpaint)
143 {
144         VPaint *vp= MEM_callocN(sizeof(VPaint), "VPaint");
145         
146         vp->flag= VP_AREA+VP_SPRAY;
147         
148         if(wpaint)
149                 vp->flag= VP_AREA;
150
151         return vp;
152 }
153
154 static int *get_indexarray(Mesh *me)
155 {
156         return MEM_mallocN(sizeof(int)*(me->totface+1), "vertexpaint");
157 }
158
159
160 /* in contradiction to cpack drawing colors, the MCOL colors (vpaint colors) are per byte! 
161    so not endian sensitive. Mcol = ABGR!!! so be cautious with cpack calls */
162
163 unsigned int rgba_to_mcol(float r, float g, float b, float a)
164 {
165         int ir, ig, ib, ia;
166         unsigned int col;
167         char *cp;
168         
169         ir= floor(255.0*r);
170         if(ir<0) ir= 0; else if(ir>255) ir= 255;
171         ig= floor(255.0*g);
172         if(ig<0) ig= 0; else if(ig>255) ig= 255;
173         ib= floor(255.0*b);
174         if(ib<0) ib= 0; else if(ib>255) ib= 255;
175         ia= floor(255.0*a);
176         if(ia<0) ia= 0; else if(ia>255) ia= 255;
177         
178         cp= (char *)&col;
179         cp[0]= ia;
180         cp[1]= ib;
181         cp[2]= ig;
182         cp[3]= ir;
183         
184         return col;
185         
186 }
187
188 unsigned int vpaint_get_current_col(VPaint *vp)
189 {
190         Brush *brush = paint_brush(&vp->paint);
191         return rgba_to_mcol(brush->rgb[0], brush->rgb[1], brush->rgb[2], 1.0f);
192 }
193
194 static void do_shared_vertexcol(Mesh *me)
195 {
196         /* if no mcol: do not do */
197         /* if tface: only the involved faces, otherwise all */
198         MFace *mface;
199         MTFace *tface;
200         int a;
201         short *scolmain, *scol;
202         char *mcol;
203         
204         if(me->mcol==0 || me->totvert==0 || me->totface==0) return;
205         
206         scolmain= MEM_callocN(4*sizeof(short)*me->totvert, "colmain");
207         
208         tface= me->mtface;
209         mface= me->mface;
210         mcol= (char *)me->mcol;
211         for(a=me->totface; a>0; a--, mface++, mcol+=16) {
212                 if((tface && tface->mode & TF_SHAREDCOL) || (me->editflag & ME_EDIT_PAINT_MASK)==0) {
213                         scol= scolmain+4*mface->v1;
214                         scol[0]++; scol[1]+= mcol[1]; scol[2]+= mcol[2]; scol[3]+= mcol[3];
215                         scol= scolmain+4*mface->v2;
216                         scol[0]++; scol[1]+= mcol[5]; scol[2]+= mcol[6]; scol[3]+= mcol[7];
217                         scol= scolmain+4*mface->v3;
218                         scol[0]++; scol[1]+= mcol[9]; scol[2]+= mcol[10]; scol[3]+= mcol[11];
219                         if(mface->v4) {
220                                 scol= scolmain+4*mface->v4;
221                                 scol[0]++; scol[1]+= mcol[13]; scol[2]+= mcol[14]; scol[3]+= mcol[15];
222                         }
223                 }
224                 if(tface) tface++;
225         }
226         
227         a= me->totvert;
228         scol= scolmain;
229         while(a--) {
230                 if(scol[0]>1) {
231                         scol[1]/= scol[0];
232                         scol[2]/= scol[0];
233                         scol[3]/= scol[0];
234                 }
235                 scol+= 4;
236         }
237         
238         tface= me->mtface;
239         mface= me->mface;
240         mcol= (char *)me->mcol;
241         for(a=me->totface; a>0; a--, mface++, mcol+=16) {
242                 if((tface && tface->mode & TF_SHAREDCOL) || (me->editflag & ME_EDIT_PAINT_MASK)==0) {
243                         scol= scolmain+4*mface->v1;
244                         mcol[1]= scol[1]; mcol[2]= scol[2]; mcol[3]= scol[3];
245                         scol= scolmain+4*mface->v2;
246                         mcol[5]= scol[1]; mcol[6]= scol[2]; mcol[7]= scol[3];
247                         scol= scolmain+4*mface->v3;
248                         mcol[9]= scol[1]; mcol[10]= scol[2]; mcol[11]= scol[3];
249                         if(mface->v4) {
250                                 scol= scolmain+4*mface->v4;
251                                 mcol[13]= scol[1]; mcol[14]= scol[2]; mcol[15]= scol[3];
252                         }
253                 }
254                 if(tface) tface++;
255         }
256
257         MEM_freeN(scolmain);
258 }
259
260 static void make_vertexcol(Object *ob)  /* single ob */
261 {
262         Mesh *me;
263         if(!ob || ob->id.lib) return;
264         me= get_mesh(ob);
265         if(me==0) return;
266         if(me->edit_mesh) return;
267
268         /* copies from shadedisplist to mcol */
269         if(!me->mcol) {
270                 CustomData_add_layer(&me->fdata, CD_MCOL, CD_CALLOC, NULL, me->totface);
271                 mesh_update_customdata_pointers(me);
272         }
273
274         //if(shade)
275         //      shadeMeshMCol(scene, ob, me);
276         //else
277
278         memset(me->mcol, 255, 4*sizeof(MCol)*me->totface);
279         
280         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
281         
282 }
283
284 static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
285 {
286         if(vp->vpaint_prev) {
287                 MEM_freeN(vp->vpaint_prev);
288                 vp->vpaint_prev= NULL;
289         }
290         vp->tot= tot;   
291         
292         if(mcol==NULL || tot==0) return;
293         
294         vp->vpaint_prev= MEM_mallocN(4*sizeof(int)*tot, "vpaint_prev");
295         memcpy(vp->vpaint_prev, mcol, 4*sizeof(int)*tot);
296         
297 }
298
299 static void copy_wpaint_prev (VPaint *wp, MDeformVert *dverts, int dcount)
300 {
301         if (wp->wpaint_prev) {
302                 free_dverts(wp->wpaint_prev, wp->tot);
303                 wp->wpaint_prev= NULL;
304         }
305         
306         if(dverts && dcount) {
307                 
308                 wp->wpaint_prev = MEM_mallocN (sizeof(MDeformVert)*dcount, "wpaint prev");
309                 wp->tot = dcount;
310                 copy_dverts (wp->wpaint_prev, dverts, dcount);
311         }
312 }
313
314
315 void vpaint_fill(Object *ob, unsigned int paintcol)
316 {
317         Mesh *me;
318         MFace *mf;
319         unsigned int *mcol;
320         int i, selected;
321
322         me= get_mesh(ob);
323         if(me==0 || me->totface==0) return;
324
325         if(!me->mcol)
326                 make_vertexcol(ob);
327
328         selected= (me->editflag & ME_EDIT_PAINT_MASK);
329
330         mf = me->mface;
331         mcol = (unsigned int*)me->mcol;
332         for (i = 0; i < me->totface; i++, mf++, mcol+=4) {
333                 if (!selected || mf->flag & ME_FACE_SEL) {
334                         mcol[0] = paintcol;
335                         mcol[1] = paintcol;
336                         mcol[2] = paintcol;
337                         mcol[3] = paintcol;
338                 }
339         }
340         
341         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
342 }
343
344
345 /* fills in the selected faces with the current weight and vertex group */
346 void wpaint_fill(VPaint *wp, Object *ob, float paintweight)
347 {
348         Mesh *me;
349         MFace *mface;
350         MDeformWeight *dw, *uw;
351         int *indexar;
352         int index, vgroup;
353         unsigned int faceverts[5]={0,0,0,0,0};
354         unsigned char i;
355         int vgroup_mirror= -1;
356         int selected;
357         
358         me= ob->data;
359         if(me==0 || me->totface==0 || me->dvert==0 || !me->mface) return;
360         
361         selected= (me->editflag & ME_EDIT_PAINT_MASK);
362
363         indexar= get_indexarray(me);
364
365         if(selected) {
366                 for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
367                         if((mface->flag & ME_FACE_SEL)==0)
368                                 indexar[index]= 0;
369                         else
370                                 indexar[index]= index+1;
371                 }
372         }
373         else {
374                 for(index=0; index<me->totface; index++)
375                         indexar[index]= index+1;
376         }
377         
378         vgroup= ob->actdef-1;
379         
380         /* directly copied from weight_paint, should probaby split into a separate function */
381         /* if mirror painting, find the other group */          
382         if(me->editflag & ME_EDIT_MIRROR_X) {
383                 bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
384                 if(defgroup) {
385                         bDeformGroup *curdef;
386                         int actdef= 0;
387                         char name[32];
388
389                         BLI_strncpy(name, defgroup->name, 32);
390                         bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
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                         BLI_strncpy(name, defgroup->name, 32);
1395                         bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
1396                         
1397                         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
1398                                 if (!strcmp(curdef->name, name))
1399                                         break;
1400                         if(curdef==NULL) {
1401                                 int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
1402                                 curdef= ED_vgroup_add_name (ob, name);
1403                                 ob->actdef= olddef;
1404                         }
1405                         
1406                         if(curdef && curdef!=defgroup)
1407                                 wpd->vgroup_mirror= actdef;
1408                 }
1409         }
1410         
1411         return 1;
1412 }
1413
1414 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1415 {
1416         ToolSettings *ts= CTX_data_tool_settings(C);
1417         VPaint *wp= ts->wpaint;
1418         Brush *brush = paint_brush(&wp->paint);
1419         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1420         ViewContext *vc;
1421         Object *ob;
1422         Mesh *me;
1423         float mat[4][4];
1424         float paintweight= ts->vgroup_weight;
1425         int *indexar;
1426         int totindex, index, totw, flip;
1427         float alpha;
1428         float mval[2], pressure;
1429         
1430         /* cannot paint if there is no stroke data */
1431         if (wpd == NULL) {
1432                 // XXX: force a redraw here, since even though we can't paint, 
1433                 // at least view won't freeze until stroke ends
1434                 ED_region_tag_redraw(CTX_wm_region(C));
1435                 return;
1436         }
1437                 
1438         vc= &wpd->vc;
1439         ob= vc->obact;
1440         me= ob->data;
1441         indexar= wpd->indexar;
1442         
1443         view3d_operator_needs_opengl(C);
1444                 
1445         /* load projection matrix */
1446         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1447
1448         flip = RNA_boolean_get(itemptr, "flip");
1449         pressure = RNA_float_get(itemptr, "pressure");
1450         RNA_float_get_array(itemptr, "mouse", mval);
1451         mval[0]-= vc->ar->winrct.xmin;
1452         mval[1]-= vc->ar->winrct.ymin;
1453                         
1454         swap_m4m4(wpd->vc.rv3d->persmat, mat);
1455                         
1456         /* which faces are involved */
1457         if(wp->flag & VP_AREA) {
1458                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1459         }
1460         else {
1461                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1462                 if(indexar[0]) totindex= 1;
1463                 else totindex= 0;
1464         }
1465                         
1466         if(wp->flag & VP_COLINDEX) {
1467                 for(index=0; index<totindex; index++) {
1468                         if(indexar[index] && indexar[index]<=me->totface) {
1469                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1470                                                 
1471                                 if(mface->mat_nr!=ob->actcol-1) {
1472                                         indexar[index]= 0;
1473                                 }
1474                         }
1475                 }
1476         }
1477                         
1478         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
1479                 for(index=0; index<totindex; index++) {
1480                         if(indexar[index] && indexar[index]<=me->totface) {
1481                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1482                                                 
1483                                 if((mface->flag & ME_FACE_SEL)==0) {
1484                                         indexar[index]= 0;
1485                                 }
1486                         }                                       
1487                 }
1488         }
1489                         
1490         /* make sure each vertex gets treated only once */
1491         /* and calculate filter weight */
1492         totw= 0;
1493         if(brush->vertexpaint_tool==VP_BLUR) 
1494                 paintweight= 0.0f;
1495         else
1496                 paintweight= ts->vgroup_weight;
1497                         
1498         for(index=0; index<totindex; index++) {
1499                 if(indexar[index] && indexar[index]<=me->totface) {
1500                         MFace *mface= me->mface + (indexar[index]-1);
1501                                         
1502                         (me->dvert+mface->v1)->flag= 1;
1503                         (me->dvert+mface->v2)->flag= 1;
1504                         (me->dvert+mface->v3)->flag= 1;
1505                         if(mface->v4) (me->dvert+mface->v4)->flag= 1;
1506                                         
1507                         if(brush->vertexpaint_tool==VP_BLUR) {
1508                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, int);
1509                                                 
1510                                 if(wp->flag & VP_ONLYVGROUP)
1511                                         dw_func= (void *)defvert_find_index; /* uses a const, cast to quiet warning */
1512                                 else
1513                                         dw_func= defvert_verify_index;
1514                                                 
1515                                 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
1516                                 if(dw) {paintweight+= dw->weight; totw++;}
1517                                 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
1518                                 if(dw) {paintweight+= dw->weight; totw++;}
1519                                 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
1520                                 if(dw) {paintweight+= dw->weight; totw++;}
1521                                 if(mface->v4) {
1522                                         dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
1523                                         if(dw) {paintweight+= dw->weight; totw++;}
1524                                 }
1525                         }
1526                 }
1527         }
1528                         
1529         if(brush->vertexpaint_tool==VP_BLUR) 
1530                 paintweight/= (float)totw;
1531                         
1532         for(index=0; index<totindex; index++) {
1533                                 
1534                 if(indexar[index] && indexar[index]<=me->totface) {
1535                         MFace *mface= me->mface + (indexar[index]-1);
1536                                         
1537                         if((me->dvert+mface->v1)->flag) {
1538                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
1539                                 if(alpha) {
1540                                         do_weight_paint_vertex(wp, ob, mface->v1, 
1541                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1542                                                 wpd->vgroup_validmap);
1543                                 }
1544                                 (me->dvert+mface->v1)->flag= 0;
1545                         }
1546                                         
1547                         if((me->dvert+mface->v2)->flag) {
1548                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
1549                                 if(alpha) {
1550                                         do_weight_paint_vertex(wp, ob, mface->v2, 
1551                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1552                                                 wpd->vgroup_validmap);
1553                                 }
1554                                 (me->dvert+mface->v2)->flag= 0;
1555                         }
1556                                         
1557                         if((me->dvert+mface->v3)->flag) {
1558                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
1559                                 if(alpha) {
1560                                         do_weight_paint_vertex(wp, ob, mface->v3, 
1561                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1562                                                 wpd->vgroup_validmap);
1563                                 }
1564                                 (me->dvert+mface->v3)->flag= 0;
1565                         }
1566                                         
1567                         if((me->dvert+mface->v4)->flag) {
1568                                 if(mface->v4) {
1569                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
1570                                         if(alpha) {
1571                                                 do_weight_paint_vertex(wp, ob, mface->v4, 
1572                                                         alpha, paintweight, flip, wpd->vgroup_mirror,
1573                                                         wpd->vgroup_validmap);
1574                                         }
1575                                         (me->dvert+mface->v4)->flag= 0;
1576                                 }
1577                         }
1578                 }
1579         }
1580                         
1581         swap_m4m4(vc->rv3d->persmat, mat);
1582                         
1583         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
1584         ED_region_tag_redraw(vc->ar);
1585 }
1586
1587 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
1588 {
1589         ToolSettings *ts= CTX_data_tool_settings(C);
1590         Object *ob= CTX_data_active_object(C);
1591         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1592         
1593         if(wpd) {
1594                 if(wpd->vertexcosnos)
1595                         MEM_freeN(wpd->vertexcosnos);
1596                 MEM_freeN(wpd->indexar);
1597                 
1598                 if (wpd->vgroup_validmap)
1599                         MEM_freeN(wpd->vgroup_validmap);
1600                 
1601                 MEM_freeN(wpd);
1602         }
1603         
1604         /* frees prev buffer */
1605         copy_wpaint_prev(ts->wpaint, NULL, 0);
1606         
1607         /* and particles too */
1608         if(ob->particlesystem.first) {
1609                 ParticleSystem *psys;
1610                 int i;
1611                 
1612                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
1613                         for(i=0; i<PSYS_TOT_VG; i++) {
1614                                 if(psys->vgroup[i]==ob->actdef) {
1615                                         psys->recalc |= PSYS_RECALC_RESET;
1616                                         break;
1617                                 }
1618                         }
1619                 }
1620         }
1621         
1622         DAG_id_flush_update(ob->data, OB_RECALC_DATA);  
1623 }
1624
1625
1626 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1627 {
1628         
1629         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
1630                                           wpaint_stroke_update_step,
1631                                           wpaint_stroke_done);
1632         
1633         /* add modal handler */
1634         WM_event_add_modal_handler(C, op);
1635
1636         op->type->modal(C, op, event);
1637         
1638         return OPERATOR_RUNNING_MODAL;
1639 }
1640
1641 void PAINT_OT_weight_paint(wmOperatorType *ot)
1642 {
1643         
1644         /* identifiers */
1645         ot->name= "Weight Paint";
1646         ot->idname= "PAINT_OT_weight_paint";
1647         
1648         /* api callbacks */
1649         ot->invoke= wpaint_invoke;
1650         ot->modal= paint_stroke_modal;
1651         /* ot->exec= vpaint_exec; <-- needs stroke property */
1652         ot->poll= weight_paint_poll;
1653         
1654         /* flags */
1655         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1656
1657         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1658 }
1659
1660 static int weight_paint_set_exec(bContext *C, wmOperator *op)
1661 {
1662         struct Scene *scene= CTX_data_scene(C);
1663         Object *obact = CTX_data_active_object(C);
1664
1665         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
1666         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
1667         return OPERATOR_FINISHED;
1668 }
1669
1670 void PAINT_OT_weight_set(wmOperatorType *ot)
1671 {
1672         /* identifiers */
1673         ot->name= "Weight Set";
1674         ot->idname= "PAINT_OT_weight_set";
1675
1676         /* api callbacks */
1677         ot->exec= weight_paint_set_exec;
1678         ot->poll= facemask_paint_poll;
1679
1680         /* flags */
1681         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1682 }
1683
1684 /* ************ set / clear vertex paint mode ********** */
1685
1686
1687 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
1688 {       
1689         Object *ob= CTX_data_active_object(C);
1690         Scene *scene= CTX_data_scene(C);
1691         VPaint *vp= scene->toolsettings->vpaint;
1692         Mesh *me;
1693         
1694         me= get_mesh(ob);
1695         
1696         if(me==NULL || object_data_is_libdata(ob)) {
1697                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
1698                 return OPERATOR_PASS_THROUGH;
1699         }
1700         
1701         if(me && me->mcol==NULL) make_vertexcol(ob);
1702         
1703         /* toggle: end vpaint */
1704         if(ob->mode & OB_MODE_VERTEX_PAINT) {
1705                 
1706                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
1707         }
1708         else {
1709                 ob->mode |= OB_MODE_VERTEX_PAINT;
1710                 /* Turn off weight painting */
1711                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
1712                         set_wpaint(C, op);
1713                 
1714                 if(vp==NULL)
1715                         vp= scene->toolsettings->vpaint= new_vpaint(0);
1716                 
1717                 paint_cursor_start(C, vertex_paint_poll);
1718
1719                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
1720         }
1721         
1722         if (me)
1723                 /* update modifier stack for mapping requirements */
1724                 DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1725         
1726         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1727         
1728         return OPERATOR_FINISHED;
1729 }
1730
1731 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
1732 {
1733         
1734         /* identifiers */
1735         ot->name= "Vertex Paint Mode";
1736         ot->idname= "PAINT_OT_vertex_paint_toggle";
1737         
1738         /* api callbacks */
1739         ot->exec= set_vpaint;
1740         ot->poll= paint_poll_test;
1741         
1742         /* flags */
1743         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1744 }
1745
1746
1747
1748 /* ********************** vertex paint operator ******************* */
1749
1750 /* Implementation notes:
1751
1752 Operator->invoke()
1753   - validate context (add mcol)
1754   - create customdata storage
1755   - call paint once (mouse click)
1756   - add modal handler 
1757
1758 Operator->modal()
1759   - for every mousemove, apply vertex paint
1760   - exit on mouse release, free customdata
1761         (return OPERATOR_FINISHED also removes handler and operator)
1762
1763 For future:
1764   - implement a stroke event (or mousemove with past positons)
1765   - revise whether op->customdata should be added in object, in set_vpaint
1766
1767 */
1768
1769 typedef struct VPaintData {
1770         ViewContext vc;
1771         unsigned int paintcol;
1772         int *indexar;
1773         float *vertexcosnos;
1774         float vpimat[3][3];
1775 } VPaintData;
1776
1777 static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *event)
1778 {
1779         ToolSettings *ts= CTX_data_tool_settings(C);
1780         struct PaintStroke *stroke = op->customdata;
1781         VPaint *vp= ts->vpaint;
1782         struct VPaintData *vpd;
1783         Object *ob= CTX_data_active_object(C);
1784         Mesh *me;
1785         float mat[4][4], imat[4][4];
1786
1787         /* context checks could be a poll() */
1788         me= get_mesh(ob);
1789         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1790         
1791         if(me->mcol==NULL) make_vertexcol(ob);
1792         if(me->mcol==NULL) return OPERATOR_CANCELLED;
1793         
1794         /* make mode data storage */
1795         vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
1796         paint_stroke_set_mode_data(stroke, vpd);
1797         view3d_set_viewcontext(C, &vpd->vc);
1798         
1799         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
1800         vpd->indexar= get_indexarray(me);
1801         vpd->paintcol= vpaint_get_current_col(vp);
1802         
1803         /* for filtering */
1804         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
1805         
1806         /* some old cruft to sort out later */
1807         mul_m4_m4m4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
1808         invert_m4_m4(imat, mat);
1809         copy_m3_m4(vpd->vpimat, imat);
1810
1811         return 1;
1812 }
1813
1814 static void vpaint_paint_face(VPaint *vp, VPaintData *vpd, Object *ob, int index, float mval[2], float pressure, int flip)
1815 {
1816         ViewContext *vc = &vpd->vc;
1817         Brush *brush = paint_brush(&vp->paint);
1818         Mesh *me = get_mesh(ob);
1819         MFace *mface= ((MFace*)me->mface) + index;
1820         unsigned int *mcol= ((unsigned int*)me->mcol) + 4*index;
1821         unsigned int *mcolorig= ((unsigned int*)vp->vpaint_prev) + 4*index;
1822         float alpha;
1823         int i;
1824         
1825         if((vp->flag & VP_COLINDEX && mface->mat_nr!=ob->actcol-1) ||
1826            ((me->editflag & ME_EDIT_PAINT_MASK) && !(mface->flag & ME_FACE_SEL)))
1827                 return;
1828
1829         if(brush->vertexpaint_tool==VP_BLUR) {
1830                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
1831                 if(mface->v4) {
1832                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
1833                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
1834                 }
1835                 else {
1836                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
1837                 }
1838                 
1839         }
1840
1841         for(i = 0; i < (mface->v4 ? 4 : 3); ++i) {
1842                 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*(&mface->v1)[i], mval, pressure);
1843                 if(alpha)
1844                         vpaint_blend(vp, mcol+i, mcolorig+i, vpd->paintcol, (int)(alpha*255.0));
1845         }
1846 }
1847
1848 static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1849 {
1850         ToolSettings *ts= CTX_data_tool_settings(C);
1851         struct VPaintData *vpd = paint_stroke_mode_data(stroke);
1852         VPaint *vp= ts->vpaint;
1853         Brush *brush = paint_brush(&vp->paint);
1854         ViewContext *vc= &vpd->vc;
1855         Object *ob= vc->obact;
1856         Mesh *me= ob->data;
1857         float mat[4][4];
1858         int *indexar= vpd->indexar;
1859         int totindex, index, flip;
1860         float pressure, mval[2];
1861
1862         RNA_float_get_array(itemptr, "mouse", mval);
1863         flip = RNA_boolean_get(itemptr, "flip");
1864         pressure = RNA_float_get(itemptr, "pressure");
1865                         
1866         view3d_operator_needs_opengl(C);
1867                         
1868         /* load projection matrix */
1869         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1870
1871         mval[0]-= vc->ar->winrct.xmin;
1872         mval[1]-= vc->ar->winrct.ymin;
1873
1874                         
1875         /* which faces are involved */
1876         if(vp->flag & VP_AREA) {
1877                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush_size(brush));
1878         }
1879         else {
1880                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1881                 if(indexar[0]) totindex= 1;
1882                 else totindex= 0;
1883         }
1884                         
1885         swap_m4m4(vc->rv3d->persmat, mat);
1886                         
1887         for(index=0; index<totindex; index++) {                         
1888                 if(indexar[index] && indexar[index]<=me->totface)
1889                         vpaint_paint_face(vp, vpd, ob, indexar[index]-1, mval, pressure, flip);
1890         }
1891                                                 
1892         swap_m4m4(vc->rv3d->persmat, mat);
1893
1894         /* was disabled because it is slow, but necessary for blur */
1895         if(brush->vertexpaint_tool == VP_BLUR)
1896                 do_shared_vertexcol(me);
1897                         
1898         ED_region_tag_redraw(vc->ar);
1899                         
1900         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
1901 }
1902
1903 static void vpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
1904 {
1905         ToolSettings *ts= CTX_data_tool_settings(C);
1906         struct VPaintData *vpd= paint_stroke_mode_data(stroke);
1907         
1908         if(vpd->vertexcosnos)
1909                 MEM_freeN(vpd->vertexcosnos);
1910         MEM_freeN(vpd->indexar);
1911         
1912         /* frees prev buffer */
1913         copy_vpaint_prev(ts->vpaint, NULL, 0);
1914         
1915         MEM_freeN(vpd);
1916 }
1917
1918 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1919 {
1920         
1921         op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
1922                                           vpaint_stroke_update_step,
1923                                           vpaint_stroke_done);
1924         
1925         /* add modal handler */
1926         WM_event_add_modal_handler(C, op);
1927
1928         op->type->modal(C, op, event);
1929         
1930         return OPERATOR_RUNNING_MODAL;
1931 }
1932
1933 void PAINT_OT_vertex_paint(wmOperatorType *ot)
1934 {
1935         /* identifiers */
1936         ot->name= "Vertex Paint";
1937         ot->idname= "PAINT_OT_vertex_paint";
1938         
1939         /* api callbacks */
1940         ot->invoke= vpaint_invoke;
1941         ot->modal= paint_stroke_modal;
1942         /* ot->exec= vpaint_exec; <-- needs stroke property */
1943         ot->poll= vertex_paint_poll;
1944         
1945         /* flags */
1946         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1947
1948         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1949 }
1950
1951 /* ********************** weight from bones operator ******************* */
1952
1953 static int weight_from_bones_poll(bContext *C)
1954 {
1955         Object *ob= CTX_data_active_object(C);
1956
1957         return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob));
1958 }
1959
1960 static int weight_from_bones_exec(bContext *C, wmOperator *op)
1961 {
1962         Scene *scene= CTX_data_scene(C);
1963         Object *ob= CTX_data_active_object(C);
1964         Object *armob= modifiers_isDeformedByArmature(ob);
1965         Mesh *me= ob->data;
1966         int type= RNA_enum_get(op->ptr, "type");
1967
1968         create_vgroups_from_armature(scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X));
1969
1970         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1971         WM_event_add_notifier(C, NC_GEOM|ND_DATA, me);
1972
1973         return OPERATOR_FINISHED;
1974 }
1975
1976 void PAINT_OT_weight_from_bones(wmOperatorType *ot)
1977 {
1978         static EnumPropertyItem type_items[]= {
1979                 {ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights froms bones"},
1980                 {ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"},
1981                 {0, NULL, 0, NULL, NULL}};
1982
1983         /* identifiers */
1984         ot->name= "Weight from Bones";
1985         ot->idname= "PAINT_OT_weight_from_bones";
1986         
1987         /* api callbacks */
1988         ot->exec= weight_from_bones_exec;
1989         ot->invoke= WM_menu_invoke;
1990         ot->poll= weight_from_bones_poll;
1991         
1992         /* flags */
1993         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1994
1995         /* properties */
1996         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights.");
1997 }
1998