15404acc10539d95a930ea4ea5b4a01f06faf177
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_brush_types.h"
36 #include "DNA_color_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_windowmanager_types.h"
40
41 #include "WM_types.h"
42
43 #include "RNA_access.h"
44
45 #include "BLI_math.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_rand.h"
48 #include "BLI_utildefines.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_ALL_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->preview = NULL;
152
153         brushn->curve= curvemapping_copy(brush->curve);
154
155         /* enable fake user by default */
156         if (!(brushn->id.flag & LIB_FAKEUSER)) {
157                 brushn->id.flag |= LIB_FAKEUSER;
158                 brushn->id.us++;
159         }
160         
161         return brushn;
162 }
163
164 /* not brush itself */
165 void free_brush(Brush *brush)
166 {
167         if (brush->mtex.tex)
168                 brush->mtex.tex->id.us--;
169
170         if (brush->icon_imbuf)
171                 IMB_freeImBuf(brush->icon_imbuf);
172
173         BKE_previewimg_free(&(brush->preview));
174
175         curvemapping_free(brush->curve);
176 }
177
178 void make_local_brush(Brush *brush)
179 {
180         /* - only lib users: do nothing
181                 * - only local users: set flag
182                 * - mixed: make copy
183                 */
184         
185         Brush *brushn;
186         Scene *scene;
187         int local= 0, lib= 0;
188
189         if(brush->id.lib==NULL) return;
190
191         if(brush->clone.image) {
192                 /* special case: ima always local immediately */
193                 brush->clone.image->id.lib= NULL;
194                 brush->clone.image->id.flag= LIB_LOCAL;
195                 new_id(NULL, (ID *)brush->clone.image, NULL);
196         }
197
198         for(scene= G.main->scene.first; scene; scene=scene->id.next)
199                 if(paint_brush(&scene->toolsettings->imapaint.paint)==brush) {
200                         if(scene->id.lib) lib= 1;
201                         else local= 1;
202                 }
203
204         if(local && lib==0) {
205                 brush->id.lib= NULL;
206                 brush->id.flag= LIB_LOCAL;
207                 new_id(NULL, (ID *)brush, NULL);
208
209                 /* enable fake user by default */
210                 if (!(brush->id.flag & LIB_FAKEUSER)) {
211                         brush->id.flag |= LIB_FAKEUSER;
212                         brush->id.us++;
213                 }
214         }
215         else if(local && lib) {
216                 brushn= copy_brush(brush);
217                 brushn->id.us= 1; /* only keep fake user */
218                 brushn->id.flag |= LIB_FAKEUSER;
219                 
220                 for(scene= G.main->scene.first; scene; scene=scene->id.next)
221                         if(paint_brush(&scene->toolsettings->imapaint.paint)==brush)
222                                 if(scene->id.lib==NULL) {
223                                         paint_brush_set(&scene->toolsettings->imapaint.paint, brushn);
224                                         brushn->id.us++;
225                                         brush->id.us--;
226                                 }
227         }
228 }
229
230 void brush_debug_print_state(Brush *br)
231 {
232         /* create a fake brush and set it to the defaults */
233         Brush def= {{NULL}};
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==NULL) { /* 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, const int thread)
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, thread);
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);
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, 0);
550                                 }
551                                 else {
552                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
553
554                                         brush_sample_tex(brush, xy, rgba, 0);
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, 0);
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, 0);
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, 0);
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, 0);
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);
799         ibuf= cache->ibuf;
800
801         oldtexibuf= cache->texibuf;
802         cache->texibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag);
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         int use_jitter= brush->jitter != 0;
911
912         /* jitter-ed brush gives wierd and unpredictable result for this
913            kinds of stroke, so manyally disable jitter usage (sergey) */
914         use_jitter&= (brush->flag & (BRUSH_RESTORE_MESH|BRUSH_ANCHORED)) == 0;
915
916         if(use_jitter){
917                 float rand_pos[2];
918                 const int radius= brush_size(brush);
919                 const int diameter= 2*radius;
920
921                 // find random position within a circle of diameter 1
922                 do {
923                         rand_pos[0] = BLI_frand()-0.5f;
924                         rand_pos[1] = BLI_frand()-0.5f;
925                 } while (len_v2(rand_pos) > 0.5f);
926
927                 jitterpos[0] = pos[0] + 2*rand_pos[0]*diameter*brush->jitter;
928                 jitterpos[1] = pos[1] + 2*rand_pos[1]*diameter*brush->jitter;
929         }
930         else {
931                 VECCOPY2D(jitterpos, pos);
932         }
933 }
934
935 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user)
936 {
937         Brush *brush= painter->brush;
938         int totpaintops= 0;
939
940         if (pressure == 0.0f) {
941                 if(painter->lastpressure) // XXX - hack, operator misses
942                         pressure= painter->lastpressure;
943                 else
944                         pressure = 1.0f;        /* zero pressure == not using tablet */
945         }
946         if (painter->firsttouch) {
947                 /* paint exactly once on first touch */
948                 painter->startpaintpos[0]= pos[0];
949                 painter->startpaintpos[1]= pos[1];
950
951                 brush_apply_pressure(painter, brush, pressure);
952                 if (painter->cache.enabled)
953                         brush_painter_refresh_cache(painter, pos);
954                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
955                 
956                 painter->lasttime= time;
957                 painter->firsttouch= 0;
958                 painter->lastpaintpos[0]= pos[0];
959                 painter->lastpaintpos[1]= pos[1];
960         }
961 #if 0
962         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
963                 float spacing, step, paintpos[2], dmousepos[2], len;
964                 double starttime, curtime= time;
965
966                 /* compute brush spacing adapted to brush size */
967                 spacing= brush->rate; //radius*brush->spacing*0.01f;
968
969                 /* setup starting time, direction vector and accumulated time */
970                 starttime= painter->accumtime;
971                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
972                 len= normalize_v2(dmousepos);
973                 painter->accumtime += curtime - painter->lasttime;
974
975                 /* do paint op over unpainted time distance */
976                 while (painter->accumtime >= spacing) {
977                         step= (spacing - starttime)*len;
978                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
979                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
980
981                         if (painter->cache.enabled)
982                                 brush_painter_refresh_cache(painter);
983                         totpaintops += func(user, painter->cache.ibuf,
984                                 painter->lastpaintpos, paintpos);
985
986                         painter->lastpaintpos[0]= paintpos[0];
987                         painter->lastpaintpos[1]= paintpos[1];
988                         painter->accumtime -= spacing;
989                         starttime -= spacing;
990                 }
991                 
992                 painter->lasttime= curtime;
993         }
994 #endif
995         else {
996                 float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
997                 float t, len, press;
998                 const int radius= brush_size(brush);
999
1000                 /* compute brush spacing adapted to brush radius, spacing may depend
1001                    on pressure, so update it */
1002                 brush_apply_pressure(painter, brush, painter->lastpressure);
1003                 spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
1004
1005                 /* setup starting distance, direction vector and accumulated distance */
1006                 startdistance= painter->accumdistance;
1007                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
1008                 len= normalize_v2(dmousepos);
1009                 painter->accumdistance += len;
1010
1011                 /* do paint op over unpainted distance */
1012                 while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
1013                         step= spacing - startdistance;
1014                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
1015                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
1016
1017                         t = step/len;
1018                         press= (1.0f-t)*painter->lastpressure + t*pressure;
1019                         brush_apply_pressure(painter, brush, press);
1020                         spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
1021
1022                         brush_jitter_pos(brush, paintpos, finalpos);
1023
1024                         if (painter->cache.enabled)
1025                                 brush_painter_refresh_cache(painter, finalpos);
1026
1027                         totpaintops +=
1028                                 func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
1029
1030                         painter->lastpaintpos[0]= paintpos[0];
1031                         painter->lastpaintpos[1]= paintpos[1];
1032                         painter->accumdistance -= spacing;
1033                         startdistance -= spacing;
1034                 }
1035
1036                 /* do airbrush paint ops, based on the number of paint ops left over
1037                    from regular painting. this is a temporary solution until we have
1038                    accurate time stamps for mouse move events */
1039                 if (brush->flag & BRUSH_AIRBRUSH) {
1040                         double curtime= time;
1041                         double painttime= brush->rate*totpaintops;
1042
1043                         painter->accumtime += curtime - painter->lasttime;
1044                         if (painter->accumtime <= painttime)
1045                                 painter->accumtime= 0.0;
1046                         else
1047                                 painter->accumtime -= painttime;
1048
1049                         while (painter->accumtime >= brush->rate) {
1050                                 brush_apply_pressure(painter, brush, pressure);
1051
1052                                 brush_jitter_pos(brush, pos, finalpos);
1053
1054                                 if (painter->cache.enabled)
1055                                         brush_painter_refresh_cache(painter, finalpos);
1056
1057                                 totpaintops +=
1058                                         func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
1059                                 painter->accumtime -= brush->rate;
1060                         }
1061
1062                         painter->lasttime= curtime;
1063                 }
1064         }
1065
1066         painter->lastmousepos[0]= pos[0];
1067         painter->lastmousepos[1]= pos[1];
1068         painter->lastpressure= pressure;
1069
1070         brush_set_alpha(brush, painter->startalpha);
1071         brush_set_size(brush, painter->startsize);
1072         brush->jitter = painter->startjitter;
1073         brush->spacing = painter->startspacing;
1074
1075         return totpaintops;
1076 }
1077
1078 /* Uses the brush curve control to find a strength value between 0 and 1 */
1079 float brush_curve_strength_clamp(Brush *br, float p, const float len)
1080 {
1081         if(p >= len)    return 0;
1082         else                    p= p/len;
1083
1084         p= curvemapping_evaluateF(br->curve, 0, p);
1085         if(p < 0.0)                     p= 0.0f;
1086         else if(p > 1.0f)       p= 1.0f;
1087         return p;
1088 }
1089 /* same as above but can return negative values if the curve enables
1090  * used for sculpt only */
1091 float brush_curve_strength(Brush *br, float p, const float len)
1092 {
1093     if(p >= len)
1094         p= 1.0f;
1095     else
1096         p= p/len;
1097
1098     return curvemapping_evaluateF(br->curve, 0, p);
1099 }
1100
1101 /* TODO: should probably be unified with BrushPainter stuff? */
1102 unsigned int *brush_gen_texture_cache(Brush *br, int half_side)
1103 {
1104         unsigned int *texcache = NULL;
1105         MTex *mtex = &br->mtex;
1106         TexResult texres= {0};
1107         int hasrgb, ix, iy;
1108         int side = half_side * 2;
1109         
1110         if(mtex->tex) {
1111                 float x, y, step = 2.0 / side, co[3];
1112
1113                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1114
1115                 BKE_image_get_ibuf(mtex->tex->ima, NULL);
1116                 
1117                 /*do normalized cannonical view coords for texture*/
1118                 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
1119                         for (x=-1.0, ix=0; ix<side; ix++, x += step) {
1120                                 co[0]= x;
1121                                 co[1]= y;
1122                                 co[2]= 0.0f;
1123                                 
1124                                 /* This is copied from displace modifier code */
1125                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres);
1126                         
1127                                 /* if the texture gave an RGB value, we assume it didn't give a valid
1128                                  * intensity, so calculate one (formula from do_material_tex).
1129                                  * if the texture didn't give an RGB value, copy the intensity across
1130                                  */
1131                                 if(hasrgb & TEX_RGB)
1132                                         texres.tin = (0.35 * texres.tr + 0.45 *
1133                                                                   texres.tg + 0.2 * texres.tb);
1134
1135                                 texres.tin = texres.tin * 255.0;
1136                                 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
1137                                 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
1138                                 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
1139                                 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
1140                         }
1141                 }
1142         }
1143
1144         return texcache;
1145 }
1146
1147 /**** Radial Control ****/
1148 static struct ImBuf *brush_gen_radial_control_imbuf(Brush *br)
1149 {
1150         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1151         unsigned int *texcache;
1152         int side = 128;
1153         int half = side / 2;
1154         int i, j;
1155
1156         texcache = brush_gen_texture_cache(br, half);
1157         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1158         im->x = im->y = side;
1159
1160         for(i=0; i<side; ++i) {
1161                 for(j=0; j<side; ++j) {
1162                         float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
1163                         im->rect_float[i*side + j]= brush_curve_strength_clamp(br, magn, half);
1164                 }
1165         }
1166
1167         /* Modulate curve with texture */
1168         if(texcache) {
1169                 for(i=0; i<side; ++i) {
1170                         for(j=0; j<side; ++j) {
1171                                 const int col= texcache[i*side+j];
1172                                 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
1173                         }
1174                 }
1175
1176                 MEM_freeN(texcache);
1177         }
1178
1179         return im;
1180 }
1181
1182 void brush_radial_control_invoke(wmOperator *op, Brush *br, float size_weight)
1183 {
1184         int mode = RNA_int_get(op->ptr, "mode");
1185         float original_value= 0;
1186
1187         if(mode == WM_RADIALCONTROL_SIZE)
1188                 original_value = brush_size(br) * size_weight;
1189         else if(mode == WM_RADIALCONTROL_STRENGTH)
1190                 original_value = brush_alpha(br);
1191         else if(mode == WM_RADIALCONTROL_ANGLE) {
1192                 MTex *mtex = brush_active_texture(br);
1193                 if(mtex)
1194                         original_value = mtex->rot;
1195         }
1196
1197         RNA_float_set(op->ptr, "initial_value", original_value);
1198         op->customdata = brush_gen_radial_control_imbuf(br);
1199 }
1200
1201 int brush_radial_control_exec(wmOperator *op, Brush *br, float size_weight)
1202 {
1203         int mode = RNA_int_get(op->ptr, "mode");
1204         float new_value = RNA_float_get(op->ptr, "new_value");
1205         const float conv = 0.017453293;
1206
1207         if(mode == WM_RADIALCONTROL_SIZE)
1208                 if (brush_use_locked_size(br)) {
1209                         float initial_value = RNA_float_get(op->ptr, "initial_value");
1210                         const float unprojected_radius = brush_unprojected_radius(br);
1211                         brush_set_unprojected_radius(br, unprojected_radius * new_value/initial_value * size_weight);
1212                 }
1213                 else
1214                         brush_set_size(br, new_value * size_weight);
1215         else if(mode == WM_RADIALCONTROL_STRENGTH)
1216                 brush_set_alpha(br, new_value);
1217         else if(mode == WM_RADIALCONTROL_ANGLE) {
1218                 MTex *mtex = brush_active_texture(br);
1219                 if(mtex)
1220                         mtex->rot = new_value * conv;
1221         }
1222
1223         return OPERATOR_FINISHED;
1224 }
1225
1226 /* Unified Size and Strength */
1227
1228 static void set_unified_settings(Brush *brush, short flag, int value)
1229 {
1230         Scene *sce;
1231         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1232                 if (sce->toolsettings && 
1233                         ELEM4(brush,
1234                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1235                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1236                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1237                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1238                 {
1239                         if (value)
1240                                 sce->toolsettings->sculpt_paint_settings |= flag;
1241                         else
1242                                 sce->toolsettings->sculpt_paint_settings &= ~flag;
1243                 }
1244         }
1245 }
1246
1247 static short unified_settings(Brush *brush)
1248 {
1249         Scene *sce;
1250         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1251                 if (sce->toolsettings && 
1252                         ELEM4(brush,
1253                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1254                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1255                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1256                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1257                 {
1258                         return sce->toolsettings->sculpt_paint_settings;
1259                 }
1260         }
1261
1262         return 0;
1263 }
1264
1265 static void set_unified_size(Brush *brush, int value)
1266 {
1267         Scene *sce;
1268         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1269                 if (sce->toolsettings && 
1270                         ELEM4(brush,
1271                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1272                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1273                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1274                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1275                 {
1276                         sce->toolsettings->sculpt_paint_unified_size= value;
1277                 }
1278         }
1279 }
1280
1281 static int unified_size(Brush *brush)
1282 {
1283         Scene *sce;
1284         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1285                 if (sce->toolsettings && 
1286                         ELEM4(brush,
1287                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1288                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1289                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1290                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1291                 {
1292                         return sce->toolsettings->sculpt_paint_unified_size;
1293                 }
1294         }
1295
1296         return 35; // XXX magic number
1297 }
1298
1299 static void set_unified_alpha(Brush *brush, float value)
1300 {
1301         Scene *sce;
1302         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1303                 if (sce->toolsettings && 
1304                         ELEM4(brush,
1305                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1306                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1307                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1308                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1309                 {
1310                         sce->toolsettings->sculpt_paint_unified_alpha= value;
1311                 }
1312         }
1313 }
1314
1315 static float unified_alpha(Brush *brush)
1316 {
1317         Scene *sce;
1318         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1319                 if (sce->toolsettings && 
1320                         ELEM4(brush,
1321                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1322                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1323                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1324                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1325                 {
1326                         return sce->toolsettings->sculpt_paint_unified_alpha;
1327                 }
1328         }
1329
1330         return 0.5f; // XXX magic number
1331 }
1332
1333 static void set_unified_unprojected_radius(Brush *brush, float value)
1334 {
1335         Scene *sce;
1336         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1337                 if (sce->toolsettings && 
1338                         ELEM4(brush,
1339                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1340                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1341                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1342                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1343                 {
1344                         sce->toolsettings->sculpt_paint_unified_unprojected_radius= value;
1345                 }
1346         }
1347 }
1348
1349 static float unified_unprojected_radius(Brush *brush)
1350 {
1351         Scene *sce;
1352         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1353                 if (sce->toolsettings && 
1354                         ELEM4(brush,
1355                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1356                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1357                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1358                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1359                 {
1360                         return sce->toolsettings->sculpt_paint_unified_unprojected_radius;
1361                 }
1362         }
1363
1364         return 0.125f; // XXX magic number
1365 }
1366 void brush_set_size(Brush *brush, int size)
1367 {
1368         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE)
1369                 set_unified_size(brush, size);
1370         else
1371                 brush->size= size;
1372
1373         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1374 }
1375
1376 int brush_size(Brush *brush)
1377 {
1378         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? unified_size(brush) : brush->size;
1379 }
1380
1381 void brush_set_use_locked_size(Brush *brush, int value)
1382 {
1383         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) {
1384                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_LOCK_BRUSH_SIZE, value);
1385         }
1386         else {
1387                 if (value)
1388                         brush->flag |= BRUSH_LOCK_SIZE;
1389                 else
1390                         brush->flag &= ~BRUSH_LOCK_SIZE;
1391         }
1392
1393         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1394 }
1395
1396 int brush_use_locked_size(Brush *brush)
1397 {
1398         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_LOCK_BRUSH_SIZE) : (brush->flag & BRUSH_LOCK_SIZE);
1399 }
1400
1401 void brush_set_use_size_pressure(Brush *brush, int value)
1402 {
1403         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) {
1404                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_SIZE_PRESSURE, value);
1405         }
1406         else {
1407                 if (value)
1408                         brush->flag |= BRUSH_SIZE_PRESSURE;
1409                 else
1410                         brush->flag &= ~BRUSH_SIZE_PRESSURE;
1411         }
1412
1413         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1414 }
1415
1416 int brush_use_size_pressure(Brush *brush)
1417 {
1418         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_SIZE_PRESSURE) : (brush->flag & BRUSH_SIZE_PRESSURE);
1419 }
1420
1421 void brush_set_use_alpha_pressure(Brush *brush, int value)
1422 {
1423         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) {
1424                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_ALPHA_PRESSURE, value);
1425         }
1426         else {
1427                 if (value)
1428                         brush->flag |= BRUSH_ALPHA_PRESSURE;
1429                 else
1430                         brush->flag &= ~BRUSH_ALPHA_PRESSURE;
1431         }
1432
1433         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1434 }
1435
1436 int brush_use_alpha_pressure(Brush *brush)
1437 {
1438         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_ALPHA_PRESSURE) : (brush->flag & BRUSH_ALPHA_PRESSURE);
1439 }
1440
1441 void brush_set_unprojected_radius(Brush *brush, float unprojected_radius)
1442 {
1443         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE)
1444                 set_unified_unprojected_radius(brush, unprojected_radius);
1445         else
1446                 brush->unprojected_radius= unprojected_radius;
1447
1448         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1449 }
1450
1451 float brush_unprojected_radius(Brush *brush)
1452 {
1453         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? unified_unprojected_radius(brush) : brush->unprojected_radius;
1454 }
1455
1456 void brush_set_alpha(Brush *brush, float alpha)
1457 {
1458         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) 
1459                 set_unified_alpha(brush, alpha);
1460         else
1461                 brush->alpha= alpha;
1462
1463         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1464 }
1465
1466 float brush_alpha(Brush *brush)
1467 {
1468         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) ? unified_alpha(brush) : brush->alpha;
1469 }