4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
39 #include "MEM_guardedalloc.h"
41 #include "IMB_imbuf.h"
42 #include "IMB_imbuf_types.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_arithb.h"
46 #include "MTC_matrixops.h"
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"
64 #include "RNA_access.h"
66 #include "BKE_armature.h"
67 #include "BKE_DerivedMesh.h"
68 #include "BKE_cloth.h"
69 #include "BKE_context.h"
70 #include "BKE_customdata.h"
71 #include "BKE_depsgraph.h"
72 #include "BKE_deform.h"
73 #include "BKE_displist.h"
74 #include "BKE_global.h"
76 #include "BKE_modifier.h"
77 #include "BKE_multires.h"
78 #include "BKE_object.h"
79 #include "BKE_utildefines.h"
85 #include "BIF_glutil.h"
88 #include "ED_object.h"
89 #include "ED_screen.h"
91 #include "ED_view3d.h"
102 #define MAXINDEX 512000
105 static void error() {}
107 /* polling - retrieve whether cursor should be set or operator should be done */
109 static int vp_poll(bContext *C)
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)
122 static int wp_poll(bContext *C)
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)
137 static void vp_drawcursor(bContext *C, int x, int y, void *customdata)
139 ToolSettings *ts= CTX_data_tool_settings(C);
141 glTranslatef((float)x, (float)y, 0.0f);
143 glColor4ub(255, 255, 255, 128);
144 glEnable( GL_LINE_SMOOTH );
146 glutil_draw_lined_arc(0.0, M_PI*2.0, ts->vpaint->size, 40);
148 glDisable( GL_LINE_SMOOTH );
150 glTranslatef((float)-x, (float)-y, 0.0f);
153 static void wp_drawcursor(bContext *C, int x, int y, void *customdata)
155 ToolSettings *ts= CTX_data_tool_settings(C);
157 glTranslatef((float)x, (float)y, 0.0f);
159 glColor4ub(200, 200, 255, 128);
160 glEnable( GL_LINE_SMOOTH );
162 glutil_draw_lined_arc(0.0, M_PI*2.0, ts->wpaint->size, 40);
164 glDisable( GL_LINE_SMOOTH );
166 glTranslatef((float)-x, (float)-y, 0.0f);
169 static void toggle_paint_cursor(bContext *C, int wpaint)
171 ToolSettings *ts = CTX_data_scene(C)->toolsettings;
172 VPaint *vp = wpaint ? ts->wpaint : ts->vpaint;
174 if(vp->paintcursor) {
175 WM_paint_cursor_end(CTX_wm_manager(C), vp->paintcursor);
176 vp->paintcursor = NULL;
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);
185 static VPaint *new_vpaint(int wpaint)
187 VPaint *vp= MEM_callocN(sizeof(VPaint), "VPaint");
194 vp->gamma= vp->mul= 1.0f;
196 vp->flag= VP_AREA+VP_SOFT+VP_SPRAY;
201 vp->flag= VP_AREA+VP_SOFT;
206 static int *get_indexarray(void)
208 return MEM_mallocN(sizeof(int)*MAXINDEX + 2, "vertexpaint");
212 /* in contradiction to cpack drawing colors, the MCOL colors (vpaint colors) are per byte!
213 so not endian sensitive. Mcol = ABGR!!! so be cautious with cpack calls */
215 unsigned int rgba_to_mcol(float r, float g, float b, float a)
222 if(ir<0) ir= 0; else if(ir>255) ir= 255;
224 if(ig<0) ig= 0; else if(ig>255) ig= 255;
226 if(ib<0) ib= 0; else if(ib>255) ib= 255;
228 if(ia<0) ia= 0; else if(ia>255) ia= 255;
240 static unsigned int vpaint_get_current_col(VPaint *vp)
242 return rgba_to_mcol(vp->r, vp->g, vp->b, 1.0f);
245 void do_shared_vertexcol(Mesh *me)
247 /* if no mcol: do not do */
248 /* if tface: only the involved faces, otherwise all */
252 short *scolmain, *scol;
255 if(me->mcol==0 || me->totvert==0 || me->totface==0) return;
257 scolmain= MEM_callocN(4*sizeof(short)*me->totvert, "colmain");
261 mcol= (char *)me->mcol;
262 for(a=me->totface; a>0; a--, mface++, mcol+=16) {
263 if((tface && tface->mode & TF_SHAREDCOL) || (G.f & G_FACESELECT)==0) {
264 scol= scolmain+4*mface->v1;
265 scol[0]++; scol[1]+= mcol[1]; scol[2]+= mcol[2]; scol[3]+= mcol[3];
266 scol= scolmain+4*mface->v2;
267 scol[0]++; scol[1]+= mcol[5]; scol[2]+= mcol[6]; scol[3]+= mcol[7];
268 scol= scolmain+4*mface->v3;
269 scol[0]++; scol[1]+= mcol[9]; scol[2]+= mcol[10]; scol[3]+= mcol[11];
271 scol= scolmain+4*mface->v4;
272 scol[0]++; scol[1]+= mcol[13]; scol[2]+= mcol[14]; scol[3]+= mcol[15];
291 mcol= (char *)me->mcol;
292 for(a=me->totface; a>0; a--, mface++, mcol+=16) {
293 if((tface && tface->mode & TF_SHAREDCOL) || (G.f & G_FACESELECT)==0) {
294 scol= scolmain+4*mface->v1;
295 mcol[1]= scol[1]; mcol[2]= scol[2]; mcol[3]= scol[3];
296 scol= scolmain+4*mface->v2;
297 mcol[5]= scol[1]; mcol[6]= scol[2]; mcol[7]= scol[3];
298 scol= scolmain+4*mface->v3;
299 mcol[9]= scol[1]; mcol[10]= scol[2]; mcol[11]= scol[3];
301 scol= scolmain+4*mface->v4;
302 mcol[13]= scol[1]; mcol[14]= scol[2]; mcol[15]= scol[3];
311 void make_vertexcol(Scene *scene, int shade) /* single ob */
317 error("Unable to perform function in Edit Mode");
322 if(!ob || ob->id.lib) return;
326 /* copies from shadedisplist to mcol */
328 CustomData_add_layer(&me->fdata, CD_MCOL, CD_CALLOC, NULL, me->totface);
329 mesh_update_customdata_pointers(me);
333 shadeMeshMCol(scene, ob, me);
335 memset(me->mcol, 255, 4*sizeof(MCol)*me->totface);
337 // XXX if (me->mr) multires_load_cols(me);
339 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
343 static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
345 if(vp->vpaint_prev) {
346 MEM_freeN(vp->vpaint_prev);
347 vp->vpaint_prev= NULL;
351 if(mcol==NULL || tot==0) return;
353 vp->vpaint_prev= MEM_mallocN(4*sizeof(int)*tot, "vpaint_prev");
354 memcpy(vp->vpaint_prev, mcol, 4*sizeof(int)*tot);
358 static void copy_wpaint_prev (VPaint *wp, MDeformVert *dverts, int dcount)
360 if (wp->wpaint_prev) {
361 free_dverts(wp->wpaint_prev, wp->tot);
362 wp->wpaint_prev= NULL;
365 if(dverts && dcount) {
367 wp->wpaint_prev = MEM_mallocN (sizeof(MDeformVert)*dcount, "wpaint prev");
369 copy_dverts (wp->wpaint_prev, dverts, dcount);
374 void clear_vpaint(Scene *scene)
378 unsigned int *to, paintcol;
381 if((G.f & G_VERTEXPAINT)==0) return;
385 if(!ob || ob->id.lib) return;
387 if(me==0 || me->mcol==0 || me->totface==0) return;
389 paintcol= vpaint_get_current_col(scene->toolsettings->vpaint);
391 to= (unsigned int *)me->mcol;
397 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
401 void clear_vpaint_selectedfaces(Scene *scene)
406 unsigned int paintcol, *mcol;
411 if(me==0 || me->totface==0) return;
414 make_vertexcol(scene, 0);
416 paintcol= vpaint_get_current_col(scene->toolsettings->vpaint);
419 mcol = (unsigned int*)me->mcol;
420 for (i = 0; i < me->totface; i++, mf++, mcol+=4) {
421 if (mf->flag & ME_FACE_SEL) {
429 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
433 /* fills in the selected faces with the current weight and vertex group */
434 void clear_wpaint_selectedfaces(Scene *scene)
436 VPaint *wp= scene->toolsettings->wpaint;
437 float paintweight= wp->weight;
441 MDeformWeight *dw, *uw;
444 unsigned int faceverts[5]={0,0,0,0,0};
446 int vgroup_mirror= -1;
450 if(me==0 || me->totface==0 || me->dvert==0 || !me->mface) return;
452 indexar= get_indexarray();
453 for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
454 if((mface->flag & ME_FACE_SEL)==0)
457 indexar[index]= index+1;
460 vgroup= ob->actdef-1;
462 /* directly copied from weight_paint, should probaby split into a seperate function */
463 /* if mirror painting, find the other group */
464 if(wp->flag & VP_MIRROR_X) {
465 bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
467 bDeformGroup *curdef;
471 BLI_strncpy(name, defgroup->name, 32);
472 bone_flip_name(name, 0); /* 0 = don't strip off number extensions */
474 for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
475 if (!strcmp(curdef->name, name))
478 int olddef= ob->actdef; /* tsk, add_defgroup sets the active defgroup */
479 curdef= add_defgroup_name (ob, name);
483 if(curdef && curdef!=defgroup)
484 vgroup_mirror= actdef;
487 /* end copy from weight_paint*/
489 copy_wpaint_prev(wp, me->dvert, me->totvert);
491 for(index=0; index<me->totface; index++) {
492 if(indexar[index] && indexar[index]<=me->totface) {
493 mface= me->mface + (indexar[index]-1);
494 /* just so we can loop through the verts */
495 faceverts[0]= mface->v1;
496 faceverts[1]= mface->v2;
497 faceverts[2]= mface->v3;
498 faceverts[3]= mface->v4;
499 for (i=0; i<3 || faceverts[i]; i++) {
500 if(!((me->dvert+faceverts[i])->flag)) {
501 dw= verify_defweight(me->dvert+faceverts[i], vgroup);
503 uw= verify_defweight(wp->wpaint_prev+faceverts[i], vgroup);
504 uw->weight= dw->weight; /* set the undo weight */
505 dw->weight= paintweight;
507 if(wp->flag & VP_MIRROR_X) { /* x mirror painting */
508 int j= mesh_get_x_mirror_vert(ob, faceverts[i]);
510 /* copy, not paint again */
511 if(vgroup_mirror != -1) {
512 dw= verify_defweight(me->dvert+j, vgroup_mirror);
513 uw= verify_defweight(wp->wpaint_prev+j, vgroup_mirror);
515 dw= verify_defweight(me->dvert+j, vgroup);
516 uw= verify_defweight(wp->wpaint_prev+j, vgroup);
518 uw->weight= dw->weight; /* set the undo weight */
519 dw->weight= paintweight;
523 (me->dvert+faceverts[i])->flag= 1;
530 while (index<me->totvert) {
531 (me->dvert+index)->flag= 0;
536 copy_wpaint_prev(wp, NULL, 0);
538 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
542 void vpaint_dogamma(Scene *scene)
544 VPaint *vp= scene->toolsettings->vpaint;
549 unsigned char *cp, gamtab[256];
551 if((G.f & G_VERTEXPAINT)==0) return;
555 if(me==0 || me->mcol==0 || me->totface==0) return;
558 for(a=0; a<256; a++) {
560 fac= ((float)a)/255.0;
561 fac= vp->mul*pow( fac, igam);
565 if(temp<=0) gamtab[a]= 0;
566 else if(temp>=255) gamtab[a]= 255;
567 else gamtab[a]= temp;
571 cp= (unsigned char *)me->mcol;
574 cp[1]= gamtab[ cp[1] ];
575 cp[2]= gamtab[ cp[2] ];
576 cp[3]= gamtab[ cp[3] ];
582 static unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
584 char *cp1, *cp2, *cp;
588 if(fac==0) return col1;
589 if(fac>=255) return col2;
598 cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
599 cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
600 cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
605 static unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
607 char *cp1, *cp2, *cp;
611 if(fac==0) return col1;
618 temp= cp1[1] + ((fac*cp2[1])/255);
619 if(temp>254) cp[1]= 255; else cp[1]= temp;
620 temp= cp1[2] + ((fac*cp2[2])/255);
621 if(temp>254) cp[2]= 255; else cp[2]= temp;
622 temp= cp1[3] + ((fac*cp2[3])/255);
623 if(temp>254) cp[3]= 255; else cp[3]= temp;
628 static unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
630 char *cp1, *cp2, *cp;
634 if(fac==0) return col1;
641 temp= cp1[1] - ((fac*cp2[1])/255);
642 if(temp<0) cp[1]= 0; else cp[1]= temp;
643 temp= cp1[2] - ((fac*cp2[2])/255);
644 if(temp<0) cp[2]= 0; else cp[2]= temp;
645 temp= cp1[3] - ((fac*cp2[3])/255);
646 if(temp<0) cp[3]= 0; else cp[3]= temp;
651 static unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac)
653 char *cp1, *cp2, *cp;
657 if(fac==0) return col1;
665 /* first mul, then blend the fac */
667 cp[1]= (mfac*cp1[1] + fac*((cp2[1]*cp1[1])/255) )/255;
668 cp[2]= (mfac*cp1[2] + fac*((cp2[2]*cp1[2])/255) )/255;
669 cp[3]= (mfac*cp1[3] + fac*((cp2[3]*cp1[3])/255) )/255;
675 static unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
677 char *cp1, *cp2, *cp;
681 if(fac==0) return col1;
682 if(fac>=255) return col2;
690 /* See if are lighter, if so mix, else dont do anything.
691 if the paint col is darker then the original, then ignore */
692 if (cp1[1]+cp1[2]+cp1[3] > cp2[1]+cp2[2]+cp2[3])
696 cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
697 cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
698 cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
703 static unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
705 char *cp1, *cp2, *cp;
709 if(fac==0) return col1;
710 if(fac>=255) return col2;
718 /* See if were darker, if so mix, else dont do anything.
719 if the paint col is brighter then the original, then ignore */
720 if (cp1[1]+cp1[2]+cp1[3] < cp2[1]+cp2[2]+cp2[3])
724 cp[1]= (mfac*cp1[1]+fac*cp2[1])/255;
725 cp[2]= (mfac*cp1[2]+fac*cp2[2])/255;
726 cp[3]= (mfac*cp1[3]+fac*cp2[3])/255;
730 static void vpaint_blend(VPaint *vp, unsigned int *col, unsigned int *colorig, unsigned int paintcol, int alpha)
733 if(vp->mode==VP_MIX || vp->mode==VP_BLUR) *col= mcol_blend( *col, paintcol, alpha);
734 else if(vp->mode==VP_ADD) *col= mcol_add( *col, paintcol, alpha);
735 else if(vp->mode==VP_SUB) *col= mcol_sub( *col, paintcol, alpha);
736 else if(vp->mode==VP_MUL) *col= mcol_mul( *col, paintcol, alpha);
737 else if(vp->mode==VP_LIGHTEN) *col= mcol_lighten( *col, paintcol, alpha);
738 else if(vp->mode==VP_DARKEN) *col= mcol_darken( *col, paintcol, alpha);
740 /* if no spray, clip color adding with colorig & orig alpha */
741 if((vp->flag & VP_SPRAY)==0) {
742 unsigned int testcol=0, a;
745 alpha= (int)(255.0*vp->a);
747 if(vp->mode==VP_MIX || vp->mode==VP_BLUR) testcol= mcol_blend( *colorig, paintcol, alpha);
748 else if(vp->mode==VP_ADD) testcol= mcol_add( *colorig, paintcol, alpha);
749 else if(vp->mode==VP_SUB) testcol= mcol_sub( *colorig, paintcol, alpha);
750 else if(vp->mode==VP_MUL) testcol= mcol_mul( *colorig, paintcol, alpha);
751 else if(vp->mode==VP_LIGHTEN) testcol= mcol_lighten( *colorig, paintcol, alpha);
752 else if(vp->mode==VP_DARKEN) testcol= mcol_darken( *colorig, paintcol, alpha);
755 ct= (char *)&testcol;
760 if( cp[a]<ct[a] ) cp[a]= ct[a];
761 else if( cp[a]>co[a] ) cp[a]= co[a];
764 if( cp[a]<co[a] ) cp[a]= co[a];
765 else if( cp[a]>ct[a] ) cp[a]= ct[a];
772 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
777 if(totface+4>=MAXINDEX) return 0;
779 if(size>64.0) size= 64.0;
781 ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
783 unsigned int *rt= ibuf->rect;
785 memset(indexar, 0, sizeof(int)*totface+4); /* plus 2! first element is total, +2 was giving valgrind errors, +4 seems ok */
787 size= ibuf->x*ibuf->y;
791 index= WM_framebuffer_to_index(*rt);
792 if(index>0 && index<=totface)
799 for(a=1; a<=totface; a++) {
800 if(indexar[a]) indexar[tot++]= a;
809 static int calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], float *vert_nor, short *mval)
815 if(vp->flag & VP_SOFT) {
816 project_short_noclip(vc->ar, vert_nor, vertco);
817 dx= mval[0]-vertco[0];
818 dy= mval[1]-vertco[1];
820 fac= sqrt(dx*dx + dy*dy);
821 if(fac > vp->size) return 0;
822 if(vp->flag & VP_HARD)
825 alpha= 255.0*vp->a*(1.0-fac/vp->size);
831 if(vp->flag & VP_NORMALS) {
832 float *no= vert_nor+3;
835 fac= vpimat[2][0]*no[0]+vpimat[2][1]*no[1]+vpimat[2][2]*no[2];
837 dx= vpimat[0][0]*no[0]+vpimat[0][1]*no[1]+vpimat[0][2]*no[2];
838 dy= vpimat[1][0]*no[0]+vpimat[1][1]*no[1]+vpimat[1][2]*no[2];
840 alpha*= fac/sqrt(dx*dx + dy*dy + fac*fac);
848 static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval)
851 if(dw==NULL || uw==NULL) return;
853 if(wp->mode==VP_MIX || wp->mode==VP_BLUR)
854 dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
855 else if(wp->mode==VP_ADD)
856 dw->weight += paintval*alpha;
857 else if(wp->mode==VP_SUB)
858 dw->weight -= paintval*alpha;
859 else if(wp->mode==VP_MUL)
860 /* first mul, then blend the fac */
861 dw->weight = ((1.0-alpha) + alpha*paintval)*dw->weight;
862 else if(wp->mode==VP_LIGHTEN) {
863 if (dw->weight < paintval)
864 dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
865 } else if(wp->mode==VP_DARKEN) {
866 if (dw->weight > paintval)
867 dw->weight = paintval*alpha + dw->weight*(1.0-alpha);
869 CLAMP(dw->weight, 0.0f, 1.0f);
871 /* if no spray, clip result with orig weight & orig alpha */
872 if((wp->flag & VP_SPRAY)==0) {
876 if(wp->mode==VP_MIX || wp->mode==VP_BLUR)
877 testw = paintval*alpha + uw->weight*(1.0-alpha);
878 else if(wp->mode==VP_ADD)
879 testw = uw->weight + paintval*alpha;
880 else if(wp->mode==VP_SUB)
881 testw = uw->weight - paintval*alpha;
882 else if(wp->mode==VP_MUL)
883 /* first mul, then blend the fac */
884 testw = ((1.0-alpha) + alpha*paintval)*uw->weight;
885 else if(wp->mode==VP_LIGHTEN) {
886 if (uw->weight < paintval)
887 testw = paintval*alpha + uw->weight*(1.0-alpha);
890 } else if(wp->mode==VP_DARKEN) {
891 if (uw->weight > paintval)
892 testw = paintval*alpha + uw->weight*(1.0-alpha);
896 CLAMP(testw, 0.0f, 1.0f);
898 if( testw<uw->weight ) {
899 if(dw->weight < testw) dw->weight= testw;
900 else if(dw->weight > uw->weight) dw->weight= uw->weight;
903 if(dw->weight > testw) dw->weight= testw;
904 else if(dw->weight < uw->weight) dw->weight= uw->weight;
910 /* ----------------------------------------------------- */
912 /* used for 3d view, on active object, assumes me->dvert exists */
914 /* samples cursor location, and gives menu with vertex groups to activate */
916 /* sets wp->weight to the closest weight value to vertex */
917 /* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
918 void sample_wpaint(Scene *scene, ARegion *ar, View3D *v3d, int mode)
921 VPaint *wp= scene->toolsettings->wpaint;
923 Mesh *me= get_mesh(ob);
925 short mval[2], sco[2];
929 // getmouseco_areawin(mval);
930 index= view3d_sample_backbuf(&vc, mval[0], mval[1]);
932 if(index && index<=me->totface) {
935 mface= ((MFace *)me->mface) + index-1;
937 if(mode==1) { /* sampe which groups are in here */
941 totgroup= BLI_countlist(&ob->defbase);
944 int *groups=MEM_callocN(totgroup*sizeof(int), "groups");
946 dv= me->dvert+mface->v1;
947 for(a=0; a<dv->totweight; a++) {
948 if (dv->dw[a].def_nr<totgroup)
949 groups[dv->dw[a].def_nr]= 1;
951 dv= me->dvert+mface->v2;
952 for(a=0; a<dv->totweight; a++) {
953 if (dv->dw[a].def_nr<totgroup)
954 groups[dv->dw[a].def_nr]= 1;
956 dv= me->dvert+mface->v3;
957 for(a=0; a<dv->totweight; a++) {
958 if (dv->dw[a].def_nr<totgroup)
959 groups[dv->dw[a].def_nr]= 1;
962 dv= me->dvert+mface->v4;
963 for(a=0; a<dv->totweight; a++) {
964 if (dv->dw[a].def_nr<totgroup)
965 groups[dv->dw[a].def_nr]= 1;
968 for(a=0; a<totgroup; a++)
969 if(groups[a]) totmenu++;
972 //notice("No Vertex Group Selected");
977 char item[40], *str= MEM_mallocN(40*totmenu+40, "menu");
979 strcpy(str, "Vertex Groups %t");
980 for(a=0, dg=ob->defbase.first; dg && a<totgroup; a++, dg= dg->next) {
982 sprintf(item, "|%s %%x%d", dg->name, a);
987 val= 0; // XXX pupmenu(str);
990 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
996 // else notice("No Vertex Groups in Object");
1001 float w1, w2, w3, w4, co[3], fac;
1003 dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
1004 if(dm->getVertCo==NULL) {
1005 //notice("Not supported yet");
1008 /* calc 3 or 4 corner weights */
1009 dm->getVertCo(dm, mface->v1, co);
1010 project_short_noclip(ar, co, sco);
1011 w1= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
1013 dm->getVertCo(dm, mface->v2, co);
1014 project_short_noclip(ar, co, sco);
1015 w2= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
1017 dm->getVertCo(dm, mface->v3, co);
1018 project_short_noclip(ar, co, sco);
1019 w3= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
1022 dm->getVertCo(dm, mface->v4, co);
1023 project_short_noclip(ar, co, sco);
1024 w4= ((mval[0]-sco[0])*(mval[0]-sco[0]) + (mval[1]-sco[1])*(mval[1]-sco[1]));
1028 fac= MIN4(w1, w2, w3, w4);
1030 dw= get_defweight(me->dvert+mface->v1, ob->actdef-1);
1031 if(dw) wp->weight= dw->weight; else wp->weight= 0.0f;
1034 dw= get_defweight(me->dvert+mface->v2, ob->actdef-1);
1035 if(dw) wp->weight= dw->weight; else wp->weight= 0.0f;
1038 dw= get_defweight(me->dvert+mface->v3, ob->actdef-1);
1039 if(dw) wp->weight= dw->weight; else wp->weight= 0.0f;
1043 dw= get_defweight(me->dvert+mface->v4, ob->actdef-1);
1044 if(dw) wp->weight= dw->weight; else wp->weight= 0.0f;
1055 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, int alpha, float paintweight, int vgroup_mirror)
1058 MDeformWeight *dw, *uw;
1059 int vgroup= ob->actdef-1;
1061 if(wp->flag & VP_ONLYVGROUP) {
1062 dw= get_defweight(me->dvert+index, vgroup);
1063 uw= get_defweight(wp->wpaint_prev+index, vgroup);
1066 dw= verify_defweight(me->dvert+index, vgroup);
1067 uw= verify_defweight(wp->wpaint_prev+index, vgroup);
1069 if(dw==NULL || uw==NULL)
1072 wpaint_blend(wp, dw, uw, (float)alpha/255.0, paintweight);
1074 if(wp->flag & VP_MIRROR_X) { /* x mirror painting */
1075 int j= mesh_get_x_mirror_vert(ob, index);
1077 /* copy, not paint again */
1078 if(vgroup_mirror != -1)
1079 uw= verify_defweight(me->dvert+j, vgroup_mirror);
1081 uw= verify_defweight(me->dvert+j, vgroup);
1083 uw->weight= dw->weight;
1089 /* *************** set wpaint operator ****************** */
1091 static int set_wpaint(bContext *C, wmOperator *op) /* toggle */
1093 Object *ob= CTX_data_active_object(C);
1094 Scene *scene= CTX_data_scene(C);
1095 VPaint *wp= scene->toolsettings->wpaint;
1099 if(ob->id.lib || me==NULL) return OPERATOR_CANCELLED;
1101 if(me && me->totface>=MAXINDEX) {
1102 error("Maximum number of faces: %d", MAXINDEX-1);
1103 G.f &= ~G_WEIGHTPAINT;
1104 return OPERATOR_CANCELLED;
1107 if(G.f & G_WEIGHTPAINT) G.f &= ~G_WEIGHTPAINT;
1108 else G.f |= G_WEIGHTPAINT;
1111 /* Weightpaint works by overriding colors in mesh,
1112 * so need to make sure we recalc on enter and
1113 * exit (exit needs doing regardless because we
1116 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1118 if(G.f & G_WEIGHTPAINT) {
1122 wp= scene->toolsettings->wpaint= new_vpaint(1);
1124 toggle_paint_cursor(C, 1);
1126 mesh_octree_table(ob, NULL, NULL, 's');
1128 /* verify if active weight group is also active bone */
1129 par= modifiers_isDeformedByArmature(ob);
1130 if(par && (par->flag & OB_POSEMODE)) {
1131 bPoseChannel *pchan;
1132 for(pchan= par->pose->chanbase.first; pchan; pchan= pchan->next)
1133 if(pchan->bone->flag & BONE_ACTIVE)
1136 vertexgroup_select_by_name(ob, pchan->name);
1141 toggle_paint_cursor(C, 1);
1143 mesh_octree_table(ob, NULL, NULL, 'e');
1146 WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1148 return OPERATOR_FINISHED;
1151 /* for switching to/from mode */
1152 static int paint_poll_test(bContext *C)
1154 if(ED_operator_view3d_active(C)==0)
1156 if(CTX_data_edit_object(C))
1158 if(CTX_data_active_object(C)==NULL)
1163 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1167 ot->name= "Weight Paint Mode";
1168 ot->idname= "PAINT_OT_weight_paint_toggle";
1171 ot->exec= set_wpaint;
1172 ot->poll= paint_poll_test;
1175 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1179 /* ************ paint radial controls *************/
1181 void paint_radial_control_invoke(wmOperator *op, VPaint *vp)
1183 int mode = RNA_int_get(op->ptr, "mode");
1184 float original_value;
1186 if(mode == WM_RADIALCONTROL_SIZE)
1187 original_value = vp->size;
1188 else if(mode == WM_RADIALCONTROL_STRENGTH)
1189 original_value = vp->a;
1191 RNA_float_set(op->ptr, "initial_value", original_value);
1194 static int paint_radial_control_exec(wmOperator *op, VPaint *vp)
1196 int mode = RNA_int_get(op->ptr, "mode");
1197 float new_value = RNA_float_get(op->ptr, "new_value");
1199 if(mode == WM_RADIALCONTROL_SIZE)
1200 vp->size = new_value;
1201 else if(mode == WM_RADIALCONTROL_STRENGTH)
1204 return OPERATOR_FINISHED;
1207 static int vpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1209 toggle_paint_cursor(C, 0);
1210 paint_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->vpaint);
1211 return WM_radial_control_invoke(C, op, event);
1214 static int vpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1216 int ret = WM_radial_control_modal(C, op, event);
1217 if(ret != OPERATOR_RUNNING_MODAL)
1218 toggle_paint_cursor(C, 0);
1222 static int vpaint_radial_control_exec(bContext *C, wmOperator *op)
1224 int ret = paint_radial_control_exec(op, CTX_data_scene(C)->toolsettings->vpaint);
1226 WM_radial_control_string(op, str, 256);
1230 static int wpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1232 toggle_paint_cursor(C, 1);
1233 paint_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->wpaint);
1234 return WM_radial_control_invoke(C, op, event);
1237 static int wpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1239 int ret = WM_radial_control_modal(C, op, event);
1240 if(ret != OPERATOR_RUNNING_MODAL)
1241 toggle_paint_cursor(C, 1);
1245 static int wpaint_radial_control_exec(bContext *C, wmOperator *op)
1247 int ret = paint_radial_control_exec(op, CTX_data_scene(C)->toolsettings->wpaint);
1249 WM_radial_control_string(op, str, 256);
1253 void PAINT_OT_weight_paint_radial_control(wmOperatorType *ot)
1255 WM_OT_radial_control_partial(ot);
1257 ot->name= "Weight Paint Radial Control";
1258 ot->idname= "PAINT_OT_weight_paint_radial_control";
1260 ot->invoke= wpaint_radial_control_invoke;
1261 ot->modal= wpaint_radial_control_modal;
1262 ot->exec= wpaint_radial_control_exec;
1266 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1269 void PAINT_OT_vertex_paint_radial_control(wmOperatorType *ot)
1271 WM_OT_radial_control_partial(ot);
1273 ot->name= "Vertex Paint Radial Control";
1274 ot->idname= "PAINT_OT_vertex_paint_radial_control";
1276 ot->invoke= vpaint_radial_control_invoke;
1277 ot->modal= vpaint_radial_control_modal;
1278 ot->exec= vpaint_radial_control_exec;
1282 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1285 /* ************ weight paint operator ********** */
1291 float *vertexcosnos;
1295 static void wpaint_exit(bContext *C, wmOperator *op)
1297 ToolSettings *ts= CTX_data_tool_settings(C);
1298 Object *ob= CTX_data_active_object(C);
1299 struct WPaintData *wpd= op->customdata;
1301 if(wpd->vertexcosnos)
1302 MEM_freeN(wpd->vertexcosnos);
1303 MEM_freeN(wpd->indexar);
1305 /* frees prev buffer */
1306 copy_wpaint_prev(ts->wpaint, NULL, 0);
1308 /* and particles too */
1309 if(ob->particlesystem.first) {
1310 ParticleSystem *psys;
1313 for(psys= ob->particlesystem.first; psys; psys= psys->next) {
1314 for(i=0; i<PSYS_TOT_VG; i++) {
1315 if(psys->vgroup[i]==ob->actdef) {
1316 psys->recalc |= PSYS_RECALC_HAIR;
1323 DAG_object_flush_update(CTX_data_scene(C), ob, OB_RECALC_DATA);
1326 op->customdata= NULL;
1330 static int wpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
1332 ToolSettings *ts= CTX_data_tool_settings(C);
1333 VPaint *wp= ts->wpaint;
1335 switch(event->type) {
1337 if(event->val==0) { /* release */
1339 return OPERATOR_FINISHED;
1341 /* pass on, first press gets painted too */
1345 struct WPaintData *wpd= op->customdata;
1346 ViewContext *vc= &wpd->vc;
1347 Object *ob= vc->obact;
1350 float paintweight= wp->weight;
1351 int *indexar= wpd->indexar;
1352 int totindex, index, alpha, totw;
1355 view3d_operator_needs_opengl(C);
1357 /* load projection matrix */
1358 wmMultMatrix(ob->obmat);
1359 wmGetSingleMatrix(mat);
1360 wmLoadMatrix(wpd->vc.rv3d->viewmat);
1362 MTC_Mat4SwapMat4(wpd->vc.rv3d->persmat, mat);
1364 mval[0]= event->x - vc->ar->winrct.xmin;
1365 mval[1]= event->y - vc->ar->winrct.ymin;
1367 /* which faces are involved */
1368 if(wp->flag & VP_AREA) {
1369 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], wp->size);
1372 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1373 if(indexar[0]) totindex= 1;
1377 if(wp->flag & VP_COLINDEX) {
1378 for(index=0; index<totindex; index++) {
1379 if(indexar[index] && indexar[index]<=me->totface) {
1380 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1382 if(mface->mat_nr!=ob->actcol-1) {
1389 if((G.f & G_FACESELECT) && me->mface) {
1390 for(index=0; index<totindex; index++) {
1391 if(indexar[index] && indexar[index]<=me->totface) {
1392 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1394 if((mface->flag & ME_FACE_SEL)==0) {
1401 /* make sure each vertex gets treated only once */
1402 /* and calculate filter weight */
1404 if(wp->mode==VP_BLUR)
1407 paintweight= wp->weight;
1409 for(index=0; index<totindex; index++) {
1410 if(indexar[index] && indexar[index]<=me->totface) {
1411 MFace *mface= me->mface + (indexar[index]-1);
1413 (me->dvert+mface->v1)->flag= 1;
1414 (me->dvert+mface->v2)->flag= 1;
1415 (me->dvert+mface->v3)->flag= 1;
1416 if(mface->v4) (me->dvert+mface->v4)->flag= 1;
1418 if(wp->mode==VP_BLUR) {
1419 MDeformWeight *dw, *(*dw_func)(MDeformVert *, int) = verify_defweight;
1421 if(wp->flag & VP_ONLYVGROUP)
1422 dw_func= get_defweight;
1424 dw= dw_func(me->dvert+mface->v1, ob->actdef-1);
1425 if(dw) {paintweight+= dw->weight; totw++;}
1426 dw= dw_func(me->dvert+mface->v2, ob->actdef-1);
1427 if(dw) {paintweight+= dw->weight; totw++;}
1428 dw= dw_func(me->dvert+mface->v3, ob->actdef-1);
1429 if(dw) {paintweight+= dw->weight; totw++;}
1431 dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
1432 if(dw) {paintweight+= dw->weight; totw++;}
1438 if(wp->mode==VP_BLUR)
1439 paintweight/= (float)totw;
1441 for(index=0; index<totindex; index++) {
1443 if(indexar[index] && indexar[index]<=me->totface) {
1444 MFace *mface= me->mface + (indexar[index]-1);
1446 if((me->dvert+mface->v1)->flag) {
1447 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval);
1449 do_weight_paint_vertex(wp, ob, mface->v1, alpha, paintweight, wpd->vgroup_mirror);
1451 (me->dvert+mface->v1)->flag= 0;
1454 if((me->dvert+mface->v2)->flag) {
1455 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval);
1457 do_weight_paint_vertex(wp, ob, mface->v2, alpha, paintweight, wpd->vgroup_mirror);
1459 (me->dvert+mface->v2)->flag= 0;
1462 if((me->dvert+mface->v3)->flag) {
1463 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval);
1465 do_weight_paint_vertex(wp, ob, mface->v3, alpha, paintweight, wpd->vgroup_mirror);
1467 (me->dvert+mface->v3)->flag= 0;
1470 if((me->dvert+mface->v4)->flag) {
1472 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval);
1474 do_weight_paint_vertex(wp, ob, mface->v4, alpha, paintweight, wpd->vgroup_mirror);
1476 (me->dvert+mface->v4)->flag= 0;
1482 MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
1484 DAG_object_flush_update(vc->scene, ob, OB_RECALC_DATA);
1485 ED_region_tag_redraw(vc->ar);
1489 return OPERATOR_RUNNING_MODAL;
1492 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1494 Scene *scene= CTX_data_scene(C);
1495 ToolSettings *ts= CTX_data_tool_settings(C);
1496 VPaint *wp= ts->wpaint;
1497 Object *ob= CTX_data_active_object(C);
1498 struct WPaintData *wpd;
1500 float mat[4][4], imat[4][4];
1502 if(scene->obedit) return OPERATOR_CANCELLED;
1503 // XXX if(multires_level1_test()) return;
1506 if(me==NULL || me->totface==0) return OPERATOR_CANCELLED;
1508 /* if nothing was added yet, we make dverts and a vertex deform group */
1510 create_dverts(&me->id);
1512 /* make customdata storage */
1513 op->customdata= wpd= MEM_callocN(sizeof(struct WPaintData), "WPaintData");
1514 view3d_set_viewcontext(C, &wpd->vc);
1515 wpd->vgroup_mirror= -1;
1517 // if(qual & LR_CTRLKEY) {
1518 // sample_wpaint(scene, ar, v3d, 0);
1521 // if(qual & LR_SHIFTKEY) {
1522 // sample_wpaint(scene, ar, v3d, 1);
1526 /* ALLOCATIONS! no return after this line */
1527 /* painting on subsurfs should give correct points too, this returns me->totvert amount */
1528 wpd->vertexcosnos= mesh_get_mapped_verts_nors(scene, ob);
1529 wpd->indexar= get_indexarray();
1530 copy_wpaint_prev(wp, me->dvert, me->totvert);
1532 /* this happens on a Bone select, when no vgroup existed yet */
1535 if((modob = modifiers_isDeformedByArmature(ob))) {
1536 bPoseChannel *pchan;
1537 for(pchan= modob->pose->chanbase.first; pchan; pchan= pchan->next)
1538 if(pchan->bone->flag & SELECT)
1541 bDeformGroup *dg= get_named_vertexgroup(ob, pchan->name);
1543 dg= add_defgroup_name(ob, pchan->name); /* sets actdef */
1545 ob->actdef= get_defgroup_num(ob, dg);
1549 if(ob->defbase.first==NULL) {
1553 // if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1555 /* imat for normals */
1556 Mat4MulMat4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1557 Mat4Invert(imat, mat);
1558 Mat3CpyMat4(wpd->wpimat, imat);
1560 /* if mirror painting, find the other group */
1561 if(wp->flag & VP_MIRROR_X) {
1562 bDeformGroup *defgroup= BLI_findlink(&ob->defbase, ob->actdef-1);
1564 bDeformGroup *curdef;
1568 BLI_strncpy(name, defgroup->name, 32);
1569 bone_flip_name(name, 0); /* 0 = don't strip off number extensions */
1571 for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
1572 if (!strcmp(curdef->name, name))
1575 int olddef= ob->actdef; /* tsk, add_defgroup sets the active defgroup */
1576 curdef= add_defgroup_name (ob, name);
1580 if(curdef && curdef!=defgroup)
1581 wpd->vgroup_mirror= actdef;
1585 /* do paint once for click only paint */
1586 wpaint_modal(C, op, event);
1588 /* add modal handler */
1589 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1591 return OPERATOR_RUNNING_MODAL;
1594 void PAINT_OT_weight_paint(wmOperatorType *ot)
1598 ot->name= "Weight Paint";
1599 ot->idname= "PAINT_OT_weight_paint";
1602 ot->invoke= wpaint_invoke;
1603 ot->modal= wpaint_modal;
1604 /* ot->exec= vpaint_exec; <-- needs stroke property */
1608 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1612 /* ************ set / clear vertex paint mode ********** */
1615 static int set_vpaint(bContext *C, wmOperator *op) /* toggle */
1617 Object *ob= CTX_data_active_object(C);
1618 Scene *scene= CTX_data_scene(C);
1619 VPaint *vp= scene->toolsettings->vpaint;
1624 if(me==NULL || object_data_is_libdata(ob)) {
1625 G.f &= ~G_VERTEXPAINT;
1626 return OPERATOR_FINISHED;
1629 if(me && me->totface>=MAXINDEX) {
1630 error("Maximum number of faces: %d", MAXINDEX-1);
1631 G.f &= ~G_VERTEXPAINT;
1632 return OPERATOR_FINISHED;
1635 if(me && me->mcol==NULL) make_vertexcol(scene, 0);
1637 /* toggle: end vpaint */
1638 if(G.f & G_VERTEXPAINT) {
1640 G.f &= ~G_VERTEXPAINT;
1643 toggle_paint_cursor(C, 0);
1644 vp->paintcursor= NULL;
1649 G.f |= G_VERTEXPAINT;
1650 /* Turn off weight painting */
1651 if (G.f & G_WEIGHTPAINT)
1655 vp= scene->toolsettings->vpaint= new_vpaint(0);
1657 toggle_paint_cursor(C, 0);
1661 /* update modifier stack for mapping requirements */
1662 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1664 WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1666 return OPERATOR_FINISHED;
1669 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
1673 ot->name= "Vertex Paint Mode";
1674 ot->idname= "PAINT_OT_vertex_paint_toggle";
1677 ot->exec= set_vpaint;
1678 ot->poll= paint_poll_test;
1681 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1687 /* ********************** vertex paint operator ******************* */
1689 /* Implementation notes:
1692 - validate context (add mcol)
1693 - create customdata storage
1694 - call paint once (mouse click)
1698 - for every mousemove, apply vertex paint
1699 - exit on mouse release, free customdata
1700 (return OPERATOR_FINISHED also removes handler and operator)
1703 - implement a stroke event (or mousemove with past positons)
1704 - revise whether op->customdata should be added in object, in set_vpaint
1710 unsigned int paintcol;
1712 float *vertexcosnos;
1716 static void vpaint_exit(bContext *C, wmOperator *op)
1718 ToolSettings *ts= CTX_data_tool_settings(C);
1719 struct VPaintData *vpd= op->customdata;
1721 if(vpd->vertexcosnos)
1722 MEM_freeN(vpd->vertexcosnos);
1723 MEM_freeN(vpd->indexar);
1725 /* frees prev buffer */
1726 copy_vpaint_prev(ts->vpaint, NULL, 0);
1729 op->customdata= NULL;
1732 static int vpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
1734 ToolSettings *ts= CTX_data_tool_settings(C);
1735 VPaint *vp= ts->vpaint;
1737 switch(event->type) {
1739 if(event->val==0) { /* release */
1741 return OPERATOR_FINISHED;
1743 /* pass on, first press gets painted too */
1747 struct VPaintData *vpd= op->customdata;
1748 ViewContext *vc= &vpd->vc;
1749 Object *ob= vc->obact;
1752 int *indexar= vpd->indexar;
1753 int totindex, index;
1756 view3d_operator_needs_opengl(C);
1758 /* load projection matrix */
1759 wmMultMatrix(ob->obmat);
1760 wmGetSingleMatrix(mat);
1761 wmLoadMatrix(vc->rv3d->viewmat);
1763 mval[0]= event->x - vc->ar->winrct.xmin;
1764 mval[1]= event->y - vc->ar->winrct.ymin;
1766 /* which faces are involved */
1767 if(vp->flag & VP_AREA) {
1768 totindex= sample_backbuf_area(vc, indexar, me->totface, mval[0], mval[1], vp->size);
1771 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1772 if(indexar[0]) totindex= 1;
1776 MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
1778 if(vp->flag & VP_COLINDEX) {
1779 for(index=0; index<totindex; index++) {
1780 if(indexar[index] && indexar[index]<=me->totface) {
1781 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1783 if(mface->mat_nr!=ob->actcol-1) {
1789 if((G.f & G_FACESELECT) && me->mface) {
1790 for(index=0; index<totindex; index++) {
1791 if(indexar[index] && indexar[index]<=me->totface) {
1792 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1794 if((mface->flag & ME_FACE_SEL)==0)
1800 for(index=0; index<totindex; index++) {
1802 if(indexar[index] && indexar[index]<=me->totface) {
1803 MFace *mface= ((MFace *)me->mface) + (indexar[index]-1);
1804 unsigned int *mcol= ( (unsigned int *)me->mcol) + 4*(indexar[index]-1);
1805 unsigned int *mcolorig= ( (unsigned int *)vp->vpaint_prev) + 4*(indexar[index]-1);
1808 if(vp->mode==VP_BLUR) {
1809 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
1811 unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
1812 vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
1815 vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
1820 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*mface->v1, mval);
1821 if(alpha) vpaint_blend(vp, mcol, mcolorig, vpd->paintcol, alpha);
1823 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*mface->v2, mval);
1824 if(alpha) vpaint_blend(vp, mcol+1, mcolorig+1, vpd->paintcol, alpha);
1826 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*mface->v3, mval);
1827 if(alpha) vpaint_blend(vp, mcol+2, mcolorig+2, vpd->paintcol, alpha);
1830 alpha= calc_vp_alpha_dl(vp, vc, vpd->vpimat, vpd->vertexcosnos+6*mface->v4, mval);
1831 if(alpha) vpaint_blend(vp, mcol+3, mcolorig+3, vpd->paintcol, alpha);
1836 MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
1838 do_shared_vertexcol(me);
1840 ED_region_tag_redraw(vc->ar);
1842 DAG_object_flush_update(vc->scene, ob, OB_RECALC_DATA);
1847 return OPERATOR_RUNNING_MODAL;
1850 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
1852 ToolSettings *ts= CTX_data_tool_settings(C);
1853 VPaint *vp= ts->vpaint;
1854 struct VPaintData *vpd;
1855 Object *ob= CTX_data_active_object(C);
1857 float mat[4][4], imat[4][4];
1859 /* context checks could be a poll() */
1861 if(me==NULL || me->totface==0) return OPERATOR_CANCELLED;
1863 if(me->mcol==NULL) make_vertexcol(CTX_data_scene(C), 0);
1864 if(me->mcol==NULL) return OPERATOR_CANCELLED;
1866 /* make customdata storage */
1867 op->customdata= vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
1868 view3d_set_viewcontext(C, &vpd->vc);
1870 vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob);
1871 vpd->indexar= get_indexarray();
1872 vpd->paintcol= vpaint_get_current_col(vp);
1875 copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
1877 /* some old cruft to sort out later */
1878 Mat4MulMat4(mat, ob->obmat, vpd->vc.rv3d->viewmat);
1879 Mat4Invert(imat, mat);
1880 Mat3CpyMat4(vpd->vpimat, imat);
1882 /* do paint once for click only paint */
1883 vpaint_modal(C, op, event);
1885 /* add modal handler */
1886 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1888 return OPERATOR_RUNNING_MODAL;
1891 void PAINT_OT_vertex_paint(wmOperatorType *ot)
1894 ot->name= "Vertex Paint";
1895 ot->idname= "PAINT_OT_vertex_paint";
1898 ot->invoke= vpaint_invoke;
1899 ot->modal= vpaint_modal;
1900 /* ot->exec= vpaint_exec; <-- needs stroke property */
1904 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;