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