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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_scene_types.h"
38 #include "DNA_windowmanager_types.h"
42 #include "RNA_access.h"
45 #include "BLI_blenlib.h"
48 #include "BKE_brush.h"
49 #include "BKE_colortools.h"
50 #include "BKE_global.h"
51 #include "BKE_image.h"
52 #include "BKE_library.h"
54 #include "BKE_paint.h"
55 #include "BKE_texture.h"
59 #include "IMB_imbuf.h"
60 #include "IMB_imbuf_types.h"
62 #include "RE_render_ext.h" /* externtex */
63 #include "RE_shader_ext.h"
65 /* Datablock add/copy/free/make_local */
67 Brush *add_brush(const char *name)
71 brush= alloc_libblock(&G.main->brush, ID_BR, name);
79 brush->smooth_stroke_radius= 75;
80 brush->smooth_stroke_factor= 0.9;
83 brush->clone.alpha= 0.5;
84 brush->sculpt_tool = SCULPT_TOOL_DRAW;
85 brush->flag |= BRUSH_SPACE;
87 brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
89 default_mtex(&brush->mtex);
91 /* enable fake user by default */
92 brush->id.flag |= LIB_FAKEUSER;
93 brush_toggled_fake_user(brush);
98 Brush *copy_brush(Brush *brush)
102 brushn= copy_libblock(brush);
104 if(brush->mtex.tex) id_us_plus((ID*)brush->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)
120 if(brush->mtex.tex) brush->mtex.tex->id.us--;
122 curvemapping_free(brush->curve);
125 void make_local_brush(Brush *brush)
127 /* - only lib users: do nothing
128 * - only local users: set flag
134 int local= 0, lib= 0;
136 if(brush->id.lib==0) return;
138 if(brush->clone.image) {
139 /* special case: ima always local immediately */
140 brush->clone.image->id.lib= 0;
141 brush->clone.image->id.flag= LIB_LOCAL;
142 new_id(0, (ID *)brush->clone.image, 0);
145 for(scene= G.main->scene.first; scene; scene=scene->id.next)
146 if(paint_brush(&scene->toolsettings->imapaint.paint)==brush) {
147 if(scene->id.lib) lib= 1;
151 if(local && lib==0) {
153 brush->id.flag= LIB_LOCAL;
154 new_id(0, (ID *)brush, 0);
156 /* enable fake user by default */
157 if (!(brush->id.flag & LIB_FAKEUSER)) {
158 brush->id.flag |= LIB_FAKEUSER;
159 brush_toggled_fake_user(brush);
162 else if(local && lib) {
163 brushn= copy_brush(brush);
164 brushn->id.us= 1; /* only keep fake user */
165 brushn->id.flag |= LIB_FAKEUSER;
167 for(scene= G.main->scene.first; scene; scene=scene->id.next)
168 if(paint_brush(&scene->toolsettings->imapaint.paint)==brush)
169 if(scene->id.lib==0) {
170 paint_brush_set(&scene->toolsettings->imapaint.paint, brushn);
177 /* Library Operations */
179 int brush_set_nr(Brush **current_brush, int nr, const char *name)
183 id= (ID*)(*current_brush);
184 idtest= (ID*)BLI_findlink(&G.main->brush, nr-1);
186 if(idtest==0) { /* new brush */
187 if(id) idtest= (ID *)copy_brush((Brush *)id);
188 else idtest= (ID *)add_brush(name);
192 brush_delete(current_brush);
193 *current_brush= (Brush *)idtest;
202 int brush_delete(Brush **current_brush)
204 if (*current_brush) {
205 (*current_brush)->id.us--;
206 *current_brush= NULL;
214 void brush_toggled_fake_user(Brush *brush)
218 if(id->flag & LIB_FAKEUSER) {
226 void brush_curve_preset(Brush *b, /*CurveMappingPreset*/int preset)
231 b->curve = curvemapping_add(1, 0, 0, 1, 1);
234 cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
236 b->curve->preset = preset;
237 curvemap_reset(cm, &b->curve->clipr, b->curve->preset);
238 curvemapping_changed(b->curve, 0);
241 static MTex *brush_active_texture(Brush *brush)
248 int brush_texture_set_nr(Brush *brush, int nr)
250 ID *idtest, *id=NULL;
252 id= (ID *)brush->mtex.tex;
254 idtest= (ID*)BLI_findlink(&G.main->tex, nr-1);
255 if(idtest==0) { /* new tex */
256 if(id) idtest= (ID *)copy_texture((Tex *)id);
257 else idtest= (ID *)add_texture("Tex");
261 brush_texture_delete(brush);
263 brush->mtex.tex= (Tex*)idtest;
272 int brush_texture_delete(Brush *brush)
275 brush->mtex.tex->id.us--;
280 int brush_clone_image_set_nr(Brush *brush, int nr)
282 if(brush && nr > 0) {
283 Image *ima= (Image*)BLI_findlink(&G.main->image, nr-1);
286 brush_clone_image_delete(brush);
287 brush->clone.image= ima;
288 id_us_plus(&ima->id);
289 brush->clone.offset[0]= brush->clone.offset[1]= 0.0f;
298 int brush_clone_image_delete(Brush *brush)
300 if (brush && brush->clone.image) {
301 brush->clone.image->id.us--;
302 brush->clone.image= NULL;
309 void brush_check_exists(Brush **brush, const char *name)
312 brush_set_nr(brush, 1, name);
316 void brush_sample_tex(Brush *brush, float *xy, float *rgba)
318 MTex *mtex= &brush->mtex;
320 if (mtex && mtex->tex) {
321 float co[3], tin, tr, tg, tb, ta;
324 co[0]= xy[0]/(brush->size >> 1);
325 co[1]= xy[1]/(brush->size >> 1);
328 hasrgb= externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
344 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
348 void brush_imbuf_new(Brush *brush, short flt, short texfall, int size, ImBuf **outbuf)
351 float xy[2], dist, rgba[4], *dstf;
352 int x, y, rowbytes, xoff, yoff, imbflag;
353 int maxsize = brush->size >> 1;
356 imbflag= (flt)? IB_rectfloat: IB_rect;
357 xoff = -size/2.0f + 0.5f;
358 yoff = -size/2.0f + 0.5f;
364 ibuf= IMB_allocImBuf(size, size, 32, imbflag, 0);
367 for (y=0; y < ibuf->y; y++) {
368 dstf = ibuf->rect_float + y*rowbytes;
370 for (x=0; x < ibuf->x; x++, dstf+=4) {
375 dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
377 VECCOPY(dstf, brush->rgb);
378 dstf[3]= brush->alpha*brush_curve_strength_clamp(brush, dist, maxsize);
380 else if (texfall == 1) {
381 brush_sample_tex(brush, xy, dstf);
384 dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
386 brush_sample_tex(brush, xy, rgba);
388 dstf[0] = rgba[0]*brush->rgb[0];
389 dstf[1] = rgba[1]*brush->rgb[1];
390 dstf[2] = rgba[2]*brush->rgb[2];
391 dstf[3] = rgba[3]*brush->alpha*brush_curve_strength_clamp(brush, dist, maxsize);
397 crgb[0]= FTOCHAR(brush->rgb[0]);
398 crgb[1]= FTOCHAR(brush->rgb[1]);
399 crgb[2]= FTOCHAR(brush->rgb[2]);
401 for (y=0; y < ibuf->y; y++) {
402 dst = (char*)ibuf->rect + y*rowbytes;
404 for (x=0; x < ibuf->x; x++, dst+=4) {
409 dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
414 dst[3]= FTOCHAR(brush->alpha*brush_curve_strength(brush, dist, maxsize));
416 else if (texfall == 1) {
417 brush_sample_tex(brush, xy, rgba);
418 dst[0]= FTOCHAR(rgba[0]);
419 dst[1]= FTOCHAR(rgba[1]);
420 dst[2]= FTOCHAR(rgba[2]);
421 dst[3]= FTOCHAR(rgba[3]);
424 dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
426 brush_sample_tex(brush, xy, rgba);
427 dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
428 dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
429 dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
430 dst[3] = FTOCHAR(rgba[3]*brush->alpha*brush_curve_strength_clamp(brush, dist, maxsize));
441 typedef struct BrushPainterCache {
444 int size; /* size override, if 0 uses brush->size */
445 short flt; /* need float imbuf? */
446 short texonly; /* no alpha, color or fallof, only texture in imbuf */
457 struct BrushPainter {
460 float lastmousepos[2]; /* mouse position of last paint call */
462 float accumdistance; /* accumulated distance of brush since last paint op */
463 float lastpaintpos[2]; /* position of last paint op */
464 float startpaintpos[2]; /* position of first paint */
466 double accumtime; /* accumulated time since last paint op (airbrush) */
467 double lasttime; /* time of last update */
471 short firsttouch; /* first paint op */
478 BrushPainterCache cache;
481 BrushPainter *brush_painter_new(Brush *brush)
483 BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
485 painter->brush= brush;
486 painter->firsttouch= 1;
487 painter->cache.lastsize= -1; /* force ibuf create in refresh */
489 painter->startsize = brush->size;
490 painter->startalpha = brush->alpha;
491 painter->startjitter = brush->jitter;
492 painter->startspacing = brush->spacing;
497 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
499 if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
500 ((painter->cache.texonly != texonly) && texonly)) {
501 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
502 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
503 painter->cache.ibuf= painter->cache.maskibuf= NULL;
504 painter->cache.lastsize= -1; /* force ibuf create in refresh */
507 if (painter->cache.flt != flt) {
508 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
509 painter->cache.texibuf= NULL;
510 painter->cache.lastsize= -1; /* force ibuf create in refresh */
513 painter->cache.size= size;
514 painter->cache.flt= flt;
515 painter->cache.texonly= texonly;
516 painter->cache.enabled= 1;
519 void brush_painter_free(BrushPainter *painter)
521 Brush *brush = painter->brush;
523 brush->size = painter->startsize;
524 brush->alpha = painter->startalpha;
525 brush->jitter = painter->startjitter;
526 brush->spacing = painter->startspacing;
528 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
529 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
530 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
534 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
536 Brush *brush= painter->brush;
537 ImBuf *ibuf, *maskibuf, *texibuf;
538 float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
539 char *b, *m, *t, *ot= NULL;
540 int dotexold, origx= x, origy= y;
542 xoff = -brush->size/2.0f + 0.5f;
543 yoff = -brush->size/2.0f + 0.5f;
544 xoff += (int)pos[0] - (int)painter->startpaintpos[0];
545 yoff += (int)pos[1] - (int)painter->startpaintpos[1];
547 ibuf = painter->cache.ibuf;
548 texibuf = painter->cache.texibuf;
549 maskibuf = painter->cache.maskibuf;
551 dotexold = (oldtexibuf != NULL);
553 if (painter->cache.flt) {
555 bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
556 tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
557 mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
560 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
562 for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
572 brush_sample_tex(brush, xy, tf);
584 b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
585 t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
586 m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
589 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
591 for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
603 brush_sample_tex(brush, xy, rgba);
604 t[0]= FTOCHAR(rgba[0]);
605 t[1]= FTOCHAR(rgba[1]);
606 t[2]= FTOCHAR(rgba[2]);
607 t[3]= FTOCHAR(rgba[3]);
610 b[0] = t[0]*m[0]/255;
611 b[1] = t[1]*m[1]/255;
612 b[2] = t[2]*m[2]/255;
613 b[3] = t[3]*m[3]/255;
619 static void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
621 Brush *brush= painter->brush;
622 BrushPainterCache *cache= &painter->cache;
623 ImBuf *oldtexibuf, *ibuf;
624 int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
626 imbflag= (cache->flt)? IB_rectfloat: IB_rect;
628 cache->ibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
631 oldtexibuf= cache->texibuf;
632 cache->texibuf= IMB_allocImBuf(brush->size, brush->size, 32, imbflag, 0);
636 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
637 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
641 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
654 /* blend existing texture in new position */
655 if ((x1 < x2) && (y1 < y2))
656 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
659 IMB_freeImBuf(oldtexibuf);
661 /* sample texture in new areas */
662 if ((0 < x1) && (0 < ibuf->y))
663 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
664 if ((x2 < ibuf->x) && (0 < ibuf->y))
665 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
666 if ((x1 < x2) && (0 < y1))
667 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
668 if ((x1 < x2) && (y2 < ibuf->y))
669 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
672 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
674 Brush *brush= painter->brush;
675 BrushPainterCache *cache= &painter->cache;
676 MTex *mtex= &brush->mtex;
680 if ((brush->size != cache->lastsize) || (brush->alpha != cache->lastalpha)
681 || (brush->jitter != cache->lastjitter)) {
683 IMB_freeImBuf(cache->ibuf);
686 if (cache->maskibuf) {
687 IMB_freeImBuf(cache->maskibuf);
688 cache->maskibuf= NULL;
692 size= (cache->size)? cache->size: brush->size;
694 if (!(mtex && mtex->tex) || (mtex->tex->type==0)) {
695 brush_imbuf_new(brush, flt, 0, size, &cache->ibuf);
697 else if (brush->flag & BRUSH_FIXED_TEX) {
698 brush_imbuf_new(brush, flt, 0, size, &cache->maskibuf);
699 brush_painter_fixed_tex_partial_update(painter, pos);
702 brush_imbuf_new(brush, flt, 2, size, &cache->ibuf);
704 cache->lastsize= brush->size;
705 cache->lastalpha= brush->alpha;
706 cache->lastjitter= brush->jitter;
708 else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
709 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
710 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
712 if ((dx != 0) || (dy != 0))
713 brush_painter_fixed_tex_partial_update(painter, pos);
717 void brush_painter_break_stroke(BrushPainter *painter)
719 painter->firsttouch= 1;
722 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
724 if (brush->flag & BRUSH_ALPHA_PRESSURE)
725 brush->alpha = MAX2(0.0, painter->startalpha*pressure);
726 if (brush->flag & BRUSH_SIZE_PRESSURE)
727 brush->size = MAX2(1.0, painter->startsize*pressure);
728 if (brush->flag & BRUSH_JITTER_PRESSURE)
729 brush->jitter = MAX2(0.0, painter->startjitter*pressure);
730 if (brush->flag & BRUSH_SPACING_PRESSURE)
731 brush->spacing = MAX2(1.0, painter->startspacing*(1.5f-pressure));
734 static void brush_jitter_pos(Brush *brush, float *pos, float *jitterpos)
737 jitterpos[0] = pos[0] + ((BLI_frand()-0.5f) * brush->size * brush->jitter * 2);
738 jitterpos[1] = pos[1] + ((BLI_frand()-0.5f) * brush->size * brush->jitter * 2);
741 VECCOPY2D(jitterpos, pos);
745 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
747 Brush *brush= painter->brush;
750 if (pressure == 0.0f) {
751 if(painter->lastpressure) // XXX - hack, operator misses
752 pressure= painter->lastpressure;
754 pressure = 1.0f; /* zero pressure == not using tablet */
756 if (painter->firsttouch) {
757 /* paint exactly once on first touch */
758 painter->startpaintpos[0]= pos[0];
759 painter->startpaintpos[1]= pos[1];
761 brush_apply_pressure(painter, brush, pressure);
762 if (painter->cache.enabled)
763 brush_painter_refresh_cache(painter, pos);
764 totpaintops += func(user, painter->cache.ibuf, pos, pos);
766 painter->lasttime= time;
767 painter->firsttouch= 0;
768 painter->lastpaintpos[0]= pos[0];
769 painter->lastpaintpos[1]= pos[1];
772 else if (painter->brush->flag & BRUSH_AIRBRUSH) {
773 float spacing, step, paintpos[2], dmousepos[2], len;
774 double starttime, curtime= time;
776 /* compute brush spacing adapted to brush size */
777 spacing= brush->rate; //brush->size*brush->spacing*0.01f;
779 /* setup starting time, direction vector and accumulated time */
780 starttime= painter->accumtime;
781 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
782 len= normalize_v2(dmousepos);
783 painter->accumtime += curtime - painter->lasttime;
785 /* do paint op over unpainted time distance */
786 while (painter->accumtime >= spacing) {
787 step= (spacing - starttime)*len;
788 paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
789 paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
791 if (painter->cache.enabled)
792 brush_painter_refresh_cache(painter);
793 totpaintops += func(user, painter->cache.ibuf,
794 painter->lastpaintpos, paintpos);
796 painter->lastpaintpos[0]= paintpos[0];
797 painter->lastpaintpos[1]= paintpos[1];
798 painter->accumtime -= spacing;
799 starttime -= spacing;
802 painter->lasttime= curtime;
806 float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
809 /* compute brush spacing adapted to brush size, spacing may depend
810 on pressure, so update it */
811 brush_apply_pressure(painter, brush, painter->lastpressure);
812 spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
814 /* setup starting distance, direction vector and accumulated distance */
815 startdistance= painter->accumdistance;
816 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
817 len= normalize_v2(dmousepos);
818 painter->accumdistance += len;
820 /* do paint op over unpainted distance */
821 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
822 step= spacing - startdistance;
823 paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
824 paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
827 press= (1.0f-t)*painter->lastpressure + t*pressure;
828 brush_apply_pressure(painter, brush, press);
829 spacing= MAX2(1.0f, brush->size)*brush->spacing*0.01f;
831 brush_jitter_pos(brush, paintpos, finalpos);
833 if (painter->cache.enabled)
834 brush_painter_refresh_cache(painter, finalpos);
837 func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
839 painter->lastpaintpos[0]= paintpos[0];
840 painter->lastpaintpos[1]= paintpos[1];
841 painter->accumdistance -= spacing;
842 startdistance -= spacing;
845 /* do airbrush paint ops, based on the number of paint ops left over
846 from regular painting. this is a temporary solution until we have
847 accurate time stamps for mouse move events */
848 if (brush->flag & BRUSH_AIRBRUSH) {
849 double curtime= time;
850 double painttime= brush->rate*totpaintops;
852 painter->accumtime += curtime - painter->lasttime;
853 if (painter->accumtime <= painttime)
854 painter->accumtime= 0.0;
856 painter->accumtime -= painttime;
858 while (painter->accumtime >= brush->rate) {
859 brush_apply_pressure(painter, brush, pressure);
861 brush_jitter_pos(brush, pos, finalpos);
863 if (painter->cache.enabled)
864 brush_painter_refresh_cache(painter, finalpos);
867 func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
868 painter->accumtime -= brush->rate;
871 painter->lasttime= curtime;
875 painter->lastmousepos[0]= pos[0];
876 painter->lastmousepos[1]= pos[1];
877 painter->lastpressure= pressure;
879 brush->alpha = painter->startalpha;
880 brush->size = painter->startsize;
881 brush->jitter = painter->startjitter;
882 brush->spacing = painter->startspacing;
887 /* Uses the brush curve control to find a strength value between 0 and 1 */
888 float brush_curve_strength_clamp(Brush *br, float p, const float len)
890 if(p >= len) p= 1.0f;
893 p= curvemapping_evaluateF(br->curve, 0, p);
895 else if(p > 1.0f) p= 1.0f;
898 /* same as above but can return negative values if the curve enables
899 * used for sculpt only */
900 float brush_curve_strength(Brush *br, float p, const float len)
902 if(p >= len) p= 1.0f;
904 return curvemapping_evaluateF(br->curve, 0, p);
907 /* TODO: should probably be unified with BrushPainter stuff? */
908 unsigned int *brush_gen_texture_cache(Brush *br, int half_side)
910 unsigned int *texcache = NULL;
911 MTex *mtex = &br->mtex;
914 int side = half_side * 2;
916 memset(&texres, 0, sizeof(TexResult));
918 if(mtex && mtex->tex) {
919 float x, y, step = 2.0 / side, co[3];
921 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
923 BKE_image_get_ibuf(mtex->tex->ima, NULL);
925 /*do normalized cannonical view coords for texture*/
926 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
927 for (x=-1.0, ix=0; ix<side; ix++, x += step) {
932 /* This is copied from displace modifier code */
933 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 1, &texres);
935 /* if the texture gave an RGB value, we assume it didn't give a valid
936 * intensity, so calculate one (formula from do_material_tex).
937 * if the texture didn't give an RGB value, copy the intensity across
940 texres.tin = (0.35 * texres.tr + 0.45 *
941 texres.tg + 0.2 * texres.tb);
943 texres.tin = texres.tin * 255.0;
944 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
945 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
946 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
947 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
955 /**** Radial Control ****/
956 static struct ImBuf *brush_gen_radial_control_imbuf(Brush *br)
958 ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
959 unsigned int *texcache;
964 texcache = brush_gen_texture_cache(br, half);
965 im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
966 im->x = im->y = side;
968 for(i=0; i<side; ++i) {
969 for(j=0; j<side; ++j) {
970 float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
971 im->rect_float[i*side + j]= brush_curve_strength_clamp(br, magn, half);
975 /* Modulate curve with texture */
977 for(i=0; i<side; ++i) {
978 for(j=0; j<side; ++j) {
979 const int col= texcache[i*side+j];
980 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
990 void brush_radial_control_invoke(wmOperator *op, Brush *br, float size_weight)
992 int mode = RNA_int_get(op->ptr, "mode");
993 float original_value= 0;
995 if(mode == WM_RADIALCONTROL_SIZE)
996 original_value = br->size * size_weight;
997 else if(mode == WM_RADIALCONTROL_STRENGTH)
998 original_value = br->alpha;
999 else if(mode == WM_RADIALCONTROL_ANGLE) {
1000 MTex *mtex = brush_active_texture(br);
1002 original_value = mtex->rot;
1005 RNA_float_set(op->ptr, "initial_value", original_value);
1006 op->customdata = brush_gen_radial_control_imbuf(br);
1009 int brush_radial_control_exec(wmOperator *op, Brush *br, float size_weight)
1011 int mode = RNA_int_get(op->ptr, "mode");
1012 float new_value = RNA_float_get(op->ptr, "new_value");
1013 const float conv = 0.017453293;
1015 if(mode == WM_RADIALCONTROL_SIZE)
1016 br->size = new_value * size_weight;
1017 else if(mode == WM_RADIALCONTROL_STRENGTH)
1018 br->alpha = new_value;
1019 else if(mode == WM_RADIALCONTROL_ANGLE) {
1020 MTex *mtex = brush_active_texture(br);
1022 mtex->rot = new_value * conv;
1025 return OPERATOR_FINISHED;