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