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