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