Recreating my GSoC branch.
[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);
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         
737         project_short_noclip(vc->ar, vert_nor, vertco);
738         dx= mval[0]-vertco[0];
739         dy= mval[1]-vertco[1];
740         
741         if (brush->flag & BRUSH_SIZE_PRESSURE)
742                 size = pressure * brush->size;
743         else
744                 size = brush->size;
745         
746         fac_2= dx*dx + dy*dy;
747         if(fac_2 > size*size) return 0.f;
748         fac = sqrtf(fac_2);
749         
750         alpha= brush->alpha * brush_curve_strength_clamp(brush, fac, size);
751         
752         if (brush->flag & BRUSH_ALPHA_PRESSURE)
753                 alpha *= pressure;
754                 
755         if(vp->flag & VP_NORMALS) {
756                 float *no= vert_nor+3;
757                 
758                 /* transpose ! */
759                 fac= vpimat[2][0]*no[0]+vpimat[2][1]*no[1]+vpimat[2][2]*no[2];
760                 if(fac>0.0) {
761                         dx= vpimat[0][0]*no[0]+vpimat[0][1]*no[1]+vpimat[0][2]*no[2];
762                         dy= vpimat[1][0]*no[0]+vpimat[1][1]*no[1]+vpimat[1][2]*no[2];
763                         
764                         alpha*= fac/sqrtf(dx*dx + dy*dy + fac*fac);
765                 }
766                 else return 0.f;
767         }
768         
769         return alpha;
770 }
771
772 static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval, int flip)
773 {
774         Brush *brush = paint_brush(&wp->paint);
775         int tool = brush->vertexpaint_tool;
776         
777         if(dw==NULL || uw==NULL) return;
778         
779         if (flip) {
780                 switch(tool) {
781                         case VP_MIX:
782                                 paintval = 1.f - paintval; break;
783                         case VP_ADD:
784                                 tool= VP_SUB; break;
785                         case VP_SUB:
786                                 tool= VP_ADD; break;
787                         case VP_LIGHTEN:
788                                 tool= VP_DARKEN; break;
789                         case VP_DARKEN:
790                                 tool= VP_LIGHTEN; break;
791                 }
792         }
793         
794         if(tool==VP_MIX || tool==VP_BLUR)
795                 dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
796         else if(tool==VP_ADD)
797                 dw->weight += paintval*alpha;
798         else if(tool==VP_SUB) 
799                 dw->weight -= paintval*alpha;
800         else if(tool==VP_MUL) 
801                 /* first mul, then blend the fac */
802                 dw->weight = ((1.0-alpha) + alpha*paintval)*dw->weight;
803         else if(tool==VP_LIGHTEN) {
804                 if (dw->weight < paintval)
805                         dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
806         } else if(tool==VP_DARKEN) {
807                 if (dw->weight > paintval)
808                         dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
809         }
810         CLAMP(dw->weight, 0.0f, 1.0f);
811         
812         /* if no spray, clip result with orig weight & orig alpha */
813         if((wp->flag & VP_SPRAY)==0) {
814                 float testw=0.0f;
815                 
816                 alpha= brush->alpha;
817                 if(tool==VP_MIX || tool==VP_BLUR)
818                         testw = paintval*alpha + uw->weight*(1.0-alpha);
819                 else if(tool==VP_ADD)
820                         testw = uw->weight + paintval*alpha;
821                 else if(tool==VP_SUB) 
822                         testw = uw->weight - paintval*alpha;
823                 else if(tool==VP_MUL) 
824                         /* first mul, then blend the fac */
825                         testw = ((1.0-alpha) + alpha*paintval)*uw->weight;              
826                 else if(tool==VP_LIGHTEN) {
827                         if (uw->weight < paintval)
828                                 testw = paintval*alpha + uw->weight*(1.0-alpha);
829                         else
830                                 testw = uw->weight;
831                 } else if(tool==VP_DARKEN) {
832                         if (uw->weight > paintval)
833                                 testw = paintval*alpha + uw->weight*(1.0-alpha);
834                         else
835                                 testw = uw->weight;
836                 }
837                 CLAMP(testw, 0.0f, 1.0f);
838                 
839                 if( testw<uw->weight ) {
840                         if(dw->weight < testw) dw->weight= testw;
841                         else if(dw->weight > uw->weight) dw->weight= uw->weight;
842                 }
843                 else {
844                         if(dw->weight > testw) dw->weight= testw;
845                         else if(dw->weight < uw->weight) dw->weight= uw->weight;
846                 }
847         }
848         
849 }
850
851 /* ----------------------------------------------------- */
852
853 /* used for 3d view, on active object, assumes me->dvert exists */
854 /* if mode==1: */
855 /*     samples cursor location, and gives menu with vertex groups to activate */
856 /* else */
857 /*     sets wp->weight to the closest weight value to vertex */
858 /*     note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
859 void sample_wpaint(Scene *scene, ARegion *ar, View3D *v3d, int mode)
860 {
861         ViewContext vc;
862         ToolSettings *ts= scene->toolsettings;
863         Object *ob= OBACT;
864         Mesh *me= get_mesh(ob);
865         int index;
866         short mval[2] = {0, 0}, sco[2];
867         int vgroup= ob->actdef-1;
868
869         if (!me) return;
870         
871 //      getmouseco_areawin(mval);
872         index= view3d_sample_backbuf(&vc, mval[0], mval[1]);
873         
874         if(index && index<=me->totface) {
875                 MFace *mface;
876                 
877                 mface= ((MFace *)me->mface) + index-1;
878                 
879                 if(mode==1) {   /* sampe which groups are in here */
880                         MDeformVert *dv;
881                         int a, totgroup;
882                         
883                         totgroup= BLI_countlist(&ob->defbase);
884                         if(totgroup) {
885                                 int totmenu=0;
886                                 int *groups=MEM_callocN(totgroup*sizeof(int), "groups");
887                                 
888                                 dv= me->dvert+mface->v1;
889                                 for(a=0; a<dv->totweight; a++) {
890                                         if (dv->dw[a].def_nr<totgroup)
891                                                 groups[dv->dw[a].def_nr]= 1;
892                                 }
893                                 dv= me->dvert+mface->v2;
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->v3;
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                                 if(mface->v4) {
904                                         dv= me->dvert+mface->v4;
905                                         for(a=0; a<dv->totweight; a++) {
906                                                 if (dv->dw[a].def_nr<totgroup)
907                                                         groups[dv->dw[a].def_nr]= 1;
908                                         }
909                                 }
910                                 for(a=0; a<totgroup; a++)
911                                         if(groups[a]) totmenu++;
912                                 
913                                 if(totmenu==0) {
914                                         //notice("No Vertex Group Selected");
915                                 }
916                                 else {
917                                         bDeformGroup *dg;
918                                         short val;
919                                         char item[40], *str= MEM_mallocN(40*totmenu+40, "menu");
920                                         
921                                         strcpy(str, "Vertex Groups %t");
922                                         for(a=0, dg=ob->defbase.first; dg && a<totgroup; a++, dg= dg->next) {
923                                                 if(groups[a]) {
924                                                         sprintf(item, "|%s %%x%d", dg->name, a);
925                                                         strcat(str, item);
926                                                 }
927                                         }
928                                         
929                                         val= 0; // XXX pupmenu(str);
930                                         if(val>=0) {
931                                                 ob->actdef= val+1;
932                                                 DAG_id_flush_update(&me->id, OB_RECALC_DATA);
933                                         }
934                                         MEM_freeN(str);
935                                 }
936                                 MEM_freeN(groups);
937                         }
938 //                      else notice("No Vertex Groups in Object");
939                 }
940                 else {
941                         DerivedMesh *dm;
942                         float w1, w2, w3, w4, co[3], fac;
943                         
944                         dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
945                         if(dm->getVertCo==NULL) {
946                                 //notice("Not supported yet");
947                         }
948                         else {
949                                 /* calc 3 or 4 corner weights */
950                                 dm->getVertCo(dm, mface->v1, co);
951                                 project_short_noclip(ar, co, sco);
952                                 w1= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
953                                 
954                                 dm->getVertCo(dm, mface->v2, co);
955                                 project_short_noclip(ar, co, sco);
956                                 w2= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
957                                 
958                                 dm->getVertCo(dm, mface->v3, co);
959                                 project_short_noclip(ar, co, sco);
960                                 w3= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
961                                 
962                                 if(mface->v4) {
963                                         dm->getVertCo(dm, mface->v4, co);
964                                         project_short_noclip(ar, co, sco);
965                                         w4= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
966                                 }
967                                 else w4= 1.0e10;
968                                 
969                                 fac= MIN4(w1, w2, w3, w4);
970                                 if(w1==fac) {
971                                         ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v1, vgroup);
972                                 }
973                                 else if(w2==fac) {
974                                         ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v2, vgroup);
975                                 }
976                                 else if(w3==fac) {
977                                         ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v3, vgroup);
978                                 }
979                                 else if(w4==fac) {
980                                         if(mface->v4) {
981                                                 ts->vgroup_weight= defvert_find_weight(me->dvert+mface->v4, vgroup);
982                                         }
983                                 }
984                         }
985                         dm->release(dm);
986                 }               
987                 
988         }
989         
990 }
991
992 static void do_weight_paint_auto_normalize(MDeformVert *dvert, 
993                                            int paint_nr, char *map)
994 {
995 //      MDeformWeight *dw = dvert->dw;
996         float sum=0.0f, fac=0.0f, paintw=0.0f;
997         int i, tot=0;
998
999         if (!map)
1000                 return;
1001
1002         for (i=0; i<dvert->totweight; i++) {
1003                 if (dvert->dw[i].def_nr == paint_nr)
1004                         paintw = dvert->dw[i].weight;
1005
1006                 if (map[dvert->dw[i].def_nr]) {
1007                         tot += 1;
1008                         if (dvert->dw[i].def_nr != paint_nr)
1009                                 sum += dvert->dw[i].weight;
1010                 }
1011         }
1012         
1013         if (!tot || sum <= (1.0f - paintw))
1014                 return;
1015
1016         fac = sum / (1.0f - paintw);
1017         fac = fac==0.0f ? 1.0f : 1.0f / fac;
1018
1019         for (i=0; i<dvert->totweight; i++) {
1020                 if (map[dvert->dw[i].def_nr]) {
1021                         if (dvert->dw[i].def_nr != paint_nr)
1022                                 dvert->dw[i].weight *= fac;
1023                 }
1024         }
1025 }
1026
1027 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, 
1028                                    float alpha, float paintweight, int flip, 
1029                                    int vgroup_mirror, char *validmap)
1030 {
1031         Mesh *me= ob->data;
1032         MDeformWeight *dw, *uw;
1033         int vgroup= ob->actdef-1;
1034         
1035         if(wp->flag & VP_ONLYVGROUP) {
1036                 dw= defvert_find_index(me->dvert+index, vgroup);
1037                 uw= defvert_find_index(wp->wpaint_prev+index, vgroup);
1038         }
1039         else {
1040                 dw= defvert_verify_index(me->dvert+index, vgroup);
1041                 uw= defvert_verify_index(wp->wpaint_prev+index, vgroup);
1042         }
1043         if(dw==NULL || uw==NULL)
1044                 return;
1045         
1046         wpaint_blend(wp, dw, uw, alpha, paintweight, flip);
1047         do_weight_paint_auto_normalize(me->dvert+index, vgroup, validmap);
1048
1049         if(me->editflag & ME_EDIT_MIRROR_X) {   /* x mirror painting */
1050                 int j= mesh_get_x_mirror_vert(ob, index);
1051                 if(j>=0) {
1052                         /* copy, not paint again */
1053                         if(vgroup_mirror != -1)
1054                                 uw= defvert_verify_index(me->dvert+j, vgroup_mirror);
1055                         else
1056                                 uw= defvert_verify_index(me->dvert+j, vgroup);
1057                                 
1058                         uw->weight= dw->weight;
1059
1060                         do_weight_paint_auto_normalize(me->dvert+j, vgroup, validmap);
1061                 }
1062         }
1063 }
1064
1065
1066 /* *************** set wpaint operator ****************** */
1067
1068 static int set_wpaint(bContext *C, wmOperator *op)              /* toggle */
1069 {               
1070         Object *ob= CTX_data_active_object(C);
1071         Scene *scene= CTX_data_scene(C);
1072         VPaint *wp= scene->toolsettings->wpaint;
1073         Mesh *me;
1074         
1075         me= get_mesh(ob);
1076         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1077         
1078         if(ob->mode & OB_MODE_WEIGHT_PAINT) ob->mode &= ~OB_MODE_WEIGHT_PAINT;
1079         else ob->mode |= OB_MODE_WEIGHT_PAINT;
1080         
1081         
1082         /* Weightpaint works by overriding colors in mesh,
1083                 * so need to make sure we recalc on enter and
1084                 * exit (exit needs doing regardless because we
1085                                 * should redeform).
1086                 */
1087         DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1088         
1089         if(ob->mode & OB_MODE_WEIGHT_PAINT) {
1090                 Object *par;
1091                 
1092                 if(wp==NULL)
1093                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1094
1095                 paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);
1096                 paint_cursor_start(C, weight_paint_poll);
1097                 
1098                 mesh_octree_table(ob, NULL, NULL, 's');
1099                 
1100                 /* verify if active weight group is also active bone */
1101                 par= modifiers_isDeformedByArmature(ob);
1102                 if(par && (par->mode & OB_MODE_POSE)) {
1103                         bArmature *arm= par->data;
1104
1105                         if(arm->act_bone)
1106                                 ED_vgroup_select_by_name(ob, arm->act_bone->name);
1107                 }
1108         }
1109         else {
1110                 mesh_octree_table(NULL, NULL, NULL, 'e');
1111                 mesh_mirrtopo_table(NULL, 'e');
1112         }
1113         
1114         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1115         
1116         return OPERATOR_FINISHED;
1117 }
1118
1119 /* for switching to/from mode */
1120 static int paint_poll_test(bContext *C)
1121 {
1122         if(CTX_data_edit_object(C))
1123                 return 0;
1124         if(CTX_data_active_object(C)==NULL)
1125                 return 0;
1126         return 1;
1127 }
1128
1129 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1130 {
1131         
1132         /* identifiers */
1133         ot->name= "Weight Paint Mode";
1134         ot->idname= "PAINT_OT_weight_paint_toggle";
1135         
1136         /* api callbacks */
1137         ot->exec= set_wpaint;
1138         ot->poll= paint_poll_test;
1139         
1140         /* flags */
1141         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1142         
1143 }
1144
1145 /* ************ paint radial controls *************/
1146
1147 static int vpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1148 {
1149         Paint *p = paint_get_active(CTX_data_scene(C));
1150         Brush *brush = paint_brush(p);
1151         
1152         WM_paint_cursor_end(CTX_wm_manager(C), p->paint_cursor);
1153         p->paint_cursor = NULL;
1154         brush_radial_control_invoke(op, brush, 1);
1155         return WM_radial_control_invoke(C, op, event);
1156 }
1157
1158 static int vpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1159 {
1160         int ret = WM_radial_control_modal(C, op, event);
1161         if(ret != OPERATOR_RUNNING_MODAL)
1162                 paint_cursor_start(C, vertex_paint_poll);
1163         return ret;
1164 }
1165
1166 static int vpaint_radial_control_exec(bContext *C, wmOperator *op)
1167 {
1168         Brush *brush = paint_brush(&CTX_data_scene(C)->toolsettings->vpaint->paint);
1169         int ret = brush_radial_control_exec(op, brush, 1);
1170         
1171         WM_event_add_notifier(C, NC_BRUSH|NA_EDITED, brush);
1172         
1173         return ret;
1174 }
1175
1176 static int wpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1177 {
1178         Paint *p = paint_get_active(CTX_data_scene(C));
1179         Brush *brush = paint_brush(p);
1180         
1181         WM_paint_cursor_end(CTX_wm_manager(C), p->paint_cursor);
1182         p->paint_cursor = NULL;
1183         brush_radial_control_invoke(op, brush, 1);
1184         return WM_radial_control_invoke(C, op, event);
1185 }
1186
1187 static int wpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1188 {
1189         int ret = WM_radial_control_modal(C, op, event);
1190         if(ret != OPERATOR_RUNNING_MODAL)
1191                 paint_cursor_start(C, weight_paint_poll);
1192         return ret;
1193 }
1194
1195 static int wpaint_radial_control_exec(bContext *C, wmOperator *op)
1196 {
1197         Brush *brush = paint_brush(&CTX_data_scene(C)->toolsettings->wpaint->paint);
1198         int ret = brush_radial_control_exec(op, brush, 1);
1199         
1200         WM_event_add_notifier(C, NC_BRUSH|NA_EDITED, brush);
1201         
1202         return ret;
1203 }
1204
1205 void PAINT_OT_weight_paint_radial_control(wmOperatorType *ot)
1206 {
1207         WM_OT_radial_control_partial(ot);
1208
1209         ot->name= "Weight Paint Radial Control";
1210         ot->idname= "PAINT_OT_weight_paint_radial_control";
1211
1212         ot->invoke= wpaint_radial_control_invoke;
1213         ot->modal= wpaint_radial_control_modal;
1214         ot->exec= wpaint_radial_control_exec;
1215         ot->poll= weight_paint_poll;
1216         
1217         /* flags */
1218         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1219 }
1220
1221 void PAINT_OT_vertex_paint_radial_control(wmOperatorType *ot)
1222 {
1223         WM_OT_radial_control_partial(ot);
1224
1225         ot->name= "Vertex Paint Radial Control";
1226         ot->idname= "PAINT_OT_vertex_paint_radial_control";
1227
1228         ot->invoke= vpaint_radial_control_invoke;
1229         ot->modal= vpaint_radial_control_modal;
1230         ot->exec= vpaint_radial_control_exec;
1231         ot->poll= vertex_paint_poll;
1232         
1233         /* flags */
1234         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1235 }
1236
1237 /* ************ weight paint operator ********** */
1238
1239 struct WPaintData {
1240         ViewContext vc;
1241         int *indexar;
1242         int vgroup_mirror;
1243         float *vertexcosnos;
1244         float wpimat[3][3];
1245         
1246         /*variables for auto normalize*/
1247         int auto_normalize;
1248         char *vgroup_validmap; /*stores if vgroups tie to deforming bones or not*/
1249 };
1250
1251 static char *wpaint_make_validmap(Mesh *me, Object *ob)
1252 {
1253         bDeformGroup *dg;
1254         ModifierData *md;
1255         char *validmap;
1256         bPose *pose;
1257         bPoseChannel *chan;
1258         ArmatureModifierData *amd;
1259         GHash *gh = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "wpaint_make_validmap gh");
1260         int i = 0, step1=1;
1261
1262         /*add all names to a hash table*/
1263         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1264                 BLI_ghash_insert(gh, dg->name, NULL);
1265         }
1266
1267         if (!i)
1268                 return NULL;
1269
1270         validmap = MEM_callocN(i, "wpaint valid map");
1271
1272         /*now loop through the armature modifiers and identify deform bones*/
1273         for (md = ob->modifiers.first; md; md= !md->next && step1 ? (step1=0), modifiers_getVirtualModifierList(ob) : md->next) {
1274                 if (!(md->mode & (eModifierMode_Realtime|eModifierMode_Virtual)))
1275                         continue;
1276
1277                 if (md->type == eModifierType_Armature) 
1278                 {
1279                         amd = (ArmatureModifierData*) md;
1280
1281                         if(amd->object && amd->object->pose) {
1282                                 pose = amd->object->pose;
1283                                 
1284                                 for (chan=pose->chanbase.first; chan; chan=chan->next) {
1285                                         if (chan->bone->flag & BONE_NO_DEFORM)
1286                                                 continue;
1287
1288                                         if (BLI_ghash_haskey(gh, chan->name)) {
1289                                                 BLI_ghash_remove(gh, chan->name, NULL, NULL);
1290                                                 BLI_ghash_insert(gh, chan->name, SET_INT_IN_POINTER(1));
1291                                         }
1292                                 }
1293                         }
1294                 }
1295         }
1296         
1297         /*add all names to a hash table*/
1298         for (dg=ob->defbase.first, i=0; dg; dg=dg->next, i++) {
1299                 if (BLI_ghash_lookup(gh, dg->name) != NULL) {
1300                         validmap[i] = 1;
1301                 }
1302         }
1303
1304         BLI_ghash_free(gh, NULL, NULL);
1305
1306         return validmap;
1307 }
1308
1309 static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *event)
1310 {
1311         Scene *scene= CTX_data_scene(C);
1312         struct PaintStroke *stroke = op->customdata;
1313         ToolSettings *ts= CTX_data_tool_settings(C);
1314         VPaint *wp= ts->wpaint;
1315         Object *ob= CTX_data_active_object(C);
1316         struct WPaintData *wpd;
1317         Mesh *me;
1318         float mat[4][4], imat[4][4];
1319         
1320         if(scene->obedit) return OPERATOR_CANCELLED;
1321         
1322         me= get_mesh(ob);
1323         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1324         
1325         /* if nothing was added yet, we make dverts and a vertex deform group */
1326         if (!me->dvert)
1327                 ED_vgroup_data_create(&me->id);
1328         
1329         /* make mode data storage */
1330         wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1331         paint_stroke_set_mode_data(stroke, wpd);
1332         view3d_set_viewcontext(C, &wpd->vc);
1333         wpd->vgroup_mirror= -1;
1334         
1335         /*set up auto-normalize, and generate map for detecting which
1336           vgroups affect deform bones*/
1337         wpd->auto_normalize = ts->auto_normalize;
1338         if (wpd->auto_normalize)
1339                 wpd->vgroup_validmap = wpaint_make_validmap(me, ob);
1340         
1341         //      if(qual & LR_CTRLKEY) {
1342         //              sample_wpaint(scene, ar, v3d, 0);
1343         //              return;
1344         //      }
1345         //      if(qual & LR_SHIFTKEY) {
1346         //              sample_wpaint(scene, ar, v3d, 1);
1347         //              return;
1348         //      }
1349         
1350         /* ALLOCATIONS! no return after this line */
1351         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
1352         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
1353         wpd->indexar= get_indexarray(me);
1354         copy_wpaint_prev(wp, me->dvert, me->totvert);
1355         
1356         /* this happens on a Bone select, when no vgroup existed yet */
1357         if(ob->actdef<=0) {
1358                 Object *modob;
1359                 if((modob = modifiers_isDeformedByArmature(ob))) {
1360                         Bone *actbone= ((bArmature *)modob->data)->act_bone;
1361                         if(actbone) {
1362                                 bPoseChannel *pchan= get_pose_channel(modob->pose, actbone->name);
1363
1364                                 if(pchan) {
1365                                         bDeformGroup *dg= defgroup_find_name(ob, pchan->name);
1366                                         if(dg==NULL)
1367                                                 dg= ED_vgroup_add_name(ob, pchan->name);        /* sets actdef */
1368                                         else
1369                                                 ob->actdef= 1 + defgroup_find_index(ob, dg);
1370                                 }
1371                         }
1372                 }
1373         }
1374         if(ob->defbase.first==NULL) {
1375                 ED_vgroup_add(ob);
1376         }
1377         
1378         //      if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1379         
1380         /* imat for normals */
1381         mul_m4_m4m4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1382         invert_m4_m4(imat, mat);
1383         copy_m3_m4(wpd->wpimat, imat);
1384         
1385         /* if mirror painting, find the other group */
1386         if(me->editflag & ME_EDIT_MIRROR_X) {
1387                 bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
1388                 if(defgroup) {
1389                         bDeformGroup *curdef;
1390                         int actdef= 0;
1391                         char name[32];
1392                         
1393                         BLI_strncpy(name, defgroup->name, 32);
1394                         bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
1395                         
1396                         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
1397                                 if (!strcmp(curdef->name, name))
1398                                         break;
1399                         if(curdef==NULL) {
1400                                 int olddef= ob->actdef; /* tsk, ED_vgroup_add sets the active defgroup */
1401                                 curdef= ED_vgroup_add_name (ob, name);
1402                                 ob->actdef= olddef;
1403                         }
1404                         
1405                         if(curdef && curdef!=defgroup)
1406                                 wpd->vgroup_mirror= actdef;
1407                 }
1408         }
1409         
1410         return 1;
1411 }
1412
1413 static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1414 {
1415         ToolSettings *ts= CTX_data_tool_settings(C);
1416         VPaint *wp= ts->wpaint;
1417         Brush *brush = paint_brush(&wp->paint);
1418         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1419         ViewContext *vc;
1420         Object *ob;
1421         Mesh *me;
1422         float mat[4][4];
1423         float paintweight= ts->vgroup_weight;
1424         int *indexar;
1425         int totindex, index, totw, flip;
1426         float alpha;
1427         float mval[2], pressure;
1428         
1429         /* cannot paint if there is no stroke data */
1430         if (wpd == NULL) {
1431                 // XXX: force a redraw here, since even though we can't paint, 
1432                 // at least view won't freeze until stroke ends
1433                 ED_region_tag_redraw(CTX_wm_region(C));
1434                 return;
1435         }
1436                 
1437         vc= &wpd->vc;
1438         ob= vc->obact;
1439         me= ob->data;
1440         indexar= wpd->indexar;
1441         
1442         view3d_operator_needs_opengl(C);
1443                 
1444         /* load projection matrix */
1445         mul_m4_m4m4(mat, ob->obmat, vc->rv3d->persmat);
1446
1447         flip = RNA_boolean_get(itemptr, "flip");
1448         pressure = RNA_float_get(itemptr, "pressure");
1449         RNA_float_get_array(itemptr, "mouse", mval);
1450         mval[0]-= vc->ar->winrct.xmin;
1451         mval[1]-= vc->ar->winrct.ymin;
1452                         
1453         swap_m4m4(wpd->vc.rv3d->persmat, mat);
1454                         
1455         /* which faces are involved */
1456         if(wp->flag & VP_AREA) {
1457                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], brush->size);
1458         }
1459         else {
1460                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1461                 if(indexar[0]) totindex= 1;
1462                 else totindex= 0;
1463         }
1464                         
1465         if(wp->flag & VP_COLINDEX) {
1466                 for(index=0; index<totindex; index++) {
1467                         if(indexar[index] && indexar[index]<=me->totface) {
1468                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1469                                                 
1470                                 if(mface->mat_nr!=ob->actcol-1) {
1471                                         indexar[index]= 0;
1472                                 }
1473                         }
1474                 }
1475         }
1476                         
1477         if((me->editflag & ME_EDIT_PAINT_MASK) && me->mface) {
1478                 for(index=0; index<totindex; index++) {
1479                         if(indexar[index] && indexar[index]<=me->totface) {
1480                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1481                                                 
1482                                 if((mface->flag & ME_FACE_SEL)==0) {
1483                                         indexar[index]= 0;
1484                                 }
1485                         }                                       
1486                 }
1487         }
1488                         
1489         /* make sure each vertex gets treated only once */
1490         /* and calculate filter weight */
1491         totw= 0;
1492         if(brush->vertexpaint_tool==VP_BLUR) 
1493                 paintweight= 0.0f;
1494         else
1495                 paintweight= ts->vgroup_weight;
1496                         
1497         for(index=0; index<totindex; index++) {
1498                 if(indexar[index] && indexar[index]<=me->totface) {
1499                         MFace *mface= me->mface + (indexar[index]-1);
1500                                         
1501                         (me->dvert+mface->v1)->flag= 1;
1502                         (me->dvert+mface->v2)->flag= 1;
1503                         (me->dvert+mface->v3)->flag= 1;
1504                         if(mface->v4) (me->dvert+mface->v4)->flag= 1;
1505                                         
1506                         if(brush->vertexpaint_tool==VP_BLUR) {
1507                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, int);
1508                                                 
1509                                 if(wp->flag & VP_ONLYVGROUP)
1510                                         dw_func= (void *)defvert_find_index; /* uses a const, cast to quiet warning */
1511                                 else
1512                                         dw_func= defvert_verify_index;
1513                                                 
1514                                 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
1515                                 if(dw) {paintweight+= dw->weight; totw++;}
1516                                 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
1517                                 if(dw) {paintweight+= dw->weight; totw++;}
1518                                 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
1519                                 if(dw) {paintweight+= dw->weight; totw++;}
1520                                 if(mface->v4) {
1521                                         dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
1522                                         if(dw) {paintweight+= dw->weight; totw++;}
1523                                 }
1524                         }
1525                 }
1526         }
1527                         
1528         if(brush->vertexpaint_tool==VP_BLUR) 
1529                 paintweight/= (float)totw;
1530                         
1531         for(index=0; index<totindex; index++) {
1532                                 
1533                 if(indexar[index] && indexar[index]<=me->totface) {
1534                         MFace *mface= me->mface + (indexar[index]-1);
1535                                         
1536                         if((me->dvert+mface->v1)->flag) {
1537                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval, pressure);
1538                                 if(alpha) {
1539                                         do_weight_paint_vertex(wp, ob, mface->v1, 
1540                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1541                                                 wpd->vgroup_validmap);
1542                                 }
1543                                 (me->dvert+mface->v1)->flag= 0;
1544                         }
1545                                         
1546                         if((me->dvert+mface->v2)->flag) {
1547                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval, pressure);
1548                                 if(alpha) {
1549                                         do_weight_paint_vertex(wp, ob, mface->v2, 
1550                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1551                                                 wpd->vgroup_validmap);
1552                                 }
1553                                 (me->dvert+mface->v2)->flag= 0;
1554                         }
1555                                         
1556                         if((me->dvert+mface->v3)->flag) {
1557                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval, pressure);
1558                                 if(alpha) {
1559                                         do_weight_paint_vertex(wp, ob, mface->v3, 
1560                                                 alpha, paintweight, flip, wpd->vgroup_mirror, 
1561                                                 wpd->vgroup_validmap);
1562                                 }
1563                                 (me->dvert+mface->v3)->flag= 0;
1564                         }
1565                                         
1566                         if((me->dvert+mface->v4)->flag) {
1567                                 if(mface->v4) {
1568                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval, pressure);
1569                                         if(alpha) {
1570                                                 do_weight_paint_vertex(wp, ob, mface->v4, 
1571                                                         alpha, paintweight, flip, wpd->vgroup_mirror,
1572                                                         wpd->vgroup_validmap);
1573                                         }
1574                                         (me->dvert+mface->v4)->flag= 0;
1575                                 }
1576                         }
1577                 }
1578         }
1579                         
1580         swap_m4m4(vc->rv3d->persmat, mat);
1581                         
1582         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
1583         ED_region_tag_redraw(vc->ar);
1584 }
1585
1586 static void wpaint_stroke_done(bContext *C, struct PaintStroke *stroke)
1587 {
1588         ToolSettings *ts= CTX_data_tool_settings(C);
1589         Object *ob= CTX_data_active_object(C);
1590         struct WPaintData *wpd= paint_stroke_mode_data(stroke);
1591         
1592         if(wpd) {
1593                 if(wpd->vertexcosnos)
1594                         MEM_freeN(wpd->vertexcosnos);
1595                 MEM_freeN(wpd->indexar);
1596                 
1597                 if (wpd->vgroup_validmap)
1598                         MEM_freeN(wpd->vgroup_validmap);
1599                 
1600                 MEM_freeN(wpd);
1601         }
1602         
1603         /* frees prev buffer */
1604         copy_wpaint_prev(ts->wpaint, NULL, 0);
1605         
1606         /* and particles too */
1607         if(ob->particlesystem.first) {
1608                 ParticleSystem *psys;
1609                 int i;
1610                 
1611                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
1612                         for(i=0; i<PSYS_TOT_VG; i++) {
1613                                 if(psys->vgroup[i]==ob->actdef) {
1614                                         psys->recalc |= PSYS_RECALC_RESET;
1615                                         break;
1616                                 }
1617                         }
1618                 }
1619         }
1620         
1621         DAG_id_flush_update(ob->data, OB_RECALC_DATA);  
1622 }
1623
1624
1625 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1626 {
1627         
1628         op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
1629                                           wpaint_stroke_update_step,
1630                                           wpaint_stroke_done);
1631         
1632         /* add modal handler */
1633         WM_event_add_modal_handler(C, op);
1634
1635         op->type->modal(C, op, event);
1636         
1637         return OPERATOR_RUNNING_MODAL;
1638 }
1639
1640 void PAINT_OT_weight_paint(wmOperatorType *ot)
1641 {
1642         
1643         /* identifiers */
1644         ot->name= "Weight Paint";
1645         ot->idname= "PAINT_OT_weight_paint";
1646         
1647         /* api callbacks */
1648         ot->invoke= wpaint_invoke;
1649         ot->modal= paint_stroke_modal;
1650         /* ot->exec= vpaint_exec; <-- needs stroke property */
1651         ot->poll= weight_paint_poll;
1652         
1653         /* flags */
1654         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1655
1656         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1657 }
1658
1659 static int weight_paint_set_exec(bContext *C, wmOperator *op)
1660 {
1661         struct Scene *scene= CTX_data_scene(C);
1662         Object *obact = CTX_data_active_object(C);
1663
1664         wpaint_fill(scene->toolsettings->wpaint, obact, scene->toolsettings->vgroup_weight);
1665         ED_region_tag_redraw(CTX_wm_region(C)); // XXX - should redraw all 3D views
1666         return OPERATOR_FINISHED;
1667 }
1668
1669 void PAINT_OT_weight_set(wmOperatorType *ot)
1670 {
1671         /* identifiers */
1672         ot->name= "Weight Set";
1673         ot->idname= "PAINT_OT_weight_set";
1674
1675         /* api callbacks */
1676         ot->exec= weight_paint_set_exec;
1677         ot->poll= facemask_paint_poll;
1678
1679         /* flags */
1680         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1681 }
1682
1683 /* ************ set / clear vertex paint mode ********** */
1684
1685
1686 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
1687 {       
1688         Object *ob= CTX_data_active_object(C);
1689         Scene *scene= CTX_data_scene(C);
1690         VPaint *vp= scene->toolsettings->vpaint;
1691         Mesh *me;
1692         
1693         me= get_mesh(ob);
1694         
1695         if(me==NULL || object_data_is_libdata(ob)) {
1696                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
1697                 return OPERATOR_PASS_THROUGH;
1698         }
1699         
1700         if(me && me->mcol==NULL) make_vertexcol(ob);
1701         
1702         /* toggle: end vpaint */
1703         if(ob->mode & OB_MODE_VERTEX_PAINT) {
1704                 
1705                 ob->mode &= ~OB_MODE_VERTEX_PAINT;
1706         }
1707         else {
1708                 ob->mode |= OB_MODE_VERTEX_PAINT;
1709                 /* Turn off weight painting */
1710                 if (ob->mode & OB_MODE_WEIGHT_PAINT)
1711                         set_wpaint(C, op);
1712                 
1713                 if(vp==NULL)
1714                         vp= scene->toolsettings->vpaint= new_vpaint(0);
1715                 
1716                 paint_cursor_start(C, vertex_paint_poll);
1717
1718                 paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
1719         }
1720         
1721         if (me)
1722                 /* update modifier stack for mapping requirements */
1723                 DAG_id_flush_update(&me->id, OB_RECALC_DATA);
1724         
1725         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1726         
1727         return OPERATOR_FINISHED;
1728 }
1729
1730 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
1731 {
1732         
1733         /* identifiers */
1734         ot->name= "Vertex Paint Mode";
1735         ot->idname= "PAINT_OT_vertex_paint_toggle";
1736         
1737         /* api callbacks */
1738         ot->exec= set_vpaint;
1739         ot->poll= paint_poll_test;
1740         
1741         /* flags */
1742         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1743         
1744 }
1745
1746
1747
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);
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