code cleanup: function naming, use BKE_*type* prefix.
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/brush.c
29  *  \ingroup bke
30  */
31
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_brush_types.h"
39 #include "DNA_color_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_windowmanager_types.h"
43
44 #include "WM_types.h"
45
46 #include "RNA_access.h"
47
48 #include "BLI_bpath.h"
49 #include "BLI_math.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_rand.h"
52 #include "BLI_utildefines.h"
53
54 #include "BKE_brush.h"
55 #include "BKE_colortools.h"
56 #include "BKE_global.h"
57 #include "BKE_image.h"
58 #include "BKE_library.h"
59 #include "BKE_main.h"
60 #include "BKE_paint.h"
61 #include "BKE_texture.h"
62 #include "BKE_icons.h"
63
64 #include "IMB_imbuf.h"
65 #include "IMB_imbuf_types.h"
66
67 #include "RE_render_ext.h" /* externtex */
68 #include "RE_shader_ext.h"
69
70 static void brush_defaults(Brush *brush)
71 {
72         brush->blend = 0;
73         brush->flag = 0;
74
75         brush->ob_mode = OB_MODE_ALL_PAINT;
76
77         /* BRUSH SCULPT TOOL SETTINGS */
78         brush->weight= 1.0f; /* weight of brush 0 - 1.0 */
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 *BKE_brush_add(const char *name)
126 {
127         Brush *brush;
128
129         brush= BKE_libblock_alloc(&G.main->brush, ID_BR, name);
130
131         /* enable fake user by default */
132         brush->id.flag |= LIB_FAKEUSER;
133
134         brush_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         BKE_brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
140
141         return brush;
142 }
143
144 Brush *BKE_brush_copy(Brush *brush)
145 {
146         Brush *brushn;
147         
148         brushn= BKE_libblock_copy(&brush->id);
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 BKE_brush_free(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         id_lib_extern((ID *)brush->clone.image);
187 }
188
189 void BKE_brush_make_local(Brush *brush)
190 {
191
192         /* - only lib users: do nothing
193          * - only local users: set flag
194          * - mixed: make copy
195          */
196
197         Main *bmain= G.main;
198         Scene *scene;
199         int is_local= FALSE, is_lib= FALSE;
200
201         if (brush->id.lib==NULL) return;
202
203         if (brush->clone.image) {
204                 /* special case: ima always local immediately. Clone image should only
205                  * have one user anyway. */
206                 id_clear_lib_data(bmain, &brush->clone.image->id);
207                 extern_local_brush(brush);
208         }
209
210         for (scene= bmain->scene.first; scene && ELEM(0, is_lib, is_local); scene=scene->id.next) {
211                 if (paint_brush(&scene->toolsettings->imapaint.paint)==brush) {
212                         if (scene->id.lib) is_lib= TRUE;
213                         else is_local= TRUE;
214                 }
215         }
216
217         if (is_local && is_lib == FALSE) {
218                 id_clear_lib_data(bmain, &brush->id);
219                 extern_local_brush(brush);
220
221                 /* enable fake user by default */
222                 if (!(brush->id.flag & LIB_FAKEUSER)) {
223                         brush->id.flag |= LIB_FAKEUSER;
224                         brush->id.us++;
225                 }
226         }
227         else if (is_local && is_lib) {
228                 Brush *brush_new= BKE_brush_copy(brush);
229                 brush_new->id.us= 1; /* only keep fake user */
230                 brush_new->id.flag |= LIB_FAKEUSER;
231
232                 /* Remap paths of new ID using old library as base. */
233                 BKE_id_lib_local_paths(bmain, brush->id.lib, &brush_new->id);
234                 
235                 for (scene= bmain->scene.first; scene; scene=scene->id.next) {
236                         if (paint_brush(&scene->toolsettings->imapaint.paint)==brush) {
237                                 if (scene->id.lib==NULL) {
238                                         paint_brush_set(&scene->toolsettings->imapaint.paint, brush_new);
239                                 }
240                         }
241                 }
242         }
243 }
244
245 void BKE_brush_debug_print_state(Brush *br)
246 {
247         /* create a fake brush and set it to the defaults */
248         Brush def= {{NULL}};
249         brush_defaults(&def);
250         
251 #define BR_TEST(field, t)                                       \
252         if (br->field != def.field)                             \
253                 printf("br->" #field " = %" #t ";\n", br->field)
254
255 #define BR_TEST_FLAG(_f)                                \
256         if ((br->flag & _f) && !(def.flag & _f))                \
257                 printf("br->flag |= " #_f ";\n");       \
258         else if (!(br->flag & _f) && (def.flag & _f))   \
259                 printf("br->flag &= ~" #_f ";\n")
260         
261
262         /* print out any non-default brush state */
263         BR_TEST(normal_weight, f);
264
265         BR_TEST(blend, d);
266         BR_TEST(size, d);
267
268         /* br->flag */
269         BR_TEST_FLAG(BRUSH_AIRBRUSH);
270         BR_TEST_FLAG(BRUSH_TORUS);
271         BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
272         BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
273         BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
274         BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
275         BR_TEST_FLAG(BRUSH_FIXED_TEX);
276         BR_TEST_FLAG(BRUSH_RAKE);
277         BR_TEST_FLAG(BRUSH_ANCHORED);
278         BR_TEST_FLAG(BRUSH_DIR_IN);
279         BR_TEST_FLAG(BRUSH_SPACE);
280         BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
281         BR_TEST_FLAG(BRUSH_PERSISTENT);
282         BR_TEST_FLAG(BRUSH_ACCUMULATE);
283         BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
284         BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
285         BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
286         BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
287         BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
288         BR_TEST_FLAG(BRUSH_LOCK_SIZE);
289         BR_TEST_FLAG(BRUSH_TEXTURE_OVERLAY);
290         BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
291         BR_TEST_FLAG(BRUSH_RESTORE_MESH);
292         BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
293         BR_TEST_FLAG(BRUSH_RANDOM_ROTATION);
294         BR_TEST_FLAG(BRUSH_PLANE_TRIM);
295         BR_TEST_FLAG(BRUSH_FRONTFACE);
296         BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
297
298         BR_TEST(jitter, f);
299         BR_TEST(spacing, d);
300         BR_TEST(smooth_stroke_radius, d);
301         BR_TEST(smooth_stroke_factor, f);
302         BR_TEST(rate, f);
303
304         BR_TEST(alpha, f);
305
306         BR_TEST(sculpt_plane, d);
307
308         BR_TEST(plane_offset, f);
309
310         BR_TEST(autosmooth_factor, f);
311
312         BR_TEST(crease_pinch_factor, f);
313
314         BR_TEST(plane_trim, f);
315
316         BR_TEST(texture_sample_bias, f);
317         BR_TEST(texture_overlay_alpha, d);
318
319         BR_TEST(add_col[0], f);
320         BR_TEST(add_col[1], f);
321         BR_TEST(add_col[2], f);
322         BR_TEST(sub_col[0], f);
323         BR_TEST(sub_col[1], f);
324         BR_TEST(sub_col[2], f);
325
326         printf("\n");
327         
328 #undef BR_TEST
329 #undef BR_TEST_FLAG
330 }
331
332 void BKE_brush_sculpt_reset(Brush *br)
333 {
334         /* enable this to see any non-default
335          * settings used by a brush: */
336         // BKE_brush_debug_print_state(br);
337
338         brush_defaults(br);
339         BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
340
341         switch (br->sculpt_tool) {
342         case SCULPT_TOOL_CLAY:
343                 br->flag |= BRUSH_FRONTFACE;
344                 break;
345         case SCULPT_TOOL_CREASE:
346                 br->flag |= BRUSH_DIR_IN;
347                 br->alpha = 0.25;
348                 break;
349         case SCULPT_TOOL_FILL:
350                 br->add_col[1] = 1;
351                 br->sub_col[0] = 0.25;
352                 br->sub_col[1] = 1;
353                 break;
354         case SCULPT_TOOL_FLATTEN:
355                 br->add_col[1] = 1;
356                 br->sub_col[0] = 0.25;
357                 br->sub_col[1] = 1;
358                 break;
359         case SCULPT_TOOL_INFLATE:
360                 br->add_col[0] = 0.750000;
361                 br->add_col[1] = 0.750000;
362                 br->add_col[2] = 0.750000;
363                 br->sub_col[0] = 0.250000;
364                 br->sub_col[1] = 0.250000;
365                 br->sub_col[2] = 0.250000;
366                 break;
367         case SCULPT_TOOL_NUDGE:
368                 br->add_col[0] = 0.250000;
369                 br->add_col[1] = 1.000000;
370                 br->add_col[2] = 0.250000;
371                 break;
372         case SCULPT_TOOL_PINCH:
373                 br->add_col[0] = 0.750000;
374                 br->add_col[1] = 0.750000;
375                 br->add_col[2] = 0.750000;
376                 br->sub_col[0] = 0.250000;
377                 br->sub_col[1] = 0.250000;
378                 br->sub_col[2] = 0.250000;
379                 break;
380         case SCULPT_TOOL_SCRAPE:
381                 br->add_col[1] = 1.000000;
382                 br->sub_col[0] = 0.250000;
383                 br->sub_col[1] = 1.000000;
384                 break;
385         case SCULPT_TOOL_ROTATE:
386                 br->alpha = 1.0;
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 BKE_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 BKE_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 *)BKE_texture_copy((Tex *)id);
436                 else idtest= (ID *)add_texture("Tex");
437                 idtest->us--;
438         }
439         if (idtest!=id) {
440                 BKE_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 BKE_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 BKE_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                         BKE_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 BKE_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 BKE_brush_sample_tex(const Scene *scene, Brush *brush, const float xy[2], float rgba[4], 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= BKE_brush_size_get(scene, 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 {
518                 rgba[0]= rgba[1]= rgba[2]= rgba[3]= 1.0f;
519         }
520 }
521
522 /* TODO, use define for 'texfall' arg */
523 void BKE_brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall, int bufsize, ImBuf **outbuf, int use_color_correction)
524 {
525         ImBuf *ibuf;
526         float xy[2], rgba[4], *dstf;
527         int x, y, rowbytes, xoff, yoff, imbflag;
528         const int radius= BKE_brush_size_get(scene, brush);
529         unsigned char *dst, crgb[3];
530         const float alpha= BKE_brush_alpha_get(scene, brush);
531         float brush_rgb[3];
532     
533         imbflag= (flt)? IB_rectfloat: IB_rect;
534         xoff = -bufsize/2.0f + 0.5f;
535         yoff = -bufsize/2.0f + 0.5f;
536         rowbytes= bufsize*4;
537
538         if (*outbuf)
539                 ibuf= *outbuf;
540         else
541                 ibuf= IMB_allocImBuf(bufsize, bufsize, 32, imbflag);
542
543         if (flt) {
544                 copy_v3_v3(brush_rgb, brush->rgb);
545                 if (use_color_correction) {
546                         srgb_to_linearrgb_v3_v3(brush_rgb, brush_rgb);
547                 }
548
549                 for (y=0; y < ibuf->y; y++) {
550                         dstf = ibuf->rect_float + y*rowbytes;
551
552                         for (x=0; x < ibuf->x; x++, dstf+=4) {
553                                 xy[0] = x + xoff;
554                                 xy[1] = y + yoff;
555
556                                 if (texfall == 0) {
557                                         copy_v3_v3(dstf, brush_rgb);
558                                         dstf[3]= alpha*BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
559                                 }
560                                 else if (texfall == 1) {
561                                         BKE_brush_sample_tex(scene, brush, xy, dstf, 0);
562                                 }
563                                 else {
564                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
565                                         mul_v3_v3v3(dstf, rgba, brush_rgb);
566                                         dstf[3] = rgba[3]*alpha*BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
567                                 }
568                         }
569                 }
570         }
571         else {
572                 float alpha_f; /* final float alpha to convert to char */
573                 rgb_float_to_uchar(crgb, brush->rgb);
574
575                 for (y=0; y < ibuf->y; y++) {
576                         dst = (unsigned char *)ibuf->rect + y*rowbytes;
577
578                         for (x=0; x < ibuf->x; x++, dst+=4) {
579                                 xy[0] = x + xoff;
580                                 xy[1] = y + yoff;
581
582                                 if (texfall == 0) {
583                                         alpha_f = alpha * BKE_brush_curve_strength(brush, len_v2(xy), radius);
584
585                                         dst[0] = crgb[0];
586                                         dst[1] = crgb[1];
587                                         dst[2] = crgb[2];
588                                         dst[3] = FTOCHAR(alpha_f);
589                                 }
590                                 else if (texfall == 1) {
591                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
592                                         rgba_float_to_uchar(dst, rgba);
593                                 }
594                                 else if (texfall == 2) {
595                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
596                                         mul_v3_v3(rgba, brush->rgb);
597                                         alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
598
599                                         rgb_float_to_uchar(dst, rgba);
600
601                                         dst[3] = FTOCHAR(alpha_f);
602                                 }
603                                 else {
604                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
605                                         alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
606
607                                         dst[0] = crgb[0];
608                                         dst[1] = crgb[1];
609                                         dst[2] = crgb[2];
610                                         dst[3] = FTOCHAR(alpha_f);
611                                 }
612                         }
613                 }
614         }
615
616         *outbuf= ibuf;
617 }
618
619 /* Unified Size and Strength */
620
621 // XXX: be careful about setting size and unprojected radius
622 // because they depend on one another
623 // these functions do not set the other corresponding value
624 // this can lead to odd behavior if size and unprojected
625 // radius become inconsistent.
626 // the biggest problem is that it isn't possible to change
627 // unprojected radius because a view context is not
628 // available.  my ussual solution to this is to use the
629 // ratio of change of the size to change the unprojected
630 // radius.  Not completely convinced that is correct.
631 // In anycase, a better solution is needed to prevent
632 // inconsistency.
633
634 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
635 {
636         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
637
638         if (ups->flag & UNIFIED_PAINT_SIZE)
639                 ups->size= size;
640         else
641                 brush->size= size;
642 }
643
644 int BKE_brush_size_get(const Scene *scene, Brush *brush)
645 {
646         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
647
648         return (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
649 }
650
651 int BKE_brush_use_locked_size(const Scene *scene, Brush *brush)
652 {
653         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
654
655         return (us_flag & UNIFIED_PAINT_SIZE) ?
656                 (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
657                 (brush->flag & BRUSH_LOCK_SIZE);
658 }
659
660 int BKE_brush_use_size_pressure(const Scene *scene, Brush *brush)
661 {
662         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
663
664         return (us_flag & UNIFIED_PAINT_SIZE) ?
665                 (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
666                 (brush->flag & BRUSH_SIZE_PRESSURE);
667 }
668
669 int BKE_brush_use_alpha_pressure(const Scene *scene, Brush *brush)
670 {
671         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
672
673         return (us_flag & UNIFIED_PAINT_ALPHA) ?
674                 (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
675                 (brush->flag & BRUSH_ALPHA_PRESSURE);
676 }
677
678 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
679 {
680         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
681
682         if (ups->flag & UNIFIED_PAINT_SIZE)
683                 ups->unprojected_radius= unprojected_radius;
684         else
685                 brush->unprojected_radius= unprojected_radius;
686 }
687
688 float BKE_brush_unprojected_radius_get(const Scene *scene, Brush *brush)
689 {
690         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
691
692         return (ups->flag & UNIFIED_PAINT_SIZE) ?
693                 ups->unprojected_radius :
694                 brush->unprojected_radius;
695 }
696
697 static void brush_alpha_set(Scene *scene, Brush *brush, float alpha)
698 {
699         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
700
701         if (ups->flag & UNIFIED_PAINT_ALPHA)
702                 ups->alpha= alpha;
703         else
704                 brush->alpha= alpha;
705 }
706
707 float BKE_brush_alpha_get(const Scene *scene, Brush *brush)
708 {
709         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
710
711         return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
712 }
713
714 float BKE_brush_weight_get(const Scene *scene, Brush *brush)
715 {
716         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
717
718         return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
719 }
720
721 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
722 {
723         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
724
725         if(ups->flag & UNIFIED_PAINT_WEIGHT)
726                 ups->weight = value;
727         else
728                 brush->weight = value;
729 }
730
731 /* scale unprojected radius to reflect a change in the brush's 2D size */
732 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
733                                         int new_brush_size,
734                                         int old_brush_size)
735 {
736         float scale = new_brush_size;
737         /* avoid division by zero */
738         if (old_brush_size != 0)
739                 scale /= (float)old_brush_size;
740         (*unprojected_radius) *= scale;
741 }
742
743 /* scale brush size to reflect a change in the brush's unprojected radius */
744 void BKE_brush_scale_size(int *BKE_brush_size_get,
745                           float new_unprojected_radius,
746                           float old_unprojected_radius)
747 {
748         float scale = new_unprojected_radius;
749         /* avoid division by zero */
750         if (old_unprojected_radius != 0)
751                 scale /= new_unprojected_radius;
752         (*BKE_brush_size_get)= (int)((float)(*BKE_brush_size_get) * scale);
753 }
754
755 /* Brush Painting */
756
757 typedef struct BrushPainterCache {
758         short enabled;
759
760         int size;                       /* size override, if 0 uses 2*BKE_brush_size_get(brush) */
761         short flt;                      /* need float imbuf? */
762         short texonly;          /* no alpha, color or fallof, only texture in imbuf */
763
764         int lastsize;
765         float lastalpha;
766         float lastjitter;
767
768         ImBuf *ibuf;
769         ImBuf *texibuf;
770         ImBuf *maskibuf;
771 } BrushPainterCache;
772
773 struct BrushPainter {
774         Scene *scene;
775         Brush *brush;
776
777         float lastmousepos[2];  /* mouse position of last paint call */
778
779         float accumdistance;    /* accumulated distance of brush since last paint op */
780         float lastpaintpos[2];  /* position of last paint op */
781         float startpaintpos[2]; /* position of first paint */
782
783         double accumtime;               /* accumulated time since last paint op (airbrush) */
784         double lasttime;                /* time of last update */
785
786         float lastpressure;
787
788         short firsttouch;               /* first paint op */
789
790         float startsize;
791         float startalpha;
792         float startjitter;
793         float startspacing;
794
795         BrushPainterCache cache;
796 };
797
798 BrushPainter *BKE_brush_painter_new(Scene *scene, Brush *brush)
799 {
800         BrushPainter *painter= MEM_callocN(sizeof(BrushPainter), "BrushPainter");
801
802         painter->brush= brush;
803         painter->scene= scene;
804         painter->firsttouch= 1;
805         painter->cache.lastsize= -1; /* force ibuf create in refresh */
806
807         painter->startsize = BKE_brush_size_get(scene, brush);
808         painter->startalpha = BKE_brush_alpha_get(scene, brush);
809         painter->startjitter = brush->jitter;
810         painter->startspacing = brush->spacing;
811
812         return painter;
813 }
814
815 void BKE_brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
816 {
817         if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
818                 ((painter->cache.texonly != texonly) && texonly)) {
819                 if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
820                 if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
821                 painter->cache.ibuf= painter->cache.maskibuf= NULL;
822                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
823         }
824
825         if (painter->cache.flt != flt) {
826                 if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
827                 painter->cache.texibuf= NULL;
828                 painter->cache.lastsize= -1; /* force ibuf create in refresh */
829         }
830
831         painter->cache.size= size;
832         painter->cache.flt= flt;
833         painter->cache.texonly= texonly;
834         painter->cache.enabled= 1;
835 }
836
837 void BKE_brush_painter_free(BrushPainter *painter)
838 {
839         Brush *brush = painter->brush;
840
841         BKE_brush_size_set(painter->scene, brush, painter->startsize);
842         brush_alpha_set(painter->scene, brush, painter->startalpha);
843         brush->jitter = painter->startjitter;
844         brush->spacing = painter->startspacing;
845
846         if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
847         if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
848         if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
849         MEM_freeN(painter);
850 }
851
852 static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf,
853                                      int x, int y, int w, int h, int xt, int yt,
854                                      const float pos[2])
855 {
856         Scene *scene= painter->scene;
857         Brush *brush= painter->brush;
858         ImBuf *ibuf, *maskibuf, *texibuf;
859         float *bf, *mf, *tf, *otf=NULL, xoff, yoff, xy[2], rgba[4];
860         unsigned char *b, *m, *t, *ot= NULL;
861         int dotexold, origx= x, origy= y;
862         const int radius= BKE_brush_size_get(painter->scene, brush);
863
864         xoff = -radius + 0.5f;
865         yoff = -radius + 0.5f;
866         xoff += (int)pos[0] - (int)painter->startpaintpos[0];
867         yoff += (int)pos[1] - (int)painter->startpaintpos[1];
868
869         ibuf = painter->cache.ibuf;
870         texibuf = painter->cache.texibuf;
871         maskibuf = painter->cache.maskibuf;
872
873         dotexold = (oldtexibuf != NULL);
874
875         /* not sure if it's actually needed or it's a mistake in coords/sizes
876          * calculation in brush_painter_fixed_tex_partial_update(), but without this
877          * limitation memory gets corrupted at fast strokes with quite big spacing (sergey) */
878         w = MIN2(w, ibuf->x);
879         h = MIN2(h, ibuf->y);
880
881         if (painter->cache.flt) {
882                 for (; y < h; y++) {
883                         bf = ibuf->rect_float + (y*ibuf->x + origx)*4;
884                         tf = texibuf->rect_float + (y*texibuf->x + origx)*4;
885                         mf = maskibuf->rect_float + (y*maskibuf->x + origx)*4;
886
887                         if (dotexold)
888                                 otf = oldtexibuf->rect_float + ((y - origy + yt)*oldtexibuf->x + xt)*4;
889
890                         for (x=origx; x < w; x++, bf+=4, mf+=4, tf+=4) {
891                                 if (dotexold) {
892                                         copy_v3_v3(tf, otf);
893                                         tf[3] = otf[3];
894                                         otf += 4;
895                                 }
896                                 else {
897                                         xy[0] = x + xoff;
898                                         xy[1] = y + yoff;
899
900                                         BKE_brush_sample_tex(scene, brush, xy, tf, 0);
901                                 }
902
903                                 bf[0] = tf[0]*mf[0];
904                                 bf[1] = tf[1]*mf[1];
905                                 bf[2] = tf[2]*mf[2];
906                                 bf[3] = tf[3]*mf[3];
907                         }
908                 }
909         }
910         else {
911                 for (; y < h; y++) {
912                         b = (unsigned char *)ibuf->rect + (y*ibuf->x + origx)*4;
913                         t = (unsigned char *)texibuf->rect + (y*texibuf->x + origx)*4;
914                         m = (unsigned char *)maskibuf->rect + (y*maskibuf->x + origx)*4;
915
916                         if (dotexold)
917                                 ot = (unsigned char *)oldtexibuf->rect + ((y - origy + yt)*oldtexibuf->x + xt)*4;
918
919                         for (x=origx; x < w; x++, b+=4, m+=4, t+=4) {
920                                 if (dotexold) {
921                                         t[0] = ot[0];
922                                         t[1] = ot[1];
923                                         t[2] = ot[2];
924                                         t[3] = ot[3];
925                                         ot += 4;
926                                 }
927                                 else {
928                                         xy[0] = x + xoff;
929                                         xy[1] = y + yoff;
930
931                                         BKE_brush_sample_tex(scene, brush, xy, rgba, 0);
932                                         rgba_float_to_uchar(t, rgba);
933                                 }
934
935                                 b[0] = t[0]*m[0]/255;
936                                 b[1] = t[1]*m[1]/255;
937                                 b[2] = t[2]*m[2]/255;
938                                 b[3] = t[3]*m[3]/255;
939                         }
940                 }
941         }
942 }
943
944 static void brush_painter_fixed_tex_partial_update(BrushPainter *painter, const float pos[2])
945 {
946         const Scene *scene= painter->scene;
947         Brush *brush= painter->brush;
948         BrushPainterCache *cache= &painter->cache;
949         ImBuf *oldtexibuf, *ibuf;
950         int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
951         const int diameter= 2*BKE_brush_size_get(scene, brush);
952
953         imbflag= (cache->flt)? IB_rectfloat: IB_rect;
954         if (!cache->ibuf)
955                 cache->ibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag);
956         ibuf= cache->ibuf;
957
958         oldtexibuf= cache->texibuf;
959         cache->texibuf= IMB_allocImBuf(diameter, diameter, 32, imbflag);
960
961         if (oldtexibuf) {
962                 srcx= srcy= 0;
963                 destx= (int)painter->lastpaintpos[0] - (int)pos[0];
964                 desty= (int)painter->lastpaintpos[1] - (int)pos[1];
965                 w= oldtexibuf->x;
966                 h= oldtexibuf->y;
967
968                 IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
969         }
970         else {
971                 srcx= srcy= 0;
972                 destx= desty= 0;
973                 w= h= 0;
974         }
975         
976         x1= destx;
977         y1= desty;
978         x2= destx+w;
979         y2= desty+h;
980
981         /* blend existing texture in new position */
982         if ((x1 < x2) && (y1 < y2))
983                 brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
984
985         if (oldtexibuf)
986                 IMB_freeImBuf(oldtexibuf);
987
988         /* sample texture in new areas */
989         if ((0 < x1) && (0 < ibuf->y))
990                 brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
991         if ((x2 < ibuf->x) && (0 < ibuf->y))
992                 brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
993         if ((x1 < x2) && (0 < y1))
994                 brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
995         if ((x1 < x2) && (y2 < ibuf->y))
996                 brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
997 }
998
999 static void brush_painter_refresh_cache(BrushPainter *painter, const float pos[2], int use_color_correction)
1000 {
1001         const Scene *scene= painter->scene;
1002         Brush *brush= painter->brush;
1003         BrushPainterCache *cache= &painter->cache;
1004         MTex *mtex= &brush->mtex;
1005         int size;
1006         short flt;
1007         const int diameter= 2*BKE_brush_size_get(scene, brush);
1008         const float alpha= BKE_brush_alpha_get(scene, brush);
1009
1010         if (diameter != cache->lastsize ||
1011                 alpha != cache->lastalpha ||
1012                 brush->jitter != cache->lastjitter)
1013         {
1014                 if (cache->ibuf) {
1015                         IMB_freeImBuf(cache->ibuf);
1016                         cache->ibuf= NULL;
1017                 }
1018                 if (cache->maskibuf) {
1019                         IMB_freeImBuf(cache->maskibuf);
1020                         cache->maskibuf= NULL;
1021                 }
1022
1023                 flt= cache->flt;
1024                 size= (cache->size)? cache->size: diameter;
1025
1026                 if (brush->flag & BRUSH_FIXED_TEX) {
1027                         BKE_brush_imbuf_new(scene, brush, flt, 3, size, &cache->maskibuf, use_color_correction);
1028                         brush_painter_fixed_tex_partial_update(painter, pos);
1029                 }
1030                 else
1031                         BKE_brush_imbuf_new(scene, brush, flt, 2, size, &cache->ibuf, use_color_correction);
1032
1033                 cache->lastsize= diameter;
1034                 cache->lastalpha= alpha;
1035                 cache->lastjitter= brush->jitter;
1036         }
1037         else if ((brush->flag & BRUSH_FIXED_TEX) && mtex && mtex->tex) {
1038                 int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
1039                 int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
1040
1041                 if ((dx != 0) || (dy != 0))
1042                         brush_painter_fixed_tex_partial_update(painter, pos);
1043         }
1044 }
1045
1046 void BKE_brush_painter_break_stroke(BrushPainter *painter)
1047 {
1048         painter->firsttouch= 1;
1049 }
1050
1051 static void brush_pressure_apply(BrushPainter *painter, Brush *brush, float pressure)
1052 {
1053         if (BKE_brush_use_alpha_pressure(painter->scene, brush))
1054                 brush_alpha_set(painter->scene, brush, MAX2(0.0f, painter->startalpha*pressure));
1055         if (BKE_brush_use_size_pressure(painter->scene, brush))
1056                 BKE_brush_size_set(painter->scene, brush, MAX2(1.0f, painter->startsize*pressure));
1057         if (brush->flag & BRUSH_JITTER_PRESSURE)
1058                 brush->jitter = MAX2(0.0f, painter->startjitter*pressure);
1059         if (brush->flag & BRUSH_SPACING_PRESSURE)
1060                 brush->spacing = MAX2(1.0f, painter->startspacing*(1.5f-pressure));
1061 }
1062
1063 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
1064 {
1065         int use_jitter= brush->jitter != 0;
1066
1067         /* jitter-ed brush gives weird and unpredictable result for this
1068          * kinds of stroke, so manyally disable jitter usage (sergey) */
1069         use_jitter &= (brush->flag & (BRUSH_RESTORE_MESH|BRUSH_ANCHORED)) == 0;
1070
1071         if (use_jitter) {
1072                 float rand_pos[2];
1073                 const int radius= BKE_brush_size_get(scene, brush);
1074                 const int diameter= 2*radius;
1075
1076                 // find random position within a circle of diameter 1
1077                 do {
1078                         rand_pos[0] = BLI_frand()-0.5f;
1079                         rand_pos[1] = BLI_frand()-0.5f;
1080                 } while (len_v2(rand_pos) > 0.5f);
1081
1082                 jitterpos[0] = pos[0] + 2*rand_pos[0]*diameter*brush->jitter;
1083                 jitterpos[1] = pos[1] + 2*rand_pos[1]*diameter*brush->jitter;
1084         }
1085         else {
1086                 copy_v2_v2(jitterpos, pos);
1087         }
1088 }
1089
1090 int BKE_brush_painter_paint(BrushPainter *painter, BrushFunc func, const float pos[2], double time, float pressure,
1091                             void *user, int use_color_correction)
1092 {
1093         Scene *scene= painter->scene;
1094         Brush *brush= painter->brush;
1095         int totpaintops= 0;
1096
1097         if (pressure == 0.0f) {
1098                 if (painter->lastpressure) // XXX - hack, operator misses
1099                         pressure= painter->lastpressure;
1100                 else
1101                         pressure = 1.0f;        /* zero pressure == not using tablet */
1102         }
1103         if (painter->firsttouch) {
1104                 /* paint exactly once on first touch */
1105                 painter->startpaintpos[0]= pos[0];
1106                 painter->startpaintpos[1]= pos[1];
1107
1108                 brush_pressure_apply(painter, brush, pressure);
1109                 if (painter->cache.enabled)
1110                         brush_painter_refresh_cache(painter, pos, use_color_correction);
1111                 totpaintops += func(user, painter->cache.ibuf, pos, pos);
1112                 
1113                 painter->lasttime= time;
1114                 painter->firsttouch= 0;
1115                 painter->lastpaintpos[0]= pos[0];
1116                 painter->lastpaintpos[1]= pos[1];
1117         }
1118 #if 0
1119         else if (painter->brush->flag & BRUSH_AIRBRUSH) {
1120                 float spacing, step, paintpos[2], dmousepos[2], len;
1121                 double starttime, curtime= time;
1122
1123                 /* compute brush spacing adapted to brush size */
1124                 spacing= brush->rate; //radius*brush->spacing*0.01f;
1125
1126                 /* setup starting time, direction vector and accumulated time */
1127                 starttime= painter->accumtime;
1128                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
1129                 len= normalize_v2(dmousepos);
1130                 painter->accumtime += curtime - painter->lasttime;
1131
1132                 /* do paint op over unpainted time distance */
1133                 while (painter->accumtime >= spacing) {
1134                         step= (spacing - starttime)*len;
1135                         paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
1136                         paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
1137
1138                         if (painter->cache.enabled)
1139                                 brush_painter_refresh_cache(painter);
1140                         totpaintops += func(user, painter->cache.ibuf,
1141                                 painter->lastpaintpos, paintpos);
1142
1143                         painter->lastpaintpos[0]= paintpos[0];
1144                         painter->lastpaintpos[1]= paintpos[1];
1145                         painter->accumtime -= spacing;
1146                         starttime -= spacing;
1147                 }
1148                 
1149                 painter->lasttime= curtime;
1150         }
1151 #endif
1152         else {
1153                 float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
1154                 float t, len, press;
1155                 const int radius= BKE_brush_size_get(scene, brush);
1156
1157                 /* compute brush spacing adapted to brush radius, spacing may depend
1158                  * on pressure, so update it */
1159                 brush_pressure_apply(painter, brush, painter->lastpressure);
1160                 spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
1161
1162                 /* setup starting distance, direction vector and accumulated distance */
1163                 startdistance= painter->accumdistance;
1164                 sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
1165                 len= normalize_v2(dmousepos);
1166                 painter->accumdistance += len;
1167
1168                 if (brush->flag & BRUSH_SPACE) {
1169                         /* do paint op over unpainted distance */
1170                         while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
1171                                 step= spacing - startdistance;
1172                                 paintpos[0]= painter->lastmousepos[0] + dmousepos[0]*step;
1173                                 paintpos[1]= painter->lastmousepos[1] + dmousepos[1]*step;
1174
1175                                 t = step/len;
1176                                 press= (1.0f-t)*painter->lastpressure + t*pressure;
1177                                 brush_pressure_apply(painter, brush, press);
1178                                 spacing= MAX2(1.0f, radius)*brush->spacing*0.01f;
1179
1180                                 BKE_brush_jitter_pos(scene, brush, paintpos, finalpos);
1181
1182                                 if (painter->cache.enabled)
1183                                         brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1184
1185                                 totpaintops +=
1186                                         func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
1187
1188                                 painter->lastpaintpos[0]= paintpos[0];
1189                                 painter->lastpaintpos[1]= paintpos[1];
1190                                 painter->accumdistance -= spacing;
1191                                 startdistance -= spacing;
1192                         }
1193                 }
1194                 else {
1195                         BKE_brush_jitter_pos(scene, brush, pos, finalpos);
1196
1197                         if (painter->cache.enabled)
1198                                 brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1199
1200                         totpaintops += func(user, painter->cache.ibuf, pos, finalpos);
1201
1202                         painter->lastpaintpos[0]= pos[0];
1203                         painter->lastpaintpos[1]= pos[1];
1204                         painter->accumdistance= 0;
1205                 }
1206
1207                 /* do airbrush paint ops, based on the number of paint ops left over
1208                  * from regular painting. this is a temporary solution until we have
1209                  * accurate time stamps for mouse move events */
1210                 if (brush->flag & BRUSH_AIRBRUSH) {
1211                         double curtime= time;
1212                         double painttime= brush->rate*totpaintops;
1213
1214                         painter->accumtime += curtime - painter->lasttime;
1215                         if (painter->accumtime <= painttime)
1216                                 painter->accumtime= 0.0;
1217                         else
1218                                 painter->accumtime -= painttime;
1219
1220                         while (painter->accumtime >= (double)brush->rate) {
1221                                 brush_pressure_apply(painter, brush, pressure);
1222
1223                                 BKE_brush_jitter_pos(scene, brush, pos, finalpos);
1224
1225                                 if (painter->cache.enabled)
1226                                         brush_painter_refresh_cache(painter, finalpos, use_color_correction);
1227
1228                                 totpaintops +=
1229                                         func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
1230                                 painter->accumtime -= (double)brush->rate;
1231                         }
1232
1233                         painter->lasttime= curtime;
1234                 }
1235         }
1236
1237         painter->lastmousepos[0]= pos[0];
1238         painter->lastmousepos[1]= pos[1];
1239         painter->lastpressure= pressure;
1240
1241         brush_alpha_set(scene, brush, painter->startalpha);
1242         BKE_brush_size_set(scene, brush, painter->startsize);
1243         brush->jitter = painter->startjitter;
1244         brush->spacing = painter->startspacing;
1245
1246         return totpaintops;
1247 }
1248
1249 /* Uses the brush curve control to find a strength value between 0 and 1 */
1250 float BKE_brush_curve_strength_clamp(Brush *br, float p, const float len)
1251 {
1252         if (p >= len)   return 0;
1253         else                    p= p/len;
1254
1255         p= curvemapping_evaluateF(br->curve, 0, p);
1256         if (p < 0.0f)           p= 0.0f;
1257         else if (p > 1.0f)      p= 1.0f;
1258         return p;
1259 }
1260 /* same as above but can return negative values if the curve enables
1261  * used for sculpt only */
1262 float BKE_brush_curve_strength(Brush *br, float p, const float len)
1263 {
1264         if (p >= len)
1265                 p= 1.0f;
1266         else
1267                 p= p/len;
1268
1269         return curvemapping_evaluateF(br->curve, 0, p);
1270 }
1271
1272 /* TODO: should probably be unified with BrushPainter stuff? */
1273 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side)
1274 {
1275         unsigned int *texcache = NULL;
1276         MTex *mtex = &br->mtex;
1277         TexResult texres= {0};
1278         int hasrgb, ix, iy;
1279         int side = half_side * 2;
1280         
1281         if (mtex->tex) {
1282                 float x, y, step = 2.0 / side, co[3];
1283
1284                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1285
1286                 BKE_image_get_ibuf(mtex->tex->ima, NULL);
1287                 
1288                 /*do normalized cannonical view coords for texture*/
1289                 for (y=-1.0, iy=0; iy<side; iy++, y += step) {
1290                         for (x=-1.0, ix=0; ix<side; ix++, x += step) {
1291                                 co[0]= x;
1292                                 co[1]= y;
1293                                 co[2]= 0.0f;
1294                                 
1295                                 /* This is copied from displace modifier code */
1296                                 hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres);
1297                         
1298                                 /* if the texture gave an RGB value, we assume it didn't give a valid
1299                                  * intensity, so calculate one (formula from do_material_tex).
1300                                  * if the texture didn't give an RGB value, copy the intensity across
1301                                  */
1302                                 if (hasrgb & TEX_RGB)
1303                                         texres.tin = (0.35f * texres.tr + 0.45f *
1304                                                                   texres.tg + 0.2f * texres.tb);
1305
1306                                 texres.tin = texres.tin * 255.0f;
1307                                 ((char*)texcache)[(iy*side+ix)*4] = (char)texres.tin;
1308                                 ((char*)texcache)[(iy*side+ix)*4+1] = (char)texres.tin;
1309                                 ((char*)texcache)[(iy*side+ix)*4+2] = (char)texres.tin;
1310                                 ((char*)texcache)[(iy*side+ix)*4+3] = (char)texres.tin;
1311                         }
1312                 }
1313         }
1314
1315         return texcache;
1316 }
1317
1318 /**** Radial Control ****/
1319 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br)
1320 {
1321         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1322         unsigned int *texcache;
1323         int side = 128;
1324         int half = side / 2;
1325         int i, j;
1326
1327         texcache = BKE_brush_gen_texture_cache(br, half);
1328         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1329         im->x = im->y = side;
1330
1331         for (i=0; i<side; ++i) {
1332                 for (j=0; j<side; ++j) {
1333                         float magn= sqrt(pow(i - half, 2) + pow(j - half, 2));
1334                         im->rect_float[i*side + j]= BKE_brush_curve_strength_clamp(br, magn, half);
1335                 }
1336         }
1337
1338         /* Modulate curve with texture */
1339         if (texcache) {
1340                 for (i=0; i<side; ++i) {
1341                         for (j=0; j<side; ++j) {
1342                                 const int col= texcache[i*side+j];
1343                                 im->rect_float[i*side+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
1344                         }
1345                 }
1346
1347                 MEM_freeN(texcache);
1348         }
1349
1350         return im;
1351 }