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