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