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