~ubuntu-branches/ubuntu/trusty/blender/trusty-proposed

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/paint.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2013-08-14 10:43:49 UTC
  • mfrom: (14.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20130814104349-t1d5mtwkphp12dyj
Tags: 2.68a-3
* Upload to unstable
* debian/: python3.3 Depends simplified
  - debian/control: python3.3 Depends dropped
    for blender-data package
  - 0001-blender_thumbnailer.patch refreshed
* debian/control: libavcodec b-dep versioning dropped

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 *  \ingroup bke
30
30
 */
31
31
 
 
32
#include <stdlib.h>
 
33
#include <string.h>
32
34
 
 
35
#include "MEM_guardedalloc.h"
33
36
 
34
37
#include "DNA_object_types.h"
35
38
#include "DNA_mesh_types.h"
40
43
 
41
44
#include "BLI_bitmap.h"
42
45
#include "BLI_utildefines.h"
 
46
#include "BLI_math_vector.h"
43
47
 
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"
 
56
#include "BKE_pbvh.h"
49
57
#include "BKE_subsurf.h"
50
58
 
51
59
#include "bmesh.h"
52
60
 
53
 
#include <stdlib.h>
54
 
#include <string.h>
55
 
 
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};
60
65
 
61
 
Paint *paint_get_active(Scene *sce)
 
66
static OverlayControlFlags overlay_flags = 0;
 
67
 
 
68
void BKE_paint_invalidate_overlay_tex(Scene *scene, const Tex *tex)
 
69
{
 
70
        Paint *p = BKE_paint_get_active(scene);
 
71
        Brush *br = p->brush;
 
72
 
 
73
        if (!br)
 
74
                return;
 
75
 
 
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;
 
80
}
 
81
 
 
82
void BKE_paint_invalidate_cursor_overlay(Scene *scene, CurveMapping *curve)
 
83
{
 
84
        Paint *p = BKE_paint_get_active(scene);
 
85
        Brush *br = p->brush;
 
86
 
 
87
        if (br && br->curve == curve)
 
88
                overlay_flags |= PAINT_INVALID_OVERLAY_CURVE;
 
89
}
 
90
 
 
91
void BKE_paint_invalidate_overlay_all(void)
 
92
{
 
93
        overlay_flags |= (PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY |
 
94
                          PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY |
 
95
                          PAINT_INVALID_OVERLAY_CURVE);
 
96
}
 
97
 
 
98
OverlayControlFlags BKE_paint_get_overlay_flags(void)
 
99
{
 
100
        return overlay_flags;
 
101
}
 
102
 
 
103
void BKE_paint_set_overlay_override(OverlayFlags flags)
 
104
{
 
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;
 
112
        }
 
113
        else {
 
114
                overlay_flags &= ~(PAINT_OVERRIDE_MASK);
 
115
        }
 
116
}
 
117
 
 
118
void BKE_paint_reset_overlay_invalid(OverlayControlFlags flag)
 
119
{
 
120
        overlay_flags &= ~(flag);
 
121
}
 
122
 
 
123
 
 
124
Paint *BKE_paint_get_active(Scene *sce)
62
125
{
63
126
        if (sce) {
64
127
                ToolSettings *ts = sce->toolsettings;
88
151
        return NULL;
89
152
}
90
153
 
91
 
Paint *paint_get_active_from_context(const bContext *C)
 
154
Paint *BKE_paint_get_active_from_context(const bContext *C)
92
155
{
93
156
        Scene *sce = CTX_data_scene(C);
94
157
        SpaceImage *sima;
126
189
                                                return &ts->uvsculpt->paint;
127
190
                                        else
128
191
                                                return &ts->imapaint.paint;
 
192
                                default:
 
193
                                        return &ts->imapaint.paint;
129
194
                        }
130
195
                }
131
196
                else {
137
202
        return NULL;
138
203
}
139
204
 
140
 
Brush *paint_brush(Paint *p)
 
205
PaintMode BKE_paintmode_get_active_from_context(const bContext *C)
 
206
{
 
207
        Scene *sce = CTX_data_scene(C);
 
208
        SpaceImage *sima;
 
209
 
 
210
        if (sce) {
 
211
                ToolSettings *ts = sce->toolsettings;
 
212
                Object *obact = NULL;
 
213
 
 
214
                if (sce->basact && sce->basact->object)
 
215
                        obact = sce->basact->object;
 
216
 
 
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;
 
223
                        }
 
224
                        else {
 
225
                                return PAINT_TEXTURE_2D;
 
226
                        }
 
227
                }
 
228
                else if (obact) {
 
229
                        switch (obact->mode) {
 
230
                                case OB_MODE_SCULPT:
 
231
                                        return PAINT_SCULPT;
 
232
                                case OB_MODE_VERTEX_PAINT:
 
233
                                        return PAINT_VERTEX;
 
234
                                case OB_MODE_WEIGHT_PAINT:
 
235
                                        return PAINT_WEIGHT;
 
236
                                case OB_MODE_TEXTURE_PAINT:
 
237
                                        return PAINT_TEXTURE_PROJECTIVE;
 
238
                                case OB_MODE_EDIT:
 
239
                                        if (ts->use_uv_sculpt)
 
240
                                                return PAINT_SCULPT_UV;
 
241
                                        else
 
242
                                                return PAINT_TEXTURE_2D;
 
243
                                default:
 
244
                                        return PAINT_TEXTURE_2D;
 
245
                        }
 
246
                }
 
247
                else {
 
248
                        /* default to image paint */
 
249
                        return PAINT_TEXTURE_2D;
 
250
                }
 
251
        }
 
252
 
 
253
        return PAINT_INVALID;
 
254
}
 
255
 
 
256
Brush *BKE_paint_brush(Paint *p)
141
257
{
142
258
        return p ? p->brush : NULL;
143
259
}
144
260
 
145
 
void paint_brush_set(Paint *p, Brush *br)
 
261
void BKE_paint_brush_set(Paint *p, Brush *br)
146
262
{
147
263
        if (p) {
148
264
                id_us_min((ID *)p->brush);
178
294
        Brush *brush;
179
295
 
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);
185
301
 
186
302
        memcpy(p->paint_cursor_col, col, 3);
187
303
        p->paint_cursor_col[3] = 128;
251
367
        
252
368
        return gpm->data[(y * factor) * gridsize + (x * factor)];
253
369
}
 
370
 
 
371
/* threshhold to move before updating the brush rotation */
 
372
#define RAKE_THRESHHOLD 20
 
373
 
 
374
void paint_calculate_rake_rotation(UnifiedPaintSettings *ups, const float mouse_pos[2])
 
375
{
 
376
        const float u = 0.5f;
 
377
        const float r = RAKE_THRESHHOLD;
 
378
 
 
379
        float dpos[2];
 
380
        sub_v2_v2v2(dpos, ups->last_rake, mouse_pos);
 
381
 
 
382
        if (len_squared_v2(dpos) >= r * r) {
 
383
                ups->brush_rotation = atan2(dpos[0], dpos[1]);
 
384
 
 
385
                interp_v2_v2v2(ups->last_rake, ups->last_rake,
 
386
                               mouse_pos, u);
 
387
        }
 
388
}
 
389
 
 
390
void free_sculptsession_deformMats(SculptSession *ss)
 
391
{
 
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);
 
395
 
 
396
        ss->orig_cos = NULL;
 
397
        ss->deform_cos = NULL;
 
398
        ss->deform_imats = NULL;
 
399
}
 
400
 
 
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)
 
403
{
 
404
        SculptSession *ss = ob->sculpt;
 
405
 
 
406
        if (ss->bm) {
 
407
                if (ob->data) {
 
408
                        BMIter iter;
 
409
                        BMFace *efa;
 
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);
 
413
                        }
 
414
                        if (reorder)
 
415
                                BM_log_mesh_elems_reorder(ss->bm, ss->bm_log);
 
416
                        BM_mesh_bm_to_me(ss->bm, ob->data, FALSE);
 
417
                }
 
418
        }
 
419
}
 
420
 
 
421
void sculptsession_bm_to_me(Object *ob, int reorder)
 
422
{
 
423
        if (ob && ob->sculpt) {
 
424
                sculptsession_bm_to_me_update_data_only(ob, reorder);
 
425
 
 
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);
 
428
        }
 
429
}
 
430
 
 
431
void sculptsession_bm_to_me_for_render(Object *object)
 
432
{
 
433
        if (object && object->sculpt) {
 
434
                if (object->sculpt->bm) {
 
435
                        /* Ensure no points to old arrays are stored in DM
 
436
                         *
 
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.
 
440
                         */
 
441
                        BKE_object_free_derived_caches(object);
 
442
 
 
443
                        if (object->sculpt->pbvh) {
 
444
                                BKE_pbvh_free(object->sculpt->pbvh);
 
445
                                object->sculpt->pbvh = NULL;
 
446
                        }
 
447
 
 
448
                        sculptsession_bm_to_me_update_data_only(object, false);
 
449
 
 
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.
 
453
                         */
 
454
                }
 
455
        }
 
456
}
 
457
 
 
458
void free_sculptsession(Object *ob)
 
459
{
 
460
        if (ob && ob->sculpt) {
 
461
                SculptSession *ss = ob->sculpt;
 
462
                DerivedMesh *dm = ob->derivedFinal;
 
463
 
 
464
                if (ss->bm) {
 
465
                        sculptsession_bm_to_me(ob, TRUE);
 
466
                        BM_mesh_free(ss->bm);
 
467
                }
 
468
 
 
469
                if (ss->pbvh)
 
470
                        BKE_pbvh_free(ss->pbvh);
 
471
                if (ss->bm_log)
 
472
                        BM_log_free(ss->bm_log);
 
473
 
 
474
                if (dm && dm->getPBVH)
 
475
                        dm->getPBVH(NULL, dm);  /* signal to clear */
 
476
 
 
477
                if (ss->texcache)
 
478
                        MEM_freeN(ss->texcache);
 
479
 
 
480
                if (ss->tex_pool)
 
481
                        BKE_image_pool_free(ss->tex_pool);
 
482
 
 
483
                if (ss->layer_co)
 
484
                        MEM_freeN(ss->layer_co);
 
485
 
 
486
                if (ss->orig_cos)
 
487
                        MEM_freeN(ss->orig_cos);
 
488
                if (ss->deform_cos)
 
489
                        MEM_freeN(ss->deform_cos);
 
490
                if (ss->deform_imats)
 
491
                        MEM_freeN(ss->deform_imats);
 
492
 
 
493
                MEM_freeN(ss);
 
494
 
 
495
                ob->sculpt = NULL;
 
496
        }
 
497
}