GPencil: Only brushes with pinned materials have materials
[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
419         brush->gpencil_settings->draw_random_press = 0.0f;
420
421         brush->gpencil_settings->draw_jitter = 0.0f;
422         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
423
424         brush->gpencil_settings->draw_angle = 0.0f;
425         brush->gpencil_settings->draw_angle_factor = 0.0f;
426
427         brush->gpencil_settings->draw_smoothfac = 0.1f;
428         brush->gpencil_settings->draw_smoothlvl = 1;
429         brush->gpencil_settings->thick_smoothfac = 1.0f;
430         brush->gpencil_settings->thick_smoothlvl = 3;
431         brush->gpencil_settings->draw_subdivide = 0;
432         brush->gpencil_settings->draw_random_sub = 0;
433         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_BLOCK;
434         brush->gpencil_tool = GPAINT_TOOL_DRAW;
435
436         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
437         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
438
439         /* Marker brush */
440         brush = BKE_brush_add_gpencil(bmain, ts, "Draw Marker");
441         brush->size = 80.0f;
442         brush->gpencil_settings->flag |= (GP_BRUSH_USE_PRESSURE | GP_BRUSH_ENABLE_CURSOR);
443         brush->gpencil_settings->draw_sensitivity = 1.0f;
444
445         brush->gpencil_settings->draw_strength = 1.0f;
446
447         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_RANDOM;
448         brush->gpencil_settings->draw_random_press = 0.374f;
449         brush->gpencil_settings->draw_random_strength = 0.0f;
450
451         brush->gpencil_settings->draw_jitter = 0.0f;
452         brush->gpencil_settings->flag |= GP_BRUSH_USE_JITTER_PRESSURE;
453
454         brush->gpencil_settings->draw_angle = DEG2RAD(20.0f);
455         brush->gpencil_settings->draw_angle_factor = 1.0f;
456
457         brush->gpencil_settings->flag |= GP_BRUSH_GROUP_SETTINGS;
458         brush->gpencil_settings->draw_smoothfac = 0.1f;
459         brush->gpencil_settings->draw_smoothlvl = 1;
460         brush->gpencil_settings->thick_smoothfac = 1.0f;
461         brush->gpencil_settings->thick_smoothlvl = 3;
462         brush->gpencil_settings->draw_subdivide = 1;
463         brush->gpencil_settings->draw_random_sub = 0.0f;
464         brush->gpencil_settings->icon_id = GP_BRUSH_ICON_MARKER;
465         brush->gpencil_tool = GPAINT_TOOL_DRAW;
466
467         brush->smooth_stroke_radius = SMOOTH_STROKE_RADIUS;
468         brush->smooth_stroke_factor = SMOOTH_STROKE_FACTOR;
469         /* Curve */
470         custom_curve = brush->gpencil_settings->curve_sensitivity;
471         curvemapping_set_defaults(custom_curve, 0, 0.0f, 0.0f, 1.0f, 1.0f);
472         curvemapping_initialize(custom_curve);
473         brush_gpencil_curvemap_reset(custom_curve->cm, 4, GPCURVE_PRESET_MARKER);
474
475         /* Fill brush */
476         brush = BKE_brush_add_gpencil(bmain, ts, "Fill Area");
477         brush->size = 1.0f;
478         brush->gpencil_settings->flag |= GP_BRUSH_ENABLE_CURSOR;
479         brush->gpencil_settings->draw_sensitivity = 1.0f;
480         brush->gpencil_settings->fill_leak = 3;
481         brush->gpencil_settings->fill_threshold = 0.1f;
482         brush->gpencil_settings->fill_simplylvl = 1;
483         brush->gpencil_settings->fill_factor = 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 struct Brush *BKE_brush_first_search(struct Main *bmain, const eObjectMode ob_mode)
544 {
545         Brush *brush;
546
547         for (brush = bmain->brushes.first; brush; brush = brush->id.next) {
548                 if (brush->ob_mode & ob_mode)
549                         return brush;
550         }
551         return NULL;
552 }
553
554 /**
555  * Only copy internal data of Brush ID from source to already allocated/initialized destination.
556  * You probably never want to use that directly, use BKE_id_copy or BKE_id_copy_ex for typical needs.
557  *
558  * WARNING! This function will not handle ID user count!
559  *
560  * \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
561  */
562 void BKE_brush_copy_data(Main *UNUSED(bmain), Brush *brush_dst, const Brush *brush_src, const int flag)
563 {
564         if (brush_src->icon_imbuf) {
565                 brush_dst->icon_imbuf = IMB_dupImBuf(brush_src->icon_imbuf);
566         }
567
568         if ((flag & LIB_ID_COPY_NO_PREVIEW) == 0) {
569                 BKE_previewimg_id_copy(&brush_dst->id, &brush_src->id);
570         }
571         else {
572                 brush_dst->preview = NULL;
573         }
574
575         brush_dst->curve = curvemapping_copy(brush_src->curve);
576         if (brush_src->gpencil_settings != NULL) {
577                 brush_dst->gpencil_settings = MEM_dupallocN(brush_src->gpencil_settings);
578                 brush_dst->gpencil_settings->curve_sensitivity = curvemapping_copy(brush_src->gpencil_settings->curve_sensitivity);
579                 brush_dst->gpencil_settings->curve_strength = curvemapping_copy(brush_src->gpencil_settings->curve_strength);
580                 brush_dst->gpencil_settings->curve_jitter = curvemapping_copy(brush_src->gpencil_settings->curve_jitter);
581         }
582
583         /* enable fake user by default */
584         id_fake_user_set(&brush_dst->id);
585 }
586
587 Brush *BKE_brush_copy(Main *bmain, const Brush *brush)
588 {
589         Brush *brush_copy;
590         BKE_id_copy(bmain, &brush->id, (ID **)&brush_copy);
591         return brush_copy;
592 }
593
594 /** Free (or release) any data used by this brush (does not free the brush itself). */
595 void BKE_brush_free(Brush *brush)
596 {
597         if (brush->icon_imbuf) {
598                 IMB_freeImBuf(brush->icon_imbuf);
599         }
600         curvemapping_free(brush->curve);
601
602         if (brush->gpencil_settings != NULL) {
603                 curvemapping_free(brush->gpencil_settings->curve_sensitivity);
604                 curvemapping_free(brush->gpencil_settings->curve_strength);
605                 curvemapping_free(brush->gpencil_settings->curve_jitter);
606                 MEM_SAFE_FREE(brush->gpencil_settings);
607         }
608
609         MEM_SAFE_FREE(brush->gradient);
610
611
612         BKE_previewimg_free(&(brush->preview));
613 }
614
615 void BKE_brush_make_local(Main *bmain, Brush *brush, const bool lib_local)
616 {
617         bool is_local = false, is_lib = false;
618
619         /* - only lib users: do nothing (unless force_local is set)
620          * - only local users: set flag
621          * - mixed: make copy
622          */
623
624         if (!ID_IS_LINKED(brush)) {
625                 return;
626         }
627
628         if (brush->clone.image) {
629                 /* Special case: ima always local immediately. Clone image should only have one user anyway. */
630                 id_make_local(bmain, &brush->clone.image->id, false, false);
631         }
632
633         BKE_library_ID_test_usages(bmain, brush, &is_local, &is_lib);
634
635         if (lib_local || is_local) {
636                 if (!is_lib) {
637                         id_clear_lib_data(bmain, &brush->id);
638                         BKE_id_expand_local(bmain, &brush->id);
639
640                         /* enable fake user by default */
641                         id_fake_user_set(&brush->id);
642                 }
643                 else {
644                         Brush *brush_new = BKE_brush_copy(bmain, brush);  /* Ensures FAKE_USER is set */
645
646                         brush_new->id.us = 0;
647
648                         /* setting newid is mandatory for complex make_lib_local logic... */
649                         ID_NEW_SET(brush, brush_new);
650
651                         if (!lib_local) {
652                                 BKE_libblock_remap(bmain, brush, brush_new, ID_REMAP_SKIP_INDIRECT_USAGE);
653                         }
654                 }
655         }
656 }
657
658 void BKE_brush_debug_print_state(Brush *br)
659 {
660         /* create a fake brush and set it to the defaults */
661         Brush def = {{NULL}};
662         brush_defaults(&def);
663
664 #define BR_TEST(field, t)                                       \
665         if (br->field != def.field)                             \
666                 printf("br->" #field " = %" #t ";\n", br->field)
667
668 #define BR_TEST_FLAG(_f)                                                        \
669         if ((br->flag & _f) && !(def.flag & _f))                \
670                 printf("br->flag |= " #_f ";\n");                       \
671         else if (!(br->flag & _f) && (def.flag & _f))   \
672                 printf("br->flag &= ~" #_f ";\n")
673
674 #define BR_TEST_FLAG_OVERLAY(_f)                                                        \
675         if ((br->overlay_flags & _f) && !(def.overlay_flags & _f))              \
676                 printf("br->overlay_flags |= " #_f ";\n");                      \
677         else if (!(br->overlay_flags & _f) && (def.overlay_flags & _f)) \
678                 printf("br->overlay_flags &= ~" #_f ";\n")
679
680         /* print out any non-default brush state */
681         BR_TEST(normal_weight, f);
682
683         BR_TEST(blend, d);
684         BR_TEST(size, d);
685
686         /* br->flag */
687         BR_TEST_FLAG(BRUSH_AIRBRUSH);
688         BR_TEST_FLAG(BRUSH_ALPHA_PRESSURE);
689         BR_TEST_FLAG(BRUSH_SIZE_PRESSURE);
690         BR_TEST_FLAG(BRUSH_JITTER_PRESSURE);
691         BR_TEST_FLAG(BRUSH_SPACING_PRESSURE);
692         BR_TEST_FLAG(BRUSH_ANCHORED);
693         BR_TEST_FLAG(BRUSH_DIR_IN);
694         BR_TEST_FLAG(BRUSH_SPACE);
695         BR_TEST_FLAG(BRUSH_SMOOTH_STROKE);
696         BR_TEST_FLAG(BRUSH_PERSISTENT);
697         BR_TEST_FLAG(BRUSH_ACCUMULATE);
698         BR_TEST_FLAG(BRUSH_LOCK_ALPHA);
699         BR_TEST_FLAG(BRUSH_ORIGINAL_NORMAL);
700         BR_TEST_FLAG(BRUSH_OFFSET_PRESSURE);
701         BR_TEST_FLAG(BRUSH_SPACE_ATTEN);
702         BR_TEST_FLAG(BRUSH_ADAPTIVE_SPACE);
703         BR_TEST_FLAG(BRUSH_LOCK_SIZE);
704         BR_TEST_FLAG(BRUSH_EDGE_TO_EDGE);
705         BR_TEST_FLAG(BRUSH_DRAG_DOT);
706         BR_TEST_FLAG(BRUSH_INVERSE_SMOOTH_PRESSURE);
707         BR_TEST_FLAG(BRUSH_PLANE_TRIM);
708         BR_TEST_FLAG(BRUSH_FRONTFACE);
709         BR_TEST_FLAG(BRUSH_CUSTOM_ICON);
710
711         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR);
712         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY);
713         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY);
714         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE);
715         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE);
716         BR_TEST_FLAG_OVERLAY(BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE);
717
718         BR_TEST(jitter, f);
719         BR_TEST(spacing, d);
720         BR_TEST(smooth_stroke_radius, d);
721         BR_TEST(smooth_stroke_factor, f);
722         BR_TEST(rate, f);
723
724         BR_TEST(alpha, f);
725
726         BR_TEST(sculpt_plane, d);
727
728         BR_TEST(plane_offset, f);
729
730         BR_TEST(autosmooth_factor, f);
731
732         BR_TEST(topology_rake_factor, f);
733
734         BR_TEST(crease_pinch_factor, f);
735
736         BR_TEST(plane_trim, f);
737
738         BR_TEST(texture_sample_bias, f);
739         BR_TEST(texture_overlay_alpha, d);
740
741         BR_TEST(add_col[0], f);
742         BR_TEST(add_col[1], f);
743         BR_TEST(add_col[2], f);
744         BR_TEST(sub_col[0], f);
745         BR_TEST(sub_col[1], f);
746         BR_TEST(sub_col[2], f);
747
748         printf("\n");
749
750 #undef BR_TEST
751 #undef BR_TEST_FLAG
752 }
753
754 void BKE_brush_sculpt_reset(Brush *br)
755 {
756         /* enable this to see any non-default
757          * settings used by a brush: */
758         // BKE_brush_debug_print_state(br);
759
760         brush_defaults(br);
761         BKE_brush_curve_preset(br, CURVE_PRESET_SMOOTH);
762
763         switch (br->sculpt_tool) {
764                 case SCULPT_TOOL_CLAY:
765                         br->flag |= BRUSH_FRONTFACE;
766                         break;
767                 case SCULPT_TOOL_CREASE:
768                         br->flag |= BRUSH_DIR_IN;
769                         br->alpha = 0.25;
770                         break;
771                 case SCULPT_TOOL_FILL:
772                         br->add_col[1] = 1;
773                         br->sub_col[0] = 0.25;
774                         br->sub_col[1] = 1;
775                         break;
776                 case SCULPT_TOOL_FLATTEN:
777                         br->add_col[1] = 1;
778                         br->sub_col[0] = 0.25;
779                         br->sub_col[1] = 1;
780                         break;
781                 case SCULPT_TOOL_INFLATE:
782                         br->add_col[0] = 0.750000;
783                         br->add_col[1] = 0.750000;
784                         br->add_col[2] = 0.750000;
785                         br->sub_col[0] = 0.250000;
786                         br->sub_col[1] = 0.250000;
787                         br->sub_col[2] = 0.250000;
788                         break;
789                 case SCULPT_TOOL_NUDGE:
790                         br->add_col[0] = 0.250000;
791                         br->add_col[1] = 1.000000;
792                         br->add_col[2] = 0.250000;
793                         break;
794                 case SCULPT_TOOL_PINCH:
795                         br->add_col[0] = 0.750000;
796                         br->add_col[1] = 0.750000;
797                         br->add_col[2] = 0.750000;
798                         br->sub_col[0] = 0.250000;
799                         br->sub_col[1] = 0.250000;
800                         br->sub_col[2] = 0.250000;
801                         break;
802                 case SCULPT_TOOL_SCRAPE:
803                         br->add_col[1] = 1.000000;
804                         br->sub_col[0] = 0.250000;
805                         br->sub_col[1] = 1.000000;
806                         break;
807                 case SCULPT_TOOL_ROTATE:
808                         br->alpha = 1.0;
809                         break;
810                 case SCULPT_TOOL_SMOOTH:
811                         br->flag &= ~BRUSH_SPACE_ATTEN;
812                         br->spacing = 5;
813                         br->add_col[0] = 0.750000;
814                         br->add_col[1] = 0.750000;
815                         br->add_col[2] = 0.750000;
816                         break;
817                 case SCULPT_TOOL_GRAB:
818                 case SCULPT_TOOL_SNAKE_HOOK:
819                 case SCULPT_TOOL_THUMB:
820                         br->size = 75;
821                         br->flag &= ~BRUSH_ALPHA_PRESSURE;
822                         br->flag &= ~BRUSH_SPACE;
823                         br->flag &= ~BRUSH_SPACE_ATTEN;
824                         br->add_col[0] = 0.250000;
825                         br->add_col[1] = 1.000000;
826                         br->add_col[2] = 0.250000;
827                         break;
828                 default:
829                         break;
830         }
831 }
832
833 /**
834  * Library Operations
835  */
836 void BKE_brush_curve_preset(Brush *b, eCurveMappingPreset preset)
837 {
838         CurveMap *cm = NULL;
839
840         if (!b->curve)
841                 b->curve = curvemapping_add(1, 0, 0, 1, 1);
842
843         cm = b->curve->cm;
844         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
845
846         b->curve->preset = preset;
847         curvemap_reset(cm, &b->curve->clipr, b->curve->preset, CURVEMAP_SLOPE_NEGATIVE);
848         curvemapping_changed(b->curve, false);
849 }
850
851 /* Generic texture sampler for 3D painting systems. point has to be either in
852  * region space mouse coordinates, or 3d world coordinates for 3D mapping.
853  *
854  * rgba outputs straight alpha. */
855 float BKE_brush_sample_tex_3d(const Scene *scene, const Brush *br,
856                               const float point[3],
857                               float rgba[4], const int thread,
858                               struct ImagePool *pool)
859 {
860         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
861         const MTex *mtex = &br->mtex;
862         float intensity = 1.0;
863         bool hasrgb = false;
864
865         if (!mtex->tex) {
866                 intensity = 1;
867         }
868         else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
869                 /* Get strength by feeding the vertex
870                  * location directly into a texture */
871                 hasrgb = externtex(mtex, point, &intensity,
872                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
873         }
874         else if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
875                 float rotation = -mtex->rot;
876                 float point_2d[2] = {point[0], point[1]};
877                 float x, y;
878                 float co[3];
879
880                 x = point_2d[0] - br->stencil_pos[0];
881                 y = point_2d[1] - br->stencil_pos[1];
882
883                 if (rotation > 0.001f || rotation < -0.001f) {
884                         const float angle    = atan2f(y, x) + rotation;
885                         const float flen     = sqrtf(x * x + y * y);
886
887                         x = flen * cosf(angle);
888                         y = flen * sinf(angle);
889                 }
890
891                 if (fabsf(x) > br->stencil_dimension[0] || fabsf(y) > br->stencil_dimension[1]) {
892                         zero_v4(rgba);
893                         return 0.0f;
894                 }
895                 x /= (br->stencil_dimension[0]);
896                 y /= (br->stencil_dimension[1]);
897
898                 co[0] = x;
899                 co[1] = y;
900                 co[2] = 0.0f;
901
902                 hasrgb = externtex(mtex, co, &intensity,
903                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
904         }
905         else {
906                 float rotation = -mtex->rot;
907                 float point_2d[2] = {point[0], point[1]};
908                 float x = 0.0f, y = 0.0f; /* Quite warnings */
909                 float invradius = 1.0f; /* Quite warnings */
910                 float co[3];
911
912                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
913                         /* keep coordinates relative to mouse */
914
915                         rotation += ups->brush_rotation;
916
917                         x = point_2d[0] - ups->tex_mouse[0];
918                         y = point_2d[1] - ups->tex_mouse[1];
919
920                         /* use pressure adjusted size for fixed mode */
921                         invradius = 1.0f / ups->pixel_radius;
922                 }
923                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
924                         /* leave the coordinates relative to the screen */
925
926                         /* use unadjusted size for tiled mode */
927                         invradius = 1.0f / BKE_brush_size_get(scene, br);
928
929                         x = point_2d[0];
930                         y = point_2d[1];
931                 }
932                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
933                         rotation += ups->brush_rotation;
934                         /* these contain a random coordinate */
935                         x = point_2d[0] - ups->tex_mouse[0];
936                         y = point_2d[1] - ups->tex_mouse[1];
937
938                         invradius = 1.0f / ups->pixel_radius;
939                 }
940
941                 x *= invradius;
942                 y *= invradius;
943
944                 /* it is probably worth optimizing for those cases where
945                  * the texture is not rotated by skipping the calls to
946                  * atan2, sqrtf, sin, and cos. */
947                 if (rotation > 0.001f || rotation < -0.001f) {
948                         const float angle    = atan2f(y, x) + rotation;
949                         const float flen     = sqrtf(x * x + y * y);
950
951                         x = flen * cosf(angle);
952                         y = flen * sinf(angle);
953                 }
954
955                 co[0] = x;
956                 co[1] = y;
957                 co[2] = 0.0f;
958
959                 hasrgb = externtex(mtex, co, &intensity,
960                                    rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
961         }
962
963         intensity += br->texture_sample_bias;
964
965         if (!hasrgb) {
966                 rgba[0] = intensity;
967                 rgba[1] = intensity;
968                 rgba[2] = intensity;
969                 rgba[3] = 1.0f;
970         }
971         /* For consistency, sampling always returns color in linear space */
972         else if (ups->do_linear_conversion) {
973                 IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, ups->colorspace);
974         }
975
976         return intensity;
977 }
978
979 float BKE_brush_sample_masktex(const Scene *scene, Brush *br,
980                                const float point[2],
981                                const int thread,
982                                struct ImagePool *pool)
983 {
984         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
985         MTex *mtex = &br->mask_mtex;
986         float rgba[4], intensity;
987
988         if (!mtex->tex) {
989                 return 1.0f;
990         }
991         if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
992                 float rotation = -mtex->rot;
993                 float point_2d[2] = {point[0], point[1]};
994                 float x, y;
995                 float co[3];
996
997                 x = point_2d[0] - br->mask_stencil_pos[0];
998                 y = point_2d[1] - br->mask_stencil_pos[1];
999
1000                 if (rotation > 0.001f || rotation < -0.001f) {
1001                         const float angle    = atan2f(y, x) + rotation;
1002                         const float flen     = sqrtf(x * x + y * y);
1003
1004                         x = flen * cosf(angle);
1005                         y = flen * sinf(angle);
1006                 }
1007
1008                 if (fabsf(x) > br->mask_stencil_dimension[0] || fabsf(y) > br->mask_stencil_dimension[1]) {
1009                         zero_v4(rgba);
1010                         return 0.0f;
1011                 }
1012                 x /= (br->mask_stencil_dimension[0]);
1013                 y /= (br->mask_stencil_dimension[1]);
1014
1015                 co[0] = x;
1016                 co[1] = y;
1017                 co[2] = 0.0f;
1018
1019                 externtex(mtex, co, &intensity,
1020                           rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1021         }
1022         else {
1023                 float rotation = -mtex->rot;
1024                 float point_2d[2] = {point[0], point[1]};
1025                 float x = 0.0f, y = 0.0f; /* Quite warnings */
1026                 float invradius = 1.0f; /* Quite warnings */
1027                 float co[3];
1028
1029                 if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
1030                         /* keep coordinates relative to mouse */
1031
1032                         rotation += ups->brush_rotation_sec;
1033
1034                         x = point_2d[0] - ups->mask_tex_mouse[0];
1035                         y = point_2d[1] - ups->mask_tex_mouse[1];
1036
1037                         /* use pressure adjusted size for fixed mode */
1038                         invradius = 1.0f / ups->pixel_radius;
1039                 }
1040                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
1041                         /* leave the coordinates relative to the screen */
1042
1043                         /* use unadjusted size for tiled mode */
1044                         invradius = 1.0f / BKE_brush_size_get(scene, br);
1045
1046                         x = point_2d[0];
1047                         y = point_2d[1];
1048                 }
1049                 else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
1050                         rotation += ups->brush_rotation_sec;
1051                         /* these contain a random coordinate */
1052                         x = point_2d[0] - ups->mask_tex_mouse[0];
1053                         y = point_2d[1] - ups->mask_tex_mouse[1];
1054
1055                         invradius = 1.0f / ups->pixel_radius;
1056                 }
1057
1058                 x *= invradius;
1059                 y *= invradius;
1060
1061                 /* it is probably worth optimizing for those cases where
1062                  * the texture is not rotated by skipping the calls to
1063                  * atan2, sqrtf, sin, and cos. */
1064                 if (rotation > 0.001f || rotation < -0.001f) {
1065                         const float angle    = atan2f(y, x) + rotation;
1066                         const float flen     = sqrtf(x * x + y * y);
1067
1068                         x = flen * cosf(angle);
1069                         y = flen * sinf(angle);
1070                 }
1071
1072                 co[0] = x;
1073                 co[1] = y;
1074                 co[2] = 0.0f;
1075
1076                 externtex(mtex, co, &intensity,
1077                           rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
1078         }
1079
1080         CLAMP(intensity, 0.0f, 1.0f);
1081
1082         switch (br->mask_pressure) {
1083                 case BRUSH_MASK_PRESSURE_CUTOFF:
1084                         intensity  = ((1.0f - intensity) < ups->size_pressure_value) ? 1.0f : 0.0f;
1085                         break;
1086                 case BRUSH_MASK_PRESSURE_RAMP:
1087                         intensity = ups->size_pressure_value + intensity * (1.0f - ups->size_pressure_value);
1088                         break;
1089                 default:
1090                         break;
1091         }
1092
1093         return intensity;
1094 }
1095
1096 /* Unified Size / Strength / Color */
1097
1098 /* XXX: be careful about setting size and unprojected radius
1099  * because they depend on one another
1100  * these functions do not set the other corresponding value
1101  * this can lead to odd behavior if size and unprojected
1102  * radius become inconsistent.
1103  * the biggest problem is that it isn't possible to change
1104  * unprojected radius because a view context is not
1105  * available.  my usual solution to this is to use the
1106  * ratio of change of the size to change the unprojected
1107  * radius.  Not completely convinced that is correct.
1108  * In any case, a better solution is needed to prevent
1109  * inconsistency. */
1110
1111
1112 const float *BKE_brush_color_get(const struct Scene *scene, const struct Brush *brush)
1113 {
1114         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1115         return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->rgb : brush->rgb;
1116 }
1117
1118 const float *BKE_brush_secondary_color_get(const struct Scene *scene, const struct Brush *brush)
1119 {
1120         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1121         return (ups->flag & UNIFIED_PAINT_COLOR) ? ups->secondary_rgb : brush->secondary_rgb;
1122 }
1123
1124 void BKE_brush_color_set(struct Scene *scene, struct Brush *brush, const float color[3])
1125 {
1126         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1127
1128         if (ups->flag & UNIFIED_PAINT_COLOR)
1129                 copy_v3_v3(ups->rgb, color);
1130         else
1131                 copy_v3_v3(brush->rgb, color);
1132 }
1133
1134 void BKE_brush_size_set(Scene *scene, Brush *brush, int size)
1135 {
1136         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1137
1138         /* make sure range is sane */
1139         CLAMP(size, 1, MAX_BRUSH_PIXEL_RADIUS);
1140
1141         if (ups->flag & UNIFIED_PAINT_SIZE)
1142                 ups->size = size;
1143         else
1144                 brush->size = size;
1145 }
1146
1147 int BKE_brush_size_get(const Scene *scene, const Brush *brush)
1148 {
1149         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1150         int size = (ups->flag & UNIFIED_PAINT_SIZE) ? ups->size : brush->size;
1151
1152         return size;
1153 }
1154
1155 bool BKE_brush_use_locked_size(const Scene *scene, const Brush *brush)
1156 {
1157         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
1158
1159         return (us_flag & UNIFIED_PAINT_SIZE) ?
1160                (us_flag & UNIFIED_PAINT_BRUSH_LOCK_SIZE) :
1161                (brush->flag & BRUSH_LOCK_SIZE);
1162 }
1163
1164 bool BKE_brush_use_size_pressure(const Scene *scene, const Brush *brush)
1165 {
1166         const short us_flag = scene->toolsettings->unified_paint_settings.flag;
1167
1168         return (us_flag & UNIFIED_PAINT_SIZE) ?
1169                (us_flag & UNIFIED_PAINT_BRUSH_SIZE_PRESSURE) :
1170                (brush->flag & BRUSH_SIZE_PRESSURE);
1171 }
1172
1173 bool BKE_brush_use_alpha_pressure(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_ALPHA) ?
1178                (us_flag & UNIFIED_PAINT_BRUSH_ALPHA_PRESSURE) :
1179                (brush->flag & BRUSH_ALPHA_PRESSURE);
1180 }
1181
1182 bool BKE_brush_sculpt_has_secondary_color(const Brush *brush)
1183 {
1184         return ELEM(
1185                 brush->sculpt_tool, SCULPT_TOOL_BLOB, SCULPT_TOOL_DRAW,
1186                 SCULPT_TOOL_INFLATE, SCULPT_TOOL_CLAY, SCULPT_TOOL_CLAY_STRIPS,
1187                 SCULPT_TOOL_PINCH, SCULPT_TOOL_CREASE, SCULPT_TOOL_LAYER,
1188                 SCULPT_TOOL_FLATTEN, SCULPT_TOOL_FILL, SCULPT_TOOL_SCRAPE,
1189                 SCULPT_TOOL_MASK);
1190 }
1191
1192 void BKE_brush_unprojected_radius_set(Scene *scene, Brush *brush, float unprojected_radius)
1193 {
1194         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1195
1196         if (ups->flag & UNIFIED_PAINT_SIZE)
1197                 ups->unprojected_radius = unprojected_radius;
1198         else
1199                 brush->unprojected_radius = unprojected_radius;
1200 }
1201
1202 float BKE_brush_unprojected_radius_get(const Scene *scene, const Brush *brush)
1203 {
1204         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1205
1206         return (ups->flag & UNIFIED_PAINT_SIZE) ?
1207                ups->unprojected_radius :
1208                brush->unprojected_radius;
1209 }
1210
1211 void BKE_brush_alpha_set(Scene *scene, Brush *brush, float alpha)
1212 {
1213         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1214
1215         if (ups->flag & UNIFIED_PAINT_ALPHA)
1216                 ups->alpha = alpha;
1217         else
1218                 brush->alpha = alpha;
1219 }
1220
1221 float BKE_brush_alpha_get(const Scene *scene, const Brush *brush)
1222 {
1223         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1224
1225         return (ups->flag & UNIFIED_PAINT_ALPHA) ? ups->alpha : brush->alpha;
1226 }
1227
1228 float BKE_brush_weight_get(const Scene *scene, const Brush *brush)
1229 {
1230         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1231
1232         return (ups->flag & UNIFIED_PAINT_WEIGHT) ? ups->weight : brush->weight;
1233 }
1234
1235 void BKE_brush_weight_set(const Scene *scene, Brush *brush, float value)
1236 {
1237         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
1238
1239         if (ups->flag & UNIFIED_PAINT_WEIGHT)
1240                 ups->weight = value;
1241         else
1242                 brush->weight = value;
1243 }
1244
1245 /* scale unprojected radius to reflect a change in the brush's 2D size */
1246 void BKE_brush_scale_unprojected_radius(float *unprojected_radius,
1247                                         int new_brush_size,
1248                                         int old_brush_size)
1249 {
1250         float scale = new_brush_size;
1251         /* avoid division by zero */
1252         if (old_brush_size != 0)
1253                 scale /= (float)old_brush_size;
1254         (*unprojected_radius) *= scale;
1255 }
1256
1257 /* scale brush size to reflect a change in the brush's unprojected radius */
1258 void BKE_brush_scale_size(
1259         int *r_brush_size,
1260         float new_unprojected_radius,
1261         float old_unprojected_radius)
1262 {
1263         float scale = new_unprojected_radius;
1264         /* avoid division by zero */
1265         if (old_unprojected_radius != 0)
1266                 scale /= new_unprojected_radius;
1267         (*r_brush_size) = (int)((float)(*r_brush_size) * scale);
1268 }
1269
1270 void BKE_brush_jitter_pos(const Scene *scene, Brush *brush, const float pos[2], float jitterpos[2])
1271 {
1272         float rand_pos[2];
1273         float spread;
1274         int diameter;
1275
1276         do {
1277                 rand_pos[0] = BLI_rng_get_float(brush_rng) - 0.5f;
1278                 rand_pos[1] = BLI_rng_get_float(brush_rng) - 0.5f;
1279         } while (len_squared_v2(rand_pos) > SQUARE(0.5f));
1280
1281
1282         if (brush->flag & BRUSH_ABSOLUTE_JITTER) {
1283                 diameter = 2 * brush->jitter_absolute;
1284                 spread = 1.0;
1285         }
1286         else {
1287                 diameter = 2 * BKE_brush_size_get(scene, brush);
1288                 spread = brush->jitter;
1289         }
1290         /* find random position within a circle of diameter 1 */
1291         jitterpos[0] = pos[0] + 2 * rand_pos[0] * diameter * spread;
1292         jitterpos[1] = pos[1] + 2 * rand_pos[1] * diameter * spread;
1293 }
1294
1295 void BKE_brush_randomize_texture_coords(UnifiedPaintSettings *ups, bool mask)
1296 {
1297         /* we multiply with brush radius as an optimization for the brush
1298          * texture sampling functions */
1299         if (mask) {
1300                 ups->mask_tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1301                 ups->mask_tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1302         }
1303         else {
1304                 ups->tex_mouse[0] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1305                 ups->tex_mouse[1] = BLI_rng_get_float(brush_rng) * ups->pixel_radius;
1306         }
1307 }
1308
1309 /* Uses the brush curve control to find a strength value */
1310 float BKE_brush_curve_strength(const Brush *br, float p, const float len)
1311 {
1312         float strength;
1313
1314         if (p >= len) return 0;
1315         else p = p / len;
1316
1317         strength = curvemapping_evaluateF(br->curve, 0, p);
1318
1319         return strength;
1320 }
1321
1322
1323 /* Uses the brush curve control to find a strength value between 0 and 1 */
1324 float BKE_brush_curve_strength_clamped(Brush *br, float p, const float len)
1325 {
1326         float strength = BKE_brush_curve_strength(br, p, len);
1327
1328         CLAMP(strength, 0.0f, 1.0f);
1329
1330         return strength;
1331 }
1332
1333 /* TODO: should probably be unified with BrushPainter stuff? */
1334 unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side, bool use_secondary)
1335 {
1336         unsigned int *texcache = NULL;
1337         MTex *mtex = (use_secondary) ? &br->mask_mtex : &br->mtex;
1338         float intensity;
1339         float rgba[4];
1340         int ix, iy;
1341         int side = half_side * 2;
1342
1343         if (mtex->tex) {
1344                 float x, y, step = 2.0 / side, co[3];
1345
1346                 texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
1347
1348                 /* do normalized canonical view coords for texture */
1349                 for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
1350                         for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
1351                                 co[0] = x;
1352                                 co[1] = y;
1353                                 co[2] = 0.0f;
1354
1355                                 /* This is copied from displace modifier code */
1356                                 /* TODO(sergey): brush are always caching with CM enabled for now. */
1357                                 externtex(mtex, co, &intensity,
1358                                           rgba, rgba + 1, rgba + 2, rgba + 3, 0, NULL, false, false);
1359
1360                                 ((char *)texcache)[(iy * side + ix) * 4] =
1361                                 ((char *)texcache)[(iy * side + ix) * 4 + 1] =
1362                                 ((char *)texcache)[(iy * side + ix) * 4 + 2] =
1363                                 ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(intensity * 255.0f);
1364                         }
1365                 }
1366         }
1367
1368         return texcache;
1369 }
1370
1371
1372 /**** Radial Control ****/
1373 struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br, bool secondary)
1374 {
1375         ImBuf *im = MEM_callocN(sizeof(ImBuf), "radial control texture");
1376         unsigned int *texcache;
1377         int side = 128;
1378         int half = side / 2;
1379         int i, j;
1380
1381         curvemapping_initialize(br->curve);
1382         texcache = BKE_brush_gen_texture_cache(br, half, secondary);
1383         im->rect_float = MEM_callocN(sizeof(float) * side * side, "radial control rect");
1384         im->x = im->y = side;
1385
1386         for (i = 0; i < side; ++i) {
1387                 for (j = 0; j < side; ++j) {
1388                         float magn = sqrtf(pow2f(i - half) + pow2f(j - half));
1389                         im->rect_float[i * side + j] = BKE_brush_curve_strength_clamped(br, magn, half);
1390                 }
1391         }
1392
1393         /* Modulate curve with texture */
1394         if (texcache) {
1395                 for (i = 0; i < side; ++i) {
1396                         for (j = 0; j < side; ++j) {
1397                                 const int col = texcache[i * side + j];
1398                                 im->rect_float[i * side + j] *=
1399                                         (((char *)&col)[0] + ((char *)&col)[1] + ((char *)&col)[2]) / 3.0f / 255.0f;
1400                         }
1401                 }
1402
1403                 MEM_freeN(texcache);
1404         }
1405
1406         return im;
1407 }