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