41
44
#include "BLI_bitmap.h"
42
45
#include "BLI_utildefines.h"
46
#include "BLI_math_vector.h"
44
48
#include "BKE_brush.h"
45
49
#include "BKE_context.h"
50
#include "BKE_depsgraph.h"
46
51
#include "BKE_global.h"
52
#include "BKE_image.h"
47
53
#include "BKE_library.h"
54
#include "BKE_object.h"
48
55
#include "BKE_paint.h"
49
57
#include "BKE_subsurf.h"
56
61
const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
57
62
const char PAINT_CURSOR_VERTEX_PAINT[3] = {255, 255, 255};
58
63
const char PAINT_CURSOR_WEIGHT_PAINT[3] = {200, 200, 255};
59
64
const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
61
Paint *paint_get_active(Scene *sce)
66
static OverlayControlFlags overlay_flags = 0;
68
void BKE_paint_invalidate_overlay_tex(Scene *scene, const Tex *tex)
70
Paint *p = BKE_paint_get_active(scene);
76
if (br->mtex.tex == tex)
77
overlay_flags |= PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY;
78
if (br->mask_mtex.tex == tex)
79
overlay_flags |= PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY;
82
void BKE_paint_invalidate_cursor_overlay(Scene *scene, CurveMapping *curve)
84
Paint *p = BKE_paint_get_active(scene);
87
if (br && br->curve == curve)
88
overlay_flags |= PAINT_INVALID_OVERLAY_CURVE;
91
void BKE_paint_invalidate_overlay_all(void)
93
overlay_flags |= (PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY |
94
PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY |
95
PAINT_INVALID_OVERLAY_CURVE);
98
OverlayControlFlags BKE_paint_get_overlay_flags(void)
100
return overlay_flags;
103
void BKE_paint_set_overlay_override(OverlayFlags flags)
105
if (flags & BRUSH_OVERLAY_OVERRIDE_MASK) {
106
if (flags & BRUSH_OVERLAY_CURSOR_OVERRIDE_ON_STROKE)
107
overlay_flags |= PAINT_OVERLAY_OVERRIDE_CURSOR;
108
if (flags & BRUSH_OVERLAY_PRIMARY_OVERRIDE_ON_STROKE)
109
overlay_flags |= PAINT_OVERLAY_OVERRIDE_PRIMARY;
110
if (flags & BRUSH_OVERLAY_SECONDARY_OVERRIDE_ON_STROKE)
111
overlay_flags |= PAINT_OVERLAY_OVERRIDE_SECONDARY;
114
overlay_flags &= ~(PAINT_OVERRIDE_MASK);
118
void BKE_paint_reset_overlay_invalid(OverlayControlFlags flag)
120
overlay_flags &= ~(flag);
124
Paint *BKE_paint_get_active(Scene *sce)
64
127
ToolSettings *ts = sce->toolsettings;
140
Brush *paint_brush(Paint *p)
205
PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
207
Scene *sce = CTX_data_scene(C);
211
ToolSettings *ts = sce->toolsettings;
212
Object *obact = NULL;
214
if (sce->basact && sce->basact->object)
215
obact = sce->basact->object;
217
if ((sima = CTX_wm_space_image(C)) != NULL) {
218
if (obact && obact->mode == OB_MODE_EDIT) {
219
if (sima->mode == SI_MODE_PAINT)
220
return PAINT_TEXTURE_2D;
221
else if (ts->use_uv_sculpt)
222
return PAINT_SCULPT_UV;
225
return PAINT_TEXTURE_2D;
229
switch (obact->mode) {
232
case OB_MODE_VERTEX_PAINT:
234
case OB_MODE_WEIGHT_PAINT:
236
case OB_MODE_TEXTURE_PAINT:
237
return PAINT_TEXTURE_PROJECTIVE;
239
if (ts->use_uv_sculpt)
240
return PAINT_SCULPT_UV;
242
return PAINT_TEXTURE_2D;
244
return PAINT_TEXTURE_2D;
248
/* default to image paint */
249
return PAINT_TEXTURE_2D;
253
return PAINT_INVALID;
256
Brush *BKE_paint_brush(Paint *p)
142
258
return p ? p->brush : NULL;
145
void paint_brush_set(Paint *p, Brush *br)
261
void BKE_paint_brush_set(Paint *p, Brush *br)
148
264
id_us_min((ID *)p->brush);
180
296
/* If there's no brush, create one */
181
brush = paint_brush(p);
297
brush = BKE_paint_brush(p);
182
298
if (brush == NULL)
183
299
brush = BKE_brush_add(G.main, "Brush");
184
paint_brush_set(p, brush);
300
BKE_paint_brush_set(p, brush);
186
302
memcpy(p->paint_cursor_col, col, 3);
187
303
p->paint_cursor_col[3] = 128;
252
368
return gpm->data[(y * factor) * gridsize + (x * factor)];
371
/* threshhold to move before updating the brush rotation */
372
#define RAKE_THRESHHOLD 20
374
void paint_calculate_rake_rotation(UnifiedPaintSettings *ups, const float mouse_pos[2])
376
const float u = 0.5f;
377
const float r = RAKE_THRESHHOLD;
380
sub_v2_v2v2(dpos, ups->last_rake, mouse_pos);
382
if (len_squared_v2(dpos) >= r * r) {
383
ups->brush_rotation = atan2(dpos[0], dpos[1]);
385
interp_v2_v2v2(ups->last_rake, ups->last_rake,
390
void free_sculptsession_deformMats(SculptSession *ss)
392
if (ss->orig_cos) MEM_freeN(ss->orig_cos);
393
if (ss->deform_cos) MEM_freeN(ss->deform_cos);
394
if (ss->deform_imats) MEM_freeN(ss->deform_imats);
397
ss->deform_cos = NULL;
398
ss->deform_imats = NULL;
401
/* Write out the sculpt dynamic-topology BMesh to the Mesh */
402
static void sculptsession_bm_to_me_update_data_only(Object *ob, bool reorder)
404
SculptSession *ss = ob->sculpt;
410
BM_ITER_MESH (efa, &iter, ss->bm, BM_FACES_OF_MESH) {
411
BM_elem_flag_set(efa, BM_ELEM_SMOOTH,
412
ss->bm_smooth_shading);
415
BM_log_mesh_elems_reorder(ss->bm, ss->bm_log);
416
BM_mesh_bm_to_me(ss->bm, ob->data, FALSE);
421
void sculptsession_bm_to_me(Object *ob, int reorder)
423
if (ob && ob->sculpt) {
424
sculptsession_bm_to_me_update_data_only(ob, reorder);
426
/* ensure the objects DerivedMesh mesh doesn't hold onto arrays now realloc'd in the mesh [#34473] */
427
DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
431
void sculptsession_bm_to_me_for_render(Object *object)
433
if (object && object->sculpt) {
434
if (object->sculpt->bm) {
435
/* Ensure no points to old arrays are stored in DM
437
* Apparently, we could not use DAG_id_tag_update
438
* here because this will lead to the while object
439
* surface to disappear, so we'll release DM in place.
441
BKE_object_free_derived_caches(object);
443
if (object->sculpt->pbvh) {
444
BKE_pbvh_free(object->sculpt->pbvh);
445
object->sculpt->pbvh = NULL;
448
sculptsession_bm_to_me_update_data_only(object, false);
450
/* In contrast with sculptsession_bm_to_me no need in
451
* DAG tag update here - derived mesh was freed and
452
* old pointers are nowhere stored.
458
void free_sculptsession(Object *ob)
460
if (ob && ob->sculpt) {
461
SculptSession *ss = ob->sculpt;
462
DerivedMesh *dm = ob->derivedFinal;
465
sculptsession_bm_to_me(ob, TRUE);
466
BM_mesh_free(ss->bm);
470
BKE_pbvh_free(ss->pbvh);
472
BM_log_free(ss->bm_log);
474
if (dm && dm->getPBVH)
475
dm->getPBVH(NULL, dm); /* signal to clear */
478
MEM_freeN(ss->texcache);
481
BKE_image_pool_free(ss->tex_pool);
484
MEM_freeN(ss->layer_co);
487
MEM_freeN(ss->orig_cos);
489
MEM_freeN(ss->deform_cos);
490
if (ss->deform_imats)
491
MEM_freeN(ss->deform_imats);