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