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