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