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