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