Rename any instance of scene layer or render layer in code with view layer
[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 *sl, const Tex *tex)
79 {
80         Paint *p = BKE_paint_get_active(scene, sl);
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 *sl, CurveMapping *curve)
93 {
94         Paint *p = BKE_paint_get_active(scene, sl);
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 *sl)
161 {
162         if (sce && sl) {
163                 ToolSettings *ts = sce->toolsettings;
164                 
165                 if (sl->basact && sl->basact->object) {
166                         switch (sl->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 *sl = CTX_data_view_layer(C);
193         SpaceImage *sima;
194
195         if (sce && sl) {
196                 ToolSettings *ts = sce->toolsettings;
197                 Object *obact = NULL;
198
199                 if (sl->basact && sl->basact->object)
200                         obact = sl->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 *sl = CTX_data_view_layer(C);
244         SpaceImage *sima;
245
246         if (sce && sl) {
247                 ToolSettings *ts = sce->toolsettings;
248                 Object *obact = NULL;
249
250                 if (sl->basact && sl->basact->object)
251                         obact = sl->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 void paint_calculate_rake_rotation(UnifiedPaintSettings *ups, Brush *brush, const float mouse_pos[2])
645 {
646         if ((brush->mtex.brush_angle_mode & MTEX_ANGLE_RAKE) || (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RAKE)) {
647                 const float r = RAKE_THRESHHOLD;
648                 float rotation;
649
650                 float dpos[2];
651                 sub_v2_v2v2(dpos, ups->last_rake, mouse_pos);
652
653                 if (len_squared_v2(dpos) >= r * r) {
654                         rotation = atan2f(dpos[0], dpos[1]);
655
656                         copy_v2_v2(ups->last_rake, mouse_pos);
657
658                         ups->last_rake_angle = rotation;
659
660                         paint_update_brush_rake_rotation(ups, brush, rotation);
661                 }
662                 /* make sure we reset here to the last rotation to avoid accumulating
663                  * values in case a random rotation is also added */
664                 else {
665                         paint_update_brush_rake_rotation(ups, brush, ups->last_rake_angle);
666                 }
667         }
668         else {
669                 ups->brush_rotation = ups->brush_rotation_sec = 0.0f;
670         }
671 }
672
673 void BKE_sculptsession_free_deformMats(SculptSession *ss)
674 {
675         MEM_SAFE_FREE(ss->orig_cos);
676         MEM_SAFE_FREE(ss->deform_cos);
677         MEM_SAFE_FREE(ss->deform_imats);
678 }
679
680 void BKE_sculptsession_free_vwpaint_data(struct SculptSession *ss)
681 {
682         struct SculptVertexPaintGeomMap *gmap = NULL;
683         if (ss->mode_type == OB_MODE_VERTEX_PAINT) {
684                 gmap = &ss->mode.vpaint.gmap;
685
686                 MEM_SAFE_FREE(ss->mode.vpaint.previous_color);
687         }
688         else if (ss->mode_type == OB_MODE_WEIGHT_PAINT) {
689                 gmap = &ss->mode.wpaint.gmap;
690
691                 MEM_SAFE_FREE(ss->mode.wpaint.alpha_weight);
692                 if (ss->mode.wpaint.dvert_prev) {
693                         BKE_defvert_array_free_elems(ss->mode.wpaint.dvert_prev, ss->totvert);
694                         MEM_freeN(ss->mode.wpaint.dvert_prev);
695                         ss->mode.wpaint.dvert_prev = NULL;
696                 }
697         }
698         else {
699                 return;
700         }
701         MEM_SAFE_FREE(gmap->vert_to_loop);
702         MEM_SAFE_FREE(gmap->vert_map_mem);
703         MEM_SAFE_FREE(gmap->vert_to_poly);
704         MEM_SAFE_FREE(gmap->poly_map_mem);
705 }
706
707 /* Write out the sculpt dynamic-topology BMesh to the Mesh */
708 static void sculptsession_bm_to_me_update_data_only(Object *ob, bool reorder)
709 {
710         SculptSession *ss = ob->sculpt;
711
712         if (ss->bm) {
713                 if (ob->data) {
714                         BMIter iter;
715                         BMFace *efa;
716                         BM_ITER_MESH (efa, &iter, ss->bm, BM_FACES_OF_MESH) {
717                                 BM_elem_flag_set(efa, BM_ELEM_SMOOTH, ss->bm_smooth_shading);
718                         }
719                         if (reorder)
720                                 BM_log_mesh_elems_reorder(ss->bm, ss->bm_log);
721                         BM_mesh_bm_to_me(ss->bm, ob->data, (&(struct BMeshToMeshParams){0}));
722                 }
723         }
724 }
725
726 void BKE_sculptsession_bm_to_me(Object *ob, bool reorder)
727 {
728         if (ob && ob->sculpt) {
729                 sculptsession_bm_to_me_update_data_only(ob, reorder);
730
731                 /* ensure the objects DerivedMesh mesh doesn't hold onto arrays now realloc'd in the mesh [#34473] */
732                 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
733         }
734 }
735
736 void BKE_sculptsession_bm_to_me_for_render(Object *object)
737 {
738         if (object && object->sculpt) {
739                 if (object->sculpt->bm) {
740                         /* Ensure no points to old arrays are stored in DM
741                          *
742                          * Apparently, we could not use DEG_id_tag_update
743                          * here because this will lead to the while object
744                          * surface to disappear, so we'll release DM in place.
745                          */
746                         BKE_object_free_derived_caches(object);
747
748                         if (object->sculpt->pbvh) {
749                                 BKE_pbvh_free(object->sculpt->pbvh);
750                                 object->sculpt->pbvh = NULL;
751                         }
752
753                         sculptsession_bm_to_me_update_data_only(object, false);
754
755                         /* In contrast with sculptsession_bm_to_me no need in
756                          * DAG tag update here - derived mesh was freed and
757                          * old pointers are nowhere stored.
758                          */
759                 }
760         }
761 }
762
763 void BKE_sculptsession_free(Object *ob)
764 {
765         if (ob && ob->sculpt) {
766                 SculptSession *ss = ob->sculpt;
767                 DerivedMesh *dm = ob->derivedFinal;
768
769                 if (ss->bm) {
770                         BKE_sculptsession_bm_to_me(ob, true);
771                         BM_mesh_free(ss->bm);
772                 }
773
774                 if (ss->pbvh)
775                         BKE_pbvh_free(ss->pbvh);
776                 if (ss->bm_log)
777                         BM_log_free(ss->bm_log);
778
779                 if (dm && dm->getPBVH)
780                         dm->getPBVH(NULL, dm);  /* signal to clear */
781
782                 if (ss->texcache)
783                         MEM_freeN(ss->texcache);
784
785                 if (ss->tex_pool)
786                         BKE_image_pool_free(ss->tex_pool);
787
788                 if (ss->layer_co)
789                         MEM_freeN(ss->layer_co);
790
791                 if (ss->orig_cos)
792                         MEM_freeN(ss->orig_cos);
793                 if (ss->deform_cos)
794                         MEM_freeN(ss->deform_cos);
795                 if (ss->deform_imats)
796                         MEM_freeN(ss->deform_imats);
797
798                 BKE_sculptsession_free_vwpaint_data(ob->sculpt);
799
800                 MEM_freeN(ss);
801
802                 ob->sculpt = NULL;
803         }
804 }
805
806 /* Sculpt mode handles multires differently from regular meshes, but only if
807  * it's the last modifier on the stack and it is not on the first level */
808 MultiresModifierData *BKE_sculpt_multires_active(Scene *scene, Object *ob)
809 {
810         Mesh *me = (Mesh *)ob->data;
811         ModifierData *md;
812         VirtualModifierData virtualModifierData;
813
814         if (ob->sculpt && ob->sculpt->bm) {
815                 /* can't combine multires and dynamic topology */
816                 return NULL;
817         }
818
819         if (!CustomData_get_layer(&me->ldata, CD_MDISPS)) {
820                 /* multires can't work without displacement layer */
821                 return NULL;
822         }
823
824         for (md = modifiers_getVirtualModifierList(ob, &virtualModifierData); md; md = md->next) {
825                 if (md->type == eModifierType_Multires) {
826                         MultiresModifierData *mmd = (MultiresModifierData *)md;
827
828                         if (!modifier_isEnabled(scene, md, eModifierMode_Realtime))
829                                 continue;
830
831                         if (mmd->sculptlvl > 0) return mmd;
832                         else return NULL;
833                 }
834         }
835
836         return NULL;
837 }
838
839
840 /* Checks if there are any supported deformation modifiers active */
841 static bool sculpt_modifiers_active(Scene *scene, Sculpt *sd, Object *ob)
842 {
843         ModifierData *md;
844         Mesh *me = (Mesh *)ob->data;
845         MultiresModifierData *mmd = BKE_sculpt_multires_active(scene, ob);
846         VirtualModifierData virtualModifierData;
847
848         if (mmd || ob->sculpt->bm)
849                 return false;
850
851         /* non-locked shape keys could be handled in the same way as deformed mesh */
852         if ((ob->shapeflag & OB_SHAPE_LOCK) == 0 && me->key && ob->shapenr)
853                 return true;
854
855         md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
856
857         /* exception for shape keys because we can edit those */
858         for (; md; md = md->next) {
859                 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
860                 if (!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
861                 if (ELEM(md->type, eModifierType_ShapeKey, eModifierType_Multires)) continue;
862
863                 if (mti->type == eModifierTypeType_OnlyDeform) return true;
864                 else if ((sd->flags & SCULPT_ONLY_DEFORM) == 0) return true;
865         }
866
867         return false;
868 }
869
870 /**
871  * \param need_mask So the DerivedMesh thats returned has mask data
872  */
873 void BKE_sculpt_update_mesh_elements(
874         const EvaluationContext *eval_ctx, Scene *scene, Sculpt *sd, Object *ob,
875         bool need_pmap, bool need_mask)
876 {
877         DerivedMesh *dm;
878         SculptSession *ss = ob->sculpt;
879         Mesh *me = ob->data;
880         MultiresModifierData *mmd = BKE_sculpt_multires_active(scene, ob);
881
882         ss->modifiers_active = sculpt_modifiers_active(scene, sd, ob);
883         ss->show_diffuse_color = (sd->flags & SCULPT_SHOW_DIFFUSE) != 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(eval_ctx, 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
939         if (ss->modifiers_active) {
940                 if (!ss->orig_cos) {
941                         int a;
942
943                         BKE_sculptsession_free_deformMats(ss);
944
945                         ss->orig_cos = (ss->kb) ? BKE_keyblock_convert_to_vertcos(ob, ss->kb) : BKE_mesh_vertexCos_get(me, NULL);
946
947                         BKE_crazyspace_build_sculpt(eval_ctx, scene, ob, &ss->deform_imats, &ss->deform_cos);
948                         BKE_pbvh_apply_vertCos(ss->pbvh, ss->deform_cos);
949
950                         for (a = 0; a < me->totvert; ++a) {
951                                 invert_m3(ss->deform_imats[a]);
952                         }
953                 }
954         }
955         else {
956                 BKE_sculptsession_free_deformMats(ss);
957         }
958
959         if (ss->kb != NULL && ss->deform_cos == NULL) {
960                 ss->deform_cos = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
961         }
962
963         /* if pbvh is deformed, key block is already applied to it */
964         if (ss->kb) {
965                 bool pbvh_deformd = BKE_pbvh_isDeformed(ss->pbvh);
966                 if (!pbvh_deformd || ss->deform_cos == NULL) {
967                         float (*vertCos)[3] = BKE_keyblock_convert_to_vertcos(ob, ss->kb);
968
969                         if (vertCos) {
970                                 if (!pbvh_deformd) {
971                                         /* apply shape keys coordinates to PBVH */
972                                         BKE_pbvh_apply_vertCos(ss->pbvh, vertCos);
973                                 }
974                                 if (ss->deform_cos == NULL) {
975                                         ss->deform_cos = vertCos;
976                                 }
977                                 if (vertCos != ss->deform_cos) {
978                                         MEM_freeN(vertCos);
979                                 }
980                         }
981                 }
982         }
983
984         /* 2.8x - avoid full mesh update! */
985         BKE_mesh_batch_cache_dirty(me, BKE_MESH_BATCH_DIRTY_SCULPT_COORDS);
986 }
987
988 int BKE_sculpt_mask_layers_ensure(Object *ob, MultiresModifierData *mmd)
989 {
990         const float *paint_mask;
991         Mesh *me = ob->data;
992         int ret = 0;
993
994         paint_mask = CustomData_get_layer(&me->vdata, CD_PAINT_MASK);
995
996         /* if multires is active, create a grid paint mask layer if there
997          * isn't one already */
998         if (mmd && !CustomData_has_layer(&me->ldata, CD_GRID_PAINT_MASK)) {
999                 GridPaintMask *gmask;
1000                 int level = max_ii(1, mmd->sculptlvl);
1001                 int gridsize = BKE_ccg_gridsize(level);
1002                 int gridarea = gridsize * gridsize;
1003                 int i, j;
1004
1005                 gmask = CustomData_add_layer(&me->ldata, CD_GRID_PAINT_MASK,
1006                                              CD_CALLOC, NULL, me->totloop);
1007
1008                 for (i = 0; i < me->totloop; i++) {
1009                         GridPaintMask *gpm = &gmask[i];
1010
1011                         gpm->level = level;
1012                         gpm->data = MEM_callocN(sizeof(float) * gridarea,
1013                                                 "GridPaintMask.data");
1014                 }
1015
1016                 /* if vertices already have mask, copy into multires data */
1017                 if (paint_mask) {
1018                         for (i = 0; i < me->totpoly; i++) {
1019                                 const MPoly *p = &me->mpoly[i];
1020                                 float avg = 0;
1021
1022                                 /* mask center */
1023                                 for (j = 0; j < p->totloop; j++) {
1024                                         const MLoop *l = &me->mloop[p->loopstart + j];
1025                                         avg += paint_mask[l->v];
1026                                 }
1027                                 avg /= (float)p->totloop;
1028
1029                                 /* fill in multires mask corner */
1030                                 for (j = 0; j < p->totloop; j++) {
1031                                         GridPaintMask *gpm = &gmask[p->loopstart + j];
1032                                         const MLoop *l = &me->mloop[p->loopstart + j];
1033                                         const MLoop *prev = ME_POLY_LOOP_PREV(me->mloop, p, j);
1034                                         const MLoop *next = ME_POLY_LOOP_NEXT(me->mloop, p, j);
1035
1036                                         gpm->data[0] = avg;
1037                                         gpm->data[1] = (paint_mask[l->v] +
1038                                                         paint_mask[next->v]) * 0.5f;
1039                                         gpm->data[2] = (paint_mask[l->v] +
1040                                                         paint_mask[prev->v]) * 0.5f;
1041                                         gpm->data[3] = paint_mask[l->v];
1042                                 }
1043                         }
1044                 }
1045
1046                 ret |= SCULPT_MASK_LAYER_CALC_LOOP;
1047         }
1048
1049         /* create vertex paint mask layer if there isn't one already */
1050         if (!paint_mask) {
1051                 CustomData_add_layer(&me->vdata, CD_PAINT_MASK,
1052                                      CD_CALLOC, NULL, me->totvert);
1053                 ret |= SCULPT_MASK_LAYER_CALC_VERT;
1054         }
1055
1056         return ret;
1057 }