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