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