Commenting out code from commit 22457 by Genscher for PointCache.
[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         ToolSettings *ts= scene->toolsettings;
428         VPaint *wp= ts->wpaint;
429         float paintweight= ts->vgroup_weight;
430         Mesh *me;
431         MFace *mface;
432         Object *ob;
433         MDeformWeight *dw, *uw;
434         int *indexar;
435         int index, vgroup;
436         unsigned int faceverts[5]={0,0,0,0,0};
437         unsigned char i;
438         int vgroup_mirror= -1;
439         
440         ob= OBACT;
441         me= ob->data;
442         if(me==0 || me->totface==0 || me->dvert==0 || !me->mface) return;
443         
444         indexar= get_indexarray();
445         for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
446                 if((mface->flag & ME_FACE_SEL)==0)
447                         indexar[index]= 0;
448                 else
449                         indexar[index]= index+1;
450         }
451         
452         vgroup= ob->actdef-1;
453         
454         /* directly copied from weight_paint, should probaby split into a seperate function */
455         /* if mirror painting, find the other group */          
456         if(wp->flag & VP_MIRROR_X) {
457                 bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
458                 if(defgroup) {
459                         bDeformGroup *curdef;
460                         int actdef= 0;
461                         char name[32];
462
463                         BLI_strncpy(name, defgroup->name, 32);
464                         bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
465                         
466                         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
467                                 if (!strcmp(curdef->name, name))
468                                         break;
469                         if(curdef==NULL) {
470                                 int olddef= ob->actdef; /* tsk, add_defgroup sets the active defgroup */
471                                 curdef= add_defgroup_name (ob, name);
472                                 ob->actdef= olddef;
473                         }
474                         
475                         if(curdef && curdef!=defgroup)
476                                 vgroup_mirror= actdef;
477                 }
478         }
479         /* end copy from weight_paint*/
480         
481         copy_wpaint_prev(wp, me->dvert, me->totvert);
482         
483         for(index=0; index<me->totface; index++) {
484                 if(indexar[index] && indexar[index]<=me->totface) {
485                         mface= me->mface + (indexar[index]-1);
486                         /* just so we can loop through the verts */
487                         faceverts[0]= mface->v1;
488                         faceverts[1]= mface->v2;
489                         faceverts[2]= mface->v3;
490                         faceverts[3]= mface->v4;
491                         for (i=0; i<3 || faceverts[i]; i++) {
492                                 if(!((me->dvert+faceverts[i])->flag)) {
493                                         dw= verify_defweight(me->dvert+faceverts[i], vgroup);
494                                         if(dw) {
495                                                 uw= verify_defweight(wp->wpaint_prev+faceverts[i], vgroup);
496                                                 uw->weight= dw->weight; /* set the undo weight */
497                                                 dw->weight= paintweight;
498                                                 
499                                                 if(wp->flag & VP_MIRROR_X) {    /* x mirror painting */
500                                                         int j= mesh_get_x_mirror_vert(ob, faceverts[i]);
501                                                         if(j>=0) {
502                                                                 /* copy, not paint again */
503                                                                 if(vgroup_mirror != -1) {
504                                                                         dw= verify_defweight(me->dvert+j, vgroup_mirror);
505                                                                         uw= verify_defweight(wp->wpaint_prev+j, vgroup_mirror);
506                                                                 } else {
507                                                                         dw= verify_defweight(me->dvert+j, vgroup);
508                                                                         uw= verify_defweight(wp->wpaint_prev+j, vgroup);
509                                                                 }
510                                                                 uw->weight= dw->weight; /* set the undo weight */
511                                                                 dw->weight= paintweight;
512                                                         }
513                                                 }
514                                         }
515                                         (me->dvert+faceverts[i])->flag= 1;
516                                 }
517                         }
518                 }
519         }
520         
521         index=0;
522         while (index<me->totvert) {
523                 (me->dvert+index)->flag= 0;
524                 index++;
525         }
526         
527         MEM_freeN(indexar);
528         copy_wpaint_prev(wp, NULL, 0);
529
530         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
531 }
532
533
534 void vpaint_dogamma(Scene *scene)
535 {
536         VPaint *vp= scene->toolsettings->vpaint;
537         Mesh *me;
538         Object *ob;
539         float igam, fac;
540         int a, temp;
541         unsigned char *cp, gamtab[256];
542
543         if((G.f & G_VERTEXPAINT)==0) return;
544
545         ob= OBACT;
546         me= get_mesh(ob);
547         if(me==0 || me->mcol==0 || me->totface==0) return;
548
549         igam= 1.0/vp->gamma;
550         for(a=0; a<256; a++) {
551                 
552                 fac= ((float)a)/255.0;
553                 fac= vp->mul*pow( fac, igam);
554                 
555                 temp= 255.9*fac;
556                 
557                 if(temp<=0) gamtab[a]= 0;
558                 else if(temp>=255) gamtab[a]= 255;
559                 else gamtab[a]= temp;
560         }
561
562         a= 4*me->totface;
563         cp= (unsigned char *)me->mcol;
564         while(a--) {
565                 
566                 cp[1]= gamtab[ cp[1] ];
567                 cp[2]= gamtab[ cp[2] ];
568                 cp[3]= gamtab[ cp[3] ];
569                 
570                 cp+= 4;
571         }
572 }
573
574 static unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
575 {
576         char *cp1, *cp2, *cp;
577         int mfac;
578         unsigned int col=0;
579         
580         if(fac==0) return col1;
581         if(fac>=255) return col2;
582
583         mfac= 255-fac;
584         
585         cp1= (char *)&col1;
586         cp2= (char *)&col2;
587         cp=  (char *)&col;
588         
589         cp[0]= 255;
590         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
591         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
592         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
593         
594         return col;
595 }
596
597 static unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
598 {
599         char *cp1, *cp2, *cp;
600         int temp;
601         unsigned int col=0;
602         
603         if(fac==0) return col1;
604         
605         cp1= (char *)&col1;
606         cp2= (char *)&col2;
607         cp=  (char *)&col;
608         
609         cp[0]= 255;
610         temp= cp1[1] + ((fac*cp2[1])/255);
611         if(temp>254) cp[1]= 255; else cp[1]= temp;
612         temp= cp1[2] + ((fac*cp2[2])/255);
613         if(temp>254) cp[2]= 255; else cp[2]= temp;
614         temp= cp1[3] + ((fac*cp2[3])/255);
615         if(temp>254) cp[3]= 255; else cp[3]= temp;
616         
617         return col;
618 }
619
620 static unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
621 {
622         char *cp1, *cp2, *cp;
623         int temp;
624         unsigned int col=0;
625         
626         if(fac==0) return col1;
627         
628         cp1= (char *)&col1;
629         cp2= (char *)&col2;
630         cp=  (char *)&col;
631         
632         cp[0]= 255;
633         temp= cp1[1] - ((fac*cp2[1])/255);
634         if(temp<0) cp[1]= 0; else cp[1]= temp;
635         temp= cp1[2] - ((fac*cp2[2])/255);
636         if(temp<0) cp[2]= 0; else cp[2]= temp;
637         temp= cp1[3] - ((fac*cp2[3])/255);
638         if(temp<0) cp[3]= 0; else cp[3]= temp;
639         
640         return col;
641 }
642
643 static unsigned int mcol_mul(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
651         mfac= 255-fac;
652         
653         cp1= (char *)&col1;
654         cp2= (char *)&col2;
655         cp=  (char *)&col;
656         
657         /* first mul, then blend the fac */
658         cp[0]= 255;
659         cp[1]= (mfac*cp1[1] + fac*((cp2[1]*cp1[1])/255)  )/255;
660         cp[2]= (mfac*cp1[2] + fac*((cp2[2]*cp1[2])/255)  )/255;
661         cp[3]= (mfac*cp1[3] + fac*((cp2[3]*cp1[3])/255)  )/255;
662
663         
664         return col;
665 }
666
667 static unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
668 {
669         char *cp1, *cp2, *cp;
670         int mfac;
671         unsigned int col=0;
672         
673         if(fac==0) return col1;
674         if(fac>=255) return col2;
675
676         mfac= 255-fac;
677         
678         cp1= (char *)&col1;
679         cp2= (char *)&col2;
680         cp=  (char *)&col;
681         
682         /* See if are lighter, if so mix, else dont do anything.
683         if the paint col is darker then the original, then ignore */
684         if (cp1[1]+cp1[2]+cp1[3] > cp2[1]+cp2[2]+cp2[3])
685                 return col1;
686         
687         cp[0]= 255;
688         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
689         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
690         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
691         
692         return col;
693 }
694
695 static unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
696 {
697         char *cp1, *cp2, *cp;
698         int mfac;
699         unsigned int col=0;
700         
701         if(fac==0) return col1;
702         if(fac>=255) return col2;
703
704         mfac= 255-fac;
705         
706         cp1= (char *)&col1;
707         cp2= (char *)&col2;
708         cp=  (char *)&col;
709         
710         /* See if were darker, if so mix, else dont do anything.
711         if the paint col is brighter then the original, then ignore */
712         if (cp1[1]+cp1[2]+cp1[3] < cp2[1]+cp2[2]+cp2[3])
713                 return col1;
714         
715         cp[0]= 255;
716         cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
717         cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
718         cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
719         return col;
720 }
721
722 static void vpaint_blend(VPaint *vp, unsigned int *col, unsigned int *colorig, unsigned int paintcol, int alpha)
723 {
724
725         if(vp->mode==VP_MIX || vp->mode==VP_BLUR) *col= mcol_blend( *col, paintcol, alpha);
726         else if(vp->mode==VP_ADD) *col= mcol_add( *col, paintcol, alpha);
727         else if(vp->mode==VP_SUB) *col= mcol_sub( *col, paintcol, alpha);
728         else if(vp->mode==VP_MUL) *col= mcol_mul( *col, paintcol, alpha);
729         else if(vp->mode==VP_LIGHTEN) *col= mcol_lighten( *col, paintcol, alpha);
730         else if(vp->mode==VP_DARKEN) *col= mcol_darken( *col, paintcol, alpha);
731         
732         /* if no spray, clip color adding with colorig & orig alpha */
733         if((vp->flag & VP_SPRAY)==0) {
734                 unsigned int testcol=0, a;
735                 char *cp, *ct, *co;
736                 
737                 alpha= (int)(255.0*vp->brush->alpha);
738                 
739                 if(vp->mode==VP_MIX || vp->mode==VP_BLUR) testcol= mcol_blend( *colorig, paintcol, alpha);
740                 else if(vp->mode==VP_ADD) testcol= mcol_add( *colorig, paintcol, alpha);
741                 else if(vp->mode==VP_SUB) testcol= mcol_sub( *colorig, paintcol, alpha);
742                 else if(vp->mode==VP_MUL) testcol= mcol_mul( *colorig, paintcol, alpha);
743                 else if(vp->mode==VP_LIGHTEN)  testcol= mcol_lighten( *colorig, paintcol, alpha);
744                 else if(vp->mode==VP_DARKEN)   testcol= mcol_darken( *colorig, paintcol, alpha);
745                 
746                 cp= (char *)col;
747                 ct= (char *)&testcol;
748                 co= (char *)colorig;
749                 
750                 for(a=0; a<4; a++) {
751                         if( ct[a]<co[a] ) {
752                                 if( cp[a]<ct[a] ) cp[a]= ct[a];
753                                 else if( cp[a]>co[a] ) cp[a]= co[a];
754                         }
755                         else {
756                                 if( cp[a]<co[a] ) cp[a]= co[a];
757                                 else if( cp[a]>ct[a] ) cp[a]= ct[a];
758                         }
759                 }
760         }
761 }
762
763
764 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
765 {
766         struct ImBuf *ibuf;
767         int a, tot=0, index;
768         
769         if(totface+4>=MAXINDEX) return 0;
770         
771         if(size>64.0) size= 64.0;
772         
773         ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
774         if(ibuf) {
775                 unsigned int *rt= ibuf->rect;
776
777                 memset(indexar, 0, sizeof(int)*totface+4);      /* plus 2! first element is total, +2 was giving valgrind errors, +4 seems ok */
778                 
779                 size= ibuf->x*ibuf->y;
780                 while(size--) {
781                                 
782                         if(*rt) {
783                                 index= WM_framebuffer_to_index(*rt);
784                                 if(index>0 && index<=totface)
785                                         indexar[index] = 1;
786                         }
787                 
788                         rt++;
789                 }
790                 
791                 for(a=1; a<=totface; a++) {
792                         if(indexar[a]) indexar[tot++]= a;
793                 }
794
795                 IMB_freeImBuf(ibuf);
796         }
797         
798         return tot;
799 }
800
801 static int calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], float *vert_nor, short *mval)
802 {
803         float fac, dx, dy;
804         int alpha;
805         short vertco[2];
806         
807         if(vp->flag & VP_SOFT) {
808                 project_short_noclip(vc->ar, vert_nor, vertco);
809                 dx= mval[0]-vertco[0];
810                 dy= mval[1]-vertco[1];
811                 
812                 fac= sqrt(dx*dx + dy*dy);
813                 if(fac > vp->brush->size) return 0;
814                 if(vp->flag & VP_HARD)
815                         alpha= 255;
816                 else
817                         alpha= 255.0*vp->brush->alpha*(1.0-fac/vp->brush->size);
818         }
819         else {
820                 alpha= 255.0*vp->brush->alpha;
821         }
822
823         if(vp->flag & VP_NORMALS) {
824                 float *no= vert_nor+3;
825                 
826                         /* transpose ! */
827                 fac= vpimat[2][0]*no[0]+vpimat[2][1]*no[1]+vpimat[2][2]*no[2];
828                 if(fac>0.0) {
829                         dx= vpimat[0][0]*no[0]+vpimat[0][1]*no[1]+vpimat[0][2]*no[2];
830                         dy= vpimat[1][0]*no[0]+vpimat[1][1]*no[1]+vpimat[1][2]*no[2];
831                         
832                         alpha*= fac/sqrt(dx*dx + dy*dy + fac*fac);
833                 }
834                 else return 0;
835         }
836         
837         return alpha;
838 }
839
840 static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval)
841 {
842         
843         if(dw==NULL || uw==NULL) return;
844         
845         if(wp->mode==VP_MIX || wp->mode==VP_BLUR)
846                 dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
847         else if(wp->mode==VP_ADD)
848                 dw->weight += paintval*alpha;
849         else if(wp->mode==VP_SUB) 
850                 dw->weight -= paintval*alpha;
851         else if(wp->mode==VP_MUL) 
852                 /* first mul, then blend the fac */
853                 dw->weight = ((1.0-alpha) + alpha*paintval)*dw->weight;
854         else if(wp->mode==VP_LIGHTEN) {
855                 if (dw->weight < paintval)
856                         dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
857         } else if(wp->mode==VP_DARKEN) {
858                 if (dw->weight > paintval)
859                         dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
860         }
861         CLAMP(dw->weight, 0.0f, 1.0f);
862         
863         /* if no spray, clip result with orig weight & orig alpha */
864         if((wp->flag & VP_SPRAY)==0) {
865                 float testw=0.0f;
866                 
867                 alpha= wp->brush->alpha;
868                 if(wp->mode==VP_MIX || wp->mode==VP_BLUR)
869                         testw = paintval*alpha + uw->weight*(1.0-alpha);
870                 else if(wp->mode==VP_ADD)
871                         testw = uw->weight + paintval*alpha;
872                 else if(wp->mode==VP_SUB) 
873                         testw = uw->weight - paintval*alpha;
874                 else if(wp->mode==VP_MUL) 
875                         /* first mul, then blend the fac */
876                         testw = ((1.0-alpha) + alpha*paintval)*uw->weight;              
877                 else if(wp->mode==VP_LIGHTEN) {
878                         if (uw->weight < paintval)
879                                 testw = paintval*alpha + uw->weight*(1.0-alpha);
880                         else
881                                 testw = uw->weight;
882                 } else if(wp->mode==VP_DARKEN) {
883                         if (uw->weight > paintval)
884                                 testw = paintval*alpha + uw->weight*(1.0-alpha);
885                         else
886                                 testw = uw->weight;
887                 }
888                 CLAMP(testw, 0.0f, 1.0f);
889                 
890                 if( testw<uw->weight ) {
891                         if(dw->weight < testw) dw->weight= testw;
892                         else if(dw->weight > uw->weight) dw->weight= uw->weight;
893                 }
894                 else {
895                         if(dw->weight > testw) dw->weight= testw;
896                         else if(dw->weight < uw->weight) dw->weight= uw->weight;
897                 }
898         }
899         
900 }
901
902 /* ----------------------------------------------------- */
903
904 /* used for 3d view, on active object, assumes me->dvert exists */
905 /* if mode==1: */
906 /*     samples cursor location, and gives menu with vertex groups to activate */
907 /* else */
908 /*     sets wp->weight to the closest weight value to vertex */
909 /*     note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
910 void sample_wpaint(Scene *scene, ARegion *ar, View3D *v3d, int mode)
911 {
912         ViewContext vc;
913         ToolSettings *ts= scene->toolsettings;
914         Object *ob= OBACT;
915         Mesh *me= get_mesh(ob);
916         int index;
917         short mval[2], sco[2];
918
919         if (!me) return;
920         
921 //      getmouseco_areawin(mval);
922         index= view3d_sample_backbuf(&vc, mval[0], mval[1]);
923         
924         if(index && index<=me->totface) {
925                 MFace *mface;
926                 
927                 mface= ((MFace *)me->mface) + index-1;
928                 
929                 if(mode==1) {   /* sampe which groups are in here */
930                         MDeformVert *dv;
931                         int a, totgroup;
932                         
933                         totgroup= BLI_countlist(&ob->defbase);
934                         if(totgroup) {
935                                 int totmenu=0;
936                                 int *groups=MEM_callocN(totgroup*sizeof(int), "groups");
937                                 
938                                 dv= me->dvert+mface->v1;
939                                 for(a=0; a<dv->totweight; a++) {
940                                         if (dv->dw[a].def_nr<totgroup)
941                                                 groups[dv->dw[a].def_nr]= 1;
942                                 }
943                                 dv= me->dvert+mface->v2;
944                                 for(a=0; a<dv->totweight; a++) {
945                                         if (dv->dw[a].def_nr<totgroup)
946                                                 groups[dv->dw[a].def_nr]= 1;
947                                 }
948                                 dv= me->dvert+mface->v3;
949                                 for(a=0; a<dv->totweight; a++) {
950                                         if (dv->dw[a].def_nr<totgroup)
951                                                 groups[dv->dw[a].def_nr]= 1;
952                                 }
953                                 if(mface->v4) {
954                                         dv= me->dvert+mface->v4;
955                                         for(a=0; a<dv->totweight; a++) {
956                                                 if (dv->dw[a].def_nr<totgroup)
957                                                         groups[dv->dw[a].def_nr]= 1;
958                                         }
959                                 }
960                                 for(a=0; a<totgroup; a++)
961                                         if(groups[a]) totmenu++;
962                                 
963                                 if(totmenu==0) {
964                                         //notice("No Vertex Group Selected");
965                                 }
966                                 else {
967                                         bDeformGroup *dg;
968                                         short val;
969                                         char item[40], *str= MEM_mallocN(40*totmenu+40, "menu");
970                                         
971                                         strcpy(str, "Vertex Groups %t");
972                                         for(a=0, dg=ob->defbase.first; dg && a<totgroup; a++, dg= dg->next) {
973                                                 if(groups[a]) {
974                                                         sprintf(item, "|%s %%x%d", dg->name, a);
975                                                         strcat(str, item);
976                                                 }
977                                         }
978                                         
979                                         val= 0; // XXX pupmenu(str);
980                                         if(val>=0) {
981                                                 ob->actdef= val+1;
982                                                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
983                                         }
984                                         MEM_freeN(str);
985                                 }
986                                 MEM_freeN(groups);
987                         }
988 //                      else notice("No Vertex Groups in Object");
989                 }
990                 else {
991                         DerivedMesh *dm;
992                         MDeformWeight *dw;
993                         float w1, w2, w3, w4, co[3], fac;
994                         
995                         dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
996                         if(dm->getVertCo==NULL) {
997                                 //notice("Not supported yet");
998                         }
999                         else {
1000                                 /* calc 3 or 4 corner weights */
1001                                 dm->getVertCo(dm, mface->v1, co);
1002                                 project_short_noclip(ar, co, sco);
1003                                 w1= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
1004                                 
1005                                 dm->getVertCo(dm, mface->v2, co);
1006                                 project_short_noclip(ar, co, sco);
1007                                 w2= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
1008                                 
1009                                 dm->getVertCo(dm, mface->v3, co);
1010                                 project_short_noclip(ar, co, sco);
1011                                 w3= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
1012                                 
1013                                 if(mface->v4) {
1014                                         dm->getVertCo(dm, mface->v4, co);
1015                                         project_short_noclip(ar, co, sco);
1016                                         w4= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
1017                                 }
1018                                 else w4= 1.0e10;
1019                                 
1020                                 fac= MIN4(w1, w2, w3, w4);
1021                                 if(w1==fac) {
1022                                         dw= get_defweight(me->dvert+mface->v1, ob->actdef-1);
1023                                         if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
1024                                 }
1025                                 else if(w2==fac) {
1026                                         dw= get_defweight(me->dvert+mface->v2, ob->actdef-1);
1027                                         if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
1028                                 }
1029                                 else if(w3==fac) {
1030                                         dw= get_defweight(me->dvert+mface->v3, ob->actdef-1);
1031                                         if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
1032                                 }
1033                                 else if(w4==fac) {
1034                                         if(mface->v4) {
1035                                                 dw= get_defweight(me->dvert+mface->v4, ob->actdef-1);
1036                                                 if(dw) ts->vgroup_weight= dw->weight; else ts->vgroup_weight= 0.0f;
1037                                         }
1038                                 }
1039                         }
1040                         dm->release(dm);
1041                 }               
1042                 
1043         }
1044         
1045 }
1046
1047 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, int alpha, float paintweight, int vgroup_mirror)
1048 {
1049         Mesh *me= ob->data;
1050         MDeformWeight *dw, *uw;
1051         int vgroup= ob->actdef-1;
1052         
1053         if(wp->flag & VP_ONLYVGROUP) {
1054                 dw= get_defweight(me->dvert+index, vgroup);
1055                 uw= get_defweight(wp->wpaint_prev+index, vgroup);
1056         }
1057         else {
1058                 dw= verify_defweight(me->dvert+index, vgroup);
1059                 uw= verify_defweight(wp->wpaint_prev+index, vgroup);
1060         }
1061         if(dw==NULL || uw==NULL)
1062                 return;
1063         
1064         wpaint_blend(wp, dw, uw, (float)alpha/255.0, paintweight);
1065         
1066         if(wp->flag & VP_MIRROR_X) {    /* x mirror painting */
1067                 int j= mesh_get_x_mirror_vert(ob, index);
1068                 if(j>=0) {
1069                         /* copy, not paint again */
1070                         if(vgroup_mirror != -1)
1071                                 uw= verify_defweight(me->dvert+j, vgroup_mirror);
1072                         else
1073                                 uw= verify_defweight(me->dvert+j, vgroup);
1074                                 
1075                         uw->weight= dw->weight;
1076                 }
1077         }
1078 }
1079
1080
1081 /* *************** set wpaint operator ****************** */
1082
1083 static int set_wpaint(bContext *C, wmOperator *op)              /* toggle */
1084 {               
1085         Object *ob= CTX_data_active_object(C);
1086         Scene *scene= CTX_data_scene(C);
1087         VPaint *wp= scene->toolsettings->wpaint;
1088         Mesh *me;
1089         
1090         me= get_mesh(ob);
1091         if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1092         
1093         if(me && me->totface>=MAXINDEX) {
1094                 error("Maximum number of faces: %d", MAXINDEX-1);
1095                 G.f &= ~G_WEIGHTPAINT;
1096                 return OPERATOR_CANCELLED;
1097         }
1098         
1099         if(G.f & G_WEIGHTPAINT) G.f &= ~G_WEIGHTPAINT;
1100         else G.f |= G_WEIGHTPAINT;
1101         
1102         
1103         /* Weightpaint works by overriding colors in mesh,
1104                 * so need to make sure we recalc on enter and
1105                 * exit (exit needs doing regardless because we
1106                                 * should redeform).
1107                 */
1108         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1109         
1110         if(G.f & G_WEIGHTPAINT) {
1111                 Object *par;
1112                 
1113                 if(wp==NULL)
1114                         wp= scene->toolsettings->wpaint= new_vpaint(1);
1115
1116                 brush_check_exists(&wp->brush);
1117                 
1118                 toggle_paint_cursor(C, 1);
1119                 
1120                 mesh_octree_table(ob, NULL, NULL, 's');
1121                 
1122                 /* verify if active weight group is also active bone */
1123                 par= modifiers_isDeformedByArmature(ob);
1124                 if(par && (par->flag & OB_POSEMODE)) {
1125                         bPoseChannel *pchan;
1126                         for(pchan= par->pose->chanbase.first; pchan; pchan= pchan->next)
1127                                 if(pchan->bone->flag & BONE_ACTIVE)
1128                                         break;
1129                                 if(pchan)
1130                                         vertexgroup_select_by_name(ob, pchan->name);
1131                 }
1132         }
1133         else {
1134                 if(wp)
1135                         toggle_paint_cursor(C, 1);
1136                 
1137                 mesh_octree_table(ob, NULL, NULL, 'e');
1138         }
1139         
1140         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1141         
1142         return OPERATOR_FINISHED;
1143 }
1144
1145 /* for switching to/from mode */
1146 static int paint_poll_test(bContext *C)
1147 {
1148         if(ED_operator_view3d_active(C)==0)
1149                 return 0;
1150         if(CTX_data_edit_object(C))
1151                 return 0;
1152         if(CTX_data_active_object(C)==NULL)
1153                 return 0;
1154         return 1;
1155 }
1156
1157 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1158 {
1159         
1160         /* identifiers */
1161         ot->name= "Weight Paint Mode";
1162         ot->idname= "PAINT_OT_weight_paint_toggle";
1163         
1164         /* api callbacks */
1165         ot->exec= set_wpaint;
1166         ot->poll= paint_poll_test;
1167         
1168         /* flags */
1169         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1170         
1171 }
1172
1173 /* ************ paint radial controls *************/
1174
1175 static int vpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1176 {
1177         toggle_paint_cursor(C, 0);
1178         brush_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->vpaint->brush, 1);
1179         return WM_radial_control_invoke(C, op, event);
1180 }
1181
1182 static int vpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1183 {
1184         int ret = WM_radial_control_modal(C, op, event);
1185         if(ret != OPERATOR_RUNNING_MODAL)
1186                 toggle_paint_cursor(C, 0);
1187         return ret;
1188 }
1189
1190 static int vpaint_radial_control_exec(bContext *C, wmOperator *op)
1191 {
1192         return brush_radial_control_exec(op, CTX_data_scene(C)->toolsettings->vpaint->brush, 1);
1193 }
1194
1195 static int wpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1196 {
1197         toggle_paint_cursor(C, 1);
1198         brush_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->wpaint->brush, 1);
1199         return WM_radial_control_invoke(C, op, event);
1200 }
1201
1202 static int wpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1203 {
1204         int ret = WM_radial_control_modal(C, op, event);
1205         if(ret != OPERATOR_RUNNING_MODAL)
1206                 toggle_paint_cursor(C, 1);
1207         return ret;
1208 }
1209
1210 static int wpaint_radial_control_exec(bContext *C, wmOperator *op)
1211 {
1212         return brush_radial_control_exec(op, CTX_data_scene(C)->toolsettings->wpaint->brush, 1);
1213 }
1214
1215 void PAINT_OT_weight_paint_radial_control(wmOperatorType *ot)
1216 {
1217         WM_OT_radial_control_partial(ot);
1218
1219         ot->name= "Weight Paint Radial Control";
1220         ot->idname= "PAINT_OT_weight_paint_radial_control";
1221
1222         ot->invoke= wpaint_radial_control_invoke;
1223         ot->modal= wpaint_radial_control_modal;
1224         ot->exec= wpaint_radial_control_exec;
1225         ot->poll= wp_poll;
1226         
1227         /* flags */
1228         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1229 }
1230
1231 void PAINT_OT_vertex_paint_radial_control(wmOperatorType *ot)
1232 {
1233         WM_OT_radial_control_partial(ot);
1234
1235         ot->name= "Vertex Paint Radial Control";
1236         ot->idname= "PAINT_OT_vertex_paint_radial_control";
1237
1238         ot->invoke= vpaint_radial_control_invoke;
1239         ot->modal= vpaint_radial_control_modal;
1240         ot->exec= vpaint_radial_control_exec;
1241         ot->poll= vp_poll;
1242         
1243         /* flags */
1244         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1245 }
1246
1247 /* ************ weight paint operator ********** */
1248
1249 struct WPaintData {
1250         ViewContext vc;
1251         int *indexar;
1252         int vgroup_mirror;
1253         float *vertexcosnos;
1254         float wpimat[3][3];
1255 };
1256
1257 static void wpaint_exit(bContext *C, wmOperator *op)
1258 {
1259         ToolSettings *ts= CTX_data_tool_settings(C);
1260         Object *ob= CTX_data_active_object(C);
1261         struct WPaintData *wpd= op->customdata;
1262         
1263         if(wpd->vertexcosnos)
1264                 MEM_freeN(wpd->vertexcosnos);
1265         MEM_freeN(wpd->indexar);
1266         
1267         /* frees prev buffer */
1268         copy_wpaint_prev(ts->wpaint, NULL, 0);
1269         
1270         /* and particles too */
1271         if(ob->particlesystem.first) {
1272                 ParticleSystem *psys;
1273                 int i;
1274                 
1275                 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
1276                         for(i=0; i<PSYS_TOT_VG; i++) {
1277                                 if(psys->vgroup[i]==ob->actdef) {
1278                                         psys->recalc |= PSYS_RECALC_RESET;
1279                                         break;
1280                                 }
1281                         }
1282                 }
1283         }
1284         
1285         DAG_object_flush_update(CTX_data_scene(C), ob, OB_RECALC_DATA);
1286         
1287         MEM_freeN(wpd);
1288         op->customdata= NULL;
1289 }
1290
1291
1292 static int wpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
1293 {
1294         ToolSettings *ts= CTX_data_tool_settings(C);
1295         VPaint *wp= ts->wpaint;
1296         
1297         switch(event->type) {
1298                 case LEFTMOUSE:
1299                         if(event->val==0) { /* release */
1300                                 wpaint_exit(C, op);
1301                                 return OPERATOR_FINISHED;
1302                         }
1303                         /* pass on, first press gets painted too */
1304                         
1305                 case MOUSEMOVE: 
1306                 {
1307                         struct WPaintData *wpd= op->customdata;
1308                         ViewContext *vc= &wpd->vc;
1309                         Object *ob= vc->obact;
1310                         Mesh *me= ob->data;
1311                         float mat[4][4];
1312                         float paintweight= ts->vgroup_weight;
1313                         int *indexar= wpd->indexar;
1314                         int totindex, index, alpha, totw;
1315                         short mval[2];
1316                         
1317                         view3d_operator_needs_opengl(C);
1318                         
1319                         /* load projection matrix */
1320                         wmMultMatrix(ob->obmat);
1321                         wmGetSingleMatrix(mat);
1322                         wmLoadMatrix(wpd->vc.rv3d->viewmat);
1323                         
1324                         MTC_Mat4SwapMat4(wpd->vc.rv3d->persmat, mat);
1325                         
1326                         mval[0]= event->x - vc->ar->winrct.xmin;
1327                         mval[1]= event->y - vc->ar->winrct.ymin;
1328                         
1329                         /* which faces are involved */
1330                         if(wp->flag & VP_AREA) {
1331                                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], wp->brush->size);
1332                         }
1333                         else {
1334                                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1335                                 if(indexar[0]) totindex= 1;
1336                                 else totindex= 0;
1337                         }
1338                         
1339                         if(wp->flag & VP_COLINDEX) {
1340                                 for(index=0; index<totindex; index++) {
1341                                         if(indexar[index] && indexar[index]<=me->totface) {
1342                                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1343                                                 
1344                                                 if(mface->mat_nr!=ob->actcol-1) {
1345                                                         indexar[index]= 0;
1346                                                 }
1347                                         }                                       
1348                                 }
1349                         }
1350                         
1351                         if((G.f & G_FACESELECT) && me->mface) {
1352                                 for(index=0; index<totindex; index++) {
1353                                         if(indexar[index] && indexar[index]<=me->totface) {
1354                                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1355                                                 
1356                                                 if((mface->flag & ME_FACE_SEL)==0) {
1357                                                         indexar[index]= 0;
1358                                                 }
1359                                         }                                       
1360                                 }
1361                         }
1362                         
1363                         /* make sure each vertex gets treated only once */
1364                         /* and calculate filter weight */
1365                         totw= 0;
1366                         if(wp->mode==VP_BLUR) 
1367                                 paintweight= 0.0f;
1368                         else
1369                                 paintweight= ts->vgroup_weight;
1370                         
1371                         for(index=0; index<totindex; index++) {
1372                                 if(indexar[index] && indexar[index]<=me->totface) {
1373                                         MFace *mface= me->mface + (indexar[index]-1);
1374                                         
1375                                         (me->dvert+mface->v1)->flag= 1;
1376                                         (me->dvert+mface->v2)->flag= 1;
1377                                         (me->dvert+mface->v3)->flag= 1;
1378                                         if(mface->v4) (me->dvert+mface->v4)->flag= 1;
1379                                         
1380                                         if(wp->mode==VP_BLUR) {
1381                                                 MDeformWeight *dw, *(*dw_func)(MDeformVert *, int) = verify_defweight;
1382                                                 
1383                                                 if(wp->flag & VP_ONLYVGROUP)
1384                                                         dw_func= get_defweight;
1385                                                 
1386                                                 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
1387                                                 if(dw) {paintweight+= dw->weight; totw++;}
1388                                                 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
1389                                                 if(dw) {paintweight+= dw->weight; totw++;}
1390                                                 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
1391                                                 if(dw) {paintweight+= dw->weight; totw++;}
1392                                                 if(mface->v4) {
1393                                                         dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
1394                                                         if(dw) {paintweight+= dw->weight; totw++;}
1395                                                 }
1396                                         }
1397                                 }
1398                         }
1399                         
1400                         if(wp->mode==VP_BLUR) 
1401                                 paintweight/= (float)totw;
1402                         
1403                         for(index=0; index<totindex; index++) {
1404                                 
1405                                 if(indexar[index] && indexar[index]<=me->totface) {
1406                                         MFace *mface= me->mface + (indexar[index]-1);
1407                                         
1408                                         if((me->dvert+mface->v1)->flag) {
1409                                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval);
1410                                                 if(alpha) {
1411                                                         do_weight_paint_vertex(wp, ob, mface->v1, alpha, paintweight, wpd->vgroup_mirror);
1412                                                 }
1413                                                 (me->dvert+mface->v1)->flag= 0;
1414                                         }
1415                                         
1416                                         if((me->dvert+mface->v2)->flag) {
1417                                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval);
1418                                                 if(alpha) {
1419                                                         do_weight_paint_vertex(wp, ob, mface->v2, alpha, paintweight, wpd->vgroup_mirror);
1420                                                 }
1421                                                 (me->dvert+mface->v2)->flag= 0;
1422                                         }
1423                                         
1424                                         if((me->dvert+mface->v3)->flag) {
1425                                                 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval);
1426                                                 if(alpha) {
1427                                                         do_weight_paint_vertex(wp, ob, mface->v3, alpha, paintweight, wpd->vgroup_mirror);
1428                                                 }
1429                                                 (me->dvert+mface->v3)->flag= 0;
1430                                         }
1431                                         
1432                                         if((me->dvert+mface->v4)->flag) {
1433                                                 if(mface->v4) {
1434                                                         alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval);
1435                                                         if(alpha) {
1436                                                                 do_weight_paint_vertex(wp, ob, mface->v4, alpha, paintweight, wpd->vgroup_mirror);
1437                                                         }
1438                                                         (me->dvert+mface->v4)->flag= 0;
1439                                                 }
1440                                         }
1441                                 }
1442                         }
1443                         
1444                         MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
1445                         
1446                         DAG_object_flush_update(vc->scene, ob, OB_RECALC_DATA);
1447                         ED_region_tag_redraw(vc->ar);
1448                 }
1449         }
1450
1451         return OPERATOR_RUNNING_MODAL;
1452 }
1453
1454 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1455 {
1456         Scene *scene= CTX_data_scene(C);
1457         ToolSettings *ts= CTX_data_tool_settings(C);
1458         VPaint *wp= ts->wpaint;
1459         Object *ob= CTX_data_active_object(C);
1460         struct WPaintData *wpd;
1461         Mesh *me;
1462         float mat[4][4], imat[4][4];
1463         
1464         if(scene->obedit) return OPERATOR_CANCELLED;
1465         // XXX  if(multires_level1_test()) return;
1466         
1467         me= get_mesh(ob);
1468         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1469         
1470         /* if nothing was added yet, we make dverts and a vertex deform group */
1471         if (!me->dvert)
1472                 create_dverts(&me->id);
1473         
1474         /* make customdata storage */
1475         op->customdata= wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1476         view3d_set_viewcontext(C, &wpd->vc);
1477         wpd->vgroup_mirror= -1;
1478         
1479         //      if(qual & LR_CTRLKEY) {
1480         //              sample_wpaint(scene, ar, v3d, 0);
1481         //              return;
1482         //      }
1483         //      if(qual & LR_SHIFTKEY) {
1484         //              sample_wpaint(scene, ar, v3d, 1);
1485         //              return;
1486         //      }
1487         
1488         /* ALLOCATIONS! no return after this line */
1489         /* painting on subsurfs should give correct points too, this returns me->totvert amount */
1490         wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
1491         wpd->indexar= get_indexarray();
1492         copy_wpaint_prev(wp, me->dvert, me->totvert);
1493         
1494         /* this happens on a Bone select, when no vgroup existed yet */
1495         if(ob->actdef<=0) {
1496                 Object *modob;
1497                 if((modob = modifiers_isDeformedByArmature(ob))) {
1498                         bPoseChannel *pchan;
1499                         for(pchan= modob->pose->chanbase.first; pchan; pchan= pchan->next)
1500                                 if(pchan->bone->flag & SELECT)
1501                                         break;
1502                         if(pchan) {
1503                                 bDeformGroup *dg= get_named_vertexgroup(ob, pchan->name);
1504                                 if(dg==NULL)
1505                                         dg= add_defgroup_name(ob, pchan->name); /* sets actdef */
1506                                 else
1507                                         ob->actdef= get_defgroup_num(ob, dg);
1508                         }
1509                 }
1510         }
1511         if(ob->defbase.first==NULL) {
1512                 add_defgroup(ob);
1513         }       
1514         
1515         //      if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1516         
1517         /* imat for normals */
1518         Mat4MulMat4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1519         Mat4Invert(imat, mat);
1520         Mat3CpyMat4(wpd->wpimat, imat);
1521         
1522         /* if mirror painting, find the other group */
1523         if(wp->flag & VP_MIRROR_X) {
1524                 bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
1525                 if(defgroup) {
1526                         bDeformGroup *curdef;
1527                         int actdef= 0;
1528                         char name[32];
1529                         
1530                         BLI_strncpy(name, defgroup->name, 32);
1531                         bone_flip_name(name, 0);                /* 0 = don't strip off number extensions */
1532                         
1533                         for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
1534                                 if (!strcmp(curdef->name, name))
1535                                         break;
1536                         if(curdef==NULL) {
1537                                 int olddef= ob->actdef; /* tsk, add_defgroup sets the active defgroup */
1538                                 curdef= add_defgroup_name (ob, name);
1539                                 ob->actdef= olddef;
1540                         }
1541                         
1542                         if(curdef && curdef!=defgroup)
1543                                 wpd->vgroup_mirror= actdef;
1544                 }
1545         }
1546         
1547         /* do paint once for click only paint */
1548         wpaint_modal(C, op, event);
1549         
1550         /* add modal handler */
1551         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1552         
1553         return OPERATOR_RUNNING_MODAL;
1554 }
1555
1556 void PAINT_OT_weight_paint(wmOperatorType *ot)
1557 {
1558         
1559         /* identifiers */
1560         ot->name= "Weight Paint";
1561         ot->idname= "PAINT_OT_weight_paint";
1562         
1563         /* api callbacks */
1564         ot->invoke= wpaint_invoke;
1565         ot->modal= wpaint_modal;
1566         /* ot->exec= vpaint_exec; <-- needs stroke property */
1567         ot->poll= wp_poll;
1568         
1569         /* flags */
1570         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1571         
1572 }
1573
1574 /* ************ set / clear vertex paint mode ********** */
1575
1576
1577 static int set_vpaint(bContext *C, wmOperator *op)              /* toggle */
1578 {       
1579         Object *ob= CTX_data_active_object(C);
1580         Scene *scene= CTX_data_scene(C);
1581         VPaint *vp= scene->toolsettings->vpaint;
1582         Mesh *me;
1583         
1584         me= get_mesh(ob);
1585         
1586         if(me==NULL || object_data_is_libdata(ob)) {
1587                 G.f &= ~G_VERTEXPAINT;
1588                 return OPERATOR_PASS_THROUGH;
1589         }
1590         
1591         if(me && me->totface>=MAXINDEX) {
1592                 error("Maximum number of faces: %d", MAXINDEX-1);
1593                 G.f &= ~G_VERTEXPAINT;
1594                 return OPERATOR_FINISHED;
1595         }
1596         
1597         if(me && me->mcol==NULL) make_vertexcol(scene, 0);
1598         
1599         /* toggle: end vpaint */
1600         if(G.f & G_VERTEXPAINT) {
1601                 
1602                 G.f &= ~G_VERTEXPAINT;
1603                 
1604                 if(vp) {
1605                         toggle_paint_cursor(C, 0);
1606                         vp->paintcursor= NULL;
1607                 }
1608         }
1609         else {
1610                 
1611                 G.f |= G_VERTEXPAINT;
1612                 /* Turn off weight painting */
1613                 if (G.f & G_WEIGHTPAINT)
1614                         set_wpaint(C, op);
1615                 
1616                 if(vp==NULL)
1617                         vp= scene->toolsettings->vpaint= new_vpaint(0);
1618                 
1619                 toggle_paint_cursor(C, 0);
1620                 brush_check_exists(&scene->toolsettings->vpaint->brush);
1621         }
1622         
1623         if (me)
1624                 /* update modifier stack for mapping requirements */
1625                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1626         
1627         WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1628         
1629         return OPERATOR_FINISHED;
1630 }
1631
1632 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
1633 {
1634         
1635         /* identifiers */
1636         ot->name= "Vertex Paint Mode";
1637         ot->idname= "PAINT_OT_vertex_paint_toggle";
1638         
1639         /* api callbacks */
1640         ot->exec= set_vpaint;
1641         ot->poll= paint_poll_test;
1642         
1643         /* flags */
1644         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1645         
1646 }
1647
1648
1649
1650 /* ********************** vertex paint operator ******************* */
1651
1652 /* Implementation notes:
1653
1654 Operator->invoke()
1655   - validate context (add mcol)
1656   - create customdata storage
1657   - call paint once (mouse click)
1658   - add modal handler 
1659
1660 Operator->modal()
1661   - for every mousemove, apply vertex paint
1662   - exit on mouse release, free customdata
1663     (return OPERATOR_FINISHED also removes handler and operator)
1664
1665 For future:
1666   - implement a stroke event (or mousemove with past positons)
1667   - revise whether op->customdata should be added in object, in set_vpaint
1668
1669 */
1670
1671 struct VPaintData {
1672         ViewContext vc;
1673         unsigned int paintcol;
1674         int *indexar;
1675         float *vertexcosnos;
1676         float vpimat[3][3];
1677 };
1678
1679 static void vpaint_exit(bContext *C, wmOperator *op)
1680 {
1681         ToolSettings *ts= CTX_data_tool_settings(C);
1682         struct VPaintData *vpd= op->customdata;
1683         
1684         if(vpd->vertexcosnos)
1685                 MEM_freeN(vpd->vertexcosnos);
1686         MEM_freeN(vpd->indexar);
1687         
1688         /* frees prev buffer */
1689         copy_vpaint_prev(ts->vpaint, NULL, 0);
1690         
1691         MEM_freeN(vpd);
1692         op->customdata= NULL;
1693 }
1694
1695 static int vpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
1696 {
1697         ToolSettings *ts= CTX_data_tool_settings(C);
1698         VPaint *vp= ts->vpaint;
1699         
1700         switch(event->type) {
1701                 case LEFTMOUSE:
1702                         if(event->val==0) { /* release */
1703                                 vpaint_exit(C, op);
1704                                 return OPERATOR_FINISHED;
1705                         }
1706                         /* pass on, first press gets painted too */
1707                         
1708                 case MOUSEMOVE: 
1709                 {
1710                         struct VPaintData *vpd= op->customdata;
1711                         ViewContext *vc= &vpd->vc;
1712                         Object *ob= vc->obact;
1713                         Mesh *me= ob->data;
1714                         float mat[4][4];
1715                         int *indexar= vpd->indexar;
1716                         int totindex, index;
1717                         short mval[2];
1718                         
1719                         view3d_operator_needs_opengl(C);
1720                         
1721                         /* load projection matrix */
1722                         wmMultMatrix(ob->obmat);
1723                         wmGetSingleMatrix(mat);
1724                         wmLoadMatrix(vc->rv3d->viewmat);
1725                         
1726                         mval[0]= event->x - vc->ar->winrct.xmin;
1727                         mval[1]= event->y - vc->ar->winrct.ymin;
1728                                 
1729                         /* which faces are involved */
1730                         if(vp->flag & VP_AREA) {
1731                                 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], vp->brush->size);
1732                         }
1733                         else {
1734                                 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1735                                 if(indexar[0]) totindex= 1;
1736                                 else totindex= 0;
1737                         }
1738                         
1739                         MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
1740                         
1741                         if(vp->flag & VP_COLINDEX) {
1742                                 for(index=0; index<totindex; index++) {
1743                                         if(indexar[index] && indexar[index]<=me->totface) {
1744                                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1745                                                 
1746                                                 if(mface->mat_nr!=ob->actcol-1) {
1747                                                         indexar[index]= 0;
1748                                                 }
1749                                         }                                       
1750                                 }
1751                         }
1752                         if((G.f & G_FACESELECT) && me->mface) {
1753                                 for(index=0; index<totindex; index++) {
1754                                         if(indexar[index] && indexar[index]<=me->totface) {
1755                                                 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1756                                                 
1757                                                 if((mface->flag & ME_FACE_SEL)==0)
1758                                                         indexar[index]= 0;
1759                                         }                                       
1760                                 }
1761                         }
1762                         
1763                         for(index=0; index<totindex; index++) {
1764                                 
1765                                 if(indexar[index] && indexar[index]<=me->totface) {
1766                                         MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1767                                         unsigned int *mcol=       ( (unsigned int *)me->mcol) + 4*(indexar[index]-1);
1768                                         unsigned int *mcolorig= ( (unsigned int *)vp->vpaint_prev) + 4*(indexar[index]-1);
1769                                         int alpha;
1770                                         
1771                                         if(vp->mode==VP_BLUR) {
1772                                                 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
1773                                                 if(mface->v4) {
1774                                                         unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
1775                                                         vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
1776                                                 }
1777                                                 else {
1778                                                         vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
1779                                                 }
1780                                                 
1781                                         }
1782                                         
1783                                         alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*mface->v1, mval);
1784                                         if(alpha) vpaint_blend(vp, mcol, mcolorig, vpd->paintcol, alpha);
1785                                         
1786                                         alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*mface->v2, mval);
1787                                         if(alpha) vpaint_blend(vp, mcol+1, mcolorig+1, vpd->paintcol, alpha);
1788                                         
1789                                         alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*mface->v3, mval);
1790                                         if(alpha) vpaint_blend(vp, mcol+2, mcolorig+2, vpd->paintcol, alpha);
1791                                         
1792                                         if(mface->v4) {
1793                                                 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*mface->v4, mval);
1794                                                 if(alpha) vpaint_blend(vp, mcol+3, mcolorig+3, vpd->paintcol, alpha);
1795                                         }
1796                                 }
1797                         }
1798                                                 
1799                         MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
1800                         
1801                         do_shared_vertexcol(me);
1802                         
1803                         ED_region_tag_redraw(vc->ar);
1804                         
1805                         DAG_object_flush_update(vc->scene, ob, OB_RECALC_DATA);
1806                 }
1807                 break;
1808         }       
1809
1810         return OPERATOR_RUNNING_MODAL;
1811 }
1812
1813 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1814 {
1815         ToolSettings *ts= CTX_data_tool_settings(C);
1816         VPaint *vp= ts->vpaint;
1817         struct VPaintData *vpd;
1818         Object *ob= CTX_data_active_object(C);
1819         Mesh *me;
1820         float mat[4][4], imat[4][4];
1821         
1822         /* context checks could be a poll() */
1823         me= get_mesh(ob);
1824         if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1825         
1826         if(me->mcol==NULL) make_vertexcol(CTX_data_scene(C), 0);
1827         if(me->mcol==NULL) return OPERATOR_CANCELLED;
1828         
1829         /* make customdata storage */
1830         op->customdata= vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
1831         view3d_set_viewcontext(C, &vpd->vc);
1832         
1833         vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
1834         vpd->indexar= get_indexarray();
1835         vpd->paintcol= vpaint_get_current_col(vp);
1836         
1837         /* for filtering */
1838         copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
1839         
1840         /* some old cruft to sort out later */
1841         Mat4MulMat4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
1842         Mat4Invert(imat, mat);
1843         Mat3CpyMat4(vpd->vpimat, imat);
1844         
1845         /* do paint once for click only paint */
1846         vpaint_modal(C, op, event);
1847         
1848         /* add modal handler */
1849         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1850         
1851         return OPERATOR_RUNNING_MODAL;
1852 }
1853
1854 void PAINT_OT_vertex_paint(wmOperatorType *ot)
1855 {
1856         /* identifiers */
1857         ot->name= "Vertex Paint";
1858         ot->idname= "PAINT_OT_vertex_paint";
1859         
1860         /* api callbacks */
1861         ot->invoke= vpaint_invoke;
1862         ot->modal= vpaint_modal;
1863         /* ot->exec= vpaint_exec; <-- needs stroke property */
1864         ot->poll= vp_poll;
1865         
1866         /* flags */
1867         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1868 }
1869