Merge branch 'master' into blender2.8
[blender.git] / source / blender / blenkernel / intern / paint.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 by Nicholas Bishop
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/paint.c
29  *  \ingroup bke
30  */
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_object_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
40 #include "DNA_modifier_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_brush_types.h"
43 #include "DNA_space_types.h"
44 #include "DNA_workspace_types.h"
45
46 #include "BLI_bitmap.h"
47 #include "BLI_utildefines.h"
48 #include "BLI_math_vector.h"
49 #include "BLI_listbase.h"
50
51 #include "BKE_brush.h"
52 #include "BKE_colortools.h"
53 #include "BKE_deform.h"
54 #include "BKE_main.h"
55 #include "BKE_context.h"
56 #include "BKE_crazyspace.h"
57 #include "BKE_global.h"
58 #include "BKE_image.h"
59 #include "BKE_key.h"
60 #include "BKE_library.h"
61 #include "BKE_mesh.h"
62 #include "BKE_modifier.h"
63 #include "BKE_object.h"
64 #include "BKE_paint.h"
65 #include "BKE_pbvh.h"
66 #include "BKE_subsurf.h"
67
68 #include "DEG_depsgraph.h"
69
70 #include "bmesh.h"
71
72 const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
73 const char PAINT_CURSOR_VERTEX_PAINT[3] = {255, 255, 255};
74 const char PAINT_CURSOR_WEIGHT_PAINT[3] = {200, 200, 255};
75 const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
76
77 static eOverlayControlFlags overlay_flags = 0;
78
79 /* Keep in sync with 'BKE_paint_get_active' */
80 #define OB_MODE_HAS_PAINT_STRUCT(SEP) \
81         OB_MODE_SCULPT SEP \
82         OB_MODE_VERTEX_PAINT SEP \
83         OB_MODE_WEIGHT_PAINT SEP \
84         OB_MODE_TEXTURE_PAINT SEP \
85         OB_MODE_EDIT
86
87 #define COMMA ,
88 static const eObjectMode ob_mode_has_paint_struct = OB_MODE_HAS_PAINT_STRUCT(|);
89 static const eObjectMode ob_mode_has_paint_struct_array[] = {OB_MODE_HAS_PAINT_STRUCT(COMMA)};
90 #undef COMMA
91
92 #define FOREACH_OB_MODE_PAINT_ITER_BEGIN(scene, view_layer, object_mode, p) \
93 { \
94         eObjectMode object_mode_test = object_mode & ob_mode_has_paint_struct; \
95         for (uint _i = 0; _i < ARRAY_SIZE(ob_mode_has_paint_struct_array) && object_mode_test; _i++) { \
96                 eObjectMode object_mode_single = ob_mode_has_paint_struct_array[_i]; \
97                 if (object_mode_test & object_mode_single) { \
98                         object_mode_test &= ~object_mode_single; \
99                         Paint *p = BKE_paint_get_active(scene, view_layer, object_mode_single); \
100                         {
101
102 #define FOREACH_OB_MODE_PAINT_ITER_END \
103                         } \
104                 } \
105         } \
106 } ((void)0)
107
108 void BKE_paint_invalidate_overlay_tex(
109         Scene *scene, ViewLayer *view_layer, const Tex *tex, eObjectMode object_mode)
110 {
111         FOREACH_OB_MODE_PAINT_ITER_BEGIN(scene, view_layer, object_mode, p)
112         {
113                 Brush *br = p->brush;
114                 if (br) {
115                         if (br->mtex.tex == tex) {
116                                 overlay_flags |= PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY;
117                         }
118                         if (br->mask_mtex.tex == tex) {
119                                 overlay_flags |= PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY;
120                         }
121                 }
122         }
123         FOREACH_OB_MODE_PAINT_ITER_END;
124 }
125
126 void BKE_paint_invalidate_cursor_overlay(
127         Scene *scene, ViewLayer *view_layer, CurveMapping *curve, eObjectMode object_mode)
128 {
129         FOREACH_OB_MODE_PAINT_ITER_BEGIN(scene, view_layer, object_mode, p)
130         {
131                 Brush *br = p->brush;
132                 if (br) {
133                         if (br->curve == curve) {
134                                 overlay_flags |= PAINT_INVALID_OVERLAY_CURVE;
135                                 break;
136                         }
137                 }
138         }
139         FOREACH_OB_MODE_PAINT_ITER_END;
140 }
141
142 void BKE_paint_invalidate_overlay_all(void)
143 {
144         overlay_flags |= (PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY |
145                           PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY |
146                           PAINT_INVALID_OVERLAY_CURVE);
147 }
148
149 eOverlayControlFlags BKE_paint_get_overlay_flags(void)
150 {
151         return overlay_flags;
152 }
153
154 void BKE_paint_set_overlay_override(eOverlayFlags flags)
155 {
156         if (flags & BRUSH_OVERLAY_OVERRIDE_MASK) {
157                 if (flags & BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE)
158                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_CURSOR;
159                 if (flags & BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE)
160                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_PRIMARY;
161                 if (flags & BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE)
162                         overlay_flags |= PAINT_OVERLAY_OVERRIDE_SECONDARY;
163         }
164         else {
165                 overlay_flags &= ~(PAINT_OVERRIDE_MASK);
166         }
167 }
168
169 void BKE_paint_reset_overlay_invalid(eOverlayControlFlags flag)
170 {
171         overlay_flags &= ~(flag);
172 }
173
174 Paint *BKE_paint_get_active_from_paintmode(Scene *sce, ePaintMode mode)
175 {
176         if (sce) {
177                 ToolSettings *ts = sce->toolsettings;
178
179                 switch (mode) {
180                         case ePaintSculpt:
181                                 return &ts->sculpt->paint;
182                         case ePaintVertex:
183                                 return &ts->vpaint->paint;
184                         case ePaintWeight:
185                                 return &ts->wpaint->paint;
186                         case ePaintTexture2D:
187                         case ePaintTextureProjective:
188                                 return &ts->imapaint.paint;
189                         case ePaintSculptUV:
190                                 return &ts->uvsculpt->paint;
191                         case ePaintInvalid:
192                                 return NULL;
193                         default:
194                                 return &ts->imapaint.paint;
195                 }
196         }
197
198         return NULL;
199 }
200
201 Paint *BKE_paint_get_active(Scene *sce, ViewLayer *view_layer, const eObjectMode object_mode)
202 {
203         if (sce && view_layer) {
204                 ToolSettings *ts = sce->toolsettings;
205                 
206                 if (view_layer->basact && view_layer->basact->object) {
207                         switch (object_mode) {
208                                 case OB_MODE_SCULPT:
209                                         return &ts->sculpt->paint;
210                                 case OB_MODE_VERTEX_PAINT:
211                                         return &ts->vpaint->paint;
212                                 case OB_MODE_WEIGHT_PAINT:
213                                         return &ts->wpaint->paint;
214                                 case OB_MODE_TEXTURE_PAINT:
215                                         return &ts->imapaint.paint;
216                                 case OB_MODE_EDIT:
217                                         if (ts->use_uv_sculpt)
218                                                 return &ts->uvsculpt->paint;
219                                         return &ts->imapaint.paint;
220                                 default:
221                                         break;
222                         }
223                 }
224
225                 /* default to image paint */
226                 return &ts->imapaint.paint;
227         }
228
229         return NULL;
230 }
231
232 Paint *BKE_paint_get_active_from_context(const bContext *C)
233 {
234         Scene *sce = CTX_data_scene(C);
235         ViewLayer *view_layer = CTX_data_view_layer(C);
236         SpaceImage *sima;
237
238         if (sce && view_layer) {
239                 const WorkSpace *workspace = CTX_wm_workspace(C);
240                 ToolSettings *ts = sce->toolsettings;
241                 Object *obact = NULL;
242
243                 if (view_layer->basact && view_layer->basact->object)
244                         obact = view_layer->basact->object;
245
246                 if ((sima = CTX_wm_space_image(C)) != NULL) {
247                         if (obact && workspace->object_mode == OB_MODE_EDIT) {
248                                 if (sima->mode == SI_MODE_PAINT)
249                                         return &ts->imapaint.paint;
250                                 else if (ts->use_uv_sculpt)
251                                         return &ts->uvsculpt->paint;
252                         }
253                         else {
254                                 return &ts->imapaint.paint;
255                         }
256                 }
257                 else if (obact) {
258                         switch (workspace->object_mode) {
259                                 case OB_MODE_SCULPT:
260                                         return &ts->sculpt->paint;
261                                 case OB_MODE_VERTEX_PAINT:
262                                         return &ts->vpaint->paint;
263                                 case OB_MODE_WEIGHT_PAINT:
264                                         return &ts->wpaint->paint;
265                                 case OB_MODE_TEXTURE_PAINT:
266                                         return &ts->imapaint.paint;
267                                 case OB_MODE_EDIT:
268                                         if (ts->use_uv_sculpt)
269                                                 return &ts->uvsculpt->paint;
270                                         return &ts->imapaint.paint;
271                                 default:
272                                         return &ts->imapaint.paint;
273                         }
274                 }
275                 else {
276                         /* default to image paint */
277                         return &ts->imapaint.paint;
278                 }
279         }
280
281         return NULL;
282 }
283
284 ePaintMode BKE_paintmode_get_active_from_context(const bContext *C)
285 {
286         Scene *sce = CTX_data_scene(C);
287         ViewLayer *view_layer = CTX_data_view_layer(C);
288         SpaceImage *sima;
289
290         if (sce && view_layer) {
291                 const WorkSpace *workspace = CTX_wm_workspace(C);
292                 ToolSettings *ts = sce->toolsettings;
293                 Object *obact = NULL;
294
295                 if (view_layer->basact && view_layer->basact->object)
296                         obact = view_layer->basact->object;
297
298                 if ((sima = CTX_wm_space_image(C)) != NULL) {
299                         if (obact && workspace->object_mode == OB_MODE_EDIT) {
300                                 if (sima->mode == SI_MODE_PAINT)
301                                         return ePaintTexture2D;
302                                 else if (ts->use_uv_sculpt)
303                                         return ePaintSculptUV;
304                         }
305                         else {
306                                 return ePaintTexture2D;
307                         }
308                 }
309                 else if (obact) {
310                         switch (workspace->object_mode) {
311                                 case OB_MODE_SCULPT:
312                                         return ePaintSculpt;
313                                 case OB_MODE_VERTEX_PAINT:
314                                         return ePaintVertex;
315                                 case OB_MODE_WEIGHT_PAINT:
316                                         return ePaintWeight;
317                                 case OB_MODE_TEXTURE_PAINT:
318                                         return ePaintTextureProjective;
319                                 case OB_MODE_EDIT:
320                                         if (ts->use_uv_sculpt)
321                                                 return ePaintSculptUV;
322                                         return ePaintTexture2D;
323                                 default:
324                                         return ePaintTexture2D;
325                         }
326                 }
327                 else {
328                         /* default to image paint */
329                         return ePaintTexture2D;
330                 }
331         }
332
333         return ePaintInvalid;
334 }
335
336 Brush *BKE_paint_brush(Paint *p)
337 {
338         return p ? p->brush : NULL;
339 }
340
341 void BKE_paint_brush_set(Paint *p, Brush *br)
342 {
343         if (p) {
344                 id_us_min((ID *)p->brush);
345                 id_us_plus((ID *)br);
346                 p->brush = br;
347         }
348 }
349
350 /** Free (or release) any data used by this paint curve (does not free the pcurve itself). */
351 void BKE_paint_curve_free(PaintCurve *pc)
352 {
353         MEM_SAFE_FREE(pc->points);
354         pc->tot_points = 0;
355 }
356
357 PaintCurve *BKE_paint_curve_add(Main *bmain, const char *name)
358 {
359         PaintCurve *pc;
360
361         pc = BKE_libblock_alloc(bmain, ID_PC, name, 0);
362
363         return pc;
364 }
365
366 /**
367  * Only copy internal data of PaintCurve ID from source to already allocated/initialized destination.
368  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
369  *
370  * WARNING! This function will not handle ID user count!
371  *
372  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
373  */
374 void BKE_paint_curve_copy_data(Main *UNUSED(bmain), PaintCurve *pc_dst, const PaintCurve *pc_src, const int UNUSED(flag))
375 {
376         if (pc_src->tot_points != 0) {
377                 pc_dst->points = MEM_dupallocN(pc_src->points);
378         }
379 }
380
381 PaintCurve *BKE_paint_curve_copy(Main *bmain, const PaintCurve *pc)
382 {
383         PaintCurve *pc_copy;
384         BKE_id_copy_ex(bmain, &pc->id, (ID **)&pc_copy, 0, false);
385         return pc_copy;
386 }
387
388 void BKE_paint_curve_make_local(Main *bmain, PaintCurve *pc, const bool lib_local)
389 {
390         BKE_id_make_local_generic(bmain, &pc->id, true, lib_local);
391 }
392
393 Palette *BKE_paint_palette(Paint *p)
394 {
395         return p ? p->palette : NULL;
396 }
397
398 void BKE_paint_palette_set(Paint *p, Palette *palette)
399 {
400         if (p) {
401                 id_us_min((ID *)p->palette);
402                 id_us_plus((ID *)palette);
403                 p->palette = palette;
404         }
405 }
406
407 void BKE_paint_curve_set(Brush *br, PaintCurve *pc)
408 {
409         if (br) {
410                 id_us_min((ID *)br->paint_curve);
411                 id_us_plus((ID *)pc);
412                 br->paint_curve = pc;
413         }
414 }
415
416 void BKE_paint_curve_clamp_endpoint_add_index(PaintCurve *pc, const int add_index)
417 {
418         pc->add_index = (add_index || pc->tot_points == 1) ? (add_index + 1) : 0;
419 }
420
421 /* remove colour from palette. Must be certain color is inside the palette! */
422 void BKE_palette_color_remove(Palette *palette, PaletteColor *color)
423 {
424         if (BLI_listbase_count_ex(&palette->colors, palette->active_color) == palette->active_color) {
425                 palette->active_color--;
426         }
427
428         BLI_remlink(&palette->colors, color);
429
430         if (palette->active_color < 0 && !BLI_listbase_is_empty(&palette->colors)) {
431                 palette->active_color = 0;
432         }
433
434         MEM_freeN(color);
435 }
436
437 void BKE_palette_clear(Palette *palette)
438 {
439         BLI_freelistN(&palette->colors);
440         palette->active_color = 0;
441 }
442
443 Palette *BKE_palette_add(Main *bmain, const char *name)
444 {
445         Palette *palette;
446
447         palette = BKE_libblock_alloc(bmain, ID_PAL, name, 0);
448
449         /* enable fake user by default */
450         id_fake_user_set(&palette->id);
451
452         return palette;
453 }
454
455 /**
456  * Only copy internal data of Palette ID from source to already allocated/initialized destination.
457  * You probably nerver want to use that directly, use id_copy or BKE_id_copy_ex for typical needs.
458  *
459  * WARNING! This function will not handle ID user count!
460  *
461  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
462  */
463 void BKE_palette_copy_data(Main *UNUSED(bmain), Palette *palette_dst, const Palette *palette_src, const int UNUSED(flag))
464 {
465         BLI_duplicatelist(&palette_dst->colors, &palette_src->colors);
466 }
467
468 Palette *BKE_palette_copy(Main *bmain, const Palette *palette)
469 {
470         Palette *palette_copy;
471         BKE_id_copy_ex(bmain, &palette->id, (ID **)&palette_copy, 0, false);
472         return palette_copy;
473 }
474
475 void BKE_palette_make_local(Main *bmain, Palette *palette, const bool lib_local)
476 {
477         BKE_id_make_local_generic(bmain, &palette->id, true, lib_local);
478 }
479
480 /** Free (or release) any data used by this palette (does not free the palette itself). */
481 void BKE_palette_free(Palette *palette)
482 {
483         BLI_freelistN(&palette->colors);
484 }
485
486 PaletteColor *BKE_palette_color_add(Palette *palette)
487 {
488         PaletteColor *color = MEM_callocN(sizeof(*color), "Pallete Color");
489         BLI_addtail(&palette->colors, color);
490         return color;
491 }
492
493
494 bool BKE_palette_is_empty(const struct Palette *palette)
495 {
496         return BLI_listbase_is_empty(&palette->colors);
497 }
498
499
500 /* are we in vertex paint or weight pain face select mode? */
501 bool BKE_paint_select_face_test(Object *ob, eObjectMode object_mode)
502 {
503         return ( (ob != NULL) &&
504                  (ob->type == OB_MESH) &&
505                  (ob->data != NULL) &&
506                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_FACE_SEL) &&
507                  (object_mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT))
508                  );
509 }
510
511 /* are we in weight paint vertex select mode? */
512 bool BKE_paint_select_vert_test(Object *ob, eObjectMode object_mode)
513 {
514         return ( (ob != NULL) &&
515                  (ob->type == OB_MESH) &&
516                  (ob->data != NULL) &&
517                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_VERT_SEL) &&
518                  (object_mode & OB_MODE_WEIGHT_PAINT || object_mode & OB_MODE_VERTEX_PAINT)
519                  );
520 }
521
522 /**
523  * used to check if selection is possible
524  * (when we don't care if its face or vert)
525  */
526 bool BKE_paint_select_elem_test(Object *ob, eObjectMode object_mode)
527 {
528         return (BKE_paint_select_vert_test(ob, object_mode) ||
529                 BKE_paint_select_face_test(ob, object_mode));
530 }
531
532 void BKE_paint_cavity_curve_preset(Paint *p, int preset)
533 {
534         CurveMap *cm = NULL;
535
536         if (!p->cavity_curve)
537                 p->cavity_curve = curvemapping_add(1, 0, 0, 1, 1);
538
539         cm = p->cavity_curve->cm;
540         cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
541
542         p->cavity_curve->preset = preset;
543         curvemap_reset(cm, &p->cavity_curve->clipr, p->cavity_curve->preset, CURVEMAP_SLOPE_POSITIVE);
544         curvemapping_changed(p->cavity_curve, false);
545 }
546
547 eObjectMode BKE_paint_object_mode_from_paint_mode(ePaintMode mode)
548 {
549         switch (mode) {
550                 case ePaintSculpt:
551                         return OB_MODE_SCULPT;
552                 case ePaintVertex:
553                         return OB_MODE_VERTEX_PAINT;
554                 case ePaintWeight:
555                         return OB_MODE_WEIGHT_PAINT;
556                 case ePaintTextureProjective:
557                         return OB_MODE_TEXTURE_PAINT;
558                 case ePaintTexture2D:
559                         return OB_MODE_TEXTURE_PAINT;
560                 case ePaintSculptUV:
561                         return OB_MODE_EDIT;
562                 case ePaintInvalid:
563                 default:
564                         return 0;
565         }
566 }
567
568 void BKE_paint_init(Scene *sce, ePaintMode mode, const char col[3])
569 {
570         UnifiedPaintSettings *ups = &sce->toolsettings->unified_paint_settings;
571         Brush *brush;
572         Paint *paint = BKE_paint_get_active_from_paintmode(sce, mode);
573
574         /* If there's no brush, create one */
575         brush = BKE_paint_brush(paint);
576         if (brush == NULL) {
577                 eObjectMode ob_mode = BKE_paint_object_mode_from_paint_mode(mode);
578                 brush = BKE_brush_first_search(G.main, ob_mode);
579
580                 if (!brush) {
581                         brush = BKE_brush_add(G.main, "Brush", ob_mode);
582                         id_us_min(&brush->id);  /* fake user only */
583                 }
584                 BKE_paint_brush_set(paint, brush);
585         }
586
587         memcpy(paint->paint_cursor_col, col, 3);
588         paint->paint_cursor_col[3] = 128;
589         ups->last_stroke_valid = false;
590         zero_v3(ups->average_stroke_accum);
591         ups->average_stroke_counter = 0;
592         if (!paint->cavity_curve)
593                 BKE_paint_cavity_curve_preset(paint, CURVE_PRESET_LINE);
594 }
595
596 void BKE_paint_free(Paint *paint)
597 {
598         curvemapping_free(paint->cavity_curve);
599 }
600
601 /* called when copying scene settings, so even if 'src' and 'tar' are the same
602  * still do a id_us_plus(), rather then if we were copying between 2 existing
603  * scenes where a matching value should decrease the existing user count as
604  * with paint_brush_set() */
605 void BKE_paint_copy(Paint *src, Paint *tar, const int flag)
606 {
607         tar->brush = src->brush;
608         tar->cavity_curve = curvemapping_copy(src->cavity_curve);
609
610         if ((flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0) {
611                 id_us_plus((ID *)tar->brush);
612                 id_us_plus((ID *)tar->palette);
613         }
614 }
615
616 void BKE_paint_stroke_get_average(Scene *scene, Object *ob, float stroke[3])
617 {
618         UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
619         if (ups->last_stroke_valid && ups->average_stroke_counter > 0) {
620                 float fac = 1.0f / ups->average_stroke_counter;
621                 mul_v3_v3fl(stroke, ups->average_stroke_accum, fac);
622         }
623         else {
624                 copy_v3_v3(stroke, ob->obmat[3]);
625         }
626 }
627
628 /* returns non-zero if any of the face's vertices
629  * are hidden, zero otherwise */
630 bool paint_is_face_hidden(const MLoopTri *lt, const MVert *mvert, const MLoop *mloop)
631 {
632         return ((mvert[mloop[lt->tri[0]].v].flag & ME_HIDE) ||
633                 (mvert[mloop[lt->tri[1]].v].flag & ME_HIDE) ||
634                 (mvert[mloop[lt->tri[2]].v].flag & ME_HIDE));
635 }
636
637 /* returns non-zero if any of the corners of the grid
638  * face whose inner corner is at (x, y) are hidden,
639  * zero otherwise */
640 bool paint_is_grid_face_hidden(const unsigned int *grid_hidden,
641                               int gridsize, int x, int y)
642 {
643         /* skip face if any of its corners are hidden */
644         return (BLI_BITMAP_TEST(grid_hidden, y * gridsize + x) ||
645                 BLI_BITMAP_TEST(grid_hidden, y * gridsize + x + 1) ||
646                 BLI_BITMAP_TEST(grid_hidden, (y + 1) * gridsize + x + 1) ||
647                 BLI_BITMAP_TEST(grid_hidden, (y + 1) * gridsize + x));
648 }
649
650 /* Return true if all vertices in the face are visible, false otherwise */
651 bool paint_is_bmesh_face_hidden(BMFace *f)
652 {
653         BMLoop *l_iter;
654         BMLoop *l_first;
655
656         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
657         do {
658                 if (BM_elem_flag_test(l_iter->v, BM_ELEM_HIDDEN)) {
659                         return true;
660                 }
661         } while ((l_iter = l_iter->next) != l_first);
662
663         return false;
664 }
665
666 float paint_grid_paint_mask(const GridPaintMask *gpm, unsigned level,
667                             unsigned x, unsigned y)
668 {
669         int factor = BKE_ccg_factor(level, gpm->level);
670         int gridsize = BKE_ccg_gridsize(gpm->level);
671         
672         return gpm->data[(y * factor) * gridsize + (x * factor)];
673 }
674
675 /* threshold to move before updating the brush rotation */
676 #define RAKE_THRESHHOLD 20
677
678 void paint_update_brush_rake_rotation(UnifiedPaintSettings *ups, Brush *brush, float rotation)
679 {
680         if (brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE)
681                 ups->brush_rotation = rotation;
682         else
683                 ups->brush_rotation = 0.0f;
684
685         if (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)
686                 ups->brush_rotation_sec = rotation;
687         else
688                 ups->brush_rotation_sec = 0.0f;
689 }
690
691 bool paint_calculate_rake_rotation(UnifiedPaintSettings *ups, Brush *brush, const float mouse_pos[2])
692 {
693         bool ok = false;
694         if ((brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE) || (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)) {
695                 const float r = RAKE_THRESHHOLD;
696                 float rotation;
697
698                 float dpos[2];
699                 sub_v2_v2v2(dpos, ups->last_rake, mouse_pos);
700
701                 if (len_squared_v2(dpos) >= r * r) {
702                         rotation = atan2f(dpos[0], dpos[1]);
703
704                         copy_v2_v2(ups->last_rake, mouse_pos);
705
706                         ups->last_rake_angle = rotation;
707
708                         paint_update_brush_rake_rotation(ups, brush, rotation);
709                         ok = true;
710                 }
711                 /* make sure we reset here to the last rotation to avoid accumulating
712                  * values in case a random rotation is also added */
713                 else {
714                         paint_update_brush_rake_rotation(ups, brush, ups->last_rake_angle);
715                         ok = false;
716                 }
717         }
718         else {
719                 ups->brush_rotation = ups->brush_rotation_sec = 0.0f;
720                 ok = true;
721         }
722         return ok;
723 }
724
725 void BKE_sculptsession_free_deformMats(SculptSession *ss)
726 {
727         MEM_SAFE_FREE(ss->orig_cos);
728         MEM_SAFE_FREE(ss->deform_cos);
729         MEM_SAFE_FREE(ss->deform_imats);
730 }
731
732 void BKE_sculptsession_free_vwpaint_data(struct SculptSession *ss)
733 {
734         struct SculptVertexPaintGeomMap *gmap = NULL;
735         if (ss->mode_type == OB_MODE_VERTEX_PAINT) {
736                 gmap = &ss->mode.vpaint.gmap;
737
738                 MEM_SAFE_FREE(ss->mode.vpaint.previous_color);
739         }
740         else if (ss->mode_type == OB_MODE_WEIGHT_PAINT) {
741                 gmap = &ss->mode.wpaint.gmap;
742
743                 MEM_SAFE_FREE(ss->mode.wpaint.alpha_weight);
744                 if (ss->mode.wpaint.dvert_prev) {
745                         BKE_defvert_array_free_elems(ss->mode.wpaint.dvert_prev, ss->totvert);
746                         MEM_freeN(ss->mode.wpaint.dvert_prev);
747                         ss->mode.wpaint.dvert_prev = NULL;
748                 }
749         }
750         else {
751                 return;
752         }
753         MEM_SAFE_FREE(gmap->vert_to_loop);
754         MEM_SAFE_FREE(gmap->vert_map_mem);
755         MEM_SAFE_FREE(gmap->vert_to_poly);
756         MEM_SAFE_FREE(gmap->poly_map_mem);
757 }
758
759 /* Write out the sculpt dynamic-topology BMesh to the Mesh */
760 static void sculptsession_bm_to_me_update_data_only(Object *ob, bool reorder)
761 {
762         SculptSession *ss = ob->sculpt;
763
764         if (ss->bm) {
765                 if (ob->data) {
766                         BMIter iter;
767                         BMFace *efa;
768                         BM_ITER_MESH (efa, &iter, ss->bm, BM_FACES_OF_MESH) {
769                                 BM_elem_flag_set(efa, BM_ELEM_SMOOTH, ss->bm_smooth_shading);
770                         }
771                         if (reorder)
772                                 BM_log_mesh_elems_reorder(ss->bm, ss->bm_log);
773                         BM_mesh_bm_to_me(ss->bm, ob->data, (&(struct BMeshToMeshParams){0}));
774                 }
775         }
776 }
777
778 void BKE_sculptsession_bm_to_me(Object *ob, bool reorder)
779 {
780         if (ob && ob->sculpt) {
781                 sculptsession_bm_to_me_update_data_only(ob, reorder);
782
783                 /* ensure the objects DerivedMesh mesh doesn't hold onto arrays now realloc'd in the mesh [#34473] */
784                 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
785         }
786 }
787
788 void BKE_sculptsession_bm_to_me_for_render(Object *object)
789 {
790         if (object && object->sculpt) {
791                 if (object->sculpt->bm) {
792                         /* Ensure no points to old arrays are stored in DM
793                          *
794                          * Apparently, we could not use DEG_id_tag_update
795                          * here because this will lead to the while object
796                          * surface to disappear, so we'll release DM in place.
797                          */
798                         BKE_object_free_derived_caches(object);
799
800                         if (object->sculpt->pbvh) {
801                                 BKE_pbvh_free(object->sculpt->pbvh);
802                                 object->sculpt->pbvh = NULL;
803                         }
804
805                         sculptsession_bm_to_me_update_data_only(object, false);
806
807                         /* In contrast with sculptsession_bm_to_me no need in
808                          * DAG tag update here - derived mesh was freed and
809                          * old pointers are nowhere stored.
810                          */
811                 }
812         }
813 }
814
815 void BKE_sculptsession_free(Object *ob)
816 {
817         if (ob && ob->sculpt) {
818                 SculptSession *ss = ob->sculpt;
819                 DerivedMesh *dm = ob->derivedFinal;
820
821                 if (ss->bm) {
822                         BKE_sculptsession_bm_to_me(ob, true);
823                         BM_mesh_free(ss->bm);
824                 }
825
826                 if (ss->pbvh)
827                         BKE_pbvh_free(ss->pbvh);
828                 if (ss->bm_log)
829                         BM_log_free(ss->bm_log);
830
831                 if (dm && dm->getPBVH)
832                         dm->getPBVH(NULL, dm, OB_MODE_OBJECT);  /* signal to clear */
833
834                 if (ss->texcache)
835                         MEM_freeN(ss->texcache);
836
837                 if (ss->tex_pool)
838                         BKE_image_pool_free(ss->tex_pool);
839
840                 if (ss->layer_co)
841                         MEM_freeN(ss->layer_co);
842
843                 if (ss->orig_cos)
844                         MEM_freeN(ss->orig_cos);
845                 if (ss->deform_cos)
846                         MEM_freeN(ss->deform_cos);
847                 if (ss->deform_imats)
848                         MEM_freeN(ss->deform_imats);
849
850                 BKE_sculptsession_free_vwpaint_data(ob->sculpt);
851
852                 MEM_freeN(ss);
853
854                 ob->sculpt = NULL;
855         }
856 }
857
858 /* Sculpt mode handles multires differently from regular meshes, but only if
859  * it's the last modifier on the stack and it is not on the first level */
860 MultiresModifierData *BKE_sculpt_multires_active(Scene *scene, Object *ob)
861 {
862         Mesh *me = (Mesh *)ob->data;
863         ModifierData *md;
864         VirtualModifierData virtualModifierData;
865
866         if (ob->sculpt && ob->sculpt->bm) {
867                 /* can't combine multires and dynamic topology */
868                 return NULL;
869         }
870
871         if (!CustomData_get_layer(&me->ldata, CD_MDISPS)) {
872                 /* multires can't work without displacement layer */
873                 return NULL;
874         }
875
876         for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData); md; md = md->next) {
877                 if (md->type == eModifierType_Multires) {
878                         MultiresModifierData *mmd = (MultiresModifierData *)md;
879
880                         if (!modifier_isEnabled(scene, md, eModifierMode_Realtime))
881                                 continue;
882
883                         if (mmd->sculptlvl > 0) return mmd;
884                         else return NULL;
885                 }
886         }
887
888         return NULL;
889 }
890
891
892 /* Checks if there are any supported deformation modifiers active */
893 static bool sculpt_modifiers_active(Scene *scene, Sculpt *sd, Object *ob)
894 {
895         ModifierData *md;
896         Mesh *me = (Mesh *)ob->data;
897         MultiresModifierData *mmd = BKE_sculpt_multires_active(scene, ob);
898         VirtualModifierData virtualModifierData;
899
900         if (mmd || ob->sculpt->bm)
901                 return false;
902
903         /* non-locked shape keys could be handled in the same way as deformed mesh */
904         if ((ob->shapeflag & OB_SHAPE_LOCK) == 0 && me->key && ob->shapenr)
905                 return true;
906
907         md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
908
909         /* exception for shape keys because we can edit those */
910         for (; md; md = md->next) {
911                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
912                 if (!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
913                 if (ELEM(md->type, eModifierType_ShapeKey, eModifierType_Multires)) continue;
914
915                 if (mti->type == eModifierTypeType_OnlyDeform) return true;
916                 else if ((sd->flags & SCULPT_ONLY_DEFORM) == 0) return true;
917         }
918
919         return false;
920 }
921
922 /**
923  * \param need_mask So the DerivedMesh thats returned has mask data
924  */
925 void BKE_sculpt_update_mesh_elements(
926         const EvaluationContext *eval_ctx, Scene *scene, Sculpt *sd, Object *ob,
927         bool need_pmap, bool need_mask)
928 {
929         DerivedMesh *dm;
930         SculptSession *ss = ob->sculpt;
931         Mesh *me = ob->data;
932         MultiresModifierData *mmd = BKE_sculpt_multires_active(scene, ob);
933
934         ss->modifiers_active = sculpt_modifiers_active(scene, sd, ob);
935         ss->show_diffuse_color = (sd->flags & SCULPT_SHOW_DIFFUSE) != 0;
936         ss->show_mask = (sd->flags & SCULPT_HIDE_MASK) == 0;
937
938         ss->building_vp_handle = false;
939
940         if (need_mask) {
941                 if (mmd == NULL) {
942                         if (!CustomData_has_layer(&me->vdata, CD_PAINT_MASK)) {
943                                 BKE_sculpt_mask_layers_ensure(ob, NULL);
944                         }
945                 }
946                 else {
947                         if (!CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK)) {
948 #if 1
949                                 BKE_sculpt_mask_layers_ensure(ob, mmd);
950 #else                           /* if we wanted to support adding mask data while multi-res painting, we would need to do this */
951                                 if ((ED_sculpt_mask_layers_ensure(ob, mmd) & ED_SCULPT_MASK_LAYER_CALC_LOOP)) {
952                                         /* remake the derived mesh */
953                                         ob->recalc |= OB_RECALC_DATA;
954                                         BKE_object_handle_update(scene, ob);
955                                 }
956 #endif
957                         }
958                 }
959         }
960
961         /* tessfaces aren't used and will become invalid */
962         BKE_mesh_tessface_clear(me);
963
964         ss->kb = (mmd == NULL) ? BKE_keyblock_from_object(ob) : NULL;
965
966         dm = mesh_get_derived_final(eval_ctx, scene, ob, CD_MASK_BAREMESH);
967
968         /* VWPaint require mesh info for loop lookup, so require sculpt mode here */
969         if (mmd && eval_ctx->object_mode & OB_MODE_SCULPT) {
970                 ss->multires = mmd;
971                 ss->totvert = dm->getNumVerts(dm);
972                 ss->totpoly = dm->getNumPolys(dm);
973                 ss->mvert = NULL;
974                 ss->mpoly = NULL;
975                 ss->mloop = NULL;
976         }
977         else {
978                 ss->totvert = me->totvert;
979                 ss->totpoly = me->totpoly;
980                 ss->mvert = me->mvert;
981                 ss->mpoly = me->mpoly;
982                 ss->mloop = me->mloop;
983                 ss->multires = NULL;
984                 ss->vmask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
985         }
986
987         ss->pbvh = dm->getPBVH(ob, dm, eval_ctx->object_mode);
988         ss->pmap = (need_pmap && dm->getPolyMap) ? dm->getPolyMap(ob, dm) : NULL;
989
990         pbvh_show_diffuse_color_set(ss->pbvh, ss->show_diffuse_color);
991         pbvh_show_mask_set(ss->pbvh, ss->show_mask);
992
993         if (ss->modifiers_active) {
994                 if (!ss->orig_cos) {
995                         int a;
996
997                         BKE_sculptsession_free_deformMats(ss);
998
999                         ss->orig_cos = (ss->kb) ? BKE_keyblock_convert_to_vertcos(ob, ss->kb) : BKE_mesh_vertexCos_get(me, NULL);
1000
1001                         BKE_crazyspace_build_sculpt(eval_ctx, scene, ob, &ss->deform_imats, &ss->deform_cos);
1002                         BKE_pbvh_apply_vertCos(ss->pbvh, ss->deform_cos);
1003
1004                         for (a = 0; a < me->totvert; ++a) {
1005                                 invert_m3(ss->deform_imats[a]);
1006                         }
1007                 }
1008         }
1009         else {
1010                 BKE_sculptsession_free_deformMats(ss);
1011         }
1012
1013         if (ss->kb != NULL && ss->deform_cos == NULL) {
1014                 ss->deform_cos = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
1015         }
1016
1017         /* if pbvh is deformed, key block is already applied to it */
1018         if (ss->kb) {
1019                 bool pbvh_deformd = BKE_pbvh_isDeformed(ss->pbvh);
1020                 if (!pbvh_deformd || ss->deform_cos == NULL) {
1021                         float (*vertCos)[3] = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
1022
1023                         if (vertCos) {
1024                                 if (!pbvh_deformd) {
1025                                         /* apply shape keys coordinates to PBVH */
1026                                         BKE_pbvh_apply_vertCos(ss->pbvh, vertCos);
1027                                 }
1028                                 if (ss->deform_cos == NULL) {
1029                                         ss->deform_cos = vertCos;
1030                                 }
1031                                 if (vertCos != ss->deform_cos) {
1032                                         MEM_freeN(vertCos);
1033                                 }
1034                         }
1035                 }
1036         }
1037
1038         /* 2.8x - avoid full mesh update! */
1039         BKE_mesh_batch_cache_dirty(me, BKE_MESH_BATCH_DIRTY_SCULPT_COORDS);
1040 }
1041
1042 int BKE_sculpt_mask_layers_ensure(Object *ob, MultiresModifierData *mmd)
1043 {
1044         const float *paint_mask;
1045         Mesh *me = ob->data;
1046         int ret = 0;
1047
1048         paint_mask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
1049
1050         /* if multires is active, create a grid paint mask layer if there
1051          * isn't one already */
1052         if (mmd && !CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK)) {
1053                 GridPaintMask *gmask;
1054                 int level = max_ii(1, mmd->sculptlvl);
1055                 int gridsize = BKE_ccg_gridsize(level);
1056                 int gridarea = gridsize * gridsize;
1057                 int i, j;
1058
1059                 gmask = CustomData_add_layer(&me->ldata, CD_GRID_PAINT_MASK,
1060                                              CD_CALLOC, NULL, me->totloop);
1061
1062                 for (i = 0; i < me->totloop; i++) {
1063                         GridPaintMask *gpm = &gmask[i];
1064
1065                         gpm->level = level;
1066                         gpm->data = MEM_callocN(sizeof(float) * gridarea,
1067                                                 "GridPaintMask.data");
1068                 }
1069
1070                 /* if vertices already have mask, copy into multires data */
1071                 if (paint_mask) {
1072                         for (i = 0; i < me->totpoly; i++) {
1073                                 const MPoly *p = &me->mpoly[i];
1074                                 float avg = 0;
1075
1076                                 /* mask center */
1077                                 for (j = 0; j < p->totloop; j++) {
1078                                         const MLoop *l = &me->mloop[p->loopstart + j];
1079                                         avg += paint_mask[l->v];
1080                                 }
1081                                 avg /= (float)p->totloop;
1082
1083                                 /* fill in multires mask corner */
1084                                 for (j = 0; j < p->totloop; j++) {
1085                                         GridPaintMask *gpm = &gmask[p->loopstart + j];
1086                                         const MLoop *l = &me->mloop[p->loopstart + j];
1087                                         const MLoop *prev = ME_POLY_LOOP_PREV(me->mloop, p, j);
1088                                         const MLoop *next = ME_POLY_LOOP_NEXT(me->mloop, p, j);
1089
1090                                         gpm->data[0] = avg;
1091                                         gpm->data[1] = (paint_mask[l->v] +
1092                                                         paint_mask[next->v]) * 0.5f;
1093                                         gpm->data[2] = (paint_mask[l->v] +
1094                                                         paint_mask[prev->v]) * 0.5f;
1095                                         gpm->data[3] = paint_mask[l->v];
1096                                 }
1097                         }
1098                 }
1099
1100                 ret |= SCULPT_MASK_LAYER_CALC_LOOP;
1101         }
1102
1103         /* create vertex paint mask layer if there isn't one already */
1104         if (!paint_mask) {
1105                 CustomData_add_layer(&me->vdata, CD_PAINT_MASK,
1106                                      CD_CALLOC, NULL, me->totvert);
1107                 ret |= SCULPT_MASK_LAYER_CALC_VERT;
1108         }
1109
1110         return ret;
1111 }
1112
1113 void BKE_sculpt_toolsettings_data_ensure(struct Scene *scene)
1114 {
1115         Sculpt *sd = scene->toolsettings->sculpt;
1116         if (sd == NULL) {
1117                 sd = scene->toolsettings->sculpt = MEM_callocN(sizeof(Sculpt), __func__);
1118
1119                 /* Turn on X plane mirror symmetry by default */
1120                 sd->paint.symmetry_flags |= PAINT_SYMM_X;
1121                 sd->paint.flags |= PAINT_SHOW_BRUSH;
1122
1123                 /* Make sure at least dyntopo subdivision is enabled */
1124                 sd->flags |= SCULPT_DYNTOPO_SUBDIVIDE | SCULPT_DYNTOPO_COLLAPSE;
1125         }
1126
1127         if (!sd->detail_size) {
1128                 sd->detail_size = 12;
1129         }
1130         if (!sd->detail_percent) {
1131                 sd->detail_percent = 25;
1132         }
1133         if (sd->constant_detail == 0.0f) {
1134                 sd->constant_detail = 3.0f;
1135         }
1136
1137         /* Set sane default tiling offsets */
1138         if (!sd->paint.tile_offset[0]) {
1139                 sd->paint.tile_offset[0] = 1.0f;
1140         }
1141         if (!sd->paint.tile_offset[1]) {
1142                 sd->paint.tile_offset[1] = 1.0f;
1143         }
1144         if (!sd->paint.tile_offset[2]) {
1145                 sd->paint.tile_offset[2] = 1.0f;
1146         }
1147 }