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