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