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