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

« back to all changes in this revision

Viewing changes to source/blender/editors/sculpt_paint/paint_image_2d.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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) 2001-2002 by NaN Holding BV.
 
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/editors/sculpt_paint/paint_image_2d.c
 
29
 *  \ingroup bke
 
30
 */
 
31
//#include <math.h>
 
32
#include <string.h>
 
33
 
 
34
#include "MEM_guardedalloc.h"
 
35
 
 
36
#include "DNA_brush_types.h"
 
37
#include "DNA_scene_types.h"
 
38
 
 
39
#include "BKE_brush.h"
 
40
 
 
41
#include "BLI_math.h"
 
42
 
 
43
#include "IMB_imbuf.h"
 
44
#include "IMB_imbuf_types.h"
 
45
 
 
46
#include "RE_shader_ext.h"
 
47
 
 
48
 /* Brush Painting for 2D image editor */
 
49
 
 
50
typedef struct BrushPainterCache {
 
51
        short enabled;
 
52
 
 
53
        int size;           /* size override, if 0 uses 2*BKE_brush_size_get(brush) */
 
54
        short flt;          /* need float imbuf? */
 
55
        short texonly;      /* no alpha, color or fallof, only texture in imbuf */
 
56
 
 
57
        int lastsize;
 
58
        float lastalpha;
 
59
        float lastjitter;
 
60
 
 
61
        ImBuf *ibuf;
 
62
        ImBuf *texibuf;
 
63
        ImBuf *maskibuf;
 
64
} BrushPainterCache;
 
65
 
 
66
struct BrushPainter {
 
67
        Scene *scene;
 
68
        Brush *brush;
 
69
 
 
70
        float lastmousepos[2];  /* mouse position of last paint call */
 
71
 
 
72
        float accumdistance;    /* accumulated distance of brush since last paint op */
 
73
        float lastpaintpos[2];  /* position of last paint op */
 
74
        float startpaintpos[2]; /* position of first paint */
 
75
 
 
76
        double accumtime;       /* accumulated time since last paint op (airbrush) */
 
77
        double lasttime;        /* time of last update */
 
78
 
 
79
        float lastpressure;
 
80
 
 
81
        short firsttouch;       /* first paint op */
 
82
 
 
83
        float startsize;
 
84
        float startalpha;
 
85
        float startjitter;
 
86
        float startspacing;
 
87
 
 
88
        BrushPainterCache cache;
 
89
};
 
90
 
 
91
BrushPainter *BKE_brush_painter_new(Scene *scene, Brush *brush)
 
92
{
 
93
        BrushPainter *painter = MEM_callocN(sizeof(BrushPainter), "BrushPainter");
 
94
 
 
95
        painter->brush = brush;
 
96
        painter->scene = scene;
 
97
        painter->firsttouch = 1;
 
98
        painter->cache.lastsize = -1; /* force ibuf create in refresh */
 
99
 
 
100
        painter->startsize = BKE_brush_size_get(scene, brush);
 
101
        painter->startalpha = BKE_brush_alpha_get(scene, brush);
 
102
        painter->startjitter = brush->jitter;
 
103
        painter->startspacing = brush->spacing;
 
104
 
 
105
        return painter;
 
106
}
 
107
 
 
108
 
 
109
static void brush_pressure_apply(BrushPainter *painter, Brush *brush, float pressure)
 
110
{
 
111
        if (BKE_brush_use_alpha_pressure(painter->scene, brush))
 
112
                BKE_brush_alpha_set(painter->scene, brush, max_ff(0.0f, painter->startalpha * pressure));
 
113
        if (BKE_brush_use_size_pressure(painter->scene, brush))
 
114
                BKE_brush_size_set(painter->scene, brush, max_ff(1.0f, painter->startsize * pressure));
 
115
        if (brush->flag & BRUSH_JITTER_PRESSURE)
 
116
                brush->jitter = max_ff(0.0f, painter->startjitter * pressure);
 
117
        if (brush->flag & BRUSH_SPACING_PRESSURE)
 
118
                brush->spacing = max_ff(1.0f, painter->startspacing * (1.5f - pressure));
 
119
}
 
120
 
 
121
 
 
122
void BKE_brush_painter_require_imbuf(BrushPainter *painter, short flt, short texonly, int size)
 
123
{
 
124
        if ((painter->cache.flt != flt) || (painter->cache.size != size) ||
 
125
            ((painter->cache.texonly != texonly) && texonly))
 
126
        {
 
127
                if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
 
128
                if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
 
129
                painter->cache.ibuf = painter->cache.maskibuf = NULL;
 
130
                painter->cache.lastsize = -1; /* force ibuf create in refresh */
 
131
        }
 
132
 
 
133
        if (painter->cache.flt != flt) {
 
134
                if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
 
135
                painter->cache.texibuf = NULL;
 
136
                painter->cache.lastsize = -1; /* force ibuf create in refresh */
 
137
        }
 
138
 
 
139
        painter->cache.size = size;
 
140
        painter->cache.flt = flt;
 
141
        painter->cache.texonly = texonly;
 
142
        painter->cache.enabled = 1;
 
143
}
 
144
 
 
145
void BKE_brush_painter_free(BrushPainter *painter)
 
146
{
 
147
        Brush *brush = painter->brush;
 
148
 
 
149
        BKE_brush_size_set(painter->scene, brush, painter->startsize);
 
150
        BKE_brush_alpha_set(painter->scene, brush, painter->startalpha);
 
151
        brush->jitter = painter->startjitter;
 
152
        brush->spacing = painter->startspacing;
 
153
 
 
154
        if (painter->cache.ibuf) IMB_freeImBuf(painter->cache.ibuf);
 
155
        if (painter->cache.texibuf) IMB_freeImBuf(painter->cache.texibuf);
 
156
        if (painter->cache.maskibuf) IMB_freeImBuf(painter->cache.maskibuf);
 
157
        MEM_freeN(painter);
 
158
}
 
159
 
 
160
static void brush_painter_do_partial(BrushPainter *painter, ImBuf *oldtexibuf,
 
161
                                     int x, int y, int w, int h, int xt, int yt,
 
162
                                     const float pos[2])
 
163
{
 
164
        Scene *scene = painter->scene;
 
165
        Brush *brush = painter->brush;
 
166
        ImBuf *ibuf, *maskibuf, *texibuf;
 
167
        float *bf, *mf, *tf, *otf = NULL, xoff, yoff, xy[2], rgba[4];
 
168
        unsigned char *b, *m, *t, *ot = NULL;
 
169
        int dotexold, origx = x, origy = y;
 
170
        const int radius = BKE_brush_size_get(painter->scene, brush);
 
171
 
 
172
        xoff = -radius + 0.5f;
 
173
        yoff = -radius + 0.5f;
 
174
        xoff += (int)pos[0] - (int)painter->startpaintpos[0];
 
175
        yoff += (int)pos[1] - (int)painter->startpaintpos[1];
 
176
 
 
177
        ibuf = painter->cache.ibuf;
 
178
        texibuf = painter->cache.texibuf;
 
179
        maskibuf = painter->cache.maskibuf;
 
180
 
 
181
        dotexold = (oldtexibuf != NULL);
 
182
 
 
183
        /* not sure if it's actually needed or it's a mistake in coords/sizes
 
184
         * calculation in brush_painter_fixed_tex_partial_update(), but without this
 
185
         * limitation memory gets corrupted at fast strokes with quite big spacing (sergey) */
 
186
        w = min_ii(w, ibuf->x);
 
187
        h = min_ii(h, ibuf->y);
 
188
 
 
189
        if (painter->cache.flt) {
 
190
                for (; y < h; y++) {
 
191
                        bf = ibuf->rect_float + (y * ibuf->x + origx) * 4;
 
192
                        tf = texibuf->rect_float + (y * texibuf->x + origx) * 4;
 
193
                        mf = maskibuf->rect_float + (y * maskibuf->x + origx) * 4;
 
194
 
 
195
                        if (dotexold)
 
196
                                otf = oldtexibuf->rect_float + ((y - origy + yt) * oldtexibuf->x + xt) * 4;
 
197
 
 
198
                        for (x = origx; x < w; x++, bf += 4, mf += 4, tf += 4) {
 
199
                                if (dotexold) {
 
200
                                        copy_v4_v4(tf, otf);
 
201
                                        otf += 4;
 
202
                                }
 
203
                                else {
 
204
                                        xy[0] = x + xoff;
 
205
                                        xy[1] = y + yoff;
 
206
 
 
207
                                        BKE_brush_sample_tex_2D(scene, brush, xy, tf, 0);
 
208
                                }
 
209
 
 
210
                                bf[0] = tf[0] * mf[0];
 
211
                                bf[1] = tf[1] * mf[1];
 
212
                                bf[2] = tf[2] * mf[2];
 
213
                                bf[3] = tf[3] * mf[3];
 
214
                        }
 
215
                }
 
216
        }
 
217
        else {
 
218
                for (; y < h; y++) {
 
219
                        b = (unsigned char *)ibuf->rect + (y * ibuf->x + origx) * 4;
 
220
                        t = (unsigned char *)texibuf->rect + (y * texibuf->x + origx) * 4;
 
221
                        m = (unsigned char *)maskibuf->rect + (y * maskibuf->x + origx) * 4;
 
222
 
 
223
                        if (dotexold)
 
224
                                ot = (unsigned char *)oldtexibuf->rect + ((y - origy + yt) * oldtexibuf->x + xt) * 4;
 
225
 
 
226
                        for (x = origx; x < w; x++, b += 4, m += 4, t += 4) {
 
227
                                if (dotexold) {
 
228
                                        t[0] = ot[0];
 
229
                                        t[1] = ot[1];
 
230
                                        t[2] = ot[2];
 
231
                                        t[3] = ot[3];
 
232
                                        ot += 4;
 
233
                                }
 
234
                                else {
 
235
                                        xy[0] = x + xoff;
 
236
                                        xy[1] = y + yoff;
 
237
 
 
238
                                        BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
 
239
                                        rgba_float_to_uchar(t, rgba);
 
240
                                }
 
241
 
 
242
                                b[0] = t[0] * m[0] / 255;
 
243
                                b[1] = t[1] * m[1] / 255;
 
244
                                b[2] = t[2] * m[2] / 255;
 
245
                                b[3] = t[3] * m[3] / 255;
 
246
                        }
 
247
                }
 
248
        }
 
249
}
 
250
 
 
251
static void brush_painter_tiled_tex_partial_update(BrushPainter *painter, const float pos[2])
 
252
{
 
253
        const Scene *scene = painter->scene;
 
254
        Brush *brush = painter->brush;
 
255
        BrushPainterCache *cache = &painter->cache;
 
256
        ImBuf *oldtexibuf, *ibuf;
 
257
        int imbflag, destx, desty, srcx, srcy, w, h, x1, y1, x2, y2;
 
258
        const int diameter = 2 * BKE_brush_size_get(scene, brush);
 
259
 
 
260
        imbflag = (cache->flt) ? IB_rectfloat : IB_rect;
 
261
        if (!cache->ibuf)
 
262
                cache->ibuf = IMB_allocImBuf(diameter, diameter, 32, imbflag);
 
263
        ibuf = cache->ibuf;
 
264
 
 
265
        oldtexibuf = cache->texibuf;
 
266
        cache->texibuf = IMB_allocImBuf(diameter, diameter, 32, imbflag);
 
267
        if (oldtexibuf) {
 
268
                srcx = srcy = 0;
 
269
                destx = (int)painter->lastpaintpos[0] - (int)pos[0];
 
270
                desty = (int)painter->lastpaintpos[1] - (int)pos[1];
 
271
                w = oldtexibuf->x;
 
272
                h = oldtexibuf->y;
 
273
 
 
274
                IMB_rectclip(cache->texibuf, oldtexibuf, &destx, &desty, &srcx, &srcy, &w, &h);
 
275
        }
 
276
        else {
 
277
                srcx = srcy = 0;
 
278
                destx = desty = 0;
 
279
                w = h = 0;
 
280
        }
 
281
        
 
282
        x1 = destx;
 
283
        y1 = desty;
 
284
        x2 = destx + w;
 
285
        y2 = desty + h;
 
286
 
 
287
        /* blend existing texture in new position */
 
288
        if ((x1 < x2) && (y1 < y2))
 
289
                brush_painter_do_partial(painter, oldtexibuf, x1, y1, x2, y2, srcx, srcy, pos);
 
290
 
 
291
        if (oldtexibuf)
 
292
                IMB_freeImBuf(oldtexibuf);
 
293
 
 
294
        /* sample texture in new areas */
 
295
        if ((0 < x1) && (0 < ibuf->y))
 
296
                brush_painter_do_partial(painter, NULL, 0, 0, x1, ibuf->y, 0, 0, pos);
 
297
        if ((x2 < ibuf->x) && (0 < ibuf->y))
 
298
                brush_painter_do_partial(painter, NULL, x2, 0, ibuf->x, ibuf->y, 0, 0, pos);
 
299
        if ((x1 < x2) && (0 < y1))
 
300
                brush_painter_do_partial(painter, NULL, x1, 0, x2, y1, 0, 0, pos);
 
301
        if ((x1 < x2) && (y2 < ibuf->y))
 
302
                brush_painter_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
 
303
}
 
304
 
 
305
static void brush_painter_refresh_cache(BrushPainter *painter, const float pos[2], int use_color_correction)
 
306
{
 
307
        const Scene *scene = painter->scene;
 
308
        Brush *brush = painter->brush;
 
309
        BrushPainterCache *cache = &painter->cache;
 
310
        MTex *mtex = &brush->mtex;
 
311
        int size;
 
312
        short flt;
 
313
        const int diameter = 2 * BKE_brush_size_get(scene, brush);
 
314
        const float alpha = BKE_brush_alpha_get(scene, brush);
 
315
        const bool do_tiled = ELEM(brush->mtex.brush_map_mode, MTEX_MAP_MODE_TILED, MTEX_MAP_MODE_3D);
 
316
 
 
317
        if (diameter != cache->lastsize ||
 
318
            alpha != cache->lastalpha ||
 
319
            brush->jitter != cache->lastjitter)
 
320
        {
 
321
                if (cache->ibuf) {
 
322
                        IMB_freeImBuf(cache->ibuf);
 
323
                        cache->ibuf = NULL;
 
324
                }
 
325
                if (cache->maskibuf) {
 
326
                        IMB_freeImBuf(cache->maskibuf);
 
327
                        cache->maskibuf = NULL;
 
328
                }
 
329
 
 
330
                flt = cache->flt;
 
331
                size = (cache->size) ? cache->size : diameter;
 
332
 
 
333
                if (do_tiled) {
 
334
                        BKE_brush_imbuf_new(scene, brush, flt, 3, size, &cache->maskibuf, use_color_correction);
 
335
                        brush_painter_tiled_tex_partial_update(painter, pos);
 
336
                }
 
337
                else
 
338
                        BKE_brush_imbuf_new(scene, brush, flt, 2, size, &cache->ibuf, use_color_correction);
 
339
 
 
340
                cache->lastsize = diameter;
 
341
                cache->lastalpha = alpha;
 
342
                cache->lastjitter = brush->jitter;
 
343
        }
 
344
        else if (do_tiled && mtex && mtex->tex) {
 
345
                int dx = (int)painter->lastpaintpos[0] - (int)pos[0];
 
346
                int dy = (int)painter->lastpaintpos[1] - (int)pos[1];
 
347
 
 
348
                if ((dx != 0) || (dy != 0))
 
349
                        brush_painter_tiled_tex_partial_update(painter, pos);
 
350
        }
 
351
}
 
352
 
 
353
void BKE_brush_painter_break_stroke(BrushPainter *painter)
 
354
{
 
355
        painter->firsttouch = 1;
 
356
}
 
357
 
 
358
 
 
359
int BKE_brush_painter_paint(BrushPainter *painter, BrushFunc func, const float pos[2], double time, float pressure,
 
360
                            void *user, int use_color_correction)
 
361
{
 
362
        Scene *scene = painter->scene;
 
363
        Brush *brush = painter->brush;
 
364
        int totpaintops = 0;
 
365
 
 
366
        if (pressure == 0.0f) {
 
367
                if (painter->lastpressure) // XXX - hack, operator misses
 
368
                        pressure = painter->lastpressure;
 
369
                else
 
370
                        pressure = 1.0f;    /* zero pressure == not using tablet */
 
371
        }
 
372
        if (painter->firsttouch) {
 
373
                /* paint exactly once on first touch */
 
374
                painter->startpaintpos[0] = pos[0];
 
375
                painter->startpaintpos[1] = pos[1];
 
376
 
 
377
                brush_pressure_apply(painter, brush, pressure);
 
378
                if (painter->cache.enabled)
 
379
                        brush_painter_refresh_cache(painter, pos, use_color_correction);
 
380
                totpaintops += func(user, painter->cache.ibuf, pos, pos);
 
381
                
 
382
                painter->lasttime = time;
 
383
                painter->firsttouch = 0;
 
384
                painter->lastpaintpos[0] = pos[0];
 
385
                painter->lastpaintpos[1] = pos[1];
 
386
        }
 
387
#if 0
 
388
        else if (painter->brush->flag & BRUSH_AIRBRUSH) {
 
389
                float spacing, step, paintpos[2], dmousepos[2], len;
 
390
                double starttime, curtime = time;
 
391
 
 
392
                /* compute brush spacing adapted to brush size */
 
393
                spacing = brush->rate; //radius*brush->spacing * 0.01f;
 
394
 
 
395
                /* setup starting time, direction vector and accumulated time */
 
396
                starttime = painter->accumtime;
 
397
                sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
 
398
                len = normalize_v2(dmousepos);
 
399
                painter->accumtime += curtime - painter->lasttime;
 
400
 
 
401
                /* do paint op over unpainted time distance */
 
402
                while (painter->accumtime >= spacing) {
 
403
                        step = (spacing - starttime) * len;
 
404
                        paintpos[0] = painter->lastmousepos[0] + dmousepos[0] * step;
 
405
                        paintpos[1] = painter->lastmousepos[1] + dmousepos[1] * step;
 
406
 
 
407
                        if (painter->cache.enabled)
 
408
                                brush_painter_refresh_cache(painter);
 
409
                        totpaintops += func(user, painter->cache.ibuf,
 
410
                                            painter->lastpaintpos, paintpos);
 
411
 
 
412
                        painter->lastpaintpos[0] = paintpos[0];
 
413
                        painter->lastpaintpos[1] = paintpos[1];
 
414
                        painter->accumtime -= spacing;
 
415
                        starttime -= spacing;
 
416
                }
 
417
                
 
418
                painter->lasttime = curtime;
 
419
        }
 
420
#endif
 
421
        else {
 
422
                float startdistance, spacing, step, paintpos[2], dmousepos[2], finalpos[2];
 
423
                float t, len, press;
 
424
                const int radius = BKE_brush_size_get(scene, brush);
 
425
 
 
426
                /* compute brush spacing adapted to brush radius, spacing may depend
 
427
                 * on pressure, so update it */
 
428
                brush_pressure_apply(painter, brush, painter->lastpressure);
 
429
                spacing = max_ff(1.0f, radius) * brush->spacing * 0.01f;
 
430
 
 
431
                /* setup starting distance, direction vector and accumulated distance */
 
432
                startdistance = painter->accumdistance;
 
433
                sub_v2_v2v2(dmousepos, pos, painter->lastmousepos);
 
434
                len = normalize_v2(dmousepos);
 
435
                painter->accumdistance += len;
 
436
 
 
437
                if (brush->flag & BRUSH_SPACE) {
 
438
                        /* do paint op over unpainted distance */
 
439
                        while ((len > 0.0f) && (painter->accumdistance >= spacing)) {
 
440
                                step = spacing - startdistance;
 
441
                                paintpos[0] = painter->lastmousepos[0] + dmousepos[0] * step;
 
442
                                paintpos[1] = painter->lastmousepos[1] + dmousepos[1] * step;
 
443
 
 
444
                                t = step / len;
 
445
                                press = (1.0f - t) * painter->lastpressure + t * pressure;
 
446
                                brush_pressure_apply(painter, brush, press);
 
447
                                spacing = max_ff(1.0f, radius) * brush->spacing * 0.01f;
 
448
 
 
449
                                BKE_brush_jitter_pos(scene, brush, paintpos, finalpos);
 
450
 
 
451
                                if (painter->cache.enabled)
 
452
                                        brush_painter_refresh_cache(painter, finalpos, use_color_correction);
 
453
 
 
454
                                totpaintops +=
 
455
                                    func(user, painter->cache.ibuf, painter->lastpaintpos, finalpos);
 
456
 
 
457
                                painter->lastpaintpos[0] = paintpos[0];
 
458
                                painter->lastpaintpos[1] = paintpos[1];
 
459
                                painter->accumdistance -= spacing;
 
460
                                startdistance -= spacing;
 
461
                        }
 
462
                }
 
463
                else {
 
464
                        BKE_brush_jitter_pos(scene, brush, pos, finalpos);
 
465
 
 
466
                        if (painter->cache.enabled)
 
467
                                brush_painter_refresh_cache(painter, finalpos, use_color_correction);
 
468
 
 
469
                        totpaintops += func(user, painter->cache.ibuf, pos, finalpos);
 
470
 
 
471
                        painter->lastpaintpos[0] = pos[0];
 
472
                        painter->lastpaintpos[1] = pos[1];
 
473
                        painter->accumdistance = 0;
 
474
                }
 
475
 
 
476
                /* do airbrush paint ops, based on the number of paint ops left over
 
477
                 * from regular painting. this is a temporary solution until we have
 
478
                 * accurate time stamps for mouse move events */
 
479
                if (brush->flag & BRUSH_AIRBRUSH) {
 
480
                        double curtime = time;
 
481
                        double painttime = brush->rate * totpaintops;
 
482
 
 
483
                        painter->accumtime += curtime - painter->lasttime;
 
484
                        if (painter->accumtime <= painttime)
 
485
                                painter->accumtime = 0.0;
 
486
                        else
 
487
                                painter->accumtime -= painttime;
 
488
 
 
489
                        while (painter->accumtime >= (double)brush->rate) {
 
490
                                brush_pressure_apply(painter, brush, pressure);
 
491
 
 
492
                                BKE_brush_jitter_pos(scene, brush, pos, finalpos);
 
493
 
 
494
                                if (painter->cache.enabled)
 
495
                                        brush_painter_refresh_cache(painter, finalpos, use_color_correction);
 
496
 
 
497
                                totpaintops +=
 
498
                                    func(user, painter->cache.ibuf, painter->lastmousepos, finalpos);
 
499
                                painter->accumtime -= (double)brush->rate;
 
500
                        }
 
501
 
 
502
                        painter->lasttime = curtime;
 
503
                }
 
504
        }
 
505
 
 
506
        painter->lastmousepos[0] = pos[0];
 
507
        painter->lastmousepos[1] = pos[1];
 
508
        painter->lastpressure = pressure;
 
509
 
 
510
        BKE_brush_alpha_set(scene, brush, painter->startalpha);
 
511
        BKE_brush_size_set(scene, brush, painter->startsize);
 
512
        brush->jitter = painter->startjitter;
 
513
        brush->spacing = painter->startspacing;
 
514
 
 
515
        return totpaintops;
 
516
}
 
517
 
 
518
 
 
519
/* TODO: should probably be unified with BrushPainter stuff? */
 
520
unsigned int *BKE_brush_gen_texture_cache(Brush *br, int half_side)
 
521
{
 
522
        unsigned int *texcache = NULL;
 
523
        MTex *mtex = &br->mtex;
 
524
        TexResult texres = {0};
 
525
        int hasrgb, ix, iy;
 
526
        int side = half_side * 2;
 
527
        
 
528
        if (mtex->tex) {
 
529
                float x, y, step = 2.0 / side, co[3];
 
530
 
 
531
                texcache = MEM_callocN(sizeof(int) * side * side, "Brush texture cache");
 
532
 
 
533
                /*do normalized cannonical view coords for texture*/
 
534
                for (y = -1.0, iy = 0; iy < side; iy++, y += step) {
 
535
                        for (x = -1.0, ix = 0; ix < side; ix++, x += step) {
 
536
                                co[0] = x;
 
537
                                co[1] = y;
 
538
                                co[2] = 0.0f;
 
539
                                
 
540
                                /* This is copied from displace modifier code */
 
541
                                hasrgb = multitex_ext(mtex->tex, co, NULL, NULL, 0, &texres, NULL);
 
542
                        
 
543
                                /* if the texture gave an RGB value, we assume it didn't give a valid
 
544
                                 * intensity, so calculate one (formula from do_material_tex).
 
545
                                 * if the texture didn't give an RGB value, copy the intensity across
 
546
                                 */
 
547
                                if (hasrgb & TEX_RGB)
 
548
                                        texres.tin = rgb_to_grayscale(&texres.tr);
 
549
 
 
550
                                ((char *)texcache)[(iy * side + ix) * 4] =
 
551
                                ((char *)texcache)[(iy * side + ix) * 4 + 1] =
 
552
                                ((char *)texcache)[(iy * side + ix) * 4 + 2] =
 
553
                                ((char *)texcache)[(iy * side + ix) * 4 + 3] = (char)(texres.tin * 255.0f);
 
554
                        }
 
555
                }
 
556
        }
 
557
 
 
558
        return texcache;
 
559
}
 
560