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