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