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