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