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

« back to all changes in this revision

Viewing changes to source/blender/editors/sculpt_paint/paint_cursor.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:
35
35
#include "BLI_utildefines.h"
36
36
 
37
37
#include "DNA_brush_types.h"
 
38
#include "DNA_customdata_types.h"
38
39
#include "DNA_color_types.h"
39
40
#include "DNA_object_types.h"
40
41
#include "DNA_scene_types.h"
45
46
#include "BKE_context.h"
46
47
#include "BKE_image.h"
47
48
#include "BKE_paint.h"
 
49
#include "BKE_colortools.h"
48
50
 
49
51
#include "WM_api.h"
50
52
 
67
69
 * There is also some ugliness with sculpt-specific code.
68
70
 */
69
71
 
70
 
typedef struct Snapshot {
71
 
        float size[3];
72
 
        float ofs[3];
73
 
        float rot;
74
 
        int BKE_brush_size_get;
 
72
typedef struct TexSnapshot {
 
73
        GLuint overlay_texture;
75
74
        int winx;
76
75
        int winy;
77
 
        int brush_map_mode;
78
 
        int curve_changed_timestamp;
79
 
} Snapshot;
 
76
        bool init;
 
77
        int old_size;
 
78
        float old_zoom;
 
79
        bool old_col;
 
80
} TexSnapshot;
80
81
 
81
 
static int same_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
 
82
static int same_tex_snap(TexSnapshot *snap, MTex *mtex, ViewContext *vc, bool col, float zoom)
82
83
{
83
 
        MTex *mtex = &brush->mtex;
84
 
 
85
 
        return (((mtex->tex) &&
86
 
                 equals_v3v3(mtex->ofs, snap->ofs) &&
87
 
                 equals_v3v3(mtex->size, snap->size) &&
88
 
                 mtex->rot == snap->rot) &&
89
 
 
90
 
                /* make brush smaller shouldn't cause a resample */
91
 
                ((mtex->brush_map_mode == MTEX_MAP_MODE_VIEW &&
92
 
                  (BKE_brush_size_get(vc->scene, brush) <= snap->BKE_brush_size_get)) ||
93
 
                 (BKE_brush_size_get(vc->scene, brush) == snap->BKE_brush_size_get)) &&
94
 
 
95
 
                (mtex->brush_map_mode == snap->brush_map_mode) &&
96
 
                (vc->ar->winx == snap->winx) &&
97
 
                (vc->ar->winy == snap->winy));
 
84
        return (/* make brush smaller shouldn't cause a resample */
 
85
                //(mtex->brush_map_mode != MTEX_MAP_MODE_VIEW ||
 
86
                //(BKE_brush_size_get(vc->scene, brush) <= snap->BKE_brush_size_get)) &&
 
87
 
 
88
                (mtex->brush_map_mode != MTEX_MAP_MODE_TILED ||
 
89
                 (vc->ar->winx == snap->winx &&
 
90
                  vc->ar->winy == snap->winy)) &&
 
91
                (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL ||
 
92
                snap->old_zoom == zoom) &&
 
93
                snap->old_col == col
 
94
                );
98
95
}
99
96
 
100
 
static void make_snap(Snapshot *snap, Brush *brush, ViewContext *vc)
 
97
static void make_tex_snap(TexSnapshot *snap, ViewContext *vc, float zoom)
101
98
{
102
 
        if (brush->mtex.tex) {
103
 
                snap->brush_map_mode = brush->mtex.brush_map_mode;
104
 
                copy_v3_v3(snap->ofs, brush->mtex.ofs);
105
 
                copy_v3_v3(snap->size, brush->mtex.size);
106
 
                snap->rot = brush->mtex.rot;
107
 
        }
108
 
        else {
109
 
                snap->brush_map_mode = -1;
110
 
                snap->ofs[0] = snap->ofs[1] = snap->ofs[2] = -1;
111
 
                snap->size[0] = snap->size[1] = snap->size[2] = -1;
112
 
                snap->rot = -1;
113
 
        }
114
 
 
115
 
        snap->BKE_brush_size_get = BKE_brush_size_get(vc->scene, brush);
 
99
        snap->old_zoom = zoom;
116
100
        snap->winx = vc->ar->winx;
117
101
        snap->winy = vc->ar->winy;
118
102
}
119
103
 
120
 
static int load_tex(Brush *br, ViewContext *vc)
 
104
static int load_tex(Brush *br, ViewContext *vc, float zoom, bool col, bool primary)
121
105
{
122
 
        static GLuint overlay_texture = 0;
123
106
        static int init = 0;
124
 
        static int tex_changed_timestamp = -1;
125
 
        static int curve_changed_timestamp = -1;
126
 
        static Snapshot snap;
127
 
        static int old_size = -1;
 
107
        static TexSnapshot primary_snap = {0};
 
108
        static TexSnapshot secondary_snap  = {0};
 
109
        TexSnapshot *target;
128
110
 
 
111
        MTex *mtex = (primary) ? &br->mtex : &br->mask_mtex;
 
112
        OverlayControlFlags overlay_flags = BKE_paint_get_overlay_flags();
129
113
        GLubyte *buffer = NULL;
130
114
 
131
115
        int size;
132
116
        int j;
133
117
        int refresh;
134
 
        
135
 
        if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED && !br->mtex.tex) return 0;
136
 
        
 
118
        GLenum format = col ? GL_RGBA : GL_ALPHA;
 
119
        OverlayControlFlags invalid = (primary) ? (overlay_flags & PAINT_INVALID_OVERLAY_TEXTURE_PRIMARY) :
 
120
                                   (overlay_flags & PAINT_INVALID_OVERLAY_TEXTURE_SECONDARY);
 
121
 
 
122
        target = (primary) ? &primary_snap : &secondary_snap;
 
123
                
137
124
        refresh = 
138
 
            !overlay_texture ||
139
 
            (br->mtex.tex &&
140
 
             (!br->mtex.tex->preview ||
141
 
              br->mtex.tex->preview->changed_timestamp[0] != tex_changed_timestamp)) ||
142
 
            !br->curve ||
143
 
            br->curve->changed_timestamp != curve_changed_timestamp ||
144
 
            !same_snap(&snap, br, vc);
 
125
            !target->overlay_texture ||
 
126
            (invalid != 0) ||
 
127
            !same_tex_snap(target, mtex, vc, col, zoom);
145
128
 
146
129
        if (refresh) {
147
130
                struct ImagePool *pool = NULL;
148
 
 
149
 
                if (br->mtex.tex && br->mtex.tex->preview)
150
 
                        tex_changed_timestamp = br->mtex.tex->preview->changed_timestamp[0];
151
 
 
152
 
                if (br->curve)
153
 
                        curve_changed_timestamp = br->curve->changed_timestamp;
154
 
 
155
 
                make_snap(&snap, br, vc);
156
 
 
157
 
                if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
 
131
                /* stencil is rotated later */
 
132
                const float rotation = (mtex->brush_map_mode != MTEX_MAP_MODE_STENCIL) ?
 
133
                                       -mtex->rot : 0;
 
134
 
 
135
                float radius = BKE_brush_size_get(vc->scene, br) * zoom;
 
136
 
 
137
                make_tex_snap(target, vc, zoom);
 
138
 
 
139
                if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
158
140
                        int s = BKE_brush_size_get(vc->scene, br);
159
141
                        int r = 1;
160
142
 
166
148
                        if (size < 256)
167
149
                                size = 256;
168
150
 
169
 
                        if (size < old_size)
170
 
                                size = old_size;
 
151
                        if (size < target->old_size)
 
152
                                size = target->old_size;
171
153
                }
172
154
                else
173
155
                        size = 512;
174
156
 
175
 
                if (old_size != size) {
176
 
                        if (overlay_texture) {
177
 
                                glDeleteTextures(1, &overlay_texture);
178
 
                                overlay_texture = 0;
 
157
                if (target->old_size != size) {
 
158
                        if (target->overlay_texture) {
 
159
                                glDeleteTextures(1, &target->overlay_texture);
 
160
                                target->overlay_texture = 0;
179
161
                        }
180
162
 
181
163
                        init = 0;
182
164
 
183
 
                        old_size = size;
 
165
                        target->old_size = size;
184
166
                }
185
 
 
186
 
                buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
187
 
 
188
 
                if (br->mtex.tex)
189
 
                        pool = BKE_image_pool_new();
 
167
                if (col)
 
168
                        buffer = MEM_mallocN(sizeof(GLubyte) * size * size * 4, "load_tex");
 
169
                else
 
170
                        buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
 
171
 
 
172
                pool = BKE_image_pool_new();
190
173
 
191
174
                #pragma omp parallel for schedule(static)
192
175
                for (j = 0; j < size; j++) {
198
181
 
199
182
                                // largely duplicated from tex_strength
200
183
 
201
 
                                const float rotation = -br->mtex.rot;
202
 
                                float radius = BKE_brush_size_get(vc->scene, br);
203
184
                                int index = j * size + i;
204
185
                                float x;
205
 
                                float avg;
206
186
 
207
187
                                x = (float)i / size;
208
188
                                y = (float)j / size;
209
189
 
210
 
                                x -= 0.5f;
211
 
                                y -= 0.5f;
212
 
 
213
 
                                if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) {
 
190
                                if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
214
191
                                        x *= vc->ar->winx / radius;
215
192
                                        y *= vc->ar->winy / radius;
216
193
                                }
217
194
                                else {
 
195
                                        x -= 0.5f;
 
196
                                        y -= 0.5f;
 
197
 
218
198
                                        x *= 2;
219
199
                                        y *= 2;
220
200
                                }
221
201
 
222
202
                                len = sqrtf(x * x + y * y);
223
203
 
224
 
                                if ((br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) || len <= 1) {
 
204
                                if (ELEM(mtex->brush_map_mode, MTEX_MAP_MODE_TILED, MTEX_MAP_MODE_STENCIL) || len <= 1) {
225
205
                                        /* it is probably worth optimizing for those cases where 
226
206
                                         * the texture is not rotated by skipping the calls to
227
207
                                         * atan2, sqrtf, sin, and cos. */
228
 
                                        if (br->mtex.tex && (rotation > 0.001f || rotation < -0.001f)) {
229
 
                                                const float angle    = atan2f(y, x) + rotation;
 
208
                                        if (mtex->tex && (rotation > 0.001f || rotation < -0.001f)) {
 
209
                                                const float angle = atan2f(y, x) + rotation;
230
210
 
231
211
                                                x = len * cosf(angle);
232
212
                                                y = len * sinf(angle);
233
213
                                        }
234
214
 
235
 
                                        x *= br->mtex.size[0];
236
 
                                        y *= br->mtex.size[1];
237
 
 
238
 
                                        x += br->mtex.ofs[0];
239
 
                                        y += br->mtex.ofs[1];
240
 
 
241
 
                                        avg = br->mtex.tex ? paint_get_tex_pixel(br, x, y, pool) : 1;
242
 
 
243
 
                                        avg += br->texture_sample_bias;
244
 
 
245
 
                                        if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW)
246
 
                                                avg *= BKE_brush_curve_strength(br, len, 1);  /* Falloff curve */
 
215
                                        x *= mtex->size[0];
 
216
                                        y *= mtex->size[1];
 
217
 
 
218
                                        x += mtex->ofs[0];
 
219
                                        y += mtex->ofs[1];
 
220
 
 
221
                                        if (col) {
 
222
                                                float rgba[4];
 
223
 
 
224
                                                paint_get_tex_pixel_col(mtex, x, y, rgba, pool);
 
225
 
 
226
                                                buffer[index * 4]     = rgba[0] * 255;
 
227
                                                buffer[index * 4 + 1] = rgba[1] * 255;
 
228
                                                buffer[index * 4 + 2] = rgba[2] * 255;
 
229
                                                buffer[index * 4 + 3] = rgba[3] * 255;
 
230
                                        }
 
231
                                        else {
 
232
                                                float avg = paint_get_tex_pixel(mtex, x, y, pool);
 
233
 
 
234
                                                avg += br->texture_sample_bias;
 
235
 
 
236
                                                /* clamp to avoid precision overflow */
 
237
                                                CLAMP(avg, 0.0f, 1.0f);
 
238
                                                buffer[index] = 255 - (GLubyte)(255 * avg);
 
239
                                        }
 
240
                                }
 
241
                                else {
 
242
                                        if (col) {
 
243
                                                buffer[index * 4]     = 0;
 
244
                                                buffer[index * 4 + 1] = 0;
 
245
                                                buffer[index * 4 + 2] = 0;
 
246
                                                buffer[index * 4 + 3] = 0;
 
247
                                        }
 
248
                                        else {
 
249
                                                buffer[index] = 0;
 
250
                                        }
 
251
                                }
 
252
                        }
 
253
                }
 
254
 
 
255
                if (pool)
 
256
                        BKE_image_pool_free(pool);
 
257
 
 
258
                if (!target->overlay_texture)
 
259
                        glGenTextures(1, &target->overlay_texture);
 
260
        }
 
261
        else {
 
262
                size = target->old_size;
 
263
        }
 
264
 
 
265
        glBindTexture(GL_TEXTURE_2D, target->overlay_texture);
 
266
 
 
267
        if (refresh) {
 
268
                if (!init || (target->old_col != col)) {
 
269
                        glTexImage2D(GL_TEXTURE_2D, 0, format, size, size, 0, format, GL_UNSIGNED_BYTE, buffer);
 
270
                        init = 1;
 
271
                }
 
272
                else {
 
273
                        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, size, size, format, GL_UNSIGNED_BYTE, buffer);
 
274
                }
 
275
 
 
276
                if (buffer)
 
277
                        MEM_freeN(buffer);
 
278
 
 
279
                target->old_col = col;
 
280
        }
 
281
 
 
282
        glEnable(GL_TEXTURE_2D);
 
283
 
 
284
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
285
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
286
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
287
 
 
288
        if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
 
289
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
 
290
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
 
291
        }
 
292
 
 
293
        BKE_paint_reset_overlay_invalid(invalid);
 
294
 
 
295
        return 1;
 
296
}
 
297
 
 
298
static int load_tex_cursor(Brush *br, ViewContext *vc, float zoom)
 
299
{
 
300
        static GLuint overlay_texture = 0;
 
301
        static int init = 0;
 
302
        static int old_size = -1;
 
303
        static int old_zoom = -1;
 
304
 
 
305
        OverlayControlFlags overlay_flags = BKE_paint_get_overlay_flags();
 
306
        GLubyte *buffer = NULL;
 
307
 
 
308
        int size;
 
309
        int j;
 
310
        int refresh;
 
311
 
 
312
        refresh =
 
313
            !overlay_texture ||
 
314
            (overlay_flags & PAINT_INVALID_OVERLAY_CURVE) ||
 
315
            old_zoom != zoom;
 
316
 
 
317
        if (refresh) {
 
318
                int s, r;
 
319
 
 
320
                old_zoom = zoom;
 
321
 
 
322
                s = BKE_brush_size_get(vc->scene, br);
 
323
                r = 1;
 
324
 
 
325
                for (s >>= 1; s > 0; s >>= 1)
 
326
                        r++;
 
327
 
 
328
                size = (1 << r);
 
329
 
 
330
                if (size < 256)
 
331
                        size = 256;
 
332
 
 
333
                if (size < old_size)
 
334
                        size = old_size;
 
335
 
 
336
                if (old_size != size) {
 
337
                        if (overlay_texture) {
 
338
                                glDeleteTextures(1, &overlay_texture);
 
339
                                overlay_texture = 0;
 
340
                        }
 
341
 
 
342
                        init = 0;
 
343
 
 
344
                        old_size = size;
 
345
                }
 
346
                buffer = MEM_mallocN(sizeof(GLubyte) * size * size, "load_tex");
 
347
 
 
348
                curvemapping_initialize(br->curve);
 
349
 
 
350
                #pragma omp parallel for schedule(static)
 
351
                for (j = 0; j < size; j++) {
 
352
                        int i;
 
353
                        float y;
 
354
                        float len;
 
355
 
 
356
                        for (i = 0; i < size; i++) {
 
357
 
 
358
                                // largely duplicated from tex_strength
 
359
 
 
360
                                int index = j * size + i;
 
361
                                float x;
 
362
 
 
363
                                x = (float)i / size;
 
364
                                y = (float)j / size;
 
365
 
 
366
                                x -= 0.5f;
 
367
                                y -= 0.5f;
 
368
 
 
369
                                x *= 2;
 
370
                                y *= 2;
 
371
 
 
372
                                len = sqrtf(x * x + y * y);
 
373
 
 
374
                                if (len <= 1) {
 
375
                                        float avg = BKE_brush_curve_strength_clamp(br, len, 1.0f);  /* Falloff curve */
247
376
 
248
377
                                        buffer[index] = 255 - (GLubyte)(255 * avg);
 
378
 
249
379
                                }
250
380
                                else {
251
381
                                        buffer[index] = 0;
253
383
                        }
254
384
                }
255
385
 
256
 
                if (pool)
257
 
                        BKE_image_pool_free(pool);
258
 
 
259
386
                if (!overlay_texture)
260
387
                        glGenTextures(1, &overlay_texture);
261
388
        }
284
411
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
285
412
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
286
413
 
287
 
        if (br->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
288
 
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
289
 
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
290
 
        }
 
414
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
 
415
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
 
416
 
 
417
        BKE_paint_reset_overlay_invalid(PAINT_INVALID_OVERLAY_CURVE);
291
418
 
292
419
        return 1;
293
420
}
294
421
 
 
422
 
 
423
 
295
424
static int project_brush_radius(ViewContext *vc,
296
425
                                float radius,
297
426
                                const float location[3])
344
473
                                     float location[3])
345
474
{
346
475
        Scene *scene = CTX_data_scene(C);
347
 
        Paint *paint = paint_get_active_from_context(C);
348
 
        float window[2];
 
476
        Paint *paint = BKE_paint_get_active_from_context(C);
 
477
        float mouse[2];
349
478
        int hit;
350
479
 
351
 
        window[0] = x + vc->ar->winrct.xmin;
352
 
        window[1] = y + vc->ar->winrct.ymin;
 
480
        mouse[0] = x;
 
481
        mouse[1] = y;
353
482
 
354
483
        if (vc->obact->sculpt && vc->obact->sculpt->pbvh &&
355
 
            sculpt_stroke_get_location(C, location, window))
 
484
            sculpt_stroke_get_location(C, location, mouse))
356
485
        {
357
 
                Brush *brush = paint_brush(paint);
 
486
                Brush *brush = BKE_paint_brush(paint);
358
487
                *pixel_radius =
359
488
                    project_brush_radius(vc,
360
489
                                         BKE_brush_unprojected_radius_get(scene, brush),
369
498
        }
370
499
        else {
371
500
                Sculpt *sd    = CTX_data_tool_settings(C)->sculpt;
372
 
                Brush *brush = paint_brush(&sd->paint);
 
501
                Brush *brush = BKE_paint_brush(&sd->paint);
373
502
 
374
503
                *pixel_radius = BKE_brush_size_get(scene, brush);
375
504
                hit = 0;
380
509
 
381
510
/* Draw an overlay that shows what effect the brush's texture will
382
511
 * have on brush strength */
383
 
/* TODO: sculpt only for now */
384
 
static void paint_draw_alpha_overlay(UnifiedPaintSettings *ups, Brush *brush,
385
 
                                     ViewContext *vc, int x, int y)
 
512
static void paint_draw_tex_overlay(UnifiedPaintSettings *ups, Brush *brush,
 
513
                                     ViewContext *vc, int x, int y, float zoom, bool col, bool primary)
386
514
{
387
515
        rctf quad;
388
 
 
389
516
        /* check for overlay mode */
390
 
        if (!(brush->flag & BRUSH_TEXTURE_OVERLAY) ||
391
 
            !(ELEM(brush->mtex.brush_map_mode, MTEX_MAP_MODE_VIEW, MTEX_MAP_MODE_TILED)))
 
517
 
 
518
        MTex *mtex = (primary) ? &brush->mtex : &brush->mask_mtex;
 
519
        bool valid = (primary) ? (brush->overlay_flags & BRUSH_OVERLAY_PRIMARY) != 0 :
 
520
                                 (brush->overlay_flags & BRUSH_OVERLAY_SECONDARY) != 0;
 
521
        int overlay_alpha = (primary) ? brush->texture_overlay_alpha : brush->mask_overlay_alpha;
 
522
 
 
523
        if (!(mtex->tex) || !((mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) ||
 
524
            (valid &&
 
525
                ELEM(mtex->brush_map_mode, MTEX_MAP_MODE_VIEW, MTEX_MAP_MODE_TILED))))
392
526
        {
393
527
                return;
394
528
        }
395
529
 
396
 
        /* save lots of GL state
397
 
         * TODO: check on whether all of these are needed? */
398
 
        glPushAttrib(GL_COLOR_BUFFER_BIT |
399
 
                     GL_CURRENT_BIT |
400
 
                     GL_DEPTH_BUFFER_BIT |
401
 
                     GL_ENABLE_BIT |
402
 
                     GL_LINE_BIT |
403
 
                     GL_POLYGON_BIT |
404
 
                     GL_STENCIL_BUFFER_BIT |
405
 
                     GL_TRANSFORM_BIT |
406
 
                     GL_VIEWPORT_BIT |
407
 
                     GL_TEXTURE_BIT);
408
 
 
409
 
        if (load_tex(brush, vc)) {
 
530
        if (load_tex(brush, vc, zoom, col, primary)) {
410
531
                glEnable(GL_BLEND);
411
532
 
412
533
                glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
417
538
                glPushMatrix();
418
539
                glLoadIdentity();
419
540
 
420
 
                if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
 
541
                if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
421
542
                        /* brush rotation */
422
543
                        glTranslatef(0.5, 0.5, 0);
423
 
                        glRotatef((double)RAD2DEGF((brush->flag & BRUSH_RAKE) ?
424
 
                                                   ups->last_angle : ups->special_rotation),
 
544
                        glRotatef((double)RAD2DEGF(ups->brush_rotation),
425
545
                                  0.0, 0.0, 1.0);
426
546
                        glTranslatef(-0.5f, -0.5f, 0);
427
547
 
428
548
                        /* scale based on tablet pressure */
429
 
                        if (ups->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
 
549
                        if (primary && ups->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
430
550
                                glTranslatef(0.5f, 0.5f, 0);
431
551
                                glScalef(1.0f / ups->pressure_value, 1.0f / ups->pressure_value, 1);
432
552
                                glTranslatef(-0.5f, -0.5f, 0);
434
554
 
435
555
                        if (ups->draw_anchored) {
436
556
                                const float *aim = ups->anchored_initial_mouse;
437
 
                                const rcti *win = &vc->ar->winrct;
438
 
                                quad.xmin = aim[0] - ups->anchored_size - win->xmin;
439
 
                                quad.ymin = aim[1] - ups->anchored_size - win->ymin;
440
 
                                quad.xmax = aim[0] + ups->anchored_size - win->xmin;
441
 
                                quad.ymax = aim[1] + ups->anchored_size - win->ymin;
 
557
                                quad.xmin = aim[0] - ups->anchored_size;
 
558
                                quad.ymin = aim[1] - ups->anchored_size;
 
559
                                quad.xmax = aim[0] + ups->anchored_size;
 
560
                                quad.ymax = aim[1] + ups->anchored_size;
442
561
                        }
443
562
                        else {
444
 
                                const int radius = BKE_brush_size_get(vc->scene, brush);
 
563
                                const int radius = BKE_brush_size_get(vc->scene, brush) * zoom;
445
564
                                quad.xmin = x - radius;
446
565
                                quad.ymin = y - radius;
447
566
                                quad.xmax = x + radius;
448
567
                                quad.ymax = y + radius;
449
568
                        }
450
569
                }
451
 
                else {
 
570
                else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
452
571
                        quad.xmin = 0;
453
572
                        quad.ymin = 0;
454
573
                        quad.xmax = BLI_rcti_size_x(&vc->ar->winrct);
455
574
                        quad.ymax = BLI_rcti_size_y(&vc->ar->winrct);
456
575
                }
457
 
 
458
 
                /* set quad color */
 
576
                /* Stencil code goes here */
 
577
                else {
 
578
                        if (primary) {
 
579
                                quad.xmin = -brush->stencil_dimension[0];
 
580
                                quad.ymin = -brush->stencil_dimension[1];
 
581
                                quad.xmax = brush->stencil_dimension[0];
 
582
                                quad.ymax = brush->stencil_dimension[1];
 
583
                        }
 
584
                        else {
 
585
                                quad.xmin = -brush->mask_stencil_dimension[0];
 
586
                                quad.ymin = -brush->mask_stencil_dimension[1];
 
587
                                quad.xmax = brush->mask_stencil_dimension[0];
 
588
                                quad.ymax = brush->mask_stencil_dimension[1];
 
589
                        }
 
590
                        glMatrixMode(GL_MODELVIEW);
 
591
                        glPushMatrix();
 
592
                        if (primary)
 
593
                                glTranslatef(brush->stencil_pos[0], brush->stencil_pos[1], 0);
 
594
                        else
 
595
                                glTranslatef(brush->mask_stencil_pos[0], brush->mask_stencil_pos[1], 0);
 
596
                        glRotatef(RAD2DEGF(mtex->rot), 0, 0, 1);
 
597
                        glMatrixMode(GL_TEXTURE);
 
598
                }
 
599
 
 
600
                /* set quad color. Colored overlay does not get blending */
 
601
                if (col)
 
602
                        glColor4f(1.0,
 
603
                                      1.0,
 
604
                                      1.0,
 
605
                                      overlay_alpha / 100.0f);
 
606
                else
 
607
                        glColor4f(U.sculpt_paint_overlay_col[0],
 
608
                                      U.sculpt_paint_overlay_col[1],
 
609
                                      U.sculpt_paint_overlay_col[2],
 
610
                                      overlay_alpha / 100.0f);
 
611
 
 
612
                /* draw textured quad */
 
613
                glBegin(GL_QUADS);
 
614
                glTexCoord2f(0, 0);
 
615
                glVertex2f(quad.xmin, quad.ymin);
 
616
                glTexCoord2f(1, 0);
 
617
                glVertex2f(quad.xmax, quad.ymin);
 
618
                glTexCoord2f(1, 1);
 
619
                glVertex2f(quad.xmax, quad.ymax);
 
620
                glTexCoord2f(0, 1);
 
621
                glVertex2f(quad.xmin, quad.ymax);
 
622
                glEnd();
 
623
 
 
624
                glPopMatrix();
 
625
 
 
626
                if (mtex->brush_map_mode == MTEX_MAP_MODE_STENCIL) {
 
627
                        glMatrixMode(GL_MODELVIEW);
 
628
                        glPopMatrix();
 
629
                }
 
630
        }
 
631
}
 
632
 
 
633
/* Draw an overlay that shows what effect the brush's texture will
 
634
 * have on brush strength */
 
635
static void paint_draw_cursor_overlay(UnifiedPaintSettings *ups, Brush *brush,
 
636
                                      ViewContext *vc, int x, int y, float zoom)
 
637
{
 
638
        rctf quad;
 
639
        /* check for overlay mode */
 
640
 
 
641
        if (!(brush->overlay_flags & BRUSH_OVERLAY_CURSOR)) {
 
642
                return;
 
643
        }
 
644
 
 
645
        if (load_tex_cursor(brush, vc, zoom)) {
 
646
                glEnable(GL_BLEND);
 
647
 
 
648
                glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 
649
                glDepthMask(GL_FALSE);
 
650
                glDepthFunc(GL_ALWAYS);
 
651
 
 
652
                /* scale based on tablet pressure */
 
653
                if (ups->draw_pressure && BKE_brush_use_size_pressure(vc->scene, brush)) {
 
654
                        glTranslatef(0.5f, 0.5f, 0);
 
655
                        glScalef(1.0f / ups->pressure_value, 1.0f / ups->pressure_value, 1);
 
656
                        glTranslatef(-0.5f, -0.5f, 0);
 
657
                }
 
658
 
 
659
                if (ups->draw_anchored) {
 
660
                        const float *aim = ups->anchored_initial_mouse;
 
661
                        quad.xmin = aim[0] - ups->anchored_size;
 
662
                        quad.ymin = aim[1] - ups->anchored_size;
 
663
                        quad.xmax = aim[0] + ups->anchored_size;
 
664
                        quad.ymax = aim[1] + ups->anchored_size;
 
665
                }
 
666
                else {
 
667
                        const int radius = BKE_brush_size_get(vc->scene, brush) * zoom;
 
668
                        quad.xmin = x - radius;
 
669
                        quad.ymin = y - radius;
 
670
                        quad.xmax = x + radius;
 
671
                        quad.ymax = y + radius;
 
672
                }
 
673
 
459
674
                glColor4f(U.sculpt_paint_overlay_col[0],
460
 
                          U.sculpt_paint_overlay_col[1],
461
 
                          U.sculpt_paint_overlay_col[2],
462
 
                          brush->texture_overlay_alpha / 100.0f);
 
675
                        U.sculpt_paint_overlay_col[1],
 
676
                        U.sculpt_paint_overlay_col[2],
 
677
                        brush->cursor_overlay_alpha / 100.0f);
463
678
 
464
679
                /* draw textured quad */
465
680
                glBegin(GL_QUADS);
472
687
                glTexCoord2f(0, 1);
473
688
                glVertex2f(quad.xmin, quad.ymax);
474
689
                glEnd();
475
 
 
476
 
                glPopMatrix();
 
690
        }
 
691
}
 
692
 
 
693
static void paint_draw_alpha_overlay(UnifiedPaintSettings *ups, Brush *brush,
 
694
                                     ViewContext *vc, int x, int y, float zoom, PaintMode mode)
 
695
{
 
696
        /* color means that primary brush texture is colured and secondary is used for alpha/mask control */
 
697
        bool col = ELEM3(mode, PAINT_TEXTURE_PROJECTIVE, PAINT_TEXTURE_2D, PAINT_VERTEX) ? true: false;
 
698
        OverlayControlFlags flags = BKE_paint_get_overlay_flags();
 
699
        /* save lots of GL state
 
700
         * TODO: check on whether all of these are needed? */
 
701
        glPushAttrib(GL_COLOR_BUFFER_BIT |
 
702
                     GL_CURRENT_BIT |
 
703
                     GL_DEPTH_BUFFER_BIT |
 
704
                     GL_ENABLE_BIT |
 
705
                     GL_LINE_BIT |
 
706
                     GL_POLYGON_BIT |
 
707
                     GL_STENCIL_BUFFER_BIT |
 
708
                     GL_TRANSFORM_BIT |
 
709
                     GL_VIEWPORT_BIT |
 
710
                     GL_TEXTURE_BIT);
 
711
 
 
712
 
 
713
        /* coloured overlay should be drawn separately */
 
714
        if (col) {
 
715
                if (!(flags & PAINT_OVERLAY_OVERRIDE_PRIMARY))
 
716
                        paint_draw_tex_overlay(ups, brush, vc, x, y, zoom, true, true);
 
717
                if (!(flags & PAINT_OVERLAY_OVERRIDE_SECONDARY))
 
718
                        paint_draw_tex_overlay(ups, brush, vc, x, y, zoom, false, false);
 
719
                if (!(flags & PAINT_OVERLAY_OVERRIDE_CURSOR))
 
720
                        paint_draw_cursor_overlay(ups, brush, vc, x, y, zoom);
 
721
        }
 
722
        else {
 
723
                if (!(flags & PAINT_OVERLAY_OVERRIDE_PRIMARY))
 
724
                        paint_draw_tex_overlay(ups, brush, vc, x, y, zoom, false, true);
 
725
                if (!(flags & PAINT_OVERLAY_OVERRIDE_CURSOR))
 
726
                        paint_draw_cursor_overlay(ups, brush, vc, x, y, zoom);
477
727
        }
478
728
 
479
729
        glPopAttrib();
515
765
{
516
766
        Scene *scene = CTX_data_scene(C);
517
767
        UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
518
 
        Paint *paint = paint_get_active_from_context(C);
519
 
        Brush *brush = paint_brush(paint);
 
768
        Paint *paint = BKE_paint_get_active_from_context(C);
 
769
        Brush *brush = BKE_paint_brush(paint);
520
770
        ViewContext vc;
 
771
        PaintMode mode;
521
772
        float final_radius;
522
773
        float translation[2];
523
774
        float outline_alpha, *outline_col;
 
775
        float zoomx, zoomy;
524
776
        
 
777
        /* check that brush drawing is enabled */
 
778
        if (!(paint->flags & PAINT_SHOW_BRUSH))
 
779
                return;
 
780
 
 
781
        /* can't use stroke vc here because this will be called during
 
782
         * mouse over too, not just during a stroke */
 
783
        view3d_set_viewcontext(C, &vc);
 
784
 
 
785
        get_imapaint_zoom(C, &zoomx, &zoomy);
 
786
        zoomx = max_ff(zoomx, zoomy);
 
787
        mode = BKE_paintmode_get_active_from_context(C);
 
788
 
525
789
        /* set various defaults */
526
790
        translation[0] = x;
527
791
        translation[1] = y;
528
792
        outline_alpha = 0.5;
529
793
        outline_col = brush->add_col;
530
 
        final_radius = BKE_brush_size_get(scene, brush);
531
 
 
532
 
        /* check that brush drawing is enabled */
533
 
        if (!(paint->flags & PAINT_SHOW_BRUSH))
534
 
                return;
535
 
 
536
 
        /* can't use stroke vc here because this will be called during
537
 
         * mouse over too, not just during a stroke */
538
 
        view3d_set_viewcontext(C, &vc);
 
794
        final_radius = BKE_brush_size_get(scene, brush) * zoomx;
 
795
 
 
796
        if (brush->flag & BRUSH_RAKE)
 
797
                /* here, translation contains the mouse coordinates. */
 
798
                paint_calculate_rake_rotation(ups, translation);
 
799
        else if (!(brush->flag & BRUSH_ANCHORED))
 
800
                ups->brush_rotation = 0.0;
 
801
 
 
802
        /* draw overlay */
 
803
        paint_draw_alpha_overlay(ups, brush, &vc, x, y, zoomx, mode);
539
804
 
540
805
        /* TODO: as sculpt and other paint modes are unified, this
541
806
         * special mode of drawing will go away */
542
 
        if (vc.obact->sculpt) {
 
807
        if ((mode == PAINT_SCULPT) && vc.obact->sculpt) {
543
808
                float location[3];
544
809
                int pixel_radius, hit;
545
810
 
546
 
                /* this is probably here so that rake takes into
547
 
                 * account the brush movements before the stroke
548
 
                 * starts, but this doesn't really belong in draw code
549
 
                 *  TODO) */
550
 
                {
551
 
                        const float u = 0.5f;
552
 
                        const float v = 1 - u;
553
 
                        const float r = 20;
554
 
 
555
 
                        const float dx = ups->last_x - x;
556
 
                        const float dy = ups->last_y - y;
557
 
 
558
 
                        if (dx * dx + dy * dy >= r * r) {
559
 
                                ups->last_angle = atan2(dx, dy);
560
 
 
561
 
                                ups->last_x = u * ups->last_x + v * x;
562
 
                                ups->last_y = u * ups->last_y + v * y;
563
 
                        }
564
 
                }
565
 
 
566
811
                /* test if brush is over the mesh */
567
812
                hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
568
813
 
569
 
                /* draw overlay */
570
 
                paint_draw_alpha_overlay(ups, brush, &vc, x, y);
571
 
 
572
814
                if (BKE_brush_use_locked_size(scene, brush))
573
815
                        BKE_brush_size_set(scene, brush, pixel_radius);
574
816
 
590
832
 
591
833
                if (ups->draw_anchored) {
592
834
                        final_radius = ups->anchored_size;
593
 
                        translation[0] = ups->anchored_initial_mouse[0] - vc.ar->winrct.xmin;
594
 
                        translation[1] = ups->anchored_initial_mouse[1] - vc.ar->winrct.ymin;
 
835
                        translation[0] = ups->anchored_initial_mouse[0];
 
836
                        translation[1] = ups->anchored_initial_mouse[1];
595
837
                }
596
838
        }
597
839
 
624
866
 
625
867
void paint_cursor_start(bContext *C, int (*poll)(bContext *C))
626
868
{
627
 
        Paint *p = paint_get_active_from_context(C);
 
869
        Paint *p = BKE_paint_get_active_from_context(C);
628
870
 
629
871
        if (p && !p->paint_cursor)
630
872
                p->paint_cursor = WM_paint_cursor_activate(CTX_wm_manager(C), poll, paint_draw_cursor, NULL);
 
873
 
 
874
        /* invalidate the paint cursors */
 
875
        BKE_paint_invalidate_overlay_all();
 
876
}
 
877
 
 
878
void paint_cursor_start_explicit(Paint *p, wmWindowManager *wm, int (*poll)(bContext *C))
 
879
{
 
880
        if (p && !p->paint_cursor)
 
881
                p->paint_cursor = WM_paint_cursor_activate(wm, poll, paint_draw_cursor, NULL);
631
882
}