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_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"
77 #include "BKE_modifier.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->brush->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->brush->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");
189 vp->gamma= vp->mul= 1.0f;
191 vp->flag= VP_AREA+VP_SOFT+VP_SPRAY;
194 vp->flag= VP_AREA+VP_SOFT;
199 static int *get_indexarray(void)
201 return MEM_mallocN(sizeof(int)*MAXINDEX + 2, "vertexpaint");
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 */
208 unsigned int rgba_to_mcol(float r, float g, float b, float a)
215 if(ir<0) ir= 0; else if(ir>255) ir= 255;
217 if(ig<0) ig= 0; else if(ig>255) ig= 255;
219 if(ib<0) ib= 0; else if(ib>255) ib= 255;
221 if(ia<0) ia= 0; else if(ia>255) ia= 255;
233 static unsigned int vpaint_get_current_col(VPaint *vp)
235 return rgba_to_mcol(vp->brush->rgb[0], vp->brush->rgb[1], vp->brush->rgb[2], 1.0f);
238 void do_shared_vertexcol(Mesh *me)
240 /* if no mcol: do not do */
241 /* if tface: only the involved faces, otherwise all */
245 short *scolmain, *scol;
248 if(me->mcol==0 || me->totvert==0 || me->totface==0) return;
250 scolmain= MEM_callocN(4*sizeof(short)*me->totvert, "colmain");
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];
264 scol= scolmain+4*mface->v4;
265 scol[0]++; scol[1]+= mcol[13]; scol[2]+= mcol[14]; scol[3]+= mcol[15];
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];
294 scol= scolmain+4*mface->v4;
295 mcol[13]= scol[1]; mcol[14]= scol[2]; mcol[15]= scol[3];
304 void make_vertexcol(Scene *scene, int shade) /* single ob */
310 error("Unable to perform function in Edit Mode");
315 if(!ob || ob->id.lib) return;
319 /* copies from shadedisplist to mcol */
321 CustomData_add_layer(&me->fdata, CD_MCOL, CD_CALLOC, NULL, me->totface);
322 mesh_update_customdata_pointers(me);
326 shadeMeshMCol(scene, ob, me);
328 memset(me->mcol, 255, 4*sizeof(MCol)*me->totface);
330 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
334 static void copy_vpaint_prev(VPaint *vp, unsigned int *mcol, int tot)
336 if(vp->vpaint_prev) {
337 MEM_freeN(vp->vpaint_prev);
338 vp->vpaint_prev= NULL;
342 if(mcol==NULL || tot==0) return;
344 vp->vpaint_prev= MEM_mallocN(4*sizeof(int)*tot, "vpaint_prev");
345 memcpy(vp->vpaint_prev, mcol, 4*sizeof(int)*tot);
349 static void copy_wpaint_prev (VPaint *wp, MDeformVert *dverts, int dcount)
351 if (wp->wpaint_prev) {
352 free_dverts(wp->wpaint_prev, wp->tot);
353 wp->wpaint_prev= NULL;
356 if(dverts && dcount) {
358 wp->wpaint_prev = MEM_mallocN (sizeof(MDeformVert)*dcount, "wpaint prev");
360 copy_dverts (wp->wpaint_prev, dverts, dcount);
365 void clear_vpaint(Scene *scene)
369 unsigned int *to, paintcol;
372 if((G.f & G_VERTEXPAINT)==0) return;
376 if(!ob || ob->id.lib) return;
378 if(me==0 || me->mcol==0 || me->totface==0) return;
380 paintcol= vpaint_get_current_col(scene->toolsettings->vpaint);
382 to= (unsigned int *)me->mcol;
388 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
392 void clear_vpaint_selectedfaces(Scene *scene)
397 unsigned int paintcol, *mcol;
402 if(me==0 || me->totface==0) return;
405 make_vertexcol(scene, 0);
407 paintcol= vpaint_get_current_col(scene->toolsettings->vpaint);
410 mcol = (unsigned int*)me->mcol;
411 for (i = 0; i < me->totface; i++, mf++, mcol+=4) {
412 if (mf->flag & ME_FACE_SEL) {
420 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
424 /* fills in the selected faces with the current weight and vertex group */
425 void clear_wpaint_selectedfaces(Scene *scene)
427 ToolSettings *ts= scene->toolsettings;
428 VPaint *wp= ts->wpaint;
429 float paintweight= ts->vgroup_weight;
433 MDeformWeight *dw, *uw;
436 unsigned int faceverts[5]={0,0,0,0,0};
438 int vgroup_mirror= -1;
442 if(me==0 || me->totface==0 || me->dvert==0 || !me->mface) return;
444 indexar= get_indexarray();
445 for(index=0, mface=me->mface; index<me->totface; index++, mface++) {
446 if((mface->flag & ME_FACE_SEL)==0)
449 indexar[index]= index+1;
452 vgroup= ob->actdef-1;
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);
459 bDeformGroup *curdef;
463 BLI_strncpy(name, defgroup->name, 32);
464 bone_flip_name(name, 0); /* 0 = don't strip off number extensions */
466 for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
467 if (!strcmp(curdef->name, name))
470 int olddef= ob->actdef; /* tsk, add_defgroup sets the active defgroup */
471 curdef= add_defgroup_name (ob, name);
475 if(curdef && curdef!=defgroup)
476 vgroup_mirror= actdef;
479 /* end copy from weight_paint*/
481 copy_wpaint_prev(wp, me->dvert, me->totvert);
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);
495 uw= verify_defweight(wp->wpaint_prev+faceverts[i], vgroup);
496 uw->weight= dw->weight; /* set the undo weight */
497 dw->weight= paintweight;
499 if(wp->flag & VP_MIRROR_X) { /* x mirror painting */
500 int j= mesh_get_x_mirror_vert(ob, faceverts[i]);
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);
507 dw= verify_defweight(me->dvert+j, vgroup);
508 uw= verify_defweight(wp->wpaint_prev+j, vgroup);
510 uw->weight= dw->weight; /* set the undo weight */
511 dw->weight= paintweight;
515 (me->dvert+faceverts[i])->flag= 1;
522 while (index<me->totvert) {
523 (me->dvert+index)->flag= 0;
528 copy_wpaint_prev(wp, NULL, 0);
530 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
534 void vpaint_dogamma(Scene *scene)
536 VPaint *vp= scene->toolsettings->vpaint;
541 unsigned char *cp, gamtab[256];
543 if((G.f & G_VERTEXPAINT)==0) return;
547 if(me==0 || me->mcol==0 || me->totface==0) return;
550 for(a=0; a<256; a++) {
552 fac= ((float)a)/255.0;
553 fac= vp->mul*pow( fac, igam);
557 if(temp<=0) gamtab[a]= 0;
558 else if(temp>=255) gamtab[a]= 255;
559 else gamtab[a]= temp;
563 cp= (unsigned char *)me->mcol;
566 cp[1]= gamtab[ cp[1] ];
567 cp[2]= gamtab[ cp[2] ];
568 cp[3]= gamtab[ cp[3] ];
574 static unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
576 char *cp1, *cp2, *cp;
580 if(fac==0) return col1;
581 if(fac>=255) return col2;
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;
597 static unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
599 char *cp1, *cp2, *cp;
603 if(fac==0) return col1;
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;
620 static unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
622 char *cp1, *cp2, *cp;
626 if(fac==0) return col1;
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;
643 static unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac)
645 char *cp1, *cp2, *cp;
649 if(fac==0) return col1;
657 /* first mul, then blend the fac */
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;
667 static unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
669 char *cp1, *cp2, *cp;
673 if(fac==0) return col1;
674 if(fac>=255) return col2;
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])
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;
695 static unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
697 char *cp1, *cp2, *cp;
701 if(fac==0) return col1;
702 if(fac>=255) return col2;
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])
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;
722 static void vpaint_blend(VPaint *vp, unsigned int *col, unsigned int *colorig, unsigned int paintcol, int alpha)
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);
732 /* if no spray, clip color adding with colorig & orig alpha */
733 if((vp->flag & VP_SPRAY)==0) {
734 unsigned int testcol=0, a;
737 alpha= (int)(255.0*vp->brush->alpha);
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);
747 ct= (char *)&testcol;
752 if( cp[a]<ct[a] ) cp[a]= ct[a];
753 else if( cp[a]>co[a] ) cp[a]= co[a];
756 if( cp[a]<co[a] ) cp[a]= co[a];
757 else if( cp[a]>ct[a] ) cp[a]= ct[a];
764 static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
769 if(totface+4>=MAXINDEX) return 0;
771 if(size>64.0) size= 64.0;
773 ibuf= view3d_read_backbuf(vc, x-size, y-size, x+size, y+size);
775 unsigned int *rt= ibuf->rect;
777 memset(indexar, 0, sizeof(int)*totface+4); /* plus 2! first element is total, +2 was giving valgrind errors, +4 seems ok */
779 size= ibuf->x*ibuf->y;
783 index= WM_framebuffer_to_index(*rt);
784 if(index>0 && index<=totface)
791 for(a=1; a<=totface; a++) {
792 if(indexar[a]) indexar[tot++]= a;
801 static int calc_vp_alpha_dl(VPaint *vp, ViewContext *vc, float vpimat[][3], float *vert_nor, short *mval)
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];
812 fac= sqrt(dx*dx + dy*dy);
813 if(fac > vp->brush->size) return 0;
814 if(vp->flag & VP_HARD)
817 alpha= 255.0*vp->brush->alpha*(1.0-fac/vp->brush->size);
820 alpha= 255.0*vp->brush->alpha;
823 if(vp->flag & VP_NORMALS) {
824 float *no= vert_nor+3;
827 fac= vpimat[2][0]*no[0]+vpimat[2][1]*no[1]+vpimat[2][2]*no[2];
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];
832 alpha*= fac/sqrt(dx*dx + dy*dy + fac*fac);
840 static void wpaint_blend(VPaint *wp, MDeformWeight *dw, MDeformWeight *uw, float alpha, float paintval)
843 if(dw==NULL || uw==NULL) return;
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);
861 CLAMP(dw->weight, 0.0f, 1.0f);
863 /* if no spray, clip result with orig weight & orig alpha */
864 if((wp->flag & VP_SPRAY)==0) {
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);
882 } else if(wp->mode==VP_DARKEN) {
883 if (uw->weight > paintval)
884 testw = paintval*alpha + uw->weight*(1.0-alpha);
888 CLAMP(testw, 0.0f, 1.0f);
890 if( testw<uw->weight ) {
891 if(dw->weight < testw) dw->weight= testw;
892 else if(dw->weight > uw->weight) dw->weight= uw->weight;
895 if(dw->weight > testw) dw->weight= testw;
896 else if(dw->weight < uw->weight) dw->weight= uw->weight;
902 /* ----------------------------------------------------- */
904 /* used for 3d view, on active object, assumes me->dvert exists */
906 /* samples cursor location, and gives menu with vertex groups to activate */
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)
913 ToolSettings *ts= scene->toolsettings;
915 Mesh *me= get_mesh(ob);
917 short mval[2], sco[2];
921 // getmouseco_areawin(mval);
922 index= view3d_sample_backbuf(&vc, mval[0], mval[1]);
924 if(index && index<=me->totface) {
927 mface= ((MFace *)me->mface) + index-1;
929 if(mode==1) { /* sampe which groups are in here */
933 totgroup= BLI_countlist(&ob->defbase);
936 int *groups=MEM_callocN(totgroup*sizeof(int), "groups");
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;
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;
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;
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;
960 for(a=0; a<totgroup; a++)
961 if(groups[a]) totmenu++;
964 //notice("No Vertex Group Selected");
969 char item[40], *str= MEM_mallocN(40*totmenu+40, "menu");
971 strcpy(str, "Vertex Groups %t");
972 for(a=0, dg=ob->defbase.first; dg && a<totgroup; a++, dg= dg->next) {
974 sprintf(item, "|%s %%x%d", dg->name, a);
979 val= 0; // XXX pupmenu(str);
982 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
988 // else notice("No Vertex Groups in Object");
993 float w1, w2, w3, w4, co[3], fac;
995 dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
996 if(dm->getVertCo==NULL) {
997 //notice("Not supported yet");
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]));
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]));
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]));
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]));
1020 fac= MIN4(w1, w2, w3, w4);
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;
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;
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;
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;
1047 static void do_weight_paint_vertex(VPaint *wp, Object *ob, int index, int alpha, float paintweight, int vgroup_mirror)
1050 MDeformWeight *dw, *uw;
1051 int vgroup= ob->actdef-1;
1053 if(wp->flag & VP_ONLYVGROUP) {
1054 dw= get_defweight(me->dvert+index, vgroup);
1055 uw= get_defweight(wp->wpaint_prev+index, vgroup);
1058 dw= verify_defweight(me->dvert+index, vgroup);
1059 uw= verify_defweight(wp->wpaint_prev+index, vgroup);
1061 if(dw==NULL || uw==NULL)
1064 wpaint_blend(wp, dw, uw, (float)alpha/255.0, paintweight);
1066 if(wp->flag & VP_MIRROR_X) { /* x mirror painting */
1067 int j= mesh_get_x_mirror_vert(ob, index);
1069 /* copy, not paint again */
1070 if(vgroup_mirror != -1)
1071 uw= verify_defweight(me->dvert+j, vgroup_mirror);
1073 uw= verify_defweight(me->dvert+j, vgroup);
1075 uw->weight= dw->weight;
1081 /* *************** set wpaint operator ****************** */
1083 static int set_wpaint(bContext *C, wmOperator *op) /* toggle */
1085 Object *ob= CTX_data_active_object(C);
1086 Scene *scene= CTX_data_scene(C);
1087 VPaint *wp= scene->toolsettings->wpaint;
1091 if(ob->id.lib || me==NULL) return OPERATOR_PASS_THROUGH;
1093 if(me && me->totface>=MAXINDEX) {
1094 error("Maximum number of faces: %d", MAXINDEX-1);
1095 G.f &= ~G_WEIGHTPAINT;
1096 return OPERATOR_CANCELLED;
1099 if(G.f & G_WEIGHTPAINT) G.f &= ~G_WEIGHTPAINT;
1100 else G.f |= G_WEIGHTPAINT;
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
1108 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1110 if(G.f & G_WEIGHTPAINT) {
1114 wp= scene->toolsettings->wpaint= new_vpaint(1);
1116 brush_check_exists(&wp->brush);
1118 toggle_paint_cursor(C, 1);
1120 mesh_octree_table(ob, NULL, NULL, 's');
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)
1130 vertexgroup_select_by_name(ob, pchan->name);
1135 toggle_paint_cursor(C, 1);
1137 mesh_octree_table(ob, NULL, NULL, 'e');
1140 WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1142 return OPERATOR_FINISHED;
1145 /* for switching to/from mode */
1146 static int paint_poll_test(bContext *C)
1148 if(ED_operator_view3d_active(C)==0)
1150 if(CTX_data_edit_object(C))
1152 if(CTX_data_active_object(C)==NULL)
1157 void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
1161 ot->name= "Weight Paint Mode";
1162 ot->idname= "PAINT_OT_weight_paint_toggle";
1165 ot->exec= set_wpaint;
1166 ot->poll= paint_poll_test;
1169 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1173 /* ************ paint radial controls *************/
1175 static int vpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
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);
1182 static int vpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1184 int ret = WM_radial_control_modal(C, op, event);
1185 if(ret != OPERATOR_RUNNING_MODAL)
1186 toggle_paint_cursor(C, 0);
1190 static int vpaint_radial_control_exec(bContext *C, wmOperator *op)
1192 return brush_radial_control_exec(op, CTX_data_scene(C)->toolsettings->vpaint->brush, 1);
1195 static int wpaint_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
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);
1202 static int wpaint_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1204 int ret = WM_radial_control_modal(C, op, event);
1205 if(ret != OPERATOR_RUNNING_MODAL)
1206 toggle_paint_cursor(C, 1);
1210 static int wpaint_radial_control_exec(bContext *C, wmOperator *op)
1212 return brush_radial_control_exec(op, CTX_data_scene(C)->toolsettings->wpaint->brush, 1);
1215 void PAINT_OT_weight_paint_radial_control(wmOperatorType *ot)
1217 WM_OT_radial_control_partial(ot);
1219 ot->name= "Weight Paint Radial Control";
1220 ot->idname= "PAINT_OT_weight_paint_radial_control";
1222 ot->invoke= wpaint_radial_control_invoke;
1223 ot->modal= wpaint_radial_control_modal;
1224 ot->exec= wpaint_radial_control_exec;
1228 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1231 void PAINT_OT_vertex_paint_radial_control(wmOperatorType *ot)
1233 WM_OT_radial_control_partial(ot);
1235 ot->name= "Vertex Paint Radial Control";
1236 ot->idname= "PAINT_OT_vertex_paint_radial_control";
1238 ot->invoke= vpaint_radial_control_invoke;
1239 ot->modal= vpaint_radial_control_modal;
1240 ot->exec= vpaint_radial_control_exec;
1244 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1247 /* ************ weight paint operator ********** */
1253 float *vertexcosnos;
1257 static void wpaint_exit(bContext *C, wmOperator *op)
1259 ToolSettings *ts= CTX_data_tool_settings(C);
1260 Object *ob= CTX_data_active_object(C);
1261 struct WPaintData *wpd= op->customdata;
1263 if(wpd->vertexcosnos)
1264 MEM_freeN(wpd->vertexcosnos);
1265 MEM_freeN(wpd->indexar);
1267 /* frees prev buffer */
1268 copy_wpaint_prev(ts->wpaint, NULL, 0);
1270 /* and particles too */
1271 if(ob->particlesystem.first) {
1272 ParticleSystem *psys;
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;
1285 DAG_object_flush_update(CTX_data_scene(C), ob, OB_RECALC_DATA);
1288 op->customdata= NULL;
1292 static int wpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
1294 ToolSettings *ts= CTX_data_tool_settings(C);
1295 VPaint *wp= ts->wpaint;
1297 switch(event->type) {
1299 if(event->val==0) { /* release */
1301 return OPERATOR_FINISHED;
1303 /* pass on, first press gets painted too */
1307 struct WPaintData *wpd= op->customdata;
1308 ViewContext *vc= &wpd->vc;
1309 Object *ob= vc->obact;
1312 float paintweight= ts->vgroup_weight;
1313 int *indexar= wpd->indexar;
1314 int totindex, index, alpha, totw;
1317 view3d_operator_needs_opengl(C);
1319 /* load projection matrix */
1320 wmMultMatrix(ob->obmat);
1321 wmGetSingleMatrix(mat);
1322 wmLoadMatrix(wpd->vc.rv3d->viewmat);
1324 MTC_Mat4SwapMat4(wpd->vc.rv3d->persmat, mat);
1326 mval[0]= event->x - vc->ar->winrct.xmin;
1327 mval[1]= event->y - vc->ar->winrct.ymin;
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);
1334 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1335 if(indexar[0]) totindex= 1;
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);
1344 if(mface->mat_nr!=ob->actcol-1) {
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);
1356 if((mface->flag & ME_FACE_SEL)==0) {
1363 /* make sure each vertex gets treated only once */
1364 /* and calculate filter weight */
1366 if(wp->mode==VP_BLUR)
1369 paintweight= ts->vgroup_weight;
1371 for(index=0; index<totindex; index++) {
1372 if(indexar[index] && indexar[index]<=me->totface) {
1373 MFace *mface= me->mface + (indexar[index]-1);
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;
1380 if(wp->mode==VP_BLUR) {
1381 MDeformWeight *dw, *(*dw_func)(MDeformVert *, int) = verify_defweight;
1383 if(wp->flag & VP_ONLYVGROUP)
1384 dw_func= get_defweight;
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++;}
1393 dw= dw_func(me->dvert+mface->v4, ob->actdef-1);
1394 if(dw) {paintweight+= dw->weight; totw++;}
1400 if(wp->mode==VP_BLUR)
1401 paintweight/= (float)totw;
1403 for(index=0; index<totindex; index++) {
1405 if(indexar[index] && indexar[index]<=me->totface) {
1406 MFace *mface= me->mface + (indexar[index]-1);
1408 if((me->dvert+mface->v1)->flag) {
1409 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v1, mval);
1411 do_weight_paint_vertex(wp, ob, mface->v1, alpha, paintweight, wpd->vgroup_mirror);
1413 (me->dvert+mface->v1)->flag= 0;
1416 if((me->dvert+mface->v2)->flag) {
1417 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v2, mval);
1419 do_weight_paint_vertex(wp, ob, mface->v2, alpha, paintweight, wpd->vgroup_mirror);
1421 (me->dvert+mface->v2)->flag= 0;
1424 if((me->dvert+mface->v3)->flag) {
1425 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v3, mval);
1427 do_weight_paint_vertex(wp, ob, mface->v3, alpha, paintweight, wpd->vgroup_mirror);
1429 (me->dvert+mface->v3)->flag= 0;
1432 if((me->dvert+mface->v4)->flag) {
1434 alpha= calc_vp_alpha_dl(wp, vc, wpd->wpimat, wpd->vertexcosnos+6*mface->v4, mval);
1436 do_weight_paint_vertex(wp, ob, mface->v4, alpha, paintweight, wpd->vgroup_mirror);
1438 (me->dvert+mface->v4)->flag= 0;
1444 MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
1446 DAG_object_flush_update(vc->scene, ob, OB_RECALC_DATA);
1447 ED_region_tag_redraw(vc->ar);
1451 return OPERATOR_RUNNING_MODAL;
1454 static int wpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
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;
1462 float mat[4][4], imat[4][4];
1464 if(scene->obedit) return OPERATOR_CANCELLED;
1465 // XXX if(multires_level1_test()) return;
1468 if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1470 /* if nothing was added yet, we make dverts and a vertex deform group */
1472 create_dverts(&me->id);
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;
1479 // if(qual & LR_CTRLKEY) {
1480 // sample_wpaint(scene, ar, v3d, 0);
1483 // if(qual & LR_SHIFTKEY) {
1484 // sample_wpaint(scene, ar, v3d, 1);
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);
1494 /* this happens on a Bone select, when no vgroup existed yet */
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)
1503 bDeformGroup *dg= get_named_vertexgroup(ob, pchan->name);
1505 dg= add_defgroup_name(ob, pchan->name); /* sets actdef */
1507 ob->actdef= get_defgroup_num(ob, dg);
1511 if(ob->defbase.first==NULL) {
1515 // if(ob->lay & v3d->lay); else error("Active object is not in this layer");
1517 /* imat for normals */
1518 Mat4MulMat4(mat, ob->obmat, wpd->vc.rv3d->viewmat);
1519 Mat4Invert(imat, mat);
1520 Mat3CpyMat4(wpd->wpimat, imat);
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);
1526 bDeformGroup *curdef;
1530 BLI_strncpy(name, defgroup->name, 32);
1531 bone_flip_name(name, 0); /* 0 = don't strip off number extensions */
1533 for (curdef = ob->defbase.first; curdef; curdef=curdef->next, actdef++)
1534 if (!strcmp(curdef->name, name))
1537 int olddef= ob->actdef; /* tsk, add_defgroup sets the active defgroup */
1538 curdef= add_defgroup_name (ob, name);
1542 if(curdef && curdef!=defgroup)
1543 wpd->vgroup_mirror= actdef;
1547 /* do paint once for click only paint */
1548 wpaint_modal(C, op, event);
1550 /* add modal handler */
1551 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1553 return OPERATOR_RUNNING_MODAL;
1556 void PAINT_OT_weight_paint(wmOperatorType *ot)
1560 ot->name= "Weight Paint";
1561 ot->idname= "PAINT_OT_weight_paint";
1564 ot->invoke= wpaint_invoke;
1565 ot->modal= wpaint_modal;
1566 /* ot->exec= vpaint_exec; <-- needs stroke property */
1570 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1574 /* ************ set / clear vertex paint mode ********** */
1577 static int set_vpaint(bContext *C, wmOperator *op) /* toggle */
1579 Object *ob= CTX_data_active_object(C);
1580 Scene *scene= CTX_data_scene(C);
1581 VPaint *vp= scene->toolsettings->vpaint;
1586 if(me==NULL || object_data_is_libdata(ob)) {
1587 G.f &= ~G_VERTEXPAINT;
1588 return OPERATOR_PASS_THROUGH;
1591 if(me && me->totface>=MAXINDEX) {
1592 error("Maximum number of faces: %d", MAXINDEX-1);
1593 G.f &= ~G_VERTEXPAINT;
1594 return OPERATOR_FINISHED;
1597 if(me && me->mcol==NULL) make_vertexcol(scene, 0);
1599 /* toggle: end vpaint */
1600 if(G.f & G_VERTEXPAINT) {
1602 G.f &= ~G_VERTEXPAINT;
1605 toggle_paint_cursor(C, 0);
1606 vp->paintcursor= NULL;
1611 G.f |= G_VERTEXPAINT;
1612 /* Turn off weight painting */
1613 if (G.f & G_WEIGHTPAINT)
1617 vp= scene->toolsettings->vpaint= new_vpaint(0);
1619 toggle_paint_cursor(C, 0);
1620 brush_check_exists(&scene->toolsettings->vpaint->brush);
1624 /* update modifier stack for mapping requirements */
1625 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1627 WM_event_add_notifier(C, NC_SCENE|ND_MODE, scene);
1629 return OPERATOR_FINISHED;
1632 void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
1636 ot->name= "Vertex Paint Mode";
1637 ot->idname= "PAINT_OT_vertex_paint_toggle";
1640 ot->exec= set_vpaint;
1641 ot->poll= paint_poll_test;
1644 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1650 /* ********************** vertex paint operator ******************* */
1652 /* Implementation notes:
1655 - validate context (add mcol)
1656 - create customdata storage
1657 - call paint once (mouse click)
1661 - for every mousemove, apply vertex paint
1662 - exit on mouse release, free customdata
1663 (return OPERATOR_FINISHED also removes handler and operator)
1666 - implement a stroke event (or mousemove with past positons)
1667 - revise whether op->customdata should be added in object, in set_vpaint
1673 unsigned int paintcol;
1675 float *vertexcosnos;
1679 static void vpaint_exit(bContext *C, wmOperator *op)
1681 ToolSettings *ts= CTX_data_tool_settings(C);
1682 struct VPaintData *vpd= op->customdata;
1684 if(vpd->vertexcosnos)
1685 MEM_freeN(vpd->vertexcosnos);
1686 MEM_freeN(vpd->indexar);
1688 /* frees prev buffer */
1689 copy_vpaint_prev(ts->vpaint, NULL, 0);
1692 op->customdata= NULL;
1695 static int vpaint_modal(bContext *C, wmOperator *op, wmEvent *event)
1697 ToolSettings *ts= CTX_data_tool_settings(C);
1698 VPaint *vp= ts->vpaint;
1700 switch(event->type) {
1702 if(event->val==0) { /* release */
1704 return OPERATOR_FINISHED;
1706 /* pass on, first press gets painted too */
1710 struct VPaintData *vpd= op->customdata;
1711 ViewContext *vc= &vpd->vc;
1712 Object *ob= vc->obact;
1715 int *indexar= vpd->indexar;
1716 int totindex, index;
1719 view3d_operator_needs_opengl(C);
1721 /* load projection matrix */
1722 wmMultMatrix(ob->obmat);
1723 wmGetSingleMatrix(mat);
1724 wmLoadMatrix(vc->rv3d->viewmat);
1726 mval[0]= event->x - vc->ar->winrct.xmin;
1727 mval[1]= event->y - vc->ar->winrct.ymin;
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);
1734 indexar[0]= view3d_sample_backbuf(vc, mval[0], mval[1]);
1735 if(indexar[0]) totindex= 1;
1739 MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
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);
1746 if(mface->mat_nr!=ob->actcol-1) {
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);
1757 if((mface->flag & ME_FACE_SEL)==0)
1763 for(index=0; index<totindex; index++) {
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);
1771 if(vp->mode==VP_BLUR) {
1772 unsigned int fcol1= mcol_blend( mcol[0], mcol[1], 128);
1774 unsigned int fcol2= mcol_blend( mcol[2], mcol[3], 128);
1775 vpd->paintcol= mcol_blend( fcol1, fcol2, 128);
1778 vpd->paintcol= mcol_blend( mcol[2], fcol1, 170);
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);
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);
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);
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);
1799 MTC_Mat4SwapMat4(vc->rv3d->persmat, mat);
1801 do_shared_vertexcol(me);
1803 ED_region_tag_redraw(vc->ar);
1805 DAG_object_flush_update(vc->scene, ob, OB_RECALC_DATA);
1810 return OPERATOR_RUNNING_MODAL;
1813 static int vpaint_invoke(bContext *C, wmOperator *op, wmEvent *event)
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);
1820 float mat[4][4], imat[4][4];
1822 /* context checks could be a poll() */
1824 if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH;
1826 if(me->mcol==NULL) make_vertexcol(CTX_data_scene(C), 0);
1827 if(me->mcol==NULL) return OPERATOR_CANCELLED;
1829 /* make customdata storage */
1830 op->customdata= vpd= MEM_callocN(sizeof(struct VPaintData), "VPaintData");
1831 view3d_set_viewcontext(C, &vpd->vc);
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);
1838 copy_vpaint_prev(vp, (unsigned int *)me->mcol, me->totface);
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);
1845 /* do paint once for click only paint */
1846 vpaint_modal(C, op, event);
1848 /* add modal handler */
1849 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1851 return OPERATOR_RUNNING_MODAL;
1854 void PAINT_OT_vertex_paint(wmOperatorType *ot)
1857 ot->name= "Vertex Paint";
1858 ot->idname= "PAINT_OT_vertex_paint";
1861 ot->invoke= vpaint_invoke;
1862 ot->modal= vpaint_modal;
1863 /* ot->exec= vpaint_exec; <-- needs stroke property */
1867 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;