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