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