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