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