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