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