== Sculpt/Paint Fixes ==
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_brush_types.h"
36 #include "DNA_color_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "WM_types.h"
41 #include "WM_api.h"
42
43 #include "RNA_access.h"
44
45 #include "BLI_math.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_rand.h"
48
49 #include "BKE_brush.h"
50 #include "BKE_colortools.h"
51 #include "BKE_global.h"
52 #include "BKE_image.h"
53 #include "BKE_library.h"
54 #include "BKE_main.h"
55 #include "BKE_paint.h"
56 #include "BKE_texture.h"
57 #include "BKE_icons.h"
58
59 #include "IMB_imbuf.h"
60 #include "IMB_imbuf_types.h"
61
62 #include "RE_render_ext.h" /* externtex */
63 #include "RE_shader_ext.h"
64
65 /* Datablock add/copy/free/make_local */
66
67 Brush *add_brush(const char *name)
68 {
69         Brush *brush;
70
71         brush= alloc_libblock(&G.main->brush, ID_BR, name);
72
73         /* BRUSH SCULPT TOOL SETTINGS */
74         brush->sculpt_tool = SCULPT_TOOL_DRAW; /* sculpting defaults to the draw tool for new brushes */
75         brush->size= 35; /* radius of the brush in pixels */
76         brush->alpha= 0.5f; /* brush strength/intensity probably variable should be renamed? */
77         brush->autosmooth_factor= 0.0f;
78         brush->crease_pinch_factor= 0.5f;
79         brush->sculpt_plane = SCULPT_DISP_DIR_VIEW;
80         brush->plane_offset= 0.0f; /* how far above or below the plane that is found by averaging the faces */
81         brush->plane_trim= 0.5f;
82         brush->clone.alpha= 0.5f;
83         brush->normal_weight= 0.0f;
84
85         /* BRUSH PAINT TOOL SETTINGS */
86         brush->rgb[0]= 1.0f; /* default rgb color of the brush when painting - white */
87         brush->rgb[1]= 1.0f;
88         brush->rgb[2]= 1.0f;
89
90         /* BRUSH STROKE SETTINGS */
91         brush->flag |= (BRUSH_SPACE|BRUSH_SPACE_ATTEN);
92         brush->spacing= 10; /* how far each brush dot should be spaced as a percentage of brush diameter */
93
94         brush->smooth_stroke_radius= 75;
95         brush->smooth_stroke_factor= 0.9f;
96
97         brush->rate= 0.1f; /* time delay between dots of paint or sculpting when doing airbrush mode */
98
99         brush->jitter= 0.0f;
100
101         /* BRUSH TEXTURE SETTINGS */
102         default_mtex(&brush->mtex);
103
104         brush->texture_sample_bias= 0; /* value to added to texture samples */
105
106         /* brush appearance  */
107
108         brush->add_col[0]= 1.00; /* add mode color is light red */
109         brush->add_col[1]= 0.39;
110         brush->add_col[2]= 0.39;
111
112         brush->sub_col[0]= 0.39; /* subtract mode color is light blue */
113         brush->sub_col[1]= 0.39;
114         brush->sub_col[2]= 1.00;
115
116          /* the default alpha falloff curve */
117         brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
118
119         /* enable fake user by default */
120         brush->id.flag |= LIB_FAKEUSER;
121         brush_toggled_fake_user(brush);
122
123         return brush;
124 }
125
126 Brush *copy_brush(Brush *brush)
127 {
128         Brush *brushn;
129         
130         brushn= copy_libblock(brush);
131
132         if (brush->mtex.tex)
133                 id_us_plus((ID*)brush->mtex.tex);
134
135         if (brush->icon_imbuf)
136                 brushn->icon_imbuf= IMB_dupImBuf(brush->icon_imbuf);
137
138         brushn->curve= curvemapping_copy(brush->curve);
139
140         /* enable fake user by default */
141         if (!(brushn->id.flag & LIB_FAKEUSER)) {
142                 brushn->id.flag |= LIB_FAKEUSER;
143                 brush_toggled_fake_user(brushn);
144         }
145         
146         return brushn;
147 }
148
149 /* not brush itself */
150 void free_brush(Brush *brush)
151 {
152         if (brush->mtex.tex)
153                 brush->mtex.tex->id.us--;
154
155         if (brush->icon_imbuf)
156                 IMB_freeImBuf(brush->icon_imbuf);
157
158         BKE_previewimg_free(&(brush->preview));
159
160         curvemapping_free(brush->curve);
161 }
162
163 void make_local_brush(Brush *brush)
164 {
165         /* - only lib users: do nothing
166                 * - only local users: set flag
167                 * - mixed: make copy
168                 */
169         
170         Brush *brushn;
171         Scene *scene;
172         int local= 0, lib= 0;
173
174         if(brush->id.lib==0) return;
175
176         if(brush->clone.image) {
177                 /* special case: ima always local immediately */
178                 brush->clone.image->id.lib= 0;
179                 brush->clone.image->id.flag= LIB_LOCAL;
180                 new_id(0, (ID *)brush->clone.image, 0);
181         }
182
183         for(scene= G.main->scene.first; scene; scene=scene->id.next)
184                 if(paint_brush(&scene->toolsettings->imapaint.paint)==brush) {
185                         if(scene->id.lib) lib= 1;
186                         else local= 1;
187                 }
188
189         if(local && lib==0) {
190                 brush->id.lib= 0;
191                 brush->id.flag= LIB_LOCAL;
192                 new_id(0, (ID *)brush, 0);
193
194                 /* enable fake user by default */
195                 if (!(brush->id.flag & LIB_FAKEUSER)) {
196                         brush->id.flag |= LIB_FAKEUSER;
197                         brush_toggled_fake_user(brush);
198                 }
199         }
200         else if(local && lib) {
201                 brushn= copy_brush(brush);
202                 brushn->id.us= 1; /* only keep fake user */
203                 brushn->id.flag |= LIB_FAKEUSER;
204                 
205                 for(scene= G.main->scene.first; scene; scene=scene->id.next)
206                         if(paint_brush(&scene->toolsettings->imapaint.paint)==brush)
207                                 if(scene->id.lib==0) {
208                                         paint_brush_set(&scene->toolsettings->imapaint.paint, brushn);
209                                         brushn->id.us++;
210                                         brush->id.us--;
211                                 }
212         }
213 }
214
215 /* Library Operations */
216
217 int brush_set_nr(Brush **current_brush, int nr, const char *name)
218 {
219         ID *idtest, *id;
220         
221         id= (ID*)(*current_brush);
222         idtest= (ID*)BLI_findlink(&G.main->brush, nr-1);
223         
224         if(idtest==0) { /* new brush */
225                 if(id) idtest= (ID *)copy_brush((Brush *)id);
226                 else idtest= (ID *)add_brush(name);
227                 idtest->us--;
228         }
229         if(idtest!=id) {
230                 brush_delete(current_brush);
231                 *current_brush= (Brush *)idtest;
232                 id_us_plus(idtest);
233
234                 return 1;
235         }
236
237         return 0;
238 }
239
240 int brush_delete(Brush **current_brush)
241 {
242         if (*current_brush) {
243                 (*current_brush)->id.us--;
244                 *current_brush= NULL;
245
246                 return 1;
247         }
248
249         return 0;
250 }
251
252 void brush_toggled_fake_user(Brush *brush)
253 {
254         ID *id= (ID*)brush;
255         if(id) {
256                 if(id->flag & LIB_FAKEUSER) {
257                         id_us_plus(id);
258                 } else {
259                         id->us--;
260                 }
261         }
262 }
263
264 void brush_curve_preset(Brush *b, /*CurveMappingPreset*/int preset)
265 {
266         CurveMap *cm = NULL;
267
268         if(!b->curve)
269                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
270
271         cm = b->curve->cm;
272         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
273
274         b->curve->preset = preset;
275         curvemap_reset(cm, &b->curve->clipr, b->curve->preset);
276         curvemapping_changed(b->curve, 0);
277 }
278
279 static MTex *brush_active_texture(Brush *brush)
280 {
281         if(brush)
282                 return &brush->mtex;
283         return NULL;
284 }
285
286 int brush_texture_set_nr(Brush *brush, int nr)
287 {
288         ID *idtest, *id=NULL;
289
290         id= (ID *)brush->mtex.tex;
291
292         idtest= (ID*)BLI_findlink(&G.main->tex, nr-1);
293         if(idtest==0) { /* new tex */
294                 if(id) idtest= (ID *)copy_texture((Tex *)id);
295                 else idtest= (ID *)add_texture("Tex");
296                 idtest->us--;
297         }
298         if(idtest!=id) {
299                 brush_texture_delete(brush);
300
301                 brush->mtex.tex= (Tex*)idtest;
302                 id_us_plus(idtest);
303
304                 return 1;
305         }
306
307         return 0;
308 }
309
310 int brush_texture_delete(Brush *brush)
311 {
312         if(brush->mtex.tex)
313                 brush->mtex.tex->id.us--;
314
315         return 1;
316 }
317
318 int brush_clone_image_set_nr(Brush *brush, int nr)
319 {
320         if(brush && nr > 0) {
321                 Image *ima= (Image*)BLI_findlink(&G.main->image, nr-1);
322
323                 if(ima) {
324                         brush_clone_image_delete(brush);
325                         brush->clone.image= ima;
326                         id_us_plus(&ima->id);
327                         brush->clone.offset[0]= brush->clone.offset[1]= 0.0f;
328
329                         return 1;
330                 }
331         }
332
333         return 0;
334 }
335
336 int brush_clone_image_delete(Brush *brush)
337 {
338         if (brush && brush->clone.image) {
339                 brush->clone.image->id.us--;
340                 brush->clone.image= NULL;
341                 return 1;
342         }
343
344         return 0;
345 }
346
347 void brush_check_exists(Brush **brush, const char *name)
348 {
349         if(*brush==NULL)
350                 brush_set_nr(brush, 1, name);
351 }
352
353 /* Brush Sampling */
354 void brush_sample_tex(Brush *brush, float *xy, float *rgba)
355 {
356         MTex *mtex= &brush->mtex;
357
358         if (mtex && mtex->tex) {
359                 float co[3], tin, tr, tg, tb, ta;
360                 int hasrgb;
361                 const int radius= brush_size(brush);
362
363                 co[0]= xy[0]/radius;
364                 co[1]= xy[1]/radius;
365                 co[2]= 0.0f;
366
367                 hasrgb= externtex(mtex, co, &tin, &tr, &tg, &tb, &ta);
368
369                 if (hasrgb) {
370                         rgba[0]= tr;
371                         rgba[1]= tg;
372                         rgba[2]= tb;
373                         rgba[3]= ta;
374                 }
375                 else {
376                         rgba[0]= tin;
377                         rgba[1]= tin;
378                         rgba[2]= tin;
379                         rgba[3]= 1.0f;
380                 }
381         }
382         else if (rgba)
383                 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
384 }
385
386
387 void brush_imbuf_new(Brush *brush, short flt, short texfall, int bufsize, ImBuf **outbuf)
388 {
389         ImBuf *ibuf;
390         float xy[2], dist, rgba[4], *dstf;
391         int x, y, rowbytes, xoff, yoff, imbflag;
392         const int radius= brush_size(brush);
393         char *dst, crgb[3];
394         const float alpha= brush_alpha(brush);
395
396         imbflag= (flt)? IB_rectfloat: IB_rect;
397         xoff = -bufsize/2.0f + 0.5f;
398         yoff = -bufsize/2.0f + 0.5f;
399         rowbytes= bufsize*4;
400
401         if (*outbuf)
402                 ibuf= *outbuf;
403         else
404                 ibuf= IMB_allocImBuf(bufsize, bufsize, 32, imbflag, 0);
405
406         if (flt) {
407                 for (y=0; y < ibuf->y; y++) {
408                         dstf = ibuf->rect_float + y*rowbytes;
409
410                         for (x=0; x < ibuf->x; x++, dstf+=4) {
411                                 xy[0] = x + xoff;
412                                 xy[1] = y + yoff;
413
414                                 if (texfall == 0) {
415                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
416
417                                         VECCOPY(dstf, brush->rgb);
418                                         dstf[3]= alpha*brush_curve_strength_clamp(brush, dist, radius);
419                                 }
420                                 else if (texfall == 1) {
421                                         brush_sample_tex(brush, xy, dstf);
422                                 }
423                                 else {
424                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
425
426                                         brush_sample_tex(brush, xy, rgba);
427
428                                         dstf[0] = rgba[0]*brush->rgb[0];
429                                         dstf[1] = rgba[1]*brush->rgb[1];
430                                         dstf[2] = rgba[2]*brush->rgb[2];
431                                         dstf[3] = rgba[3]*alpha*brush_curve_strength_clamp(brush, dist, radius);
432                                 }
433                         }
434                 }
435         }
436         else {
437                 crgb[0]= FTOCHAR(brush->rgb[0]);
438                 crgb[1]= FTOCHAR(brush->rgb[1]);
439                 crgb[2]= FTOCHAR(brush->rgb[2]);
440
441                 for (y=0; y < ibuf->y; y++) {
442                         dst = (char*)ibuf->rect + y*rowbytes;
443
444                         for (x=0; x < ibuf->x; x++, dst+=4) {
445                                 xy[0] = x + xoff;
446                                 xy[1] = y + yoff;
447
448                                 if (texfall == 0) {
449                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
450
451                                         dst[0]= crgb[0];
452                                         dst[1]= crgb[1];
453                                         dst[2]= crgb[2];
454                                         dst[3]= FTOCHAR(alpha*brush_curve_strength(brush, dist, radius));
455                                 }
456                                 else if (texfall == 1) {
457                                         brush_sample_tex(brush, xy, rgba);
458                                         dst[0]= FTOCHAR(rgba[0]);
459                                         dst[1]= FTOCHAR(rgba[1]);
460                                         dst[2]= FTOCHAR(rgba[2]);
461                                         dst[3]= FTOCHAR(rgba[3]);
462                                 }
463                                 else {
464                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
465
466                                         brush_sample_tex(brush, xy, rgba);
467                                         dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
468                                         dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
469                                         dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
470                                         dst[3] = FTOCHAR(rgba[3]*alpha*brush_curve_strength_clamp(brush, dist, radius));
471                                 }
472                         }
473                 }
474         }
475
476         *outbuf= ibuf;
477 }
478
479 /* Brush Painting */
480
481 typedef struct BrushPainterCache {
482         short enabled;
483
484         int size;                       /* size override, if 0 uses brush_size(brush) */
485         short flt;                      /* need float imbuf? */
486         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
487
488         int lastsize;
489         float lastalpha;
490         float lastjitter;
491
492         ImBuf *ibuf;
493         ImBuf *texibuf;
494         ImBuf *maskibuf;
495 } BrushPainterCache;
496
497 struct BrushPainter {
498         Brush *brush;
499
500         float lastmousepos[2];  /* mouse position of last paint call */
501
502         float accumdistance;    /* accumulated distance of brush since last paint op */
503         float lastpaintpos[2];  /* position of last paint op */
504         float startpaintpos[2]; /* position of first paint */
505
506         double accumtime;               /* accumulated time since last paint op (airbrush) */
507         double lasttime;                /* time of last update */
508
509         float lastpressure;
510
511         short firsttouch;               /* first paint op */
512
513         float startsize;
514         float startalpha;
515         float startjitter;
516         float startspacing;
517
518         BrushPainterCache cache;
519 };
520
521 BrushPainter *brush_painter_new(Brush *brush)
522 {
523         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
524
525         painter->brush= brush;
526         painter->firsttouch= 1;
527         painter->cache.lastsize= -1; /* force ibuf create in refresh */
528
529         painter->startsize = brush_size(brush);
530         painter->startalpha = brush_alpha(brush);
531         painter->startjitter = brush->jitter;
532         painter->startspacing = brush->spacing;
533
534         return painter;
535 }
536
537 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
538 {
539         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
540                 ((painter->cache.texonly != texonly) && texonly)) {
541                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
542                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
543                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
544                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
545         }
546
547         if (painter->cache.flt != flt) {
548                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
549                 painter->cache.texibuf= NULL;
550                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
551         }
552
553         painter->cache.size= size;
554         painter->cache.flt= flt;
555         painter->cache.texonly= texonly;
556         painter->cache.enabled= 1;
557 }
558
559 void brush_painter_free(BrushPainter *painter)
560 {
561         Brush *brush = painter->brush;
562
563         brush_set_size(brush, painter->startsize);
564         brush_set_alpha(brush, painter->startalpha);
565         brush->jitter = painter->startjitter;
566         brush->spacing = painter->startspacing;
567
568         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
569         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
570         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
571         MEM_freeN(painter);
572 }
573
574 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
575 {
576         Brush *brush= painter->brush;
577         ImBuf *ibuf, *maskibuf, *texibuf;
578         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
579         char *b, *m, *t, *ot= NULL;
580         int dotexold, origx= x, origy= y;
581         const int radius= brush_size(brush);
582
583         xoff = -radius + 0.5f;
584         yoff = -radius + 0.5f;
585         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
586         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
587
588         ibuf = painter->cache.ibuf;
589         texibuf = painter->cache.texibuf;
590         maskibuf = painter->cache.maskibuf;
591
592         dotexold = (oldtexibuf != NULL);
593
594         if (painter->cache.flt) {
595                 for (; y < h; y++) {
596                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
597                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
598                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
599
600                         if (dotexold)
601                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
602
603                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
604                                 if (dotexold) {
605                                         VECCOPY(tf, otf);
606                                         tf[3] = otf[3];
607                                         otf += 4;
608                                 }
609                                 else {
610                                         xy[0] = x + xoff;
611                                         xy[1] = y + yoff;
612
613                                         brush_sample_tex(brush, xy, tf);
614                                 }
615
616                                 bf[0] = tf[0]*mf[0];
617                                 bf[1] = tf[1]*mf[1];
618                                 bf[2] = tf[2]*mf[2];
619                                 bf[3] = tf[3]*mf[3];
620                         }
621                 }
622         }
623         else {
624                 for (; y < h; y++) {
625                         b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
626                         t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
627                         m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
628
629                         if (dotexold)
630                                 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
631
632                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
633                                 if (dotexold) {
634                                         t[0] = ot[0];
635                                         t[1] = ot[1];
636                                         t[2] = ot[2];
637                                         t[3] = ot[3];
638                                         ot += 4;
639                                 }
640                                 else {
641                                         xy[0] = x + xoff;
642                                         xy[1] = y + yoff;
643
644                                         brush_sample_tex(brush, xy, rgba);
645                                         t[0]= FTOCHAR(rgba[0]);
646                                         t[1]= FTOCHAR(rgba[1]);
647                                         t[2]= FTOCHAR(rgba[2]);
648                                         t[3]= FTOCHAR(rgba[3]);
649                                 }
650
651                                 b[0] = t[0]*m[0]/255;
652                                 b[1] = t[1]*m[1]/255;
653                                 b[2] = t[2]*m[2]/255;
654                                 b[3] = t[3]*m[3]/255;
655                         }
656                 }
657         }
658 }
659
660 static void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
661 {
662         Brush *brush= painter->brush;
663         BrushPainterCache *cache= &painter->cache;
664         ImBuf *oldtexibuf, *ibuf;
665         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
666         const int diameter= 2*brush_size(brush);
667
668         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
669         if (!cache->ibuf)
670                 cache->ibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag, 0);
671         ibuf= cache->ibuf;
672
673         oldtexibuf= cache->texibuf;
674         cache->texibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag, 0);
675
676         if (oldtexibuf) {
677                 srcx= srcy= 0;
678                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
679                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
680                 w= oldtexibuf->x;
681                 h= oldtexibuf->y;
682
683                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
684         }
685         else {
686                 srcx= srcy= 0;
687                 destx= desty= 0;
688                 w= h= 0;
689         }
690         
691         x1= destx;
692         y1= desty;
693         x2= destx+w;
694         y2= desty+h;
695
696         /* blend existing texture in new position */
697         if ((x1 < x2) && (y1 < y2))
698                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
699
700         if (oldtexibuf)
701                 IMB_freeImBuf(oldtexibuf);
702
703         /* sample texture in new areas */
704         if ((0 < x1) && (0 < ibuf->y))
705                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
706         if ((x2 < ibuf->x) && (0 < ibuf->y))
707                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
708         if ((x1 < x2) && (0 < y1))
709                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
710         if ((x1 < x2) && (y2 < ibuf->y))
711                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
712 }
713
714 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos)
715 {
716         Brush *brush= painter->brush;
717         BrushPainterCache *cache= &painter->cache;
718         MTex *mtex= &brush->mtex;
719         int size;
720         short flt;
721         const int radius= brush_size(brush);
722         const float alpha= brush_alpha(brush);
723
724         if (radius != cache->lastsize ||
725                 alpha != cache->lastalpha ||
726                 brush->jitter != cache->lastjitter)
727         {
728                 if (cache->ibuf) {
729                         IMB_freeImBuf(cache->ibuf);
730                         cache->ibuf= NULL;
731                 }
732                 if (cache->maskibuf) {
733                         IMB_freeImBuf(cache->maskibuf);
734                         cache->maskibuf= NULL;
735                 }
736
737                 flt= cache->flt;
738                 size= (cache->size)? cache->size: radius;
739
740                 if (!(mtex && mtex->tex) || (mtex->tex->type==0)) {
741                         brush_imbuf_new(brush, flt, 0, size, &cache->ibuf);
742                 }
743                 else if (brush->flag & BRUSH_FIXED_TEX) {
744                         brush_imbuf_new(brush, flt, 0, size, &cache->maskibuf);
745                         brush_painter_fixed_tex_partial_update(painter, pos);
746                 }
747                 else
748                         brush_imbuf_new(brush, flt, 2, size, &cache->ibuf);
749
750                 cache->lastsize= radius;
751                 cache->lastalpha= alpha;
752                 cache->lastjitter= brush->jitter;
753         }
754         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
755                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
756                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
757
758                 if ((dx != 0) || (dy != 0))
759                         brush_painter_fixed_tex_partial_update(painter, pos);
760         }
761 }
762
763 void brush_painter_break_stroke(BrushPainter *painter)
764 {
765         painter->firsttouch= 1;
766 }
767
768 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
769 {
770         if (brush_use_alpha_pressure(brush)) 
771                 brush_set_alpha(brush, MAX2(0.0, painter->startalpha*pressure));
772         if (brush_use_size_pressure(brush))
773                 brush_set_size(brush, MAX2(1.0, painter->startsize*pressure));
774         if (brush->flag & BRUSH_JITTER_PRESSURE)
775                 brush->jitter = MAX2(0.0, painter->startjitter*pressure);
776         if (brush->flag & BRUSH_SPACING_PRESSURE)
777                 brush->spacing = MAX2(1.0, painter->startspacing*(1.5f-pressure));
778 }
779
780 void brush_jitter_pos(Brush *brush, float *pos, float *jitterpos)
781 {
782         if(brush->jitter){
783                 float rand_pos[2];
784                 const int radius= brush_size(brush);
785
786                 // find random position within a circle of diameter 1
787                 do {
788                         rand_pos[0] = BLI_frand()-0.5f;
789                         rand_pos[1] = BLI_frand()-0.5f;
790                 } while (len_v2(rand_pos) > 0.5f);
791
792                 jitterpos[0] = pos[0] + 2*rand_pos[0]*radius*brush->jitter;
793                 jitterpos[1] = pos[1] + 2*rand_pos[1]*radius*brush->jitter;
794         }
795         else {
796                 VECCOPY2D(jitterpos, pos);
797         }
798 }
799
800 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
801 {
802         Brush *brush= painter->brush;
803         int totpaintops= 0;
804
805         if (pressure == 0.0f) {
806                 if(painter->lastpressure) // XXX - hack, operator misses
807                         pressure= painter->lastpressure;
808                 else
809                         pressure = 1.0f;        /* zero pressure == not using tablet */
810         }
811         if (painter->firsttouch) {
812                 /* paint exactly once on first touch */
813                 painter->startpaintpos[0]= pos[0];
814                 painter->startpaintpos[1]= pos[1];
815
816                 brush_apply_pressure(painter, brush, pressure);
817                 if (painter->cache.enabled)
818                         brush_painter_refresh_cache(painter, pos);
819                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
820                 
821                 painter->lasttime= time;
822                 painter->firsttouch= 0;
823                 painter->lastpaintpos[0]= pos[0];
824                 painter->lastpaintpos[1]= pos[1];
825         }
826 #if 0
827         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
828                 float spacing, step, paintpos[2], dmousepos[2], len;
829                 double starttime, curtime= time;
830
831                 /* compute brush spacing adapted to brush size */
832                 spacing= brush->rate; //brush_size(brush)*brush->spacing*0.01f;
833
834                 /* setup starting time, direction vector and accumulated time */
835                 starttime= painter->accumtime;
836                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
837                 len= normalize_v2(dmousepos);
838                 painter->accumtime += curtime - painter->lasttime;
839
840                 /* do paint op over unpainted time distance */
841                 while (painter->accumtime >= spacing) {
842                         step= (spacing - starttime)*len;
843                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
844                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
845
846                         if (painter->cache.enabled)
847                                 brush_painter_refresh_cache(painter);
848                         totpaintops += func(user, painter->cache.ibuf,
849                                 painter->lastpaintpos, paintpos);
850
851                         painter->lastpaintpos[0]= paintpos[0];
852                         painter->lastpaintpos[1]= paintpos[1];
853                         painter->accumtime -= spacing;
854                         starttime -= spacing;
855                 }
856                 
857                 painter->lasttime= curtime;
858         }
859 #endif
860         else {
861                 float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
862                 float t, len, press;
863                 const int radius= brush_size(brush);
864
865                 /* compute brush spacing adapted to brush radius, spacing may depend
866                    on pressure, so update it */
867                 brush_apply_pressure(painter, brush, painter->lastpressure);
868                 spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
869
870                 /* setup starting distance, direction vector and accumulated distance */
871                 startdistance= painter->accumdistance;
872                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
873                 len= normalize_v2(dmousepos);
874                 painter->accumdistance += len;
875
876                 /* do paint op over unpainted distance */
877                 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
878                         step= spacing - startdistance;
879                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
880                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
881
882                         t = step/len;
883                         press= (1.0f-t)*painter->lastpressure + t*pressure;
884                         brush_apply_pressure(painter, brush, press);
885                         spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
886
887                         brush_jitter_pos(brush, paintpos, finalpos);
888
889                         if (painter->cache.enabled)
890                                 brush_painter_refresh_cache(painter, finalpos);
891
892                         totpaintops +=
893                                 func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
894
895                         painter->lastpaintpos[0]= paintpos[0];
896                         painter->lastpaintpos[1]= paintpos[1];
897                         painter->accumdistance -= spacing;
898                         startdistance -= spacing;
899                 }
900
901                 /* do airbrush paint ops, based on the number of paint ops left over
902                    from regular painting. this is a temporary solution until we have
903                    accurate time stamps for mouse move events */
904                 if (brush->flag & BRUSH_AIRBRUSH) {
905                         double curtime= time;
906                         double painttime= brush->rate*totpaintops;
907
908                         painter->accumtime += curtime - painter->lasttime;
909                         if (painter->accumtime <= painttime)
910                                 painter->accumtime= 0.0;
911                         else
912                                 painter->accumtime -= painttime;
913
914                         while (painter->accumtime >= brush->rate) {
915                                 brush_apply_pressure(painter, brush, pressure);
916
917                                 brush_jitter_pos(brush, pos, finalpos);
918
919                                 if (painter->cache.enabled)
920                                         brush_painter_refresh_cache(painter, finalpos);
921
922                                 totpaintops +=
923                                         func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
924                                 painter->accumtime -= brush->rate;
925                         }
926
927                         painter->lasttime= curtime;
928                 }
929         }
930
931         painter->lastmousepos[0]= pos[0];
932         painter->lastmousepos[1]= pos[1];
933         painter->lastpressure= pressure;
934
935         brush_set_alpha(brush, painter->startalpha);
936         brush_set_size(brush, painter->startsize);
937         brush->jitter = painter->startjitter;
938         brush->spacing = painter->startspacing;
939
940         return totpaintops;
941 }
942
943 /* Uses the brush curve control to find a strength value between 0 and 1 */
944 float brush_curve_strength_clamp(Brush *br, float p, const float len)
945 {
946         if(p >= len)    return 0;
947         else                    p= p/len;
948
949         p= curvemapping_evaluateF(br->curve, 0, p);
950         if(p < 0.0)                     p= 0.0f;
951         else if(p > 1.0f)       p= 1.0f;
952         return p;
953 }
954 /* same as above but can return negative values if the curve enables
955  * used for sculpt only */
956 float brush_curve_strength(Brush *br, float p, const float len)
957 {
958     if(p >= len)
959         p= 1.0f;
960     else
961         p= p/len;
962
963     return curvemapping_evaluateF(br->curve, 0, p);
964 }
965
966 /* TODO: should probably be unified with BrushPainter stuff? */
967 unsigned int *brush_gen_texture_cache(Brush *br, int half_side)
968 {
969         unsigned int *texcache = NULL;
970         MTex *mtex = &br->mtex;
971         TexResult texres;
972         int hasrgb, ix, iy;
973         int side = half_side * 2;
974
975         memset(&texres, 0, sizeof(TexResult));
976         
977         if(mtex->tex) {
978                 float x, y, step = 2.0 / side, co[3];
979
980                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
981
982                 BKE_image_get_ibuf(mtex->tex->ima, NULL);
983                 
984                 /*do normalized cannonical view coords for texture*/
985                 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
986                         for (x=-1.0, ix=0; ix<side; ix++, x += step) {
987                                 co[0]= x;
988                                 co[1]= y;
989                                 co[2]= 0.0f;
990                                 
991                                 /* This is copied from displace modifier code */
992                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres);
993                         
994                                 /* if the texture gave an RGB value, we assume it didn't give a valid
995                                  * intensity, so calculate one (formula from do_material_tex).
996                                  * if the texture didn't give an RGB value, copy the intensity across
997                                  */
998                                 if(hasrgb & TEX_RGB)
999                                         texres.tin = (0.35 * texres.tr + 0.45 *
1000                                                                   texres.tg + 0.2 * texres.tb);
1001
1002                                 texres.tin = texres.tin * 255.0;
1003                                 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
1004                                 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
1005                                 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
1006                                 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
1007                         }
1008                 }
1009         }
1010
1011         return texcache;
1012 }
1013
1014 /**** Radial Control ****/
1015 static struct ImBuf *brush_gen_radial_control_imbuf(Brush *br)
1016 {
1017         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1018         unsigned int *texcache;
1019         int side = 128;
1020         int half = side / 2;
1021         int i, j;
1022
1023         texcache = brush_gen_texture_cache(br, half);
1024         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1025         im->x = im->y = side;
1026
1027         for(i=0; i<side; ++i) {
1028                 for(j=0; j<side; ++j) {
1029                         float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
1030                         im->rect_float[i*side + j]= brush_curve_strength_clamp(br, magn, half);
1031                 }
1032         }
1033
1034         /* Modulate curve with texture */
1035         if(texcache) {
1036                 for(i=0; i<side; ++i) {
1037                         for(j=0; j<side; ++j) {
1038                                 const int col= texcache[i*side+j];
1039                                 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
1040                         }
1041                 }
1042
1043                 MEM_freeN(texcache);
1044         }
1045
1046         return im;
1047 }
1048
1049 void brush_radial_control_invoke(wmOperator *op, Brush *br, float size_weight)
1050 {
1051         int mode = RNA_int_get(op->ptr, "mode");
1052         float original_value= 0;
1053
1054         if(mode == WM_RADIALCONTROL_SIZE)
1055                 original_value = brush_size(br) * size_weight;
1056         else if(mode == WM_RADIALCONTROL_STRENGTH)
1057                 original_value = brush_alpha(br);
1058         else if(mode == WM_RADIALCONTROL_ANGLE) {
1059                 MTex *mtex = brush_active_texture(br);
1060                 if(mtex)
1061                         original_value = mtex->rot;
1062         }
1063
1064         RNA_float_set(op->ptr, "initial_value", original_value);
1065         op->customdata = brush_gen_radial_control_imbuf(br);
1066 }
1067
1068 int brush_radial_control_exec(wmOperator *op, Brush *br, float size_weight)
1069 {
1070         int mode = RNA_int_get(op->ptr, "mode");
1071         float new_value = RNA_float_get(op->ptr, "new_value");
1072         const float conv = 0.017453293;
1073
1074         if(mode == WM_RADIALCONTROL_SIZE)
1075                 if (brush_use_locked_size(br)) {
1076                         float initial_value = RNA_float_get(op->ptr, "initial_value");
1077                         const float unprojected_radius = brush_unprojected_radius(br);
1078                         brush_set_unprojected_radius(br, unprojected_radius * new_value/initial_value * size_weight);
1079                 }
1080                 else
1081                         brush_set_size(br, new_value * size_weight);
1082         else if(mode == WM_RADIALCONTROL_STRENGTH)
1083                 brush_set_alpha(br, new_value);
1084         else if(mode == WM_RADIALCONTROL_ANGLE) {
1085                 MTex *mtex = brush_active_texture(br);
1086                 if(mtex)
1087                         mtex->rot = new_value * conv;
1088         }
1089
1090         return OPERATOR_FINISHED;
1091 }
1092
1093 /* Unified Size and Strength */
1094
1095 static void set_unified_settings(Brush *brush, short flag, int value)
1096 {
1097         Scene *sce;
1098         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1099                 if (sce->toolsettings && 
1100                         ELEM4(brush,
1101                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1102                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1103                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1104                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1105                 {
1106                         if (value)
1107                                 sce->toolsettings->sculpt_paint_settings |= flag;
1108                         else
1109                                 sce->toolsettings->sculpt_paint_settings &= ~flag;
1110                 }
1111         }
1112 }
1113
1114 static short unified_settings(Brush *brush)
1115 {
1116         Scene *sce;
1117         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1118                 if (sce->toolsettings && 
1119                         ELEM4(brush,
1120                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1121                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1122                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1123                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1124                 {
1125                         return sce->toolsettings->sculpt_paint_settings;
1126                 }
1127         }
1128
1129         return 0;
1130 }
1131
1132 static void set_unified_size(Brush *brush, int value)
1133 {
1134         Scene *sce;
1135         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1136                 if (sce->toolsettings && 
1137                         ELEM4(brush,
1138                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1139                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1140                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1141                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1142                 {
1143                         sce->toolsettings->sculpt_paint_unified_size= value;
1144                 }
1145         }
1146 }
1147
1148 static int unified_size(Brush *brush)
1149 {
1150         Scene *sce;
1151         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1152                 if (sce->toolsettings && 
1153                         ELEM4(brush,
1154                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1155                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1156                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1157                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1158                 {
1159                         return sce->toolsettings->sculpt_paint_unified_size;
1160                 }
1161         }
1162
1163         return 35; // XXX magic number
1164 }
1165
1166 static void set_unified_alpha(Brush *brush, float value)
1167 {
1168         Scene *sce;
1169         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1170                 if (sce->toolsettings && 
1171                         ELEM4(brush,
1172                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1173                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1174                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1175                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1176                 {
1177                         sce->toolsettings->sculpt_paint_unified_alpha= value;
1178                 }
1179         }
1180 }
1181
1182 static float unified_alpha(Brush *brush)
1183 {
1184         Scene *sce;
1185         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1186                 if (sce->toolsettings && 
1187                         ELEM4(brush,
1188                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1189                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1190                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1191                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1192                 {
1193                         return sce->toolsettings->sculpt_paint_unified_alpha;
1194                 }
1195         }
1196
1197         return 0.5f; // XXX magic number
1198 }
1199
1200 static void set_unified_unprojected_radius(Brush *brush, float value)
1201 {
1202         Scene *sce;
1203         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1204                 if (sce->toolsettings && 
1205                         ELEM4(brush,
1206                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1207                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1208                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1209                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1210                 {
1211                         sce->toolsettings->sculpt_paint_unified_unprojected_radius= value;
1212                 }
1213         }
1214 }
1215
1216 static float unified_unprojected_radius(Brush *brush)
1217 {
1218         Scene *sce;
1219         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1220                 if (sce->toolsettings && 
1221                         ELEM4(brush,
1222                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1223                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1224                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1225                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1226                 {
1227                         return sce->toolsettings->sculpt_paint_unified_unprojected_radius;
1228                 }
1229         }
1230
1231         return 0.125f; // XXX magic number
1232 }
1233 void brush_set_size(Brush *brush, int size)
1234 {
1235         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE)
1236                 set_unified_size(brush, size);
1237         else
1238                 brush->size= size;
1239
1240         WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1241 }
1242
1243 int brush_size(Brush *brush)
1244 {
1245         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? unified_size(brush) : brush->size;
1246 }
1247
1248 void brush_set_use_locked_size(Brush *brush, int value)
1249 {
1250         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) {
1251                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_LOCK_BRUSH_SIZE, value);
1252         }
1253         else {
1254                 if (value)
1255                         brush->flag |= BRUSH_LOCK_SIZE;
1256                 else
1257                         brush->flag &= ~BRUSH_LOCK_SIZE;
1258         }
1259
1260         WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1261 }
1262
1263 int brush_use_locked_size(Brush *brush)
1264 {
1265         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_LOCK_BRUSH_SIZE) : (brush->flag & BRUSH_LOCK_SIZE);
1266 }
1267
1268 void brush_set_use_size_pressure(Brush *brush, int value)
1269 {
1270         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) {
1271                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_SIZE_PRESSURE, value);
1272         }
1273         else {
1274                 if (value)
1275                         brush->flag |= BRUSH_SIZE_PRESSURE;
1276                 else
1277                         brush->flag &= ~BRUSH_SIZE_PRESSURE;
1278         }
1279
1280         WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1281 }
1282
1283 int brush_use_size_pressure(Brush *brush)
1284 {
1285         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_SIZE_PRESSURE) : (brush->flag & BRUSH_SIZE_PRESSURE);
1286 }
1287
1288 void brush_set_use_alpha_pressure(Brush *brush, int value)
1289 {
1290         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) {
1291                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_ALPHA_PRESSURE, value);
1292         }
1293         else {
1294                 if (value)
1295                         brush->flag |= BRUSH_ALPHA_PRESSURE;
1296                 else
1297                         brush->flag &= ~BRUSH_ALPHA_PRESSURE;
1298         }
1299
1300         WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1301 }
1302
1303 int brush_use_alpha_pressure(Brush *brush)
1304 {
1305         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_ALPHA_PRESSURE) : (brush->flag & BRUSH_ALPHA_PRESSURE);
1306 }
1307
1308 void brush_set_unprojected_radius(Brush *brush, float unprojected_radius)
1309 {
1310         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE)
1311                 set_unified_unprojected_radius(brush, unprojected_radius);
1312         else
1313                 brush->unprojected_radius= unprojected_radius;
1314
1315         WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1316 }
1317
1318 float brush_unprojected_radius(Brush *brush)
1319 {
1320         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? unified_unprojected_radius(brush) : brush->unprojected_radius;
1321 }
1322
1323 void brush_set_alpha(Brush *brush, float alpha)
1324 {
1325         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) 
1326                 set_unified_alpha(brush, alpha);
1327         else
1328                 brush->alpha= alpha;
1329
1330         WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1331 }
1332
1333 float brush_alpha(Brush *brush)
1334 {
1335         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) ? unified_alpha(brush) : brush->alpha;
1336 }