Cleanup: unused headers
[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  * Contributor(s): none yet.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/blenkernel/intern/brush.c
24  *  \ingroup bke
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "DNA_brush_types.h"
30 #include "DNA_scene_types.h"
31 #include "DNA_object_types.h"
32
33 #include "BLI_math.h"
34 #include "BLI_blenlib.h"
35 #include "BLI_rand.h"
36
37 #include "BKE_brush.h"
38 #include "BKE_colortools.h"
39 #include "BKE_global.h"
40 #include "BKE_library.h"
41 #include "BKE_main.h"
42 #include "BKE_paint.h"
43 #include "BKE_texture.h"
44 #include "BKE_icons.h"
45
46 #include "IMB_colormanagement.h"
47 #include "IMB_imbuf.h"
48 #include "IMB_imbuf_types.h"
49
50 #include "RE_render_ext.h" /* externtex */
51
52 static RNG *brush_rng;
53
54 void BKE_brush_system_init(void)
55 {
56         brush_rng = BLI_rng_new(0);
57         BLI_rng_srandom(brush_rng, 31415682);
58 }
59
60 void BKE_brush_system_exit(void)
61 {
62         BLI_rng_free(brush_rng);
63 }
64
65
66 static void brush_defaults(Brush *brush)
67 {
68         brush->blend = 0;
69         brush->flag = 0;
70
71         brush->ob_mode = OB_MODE_ALL_PAINT;
72
73         /* BRUSH SCULPT TOOL SETTINGS */
74         brush->weight = 1.0f; /* weight of brush 0 - 1.0 */
75         brush->size = 35; /* radius of the brush in pixels */
76         brush->alpha = 0.5f; /* brush strength/intensity probably variable should be renamed? */
77         brush->autosmooth_factor = 0.0f;
78         brush->crease_pinch_factor = 0.5f;
79         brush->sculpt_plane = SCULPT_DISP_DIR_AREA;
80         brush->plane_offset = 0.0f; /* how far above or below the plane that is found by averaging the faces */
81         brush->plane_trim = 0.5f;
82         brush->clone.alpha = 0.5f;
83         brush->normal_weight = 0.0f;
84         brush->fill_threshold = 0.2f;
85         brush->flag |= BRUSH_ALPHA_PRESSURE;
86
87         /* BRUSH PAINT TOOL SETTINGS */
88         brush->rgb[0] = 1.0f; /* default rgb color of the brush when painting - white */
89         brush->rgb[1] = 1.0f;
90         brush->rgb[2] = 1.0f;
91
92         zero_v3(brush->secondary_rgb);
93
94         /* BRUSH STROKE SETTINGS */
95         brush->flag |= (BRUSH_SPACE | BRUSH_SPACE_ATTEN);
96         brush->spacing = 10; /* how far each brush dot should be spaced as a percentage of brush diameter */
97
98         brush->smooth_stroke_radius = 75;
99         brush->smooth_stroke_factor = 0.9f;
100
101         brush->rate = 0.1f; /* time delay between dots of paint or sculpting when doing airbrush mode */
102
103         brush->jitter = 0.0f;
104
105         /* BRUSH TEXTURE SETTINGS */
106         default_mtex(&brush->mtex);
107         default_mtex(&brush->mask_mtex);
108
109         brush->texture_sample_bias = 0; /* value to added to texture samples */
110         brush->texture_overlay_alpha = 33;
111         brush->mask_overlay_alpha = 33;
112         brush->cursor_overlay_alpha = 33;
113         brush->overlay_flags = 0;
114
115         /* brush appearance  */
116
117         brush->add_col[0] = 1.00; /* add mode color is light red */
118         brush->add_col[1] = 0.39;
119         brush->add_col[2] = 0.39;
120
121         brush->sub_col[0] = 0.39; /* subtract mode color is light blue */
122         brush->sub_col[1] = 0.39;
123         brush->sub_col[2] = 1.00;
124
125         brush->stencil_pos[0] = 256;
126         brush->stencil_pos[1] = 256;
127
128         brush->stencil_dimension[0] = 256;
129         brush->stencil_dimension[1] = 256;
130 }
131
132 /* Datablock add/copy/free/make_local */
133
134 Brush *BKE_brush_add(Main *bmain, const char *name)
135 {
136         Brush *brush;
137
138         brush = BKE_libblock_alloc(bmain, ID_BR, name);
139
140         /* enable fake user by default */
141         brush->id.flag |= LIB_FAKEUSER;
142
143         brush_defaults(brush);
144
145         brush->sculpt_tool = SCULPT_TOOL_DRAW; /* sculpting defaults to the draw tool for new brushes */
146
147         /* the default alpha falloff curve */
148         BKE_brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
149
150         return brush;
151 }
152
153 Brush *BKE_brush_copy(Brush *brush)
154 {
155         Brush *brushn;
156         
157         brushn = BKE_libblock_copy(&brush->id);
158
159         if (brush->mtex.tex)
160                 id_us_plus((ID *)brush->mtex.tex);
161
162         if (brush->mask_mtex.tex)
163                 id_us_plus((ID *)brush->mask_mtex.tex);
164
165         if (brush->paint_curve)
166                 id_us_plus((ID *)brush->paint_curve);
167
168         if (brush->icon_imbuf)
169                 brushn->icon_imbuf = IMB_dupImBuf(brush->icon_imbuf);
170
171         brushn->preview = NULL;
172
173         brushn->curve = curvemapping_copy(brush->curve);
174
175         /* enable fake user by default */
176         if (!(brushn->id.flag & LIB_FAKEUSER)) {
177                 brushn->id.flag |= LIB_FAKEUSER;
178                 brushn->id.us++;
179         }
180         
181         return brushn;
182 }
183
184 /* not brush itself */
185 void BKE_brush_free(Brush *brush)
186 {
187         id_us_min((ID *)brush->mtex.tex);
188         id_us_min((ID *)brush->mask_mtex.tex);
189         id_us_min((ID *)brush->paint_curve);
190
191         if (brush->icon_imbuf)
192                 IMB_freeImBuf(brush->icon_imbuf);
193
194         BKE_previewimg_free(&(brush->preview));
195
196         curvemapping_free(brush->curve);
197
198         if (brush->gradient)
199                 MEM_freeN(brush->gradient);
200 }
201
202 static void extern_local_brush(Brush *brush)
203 {
204         id_lib_extern((ID *)brush->mtex.tex);
205         id_lib_extern((ID *)brush->mask_mtex.tex);
206         id_lib_extern((ID *)brush->clone.image);
207         id_lib_extern((ID *)brush->paint_curve);
208 }
209
210 void BKE_brush_make_local(Brush *brush)
211 {
212
213         /* - only lib users: do nothing
214          * - only local users: set flag
215          * - mixed: make copy
216          */
217
218         Main *bmain = G.main;
219         Scene *scene;
220         bool is_local = false, is_lib = false;
221
222         if (brush->id.lib == NULL) return;
223
224         if (brush->clone.image) {
225                 /* special case: ima always local immediately. Clone image should only
226                  * have one user anyway. */
227                 id_clear_lib_data(bmain, &brush->clone.image->id);
228                 extern_local_brush(brush);
229         }
230
231         for (scene = bmain->scene.first; scene && ELEM(0, is_lib, is_local); scene = scene->id.next) {
232                 if (BKE_paint_brush(&scene->toolsettings->imapaint.paint) == brush) {
233                         if (scene->id.lib) is_lib = true;
234                         else is_local = true;
235                 }
236         }
237
238         if (is_local && is_lib == false) {
239                 id_clear_lib_data(bmain, &brush->id);
240                 extern_local_brush(brush);
241
242                 /* enable fake user by default */
243                 if (!(brush->id.flag & LIB_FAKEUSER)) {
244                         brush->id.flag |= LIB_FAKEUSER;
245                         brush->id.us++;
246                 }
247         }
248         else if (is_local && is_lib) {
249                 Brush *brush_new = BKE_brush_copy(brush);
250                 brush_new->id.us = 1; /* only keep fake user */
251                 brush_new->id.flag |= LIB_FAKEUSER;
252
253                 /* Remap paths of new ID using old library as base. */
254                 BKE_id_lib_local_paths(bmain, brush->id.lib, &brush_new->id);
255                 
256                 for (scene = bmain->scene.first; scene; scene = scene->id.next) {
257                         if (BKE_paint_brush(&scene->toolsettings->imapaint.paint) == brush) {
258                                 if (scene->id.lib == NULL) {
259                                         BKE_paint_brush_set(&scene->toolsettings->imapaint.paint, brush_new);
260                                 }
261                         }
262                 }
263         }
264 }
265
266 void BKE_brush_debug_print_state(Brush *br)
267 {
268         /* create a fake brush and set it to the defaults */
269         Brush def = {{NULL}};
270         brush_defaults(&def);
271         
272 #define BR_TEST(field, t)                                       \
273         if (br->field != def.field)                             \
274                 printf("br->" #field " = %" #t ";\n", br->field)
275
276 #define BR_TEST_FLAG(_f)                                                        \
277         if ((br->flag & _f) && !(def.flag & _f))                \
278                 printf("br->flag |= " #_f ";\n");                       \
279         else if (!(br->flag & _f) && (def.flag & _f))   \
280                 printf("br->flag &= ~" #_f ";\n")
281         
282 #define BR_TEST_FLAG_OVERLAY(_f)                                                        \
283         if ((br->overlay_flags & _f) && !(def.overlay_flags & _f))              \
284                 printf("br->overlay_flags |= " #_f ";\n");                      \
285         else if (!(br->overlay_flags & _f) && (def.overlay_flags & _f)) \
286                 printf("br->overlay_flags &= ~" #_f ";\n")
287
288         /* print out any non-default brush state */
289         BR_TEST(normal_weight, f);
290
291         BR_TEST(blend, d);
292         BR_TEST(size, d);
293
294         /* br->flag */
295         BR_TEST_FLAG(BRUSH_AIRBRUSH);
296         BR_TEST_FLAG(BRUSH_TORUS);
297         BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
298         BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
299         BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
300         BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
301         BR_TEST_FLAG(BRUSH_RAKE);
302         BR_TEST_FLAG(BRUSH_ANCHORED);
303         BR_TEST_FLAG(BRUSH_DIR_IN);
304         BR_TEST_FLAG(BRUSH_SPACE);
305         BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
306         BR_TEST_FLAG(BRUSH_PERSISTENT);
307         BR_TEST_FLAG(BRUSH_ACCUMULATE);
308         BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
309         BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
310         BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
311         BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
312         BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
313         BR_TEST_FLAG(BRUSH_LOCK_SIZE);
314         BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
315         BR_TEST_FLAG(BRUSH_DRAG_DOT);
316         BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
317         BR_TEST_FLAG(BRUSH_RANDOM_ROTATION);
318         BR_TEST_FLAG(BRUSH_PLANE_TRIM);
319         BR_TEST_FLAG(BRUSH_FRONTFACE);
320         BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
321
322         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR);
323         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY);
324         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY);
325         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE);
326         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE);
327         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE);
328
329         BR_TEST(jitter, f);
330         BR_TEST(spacing, d);
331         BR_TEST(smooth_stroke_radius, d);
332         BR_TEST(smooth_stroke_factor, f);
333         BR_TEST(rate, f);
334
335         BR_TEST(alpha, f);
336
337         BR_TEST(sculpt_plane, d);
338
339         BR_TEST(plane_offset, f);
340
341         BR_TEST(autosmooth_factor, f);
342
343         BR_TEST(crease_pinch_factor, f);
344
345         BR_TEST(plane_trim, f);
346
347         BR_TEST(texture_sample_bias, f);
348         BR_TEST(texture_overlay_alpha, d);
349
350         BR_TEST(add_col[0], f);
351         BR_TEST(add_col[1], f);
352         BR_TEST(add_col[2], f);
353         BR_TEST(sub_col[0], f);
354         BR_TEST(sub_col[1], f);
355         BR_TEST(sub_col[2], f);
356
357         printf("\n");
358         
359 #undef BR_TEST
360 #undef BR_TEST_FLAG
361 }
362
363 void BKE_brush_sculpt_reset(Brush *br)
364 {
365         /* enable this to see any non-default
366          * settings used by a brush: */
367         // BKE_brush_debug_print_state(br);
368
369         brush_defaults(br);
370         BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
371
372         switch (br->sculpt_tool) {
373                 case SCULPT_TOOL_CLAY:
374                         br->flag |= BRUSH_FRONTFACE;
375                         break;
376                 case SCULPT_TOOL_CREASE:
377                         br->flag |= BRUSH_DIR_IN;
378                         br->alpha = 0.25;
379                         break;
380                 case SCULPT_TOOL_FILL:
381                         br->add_col[1] = 1;
382                         br->sub_col[0] = 0.25;
383                         br->sub_col[1] = 1;
384                         break;
385                 case SCULPT_TOOL_FLATTEN:
386                         br->add_col[1] = 1;
387                         br->sub_col[0] = 0.25;
388                         br->sub_col[1] = 1;
389                         break;
390                 case SCULPT_TOOL_INFLATE:
391                         br->add_col[0] = 0.750000;
392                         br->add_col[1] = 0.750000;
393                         br->add_col[2] = 0.750000;
394                         br->sub_col[0] = 0.250000;
395                         br->sub_col[1] = 0.250000;
396                         br->sub_col[2] = 0.250000;
397                         break;
398                 case SCULPT_TOOL_NUDGE:
399                         br->add_col[0] = 0.250000;
400                         br->add_col[1] = 1.000000;
401                         br->add_col[2] = 0.250000;
402                         break;
403                 case SCULPT_TOOL_PINCH:
404                         br->add_col[0] = 0.750000;
405                         br->add_col[1] = 0.750000;
406                         br->add_col[2] = 0.750000;
407                         br->sub_col[0] = 0.250000;
408                         br->sub_col[1] = 0.250000;
409                         br->sub_col[2] = 0.250000;
410                         break;
411                 case SCULPT_TOOL_SCRAPE:
412                         br->add_col[1] = 1.000000;
413                         br->sub_col[0] = 0.250000;
414                         br->sub_col[1] = 1.000000;
415                         break;
416                 case SCULPT_TOOL_ROTATE:
417                         br->alpha = 1.0;
418                         break;
419                 case SCULPT_TOOL_SMOOTH:
420                         br->flag &= ~BRUSH_SPACE_ATTEN;
421                         br->spacing = 5;
422                         br->add_col[0] = 0.750000;
423                         br->add_col[1] = 0.750000;
424                         br->add_col[2] = 0.750000;
425                         break;
426                 case SCULPT_TOOL_GRAB:
427                 case SCULPT_TOOL_SNAKE_HOOK:
428                 case SCULPT_TOOL_THUMB:
429                         br->size = 75;
430                         br->flag &= ~BRUSH_ALPHA_PRESSURE;
431                         br->flag &= ~BRUSH_SPACE;
432                         br->flag &= ~BRUSH_SPACE_ATTEN;
433                         br->add_col[0] = 0.250000;
434                         br->add_col[1] = 1.000000;
435                         br->add_col[2] = 0.250000;
436                         break;
437                 default:
438                         break;
439         }
440 }
441
442 /**
443  * Library Operations
444  * \param preset  CurveMappingPreset
445  */
446 void BKE_brush_curve_preset(Brush *b, int preset)
447 {
448         CurveMap *cm = NULL;
449
450         if (!b->curve)
451                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
452
453         cm = b->curve->cm;
454         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
455
456         b->curve->preset = preset;
457         curvemap_reset(cm, &b->curve->clipr, b->curve->preset, CURVEMAP_SLOPE_NEGATIVE);
458         curvemapping_changed(b->curve, false);
459 }
460
461 int BKE_brush_texture_set_nr(Brush *brush, int nr)
462 {
463         ID *idtest, *id = NULL;
464
465         id = (ID *)brush->mtex.tex;
466
467         idtest = (ID *)BLI_findlink(&G.main->tex, nr - 1);
468         if (idtest == NULL) { /* new tex */
469                 if (id) idtest = (ID *)BKE_texture_copy((Tex *)id);
470                 else idtest = (ID *)add_texture(G.main, "Tex");
471                 idtest->us--;
472         }
473         if (idtest != id) {
474                 BKE_brush_texture_delete(brush);
475
476                 brush->mtex.tex = (Tex *)idtest;
477                 id_us_plus(idtest);
478
479                 return 1;
480         }
481
482         return 0;
483 }
484
485 int BKE_brush_texture_delete(Brush *brush)
486 {
487         if (brush->mtex.tex)
488                 brush->mtex.tex->id.us--;
489
490         return 1;
491 }
492
493 int BKE_brush_clone_image_set_nr(Brush *brush, int nr)
494 {
495         if (brush && nr > 0) {
496                 Image *ima = (Image *)BLI_findlink(&G.main->image, nr - 1);
497
498                 if (ima) {
499                         BKE_brush_clone_image_delete(brush);
500                         brush->clone.image = ima;
501                         id_us_plus(&ima->id);
502                         brush->clone.offset[0] = brush->clone.offset[1] = 0.0f;
503
504                         return 1;
505                 }
506         }
507
508         return 0;
509 }
510
511 int BKE_brush_clone_image_delete(Brush *brush)
512 {
513         if (brush && brush->clone.image) {
514                 brush->clone.image->id.us--;
515                 brush->clone.image = NULL;
516                 return 1;
517         }
518
519         return 0;
520 }
521
522 /* Generic texture sampler for 3D painting systems. point has to be either in
523  * region space mouse coordinates, or 3d world coordinates for 3D mapping.
524  *
525  * rgba outputs straight alpha. */
526 float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
527                               const float point[3],
528                               float rgba[4], const int thread,
529                               struct ImagePool *pool)
530 {
531         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
532         MTex *mtex = &br->mtex;
533         float intensity = 1.0;
534         bool hasrgb = false;
535
536         if (!mtex->tex) {
537                 intensity = 1;
538         }
539         else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
540                 /* Get strength by feeding the vertex
541                  * location directly into a texture */
542                 hasrgb = externtex(mtex, point, &intensity,
543                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
544         }
545         else if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
546                 float rotation = -mtex->rot;
547                 float point_2d[2] = {point[0], point[1]};
548                 float x, y;
549                 float co[3];
550
551                 x = point_2d[0] - br->stencil_pos[0];
552                 y = point_2d[1] - br->stencil_pos[1];
553
554                 if (rotation > 0.001f || rotation < -0.001f) {
555                         const float angle    = atan2f(y, x) + rotation;
556                         const float flen     = sqrtf(x * x + y * y);
557
558                         x = flen * cosf(angle);
559                         y = flen * sinf(angle);
560                 }
561
562                 if (fabsf(x) > br->stencil_dimension[0] || fabsf(y) > br->stencil_dimension[1]) {
563                         zero_v4(rgba);
564                         return 0.0f;
565                 }
566                 x /= (br->stencil_dimension[0]);
567                 y /= (br->stencil_dimension[1]);
568
569                 co[0] = x;
570                 co[1] = y;
571                 co[2] = 0.0f;
572
573                 hasrgb = externtex(mtex, co, &intensity,
574                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
575         }
576         else {
577                 float rotation = -mtex->rot;
578                 float point_2d[2] = {point[0], point[1]};
579                 float x = 0.0f, y = 0.0f; /* Quite warnings */
580                 float invradius = 1.0f; /* Quite warnings */
581                 float co[3];
582
583                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
584                         /* keep coordinates relative to mouse */
585
586                         rotation += ups->brush_rotation;
587
588                         x = point_2d[0] - ups->tex_mouse[0];
589                         y = point_2d[1] - ups->tex_mouse[1];
590
591                         /* use pressure adjusted size for fixed mode */
592                         invradius = 1.0f / ups->pixel_radius;
593                 }
594                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
595                         /* leave the coordinates relative to the screen */
596
597                         /* use unadjusted size for tiled mode */
598                         invradius = 1.0f / BKE_brush_size_get(scene, br);
599
600                         x = point_2d[0];
601                         y = point_2d[1];
602                 }
603                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
604                         rotation += ups->brush_rotation;
605                         /* these contain a random coordinate */
606                         x = point_2d[0] - ups->tex_mouse[0];
607                         y = point_2d[1] - ups->tex_mouse[1];
608
609                         invradius = 1.0f / ups->pixel_radius;
610                 }
611
612                 x *= invradius;
613                 y *= invradius;
614
615                 /* it is probably worth optimizing for those cases where
616                  * the texture is not rotated by skipping the calls to
617                  * atan2, sqrtf, sin, and cos. */
618                 if (rotation > 0.001f || rotation < -0.001f) {
619                         const float angle    = atan2f(y, x) + rotation;
620                         const float flen     = sqrtf(x * x + y * y);
621
622                         x = flen * cosf(angle);
623                         y = flen * sinf(angle);
624                 }
625
626                 co[0] = x;
627                 co[1] = y;
628                 co[2] = 0.0f;
629
630                 hasrgb = externtex(mtex, co, &intensity,
631                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
632         }
633
634         intensity += br->texture_sample_bias;
635
636         if (!hasrgb) {
637                 rgba[0] = intensity;
638                 rgba[1] = intensity;
639                 rgba[2] = intensity;
640                 rgba[3] = 1.0f;
641         }
642         /* For consistency, sampling always returns color in linear space */
643         else if (ups->do_linear_conversion) {
644                 IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, ups->colorspace);
645         }
646
647         return intensity;
648 }
649
650 float BKE_brush_sample_masktex(const Scene *scene, Brush *br,
651                                const float point[2],
652                                const int thread,
653                                struct ImagePool *pool)
654 {
655         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
656         MTex *mtex = &br->mask_mtex;
657         float rgba[4], intensity;
658
659         if (!mtex->tex) {
660                 return 1.0f;
661         }
662         if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
663                 float rotation = -mtex->rot;
664                 float point_2d[2] = {point[0], point[1]};
665                 float x, y;
666                 float co[3];
667
668                 x = point_2d[0] - br->mask_stencil_pos[0];
669                 y = point_2d[1] - br->mask_stencil_pos[1];
670
671                 if (rotation > 0.001f || rotation < -0.001f) {
672                         const float angle    = atan2f(y, x) + rotation;
673                         const float flen     = sqrtf(x * x + y * y);
674
675                         x = flen * cosf(angle);
676                         y = flen * sinf(angle);
677                 }
678
679                 if (fabsf(x) > br->mask_stencil_dimension[0] || fabsf(y) > br->mask_stencil_dimension[1]) {
680                         zero_v4(rgba);
681                         return 0.0f;
682                 }
683                 x /= (br->mask_stencil_dimension[0]);
684                 y /= (br->mask_stencil_dimension[1]);
685
686                 co[0] = x;
687                 co[1] = y;
688                 co[2] = 0.0f;
689
690                 externtex(mtex, co, &intensity,
691                           rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
692         }
693         else {
694                 float rotation = -mtex->rot;
695                 float point_2d[2] = {point[0], point[1]};
696                 float x = 0.0f, y = 0.0f; /* Quite warnings */
697                 float invradius = 1.0f; /* Quite warnings */
698                 float co[3];
699
700                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
701                         /* keep coordinates relative to mouse */
702
703                         rotation += ups->brush_rotation;
704
705                         x = point_2d[0] - ups->mask_tex_mouse[0];
706                         y = point_2d[1] - ups->mask_tex_mouse[1];
707
708                         /* use pressure adjusted size for fixed mode */
709                         invradius = 1.0f / ups->pixel_radius;
710                 }
711                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
712                         /* leave the coordinates relative to the screen */
713
714                         /* use unadjusted size for tiled mode */
715                         invradius = 1.0f / BKE_brush_size_get(scene, br);
716
717                         x = point_2d[0];
718                         y = point_2d[1];
719                 }
720                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
721                         rotation += ups->brush_rotation;
722                         /* these contain a random coordinate */
723                         x = point_2d[0] - ups->mask_tex_mouse[0];
724                         y = point_2d[1] - ups->mask_tex_mouse[1];
725
726                         invradius = 1.0f / ups->pixel_radius;
727                 }
728
729                 x *= invradius;
730                 y *= invradius;
731
732                 /* it is probably worth optimizing for those cases where
733                  * the texture is not rotated by skipping the calls to
734                  * atan2, sqrtf, sin, and cos. */
735                 if (rotation > 0.001f || rotation < -0.001f) {
736                         const float angle    = atan2f(y, x) + rotation;
737                         const float flen     = sqrtf(x * x + y * y);
738
739                         x = flen * cosf(angle);
740                         y = flen * sinf(angle);
741                 }
742
743                 co[0] = x;
744                 co[1] = y;
745                 co[2] = 0.0f;
746
747                 externtex(mtex, co, &intensity,
748                           rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
749         }
750
751         CLAMP(intensity, 0.0f, 1.0f);
752
753         switch (br->mask_pressure) {
754                 case BRUSH_MASK_PRESSURE_CUTOFF:
755                         intensity  = ((1.0f - intensity) < ups->size_pressure_value) ? 1.0f : 0.0f;
756                         break;
757                 case BRUSH_MASK_PRESSURE_RAMP:
758                         intensity = ups->size_pressure_value + intensity * (1.0f - ups->size_pressure_value);
759                         break;
760                 default:
761                         break;
762         }
763
764         return intensity;
765 }
766
767 /* Unified Size / Strength / Color */
768
769 /* XXX: be careful about setting size and unprojected radius
770  * because they depend on one another
771  * these functions do not set the other corresponding value
772  * this can lead to odd behavior if size and unprojected
773  * radius become inconsistent.
774  * the biggest problem is that it isn't possible to change
775  * unprojected radius because a view context is not
776  * available.  my usual solution to this is to use the
777  * ratio of change of the size to change the unprojected
778  * radius.  Not completely convinced that is correct.
779  * In any case, a better solution is needed to prevent
780  * inconsistency. */
781
782
783 float *BKE_brush_color_get(const struct Scene *scene, struct Brush *brush)
784 {
785         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
786         return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->rgb : brush->rgb;
787 }
788
789 float *BKE_brush_secondary_color_get(const struct Scene *scene, struct Brush *brush)
790 {
791         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
792         return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->secondary_rgb : brush->secondary_rgb;
793 }
794
795 void BKE_brush_color_set(struct Scene *scene, struct Brush *brush, const float color[3])
796 {
797         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
798
799         if (ups->flag & UNIFIED_PAINT_COLOR)
800                 copy_v3_v3(ups->rgb, color);
801         else
802                 copy_v3_v3(brush->rgb, color);
803 }
804
805 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
806 {
807         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
808         
809         size = (int)((float)size / U.pixelsize);
810         
811         if (ups->flag & UNIFIED_PAINT_SIZE)
812                 ups->size = size;
813         else
814                 brush->size = size;
815 }
816
817 int BKE_brush_size_get(const Scene *scene, Brush *brush)
818 {
819         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
820         int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
821         
822         return (int)((float)size * U.pixelsize);
823 }
824
825 int BKE_brush_use_locked_size(const Scene *scene, Brush *brush)
826 {
827         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
828
829         return (us_flag & UNIFIED_PAINT_SIZE) ?
830                (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
831                (brush->flag & BRUSH_LOCK_SIZE);
832 }
833
834 int BKE_brush_use_size_pressure(const Scene *scene, Brush *brush)
835 {
836         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
837
838         return (us_flag & UNIFIED_PAINT_SIZE) ?
839                (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
840                (brush->flag & BRUSH_SIZE_PRESSURE);
841 }
842
843 int BKE_brush_use_alpha_pressure(const Scene *scene, Brush *brush)
844 {
845         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
846
847         return (us_flag & UNIFIED_PAINT_ALPHA) ?
848                (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
849                (brush->flag & BRUSH_ALPHA_PRESSURE);
850 }
851
852 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
853 {
854         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
855
856         if (ups->flag & UNIFIED_PAINT_SIZE)
857                 ups->unprojected_radius = unprojected_radius;
858         else
859                 brush->unprojected_radius = unprojected_radius;
860 }
861
862 float BKE_brush_unprojected_radius_get(const Scene *scene, Brush *brush)
863 {
864         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
865
866         return (ups->flag & UNIFIED_PAINT_SIZE) ?
867                ups->unprojected_radius :
868                brush->unprojected_radius;
869 }
870
871 void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
872 {
873         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
874
875         if (ups->flag & UNIFIED_PAINT_ALPHA)
876                 ups->alpha = alpha;
877         else
878                 brush->alpha = alpha;
879 }
880
881 float BKE_brush_alpha_get(const Scene *scene, Brush *brush)
882 {
883         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
884
885         return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
886 }
887
888 float BKE_brush_weight_get(const Scene *scene, Brush *brush)
889 {
890         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
891
892         return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
893 }
894
895 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
896 {
897         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
898
899         if (ups->flag & UNIFIED_PAINT_WEIGHT)
900                 ups->weight = value;
901         else
902                 brush->weight = value;
903 }
904
905 /* scale unprojected radius to reflect a change in the brush's 2D size */
906 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
907                                         int new_brush_size,
908                                         int old_brush_size)
909 {
910         float scale = new_brush_size;
911         /* avoid division by zero */
912         if (old_brush_size != 0)
913                 scale /= (float)old_brush_size;
914         (*unprojected_radius) *= scale;
915 }
916
917 /* scale brush size to reflect a change in the brush's unprojected radius */
918 void BKE_brush_scale_size(int *r_brush_size,
919                           float new_unprojected_radius,
920                           float old_unprojected_radius)
921 {
922         float scale = new_unprojected_radius;
923         /* avoid division by zero */
924         if (old_unprojected_radius != 0)
925                 scale /= new_unprojected_radius;
926         (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
927 }
928
929 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
930 {
931         float rand_pos[2];
932         float spread;
933         int diameter;
934
935         do {
936                 rand_pos[0] = BLI_rng_get_float(brush_rng) - 0.5f;
937                 rand_pos[1] = BLI_rng_get_float(brush_rng) - 0.5f;
938         } while (len_squared_v2(rand_pos) > SQUARE(0.5f));
939
940
941         if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
942                 diameter = 2 * brush->jitter_absolute;
943                 spread = 1.0;
944         }
945         else {
946                 diameter = 2 * BKE_brush_size_get(scene, brush);
947                 spread = brush->jitter;
948         }
949         /* find random position within a circle of diameter 1 */
950         jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
951         jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
952 }
953
954 void BKE_brush_randomize_texture_coordinates(UnifiedPaintSettings *ups, bool mask)
955 {
956         /* we multiply with brush radius as an optimization for the brush
957          * texture sampling functions */
958         if (mask) {
959                 ups->mask_tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
960                 ups->mask_tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
961         }
962         else {
963                 ups->tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
964                 ups->tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
965         }
966 }
967
968 /* Uses the brush curve control to find a strength value between 0 and 1 */
969 float BKE_brush_curve_strength_clamp(Brush *br, float p, const float len)
970 {
971         float strength;
972
973         if (p >= len) return 0;
974         else p = p / len;
975
976         strength = curvemapping_evaluateF(br->curve, 0, p);
977
978         CLAMP(strength, 0.0f, 1.0f);
979
980         return strength;
981 }
982 /* same as above but can return negative values if the curve enables
983  * used for sculpt only */
984 float BKE_brush_curve_strength(Brush *br, float p, const float len)
985 {
986         if (p >= len)
987                 p = 1.0f;
988         else
989                 p = p / len;
990
991         return curvemapping_evaluateF(br->curve, 0, p);
992 }
993
994 /* TODO: should probably be unified with BrushPainter stuff? */
995 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side, bool use_secondary)
996 {
997         unsigned int *texcache = NULL;
998         MTex *mtex = (use_secondary) ? &br->mask_mtex : &br->mtex;
999         float intensity;
1000         float rgba[4];
1001         int ix, iy;
1002         int side = half_side * 2;
1003
1004         if (mtex->tex) {
1005                 float x, y, step = 2.0 / side, co[3];
1006
1007                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1008
1009                 /* do normalized cannonical view coords for texture */
1010                 for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
1011                         for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
1012                                 co[0] = x;
1013                                 co[1] = y;
1014                                 co[2] = 0.0f;
1015
1016                                 /* This is copied from displace modifier code */
1017                                 /* TODO(sergey): brush are always cacheing with CM enabled for now. */
1018                                 externtex(mtex, co, &intensity,
1019                                           rgba, rgba + 1, rgba + 2, rgba + 3, 0, NULL);
1020
1021                                 ((char *)texcache)[(iy * side + ix) * 4] =
1022                                 ((char *)texcache)[(iy * side + ix) * 4 + 1] =
1023                                 ((char *)texcache)[(iy * side + ix) * 4 + 2] =
1024                                 ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(intensity * 255.0f);
1025                         }
1026                 }
1027         }
1028
1029         return texcache;
1030 }
1031
1032
1033 /**** Radial Control ****/
1034 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br, bool secondary)
1035 {
1036         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1037         unsigned int *texcache;
1038         int side = 128;
1039         int half = side / 2;
1040         int i, j;
1041
1042         curvemapping_initialize(br->curve);
1043         texcache = BKE_brush_gen_texture_cache(br, half, secondary);
1044         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1045         im->x = im->y = side;
1046
1047         for (i = 0; i < side; ++i) {
1048                 for (j = 0; j < side; ++j) {
1049                         float magn = sqrtf(pow2f(i - half) + pow2f(j - half));
1050                         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamp(br, magn, half);
1051                 }
1052         }
1053
1054         /* Modulate curve with texture */
1055         if (texcache) {
1056                 for (i = 0; i < side; ++i) {
1057                         for (j = 0; j < side; ++j) {
1058                                 const int col = texcache[i * side + j];
1059                                 im->rect_float[i * side + j] *= (((char *)&col)[0] + ((char *)&col)[1] + ((char *)&col)[2]) / 3.0f / 255.0f;
1060                         }
1061                 }
1062
1063                 MEM_freeN(texcache);
1064         }
1065
1066         return im;
1067 }