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