svn merge -r 37212:37306 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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, int use_color_correction)
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         float brush_rgb[3];
533     
534         imbflag= (flt)? IB_rectfloat: IB_rect;
535         xoff = -bufsize/2.0f + 0.5f;
536         yoff = -bufsize/2.0f + 0.5f;
537         rowbytes= bufsize*4;
538
539         if (*outbuf)
540                 ibuf= *outbuf;
541         else
542                 ibuf= IMB_allocImBuf(bufsize, bufsize, 32, imbflag);
543
544         if (flt) {
545                 copy_v3_v3(brush_rgb, brush->rgb);
546                 if(use_color_correction){
547                         srgb_to_linearrgb_v3_v3(brush_rgb, brush_rgb);
548                 }
549
550                 for (y=0; y < ibuf->y; y++) {
551                         dstf = ibuf->rect_float + y*rowbytes;
552
553                         for (x=0; x < ibuf->x; x++, dstf+=4) {
554                                 xy[0] = x + xoff;
555                                 xy[1] = y + yoff;
556
557                                 if (texfall == 0) {
558                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
559
560                                         VECCOPY(dstf, brush_rgb);
561                                         dstf[3]= alpha*brush_curve_strength_clamp(brush, dist, radius);
562                                 }
563                                 else if (texfall == 1) {
564                                         brush_sample_tex(brush, xy, dstf, 0);
565                                 }
566                                 else {
567                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
568
569                                         brush_sample_tex(brush, xy, rgba, 0);
570                                         mul_v3_v3v3(dstf, rgba, brush_rgb);
571                                         dstf[3] = rgba[3]*alpha*brush_curve_strength_clamp(brush, dist, radius);
572                                 }
573                         }
574                 }
575         }
576         else {
577                 crgb[0]= FTOCHAR(brush->rgb[0]);
578                 crgb[1]= FTOCHAR(brush->rgb[1]);
579                 crgb[2]= FTOCHAR(brush->rgb[2]);
580
581                 for (y=0; y < ibuf->y; y++) {
582                         dst = (char*)ibuf->rect + y*rowbytes;
583
584                         for (x=0; x < ibuf->x; x++, dst+=4) {
585                                 xy[0] = x + xoff;
586                                 xy[1] = y + yoff;
587
588                                 if (texfall == 0) {
589                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
590
591                                         dst[0]= crgb[0];
592                                         dst[1]= crgb[1];
593                                         dst[2]= crgb[2];
594                                         dst[3]= FTOCHAR(alpha*brush_curve_strength(brush, dist, radius));
595                                 }
596                                 else if (texfall == 1) {
597                                         brush_sample_tex(brush, xy, rgba, 0);
598                                         dst[0]= FTOCHAR(rgba[0]);
599                                         dst[1]= FTOCHAR(rgba[1]);
600                                         dst[2]= FTOCHAR(rgba[2]);
601                                         dst[3]= FTOCHAR(rgba[3]);
602                                 }
603                                 else if (texfall == 2) {
604                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
605
606                                         brush_sample_tex(brush, xy, rgba, 0);
607                                         dst[0] = FTOCHAR(rgba[0]*brush->rgb[0]);
608                                         dst[1] = FTOCHAR(rgba[1]*brush->rgb[1]);
609                                         dst[2] = FTOCHAR(rgba[2]*brush->rgb[2]);
610                                         dst[3] = FTOCHAR(rgba[3]*alpha*brush_curve_strength_clamp(brush, dist, radius));
611                                 } else {
612                                         dist = sqrt(xy[0]*xy[0] + xy[1]*xy[1]);
613
614                                         brush_sample_tex(brush, xy, rgba, 0);
615                                         dst[0]= crgb[0];
616                                         dst[1]= crgb[1];
617                                         dst[2]= crgb[2];
618                                         dst[3] = FTOCHAR(rgba[3]*alpha*brush_curve_strength_clamp(brush, dist, radius));
619                                 }
620                         }
621                 }
622         }
623
624         *outbuf= ibuf;
625 }
626
627 /* Brush Painting */
628
629 typedef struct BrushPainterCache {
630         short enabled;
631
632         int size;                       /* size override, if 0 uses 2*brush_size(brush) */
633         short flt;                      /* need float imbuf? */
634         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
635
636         int lastsize;
637         float lastalpha;
638         float lastjitter;
639
640         ImBuf *ibuf;
641         ImBuf *texibuf;
642         ImBuf *maskibuf;
643 } BrushPainterCache;
644
645 struct BrushPainter {
646         Brush *brush;
647
648         float lastmousepos[2];  /* mouse position of last paint call */
649
650         float accumdistance;    /* accumulated distance of brush since last paint op */
651         float lastpaintpos[2];  /* position of last paint op */
652         float startpaintpos[2]; /* position of first paint */
653
654         double accumtime;               /* accumulated time since last paint op (airbrush) */
655         double lasttime;                /* time of last update */
656
657         float lastpressure;
658
659         short firsttouch;               /* first paint op */
660
661         float startsize;
662         float startalpha;
663         float startjitter;
664         float startspacing;
665
666         BrushPainterCache cache;
667 };
668
669 BrushPainter *brush_painter_new(Brush *brush)
670 {
671         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
672
673         painter->brush= brush;
674         painter->firsttouch= 1;
675         painter->cache.lastsize= -1; /* force ibuf create in refresh */
676
677         painter->startsize = brush_size(brush);
678         painter->startalpha = brush_alpha(brush);
679         painter->startjitter = brush->jitter;
680         painter->startspacing = brush->spacing;
681
682         return painter;
683 }
684
685 void brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
686 {
687         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
688                 ((painter->cache.texonly != texonly) && texonly)) {
689                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
690                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
691                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
692                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
693         }
694
695         if (painter->cache.flt != flt) {
696                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
697                 painter->cache.texibuf= NULL;
698                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
699         }
700
701         painter->cache.size= size;
702         painter->cache.flt= flt;
703         painter->cache.texonly= texonly;
704         painter->cache.enabled= 1;
705 }
706
707 void brush_painter_free(BrushPainter *painter)
708 {
709         Brush *brush = painter->brush;
710
711         brush_set_size(brush, painter->startsize);
712         brush_set_alpha(brush, painter->startalpha);
713         brush->jitter = painter->startjitter;
714         brush->spacing = painter->startspacing;
715
716         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
717         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
718         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
719         MEM_freeN(painter);
720 }
721
722 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf, int x, int y, int w, int h, int xt, int yt, float *pos)
723 {
724         Brush *brush= painter->brush;
725         ImBuf *ibuf, *maskibuf, *texibuf;
726         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
727         char *b, *m, *t, *ot= NULL;
728         int dotexold, origx= x, origy= y;
729         const int radius= brush_size(brush);
730
731         xoff = -radius + 0.5f;
732         yoff = -radius + 0.5f;
733         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
734         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
735
736         ibuf = painter->cache.ibuf;
737         texibuf = painter->cache.texibuf;
738         maskibuf = painter->cache.maskibuf;
739
740         dotexold = (oldtexibuf != NULL);
741
742         /* not sure if it's actually needed or it's a mistake in coords/sizes
743            calculation in brush_painter_fixed_tex_partial_update(), but without this
744            limitation memory gets corrupted at fast strokes with quite big spacing (sergey) */
745         w = MIN2(w, ibuf->x);
746         h = MIN2(h, ibuf->y);
747
748         if (painter->cache.flt) {
749                 for (; y < h; y++) {
750                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
751                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
752                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
753
754                         if (dotexold)
755                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
756
757                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
758                                 if (dotexold) {
759                                         VECCOPY(tf, otf);
760                                         tf[3] = otf[3];
761                                         otf += 4;
762                                 }
763                                 else {
764                                         xy[0] = x + xoff;
765                                         xy[1] = y + yoff;
766
767                                         brush_sample_tex(brush, xy, tf, 0);
768                                 }
769
770                                 bf[0] = tf[0]*mf[0];
771                                 bf[1] = tf[1]*mf[1];
772                                 bf[2] = tf[2]*mf[2];
773                                 bf[3] = tf[3]*mf[3];
774                         }
775                 }
776         }
777         else {
778                 for (; y < h; y++) {
779                         b = (char*)ibuf->rect + (y*ibuf->x + origx)*4;
780                         t = (char*)texibuf->rect + (y*texibuf->x + origx)*4;
781                         m = (char*)maskibuf->rect + (y*maskibuf->x + origx)*4;
782
783                         if (dotexold)
784                                 ot = (char*)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
785
786                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
787                                 if (dotexold) {
788                                         t[0] = ot[0];
789                                         t[1] = ot[1];
790                                         t[2] = ot[2];
791                                         t[3] = ot[3];
792                                         ot += 4;
793                                 }
794                                 else {
795                                         xy[0] = x + xoff;
796                                         xy[1] = y + yoff;
797
798                                         brush_sample_tex(brush, xy, rgba, 0);
799                                         t[0]= FTOCHAR(rgba[0]);
800                                         t[1]= FTOCHAR(rgba[1]);
801                                         t[2]= FTOCHAR(rgba[2]);
802                                         t[3]= FTOCHAR(rgba[3]);
803                                 }
804
805                                 b[0] = t[0]*m[0]/255;
806                                 b[1] = t[1]*m[1]/255;
807                                 b[2] = t[2]*m[2]/255;
808                                 b[3] = t[3]*m[3]/255;
809                         }
810                 }
811         }
812 }
813
814 static void brush_painter_fixed_tex_partial_update(BrushPainter *painter, float *pos)
815 {
816         Brush *brush= painter->brush;
817         BrushPainterCache *cache= &painter->cache;
818         ImBuf *oldtexibuf, *ibuf;
819         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
820         const int diameter= 2*brush_size(brush);
821
822         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
823         if (!cache->ibuf)
824                 cache->ibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag);
825         ibuf= cache->ibuf;
826
827         oldtexibuf= cache->texibuf;
828         cache->texibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag);
829
830         if (oldtexibuf) {
831                 srcx= srcy= 0;
832                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
833                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
834                 w= oldtexibuf->x;
835                 h= oldtexibuf->y;
836
837                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
838         }
839         else {
840                 srcx= srcy= 0;
841                 destx= desty= 0;
842                 w= h= 0;
843         }
844         
845         x1= destx;
846         y1= desty;
847         x2= destx+w;
848         y2= desty+h;
849
850         /* blend existing texture in new position */
851         if ((x1 < x2) && (y1 < y2))
852                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
853
854         if (oldtexibuf)
855                 IMB_freeImBuf(oldtexibuf);
856
857         /* sample texture in new areas */
858         if ((0 < x1) && (0 < ibuf->y))
859                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
860         if ((x2 < ibuf->x) && (0 < ibuf->y))
861                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
862         if ((x1 < x2) && (0 < y1))
863                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
864         if ((x1 < x2) && (y2 < ibuf->y))
865                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
866 }
867
868 static void brush_painter_refresh_cache(BrushPainter *painter, float *pos, int use_color_correction)
869 {
870         Brush *brush= painter->brush;
871         BrushPainterCache *cache= &painter->cache;
872         MTex *mtex= &brush->mtex;
873         int size;
874         short flt;
875         const int diameter= 2*brush_size(brush);
876         const float alpha= brush_alpha(brush);
877
878         if (diameter != cache->lastsize ||
879                 alpha != cache->lastalpha ||
880                 brush->jitter != cache->lastjitter)
881         {
882                 if (cache->ibuf) {
883                         IMB_freeImBuf(cache->ibuf);
884                         cache->ibuf= NULL;
885                 }
886                 if (cache->maskibuf) {
887                         IMB_freeImBuf(cache->maskibuf);
888                         cache->maskibuf= NULL;
889                 }
890
891                 flt= cache->flt;
892                 size= (cache->size)? cache->size: diameter;
893
894                 if (brush->flag & BRUSH_FIXED_TEX) {
895                         brush_imbuf_new(brush, flt, 3, size, &cache->maskibuf, use_color_correction);
896                         brush_painter_fixed_tex_partial_update(painter, pos);
897                 }
898                 else
899                         brush_imbuf_new(brush, flt, 2, size, &cache->ibuf, use_color_correction);
900
901                 cache->lastsize= diameter;
902                 cache->lastalpha= alpha;
903                 cache->lastjitter= brush->jitter;
904         }
905         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
906                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
907                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
908
909                 if ((dx != 0) || (dy != 0))
910                         brush_painter_fixed_tex_partial_update(painter, pos);
911         }
912 }
913
914 void brush_painter_break_stroke(BrushPainter *painter)
915 {
916         painter->firsttouch= 1;
917 }
918
919 static void brush_apply_pressure(BrushPainter *painter, Brush *brush, float pressure)
920 {
921         if (brush_use_alpha_pressure(brush)) 
922                 brush_set_alpha(brush, MAX2(0.0f, painter->startalpha*pressure));
923         if (brush_use_size_pressure(brush))
924                 brush_set_size(brush, MAX2(1.0f, painter->startsize*pressure));
925         if (brush->flag & BRUSH_JITTER_PRESSURE)
926                 brush->jitter = MAX2(0.0f, painter->startjitter*pressure);
927         if (brush->flag & BRUSH_SPACING_PRESSURE)
928                 brush->spacing = MAX2(1.0f, painter->startspacing*(1.5f-pressure));
929 }
930
931 void brush_jitter_pos(Brush *brush, float *pos, float *jitterpos)
932 {
933         int use_jitter= brush->jitter != 0;
934
935         /* jitter-ed brush gives wierd and unpredictable result for this
936            kinds of stroke, so manyally disable jitter usage (sergey) */
937         use_jitter&= (brush->flag & (BRUSH_RESTORE_MESH|BRUSH_ANCHORED)) == 0;
938
939         if(use_jitter){
940                 float rand_pos[2];
941                 const int radius= brush_size(brush);
942                 const int diameter= 2*radius;
943
944                 // find random position within a circle of diameter 1
945                 do {
946                         rand_pos[0] = BLI_frand()-0.5f;
947                         rand_pos[1] = BLI_frand()-0.5f;
948                 } while (len_v2(rand_pos) > 0.5f);
949
950                 jitterpos[0] = pos[0] + 2*rand_pos[0]*diameter*brush->jitter;
951                 jitterpos[1] = pos[1] + 2*rand_pos[1]*diameter*brush->jitter;
952         }
953         else {
954                 VECCOPY2D(jitterpos, pos);
955         }
956 }
957
958 int brush_painter_paint(BrushPainter *painter, BrushFunc func, float *pos, double time, float pressure, void *user, int use_color_correction)
959 {
960         Brush *brush= painter->brush;
961         int totpaintops= 0;
962
963         if (pressure == 0.0f) {
964                 if(painter->lastpressure) // XXX - hack, operator misses
965                         pressure= painter->lastpressure;
966                 else
967                         pressure = 1.0f;        /* zero pressure == not using tablet */
968         }
969         if (painter->firsttouch) {
970                 /* paint exactly once on first touch */
971                 painter->startpaintpos[0]= pos[0];
972                 painter->startpaintpos[1]= pos[1];
973
974                 brush_apply_pressure(painter, brush, pressure);
975                 if (painter->cache.enabled)
976                         brush_painter_refresh_cache(painter, pos, use_color_correction);
977                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
978                 
979                 painter->lasttime= time;
980                 painter->firsttouch= 0;
981                 painter->lastpaintpos[0]= pos[0];
982                 painter->lastpaintpos[1]= pos[1];
983         }
984 #if 0
985         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
986                 float spacing, step, paintpos[2], dmousepos[2], len;
987                 double starttime, curtime= time;
988
989                 /* compute brush spacing adapted to brush size */
990                 spacing= brush->rate; //radius*brush->spacing*0.01f;
991
992                 /* setup starting time, direction vector and accumulated time */
993                 starttime= painter->accumtime;
994                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
995                 len= normalize_v2(dmousepos);
996                 painter->accumtime += curtime - painter->lasttime;
997
998                 /* do paint op over unpainted time distance */
999                 while (painter->accumtime >= spacing) {
1000                         step= (spacing - starttime)*len;
1001                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
1002                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
1003
1004                         if (painter->cache.enabled)
1005                                 brush_painter_refresh_cache(painter);
1006                         totpaintops += func(user, painter->cache.ibuf,
1007                                 painter->lastpaintpos, paintpos);
1008
1009                         painter->lastpaintpos[0]= paintpos[0];
1010                         painter->lastpaintpos[1]= paintpos[1];
1011                         painter->accumtime -= spacing;
1012                         starttime -= spacing;
1013                 }
1014                 
1015                 painter->lasttime= curtime;
1016         }
1017 #endif
1018         else {
1019                 float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
1020                 float t, len, press;
1021                 const int radius= brush_size(brush);
1022
1023                 /* compute brush spacing adapted to brush radius, spacing may depend
1024                    on pressure, so update it */
1025                 brush_apply_pressure(painter, brush, painter->lastpressure);
1026                 spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
1027
1028                 /* setup starting distance, direction vector and accumulated distance */
1029                 startdistance= painter->accumdistance;
1030                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
1031                 len= normalize_v2(dmousepos);
1032                 painter->accumdistance += len;
1033
1034                 if (brush->flag & BRUSH_SPACE) {
1035                         /* do paint op over unpainted distance */
1036                         while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
1037                                 step= spacing - startdistance;
1038                                 paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
1039                                 paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
1040
1041                                 t = step/len;
1042                                 press= (1.0f-t)*painter->lastpressure + t*pressure;
1043                                 brush_apply_pressure(painter, brush, press);
1044                                 spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
1045
1046                                 brush_jitter_pos(brush, paintpos, finalpos);
1047
1048                                 if (painter->cache.enabled)
1049                                         brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1050
1051                                 totpaintops +=
1052                                         func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
1053
1054                                 painter->lastpaintpos[0]= paintpos[0];
1055                                 painter->lastpaintpos[1]= paintpos[1];
1056                                 painter->accumdistance -= spacing;
1057                                 startdistance -= spacing;
1058                         }
1059                 } else {
1060                         brush_jitter_pos(brush, pos, finalpos);
1061
1062                         if (painter->cache.enabled)
1063                                 brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1064
1065                         totpaintops += func(user, painter->cache.ibuf, pos, finalpos);
1066
1067                         painter->lastpaintpos[0]= pos[0];
1068                         painter->lastpaintpos[1]= pos[1];
1069                         painter->accumdistance= 0;
1070                 }
1071
1072                 /* do airbrush paint ops, based on the number of paint ops left over
1073                    from regular painting. this is a temporary solution until we have
1074                    accurate time stamps for mouse move events */
1075                 if (brush->flag & BRUSH_AIRBRUSH) {
1076                         double curtime= time;
1077                         double painttime= brush->rate*totpaintops;
1078
1079                         painter->accumtime += curtime - painter->lasttime;
1080                         if (painter->accumtime <= painttime)
1081                                 painter->accumtime= 0.0;
1082                         else
1083                                 painter->accumtime -= painttime;
1084
1085                         while (painter->accumtime >= (double)brush->rate) {
1086                                 brush_apply_pressure(painter, brush, pressure);
1087
1088                                 brush_jitter_pos(brush, pos, finalpos);
1089
1090                                 if (painter->cache.enabled)
1091                                         brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1092
1093                                 totpaintops +=
1094                                         func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
1095                                 painter->accumtime -= (double)brush->rate;
1096                         }
1097
1098                         painter->lasttime= curtime;
1099                 }
1100         }
1101
1102         painter->lastmousepos[0]= pos[0];
1103         painter->lastmousepos[1]= pos[1];
1104         painter->lastpressure= pressure;
1105
1106         brush_set_alpha(brush, painter->startalpha);
1107         brush_set_size(brush, painter->startsize);
1108         brush->jitter = painter->startjitter;
1109         brush->spacing = painter->startspacing;
1110
1111         return totpaintops;
1112 }
1113
1114 /* Uses the brush curve control to find a strength value between 0 and 1 */
1115 float brush_curve_strength_clamp(Brush *br, float p, const float len)
1116 {
1117         if(p >= len)    return 0;
1118         else                    p= p/len;
1119
1120         p= curvemapping_evaluateF(br->curve, 0, p);
1121         if(p < 0.0f)            p= 0.0f;
1122         else if(p > 1.0f)       p= 1.0f;
1123         return p;
1124 }
1125 /* same as above but can return negative values if the curve enables
1126  * used for sculpt only */
1127 float brush_curve_strength(Brush *br, float p, const float len)
1128 {
1129         if(p >= len)
1130                 p= 1.0f;
1131         else
1132                 p= p/len;
1133
1134         return curvemapping_evaluateF(br->curve, 0, p);
1135 }
1136
1137 /* TODO: should probably be unified with BrushPainter stuff? */
1138 unsigned int *brush_gen_texture_cache(Brush *br, int half_side)
1139 {
1140         unsigned int *texcache = NULL;
1141         MTex *mtex = &br->mtex;
1142         TexResult texres= {0};
1143         int hasrgb, ix, iy;
1144         int side = half_side * 2;
1145         
1146         if(mtex->tex) {
1147                 float x, y, step = 2.0 / side, co[3];
1148
1149                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1150
1151                 BKE_image_get_ibuf(mtex->tex->ima, NULL);
1152                 
1153                 /*do normalized cannonical view coords for texture*/
1154                 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
1155                         for (x=-1.0, ix=0; ix<side; ix++, x += step) {
1156                                 co[0]= x;
1157                                 co[1]= y;
1158                                 co[2]= 0.0f;
1159                                 
1160                                 /* This is copied from displace modifier code */
1161                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres);
1162                         
1163                                 /* if the texture gave an RGB value, we assume it didn't give a valid
1164                                  * intensity, so calculate one (formula from do_material_tex).
1165                                  * if the texture didn't give an RGB value, copy the intensity across
1166                                  */
1167                                 if(hasrgb & TEX_RGB)
1168                                         texres.tin = (0.35f * texres.tr + 0.45f *
1169                                                                   texres.tg + 0.2f * texres.tb);
1170
1171                                 texres.tin = texres.tin * 255.0f;
1172                                 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
1173                                 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
1174                                 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
1175                                 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
1176                         }
1177                 }
1178         }
1179
1180         return texcache;
1181 }
1182
1183 /**** Radial Control ****/
1184 struct ImBuf *brush_gen_radial_control_imbuf(Brush *br)
1185 {
1186         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1187         unsigned int *texcache;
1188         int side = 128;
1189         int half = side / 2;
1190         int i, j;
1191
1192         texcache = brush_gen_texture_cache(br, half);
1193         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1194         im->x = im->y = side;
1195
1196         for(i=0; i<side; ++i) {
1197                 for(j=0; j<side; ++j) {
1198                         float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
1199                         im->rect_float[i*side + j]= brush_curve_strength_clamp(br, magn, half);
1200                 }
1201         }
1202
1203         /* Modulate curve with texture */
1204         if(texcache) {
1205                 for(i=0; i<side; ++i) {
1206                         for(j=0; j<side; ++j) {
1207                                 const int col= texcache[i*side+j];
1208                                 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
1209                         }
1210                 }
1211
1212                 MEM_freeN(texcache);
1213         }
1214
1215         return im;
1216 }
1217
1218 /* Unified Size and Strength */
1219
1220 static void set_unified_settings(Brush *brush, short flag, int value)
1221 {
1222         Scene *sce;
1223         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1224                 if (sce->toolsettings && 
1225                         ELEM4(brush,
1226                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1227                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1228                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1229                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1230                 {
1231                         if (value)
1232                                 sce->toolsettings->sculpt_paint_settings |= flag;
1233                         else
1234                                 sce->toolsettings->sculpt_paint_settings &= ~flag;
1235                 }
1236         }
1237 }
1238
1239 static short unified_settings(Brush *brush)
1240 {
1241         Scene *sce;
1242         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1243                 if (sce->toolsettings && 
1244                         ELEM4(brush,
1245                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1246                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1247                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1248                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1249                 {
1250                         return sce->toolsettings->sculpt_paint_settings;
1251                 }
1252         }
1253
1254         return 0;
1255 }
1256
1257 // XXX: be careful about setting size and unprojected radius
1258 // because they depend on one another
1259 // these functions do not set the other corresponding value
1260 // this can lead to odd behavior if size and unprojected
1261 // radius become inconsistent.
1262 // the biggest problem is that it isn't possible to change
1263 // unprojected radius because a view context is not
1264 // available.  my ussual solution to this is to use the
1265 // ratio of change of the size to change the unprojected
1266 // radius.  Not completely convinced that is correct.
1267 // In anycase, a better solution is needed to prevent
1268 // inconsistency.
1269
1270 static void set_unified_size(Brush *brush, int value)
1271 {
1272         Scene *sce;
1273         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1274                 if (sce->toolsettings && 
1275                         ELEM4(brush,
1276                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1277                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1278                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1279                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1280                 {
1281                         sce->toolsettings->sculpt_paint_unified_size= value;
1282                 }
1283         }
1284 }
1285
1286 static int unified_size(Brush *brush)
1287 {
1288         Scene *sce;
1289         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1290                 if (sce->toolsettings && 
1291                         ELEM4(brush,
1292                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1293                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1294                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1295                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1296                 {
1297                         return sce->toolsettings->sculpt_paint_unified_size;
1298                 }
1299         }
1300
1301         return 35; // XXX magic number
1302 }
1303
1304 static void set_unified_alpha(Brush *brush, float value)
1305 {
1306         Scene *sce;
1307         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1308                 if (sce->toolsettings && 
1309                         ELEM4(brush,
1310                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1311                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1312                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1313                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1314                 {
1315                         sce->toolsettings->sculpt_paint_unified_alpha= value;
1316                 }
1317         }
1318 }
1319
1320 static float unified_alpha(Brush *brush)
1321 {
1322         Scene *sce;
1323         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1324                 if (sce->toolsettings && 
1325                         ELEM4(brush,
1326                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1327                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1328                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1329                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1330                 {
1331                         return sce->toolsettings->sculpt_paint_unified_alpha;
1332                 }
1333         }
1334
1335         return 0.5f; // XXX magic number
1336 }
1337
1338 static void set_unified_unprojected_radius(Brush *brush, float value)
1339 {
1340         Scene *sce;
1341         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1342                 if (sce->toolsettings && 
1343                         ELEM4(brush,
1344                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1345                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1346                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1347                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1348                 {
1349                         sce->toolsettings->sculpt_paint_unified_unprojected_radius= value;
1350                 }
1351         }
1352 }
1353
1354 static float unified_unprojected_radius(Brush *brush)
1355 {
1356         Scene *sce;
1357         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1358                 if (sce->toolsettings && 
1359                         ELEM4(brush,
1360                             paint_brush(&(sce->toolsettings->imapaint.paint)),
1361                             paint_brush(&(sce->toolsettings->vpaint->paint)),
1362                             paint_brush(&(sce->toolsettings->wpaint->paint)),
1363                             paint_brush(&(sce->toolsettings->sculpt->paint))))
1364                 {
1365                         return sce->toolsettings->sculpt_paint_unified_unprojected_radius;
1366                 }
1367         }
1368
1369         return 0.125f; // XXX magic number
1370 }
1371 void brush_set_size(Brush *brush, int size)
1372 {
1373         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE)
1374                 set_unified_size(brush, size);
1375         else
1376                 brush->size= size;
1377
1378         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1379 }
1380
1381 int brush_size(Brush *brush)
1382 {
1383         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? unified_size(brush) : brush->size;
1384 }
1385
1386 void brush_set_use_locked_size(Brush *brush, int value)
1387 {
1388         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) {
1389                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_LOCK_BRUSH_SIZE, value);
1390         }
1391         else {
1392                 if (value)
1393                         brush->flag |= BRUSH_LOCK_SIZE;
1394                 else
1395                         brush->flag &= ~BRUSH_LOCK_SIZE;
1396         }
1397
1398         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1399 }
1400
1401 int brush_use_locked_size(Brush *brush)
1402 {
1403         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_LOCK_BRUSH_SIZE) : (brush->flag & BRUSH_LOCK_SIZE);
1404 }
1405
1406 void brush_set_use_size_pressure(Brush *brush, int value)
1407 {
1408         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) {
1409                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_SIZE_PRESSURE, value);
1410         }
1411         else {
1412                 if (value)
1413                         brush->flag |= BRUSH_SIZE_PRESSURE;
1414                 else
1415                         brush->flag &= ~BRUSH_SIZE_PRESSURE;
1416         }
1417
1418         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1419 }
1420
1421 int brush_use_size_pressure(Brush *brush)
1422 {
1423         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_SIZE_PRESSURE) : (brush->flag & BRUSH_SIZE_PRESSURE);
1424 }
1425
1426 void brush_set_use_alpha_pressure(Brush *brush, int value)
1427 {
1428         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) {
1429                 set_unified_settings(brush, SCULPT_PAINT_UNIFIED_ALPHA_PRESSURE, value);
1430         }
1431         else {
1432                 if (value)
1433                         brush->flag |= BRUSH_ALPHA_PRESSURE;
1434                 else
1435                         brush->flag &= ~BRUSH_ALPHA_PRESSURE;
1436         }
1437
1438         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1439 }
1440
1441 int brush_use_alpha_pressure(Brush *brush)
1442 {
1443         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) ? (unified_settings(brush) & SCULPT_PAINT_UNIFIED_ALPHA_PRESSURE) : (brush->flag & BRUSH_ALPHA_PRESSURE);
1444 }
1445
1446 void brush_set_unprojected_radius(Brush *brush, float unprojected_radius)
1447 {
1448         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE)
1449                 set_unified_unprojected_radius(brush, unprojected_radius);
1450         else
1451                 brush->unprojected_radius= unprojected_radius;
1452
1453         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1454 }
1455
1456 float brush_unprojected_radius(Brush *brush)
1457 {
1458         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_SIZE) ? unified_unprojected_radius(brush) : brush->unprojected_radius;
1459 }
1460
1461 void brush_set_alpha(Brush *brush, float alpha)
1462 {
1463         if (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) 
1464                 set_unified_alpha(brush, alpha);
1465         else
1466                 brush->alpha= alpha;
1467
1468         //WM_main_add_notifier(NC_BRUSH|NA_EDITED, brush);
1469 }
1470
1471 float brush_alpha(Brush *brush)
1472 {
1473         return (unified_settings(brush) & SCULPT_PAINT_USE_UNIFIED_ALPHA) ? unified_alpha(brush) : brush->alpha;
1474 }