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