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