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 *****
33 #include "MEM_guardedalloc.h"
35 #include "DNA_brush_types.h"
36 #include "DNA_color_types.h"
37 #include "DNA_image_types.h"
38 #include "DNA_texture_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_windowmanager_types.h"
42 #include "RNA_access.h"
44 #include "BLI_arithb.h"
45 #include "BLI_blenlib.h"
47 #include "BKE_brush.h"
48 #include "BKE_colortools.h"
49 #include "BKE_global.h"
50 #include "BKE_image.h"
51 #include "BKE_library.h"
53 #include "BKE_texture.h"
54 #include "BKE_utildefines.h"
56 #include "IMB_imbuf.h"
57 #include "IMB_imbuf_types.h"
59 #include "RE_render_ext.h" /* externtex */
60 #include "RE_shader_ext.h"
62 /* Datablock add/copy/free/make_local */
64 Brush *add_brush(char *name)
68 brush= alloc_libblock(&G.main->brush, ID_BR, name);
75 brush->spacing= 10.0f;
77 brush->innerradius= 0.5f;
78 brush->clone.alpha= 0.5;
79 brush->sculpt_tool = SCULPT_TOOL_DRAW;
81 brush_curve_preset(brush, BRUSH_PRESET_SHARP);
83 /* enable fake user by default */
84 brush->id.flag |= LIB_FAKEUSER;
85 brush_toggled_fake_user(brush);
90 Brush *copy_brush(Brush *brush)
96 brushn= copy_libblock(brush);
98 for(a=0; a<MAX_MTEX; a++) {
101 brushn->mtex[a]= MEM_dupallocN(mtex);
102 if(mtex->tex) id_us_plus((ID*)mtex->tex);
106 brushn->curve= curvemapping_copy(brush->curve);
108 /* enable fake user by default */
109 if (!(brushn->id.flag & LIB_FAKEUSER)) {
110 brushn->id.flag |= LIB_FAKEUSER;
111 brush_toggled_fake_user(brushn);
117 /* not brush itself */
118 void free_brush(Brush *brush)
123 for(a=0; a<MAX_MTEX; a++) {
124 mtex= brush->mtex[a];
126 if(mtex->tex) mtex->tex->id.us--;
131 curvemapping_free(brush->curve);
134 void make_local_brush(Brush *brush)
136 /* - only lib users: do nothing
137 * - only local users: set flag
143 int local= 0, lib= 0;
145 if(brush->id.lib==0) return;
147 if(brush->clone.image) {
148 /* special case: ima always local immediately */
149 brush->clone.image->id.lib= 0;
150 brush->clone.image->id.flag= LIB_LOCAL;
151 new_id(0, (ID *)brush->clone.image, 0);
154 for(scene= G.main->scene.first; scene; scene=scene->id.next)
155 if(scene->toolsettings->imapaint.brush==brush) {
156 if(scene->id.lib) lib= 1;
160 if(local && lib==0) {
162 brush->id.flag= LIB_LOCAL;
163 new_id(0, (ID *)brush, 0);
165 /* enable fake user by default */
166 if (!(brush->id.flag & LIB_FAKEUSER)) {
167 brush->id.flag |= LIB_FAKEUSER;
168 brush_toggled_fake_user(brush);
171 else if(local && lib) {
172 brushn= copy_brush(brush);
173 brushn->id.us= 1; /* only keep fake user */
174 brushn->id.flag |= LIB_FAKEUSER;
176 for(scene= G.main->scene.first; scene; scene=scene->id.next)
177 if(scene->toolsettings->imapaint.brush==brush)
178 if(scene->id.lib==0) {
179 scene->toolsettings->imapaint.brush= brushn;
186 /* Library Operations */
188 int brush_set_nr(Brush **current_brush, int nr)
192 id= (ID*)(*current_brush);
193 idtest= (ID*)BLI_findlink(&G.main->brush, nr-1);
195 if(idtest==0) { /* new brush */
196 if(id) idtest= (ID *)copy_brush((Brush *)id);
197 else idtest= (ID *)add_brush("Brush");
201 brush_delete(current_brush);
202 *current_brush= (Brush *)idtest;
211 int brush_delete(Brush **current_brush)
213 if (*current_brush) {
214 (*current_brush)->id.us--;
215 *current_brush= NULL;
223 void brush_toggled_fake_user(Brush *brush)
227 if(id->flag & LIB_FAKEUSER) {
235 void brush_curve_preset(Brush *b, BrushCurvePreset preset)
240 b->curve = curvemapping_add(1, 0, 0, 1, 1);
245 MEM_freeN(cm->curve);
247 if(preset == BRUSH_PRESET_SHARP)
249 if(preset == BRUSH_PRESET_SMOOTH)
251 if(preset == BRUSH_PRESET_MAX)
255 cm->curve= MEM_callocN(cm->totpoint*sizeof(CurveMapPoint), "curve points");
256 cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
258 if(preset == BRUSH_PRESET_SHARP) {
261 cm->curve[1].x= 0.33;
262 cm->curve[1].y= 0.33;
266 else if(preset == BRUSH_PRESET_SMOOTH) {
270 cm->curve[1].y= 0.97553;
272 cm->curve[2].y= 0.79389;
274 cm->curve[3].y= 0.02447;
276 cm->curve[4].y= 0.20611;
280 else if(preset == BRUSH_PRESET_MAX) {
287 curvemapping_changed(b->curve, 0);
290 int brush_texture_set_nr(Brush *brush, int nr)
292 ID *idtest, *id=NULL;
294 if(brush->mtex[brush->texact])
295 id= (ID *)brush->mtex[brush->texact]->tex;
297 idtest= (ID*)BLI_findlink(&G.main->tex, nr-1);
298 if(idtest==0) { /* new tex */
299 if(id) idtest= (ID *)copy_texture((Tex *)id);
300 else idtest= (ID *)add_texture("Tex");
304 brush_texture_delete(brush);
306 if(brush->mtex[brush->texact]==NULL) {
307 brush->mtex[brush->texact]= add_mtex();
308 brush->mtex[brush->texact]->r = 1.0f;
309 brush->mtex[brush->texact]->g = 1.0f;
310 brush->mtex[brush->texact]->b = 1.0f;
312 brush->mtex[brush->texact]->tex= (Tex*)idtest;
321 int brush_texture_delete(Brush *brush)
323 if(brush->mtex[brush->texact]) {
324 if(brush->mtex[brush->texact]->tex)
325 brush->mtex[brush->texact]->tex->id.us--;
326 MEM_freeN(brush->mtex[brush->texact]);
327 brush->mtex[brush->texact]= NULL;
335 int brush_clone_image_set_nr(Brush *brush, int nr)
337 if(brush && nr > 0) {
338 Image *ima= (Image*)BLI_findlink(&G.main->image, nr-1);
341 brush_clone_image_delete(brush);
342 brush->clone.image= ima;
343 id_us_plus(&ima->id);
344 brush->clone.offset[0]= brush->clone.offset[1]= 0.0f;
353 int brush_clone_image_delete(Brush *brush)
355 if (brush && brush->clone.image) {
356 brush->clone.image->id.us--;
357 brush->clone.image= NULL;
364 void brush_check_exists(Brush **brush)
367 brush_set_nr(brush, 1);
372 /*static float taylor_approx_cos(float f)
375 f = 1.0f - f/2.0f + f*f/24.0f;
379 float brush_sample_falloff(Brush *brush, float dist)
381 float a, outer, inner;
383 outer = brush->size >> 1;
384 inner = outer*brush->innerradius;
389 else if ((dist < outer) && (inner < outer)) {
390 a = sqrt((dist - inner)/(outer - inner));
391 return (1 - a)*brush->alpha;
393 /* formula used by sculpt, with taylor approx
394 a = 0.5f*(taylor_approx_cos(3.0f*(dist - inner)/(outer - inner)) + 1.0f);
395 return a*brush->alpha; */
401 float brush_sample_falloff_noalpha(Brush *brush, float dist)
405 outer = brush->size >> 1;
406 inner = outer*brush->innerradius;
411 else if ((dist < outer) && (inner < outer)) {
412 return 1.0f - sqrt((dist - inner)/(outer - inner));
418 void brush_sample_tex(Brush *brush, float *xy, float *rgba)
420 MTex *mtex= brush->mtex[brush->texact];
422 if (mtex && mtex->tex) {
423 float co[3], tin, tr, tg, tb, ta;
426 co[0]= xy[0]/(brush->size >> 1);
427 co[1]= xy[1]/(brush->size >> 1);
430 hasrgb= externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
446 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
450 void brush_imbuf_new(Brush *brush, short flt, short texfall, int size, ImBuf **outbuf)
453 float xy[2], dist, rgba[4], *dstf;
454 int x, y, rowbytes, xoff, yoff, imbflag;
457 imbflag= (flt)? IB_rectfloat: IB_rect;
458 xoff = -size/2.0f + 0.5f;
459 yoff = -size/2.0f + 0.5f;
465 ibuf= IMB_allocImBuf(size, size, 32, imbflag, 0);
468 for (y=0; y < ibuf->y; y++) {
469 dstf = ibuf->rect_float + y*rowbytes;
471 for (x=0; x < ibuf->x; x++, dstf+=4) {
476 dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
478 VECCOPY(dstf, brush->rgb);
479 dstf[3]= brush_sample_falloff(brush, dist);
481 else if (texfall == 1) {
482 brush_sample_tex(brush, xy, dstf);
485 dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
487 brush_sample_tex(brush, xy, rgba);
489 dstf[0] = rgba[0]*brush->rgb[0];
490 dstf[1] = rgba[1]*brush->rgb[1];
491 dstf[2] = rgba[2]*brush->rgb[2];
492 dstf[3] = rgba[3]*brush_sample_falloff(brush, dist);
498 crgb[0]= FTOCHAR(brush->rgb[0]);
499 crgb[1]= FTOCHAR(brush->rgb[1]);
500 crgb[2]= FTOCHAR(brush->rgb[2]);
502 for (y=0; y < ibuf->y; y++) {
503 dst = (char*)ibuf->rect + y*rowbytes;
505 for (x=0; x < ibuf->x; x++, dst+=4) {
510 dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
515 dst[3]= FTOCHAR(brush_sample_falloff(brush, dist));
517 else if (texfall == 1) {
518 brush_sample_tex(brush, xy, rgba);
519 dst[0]= FTOCHAR(rgba[0]);
520 dst[1]= FTOCHAR(rgba[1]);
521 dst[2]= FTOCHAR(rgba[2]);
522 dst[3]= FTOCHAR(rgba[3]);
525 dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
527 brush_sample_tex(brush, xy, rgba);
528 dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
529 dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
530 dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
531 dst[3] = FTOCHAR(rgba[3]*brush_sample_falloff(brush, dist));
542 typedef struct BrushPainterCache {
545 int size; /* size override, if 0 uses brush->size */
546 short flt; /* need float imbuf? */
547 short texonly; /* no alpha, color or fallof, only texture in imbuf */
551 float lastinnerradius;
558 struct BrushPainter {
561 float lastmousepos[2]; /* mouse position of last paint call */
563 float accumdistance; /* accumulated distance of brush since last paint op */
564 float lastpaintpos[2]; /* position of last paint op */
565 float startpaintpos[2]; /* position of first paint */
567 double accumtime; /* accumulated time since last paint op (airbrush) */
568 double lasttime; /* time of last update */
572 short firsttouch; /* first paint op */
576 float startinnerradius;
579 BrushPainterCache cache;
582 BrushPainter *brush_painter_new(Brush *brush)
584 BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
586 painter->brush= brush;
587 painter->firsttouch= 1;
588 painter->cache.lastsize= -1; /* force ibuf create in refresh */
590 painter->startsize = brush->size;
591 painter->startalpha = brush->alpha;
592 painter->startinnerradius = brush->innerradius;
593 painter->startspacing = brush->spacing;
598 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
600 if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
601 ((painter->cache.texonly != texonly) && texonly)) {
602 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
603 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
604 painter->cache.ibuf= painter->cache.maskibuf= NULL;
605 painter->cache.lastsize= -1; /* force ibuf create in refresh */
608 if (painter->cache.flt != flt) {
609 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
610 painter->cache.texibuf= NULL;
611 painter->cache.lastsize= -1; /* force ibuf create in refresh */
614 painter->cache.size= size;
615 painter->cache.flt= flt;
616 painter->cache.texonly= texonly;
617 painter->cache.enabled= 1;
620 void brush_painter_free(BrushPainter *painter)
622 Brush *brush = painter->brush;
624 brush->size = painter->startsize;
625 brush->alpha = painter->startalpha;
626 brush->innerradius = painter->startinnerradius;
627 brush->spacing = painter->startspacing;
629 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
630 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
631 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
635 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
637 Brush *brush= painter->brush;
638 ImBuf *ibuf, *maskibuf, *texibuf;
639 float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
640 char *b, *m, *t, *ot= NULL;
641 int dotexold, origx= x, origy= y;
643 xoff = -brush->size/2.0f + 0.5f;
644 yoff = -brush->size/2.0f + 0.5f;
645 xoff += (int)pos[0] - (int)painter->startpaintpos[0];
646 yoff += (int)pos[1] - (int)painter->startpaintpos[1];
648 ibuf = painter->cache.ibuf;
649 texibuf = painter->cache.texibuf;
650 maskibuf = painter->cache.maskibuf;
652 dotexold = (oldtexibuf != NULL);
654 if (painter->cache.flt) {
656 bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
657 tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
658 mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
661 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
663 for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
673 brush_sample_tex(brush, xy, tf);
685 b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
686 t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
687 m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
690 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
692 for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
704 brush_sample_tex(brush, xy, rgba);
705 t[0]= FTOCHAR(rgba[0]);
706 t[1]= FTOCHAR(rgba[1]);
707 t[2]= FTOCHAR(rgba[2]);
708 t[3]= FTOCHAR(rgba[3]);
711 b[0] = t[0]*m[0]/255;
712 b[1] = t[1]*m[1]/255;
713 b[2] = t[2]*m[2]/255;
714 b[3] = t[3]*m[3]/255;
720 void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
722 Brush *brush= painter->brush;
723 BrushPainterCache *cache= &painter->cache;
724 ImBuf *oldtexibuf, *ibuf;
725 int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
727 imbflag= (cache->flt)? IB_rectfloat: IB_rect;
729 cache->ibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
732 oldtexibuf= cache->texibuf;
733 cache->texibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
737 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
738 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
742 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
755 /* blend existing texture in new position */
756 if ((x1 < x2) && (y1 < y2))
757 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
760 IMB_freeImBuf(oldtexibuf);
762 /* sample texture in new areas */
763 if ((0 < x1) && (0 < ibuf->y))
764 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
765 if ((x2 < ibuf->x) && (0 < ibuf->y))
766 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
767 if ((x1 < x2) && (0 < y1))
768 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
769 if ((x1 < x2) && (y2 < ibuf->y))
770 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
773 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
775 Brush *brush= painter->brush;
776 BrushPainterCache *cache= &painter->cache;
777 MTex *mtex= brush->mtex[brush->texact];
781 if ((brush->size != cache->lastsize) || (brush->alpha != cache->lastalpha)
782 || (brush->innerradius != cache->lastinnerradius)) {
784 IMB_freeImBuf(cache->ibuf);
787 if (cache->maskibuf) {
788 IMB_freeImBuf(cache->maskibuf);
789 cache->maskibuf= NULL;
793 size= (cache->size)? cache->size: brush->size;
795 if (!(mtex && mtex->tex) || (mtex->tex->type==0)) {
796 brush_imbuf_new(brush, flt, 0, size, &cache->ibuf);
798 else if (brush->flag & BRUSH_FIXED_TEX) {
799 brush_imbuf_new(brush, flt, 0, size, &cache->maskibuf);
800 brush_painter_fixed_tex_partial_update(painter, pos);
803 brush_imbuf_new(brush, flt, 2, size, &cache->ibuf);
805 cache->lastsize= brush->size;
806 cache->lastalpha= brush->alpha;
807 cache->lastinnerradius= brush->innerradius;
809 else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
810 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
811 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
813 if ((dx != 0) || (dy != 0))
814 brush_painter_fixed_tex_partial_update(painter, pos);
818 void brush_painter_break_stroke(BrushPainter *painter)
820 painter->firsttouch= 1;
823 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
825 if (brush->flag & BRUSH_ALPHA_PRESSURE)
826 brush->alpha = MAX2(0.0, painter->startalpha*pressure);
827 if (brush->flag & BRUSH_SIZE_PRESSURE)
828 brush->size = MAX2(1.0, painter->startsize*pressure);
829 if (brush->flag & BRUSH_RAD_PRESSURE)
830 brush->innerradius = MAX2(0.0, painter->startinnerradius*pressure);
831 if (brush->flag & BRUSH_SPACING_PRESSURE)
832 brush->spacing = MAX2(1.0, painter->startspacing*(1.5f-pressure));
835 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
837 Brush *brush= painter->brush;
840 if (pressure == 0.0f)
841 pressure = 1.0f; /* zero pressure == not using tablet */
843 if (painter->firsttouch) {
844 /* paint exactly once on first touch */
845 painter->startpaintpos[0]= pos[0];
846 painter->startpaintpos[1]= pos[1];
848 brush_apply_pressure(painter, brush, pressure);
849 if (painter->cache.enabled)
850 brush_painter_refresh_cache(painter, pos);
851 totpaintops += func(user, painter->cache.ibuf, pos, pos);
853 painter->lasttime= time;
854 painter->firsttouch= 0;
855 painter->lastpaintpos[0]= pos[0];
856 painter->lastpaintpos[1]= pos[1];
859 else if (painter->brush->flag & BRUSH_AIRBRUSH) {
860 float spacing, step, paintpos[2], dmousepos[2], len;
861 double starttime, curtime= time;
863 /* compute brush spacing adapted to brush size */
864 spacing= brush->rate; //brush->size*brush->spacing*0.01f;
866 /* setup starting time, direction vector and accumulated time */
867 starttime= painter->accumtime;
868 Vec2Subf(dmousepos, pos, painter->lastmousepos);
869 len= Normalize2(dmousepos);
870 painter->accumtime += curtime - painter->lasttime;
872 /* do paint op over unpainted time distance */
873 while (painter->accumtime >= spacing) {
874 step= (spacing - starttime)*len;
875 paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
876 paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
878 if (painter->cache.enabled)
879 brush_painter_refresh_cache(painter);
880 totpaintops += func(user, painter->cache.ibuf,
881 painter->lastpaintpos, paintpos);
883 painter->lastpaintpos[0]= paintpos[0];
884 painter->lastpaintpos[1]= paintpos[1];
885 painter->accumtime -= spacing;
886 starttime -= spacing;
889 painter->lasttime= curtime;
893 float startdistance, spacing, step, paintpos[2], dmousepos[2];
896 /* compute brush spacing adapted to brush size, spacing may depend
897 on pressure, so update it */
898 brush_apply_pressure(painter, brush, painter->lastpressure);
899 spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
901 /* setup starting distance, direction vector and accumulated distance */
902 startdistance= painter->accumdistance;
903 Vec2Subf(dmousepos, pos, painter->lastmousepos);
904 len= Normalize2(dmousepos);
905 painter->accumdistance += len;
907 /* do paint op over unpainted distance */
908 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
909 step= spacing - startdistance;
910 paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
911 paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
914 press= (1.0f-t)*painter->lastpressure + t*pressure;
915 brush_apply_pressure(painter, brush, press);
916 spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
918 if (painter->cache.enabled)
919 brush_painter_refresh_cache(painter, paintpos);
922 func(user, painter->cache.ibuf, painter->lastpaintpos, paintpos);
924 painter->lastpaintpos[0]= paintpos[0];
925 painter->lastpaintpos[1]= paintpos[1];
926 painter->accumdistance -= spacing;
927 startdistance -= spacing;
930 /* do airbrush paint ops, based on the number of paint ops left over
931 from regular painting. this is a temporary solution until we have
932 accurate time stamps for mouse move events */
933 if (brush->flag & BRUSH_AIRBRUSH) {
934 double curtime= time;
935 double painttime= brush->rate*totpaintops;
937 painter->accumtime += curtime - painter->lasttime;
938 if (painter->accumtime <= painttime)
939 painter->accumtime= 0.0;
941 painter->accumtime -= painttime;
943 while (painter->accumtime >= brush->rate) {
944 brush_apply_pressure(painter, brush, pressure);
945 if (painter->cache.enabled)
946 brush_painter_refresh_cache(painter, paintpos);
948 func(user, painter->cache.ibuf, painter->lastmousepos, pos);
949 painter->accumtime -= brush->rate;
952 painter->lasttime= curtime;
956 painter->lastmousepos[0]= pos[0];
957 painter->lastmousepos[1]= pos[1];
958 painter->lastpressure= pressure;
960 brush->alpha = painter->startalpha;
961 brush->size = painter->startsize;
962 brush->innerradius = painter->startinnerradius;
963 brush->spacing = painter->startspacing;
968 /* Uses the brush curve control to find a strength value between 0 and 1 */
969 float brush_curve_strength(Brush *br, float p, const float len)
972 return curvemapping_evaluateF(br->curve, 0, p/len);
975 /* TODO: should probably be unified with BrushPainter stuff? */
976 unsigned int *brush_gen_texture_cache(Brush *br, int half_side)
978 unsigned int *texcache = NULL;
979 MTex *mtex = br->mtex[br->texact];
982 int side = half_side * 2;
984 memset(&texres, 0, sizeof(TexResult));
986 if(mtex && mtex->tex) {
987 float x, y, step = 2.0 / side, co[3];
989 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
991 BKE_image_get_ibuf(mtex->tex->ima, NULL);
993 /*do normalized cannonical view coords for texture*/
994 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
995 for (x=-1.0, ix=0; ix<side; ix++, x += step) {
1000 /* This is copied from displace modifier code */
1001 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 1, &texres);
1003 /* if the texture gave an RGB value, we assume it didn't give a valid
1004 * intensity, so calculate one (formula from do_material_tex).
1005 * if the texture didn't give an RGB value, copy the intensity across
1007 if(hasrgb & TEX_RGB)
1008 texres.tin = (0.35 * texres.tr + 0.45 *
1009 texres.tg + 0.2 * texres.tb);
1011 texres.tin = texres.tin * 255.0;
1012 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
1013 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
1014 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
1015 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
1023 /**** Radial Control ****/
1024 static struct ImBuf *brush_gen_radial_control_imbuf(Brush *br)
1026 ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1027 unsigned int *texcache;
1029 int half = side / 2;
1032 texcache = brush_gen_texture_cache(br, half);
1033 im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1034 im->x = im->y = side;
1036 for(i=0; i<side; ++i) {
1037 for(j=0; j<side; ++j) {
1038 float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
1039 im->rect_float[i*side + j]= brush_curve_strength(br, magn, half);
1043 /* Modulate curve with texture */
1045 for(i=0; i<side; ++i) {
1046 for(j=0; j<side; ++j) {
1047 const int col= texcache[i*side+j];
1048 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
1052 MEM_freeN(texcache);
1058 void brush_radial_control_invoke(wmOperator *op, Brush *br, float size_weight)
1060 int mode = RNA_int_get(op->ptr, "mode");
1061 float original_value= 0;
1063 if(mode == WM_RADIALCONTROL_SIZE)
1064 original_value = br->size * size_weight;
1065 else if(mode == WM_RADIALCONTROL_STRENGTH)
1066 original_value = br->alpha;
1067 else if(mode == WM_RADIALCONTROL_ANGLE)
1068 original_value = br->rot;
1070 RNA_float_set(op->ptr, "initial_value", original_value);
1071 op->customdata = brush_gen_radial_control_imbuf(br);
1074 int brush_radial_control_exec(wmOperator *op, Brush *br, float size_weight)
1076 int mode = RNA_int_get(op->ptr, "mode");
1077 float new_value = RNA_float_get(op->ptr, "new_value");
1078 const float conv = 0.017453293;
1080 if(mode == WM_RADIALCONTROL_SIZE)
1081 br->size = new_value * size_weight;
1082 else if(mode == WM_RADIALCONTROL_STRENGTH)
1083 br->alpha = new_value;
1084 else if(mode == WM_RADIALCONTROL_ANGLE)
1085 br->rot = new_value * conv;
1087 return OPERATOR_FINISHED;