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