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