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