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