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