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