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