GPencil: Change Brush defaults for Block and Marker
[blender.git] / source / blender / blenkernel / intern / brush.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup bke
19  */
20
21 #include "MEM_guardedalloc.h"
22
23 #include "DNA_brush_types.h"
24 #include "DNA_scene_types.h"
25 #include "DNA_object_types.h"
26 #include "DNA_gpencil_types.h"
27
28 #include "BLI_math.h"
29 #include "BLI_rand.h"
30
31 #include "BKE_brush.h"
32 #include "BKE_colortools.h"
33 #include "BKE_context.h"
34 #include "BKE_library.h"
35 #include "BKE_library_query.h"
36 #include "BKE_library_remap.h"
37 #include "BKE_main.h"
38 #include "BKE_paint.h"
39 #include "BKE_texture.h"
40 #include "BKE_icons.h"
41
42 #include "IMB_colormanagement.h"
43 #include "IMB_imbuf.h"
44 #include "IMB_imbuf_types.h"
45
46 #include "RE_render_ext.h" /* externtex */
47
48 static RNG *brush_rng;
49
50 void BKE_brush_system_init(void)
51 {
52         brush_rng = BLI_rng_new(0);
53         BLI_rng_srandom(brush_rng, 31415682);
54 }
55
56 void BKE_brush_system_exit(void)
57 {
58         BLI_rng_free(brush_rng);
59 }
60
61
62 static void brush_defaults(Brush *brush)
63 {
64         brush->blend = 0;
65         brush->flag = 0;
66
67         brush->ob_mode = OB_MODE_ALL_PAINT;
68
69         /* BRUSH SCULPT TOOL SETTINGS */
70         brush->weight = 1.0f; /* weight of brush 0 - 1.0 */
71         brush->size = 35; /* radius of the brush in pixels */
72         brush->alpha = 0.5f; /* brush strength/intensity probably variable should be renamed? */
73         brush->autosmooth_factor = 0.0f;
74         brush->topology_rake_factor = 0.0f;
75         brush->crease_pinch_factor = 0.5f;
76         brush->sculpt_plane = SCULPT_DISP_DIR_AREA;
77         brush->plane_offset = 0.0f; /* how far above or below the plane that is found by averaging the faces */
78         brush->plane_trim = 0.5f;
79         brush->clone.alpha = 0.5f;
80         brush->normal_weight = 0.0f;
81         brush->fill_threshold = 0.2f;
82         brush->flag |= BRUSH_ALPHA_PRESSURE;
83
84         /* BRUSH PAINT TOOL SETTINGS */
85         brush->rgb[0] = 1.0f; /* default rgb color of the brush when painting - white */
86         brush->rgb[1] = 1.0f;
87         brush->rgb[2] = 1.0f;
88
89         zero_v3(brush->secondary_rgb);
90
91         /* BRUSH STROKE SETTINGS */
92         brush->flag |= (BRUSH_SPACE | BRUSH_SPACE_ATTEN);
93         brush->spacing = 10; /* how far each brush dot should be spaced as a percentage of brush diameter */
94
95         brush->smooth_stroke_radius = 75;
96         brush->smooth_stroke_factor = 0.9f;
97
98         brush->rate = 0.1f; /* time delay between dots of paint or sculpting when doing airbrush mode */
99
100         brush->jitter = 0.0f;
101
102         /* BRUSH TEXTURE SETTINGS */
103         BKE_texture_mtex_default(&brush->mtex);
104         BKE_texture_mtex_default(&brush->mask_mtex);
105
106         brush->texture_sample_bias = 0; /* value to added to texture samples */
107         brush->texture_overlay_alpha = 33;
108         brush->mask_overlay_alpha = 33;
109         brush->cursor_overlay_alpha = 33;
110         brush->overlay_flags = 0;
111
112         /* brush appearance  */
113
114         brush->add_col[0] = 1.00; /* add mode color is light red */
115         brush->add_col[1] = 0.39;
116         brush->add_col[2] = 0.39;
117
118         brush->sub_col[0] = 0.39; /* subtract mode color is light blue */
119         brush->sub_col[1] = 0.39;
120         brush->sub_col[2] = 1.00;
121
122         brush->stencil_pos[0] = 256;
123         brush->stencil_pos[1] = 256;
124
125         brush->stencil_dimension[0] = 256;
126         brush->stencil_dimension[1] = 256;
127
128 }
129
130 /* Datablock add/copy/free/make_local */
131
132 void BKE_brush_init(Brush *brush)
133 {
134         BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(brush, id));
135
136         /* enable fake user by default */
137         id_fake_user_set(&brush->id);
138
139         brush_defaults(brush);
140
141         brush->sculpt_tool = SCULPT_TOOL_DRAW; /* sculpting defaults to the draw tool for new brushes */
142
143         /* the default alpha falloff curve */
144         BKE_brush_curve_preset(brush, CURVE_PRESET_SMOOTH);
145 }
146
147 /**
148  * \note Resulting brush will have two users: one as a fake user, another is assumed to be used by the caller.
149  */
150 Brush *BKE_brush_add(Main *bmain, const char *name, const eObjectMode ob_mode)
151 {
152         Brush *brush;
153
154         brush = BKE_libblock_alloc(bmain, ID_BR, name, 0);
155
156         BKE_brush_init(brush);
157
158         brush->ob_mode = ob_mode;
159
160         return brush;
161 }
162
163 /* add grease pencil settings */
164 void BKE_brush_init_gpencil_settings(Brush *brush)
165 {
166         if (brush->gpencil_settings == NULL) {
167                 brush->gpencil_settings = MEM_callocN(sizeof(BrushGpencilSettings), "BrushGpencilSettings");
168         }
169
170         brush->gpencil_settings->draw_smoothlvl = 1;
171         brush->gpencil_settings->flag = 0;
172         brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
173         brush->gpencil_settings->draw_sensitivity = 1.0f;
174         brush->gpencil_settings->draw_strength = 1.0f;
175         brush->gpencil_settings->draw_jitter = 0.0f;
176         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
177         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PEN;
178         brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
179
180         /* curves */
181         brush->gpencil_settings->curve_sensitivity = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
182         brush->gpencil_settings->curve_strength = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
183         brush->gpencil_settings->curve_jitter = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
184 }
185
186 /* add a new gp-brush */
187 Brush *BKE_brush_add_gpencil(Main *bmain, ToolSettings *ts, const char *name)
188 {
189         Brush *brush;
190         Paint *paint = &ts->gp_paint->paint;
191         brush = BKE_brush_add(bmain, name, OB_MODE_PAINT_GPENCIL);
192
193         BKE_paint_brush_set(paint, brush);
194         id_us_min(&brush->id);
195
196         brush->size = 3;
197
198         /* grease pencil basic settings */
199         BKE_brush_init_gpencil_settings(brush);
200
201         /* return brush */
202         return brush;
203 }
204
205 /* grease pencil cumapping->preset */
206 typedef enum eGPCurveMappingPreset {
207         GPCURVE_PRESET_PENCIL = 0,
208         GPCURVE_PRESET_INK = 1,
209         GPCURVE_PRESET_INKNOISE = 2,
210         GPCURVE_PRESET_MARKER = 3
211 } eGPCurveMappingPreset;
212
213 static void brush_gpencil_curvemap_reset(CurveMap *cuma, int tot, int preset)
214 {
215         if (cuma->curve)
216                 MEM_freeN(cuma->curve);
217
218         cuma->totpoint = tot;
219         cuma->curve = MEM_callocN(cuma->totpoint * sizeof(CurveMapPoint), __func__);
220
221         switch (preset) {
222                 case GPCURVE_PRESET_PENCIL:
223                         cuma->curve[0].x = 0.0f;
224                         cuma->curve[0].y = 0.0f;
225                         cuma->curve[1].x = 0.75115f;
226                         cuma->curve[1].y = 0.25f;
227                         cuma->curve[2].x = 1.0f;
228                         cuma->curve[2].y = 1.0f;
229                         break;
230                 case GPCURVE_PRESET_INK:
231                         cuma->curve[0].x = 0.0f;
232                         cuma->curve[0].y = 0.0f;
233                         cuma->curve[1].x = 0.63448f;
234                         cuma->curve[1].y = 0.375f;
235                         cuma->curve[2].x = 1.0f;
236                         cuma->curve[2].y = 1.0f;
237                         break;
238                 case GPCURVE_PRESET_INKNOISE:
239                         cuma->curve[0].x = 0.0f;
240                         cuma->curve[0].y = 0.0f;
241                         cuma->curve[1].x = 0.63134f;
242                         cuma->curve[1].y = 0.3625f;
243                         cuma->curve[2].x = 1.0f;
244                         cuma->curve[2].y = 1.0f;
245                         break;
246                 case GPCURVE_PRESET_MARKER:
247                         cuma->curve[0].x = 0.0f;
248                         cuma->curve[0].y = 0.0f;
249                         cuma->curve[1].x = 0.38f;
250                         cuma->curve[1].y = 0.22f;
251                         cuma->curve[2].x = 0.65f;
252                         cuma->curve[2].y = 0.68f;
253                         cuma->curve[3].x = 1.0f;
254                         cuma->curve[3].y = 1.0f;
255                         break;
256         }
257
258         if (cuma->table) {
259                 MEM_freeN(cuma->table);
260                 cuma->table = NULL;
261         }
262 }
263
264 /* create a set of grease pencil presets */
265 void BKE_brush_gpencil_presets(bContext *C)
266 {
267 #define SMOOTH_STROKE_RADIUS 40
268 #define SMOOTH_STROKE_FACTOR 0.9f
269
270         ToolSettings *ts = CTX_data_tool_settings(C);
271         Paint *paint = &ts->gp_paint->paint;
272         Main *bmain = CTX_data_main(C);
273
274         Brush *brush, *deft;
275         CurveMapping *custom_curve;
276
277         /* Pencil brush */
278         brush = BKE_brush_add_gpencil(bmain, ts, "Draw Pencil");
279         brush->size = 25.0f;
280         brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
281         brush->gpencil_settings->draw_sensitivity = 1.0f;
282
283         brush->gpencil_settings->draw_strength = 0.6f;
284         brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
285
286         brush->gpencil_settings->draw_random_press = 0.0f;
287
288         brush->gpencil_settings->draw_jitter = 0.0f;
289         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
290
291         brush->gpencil_settings->draw_angle = 0.0f;
292         brush->gpencil_settings->draw_angle_factor = 0.0f;
293
294         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
295         brush->gpencil_settings->draw_smoothfac = 0.1f;
296         brush->gpencil_settings->draw_smoothlvl = 1;
297         brush->gpencil_settings->thick_smoothfac = 1.0f;
298         brush->gpencil_settings->thick_smoothlvl = 3;
299         brush->gpencil_settings->draw_subdivide = 1;
300         brush->gpencil_settings->draw_random_sub = 0.0f;
301         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PENCIL;
302         brush->gpencil_tool = GPAINT_TOOL_DRAW;
303
304         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
305         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
306
307         /* Pen brush */
308         brush = BKE_brush_add_gpencil(bmain, ts, "Draw Pen");
309         deft = brush; /* save default brush */
310         brush->size = 30.0f;
311         brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
312         brush->gpencil_settings->draw_sensitivity = 1.0f;
313
314         brush->gpencil_settings->draw_strength = 1.0f;
315         brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
316
317         brush->gpencil_settings->draw_random_press = 0.0f;
318         brush->gpencil_settings->draw_random_strength = 0.0f;
319
320         brush->gpencil_settings->draw_jitter = 0.0f;
321         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
322
323         brush->gpencil_settings->draw_angle = 0.0f;
324         brush->gpencil_settings->draw_angle_factor = 0.0f;
325
326         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
327         brush->gpencil_settings->draw_smoothfac = 0.1f;
328         brush->gpencil_settings->draw_smoothlvl = 1;
329         brush->gpencil_settings->draw_subdivide = 1;
330         brush->gpencil_settings->thick_smoothfac = 1.0f;
331         brush->gpencil_settings->thick_smoothlvl = 3;
332         brush->gpencil_settings->draw_random_sub = 0.0f;
333         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PEN;
334         brush->gpencil_tool = GPAINT_TOOL_DRAW;
335
336         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
337         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
338
339         /* Ink brush */
340         brush = BKE_brush_add_gpencil(bmain, ts, "Draw Ink");
341         brush->size = 60.0f;
342         brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
343         brush->gpencil_settings->draw_sensitivity = 1.6f;
344
345         brush->gpencil_settings->draw_strength = 1.0f;
346
347         brush->gpencil_settings->draw_random_press = 0.0f;
348
349         brush->gpencil_settings->draw_jitter = 0.0f;
350         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
351
352         brush->gpencil_settings->draw_angle = 0.0f;
353         brush->gpencil_settings->draw_angle_factor = 0.0f;
354
355         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
356         brush->gpencil_settings->draw_smoothfac = 0.1f;
357         brush->gpencil_settings->draw_smoothlvl = 1;
358         brush->gpencil_settings->thick_smoothfac = 1.0f;
359         brush->gpencil_settings->thick_smoothlvl = 3;
360         brush->gpencil_settings->draw_subdivide = 1;
361         brush->gpencil_settings->draw_random_sub = 0.0f;
362         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_INK;
363         brush->gpencil_tool = GPAINT_TOOL_DRAW;
364
365         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
366         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
367
368         /* Curve */
369         custom_curve = brush->gpencil_settings->curve_sensitivity;
370         curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
371         curvemapping_initialize(custom_curve);
372         brush_gpencil_curvemap_reset(custom_curve->cm, 3, GPCURVE_PRESET_INK);
373
374         /* Ink Noise brush */
375         brush = BKE_brush_add_gpencil(bmain, ts, "Draw Noise");
376         brush->size = 60.0f;
377         brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
378         brush->gpencil_settings->draw_sensitivity = 1.0f;
379
380         brush->gpencil_settings->draw_strength = 1.0f;
381
382         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_RANDOM;
383         brush->gpencil_settings->draw_random_press = 0.7f;
384         brush->gpencil_settings->draw_random_strength = 0.0f;
385
386         brush->gpencil_settings->draw_jitter = 0.0f;
387         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
388
389         brush->gpencil_settings->draw_angle = 0.0f;
390         brush->gpencil_settings->draw_angle_factor = 0.0f;
391
392         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
393         brush->gpencil_settings->draw_smoothfac = 0.1f;
394         brush->gpencil_settings->draw_smoothlvl = 2;
395         brush->gpencil_settings->thick_smoothfac = 0.5f;
396         brush->gpencil_settings->thick_smoothlvl = 2;
397         brush->gpencil_settings->draw_subdivide = 1;
398         brush->gpencil_settings->draw_random_sub = 0.0f;
399         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_INKNOISE;
400         brush->gpencil_tool = GPAINT_TOOL_DRAW;
401
402         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
403         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
404
405         /* Curve */
406         custom_curve = brush->gpencil_settings->curve_sensitivity;
407         curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
408         curvemapping_initialize(custom_curve);
409         brush_gpencil_curvemap_reset(custom_curve->cm, 3, GPCURVE_PRESET_INKNOISE);
410
411         /* Block Basic brush */
412         brush = BKE_brush_add_gpencil(bmain, ts, "Draw Block");
413         brush->size = 150.0f;
414         brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
415         brush->gpencil_settings->draw_sensitivity = 1.0f;
416
417         brush->gpencil_settings->draw_strength = 1.0f;
418         brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
419
420         brush->gpencil_settings->draw_random_press = 0.0f;
421
422         brush->gpencil_settings->draw_jitter = 0.0f;
423         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
424
425         brush->gpencil_settings->draw_angle = 0.0f;
426         brush->gpencil_settings->draw_angle_factor = 0.0f;
427
428         brush->gpencil_settings->draw_smoothfac = 0.1f;
429         brush->gpencil_settings->draw_smoothlvl = 1;
430         brush->gpencil_settings->thick_smoothfac = 1.0f;
431         brush->gpencil_settings->thick_smoothlvl = 3;
432         brush->gpencil_settings->draw_subdivide = 0;
433         brush->gpencil_settings->draw_random_sub = 0;
434         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_BLOCK;
435         brush->gpencil_tool = GPAINT_TOOL_DRAW;
436
437         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
438         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
439
440         /* Marker brush */
441         brush = BKE_brush_add_gpencil(bmain, ts, "Draw Marker");
442         brush->size = 80.0f;
443         brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
444         brush->gpencil_settings->draw_sensitivity = 1.0f;
445
446         brush->gpencil_settings->draw_strength = 1.0f;
447
448         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_RANDOM;
449         brush->gpencil_settings->draw_random_press = 0.374f;
450         brush->gpencil_settings->draw_random_strength = 0.0f;
451
452         brush->gpencil_settings->draw_jitter = 0.0f;
453         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
454
455         brush->gpencil_settings->draw_angle = DEG2RAD(20.0f);
456         brush->gpencil_settings->draw_angle_factor = 1.0f;
457
458         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
459         brush->gpencil_settings->draw_smoothfac = 0.1f;
460         brush->gpencil_settings->draw_smoothlvl = 1;
461         brush->gpencil_settings->thick_smoothfac = 1.0f;
462         brush->gpencil_settings->thick_smoothlvl = 3;
463         brush->gpencil_settings->draw_subdivide = 1;
464         brush->gpencil_settings->draw_random_sub = 0.0f;
465         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_MARKER;
466         brush->gpencil_tool = GPAINT_TOOL_DRAW;
467
468         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
469         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
470         /* Curve */
471         custom_curve = brush->gpencil_settings->curve_sensitivity;
472         curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
473         curvemapping_initialize(custom_curve);
474         brush_gpencil_curvemap_reset(custom_curve->cm, 4, GPCURVE_PRESET_MARKER);
475
476         /* Fill brush */
477         brush = BKE_brush_add_gpencil(bmain, ts, "Fill Area");
478         brush->size = 1.0f;
479         brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
480         brush->gpencil_settings->draw_sensitivity = 1.0f;
481         brush->gpencil_settings->fill_leak = 3;
482         brush->gpencil_settings->fill_threshold = 0.1f;
483         brush->gpencil_settings->fill_simplylvl = 1;
484         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_FILL;
485         brush->gpencil_tool = GPAINT_TOOL_FILL;
486
487         brush->gpencil_settings->draw_smoothfac = 0.1f;
488         brush->gpencil_settings->draw_smoothlvl = 1;
489         brush->gpencil_settings->thick_smoothfac = 1.0f;
490         brush->gpencil_settings->thick_smoothlvl = 3;
491         brush->gpencil_settings->draw_subdivide = 1;
492
493         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
494         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
495
496         brush->gpencil_settings->draw_strength = 1.0f;
497
498         /* Soft Eraser brush */
499         brush = BKE_brush_add_gpencil(bmain, ts, "Eraser Soft");
500         brush->size = 30.0f;
501         brush->gpencil_settings->draw_strength = 0.5f;
502         brush->gpencil_settings->flag |= (GP_BRUSH_ENABLE_CURSOR | GP_BRUSH_DEFAULT_ERASER);
503         brush->gpencil_settings->flag |= GP_BRUSH_USE_PRESSURE;
504         brush->gpencil_settings->flag |= GP_BRUSH_USE_STENGTH_PRESSURE;
505         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_SOFT;
506         brush->gpencil_tool = GPAINT_TOOL_ERASE;
507         brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_SOFT;
508         brush->gpencil_settings->era_strength_f = 100.0f;
509         brush->gpencil_settings->era_thickness_f = 10.0f;
510
511         /* Hard Eraser brush */
512         brush = BKE_brush_add_gpencil(bmain, ts, "Eraser Hard");
513         brush->size = 30.0f;
514         brush->gpencil_settings->draw_strength = 1.0f;
515         brush->gpencil_settings->flag |= (GP_BRUSH_ENABLE_CURSOR | GP_BRUSH_DEFAULT_ERASER);
516         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_HARD;
517         brush->gpencil_tool = GPAINT_TOOL_ERASE;
518         brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_SOFT;
519         brush->gpencil_settings->era_strength_f = 100.0f;
520         brush->gpencil_settings->era_thickness_f = 50.0f;
521
522         /* Point Eraser brush */
523         brush = BKE_brush_add_gpencil(bmain, ts, "Eraser Point");
524         brush->size = 30.0f;
525         brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
526         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_HARD;
527         brush->gpencil_tool = GPAINT_TOOL_ERASE;
528         brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_HARD;
529
530         /* Stroke Eraser brush */
531         brush = BKE_brush_add_gpencil(bmain, ts, "Eraser Stroke");
532         brush->size = 30.0f;
533         brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
534         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_ERASE_STROKE;
535         brush->gpencil_tool = GPAINT_TOOL_ERASE;
536         brush->gpencil_settings->eraser_mode = GP_BRUSH_ERASER_STROKE;
537
538         /* set default brush */
539         BKE_paint_brush_set(paint, deft);
540
541 }
542
543 void BKE_brush_update_material(Main *bmain, Material *ma, Brush *exclude_brush)
544 {
545         for (Brush *brush = bmain->brushes.first; brush; brush = brush->id.next) {
546                 if ((exclude_brush != NULL) && (brush == exclude_brush)) {
547                         continue;
548                 }
549
550                 if (brush->gpencil_settings != NULL) {
551                         BrushGpencilSettings *gpencil_settings = brush->gpencil_settings;
552                         if (((gpencil_settings->flag & GP_BRUSH_MATERIAL_PINNED) == 0) &&
553                             (gpencil_settings->material != ma))
554                         {
555                                 gpencil_settings->material = ma;
556                         }
557                 }
558         }
559 }
560
561 struct Brush *BKE_brush_first_search(struct Main *bmain, const eObjectMode ob_mode)
562 {
563         Brush *brush;
564
565         for (brush = bmain->brushes.first; brush; brush = brush->id.next) {
566                 if (brush->ob_mode & ob_mode)
567                         return brush;
568         }
569         return NULL;
570 }
571
572 /**
573  * Only copy internal data of Brush ID from source to already allocated/initialized destination.
574  * You probably never want to use that directly, use BKE_id_copy or BKE_id_copy_ex for typical needs.
575  *
576  * WARNING! This function will not handle ID user count!
577  *
578  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
579  */
580 void BKE_brush_copy_data(Main *UNUSED(bmain), Brush *brush_dst, const Brush *brush_src, const int flag)
581 {
582         if (brush_src->icon_imbuf) {
583                 brush_dst->icon_imbuf = IMB_dupImBuf(brush_src->icon_imbuf);
584         }
585
586         if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
587                 BKE_previewimg_id_copy(&brush_dst->id, &brush_src->id);
588         }
589         else {
590                 brush_dst->preview = NULL;
591         }
592
593         brush_dst->curve = curvemapping_copy(brush_src->curve);
594         if (brush_src->gpencil_settings != NULL) {
595                 brush_dst->gpencil_settings = MEM_dupallocN(brush_src->gpencil_settings);
596                 brush_dst->gpencil_settings->curve_sensitivity = curvemapping_copy(brush_src->gpencil_settings->curve_sensitivity);
597                 brush_dst->gpencil_settings->curve_strength = curvemapping_copy(brush_src->gpencil_settings->curve_strength);
598                 brush_dst->gpencil_settings->curve_jitter = curvemapping_copy(brush_src->gpencil_settings->curve_jitter);
599         }
600
601         /* enable fake user by default */
602         id_fake_user_set(&brush_dst->id);
603 }
604
605 Brush *BKE_brush_copy(Main *bmain, const Brush *brush)
606 {
607         Brush *brush_copy;
608         BKE_id_copy(bmain, &brush->id, (ID **)&brush_copy);
609         return brush_copy;
610 }
611
612 /** Free (or release) any data used by this brush (does not free the brush itself). */
613 void BKE_brush_free(Brush *brush)
614 {
615         if (brush->icon_imbuf) {
616                 IMB_freeImBuf(brush->icon_imbuf);
617         }
618         curvemapping_free(brush->curve);
619
620         if (brush->gpencil_settings != NULL) {
621                 curvemapping_free(brush->gpencil_settings->curve_sensitivity);
622                 curvemapping_free(brush->gpencil_settings->curve_strength);
623                 curvemapping_free(brush->gpencil_settings->curve_jitter);
624                 MEM_SAFE_FREE(brush->gpencil_settings);
625         }
626
627         MEM_SAFE_FREE(brush->gradient);
628
629
630         BKE_previewimg_free(&(brush->preview));
631 }
632
633 void BKE_brush_make_local(Main *bmain, Brush *brush, const bool lib_local)
634 {
635         bool is_local = false, is_lib = false;
636
637         /* - only lib users: do nothing (unless force_local is set)
638          * - only local users: set flag
639          * - mixed: make copy
640          */
641
642         if (!ID_IS_LINKED(brush)) {
643                 return;
644         }
645
646         if (brush->clone.image) {
647                 /* Special case: ima always local immediately. Clone image should only have one user anyway. */
648                 id_make_local(bmain, &brush->clone.image->id, false, false);
649         }
650
651         BKE_library_ID_test_usages(bmain, brush, &is_local, &is_lib);
652
653         if (lib_local || is_local) {
654                 if (!is_lib) {
655                         id_clear_lib_data(bmain, &brush->id);
656                         BKE_id_expand_local(bmain, &brush->id);
657
658                         /* enable fake user by default */
659                         id_fake_user_set(&brush->id);
660                 }
661                 else {
662                         Brush *brush_new = BKE_brush_copy(bmain, brush);  /* Ensures FAKE_USER is set */
663
664                         brush_new->id.us = 0;
665
666                         /* setting newid is mandatory for complex make_lib_local logic... */
667                         ID_NEW_SET(brush, brush_new);
668
669                         if (!lib_local) {
670                                 BKE_libblock_remap(bmain, brush, brush_new, ID_REMAP_SKIP_INDIRECT_USAGE);
671                         }
672                 }
673         }
674 }
675
676 void BKE_brush_debug_print_state(Brush *br)
677 {
678         /* create a fake brush and set it to the defaults */
679         Brush def = {{NULL}};
680         brush_defaults(&def);
681
682 #define BR_TEST(field, t)                                       \
683         if (br->field != def.field)                             \
684                 printf("br->" #field " = %" #t ";\n", br->field)
685
686 #define BR_TEST_FLAG(_f)                                                        \
687         if ((br->flag & _f) && !(def.flag & _f))                \
688                 printf("br->flag |= " #_f ";\n");                       \
689         else if (!(br->flag & _f) && (def.flag & _f))   \
690                 printf("br->flag &= ~" #_f ";\n")
691
692 #define BR_TEST_FLAG_OVERLAY(_f)                                                        \
693         if ((br->overlay_flags & _f) && !(def.overlay_flags & _f))              \
694                 printf("br->overlay_flags |= " #_f ";\n");                      \
695         else if (!(br->overlay_flags & _f) && (def.overlay_flags & _f)) \
696                 printf("br->overlay_flags &= ~" #_f ";\n")
697
698         /* print out any non-default brush state */
699         BR_TEST(normal_weight, f);
700
701         BR_TEST(blend, d);
702         BR_TEST(size, d);
703
704         /* br->flag */
705         BR_TEST_FLAG(BRUSH_AIRBRUSH);
706         BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
707         BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
708         BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
709         BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
710         BR_TEST_FLAG(BRUSH_ANCHORED);
711         BR_TEST_FLAG(BRUSH_DIR_IN);
712         BR_TEST_FLAG(BRUSH_SPACE);
713         BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
714         BR_TEST_FLAG(BRUSH_PERSISTENT);
715         BR_TEST_FLAG(BRUSH_ACCUMULATE);
716         BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
717         BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
718         BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
719         BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
720         BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
721         BR_TEST_FLAG(BRUSH_LOCK_SIZE);
722         BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
723         BR_TEST_FLAG(BRUSH_DRAG_DOT);
724         BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
725         BR_TEST_FLAG(BRUSH_PLANE_TRIM);
726         BR_TEST_FLAG(BRUSH_FRONTFACE);
727         BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
728
729         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR);
730         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY);
731         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY);
732         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE);
733         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE);
734         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE);
735
736         BR_TEST(jitter, f);
737         BR_TEST(spacing, d);
738         BR_TEST(smooth_stroke_radius, d);
739         BR_TEST(smooth_stroke_factor, f);
740         BR_TEST(rate, f);
741
742         BR_TEST(alpha, f);
743
744         BR_TEST(sculpt_plane, d);
745
746         BR_TEST(plane_offset, f);
747
748         BR_TEST(autosmooth_factor, f);
749
750         BR_TEST(topology_rake_factor, f);
751
752         BR_TEST(crease_pinch_factor, f);
753
754         BR_TEST(plane_trim, f);
755
756         BR_TEST(texture_sample_bias, f);
757         BR_TEST(texture_overlay_alpha, d);
758
759         BR_TEST(add_col[0], f);
760         BR_TEST(add_col[1], f);
761         BR_TEST(add_col[2], f);
762         BR_TEST(sub_col[0], f);
763         BR_TEST(sub_col[1], f);
764         BR_TEST(sub_col[2], f);
765
766         printf("\n");
767
768 #undef BR_TEST
769 #undef BR_TEST_FLAG
770 }
771
772 void BKE_brush_sculpt_reset(Brush *br)
773 {
774         /* enable this to see any non-default
775          * settings used by a brush: */
776         // BKE_brush_debug_print_state(br);
777
778         brush_defaults(br);
779         BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
780
781         switch (br->sculpt_tool) {
782                 case SCULPT_TOOL_CLAY:
783                         br->flag |= BRUSH_FRONTFACE;
784                         break;
785                 case SCULPT_TOOL_CREASE:
786                         br->flag |= BRUSH_DIR_IN;
787                         br->alpha = 0.25;
788                         break;
789                 case SCULPT_TOOL_FILL:
790                         br->add_col[1] = 1;
791                         br->sub_col[0] = 0.25;
792                         br->sub_col[1] = 1;
793                         break;
794                 case SCULPT_TOOL_FLATTEN:
795                         br->add_col[1] = 1;
796                         br->sub_col[0] = 0.25;
797                         br->sub_col[1] = 1;
798                         break;
799                 case SCULPT_TOOL_INFLATE:
800                         br->add_col[0] = 0.750000;
801                         br->add_col[1] = 0.750000;
802                         br->add_col[2] = 0.750000;
803                         br->sub_col[0] = 0.250000;
804                         br->sub_col[1] = 0.250000;
805                         br->sub_col[2] = 0.250000;
806                         break;
807                 case SCULPT_TOOL_NUDGE:
808                         br->add_col[0] = 0.250000;
809                         br->add_col[1] = 1.000000;
810                         br->add_col[2] = 0.250000;
811                         break;
812                 case SCULPT_TOOL_PINCH:
813                         br->add_col[0] = 0.750000;
814                         br->add_col[1] = 0.750000;
815                         br->add_col[2] = 0.750000;
816                         br->sub_col[0] = 0.250000;
817                         br->sub_col[1] = 0.250000;
818                         br->sub_col[2] = 0.250000;
819                         break;
820                 case SCULPT_TOOL_SCRAPE:
821                         br->add_col[1] = 1.000000;
822                         br->sub_col[0] = 0.250000;
823                         br->sub_col[1] = 1.000000;
824                         break;
825                 case SCULPT_TOOL_ROTATE:
826                         br->alpha = 1.0;
827                         break;
828                 case SCULPT_TOOL_SMOOTH:
829                         br->flag &= ~BRUSH_SPACE_ATTEN;
830                         br->spacing = 5;
831                         br->add_col[0] = 0.750000;
832                         br->add_col[1] = 0.750000;
833                         br->add_col[2] = 0.750000;
834                         break;
835                 case SCULPT_TOOL_GRAB:
836                 case SCULPT_TOOL_SNAKE_HOOK:
837                 case SCULPT_TOOL_THUMB:
838                         br->size = 75;
839                         br->flag &= ~BRUSH_ALPHA_PRESSURE;
840                         br->flag &= ~BRUSH_SPACE;
841                         br->flag &= ~BRUSH_SPACE_ATTEN;
842                         br->add_col[0] = 0.250000;
843                         br->add_col[1] = 1.000000;
844                         br->add_col[2] = 0.250000;
845                         break;
846                 default:
847                         break;
848         }
849 }
850
851 /**
852  * Library Operations
853  */
854 void BKE_brush_curve_preset(Brush *b, eCurveMappingPreset preset)
855 {
856         CurveMap *cm = NULL;
857
858         if (!b->curve)
859                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
860
861         cm = b->curve->cm;
862         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
863
864         b->curve->preset = preset;
865         curvemap_reset(cm, &b->curve->clipr, b->curve->preset, CURVEMAP_SLOPE_NEGATIVE);
866         curvemapping_changed(b->curve, false);
867 }
868
869 /* Generic texture sampler for 3D painting systems. point has to be either in
870  * region space mouse coordinates, or 3d world coordinates for 3D mapping.
871  *
872  * rgba outputs straight alpha. */
873 float BKE_brush_sample_tex_3d(const Scene *scene, const Brush *br,
874                               const float point[3],
875                               float rgba[4], const int thread,
876                               struct ImagePool *pool)
877 {
878         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
879         const MTex *mtex = &br->mtex;
880         float intensity = 1.0;
881         bool hasrgb = false;
882
883         if (!mtex->tex) {
884                 intensity = 1;
885         }
886         else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
887                 /* Get strength by feeding the vertex
888                  * location directly into a texture */
889                 hasrgb = externtex(mtex, point, &intensity,
890                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
891         }
892         else if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
893                 float rotation = -mtex->rot;
894                 float point_2d[2] = {point[0], point[1]};
895                 float x, y;
896                 float co[3];
897
898                 x = point_2d[0] - br->stencil_pos[0];
899                 y = point_2d[1] - br->stencil_pos[1];
900
901                 if (rotation > 0.001f || rotation < -0.001f) {
902                         const float angle    = atan2f(y, x) + rotation;
903                         const float flen     = sqrtf(x * x + y * y);
904
905                         x = flen * cosf(angle);
906                         y = flen * sinf(angle);
907                 }
908
909                 if (fabsf(x) > br->stencil_dimension[0] || fabsf(y) > br->stencil_dimension[1]) {
910                         zero_v4(rgba);
911                         return 0.0f;
912                 }
913                 x /= (br->stencil_dimension[0]);
914                 y /= (br->stencil_dimension[1]);
915
916                 co[0] = x;
917                 co[1] = y;
918                 co[2] = 0.0f;
919
920                 hasrgb = externtex(mtex, co, &intensity,
921                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
922         }
923         else {
924                 float rotation = -mtex->rot;
925                 float point_2d[2] = {point[0], point[1]};
926                 float x = 0.0f, y = 0.0f; /* Quite warnings */
927                 float invradius = 1.0f; /* Quite warnings */
928                 float co[3];
929
930                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
931                         /* keep coordinates relative to mouse */
932
933                         rotation += ups->brush_rotation;
934
935                         x = point_2d[0] - ups->tex_mouse[0];
936                         y = point_2d[1] - ups->tex_mouse[1];
937
938                         /* use pressure adjusted size for fixed mode */
939                         invradius = 1.0f / ups->pixel_radius;
940                 }
941                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
942                         /* leave the coordinates relative to the screen */
943
944                         /* use unadjusted size for tiled mode */
945                         invradius = 1.0f / BKE_brush_size_get(scene, br);
946
947                         x = point_2d[0];
948                         y = point_2d[1];
949                 }
950                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
951                         rotation += ups->brush_rotation;
952                         /* these contain a random coordinate */
953                         x = point_2d[0] - ups->tex_mouse[0];
954                         y = point_2d[1] - ups->tex_mouse[1];
955
956                         invradius = 1.0f / ups->pixel_radius;
957                 }
958
959                 x *= invradius;
960                 y *= invradius;
961
962                 /* it is probably worth optimizing for those cases where
963                  * the texture is not rotated by skipping the calls to
964                  * atan2, sqrtf, sin, and cos. */
965                 if (rotation > 0.001f || rotation < -0.001f) {
966                         const float angle    = atan2f(y, x) + rotation;
967                         const float flen     = sqrtf(x * x + y * y);
968
969                         x = flen * cosf(angle);
970                         y = flen * sinf(angle);
971                 }
972
973                 co[0] = x;
974                 co[1] = y;
975                 co[2] = 0.0f;
976
977                 hasrgb = externtex(mtex, co, &intensity,
978                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
979         }
980
981         intensity += br->texture_sample_bias;
982
983         if (!hasrgb) {
984                 rgba[0] = intensity;
985                 rgba[1] = intensity;
986                 rgba[2] = intensity;
987                 rgba[3] = 1.0f;
988         }
989         /* For consistency, sampling always returns color in linear space */
990         else if (ups->do_linear_conversion) {
991                 IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, ups->colorspace);
992         }
993
994         return intensity;
995 }
996
997 float BKE_brush_sample_masktex(const Scene *scene, Brush *br,
998                                const float point[2],
999                                const int thread,
1000                                struct ImagePool *pool)
1001 {
1002         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1003         MTex *mtex = &br->mask_mtex;
1004         float rgba[4], intensity;
1005
1006         if (!mtex->tex) {
1007                 return 1.0f;
1008         }
1009         if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
1010                 float rotation = -mtex->rot;
1011                 float point_2d[2] = {point[0], point[1]};
1012                 float x, y;
1013                 float co[3];
1014
1015                 x = point_2d[0] - br->mask_stencil_pos[0];
1016                 y = point_2d[1] - br->mask_stencil_pos[1];
1017
1018                 if (rotation > 0.001f || rotation < -0.001f) {
1019                         const float angle    = atan2f(y, x) + rotation;
1020                         const float flen     = sqrtf(x * x + y * y);
1021
1022                         x = flen * cosf(angle);
1023                         y = flen * sinf(angle);
1024                 }
1025
1026                 if (fabsf(x) > br->mask_stencil_dimension[0] || fabsf(y) > br->mask_stencil_dimension[1]) {
1027                         zero_v4(rgba);
1028                         return 0.0f;
1029                 }
1030                 x /= (br->mask_stencil_dimension[0]);
1031                 y /= (br->mask_stencil_dimension[1]);
1032
1033                 co[0] = x;
1034                 co[1] = y;
1035                 co[2] = 0.0f;
1036
1037                 externtex(mtex, co, &intensity,
1038                           rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1039         }
1040         else {
1041                 float rotation = -mtex->rot;
1042                 float point_2d[2] = {point[0], point[1]};
1043                 float x = 0.0f, y = 0.0f; /* Quite warnings */
1044                 float invradius = 1.0f; /* Quite warnings */
1045                 float co[3];
1046
1047                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
1048                         /* keep coordinates relative to mouse */
1049
1050                         rotation += ups->brush_rotation_sec;
1051
1052                         x = point_2d[0] - ups->mask_tex_mouse[0];
1053                         y = point_2d[1] - ups->mask_tex_mouse[1];
1054
1055                         /* use pressure adjusted size for fixed mode */
1056                         invradius = 1.0f / ups->pixel_radius;
1057                 }
1058                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
1059                         /* leave the coordinates relative to the screen */
1060
1061                         /* use unadjusted size for tiled mode */
1062                         invradius = 1.0f / BKE_brush_size_get(scene, br);
1063
1064                         x = point_2d[0];
1065                         y = point_2d[1];
1066                 }
1067                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
1068                         rotation += ups->brush_rotation_sec;
1069                         /* these contain a random coordinate */
1070                         x = point_2d[0] - ups->mask_tex_mouse[0];
1071                         y = point_2d[1] - ups->mask_tex_mouse[1];
1072
1073                         invradius = 1.0f / ups->pixel_radius;
1074                 }
1075
1076                 x *= invradius;
1077                 y *= invradius;
1078
1079                 /* it is probably worth optimizing for those cases where
1080                  * the texture is not rotated by skipping the calls to
1081                  * atan2, sqrtf, sin, and cos. */
1082                 if (rotation > 0.001f || rotation < -0.001f) {
1083                         const float angle    = atan2f(y, x) + rotation;
1084                         const float flen     = sqrtf(x * x + y * y);
1085
1086                         x = flen * cosf(angle);
1087                         y = flen * sinf(angle);
1088                 }
1089
1090                 co[0] = x;
1091                 co[1] = y;
1092                 co[2] = 0.0f;
1093
1094                 externtex(mtex, co, &intensity,
1095                           rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1096         }
1097
1098         CLAMP(intensity, 0.0f, 1.0f);
1099
1100         switch (br->mask_pressure) {
1101                 case BRUSH_MASK_PRESSURE_CUTOFF:
1102                         intensity  = ((1.0f - intensity) < ups->size_pressure_value) ? 1.0f : 0.0f;
1103                         break;
1104                 case BRUSH_MASK_PRESSURE_RAMP:
1105                         intensity = ups->size_pressure_value + intensity * (1.0f - ups->size_pressure_value);
1106                         break;
1107                 default:
1108                         break;
1109         }
1110
1111         return intensity;
1112 }
1113
1114 /* Unified Size / Strength / Color */
1115
1116 /* XXX: be careful about setting size and unprojected radius
1117  * because they depend on one another
1118  * these functions do not set the other corresponding value
1119  * this can lead to odd behavior if size and unprojected
1120  * radius become inconsistent.
1121  * the biggest problem is that it isn't possible to change
1122  * unprojected radius because a view context is not
1123  * available.  my usual solution to this is to use the
1124  * ratio of change of the size to change the unprojected
1125  * radius.  Not completely convinced that is correct.
1126  * In any case, a better solution is needed to prevent
1127  * inconsistency. */
1128
1129
1130 const float *BKE_brush_color_get(const struct Scene *scene, const struct Brush *brush)
1131 {
1132         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1133         return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->rgb : brush->rgb;
1134 }
1135
1136 const float *BKE_brush_secondary_color_get(const struct Scene *scene, const struct Brush *brush)
1137 {
1138         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1139         return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->secondary_rgb : brush->secondary_rgb;
1140 }
1141
1142 void BKE_brush_color_set(struct Scene *scene, struct Brush *brush, const float color[3])
1143 {
1144         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1145
1146         if (ups->flag & UNIFIED_PAINT_COLOR)
1147                 copy_v3_v3(ups->rgb, color);
1148         else
1149                 copy_v3_v3(brush->rgb, color);
1150 }
1151
1152 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
1153 {
1154         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1155
1156         /* make sure range is sane */
1157         CLAMP(size, 1, MAX_BRUSH_PIXEL_RADIUS);
1158
1159         if (ups->flag & UNIFIED_PAINT_SIZE)
1160                 ups->size = size;
1161         else
1162                 brush->size = size;
1163 }
1164
1165 int BKE_brush_size_get(const Scene *scene, const Brush *brush)
1166 {
1167         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1168         int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
1169
1170         return size;
1171 }
1172
1173 bool BKE_brush_use_locked_size(const Scene *scene, const Brush *brush)
1174 {
1175         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
1176
1177         return (us_flag & UNIFIED_PAINT_SIZE) ?
1178                (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
1179                (brush->flag & BRUSH_LOCK_SIZE);
1180 }
1181
1182 bool BKE_brush_use_size_pressure(const Scene *scene, const Brush *brush)
1183 {
1184         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
1185
1186         return (us_flag & UNIFIED_PAINT_SIZE) ?
1187                (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
1188                (brush->flag & BRUSH_SIZE_PRESSURE);
1189 }
1190
1191 bool BKE_brush_use_alpha_pressure(const Scene *scene, const Brush *brush)
1192 {
1193         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
1194
1195         return (us_flag & UNIFIED_PAINT_ALPHA) ?
1196                (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
1197                (brush->flag & BRUSH_ALPHA_PRESSURE);
1198 }
1199
1200 bool BKE_brush_sculpt_has_secondary_color(const Brush *brush)
1201 {
1202         return ELEM(
1203                 brush->sculpt_tool, SCULPT_TOOL_BLOB, SCULPT_TOOL_DRAW,
1204                 SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_CLAY_STRIPS,
1205                 SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE, SCULPT_TOOL_LAYER,
1206                 SCULPT_TOOL_FLATTEN, SCULPT_TOOL_FILL, SCULPT_TOOL_SCRAPE,
1207                 SCULPT_TOOL_MASK);
1208 }
1209
1210 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
1211 {
1212         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1213
1214         if (ups->flag & UNIFIED_PAINT_SIZE)
1215                 ups->unprojected_radius = unprojected_radius;
1216         else
1217                 brush->unprojected_radius = unprojected_radius;
1218 }
1219
1220 float BKE_brush_unprojected_radius_get(const Scene *scene, const Brush *brush)
1221 {
1222         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1223
1224         return (ups->flag & UNIFIED_PAINT_SIZE) ?
1225                ups->unprojected_radius :
1226                brush->unprojected_radius;
1227 }
1228
1229 void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
1230 {
1231         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1232
1233         if (ups->flag & UNIFIED_PAINT_ALPHA)
1234                 ups->alpha = alpha;
1235         else
1236                 brush->alpha = alpha;
1237 }
1238
1239 float BKE_brush_alpha_get(const Scene *scene, const Brush *brush)
1240 {
1241         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1242
1243         return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
1244 }
1245
1246 float BKE_brush_weight_get(const Scene *scene, const Brush *brush)
1247 {
1248         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1249
1250         return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
1251 }
1252
1253 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
1254 {
1255         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1256
1257         if (ups->flag & UNIFIED_PAINT_WEIGHT)
1258                 ups->weight = value;
1259         else
1260                 brush->weight = value;
1261 }
1262
1263 /* scale unprojected radius to reflect a change in the brush's 2D size */
1264 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
1265                                         int new_brush_size,
1266                                         int old_brush_size)
1267 {
1268         float scale = new_brush_size;
1269         /* avoid division by zero */
1270         if (old_brush_size != 0)
1271                 scale /= (float)old_brush_size;
1272         (*unprojected_radius) *= scale;
1273 }
1274
1275 /* scale brush size to reflect a change in the brush's unprojected radius */
1276 void BKE_brush_scale_size(
1277         int *r_brush_size,
1278         float new_unprojected_radius,
1279         float old_unprojected_radius)
1280 {
1281         float scale = new_unprojected_radius;
1282         /* avoid division by zero */
1283         if (old_unprojected_radius != 0)
1284                 scale /= new_unprojected_radius;
1285         (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
1286 }
1287
1288 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
1289 {
1290         float rand_pos[2];
1291         float spread;
1292         int diameter;
1293
1294         do {
1295                 rand_pos[0] = BLI_rng_get_float(brush_rng) - 0.5f;
1296                 rand_pos[1] = BLI_rng_get_float(brush_rng) - 0.5f;
1297         } while (len_squared_v2(rand_pos) > SQUARE(0.5f));
1298
1299
1300         if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
1301                 diameter = 2 * brush->jitter_absolute;
1302                 spread = 1.0;
1303         }
1304         else {
1305                 diameter = 2 * BKE_brush_size_get(scene, brush);
1306                 spread = brush->jitter;
1307         }
1308         /* find random position within a circle of diameter 1 */
1309         jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
1310         jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
1311 }
1312
1313 void BKE_brush_randomize_texture_coords(UnifiedPaintSettings *ups, bool mask)
1314 {
1315         /* we multiply with brush radius as an optimization for the brush
1316          * texture sampling functions */
1317         if (mask) {
1318                 ups->mask_tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1319                 ups->mask_tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1320         }
1321         else {
1322                 ups->tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1323                 ups->tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1324         }
1325 }
1326
1327 /* Uses the brush curve control to find a strength value */
1328 float BKE_brush_curve_strength(const Brush *br, float p, const float len)
1329 {
1330         float strength;
1331
1332         if (p >= len) return 0;
1333         else p = p / len;
1334
1335         strength = curvemapping_evaluateF(br->curve, 0, p);
1336
1337         return strength;
1338 }
1339
1340
1341 /* Uses the brush curve control to find a strength value between 0 and 1 */
1342 float BKE_brush_curve_strength_clamped(Brush *br, float p, const float len)
1343 {
1344         float strength = BKE_brush_curve_strength(br, p, len);
1345
1346         CLAMP(strength, 0.0f, 1.0f);
1347
1348         return strength;
1349 }
1350
1351 /* TODO: should probably be unified with BrushPainter stuff? */
1352 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side, bool use_secondary)
1353 {
1354         unsigned int *texcache = NULL;
1355         MTex *mtex = (use_secondary) ? &br->mask_mtex : &br->mtex;
1356         float intensity;
1357         float rgba[4];
1358         int ix, iy;
1359         int side = half_side * 2;
1360
1361         if (mtex->tex) {
1362                 float x, y, step = 2.0 / side, co[3];
1363
1364                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1365
1366                 /* do normalized canonical view coords for texture */
1367                 for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
1368                         for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
1369                                 co[0] = x;
1370                                 co[1] = y;
1371                                 co[2] = 0.0f;
1372
1373                                 /* This is copied from displace modifier code */
1374                                 /* TODO(sergey): brush are always caching with CM enabled for now. */
1375                                 externtex(mtex, co, &intensity,
1376                                           rgba, rgba + 1, rgba + 2, rgba + 3, 0, NULL, false, false);
1377
1378                                 ((char *)texcache)[(iy * side + ix) * 4] =
1379                                 ((char *)texcache)[(iy * side + ix) * 4 + 1] =
1380                                 ((char *)texcache)[(iy * side + ix) * 4 + 2] =
1381                                 ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(intensity * 255.0f);
1382                         }
1383                 }
1384         }
1385
1386         return texcache;
1387 }
1388
1389
1390 /**** Radial Control ****/
1391 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br, bool secondary)
1392 {
1393         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1394         unsigned int *texcache;
1395         int side = 128;
1396         int half = side / 2;
1397         int i, j;
1398
1399         curvemapping_initialize(br->curve);
1400         texcache = BKE_brush_gen_texture_cache(br, half, secondary);
1401         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1402         im->x = im->y = side;
1403
1404         for (i = 0; i < side; ++i) {
1405                 for (j = 0; j < side; ++j) {
1406                         float magn = sqrtf(pow2f(i - half) + pow2f(j - half));
1407                         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamped(br, magn, half);
1408                 }
1409         }
1410
1411         /* Modulate curve with texture */
1412         if (texcache) {
1413                 for (i = 0; i < side; ++i) {
1414                         for (j = 0; j < side; ++j) {
1415                                 const int col = texcache[i * side + j];
1416                                 im->rect_float[i * side + j] *=
1417                                         (((char *)&col)[0] + ((char *)&col)[1] + ((char *)&col)[2]) / 3.0f / 255.0f;
1418                         }
1419                 }
1420
1421                 MEM_freeN(texcache);
1422         }
1423
1424         return im;
1425 }