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