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