Cleanup: unused functions
[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_library_query.h"
42 #include "BKE_library_remap.h"
43 #include "BKE_main.h"
44 #include "BKE_paint.h"
45 #include "BKE_texture.h"
46 #include "BKE_icons.h"
47
48 #include "IMB_colormanagement.h"
49 #include "IMB_imbuf.h"
50 #include "IMB_imbuf_types.h"
51
52 #include "RE_render_ext.h" /* externtex */
53
54 static RNG *brush_rng;
55
56 void BKE_brush_system_init(void)
57 {
58         brush_rng = BLI_rng_new(0);
59         BLI_rng_srandom(brush_rng, 31415682);
60 }
61
62 void BKE_brush_system_exit(void)
63 {
64         BLI_rng_free(brush_rng);
65 }
66
67
68 static void brush_defaults(Brush *brush)
69 {
70         brush->blend = 0;
71         brush->flag = 0;
72
73         brush->ob_mode = OB_MODE_ALL_PAINT;
74
75         /* BRUSH SCULPT TOOL SETTINGS */
76         brush->weight = 1.0f; /* weight of brush 0 - 1.0 */
77         brush->size = 35; /* radius of the brush in pixels */
78         brush->alpha = 0.5f; /* brush strength/intensity probably variable should be renamed? */
79         brush->autosmooth_factor = 0.0f;
80         brush->crease_pinch_factor = 0.5f;
81         brush->sculpt_plane = SCULPT_DISP_DIR_AREA;
82         brush->plane_offset = 0.0f; /* how far above or below the plane that is found by averaging the faces */
83         brush->plane_trim = 0.5f;
84         brush->clone.alpha = 0.5f;
85         brush->normal_weight = 0.0f;
86         brush->fill_threshold = 0.2f;
87         brush->flag |= BRUSH_ALPHA_PRESSURE;
88
89         /* BRUSH PAINT TOOL SETTINGS */
90         brush->rgb[0] = 1.0f; /* default rgb color of the brush when painting - white */
91         brush->rgb[1] = 1.0f;
92         brush->rgb[2] = 1.0f;
93
94         zero_v3(brush->secondary_rgb);
95
96         /* BRUSH STROKE SETTINGS */
97         brush->flag |= (BRUSH_SPACE | BRUSH_SPACE_ATTEN);
98         brush->spacing = 10; /* how far each brush dot should be spaced as a percentage of brush diameter */
99
100         brush->smooth_stroke_radius = 75;
101         brush->smooth_stroke_factor = 0.9f;
102
103         brush->rate = 0.1f; /* time delay between dots of paint or sculpting when doing airbrush mode */
104
105         brush->jitter = 0.0f;
106
107         /* BRUSH TEXTURE SETTINGS */
108         BKE_texture_mtex_default(&brush->mtex);
109         BKE_texture_mtex_default(&brush->mask_mtex);
110
111         brush->texture_sample_bias = 0; /* value to added to texture samples */
112         brush->texture_overlay_alpha = 33;
113         brush->mask_overlay_alpha = 33;
114         brush->cursor_overlay_alpha = 33;
115         brush->overlay_flags = 0;
116
117         /* brush appearance  */
118
119         brush->add_col[0] = 1.00; /* add mode color is light red */
120         brush->add_col[1] = 0.39;
121         brush->add_col[2] = 0.39;
122
123         brush->sub_col[0] = 0.39; /* subtract mode color is light blue */
124         brush->sub_col[1] = 0.39;
125         brush->sub_col[2] = 1.00;
126
127         brush->stencil_pos[0] = 256;
128         brush->stencil_pos[1] = 256;
129
130         brush->stencil_dimension[0] = 256;
131         brush->stencil_dimension[1] = 256;
132 }
133
134 /* Datablock add/copy/free/make_local */
135
136 void BKE_brush_init(Brush *brush)
137 {
138         BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(brush, id));
139
140         /* enable fake user by default */
141         id_fake_user_set(&brush->id);
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
151 /**
152  * \note Resulting brush will have two users: one as a fake user, another is assumed to be used by the caller.
153  */
154 Brush *BKE_brush_add(Main *bmain, const char *name, const eObjectMode ob_mode)
155 {
156         Brush *brush;
157
158         brush = BKE_libblock_alloc(bmain, ID_BR, name, 0);
159
160         BKE_brush_init(brush);
161
162         brush->ob_mode = ob_mode;
163
164         return brush;
165 }
166
167 struct Brush *BKE_brush_first_search(struct Main *bmain, const eObjectMode ob_mode)
168 {
169         Brush *brush;
170
171         for (brush = bmain->brush.first; brush; brush = brush->id.next) {
172                 if (brush->ob_mode & ob_mode)
173                         return brush;
174         }
175         return NULL;
176 }
177
178 /**
179  * Only copy internal data of Brush ID from source to already allocated/initialized destination.
180  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
181  *
182  * WARNING! This function will not handle ID user count!
183  *
184  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
185  */
186 void BKE_brush_copy_data(Main *UNUSED(bmain), Brush *brush_dst, const Brush *brush_src, const int flag)
187 {
188         if (brush_src->icon_imbuf) {
189                 brush_dst->icon_imbuf = IMB_dupImBuf(brush_src->icon_imbuf);
190         }
191
192         if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
193                 BKE_previewimg_id_copy(&brush_dst->id, &brush_src->id);
194         }
195         else {
196                 brush_dst->preview = NULL;
197         }
198
199         brush_dst->curve = curvemapping_copy(brush_src->curve);
200
201         /* enable fake user by default */
202         id_fake_user_set(&brush_dst->id);
203 }
204
205 Brush *BKE_brush_copy(Main *bmain, const Brush *brush)
206 {
207         Brush *brush_copy;
208         BKE_id_copy_ex(bmain, &brush->id, (ID **)&brush_copy, 0, false);
209         return brush_copy;
210 }
211
212 /** Free (or release) any data used by this brush (does not free the brush itself). */
213 void BKE_brush_free(Brush *brush)
214 {
215         if (brush->icon_imbuf) {
216                 IMB_freeImBuf(brush->icon_imbuf);
217         }
218
219         curvemapping_free(brush->curve);
220
221         MEM_SAFE_FREE(brush->gradient);
222
223         BKE_previewimg_free(&(brush->preview));
224 }
225
226 void BKE_brush_make_local(Main *bmain, Brush *brush, const bool lib_local)
227 {
228         bool is_local = false, is_lib = false;
229
230         /* - only lib users: do nothing (unless force_local is set)
231          * - only local users: set flag
232          * - mixed: make copy
233          */
234
235         if (!ID_IS_LINKED(brush)) {
236                 return;
237         }
238
239         if (brush->clone.image) {
240                 /* Special case: ima always local immediately. Clone image should only have one user anyway. */
241                 id_make_local(bmain, &brush->clone.image->id, false, false);
242         }
243
244         BKE_library_ID_test_usages(bmain, brush, &is_local, &is_lib);
245
246         if (lib_local || is_local) {
247                 if (!is_lib) {
248                         id_clear_lib_data(bmain, &brush->id);
249                         BKE_id_expand_local(bmain, &brush->id);
250
251                         /* enable fake user by default */
252                         id_fake_user_set(&brush->id);
253                 }
254                 else {
255                         Brush *brush_new = BKE_brush_copy(bmain, brush);  /* Ensures FAKE_USER is set */
256
257                         brush_new->id.us = 0;
258
259                         /* setting newid is mandatory for complex make_lib_local logic... */
260                         ID_NEW_SET(brush, brush_new);
261
262                         if (!lib_local) {
263                                 BKE_libblock_remap(bmain, brush, brush_new, ID_REMAP_SKIP_INDIRECT_USAGE);
264                         }
265                 }
266         }
267 }
268
269 void BKE_brush_debug_print_state(Brush *br)
270 {
271         /* create a fake brush and set it to the defaults */
272         Brush def = {{NULL}};
273         brush_defaults(&def);
274         
275 #define BR_TEST(field, t)                                       \
276         if (br->field != def.field)                             \
277                 printf("br->" #field " = %" #t ";\n", br->field)
278
279 #define BR_TEST_FLAG(_f)                                                        \
280         if ((br->flag & _f) && !(def.flag & _f))                \
281                 printf("br->flag |= " #_f ";\n");                       \
282         else if (!(br->flag & _f) && (def.flag & _f))   \
283                 printf("br->flag &= ~" #_f ";\n")
284         
285 #define BR_TEST_FLAG_OVERLAY(_f)                                                        \
286         if ((br->overlay_flags & _f) && !(def.overlay_flags & _f))              \
287                 printf("br->overlay_flags |= " #_f ";\n");                      \
288         else if (!(br->overlay_flags & _f) && (def.overlay_flags & _f)) \
289                 printf("br->overlay_flags &= ~" #_f ";\n")
290
291         /* print out any non-default brush state */
292         BR_TEST(normal_weight, f);
293
294         BR_TEST(blend, d);
295         BR_TEST(size, d);
296
297         /* br->flag */
298         BR_TEST_FLAG(BRUSH_AIRBRUSH);
299         BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
300         BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
301         BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
302         BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
303         BR_TEST_FLAG(BRUSH_ANCHORED);
304         BR_TEST_FLAG(BRUSH_DIR_IN);
305         BR_TEST_FLAG(BRUSH_SPACE);
306         BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
307         BR_TEST_FLAG(BRUSH_PERSISTENT);
308         BR_TEST_FLAG(BRUSH_ACCUMULATE);
309         BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
310         BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
311         BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
312         BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
313         BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
314         BR_TEST_FLAG(BRUSH_LOCK_SIZE);
315         BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
316         BR_TEST_FLAG(BRUSH_DRAG_DOT);
317         BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
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  */
445 void BKE_brush_curve_preset(Brush *b, eCurveMappingPreset preset)
446 {
447         CurveMap *cm = NULL;
448
449         if (!b->curve)
450                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
451
452         cm = b->curve->cm;
453         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
454
455         b->curve->preset = preset;
456         curvemap_reset(cm, &b->curve->clipr, b->curve->preset, CURVEMAP_SLOPE_NEGATIVE);
457         curvemapping_changed(b->curve, false);
458 }
459
460 /* Generic texture sampler for 3D painting systems. point has to be either in
461  * region space mouse coordinates, or 3d world coordinates for 3D mapping.
462  *
463  * rgba outputs straight alpha. */
464 float BKE_brush_sample_tex_3D(const Scene *scene, const Brush *br,
465                               const float point[3],
466                               float rgba[4], const int thread,
467                               struct ImagePool *pool)
468 {
469         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
470         const MTex *mtex = &br->mtex;
471         float intensity = 1.0;
472         bool hasrgb = false;
473
474         if (!mtex->tex) {
475                 intensity = 1;
476         }
477         else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
478                 /* Get strength by feeding the vertex
479                  * location directly into a texture */
480                 hasrgb = externtex(mtex, point, &intensity,
481                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
482         }
483         else if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
484                 float rotation = -mtex->rot;
485                 float point_2d[2] = {point[0], point[1]};
486                 float x, y;
487                 float co[3];
488
489                 x = point_2d[0] - br->stencil_pos[0];
490                 y = point_2d[1] - br->stencil_pos[1];
491
492                 if (rotation > 0.001f || rotation < -0.001f) {
493                         const float angle    = atan2f(y, x) + rotation;
494                         const float flen     = sqrtf(x * x + y * y);
495
496                         x = flen * cosf(angle);
497                         y = flen * sinf(angle);
498                 }
499
500                 if (fabsf(x) > br->stencil_dimension[0] || fabsf(y) > br->stencil_dimension[1]) {
501                         zero_v4(rgba);
502                         return 0.0f;
503                 }
504                 x /= (br->stencil_dimension[0]);
505                 y /= (br->stencil_dimension[1]);
506
507                 co[0] = x;
508                 co[1] = y;
509                 co[2] = 0.0f;
510
511                 hasrgb = externtex(mtex, co, &intensity,
512                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
513         }
514         else {
515                 float rotation = -mtex->rot;
516                 float point_2d[2] = {point[0], point[1]};
517                 float x = 0.0f, y = 0.0f; /* Quite warnings */
518                 float invradius = 1.0f; /* Quite warnings */
519                 float co[3];
520
521                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
522                         /* keep coordinates relative to mouse */
523
524                         rotation += ups->brush_rotation;
525
526                         x = point_2d[0] - ups->tex_mouse[0];
527                         y = point_2d[1] - ups->tex_mouse[1];
528
529                         /* use pressure adjusted size for fixed mode */
530                         invradius = 1.0f / ups->pixel_radius;
531                 }
532                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
533                         /* leave the coordinates relative to the screen */
534
535                         /* use unadjusted size for tiled mode */
536                         invradius = 1.0f / BKE_brush_size_get(scene, br);
537
538                         x = point_2d[0];
539                         y = point_2d[1];
540                 }
541                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
542                         rotation += ups->brush_rotation;
543                         /* these contain a random coordinate */
544                         x = point_2d[0] - ups->tex_mouse[0];
545                         y = point_2d[1] - ups->tex_mouse[1];
546
547                         invradius = 1.0f / ups->pixel_radius;
548                 }
549
550                 x *= invradius;
551                 y *= invradius;
552
553                 /* it is probably worth optimizing for those cases where
554                  * the texture is not rotated by skipping the calls to
555                  * atan2, sqrtf, sin, and cos. */
556                 if (rotation > 0.001f || rotation < -0.001f) {
557                         const float angle    = atan2f(y, x) + rotation;
558                         const float flen     = sqrtf(x * x + y * y);
559
560                         x = flen * cosf(angle);
561                         y = flen * sinf(angle);
562                 }
563
564                 co[0] = x;
565                 co[1] = y;
566                 co[2] = 0.0f;
567
568                 hasrgb = externtex(mtex, co, &intensity,
569                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
570         }
571
572         intensity += br->texture_sample_bias;
573
574         if (!hasrgb) {
575                 rgba[0] = intensity;
576                 rgba[1] = intensity;
577                 rgba[2] = intensity;
578                 rgba[3] = 1.0f;
579         }
580         /* For consistency, sampling always returns color in linear space */
581         else if (ups->do_linear_conversion) {
582                 IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, ups->colorspace);
583         }
584
585         return intensity;
586 }
587
588 float BKE_brush_sample_masktex(const Scene *scene, Brush *br,
589                                const float point[2],
590                                const int thread,
591                                struct ImagePool *pool)
592 {
593         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
594         MTex *mtex = &br->mask_mtex;
595         float rgba[4], intensity;
596
597         if (!mtex->tex) {
598                 return 1.0f;
599         }
600         if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
601                 float rotation = -mtex->rot;
602                 float point_2d[2] = {point[0], point[1]};
603                 float x, y;
604                 float co[3];
605
606                 x = point_2d[0] - br->mask_stencil_pos[0];
607                 y = point_2d[1] - br->mask_stencil_pos[1];
608
609                 if (rotation > 0.001f || rotation < -0.001f) {
610                         const float angle    = atan2f(y, x) + rotation;
611                         const float flen     = sqrtf(x * x + y * y);
612
613                         x = flen * cosf(angle);
614                         y = flen * sinf(angle);
615                 }
616
617                 if (fabsf(x) > br->mask_stencil_dimension[0] || fabsf(y) > br->mask_stencil_dimension[1]) {
618                         zero_v4(rgba);
619                         return 0.0f;
620                 }
621                 x /= (br->mask_stencil_dimension[0]);
622                 y /= (br->mask_stencil_dimension[1]);
623
624                 co[0] = x;
625                 co[1] = y;
626                 co[2] = 0.0f;
627
628                 externtex(mtex, co, &intensity,
629                           rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
630         }
631         else {
632                 float rotation = -mtex->rot;
633                 float point_2d[2] = {point[0], point[1]};
634                 float x = 0.0f, y = 0.0f; /* Quite warnings */
635                 float invradius = 1.0f; /* Quite warnings */
636                 float co[3];
637
638                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
639                         /* keep coordinates relative to mouse */
640
641                         rotation += ups->brush_rotation_sec;
642
643                         x = point_2d[0] - ups->mask_tex_mouse[0];
644                         y = point_2d[1] - ups->mask_tex_mouse[1];
645
646                         /* use pressure adjusted size for fixed mode */
647                         invradius = 1.0f / ups->pixel_radius;
648                 }
649                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
650                         /* leave the coordinates relative to the screen */
651
652                         /* use unadjusted size for tiled mode */
653                         invradius = 1.0f / BKE_brush_size_get(scene, br);
654
655                         x = point_2d[0];
656                         y = point_2d[1];
657                 }
658                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
659                         rotation += ups->brush_rotation_sec;
660                         /* these contain a random coordinate */
661                         x = point_2d[0] - ups->mask_tex_mouse[0];
662                         y = point_2d[1] - ups->mask_tex_mouse[1];
663
664                         invradius = 1.0f / ups->pixel_radius;
665                 }
666
667                 x *= invradius;
668                 y *= invradius;
669
670                 /* it is probably worth optimizing for those cases where
671                  * the texture is not rotated by skipping the calls to
672                  * atan2, sqrtf, sin, and cos. */
673                 if (rotation > 0.001f || rotation < -0.001f) {
674                         const float angle    = atan2f(y, x) + rotation;
675                         const float flen     = sqrtf(x * x + y * y);
676
677                         x = flen * cosf(angle);
678                         y = flen * sinf(angle);
679                 }
680
681                 co[0] = x;
682                 co[1] = y;
683                 co[2] = 0.0f;
684
685                 externtex(mtex, co, &intensity,
686                           rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
687         }
688
689         CLAMP(intensity, 0.0f, 1.0f);
690
691         switch (br->mask_pressure) {
692                 case BRUSH_MASK_PRESSURE_CUTOFF:
693                         intensity  = ((1.0f - intensity) < ups->size_pressure_value) ? 1.0f : 0.0f;
694                         break;
695                 case BRUSH_MASK_PRESSURE_RAMP:
696                         intensity = ups->size_pressure_value + intensity * (1.0f - ups->size_pressure_value);
697                         break;
698                 default:
699                         break;
700         }
701
702         return intensity;
703 }
704
705 /* Unified Size / Strength / Color */
706
707 /* XXX: be careful about setting size and unprojected radius
708  * because they depend on one another
709  * these functions do not set the other corresponding value
710  * this can lead to odd behavior if size and unprojected
711  * radius become inconsistent.
712  * the biggest problem is that it isn't possible to change
713  * unprojected radius because a view context is not
714  * available.  my usual solution to this is to use the
715  * ratio of change of the size to change the unprojected
716  * radius.  Not completely convinced that is correct.
717  * In any case, a better solution is needed to prevent
718  * inconsistency. */
719
720
721 const float *BKE_brush_color_get(const struct Scene *scene, const struct Brush *brush)
722 {
723         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
724         return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->rgb : brush->rgb;
725 }
726
727 const float *BKE_brush_secondary_color_get(const struct Scene *scene, const struct Brush *brush)
728 {
729         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
730         return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->secondary_rgb : brush->secondary_rgb;
731 }
732
733 void BKE_brush_color_set(struct Scene *scene, struct Brush *brush, const float color[3])
734 {
735         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
736
737         if (ups->flag & UNIFIED_PAINT_COLOR)
738                 copy_v3_v3(ups->rgb, color);
739         else
740                 copy_v3_v3(brush->rgb, color);
741 }
742
743 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
744 {
745         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
746
747         /* make sure range is sane */
748         CLAMP(size, 1, MAX_BRUSH_PIXEL_RADIUS);
749
750         if (ups->flag & UNIFIED_PAINT_SIZE)
751                 ups->size = size;
752         else
753                 brush->size = size;
754 }
755
756 int BKE_brush_size_get(const Scene *scene, const Brush *brush)
757 {
758         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
759         int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
760         
761         return size;
762 }
763
764 bool BKE_brush_use_locked_size(const Scene *scene, const Brush *brush)
765 {
766         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
767
768         return (us_flag & UNIFIED_PAINT_SIZE) ?
769                (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
770                (brush->flag & BRUSH_LOCK_SIZE);
771 }
772
773 bool BKE_brush_use_size_pressure(const Scene *scene, const Brush *brush)
774 {
775         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
776
777         return (us_flag & UNIFIED_PAINT_SIZE) ?
778                (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
779                (brush->flag & BRUSH_SIZE_PRESSURE);
780 }
781
782 bool BKE_brush_use_alpha_pressure(const Scene *scene, const Brush *brush)
783 {
784         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
785
786         return (us_flag & UNIFIED_PAINT_ALPHA) ?
787                (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
788                (brush->flag & BRUSH_ALPHA_PRESSURE);
789 }
790
791 bool BKE_brush_sculpt_has_secondary_color(const Brush *brush)
792 {
793         return ELEM(
794                 brush->sculpt_tool, SCULPT_TOOL_BLOB, SCULPT_TOOL_DRAW,
795                 SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_CLAY_STRIPS,
796                 SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE, SCULPT_TOOL_LAYER,
797                 SCULPT_TOOL_FLATTEN, SCULPT_TOOL_FILL, SCULPT_TOOL_SCRAPE,
798                 SCULPT_TOOL_MASK);
799 }
800
801 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
802 {
803         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
804
805         if (ups->flag & UNIFIED_PAINT_SIZE)
806                 ups->unprojected_radius = unprojected_radius;
807         else
808                 brush->unprojected_radius = unprojected_radius;
809 }
810
811 float BKE_brush_unprojected_radius_get(const Scene *scene, const Brush *brush)
812 {
813         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
814
815         return (ups->flag & UNIFIED_PAINT_SIZE) ?
816                ups->unprojected_radius :
817                brush->unprojected_radius;
818 }
819
820 void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
821 {
822         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
823
824         if (ups->flag & UNIFIED_PAINT_ALPHA)
825                 ups->alpha = alpha;
826         else
827                 brush->alpha = alpha;
828 }
829
830 float BKE_brush_alpha_get(const Scene *scene, const Brush *brush)
831 {
832         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
833
834         return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
835 }
836
837 float BKE_brush_weight_get(const Scene *scene, const Brush *brush)
838 {
839         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
840
841         return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
842 }
843
844 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
845 {
846         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
847
848         if (ups->flag & UNIFIED_PAINT_WEIGHT)
849                 ups->weight = value;
850         else
851                 brush->weight = value;
852 }
853
854 /* scale unprojected radius to reflect a change in the brush's 2D size */
855 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
856                                         int new_brush_size,
857                                         int old_brush_size)
858 {
859         float scale = new_brush_size;
860         /* avoid division by zero */
861         if (old_brush_size != 0)
862                 scale /= (float)old_brush_size;
863         (*unprojected_radius) *= scale;
864 }
865
866 /* scale brush size to reflect a change in the brush's unprojected radius */
867 void BKE_brush_scale_size(
868         int *r_brush_size,
869         float new_unprojected_radius,
870         float old_unprojected_radius)
871 {
872         float scale = new_unprojected_radius;
873         /* avoid division by zero */
874         if (old_unprojected_radius != 0)
875                 scale /= new_unprojected_radius;
876         (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
877 }
878
879 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
880 {
881         float rand_pos[2];
882         float spread;
883         int diameter;
884
885         do {
886                 rand_pos[0] = BLI_rng_get_float(brush_rng) - 0.5f;
887                 rand_pos[1] = BLI_rng_get_float(brush_rng) - 0.5f;
888         } while (len_squared_v2(rand_pos) > SQUARE(0.5f));
889
890
891         if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
892                 diameter = 2 * brush->jitter_absolute;
893                 spread = 1.0;
894         }
895         else {
896                 diameter = 2 * BKE_brush_size_get(scene, brush);
897                 spread = brush->jitter;
898         }
899         /* find random position within a circle of diameter 1 */
900         jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
901         jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
902 }
903
904 void BKE_brush_randomize_texture_coords(UnifiedPaintSettings *ups, bool mask)
905 {
906         /* we multiply with brush radius as an optimization for the brush
907          * texture sampling functions */
908         if (mask) {
909                 ups->mask_tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
910                 ups->mask_tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
911         }
912         else {
913                 ups->tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
914                 ups->tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
915         }
916 }
917
918 /* Uses the brush curve control to find a strength value */
919 float BKE_brush_curve_strength(const Brush *br, float p, const float len)
920 {
921         float strength;
922
923         if (p >= len) return 0;
924         else p = p / len;
925
926         strength = curvemapping_evaluateF(br->curve, 0, p);
927
928         return strength;
929 }
930
931
932 /* Uses the brush curve control to find a strength value between 0 and 1 */
933 float BKE_brush_curve_strength_clamped(Brush *br, float p, const float len)
934 {
935         float strength = BKE_brush_curve_strength(br, p, len);
936
937         CLAMP(strength, 0.0f, 1.0f);
938
939         return strength;
940 }
941
942 /* TODO: should probably be unified with BrushPainter stuff? */
943 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side, bool use_secondary)
944 {
945         unsigned int *texcache = NULL;
946         MTex *mtex = (use_secondary) ? &br->mask_mtex : &br->mtex;
947         float intensity;
948         float rgba[4];
949         int ix, iy;
950         int side = half_side * 2;
951
952         if (mtex->tex) {
953                 float x, y, step = 2.0 / side, co[3];
954
955                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
956
957                 /* do normalized cannonical view coords for texture */
958                 for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
959                         for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
960                                 co[0] = x;
961                                 co[1] = y;
962                                 co[2] = 0.0f;
963
964                                 /* This is copied from displace modifier code */
965                                 /* TODO(sergey): brush are always cacheing with CM enabled for now. */
966                                 externtex(mtex, co, &intensity,
967                                           rgba, rgba + 1, rgba + 2, rgba + 3, 0, NULL, false, false);
968
969                                 ((char *)texcache)[(iy * side + ix) * 4] =
970                                 ((char *)texcache)[(iy * side + ix) * 4 + 1] =
971                                 ((char *)texcache)[(iy * side + ix) * 4 + 2] =
972                                 ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(intensity * 255.0f);
973                         }
974                 }
975         }
976
977         return texcache;
978 }
979
980
981 /**** Radial Control ****/
982 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br, bool secondary)
983 {
984         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
985         unsigned int *texcache;
986         int side = 128;
987         int half = side / 2;
988         int i, j;
989
990         curvemapping_initialize(br->curve);
991         texcache = BKE_brush_gen_texture_cache(br, half, secondary);
992         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
993         im->x = im->y = side;
994
995         for (i = 0; i < side; ++i) {
996                 for (j = 0; j < side; ++j) {
997                         float magn = sqrtf(pow2f(i - half) + pow2f(j - half));
998                         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamped(br, magn, half);
999                 }
1000         }
1001
1002         /* Modulate curve with texture */
1003         if (texcache) {
1004                 for (i = 0; i < side; ++i) {
1005                         for (j = 0; j < side; ++j) {
1006                                 const int col = texcache[i * side + j];
1007                                 im->rect_float[i * side + j] *=
1008                                         (((char *)&col)[0] + ((char *)&col)[1] + ((char *)&col)[2]) / 3.0f / 255.0f;
1009                         }
1010                 }
1011
1012                 MEM_freeN(texcache);
1013         }
1014
1015         return im;
1016 }