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