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